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\FixtureFactory; |
10
|
|
|
use SilverStripe\Dev\SapphireTest; |
11
|
|
|
use SilverStripe\Forms\FieldList; |
12
|
|
|
use SilverStripe\Forms\Form; |
13
|
|
|
use SilverStripe\Forms\GridField\GridField; |
14
|
|
|
use SilverStripe\Forms\GridField\GridFieldConfig; |
15
|
|
|
use SilverStripe\ORM\DataList; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Tests for the "clear all" GridField action class |
19
|
|
|
* |
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
|
|
|
/** |
31
|
|
|
* @var GridField |
32
|
|
|
*/ |
33
|
|
|
protected $gridField; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritDoc} |
37
|
|
|
*/ |
38
|
|
|
public function setUp() |
39
|
|
|
{ |
40
|
|
|
parent::setUp(); |
41
|
|
|
|
42
|
|
|
$config = GridFieldConfig::create()->addComponent(new GridFieldClearAllButton('before')); |
43
|
|
|
$this->gridField = GridField::create('logs', 'logs', DataList::create(LogEntry::class), $config); |
44
|
|
|
$form = Form::create( |
|
|
|
|
45
|
|
|
null, |
46
|
|
|
'foobar', |
47
|
|
|
FieldList::create([$this->gridField]), |
48
|
|
|
FieldList::create() |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Return the actual class we're testing |
54
|
|
|
* |
55
|
|
|
* @return GridFieldClearAllButton |
56
|
|
|
*/ |
57
|
|
|
protected function getSubject() |
58
|
|
|
{ |
59
|
|
|
return $this->gridField->getConfig()->getComponentByType(GridFieldClearAllButton::class); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Ensure that the HTML fragment was pushed correctly and assigned to the specified fragment (in setUp above) |
64
|
|
|
*/ |
65
|
|
|
public function testGetHtmlFragments() |
66
|
|
|
{ |
67
|
|
|
$fragments = $this->getSubject()->getHTMLFragments($this->gridField); |
68
|
|
|
|
69
|
|
|
$this->assertArrayHasKey('before', $fragments); |
70
|
|
|
$this->assertContains('Clear all', $fragments['before']); |
71
|
|
|
$this->assertContains('clear-all-logs', $fragments['before']); |
72
|
|
|
$this->assertContains('font-icon-trash-bin action_clear', $fragments['before']); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Test that the GridFieldAction actions are returned correctly |
77
|
|
|
*/ |
78
|
|
|
public function testActionsAreDefined() |
79
|
|
|
{ |
80
|
|
|
$this->assertSame(['clear'], (new GridFieldClearAllButton)->getActions($this->gridField)); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Test that an exception is thrown if the Member doesn't have permission to delete the data class assigned |
85
|
|
|
* |
86
|
|
|
* @expectedException \SilverStripe\ORM\ValidationException |
87
|
|
|
* @expectedExceptionMessage No permission to unlink record |
88
|
|
|
*/ |
89
|
|
|
public function testCannotClearAllWithoutPermission() |
90
|
|
|
{ |
91
|
|
|
$forbiddenList = DataList::create(Stub\SomeDataObject::class); |
92
|
|
|
$this->gridField->setList($forbiddenList); |
93
|
|
|
|
94
|
|
|
$this->getSubject()->handleAction($this->gridField, 'clear', null, []); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Test that with permission the list can be cleared |
99
|
|
|
*/ |
100
|
|
|
public function testClearList() |
101
|
|
|
{ |
102
|
|
|
$this->logInWithPermission('ADMIN'); |
103
|
|
|
|
104
|
|
|
$this->createDummyLogs(); |
105
|
|
|
$this->assertSame(5, $this->gridField->getList()->count()); |
106
|
|
|
|
107
|
|
|
$this->getSubject()->handleAction($this->gridField, 'clear', null, []); |
108
|
|
|
$this->assertSame(0, $this->gridField->getList()->count()); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Create a set of dummy LogEntry records |
113
|
|
|
* |
114
|
|
|
* @param int $limit |
115
|
|
|
*/ |
116
|
|
|
protected function createDummyLogs($limit = 5) |
117
|
|
|
{ |
118
|
|
|
$factory = Injector::inst()->create(FixtureFactory::class); |
119
|
|
|
|
120
|
|
|
for ($i = 1; $i <= $limit; $i++) { |
121
|
|
|
$factory->createObject( |
122
|
|
|
LogEntry::class, |
123
|
|
|
'stub_log_' . $i, |
124
|
|
|
[ |
125
|
|
|
'Entry' => 'Log #' . $i, |
126
|
|
|
'Level' => 'DEBUG' |
127
|
|
|
] |
128
|
|
|
); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|