1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverLeague\LogViewer\Tests\Model; |
4
|
|
|
|
5
|
|
|
use SilverLeague\LogViewer\Model\LogEntry; |
6
|
|
|
use SilverLeague\LogViewer\Tests\Helper\Invoker; |
7
|
|
|
use SilverStripe\Core\Config\Config; |
8
|
|
|
use SilverStripe\Core\Convert; |
9
|
|
|
use SilverStripe\Dev\SapphireTest; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @package silverstripe-logviewer |
13
|
|
|
* @author Robbie Averill <[email protected]> |
14
|
|
|
* @author Simon Erkelens <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class LogEntryTest extends SapphireTest |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Test the gridfield Summary method |
21
|
|
|
*/ |
22
|
|
|
public function testGridfieldSummary() |
23
|
|
|
{ |
24
|
|
|
$shortString = 'abcdefghijklmnopqrstuvwxyz'; |
25
|
|
|
$longString = ''; |
26
|
|
|
// Generate a string of 350 characters. Because we don't care about the actual content |
27
|
|
|
// A set of a's is good enough; |
28
|
|
|
for ($i = 0; $i <= 350; $i++) { |
29
|
|
|
$longString .= 'a'; |
30
|
|
|
} |
31
|
|
|
$logEntry = LogEntry::create(); |
32
|
|
|
$logEntry->Entry = $shortString; |
33
|
|
|
$this->assertEquals($shortString, $logEntry->getGridfieldSummary()); |
34
|
|
|
$logEntry->Entry = $longString; |
35
|
|
|
$elipsisString = substr($longString, 0, 300) . '...'; |
36
|
|
|
$this->assertEquals($elipsisString, $logEntry->getGridfieldSummary()); |
37
|
|
|
$this->assertEquals($longString, $logEntry->getGridfieldSummary(351)); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Test if the fields are in the fieldlist |
42
|
|
|
* A literal field is returning null in getCMSFields. |
43
|
|
|
*/ |
44
|
|
|
public function testGetCMSFieldsFormattedEntry() |
45
|
|
|
{ |
46
|
|
|
$logEntry = LogEntry::create(array( |
47
|
|
|
'Level' => 'INFO', |
48
|
|
|
'Entry' => Convert::array2json(array('Test' => 'Message')) |
49
|
|
|
)); |
50
|
|
|
$fields = $logEntry->getCMSFields(); |
51
|
|
|
$levelField = $fields->dataFieldByName('Level'); |
52
|
|
|
$this->assertEquals("SilverStripe\\Forms\\TextField", $levelField->class); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Test if the field shows when not formatting |
57
|
|
|
*/ |
58
|
|
|
public function testGetCMSFieldsUnformattedEntry() |
59
|
|
|
{ |
60
|
|
|
Config::inst()->update('SilverLeague\LogViewer\Model\LogEntry', 'format_entry', false); |
61
|
|
|
$logEntry = LogEntry::create(array( |
62
|
|
|
'Level' => 'INFO', |
63
|
|
|
'Entry' => Convert::array2json(array('Test' => 'Message')) |
64
|
|
|
)); |
65
|
|
|
$fields = $logEntry->getCMSFields(); |
66
|
|
|
$entryField = $fields->dataFieldByName('Entry'); |
67
|
|
|
$this->assertEquals("SilverStripe\\Forms\\TextareaField", $entryField->class); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Test the entire build of a formatted entry |
72
|
|
|
*/ |
73
|
|
|
public function testBuildEntry() |
74
|
|
|
{ |
75
|
|
|
$entry = array( |
76
|
|
|
'Message' => 'Something went wrong', |
77
|
|
|
'DateTime' => '2016-01-22 03:14:23' |
78
|
|
|
); |
79
|
|
|
$logEntry = LogEntry::create(array( |
80
|
|
|
'Level' => 'INFO', |
81
|
|
|
'Entry' => Convert::array2json($entry) |
82
|
|
|
)); |
83
|
|
|
$result = Invoker::invokeMethod($logEntry, 'buildEntry', array()); |
84
|
|
|
$expectedResult = "<pre class='form-control-static logentry-entry' style='white-space: normal; max-width: 85%;'>". |
85
|
|
|
'<ul style="margin-bottom: 0"><li class="list-unstyled"><span style="color: #0000BB">Message</span>'. |
86
|
|
|
'<span style="color: #007700">: </span><span style="color: #DD0000">Something went wrong</span></li>'. |
87
|
|
|
'<li class="list-unstyled"><span style="color: #0000BB">DateTime</span>'. |
88
|
|
|
'<span style="color: #007700">: </span><span style="color: #DD0000">2016-01-22 03:14:23</span></li>'. |
89
|
|
|
'</ul></pre>'; |
90
|
|
|
$this->assertEquals($expectedResult, $result); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|