1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverLeague\LogViewer\Tests\Forms\GridField; |
4
|
|
|
|
5
|
|
|
use SilverLeague\LogViewer\Forms\GridField\GridFieldClearAllButton; |
6
|
|
|
use SilverLeague\LogViewer\Model\LogEntry; |
7
|
|
|
use SilverStripe\Control\Controller; |
8
|
|
|
use SilverStripe\Core\Injector\Injector; |
9
|
|
|
use SilverStripe\Dev\SapphireTest; |
10
|
|
|
use SilverStripe\Forms\FieldList; |
11
|
|
|
use SilverStripe\Forms\Form; |
12
|
|
|
use SilverStripe\Forms\GridField\GridField; |
13
|
|
|
use SilverStripe\Forms\GridField\GridFieldConfig; |
14
|
|
|
use SilverStripe\ORM\DataList; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Tests for the "clear all" GridField action class |
18
|
|
|
* |
19
|
|
|
* @coversDefaultClass \SilverLeague\LogViewer\Forms\GridField\GridFieldClearAllButton |
20
|
|
|
* @package silverstripe-logviewer |
21
|
|
|
* @author Robbie Averill <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class GridFieldClearAllButtonTest extends SapphireTest |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* {@inheritDoc} |
27
|
|
|
*/ |
28
|
|
|
protected $usesDatabase = true; |
29
|
|
|
|
30
|
|
|
protected $gridField; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritDoc} |
34
|
|
|
*/ |
35
|
|
|
public function setUp() |
36
|
|
|
{ |
37
|
|
|
parent::setUp(); |
38
|
|
|
|
39
|
|
|
$config = GridFieldConfig::create()->addComponent(new GridFieldClearAllButton('before')); |
40
|
|
|
$this->gridField = GridField::create('logs', 'logs', DataList::create(LogEntry::class), $config); |
41
|
|
|
$form = Form::create( |
|
|
|
|
42
|
|
|
Controller::create(), |
43
|
|
|
'foobar', |
44
|
|
|
FieldList::create([$this->gridField]), |
45
|
|
|
FieldList::create() |
46
|
|
|
); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Return the actual class we're testing |
51
|
|
|
* |
52
|
|
|
* @return GridFieldClearAllButton |
53
|
|
|
*/ |
54
|
|
|
protected function getSubject() |
55
|
|
|
{ |
56
|
|
|
return $this->gridField->getConfig()->getComponentByType(GridFieldClearAllButton::class); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Ensure that the HTML fragment was pushed correctly and assigned to the specified fragment (in setUp above) |
61
|
|
|
*/ |
62
|
|
|
public function testGetHtmlFragments() |
63
|
|
|
{ |
64
|
|
|
$fragments = $this->getSubject()->getHTMLFragments($this->gridField); |
65
|
|
|
|
66
|
|
|
$this->assertArrayHasKey('before', $fragments); |
67
|
|
|
$this->assertContains('Clear all', $fragments['before']); |
68
|
|
|
$this->assertContains('clear-all-logs', $fragments['before']); |
69
|
|
|
$this->assertContains('font-icon-trash-bin action_clear', $fragments['before']); |
70
|
|
|
$this->assertContains('<p class="grid-clear-all-button">', $fragments['before']); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Test that the GridFieldAction actions are returned correctly |
75
|
|
|
*/ |
76
|
|
|
public function testActionsAreDefined() |
77
|
|
|
{ |
78
|
|
|
$this->assertSame(['clear'], (new GridFieldClearAllButton)->getActions($this->gridField)); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Test that an exception is thrown if the Member doesn't have permission to delete the data class assigned |
83
|
|
|
* |
84
|
|
|
* @expectedException \SilverStripe\ORM\ValidationException |
85
|
|
|
* @expectedExceptionMessage No permission to unlink record |
86
|
|
|
*/ |
87
|
|
|
public function testCannotClearAllWithoutPermission() |
88
|
|
|
{ |
89
|
|
|
$forbiddenList = DataList::create(Stub\SomeDataObject::class); |
90
|
|
|
$this->gridField->setList($forbiddenList); |
91
|
|
|
|
92
|
|
|
$this->getSubject()->handleAction($this->gridField, 'clear', null, []); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Test that with permission the list can be cleared |
97
|
|
|
*/ |
98
|
|
|
public function testClearList() |
99
|
|
|
{ |
100
|
|
|
$this->logInWithPermission('ADMIN'); |
101
|
|
|
|
102
|
|
|
$this->createDummyLogs(); |
103
|
|
|
$this->assertSame(5, $this->gridField->getList()->count()); |
104
|
|
|
|
105
|
|
|
$this->getSubject()->handleAction($this->gridField, 'clear', null, []); |
|
|
|
|
106
|
|
|
$this->assertSame(0, $this->gridField->getList()->count()); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Create a set of dummy LogEntry records |
111
|
|
|
* |
112
|
|
|
* @param int $limit |
113
|
|
|
*/ |
114
|
|
|
protected function createDummyLogs($limit = 5) |
115
|
|
|
{ |
116
|
|
|
$factory = Injector::inst()->create('FixtureFactory'); |
117
|
|
|
|
118
|
|
|
for ($i = 1; $i <= $limit; $i++) { |
119
|
|
|
$factory->createObject( |
120
|
|
|
LogEntry::class, |
121
|
|
|
'stub_log_' . $i, |
122
|
|
|
[ |
123
|
|
|
'Entry' => 'Log #' . $i, |
124
|
|
|
'Level' => 'DEBUG' |
125
|
|
|
] |
126
|
|
|
); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.