1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverLeague\LogViewer\Tests\Model; |
4
|
|
|
|
5
|
|
|
use ReflectionClass; |
6
|
|
|
use SilverLeague\LogViewer\Model\LogEntry; |
7
|
|
|
use SilverStripe\Core\Config\Config; |
8
|
|
|
use SilverStripe\Core\Convert; |
9
|
|
|
use SilverStripe\Dev\SapphireTest; |
10
|
|
|
use SilverStripe\ORM\DataObject; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @package silverstripe-logviewer |
14
|
|
|
* @author Robbie Averill <[email protected]> |
15
|
|
|
* @author Simon Erkelens <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class LogEntryTest extends SapphireTest |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Test that LogEntry classes can not be edited |
21
|
|
|
*/ |
22
|
|
|
public function testDoNotAllowEditing() |
23
|
|
|
{ |
24
|
|
|
$this->assertFalse(LogEntry::create()->canEdit()); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Test the gridfield Summary method |
29
|
|
|
*/ |
30
|
|
|
public function testGridfieldSummary() |
31
|
|
|
{ |
32
|
|
|
$shortString = 'abcdefghijklmnopqrstuvwxyz'; |
33
|
|
|
$longString = ''; |
34
|
|
|
// Generate a string of 350 characters. Because we don't care about the actual content |
35
|
|
|
// A set of a's is good enough; |
36
|
|
|
for ($i = 0; $i <= 350; $i++) { |
37
|
|
|
$longString .= 'a'; |
38
|
|
|
} |
39
|
|
|
$logEntry = LogEntry::create(); |
40
|
|
|
$logEntry->Entry = $shortString; |
41
|
|
|
$this->assertEquals($shortString, $logEntry->getGridfieldSummary()); |
42
|
|
|
$logEntry->Entry = $longString; |
43
|
|
|
$elipsisString = substr($longString, 0, 300) . '...'; |
44
|
|
|
$this->assertEquals($elipsisString, $logEntry->getGridfieldSummary()); |
45
|
|
|
$this->assertEquals($longString, $logEntry->getGridfieldSummary(351)); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Test if the fields are in the fieldlist |
50
|
|
|
* A literal field is returning null in getCMSFields. |
51
|
|
|
*/ |
52
|
|
|
public function testGetCMSFieldsFormattedEntry() |
53
|
|
|
{ |
54
|
|
|
$logEntry = LogEntry::create(array( |
55
|
|
|
'Level' => 'INFO', |
56
|
|
|
'Entry' => Convert::array2json(array('Test' => 'Message')) |
57
|
|
|
)); |
58
|
|
|
$fields = $logEntry->getCMSFields(); |
59
|
|
|
$levelField = $fields->dataFieldByName('Level'); |
60
|
|
|
$this->assertEquals("SilverStripe\\Forms\\TextField", $levelField->class); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Test if the field shows when not formatting |
65
|
|
|
*/ |
66
|
|
|
public function testGetCMSFieldsUnformattedEntry() |
67
|
|
|
{ |
68
|
|
|
Config::inst()->update('SilverLeague\LogViewer\Model\LogEntry', 'format_entry', false); |
69
|
|
|
$logEntry = LogEntry::create(array( |
70
|
|
|
'Level' => 'INFO', |
71
|
|
|
'Entry' => Convert::array2json(array('Test' => 'Message')) |
72
|
|
|
)); |
73
|
|
|
$fields = $logEntry->getCMSFields(); |
74
|
|
|
$entryField = $fields->dataFieldByName('Entry'); |
75
|
|
|
$this->assertEquals("SilverStripe\\Forms\\TextareaField", $entryField->class); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Test the entire build of a formatted entry |
80
|
|
|
*/ |
81
|
|
|
public function testBuildEntry() |
82
|
|
|
{ |
83
|
|
|
$entry = array( |
84
|
|
|
'Message' => 'Something went wrong', |
85
|
|
|
'DateTime' => '2016-01-22 03:14:23' |
86
|
|
|
); |
87
|
|
|
$logEntry = LogEntry::create(array( |
88
|
|
|
'Level' => 'INFO', |
89
|
|
|
'Entry' => Convert::array2json($entry) |
90
|
|
|
)); |
91
|
|
|
$result = $this->invokeMethod($logEntry, 'buildEntry', array()); |
92
|
|
|
$expectedResult = "<pre class='form-control-static logentry-entry' style='white-space: normal; max-width: 85%;'>". |
93
|
|
|
'<ul style="margin-bottom: 0"><li class="list-unstyled"><span style="color: #0000BB">Message</span>'. |
94
|
|
|
'<span style="color: #007700">: </span><span style="color: #DD0000">Something went wrong</span></li>'. |
95
|
|
|
'<li class="list-unstyled"><span style="color: #0000BB">DateTime</span>'. |
96
|
|
|
'<span style="color: #007700">: </span><span style="color: #DD0000">2016-01-22 03:14:23</span></li>'. |
97
|
|
|
'</ul></pre>'; |
98
|
|
|
$this->assertEquals($expectedResult, $result); |
99
|
|
|
|
100
|
|
|
} |
|
|
|
|
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Call protected/private method of a class. |
104
|
|
|
* |
105
|
|
|
* @param DataObject &$object Instantiated object that we will run method on. |
106
|
|
|
* @param string $methodName Method name to call |
107
|
|
|
* @param array $parameters Array of parameters to pass into method. |
108
|
|
|
* |
109
|
|
|
* @return mixed Method return. |
110
|
|
|
*/ |
111
|
|
View Code Duplication |
public function invokeMethod(&$object, $methodName, array $parameters = array()) |
|
|
|
|
112
|
|
|
{ |
113
|
|
|
$reflection = new ReflectionClass(get_class($object)); |
114
|
|
|
$method = $reflection->getMethod($methodName); |
115
|
|
|
$method->setAccessible(true); |
116
|
|
|
|
117
|
|
|
return $method->invokeArgs($object, $parameters); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|