1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\Tests\Unit\CrudPanel; |
4
|
|
|
|
5
|
|
|
use Backpack\CRUD\PanelTraits\CrudButton; |
6
|
|
|
|
7
|
|
|
class CrudPanelButtonsTest extends BaseCrudPanelTest |
8
|
|
|
{ |
9
|
|
|
private $defaultButtonNames = [ |
10
|
|
|
'preview', 'update', 'revisions', 'delete', 'create', 'reorder', |
11
|
|
|
]; |
12
|
|
|
|
13
|
|
|
private $topViewButton; |
14
|
|
|
private $lineViewButton; |
15
|
|
|
private $bottomViewButton; |
16
|
|
|
private $topModelFunctionButton; |
17
|
|
|
|
18
|
|
|
protected function setUp() |
19
|
|
|
{ |
20
|
|
|
parent::setUp(); |
21
|
|
|
|
22
|
|
|
$this->topViewButton = new CrudButton('top', 'topViewButton', 'view', 'crud::buttons.preview'); |
23
|
|
|
$this->lineViewButton = new CrudButton('line', 'lineViewButton', 'view', 'crud::buttons.update'); |
24
|
|
|
$this->bottomViewButton = new CrudButton('bottom', 'bottomViewButton', 'view', 'crud::buttons.revisions'); |
25
|
|
|
|
26
|
|
|
$this->topModelFunctionButton = new CrudButton('top', 'topModelFunctionButton', 'someModelFunctionName', 'crud::buttons.preview'); |
|
|
|
|
27
|
|
|
} |
28
|
|
|
|
29
|
|
View Code Duplication |
public function testDefaultButtons() |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
$this->assertEquals(count($this->defaultButtonNames), count($this->crudPanel->buttons)); |
32
|
|
|
|
33
|
|
|
foreach ($this->crudPanel->buttons as $button) { |
34
|
|
|
$this->assertTrue(in_array($button->name, $this->defaultButtonNames)); |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
View Code Duplication |
public function testAddTopButtonTop() |
|
|
|
|
39
|
|
|
{ |
40
|
|
|
$expectedButton = $this->topViewButton; |
41
|
|
|
|
42
|
|
|
$this->crudPanel->addButton($expectedButton->stack, $expectedButton->name, $expectedButton->type, $expectedButton->content); |
|
|
|
|
43
|
|
|
|
44
|
|
|
$this->assertEquals($expectedButton, $this->crudPanel->buttons->last()); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
View Code Duplication |
public function testAddButtonLine() |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
$expectedButton = $this->lineViewButton; |
50
|
|
|
|
51
|
|
|
$this->crudPanel->addButton($expectedButton->stack, $expectedButton->name, $expectedButton->type, $expectedButton->content); |
|
|
|
|
52
|
|
|
|
53
|
|
|
$this->assertEquals($expectedButton, $this->crudPanel->buttons->first()); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
View Code Duplication |
public function testAddButtonBottom() |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
$expectedButton = $this->bottomViewButton; |
59
|
|
|
|
60
|
|
|
$this->crudPanel->addButton($expectedButton->stack, $expectedButton->name, $expectedButton->type, $expectedButton->content); |
|
|
|
|
61
|
|
|
|
62
|
|
|
$this->assertEquals($expectedButton, $this->crudPanel->buttons->last()); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
View Code Duplication |
public function testAddButtonBottomUnknownStackName() |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
$this->markTestIncomplete('Not correctly implemented'); |
68
|
|
|
|
69
|
|
|
$this->setExpectedException(\Exception::class); |
70
|
|
|
|
71
|
|
|
$expectedButton = $this->topViewButton; |
72
|
|
|
|
73
|
|
|
// TODO: this should throw an error. |
74
|
|
|
$this->crudPanel->addButton('unknownStackName', $expectedButton->name, $expectedButton->type, $expectedButton->content); |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function testAddButtonsWithSameName() |
78
|
|
|
{ |
79
|
|
|
$expectedButton = $this->topViewButton; |
80
|
|
|
|
81
|
|
|
$this->crudPanel->addButton($expectedButton->stack, $expectedButton->name, $expectedButton->type, $expectedButton->content); |
|
|
|
|
82
|
|
|
$this->crudPanel->addButton($expectedButton->stack, $expectedButton->name, $expectedButton->type, $expectedButton->content); |
|
|
|
|
83
|
|
|
|
84
|
|
|
$this->assertEquals(count($this->defaultButtonNames) + 1, count($this->crudPanel->buttons)); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function testAddButtonsWithSameNameWithoutReplacing() |
88
|
|
|
{ |
89
|
|
|
$expectedButton = $this->topViewButton; |
90
|
|
|
|
91
|
|
|
$this->crudPanel->addButton($expectedButton->stack, $expectedButton->name, $expectedButton->type, $expectedButton->content, false, false); |
|
|
|
|
92
|
|
|
$this->crudPanel->addButton($expectedButton->stack, $expectedButton->name, $expectedButton->type, $expectedButton->content, false, false); |
|
|
|
|
93
|
|
|
|
94
|
|
|
$this->assertEquals(count($this->defaultButtonNames) + 2, count($this->crudPanel->buttons)); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
View Code Duplication |
public function testAddButtonBeginning() |
|
|
|
|
98
|
|
|
{ |
99
|
|
|
$expectedButton = $this->topViewButton; |
100
|
|
|
|
101
|
|
|
$this->crudPanel->addButton($expectedButton->stack, $expectedButton->name, $expectedButton->type, $expectedButton->content, 'beginning'); |
|
|
|
|
102
|
|
|
|
103
|
|
|
$this->assertEquals($expectedButton, $this->crudPanel->buttons->first()); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
View Code Duplication |
public function testAddButtonEnd() |
|
|
|
|
107
|
|
|
{ |
108
|
|
|
$expectedButton = $this->lineViewButton; |
109
|
|
|
|
110
|
|
|
$this->crudPanel->addButton($expectedButton->stack, $expectedButton->name, $expectedButton->type, $expectedButton->content, 'end'); |
|
|
|
|
111
|
|
|
|
112
|
|
|
$this->assertEquals($expectedButton, $this->crudPanel->buttons->last()); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
View Code Duplication |
public function testAddButtonUnknownPosition() |
|
|
|
|
116
|
|
|
{ |
117
|
|
|
$this->markTestIncomplete('Not correctly implemented'); |
118
|
|
|
|
119
|
|
|
$this->setExpectedException(\Exception::class); |
120
|
|
|
|
121
|
|
|
$expectedButton = $this->lineViewButton; |
122
|
|
|
|
123
|
|
|
// TODO: this should throw an error. |
124
|
|
|
$this->crudPanel->addButton($expectedButton->stack, $expectedButton->name, $expectedButton->type, $expectedButton->content, 'unknownPosition'); |
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
127
|
|
View Code Duplication |
public function testAddButtonFromModelFunction() |
|
|
|
|
128
|
|
|
{ |
129
|
|
|
$expectedButton = $this->topModelFunctionButton; |
130
|
|
|
|
131
|
|
|
$this->crudPanel->addButton($expectedButton->stack, $expectedButton->name, $expectedButton->type, $expectedButton->content); |
|
|
|
|
132
|
|
|
|
133
|
|
|
$this->assertEquals($expectedButton, $this->crudPanel->buttons->last()); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function testAddButtonFromView() |
137
|
|
|
{ |
138
|
|
|
$expectedButton = $this->topViewButton; |
139
|
|
|
$viewName = 'someViewName'; |
140
|
|
|
|
141
|
|
|
$this->crudPanel->addButtonFromView($expectedButton->stack, $expectedButton->name, $viewName, $expectedButton->content); |
|
|
|
|
142
|
|
|
|
143
|
|
|
$backpackButtonViewPackage = 'vendor.backpack.crud.buttons.'; |
144
|
|
|
$actualButton = $this->crudPanel->buttons->last(); |
145
|
|
|
|
146
|
|
|
$this->assertEquals($expectedButton->stack, $actualButton->stack); |
147
|
|
|
$this->assertEquals($expectedButton->name, $actualButton->name); |
148
|
|
|
$this->assertEquals($backpackButtonViewPackage.$viewName, $actualButton->content); |
149
|
|
|
$this->assertEquals($expectedButton->stack, $actualButton->stack); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function testRemoveButton() |
153
|
|
|
{ |
154
|
|
|
$buttonName = $this->defaultButtonNames[0]; |
155
|
|
|
|
156
|
|
|
$this->crudPanel->removeButton($buttonName); |
157
|
|
|
|
158
|
|
|
$this->assertEquals(count($this->defaultButtonNames) - 1, count($this->crudPanel->buttons)); |
159
|
|
|
$this->assertNull($this->getButtonByName($buttonName)); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function testRemoveUnknownButton() |
163
|
|
|
{ |
164
|
|
|
$buttonName = 'someButtonName'; |
165
|
|
|
|
166
|
|
|
$this->crudPanel->removeButton($buttonName); |
167
|
|
|
|
168
|
|
|
$this->assertEquals(count($this->defaultButtonNames), count($this->crudPanel->buttons)); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function testRemoveAllButtons() |
172
|
|
|
{ |
173
|
|
|
$this->crudPanel->removeAllButtons(); |
174
|
|
|
|
175
|
|
|
$this->assertEmpty($this->crudPanel->buttons); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
public function testRemoveButtonFromStack() |
179
|
|
|
{ |
180
|
|
|
$button = $this->crudPanel->buttons->first(); |
181
|
|
|
|
182
|
|
|
$this->crudPanel->removeButtonFromStack($button->name, $button->stack); |
183
|
|
|
|
184
|
|
|
$this->assertEquals(count($this->defaultButtonNames) - 1, count($this->crudPanel->buttons)); |
185
|
|
|
$this->assertNull($this->getButtonByName($button->name)); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
public function testRemoveUnknownButtonFromStack() |
189
|
|
|
{ |
190
|
|
|
$this->crudPanel->removeButtonFromStack('someButtonName', 'line'); |
191
|
|
|
|
192
|
|
|
$this->assertEquals(count($this->defaultButtonNames), count($this->crudPanel->buttons)); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
View Code Duplication |
public function testRemoveButtonFromUnknownStack() |
|
|
|
|
196
|
|
|
{ |
197
|
|
|
$button = $this->crudPanel->buttons->first(); |
198
|
|
|
|
199
|
|
|
$this->crudPanel->removeButtonFromStack($button->name, 'someStackName'); |
200
|
|
|
|
201
|
|
|
$this->assertEquals(count($this->defaultButtonNames), count($this->crudPanel->buttons)); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
public function testRemoveAllButtonsFromStack() |
205
|
|
|
{ |
206
|
|
|
$this->crudPanel->removeAllButtonsFromStack('line'); |
207
|
|
|
|
208
|
|
|
$this->assertEquals(2, count($this->crudPanel->buttons)); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
public function testRemoveAllButtonsFromUnknownStack() |
212
|
|
|
{ |
213
|
|
|
$this->crudPanel->removeAllButtonsFromStack('someStackName'); |
214
|
|
|
|
215
|
|
|
$this->assertEquals(count($this->defaultButtonNames), count($this->crudPanel->buttons)); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
private function getButtonByName($name) |
|
|
|
|
219
|
|
|
{ |
220
|
|
|
return $this->crudPanel->buttons->first(function ($value) use ($name) { |
221
|
|
|
return $value->name == $name; |
222
|
|
|
}); |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.