|
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\Debug; |
|
9
|
|
|
use SilverStripe\Dev\SapphireTest; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @package silverstripe-logviewer |
|
13
|
|
|
* @author Robbie Averill <[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
|
|
|
static::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
|
|
|
static::assertEquals($shortString, $logEntry->getGridfieldSummary()); |
|
40
|
|
|
$logEntry->Entry = $longString; |
|
41
|
|
|
$elipsisString = substr($longString, 0, 300) . '...'; |
|
42
|
|
|
static::assertEquals($elipsisString, $logEntry->getGridfieldSummary()); |
|
43
|
|
|
static::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
|
|
|
static::assertEquals($expectedString, $result); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
View Code Duplication |
public function testFormatLegacyEntryEndArray() |
|
|
|
|
|
|
64
|
|
|
{ |
|
65
|
|
|
$entry = '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' => $entry |
|
70
|
|
|
)); |
|
71
|
|
|
$result = $this->invokeMethod($logEntry, 'formatLegacyEntry', array($entry)); |
|
72
|
|
|
$expectedString = '<h2>Entry</h2>E_WARNING: Invalid argument supplied for foreach() => [<ul style="margin-bottom:' . |
|
73
|
|
|
' 0"><li class="list-unstyled"><span>code => 2</span></li><li class="list-unstyled"><span>message =>' . |
|
74
|
|
|
' Invalid argument suppliedfor foreach()</span></li><li class="list-unstyled"><span>file =>' . |
|
75
|
|
|
' /data/site/docroot/logviewer/src/Model/LogEntry.php</span></li><li class="list-unstyled"><span>line' . |
|
76
|
|
|
" => 89</span></li></ul>]\n" . |
|
77
|
|
|
'Other => [<ul style="margin-bottom: 0"></ul>]'; |
|
78
|
|
|
static::assertEquals($expectedString, $result); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
View Code Duplication |
public function testFormatLegacyEntryEndString() |
|
|
|
|
|
|
82
|
|
|
{ |
|
83
|
|
|
$entry = 'E_WARNING: Invalid argument supplied for foreach() {"code":2,"message":"Invalid argument supplied' . |
|
84
|
|
|
'for foreach()","file":"/data/site/docroot/logviewer/src/Model/LogEntry.php","line":89} OOPS'; |
|
85
|
|
|
$logEntry = LogEntry::create(array( |
|
86
|
|
|
'Level' => 'INFO', |
|
87
|
|
|
'Entry' => $entry |
|
88
|
|
|
)); |
|
89
|
|
|
|
|
90
|
|
|
$result = $this->invokeMethod($logEntry, 'formatLegacyEntry', array($entry)); |
|
91
|
|
|
$expectedString = '<h2>Entry</h2>E_WARNING: Invalid argument supplied for foreach() => [<ul style="margin-bottom:' . |
|
92
|
|
|
' 0"><li class="list-unstyled"><span>code => 2</span></li><li class="list-unstyled"><span>message =>' . |
|
93
|
|
|
' Invalid argument suppliedfor foreach()</span></li><li class="list-unstyled"><span>file =>' . |
|
94
|
|
|
' /data/site/docroot/logviewer/src/Model/LogEntry.php</span></li><li class="list-unstyled"><span>line' . |
|
95
|
|
|
" => 89</span></li></ul>]\n" . |
|
96
|
|
|
"Other:\n OOPS"; |
|
97
|
|
|
static::assertEquals($result, $expectedString); |
|
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
|
|
|
static::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
|
|
|
static::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
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.