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
|
|
|
use SilverStripe\Security\Member; |
11
|
|
|
use SilverStripe\Security\Security; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @package silverstripe-logviewer |
15
|
|
|
* @author Robbie Averill <[email protected]> |
16
|
|
|
* @author Simon Erkelens <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class LogEntryTest extends SapphireTest |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Create a default admin, so we can test permissions |
23
|
|
|
*/ |
24
|
|
|
public function setUp() |
25
|
|
|
{ |
26
|
|
|
parent::setUp(); |
27
|
|
|
Config::inst()->update('SilverStripe\\Security\\Security', 'default_admin', 'admin'); |
28
|
|
|
Config::inst()->update('SilverStripe\\Security\\Security', 'default_password', 'admin'); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function tearDown() |
32
|
|
|
{ |
33
|
|
|
parent::tearDown(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Test the gridfield Summary method |
38
|
|
|
*/ |
39
|
|
|
public function testGridfieldSummary() |
40
|
|
|
{ |
41
|
|
|
$shortString = 'abcdefghijklmnopqrstuvwxyz'; |
42
|
|
|
$longString = ''; |
43
|
|
|
// Generate a string of 350 characters. Because we don't care about the actual content |
44
|
|
|
// A set of a's is good enough; |
45
|
|
|
for ($i = 0; $i <= 350; $i++) { |
46
|
|
|
$longString .= 'a'; |
47
|
|
|
} |
48
|
|
|
$logEntry = LogEntry::create(); |
49
|
|
|
$logEntry->Entry = $shortString; |
50
|
|
|
$this->assertEquals($shortString, $logEntry->getGridfieldSummary()); |
51
|
|
|
$logEntry->Entry = $longString; |
52
|
|
|
$elipsisString = substr($longString, 0, 300) . '...'; |
53
|
|
|
$this->assertEquals($elipsisString, $logEntry->getGridfieldSummary()); |
54
|
|
|
$this->assertEquals($longString, $logEntry->getGridfieldSummary(351)); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Test if the fields are in the fieldlist |
59
|
|
|
* A literal field is returning null in getCMSFields. |
60
|
|
|
*/ |
61
|
|
|
public function testGetCMSFieldsFormattedEntry() |
62
|
|
|
{ |
63
|
|
|
$logEntry = LogEntry::create(array( |
64
|
|
|
'Level' => 'INFO', |
65
|
|
|
'Entry' => Convert::array2json(array('Test' => 'Message')) |
66
|
|
|
)); |
67
|
|
|
$fields = $logEntry->getCMSFields(); |
68
|
|
|
$levelField = $fields->dataFieldByName('Level'); |
69
|
|
|
$this->assertEquals("SilverStripe\\Forms\\TextField", $levelField->class); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Test if the field shows when not formatting |
74
|
|
|
*/ |
75
|
|
|
public function testGetCMSFieldsUnformattedEntry() |
76
|
|
|
{ |
77
|
|
|
Config::inst()->update('SilverLeague\LogViewer\Model\LogEntry', 'format_entry', false); |
78
|
|
|
$logEntry = LogEntry::create(array( |
79
|
|
|
'Level' => 'INFO', |
80
|
|
|
'Entry' => Convert::array2json(array('Test' => 'Message')) |
81
|
|
|
)); |
82
|
|
|
$fields = $logEntry->getCMSFields(); |
83
|
|
|
$entryField = $fields->dataFieldByName('Entry'); |
84
|
|
|
$this->assertEquals("SilverStripe\\Forms\\TextareaField", $entryField->class); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Test the entire build of a formatted entry |
89
|
|
|
*/ |
90
|
|
|
public function testBuildEntry() |
91
|
|
|
{ |
92
|
|
|
$entry = array( |
93
|
|
|
'Message' => 'Something went wrong', |
94
|
|
|
'DateTime' => '2016-01-22 03:14:23' |
95
|
|
|
); |
96
|
|
|
$logEntry = LogEntry::create(array( |
97
|
|
|
'Level' => 'INFO', |
98
|
|
|
'Entry' => Convert::array2json($entry) |
99
|
|
|
)); |
100
|
|
|
$result = Invoker::invokeMethod($logEntry, 'buildEntry', array()); |
101
|
|
|
$expectedResult = "<pre class='form-control-static logentry-entry' style='white-space: normal; max-width: 85%;'>". |
102
|
|
|
'<ul style="margin-bottom: 0"><li class="list-unstyled"><span style="color: #0000BB">Message</span>'. |
103
|
|
|
'<span style="color: #007700">: </span><span style="color: #DD0000">Something went wrong</span></li>'. |
104
|
|
|
'<li class="list-unstyled"><span style="color: #0000BB">DateTime</span>'. |
105
|
|
|
'<span style="color: #007700">: </span><span style="color: #DD0000">2016-01-22 03:14:23</span></li>'. |
106
|
|
|
'</ul></pre>'; |
107
|
|
|
$this->assertEquals($expectedResult, $result); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* There's no reason to manually create, so don't allow manual creation |
112
|
|
|
*/ |
113
|
|
View Code Duplication |
public function testAllowCreate() |
|
|
|
|
114
|
|
|
{ |
115
|
|
|
$member = Member::default_admin(); |
116
|
|
|
$createTrue = LogEntry::create()->canCreate($member); |
117
|
|
|
$this->assertFalse($createTrue); |
118
|
|
|
$createFalse = LogEntry::create()->canCreate(null); |
119
|
|
|
$this->assertFalse($createFalse); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Test that LogEntry classes can not be edited |
124
|
|
|
*/ |
125
|
|
|
public function testtAllowEditing() |
126
|
|
|
{ |
127
|
|
|
$this->assertFalse(LogEntry::create()->canEdit()); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* We can view if we're logged in as admin. Otherwise, no. |
132
|
|
|
*/ |
133
|
|
View Code Duplication |
public function testAllowView() |
|
|
|
|
134
|
|
|
{ |
135
|
|
|
$member = Member::default_admin(); |
136
|
|
|
$viewTrue = LogEntry::create()->canView($member); |
137
|
|
|
$this->assertTrue($viewTrue); |
138
|
|
|
$viewFalse = LogEntry::create()->canView(null); |
139
|
|
|
$this->assertFalse($viewFalse); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* We can Delete if we're logged in as admin. Otherwise, no. |
144
|
|
|
*/ |
145
|
|
View Code Duplication |
public function testAllowDelete() |
|
|
|
|
146
|
|
|
{ |
147
|
|
|
$member = Member::default_admin(); |
148
|
|
|
$deleteTrue = LogEntry::create()->canDelete($member); |
149
|
|
|
$this->assertTrue($deleteTrue); |
150
|
|
|
$deleteFalse = LogEntry::create()->canDelete(null); |
151
|
|
|
$this->assertFalse($deleteFalse); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
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.