1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverLeague\LogViewer\Tests\Model; |
4
|
|
|
|
5
|
|
|
use SilverLeague\LogViewer\Model\LogEntry; |
6
|
|
|
use SilverStripe\Core\Config\Config; |
7
|
|
|
use SilverStripe\Core\Convert; |
8
|
|
|
use SilverStripe\Dev\SapphireTest; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @package silverstripe-logviewer |
12
|
|
|
* @author Robbie Averill <[email protected]> |
13
|
|
|
* @author Simon Erkelens <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class LogEntryTest extends SapphireTest |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Test that LogEntry classes can not be edited |
19
|
|
|
*/ |
20
|
|
|
public function testDoNotAllowEditing() |
21
|
|
|
{ |
22
|
|
|
$this->assertFalse(LogEntry::create()->canEdit()); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Test the gridfield Summary method |
27
|
|
|
*/ |
28
|
|
|
public function testGridfieldSummary() |
29
|
|
|
{ |
30
|
|
|
$shortString = 'abcdefghijklmnopqrstuvwxyz'; |
31
|
|
|
$longString = ''; |
32
|
|
|
// Generate a string of 350 characters. Because we don't care about the actual content |
33
|
|
|
// A set of a's is good enough; |
34
|
|
|
for ($i = 0; $i <= 350; $i++) { |
35
|
|
|
$longString .= 'a'; |
36
|
|
|
} |
37
|
|
|
$logEntry = LogEntry::create(); |
38
|
|
|
$logEntry->Entry = $shortString; |
39
|
|
|
$this->assertEquals($shortString, $logEntry->getGridfieldSummary()); |
40
|
|
|
$logEntry->Entry = $longString; |
41
|
|
|
$elipsisString = substr($longString, 0, 300) . '...'; |
42
|
|
|
$this->assertEquals($elipsisString, $logEntry->getGridfieldSummary()); |
43
|
|
|
$this->assertEquals($longString, $logEntry->getGridfieldSummary(351)); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testEntryToUl() |
47
|
|
|
{ |
48
|
|
|
$entry = array( |
49
|
|
|
'Message' => 'Something went wrong', |
50
|
|
|
'DateTime' => '2016-01-22 03:14:23' |
51
|
|
|
); |
52
|
|
|
$logEntry = LogEntry::create(array( |
53
|
|
|
'Level' => 'INFO', |
54
|
|
|
'Entry' => Convert::array2json($entry) |
55
|
|
|
)); |
56
|
|
|
$result = $this->invokeMethod($logEntry, 'entryToUl', array($entry)); |
57
|
|
|
$expectedString = '[<ul style="margin-bottom: 0"><li class="list-unstyled"><span>Message => Something went' . |
58
|
|
|
' wrong</span></li><li class="list-unstyled"><span>DateTime => 2016-01-22 03:14:23</span></li></ul>]'; |
59
|
|
|
|
60
|
|
|
$this->assertEquals($expectedString, $result); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testFormatLegacyEntry() |
64
|
|
|
{ |
65
|
|
|
$arrayEntry = 'E_WARNING: Invalid argument supplied for foreach() {"code":2,"message":"Invalid argument supplied' . |
66
|
|
|
'for foreach()","file":"/data/site/docroot/logviewer/src/Model/LogEntry.php","line":89} []'; |
67
|
|
|
$logEntry = LogEntry::create(array( |
68
|
|
|
'Level' => 'INFO', |
69
|
|
|
'Entry' => $arrayEntry |
70
|
|
|
)); |
71
|
|
|
|
72
|
|
|
$result = $this->invokeMethod($logEntry, 'formatLegacyEntry', array($arrayEntry)); |
73
|
|
|
$expectedString = '<h2>Entry</h2>E_WARNING: Invalid argument supplied for foreach() => [<ul style="margin-bottom:' . |
74
|
|
|
' 0"><li class="list-unstyled"><span>code => 2</span></li><li class="list-unstyled"><span>message =>' . |
75
|
|
|
' Invalid argument suppliedfor foreach()</span></li><li class="list-unstyled"><span>file =>' . |
76
|
|
|
' /data/site/docroot/logviewer/src/Model/LogEntry.php</span></li><li class="list-unstyled"><span>line' . |
77
|
|
|
" => 89</span></li></ul>]\n" . |
78
|
|
|
'Other => [<ul style="margin-bottom: 0"></ul>]'; |
79
|
|
|
$this->assertEquals($expectedString, $result); |
80
|
|
|
|
81
|
|
|
$stringEntry = 'E_WARNING: Invalid argument supplied for foreach() {"code":2,"message":"Invalid argument supplied' . |
82
|
|
|
'for foreach()","file":"/data/site/docroot/logviewer/src/Model/LogEntry.php","line":89} OOPS'; |
83
|
|
|
$logEntry->Entry = $stringEntry; |
84
|
|
|
|
85
|
|
|
$result = $this->invokeMethod($logEntry, 'formatLegacyEntry', array($stringEntry)); |
86
|
|
|
$expectedString = '<h2>Entry</h2>E_WARNING: Invalid argument supplied for foreach() => [<ul style="margin-bottom:' . |
87
|
|
|
' 0"><li class="list-unstyled"><span>code => 2</span></li><li class="list-unstyled"><span>message =>' . |
88
|
|
|
' Invalid argument suppliedfor foreach()</span></li><li class="list-unstyled"><span>file =>' . |
89
|
|
|
' /data/site/docroot/logviewer/src/Model/LogEntry.php</span></li><li class="list-unstyled"><span>line' . |
90
|
|
|
" => 89</span></li></ul>]\n" . |
91
|
|
|
"Other:\n OOPS"; |
92
|
|
|
$this->assertEquals($result, $expectedString); |
93
|
|
|
|
94
|
|
|
$noJsonEntry = "We're making water!"; |
95
|
|
|
$logEntry->Entry = $noJsonEntry; |
96
|
|
|
$result = $this->invokeMethod($logEntry, 'formatLegacyEntry', array($noJsonEntry)); |
97
|
|
|
$this->assertEquals('<p>' . $noJsonEntry . '</p>', $result); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* A literal field is returning null in getCMSFields. |
102
|
|
|
*/ |
103
|
|
|
public function testGetCMSFieldsFormattedEntry() |
104
|
|
|
{ |
105
|
|
|
$logEntry = LogEntry::create(array( |
106
|
|
|
'Level' => 'INFO', |
107
|
|
|
'Entry' => Convert::array2json(array('Test' => 'Message')) |
108
|
|
|
)); |
109
|
|
|
$fields = $logEntry->getCMSFields(); |
110
|
|
|
$levelField = $fields->dataFieldByName('Level'); |
111
|
|
|
$this->assertEquals("SilverStripe\\Forms\\TextField", $levelField->class); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
public function testGetCMSFieldsUnformattedEntry() |
116
|
|
|
{ |
117
|
|
|
Config::inst()->update('SilverLeague\LogViewer\Model\LogEntry', 'format_entry', false); |
118
|
|
|
$logEntry = LogEntry::create(array( |
119
|
|
|
'Level' => 'INFO', |
120
|
|
|
'Entry' => Convert::array2json(array('Test' => 'Message')) |
121
|
|
|
)); |
122
|
|
|
$fields = $logEntry->getCMSFields(); |
123
|
|
|
$entryField = $fields->dataFieldByName('Entry'); |
124
|
|
|
$this->assertEquals("SilverStripe\\Forms\\TextareaField", $entryField->class); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Call protected/private method of a class. |
129
|
|
|
* |
130
|
|
|
* @param object &$object Instantiated object that we will run method on. |
131
|
|
|
* @param string $methodName Method name to call |
132
|
|
|
* @param array $parameters Array of parameters to pass into method. |
133
|
|
|
* |
134
|
|
|
* @return mixed Method return. |
135
|
|
|
*/ |
136
|
|
|
public function invokeMethod(&$object, $methodName, array $parameters = array()) |
137
|
|
|
{ |
138
|
|
|
$reflection = new \ReflectionClass(get_class($object)); |
139
|
|
|
$method = $reflection->getMethod($methodName); |
140
|
|
|
$method->setAccessible(true); |
141
|
|
|
|
142
|
|
|
return $method->invokeArgs($object, $parameters); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|