1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverLeague\LogViewer\Tests\Model; |
4
|
|
|
|
5
|
|
|
use SilverLeague\LogViewer\Model\LogEntry; |
6
|
|
|
use SilverStripe\Dev\SapphireTest; |
7
|
|
|
use SilverStripe\Security\Security; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @package silverstripe-logviewer |
11
|
|
|
* @author Robbie Averill <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class LogEntryTest extends SapphireTest |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Test if the Permissions are an array and contain the view and delete permissions |
17
|
|
|
*/ |
18
|
|
|
public function testProvidePermissions() |
19
|
|
|
{ |
20
|
|
|
$permissions = LogEntry::create()->providePermissions(); |
21
|
|
|
$this->assertTrue(is_array($permissions)); |
22
|
|
|
$this->assertTrue(array_key_exists('DELETE_ENTRY', $permissions)); |
23
|
|
|
$this->assertTrue(array_key_exists('VIEW_ENTRY', $permissions)); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* There's no reason to manually create, so don't allow manual creation |
28
|
|
|
*/ |
29
|
|
View Code Duplication |
public function testAllowCreate() |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
$createFalse = LogEntry::create()->canCreate(null); |
32
|
|
|
$this->assertFalse($createFalse); |
33
|
|
|
$this->logInWithPermission('ADMIN'); |
34
|
|
|
$createFalse = LogEntry::create()->canCreate(); |
35
|
|
|
$this->assertFalse($createFalse); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Test that LogEntry classes can not be edited |
40
|
|
|
*/ |
41
|
|
|
public function testAllowEditing() |
42
|
|
|
{ |
43
|
|
|
$this->assertFalse(LogEntry::create()->canEdit()); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* We can view if we're logged in as admin. Otherwise, no. |
48
|
|
|
*/ |
49
|
|
View Code Duplication |
public function testAllowView() |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
$viewFalse = LogEntry::create()->canView(null); |
52
|
|
|
$this->assertFalse($viewFalse); |
53
|
|
|
$this->logInWithPermission('ADMIN'); |
54
|
|
|
$viewTrue = LogEntry::create()->canView(); |
55
|
|
|
$this->assertTrue($viewTrue); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* We can Delete if we're logged in as admin. Otherwise, no. |
60
|
|
|
*/ |
61
|
|
View Code Duplication |
public function testAllowDelete() |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
$deleteFalse = LogEntry::create()->canDelete(null); |
64
|
|
|
$this->assertFalse($deleteFalse); |
65
|
|
|
$this->logInWithPermission('ADMIN'); |
66
|
|
|
$deleteTrue = LogEntry::create()->canDelete(); |
67
|
|
|
$this->assertTrue($deleteTrue); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
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.