1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverLeague\LogViewer\Tests\Model; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Core\Convert; |
6
|
|
|
use SilverStripe\Dev\SapphireTest; |
7
|
|
|
use SilverStripe\Forms\LiteralField; |
8
|
|
|
use SilverLeague\LogViewer\Model\LogEntry; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @package silverstripe-logviewer |
12
|
|
|
* @author Robbie Averill <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class LogEntryTest extends SapphireTest |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Test if the Permissions are an array and contain the view and delete permissions |
18
|
|
|
*/ |
19
|
|
|
public function testProvidePermissions() |
20
|
|
|
{ |
21
|
|
|
$permissions = LogEntry::create()->providePermissions(); |
22
|
|
|
$this->assertTrue(is_array($permissions)); |
23
|
|
|
$this->assertTrue(array_key_exists('DELETE_ENTRY', $permissions)); |
24
|
|
|
$this->assertTrue(array_key_exists('VIEW_ENTRY', $permissions)); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* There's no reason to manually create, so don't allow manual creation |
29
|
|
|
*/ |
30
|
|
|
public function testAllowCreate() |
31
|
|
|
{ |
32
|
|
|
$createFalse = LogEntry::create()->canCreate(null); |
33
|
|
|
$this->assertFalse($createFalse); |
34
|
|
|
$this->logInWithPermission('ADMIN'); |
35
|
|
|
$createFalse = LogEntry::create()->canCreate(); |
36
|
|
|
$this->assertFalse($createFalse); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Test that LogEntry classes can not be edited |
41
|
|
|
*/ |
42
|
|
|
public function testAllowEditing() |
43
|
|
|
{ |
44
|
|
|
$this->assertFalse(LogEntry::create()->canEdit()); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* We can view if we're logged in as admin. Otherwise, no. |
49
|
|
|
*/ |
50
|
|
View Code Duplication |
public function testAllowView() |
|
|
|
|
51
|
|
|
{ |
52
|
|
|
$this->logOut(); |
53
|
|
|
$viewFalse = LogEntry::create()->canView(null); |
54
|
|
|
$this->assertFalse($viewFalse); |
55
|
|
|
|
56
|
|
|
$this->logInWithPermission('ADMIN'); |
57
|
|
|
$viewTrue = LogEntry::create()->canView(); |
58
|
|
|
$this->assertTrue($viewTrue); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* We can Delete if we're logged in as admin. Otherwise, no. |
63
|
|
|
*/ |
64
|
|
View Code Duplication |
public function testAllowDelete() |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
$this->logOut(); |
67
|
|
|
$deleteFalse = LogEntry::create()->canDelete(null); |
68
|
|
|
$this->assertFalse($deleteFalse); |
69
|
|
|
|
70
|
|
|
$this->logInWithPermission('ADMIN'); |
71
|
|
|
$deleteTrue = LogEntry::create()->canDelete(); |
72
|
|
|
$this->assertTrue($deleteTrue); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Ensure that the contents are JSON encoded and pretty printed, and that the CSS class is correct |
77
|
|
|
*/ |
78
|
|
|
public function testLogEntriesAreFormattedAsJson() |
79
|
|
|
{ |
80
|
|
|
$data = [ |
81
|
|
|
'foo' => [ |
82
|
|
|
'bar' => 'baz' |
83
|
|
|
], |
84
|
|
|
'boo' |
85
|
|
|
]; |
86
|
|
|
|
87
|
|
|
$entry = LogEntry::create(); |
88
|
|
|
$entry->Entry = Convert::raw2json($data); |
|
|
|
|
89
|
|
|
|
90
|
|
|
$fields = $entry->getCMSFields(); |
91
|
|
|
$field = $fields->fieldByName('Root.Main.Entry'); |
92
|
|
|
|
93
|
|
|
$this->assertInstanceOf(LiteralField::class, $field); |
94
|
|
|
$this->assertContains(Convert::raw2json($data, JSON_PRETTY_PRINT), $field->getContent()); |
95
|
|
|
$this->assertContains('<pre class="logviewer-logentry-entry"><code>', $field->getContent()); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
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.