1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\Tests\Unit\CrudPanel; |
4
|
|
|
|
5
|
|
|
use Backpack\CRUD\app\Library\CrudPanel\CrudFilter; |
6
|
|
|
use Backpack\CRUD\Tests\Unit\Models\User; |
7
|
|
|
use Config; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @covers Backpack\CRUD\app\Library\CrudPanel\Traits\Filters |
11
|
|
|
* @covers Backpack\CRUD\app\Library\CrudPanel\CrudFilter |
12
|
|
|
*/ |
13
|
|
|
class CrudPanelFiltersTest extends BaseCrudPanelTest |
14
|
|
|
{ |
15
|
|
|
protected $testFilter = [[ |
16
|
|
|
'name' => 'my_filter', |
17
|
|
|
'type' => 'simple', |
18
|
|
|
'label' => 'filter label', |
19
|
|
|
], false, false, false]; |
20
|
|
|
|
21
|
|
|
protected $testFilter_2 = [[ |
22
|
|
|
'name' => 'my_filter_2', |
23
|
|
|
'label' => 'filter label 2', |
24
|
|
|
], false, false, false]; |
25
|
|
|
|
26
|
|
|
public function testItEnablesTheFiltersButConsiderThemDisableIfEmpty() |
27
|
|
|
{ |
28
|
|
|
$this->crudPanel->enableFilters(); |
29
|
|
|
$this->assertFalse($this->crudPanel->filtersEnabled()); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function testItCanAddFiltersToCrudPanel() |
33
|
|
|
{ |
34
|
|
|
$this->crudPanel->addFilter(...$this->testFilter); |
35
|
|
|
|
36
|
|
|
$this->assertCount(1, $this->crudPanel->filters()); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testItCanClearFilters() |
40
|
|
|
{ |
41
|
|
|
$this->crudPanel->addFilter(...$this->testFilter); |
42
|
|
|
|
43
|
|
|
$this->crudPanel->clearFilters(); |
44
|
|
|
$this->assertCount(0, $this->crudPanel->filters()); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function testItCanCheckIfFilterIsActiveFromRequest() |
48
|
|
|
{ |
49
|
|
|
$this->crudPanel->setModel(User::class); |
50
|
|
|
$request = request()->create('/admin/users', 'GET', ['my_custom_filter' => 'foo']); |
51
|
|
|
$request->setRouteResolver(function () use ($request) { |
52
|
|
|
return (new Route('GET', 'admin/users', ['UserCrudController', 'index']))->bind($request); |
53
|
|
|
}); |
54
|
|
|
$this->crudPanel->setRequest($request); |
55
|
|
|
|
56
|
|
|
$isActive = CrudFilter::name('my_custom_filter')->isActive(); |
57
|
|
|
$this->assertTrue($isActive); |
58
|
|
|
|
59
|
|
|
public function testItCanCreateAFilterFluently() |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
CrudFilter::name('my_filter') |
62
|
|
|
->type('simple') |
63
|
|
|
->label('custom_label') |
64
|
|
|
->options(['test' => 'test']) |
65
|
|
|
->view('simple') |
66
|
|
|
->viewNamespace('crud::filters') |
67
|
|
|
->ifActive(function () { |
68
|
|
|
return true; |
69
|
|
|
}) |
70
|
|
|
->ifInactive(function () { |
71
|
|
|
return true; |
72
|
|
|
}); |
73
|
|
|
|
74
|
|
|
$this->assertCount(1, $this->crudPanel->filters()); |
75
|
|
|
$filter = $this->crudPanel->filters()[0]; |
76
|
|
|
$this->assertTrue(is_callable($filter->fallbackLogic)); |
77
|
|
|
$this->assertTrue(is_callable($filter->logic)); |
78
|
|
|
$this->assertEquals(['test' => 'test'], $filter->values); |
79
|
|
|
$this->assertEquals('custom_label', $filter->label); |
80
|
|
|
$this->assertEquals('simple', $filter->type); |
81
|
|
|
$this->assertEquals('simple', $filter->view); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testWhenActiveAndWhenInactiveAliases() |
85
|
|
|
{ |
86
|
|
|
$filter = CrudFilter::name('my_filter') |
87
|
|
|
->whenActive(function () { |
88
|
|
|
return true; |
89
|
|
|
}) |
90
|
|
|
->whenInactive(function () { |
91
|
|
|
return true; |
92
|
|
|
}); |
93
|
|
|
|
94
|
|
|
$this->assertCount(1, $this->crudPanel->filters()); |
95
|
|
|
$this->assertTrue(is_callable($filter->fallbackLogic)); |
96
|
|
|
$this->assertTrue(is_callable($filter->logic)); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function testWhenNotActiveAlias() |
100
|
|
|
{ |
101
|
|
|
$filter = CrudFilter::name('my_filter')->whenNotActive(function () { |
102
|
|
|
return true; |
103
|
|
|
}); |
104
|
|
|
$this->assertCount(1, $this->crudPanel->filters()); |
105
|
|
|
$this->assertTrue(is_callable($filter->fallbackLogic)); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function testIfNotActiveAlias() |
109
|
|
|
{ |
110
|
|
|
$filter = CrudFilter::name('my_filter')->ifNotActive(function () { |
111
|
|
|
return true; |
112
|
|
|
}); |
113
|
|
|
$this->assertCount(1, $this->crudPanel->filters()); |
114
|
|
|
$this->assertTrue(is_callable($filter->fallbackLogic)); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function testElseAlias() |
118
|
|
|
{ |
119
|
|
|
$filter = CrudFilter::name('my_filter')->else(function () { |
120
|
|
|
return true; |
121
|
|
|
}); |
122
|
|
|
$this->assertCount(1, $this->crudPanel->filters()); |
123
|
|
|
$this->assertTrue(is_callable($filter->fallbackLogic)); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function testItCanAddAFilterBeforeOtherFilter() |
127
|
|
|
{ |
128
|
|
|
$this->crudPanel->addFilter(...$this->testFilter); |
129
|
|
|
CrudFilter::name('test_filter_2')->label('just_an_hack_before_fix_gets_merged')->before('my_filter'); |
130
|
|
|
$filterCollection = $this->crudPanel->filters(); |
131
|
|
|
$this->assertCount(2, $filterCollection); |
132
|
|
|
$firstFilter = $filterCollection[0]; |
133
|
|
|
$this->assertEquals('test_filter_2', $firstFilter->name); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function testItCanAddAFilterAfterOtherFilter() |
137
|
|
|
{ |
138
|
|
|
$this->crudPanel->addFilter(...$this->testFilter); |
139
|
|
|
$this->crudPanel->addFilter(...$this->testFilter_2); |
140
|
|
|
CrudFilter::name('test_filter_2')->label('just_an_hack_before_fix_gets_merged')->after('my_filter'); |
141
|
|
|
$filterCollection = $this->crudPanel->filters(); |
142
|
|
|
$this->assertCount(3, $filterCollection); |
143
|
|
|
$secondFilter = $filterCollection[1]; |
144
|
|
|
$this->assertEquals('test_filter_2', $secondFilter->name); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function testItCanMakeAFilterFirst() |
148
|
|
|
{ |
149
|
|
|
$this->crudPanel->addFilter(...$this->testFilter); |
150
|
|
|
CrudFilter::name('test_filter_2')->label('just_an_hack_before_fix_gets_merged')->makeFirst(); |
151
|
|
|
$filterCollection = $this->crudPanel->filters(); |
152
|
|
|
$this->assertCount(2, $filterCollection); |
153
|
|
|
$firstFilter = $filterCollection[0]; |
154
|
|
|
$this->assertEquals('test_filter_2', $firstFilter->name); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function testItCanMakeAFilterLast() |
158
|
|
|
{ |
159
|
|
|
$this->crudPanel->addFilter(...$this->testFilter); |
160
|
|
|
$this->crudPanel->addFilter(...$this->testFilter_2); |
161
|
|
|
CrudFilter::name('my_filter')->makeLast(); |
162
|
|
|
$filterCollection = $this->crudPanel->filters(); |
163
|
|
|
$this->assertCount(2, $filterCollection); |
164
|
|
|
$this->assertEquals(['my_filter_2', 'my_filter'], $filterCollection->pluck('name')->toArray()); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function testItCanRemoveAFilter() |
168
|
|
|
{ |
169
|
|
|
$this->crudPanel->addFilter(...$this->testFilter); |
170
|
|
|
$this->crudPanel->addFilter(...$this->testFilter_2); |
171
|
|
|
CrudFilter::name('my_filter')->remove(); |
172
|
|
|
$filterCollection = $this->crudPanel->filters(); |
173
|
|
|
$this->assertCount(1, $filterCollection); |
174
|
|
|
$this->assertEquals(['my_filter_2'], $filterCollection->pluck('name')->toArray()); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
public function testItCanRemoveAFilterAttribute() |
178
|
|
|
{ |
179
|
|
|
$this->crudPanel->addFilter(...$this->testFilter); |
180
|
|
|
$this->crudPanel->addFilter(...$this->testFilter_2); |
181
|
|
|
CrudFilter::name('my_filter')->forget('type'); |
182
|
|
|
$filterCollection = $this->crudPanel->filters(); |
183
|
|
|
$this->assertCount(2, $filterCollection); |
184
|
|
|
$this->assertFalse($filterCollection[0]->type); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
public function testItCanGetTheViewWithNamespace() |
188
|
|
|
{ |
189
|
|
|
$this->crudPanel->addFilter(...$this->testFilter); |
190
|
|
|
$namespacedFilterView = CrudFilter::name('my_filter')->getViewWithNamespace(); |
191
|
|
|
$filterCollection = $this->crudPanel->filters(); |
192
|
|
|
$this->assertCount(1, $filterCollection); |
193
|
|
|
$this->assertEquals($filterCollection[0]->viewNamespace.'.'.$filterCollection[0]->view, $namespacedFilterView); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public function testItCanGetAllFilterViewNamespacesWithFallbacks() |
197
|
|
|
{ |
198
|
|
|
Config::set('backpack.crud.view_namespaces', ['filters' => ['pro::filters']]); |
199
|
|
|
$this->crudPanel->addFilter(...$this->testFilter); |
200
|
|
|
$filterNamespaceWithFallback = CrudFilter::name('my_filter')->getNamespacedViewWithFallbacks(); |
201
|
|
|
$this->assertEquals(['pro::filters.simple'], $filterNamespaceWithFallback); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
public function testItCanCheckIfFilterWasApplied() |
205
|
|
|
{ |
206
|
|
|
$this->crudPanel->addFilter(...$this->testFilter); |
207
|
|
|
$filter = CrudFilter::name('my_filter'); |
208
|
|
|
$this->assertTrue($filter->wasApplied()); |
209
|
|
|
$this->assertFalse($filter->wasNotApplied()); |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|