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
|
|
|
|
60
|
|
|
public function testItCanCreateAFilterFluently() |
61
|
|
|
{ |
62
|
|
|
CrudFilter::name('my_filter') |
63
|
|
|
->type('simple') |
|
|
|
|
64
|
|
|
->label('custom_label') |
65
|
|
|
->options(['test' => 'test']) |
66
|
|
|
->view('simple') |
67
|
|
|
->viewNamespace('crud::filters') |
68
|
|
|
->ifActive(function () { |
69
|
|
|
return true; |
70
|
|
|
}) |
71
|
|
|
->ifInactive(function () { |
72
|
|
|
return true; |
73
|
|
|
}); |
74
|
|
|
|
75
|
|
|
$this->assertCount(1, $this->crudPanel->filters()); |
76
|
|
|
$filter = $this->crudPanel->filters()[0]; |
77
|
|
|
$this->assertTrue(is_callable($filter->fallbackLogic)); |
78
|
|
|
$this->assertTrue(is_callable($filter->logic)); |
79
|
|
|
$this->assertEquals(['test' => 'test'], $filter->values); |
80
|
|
|
$this->assertEquals('custom_label', $filter->label); |
81
|
|
|
$this->assertEquals('simple', $filter->type); |
82
|
|
|
$this->assertEquals('simple', $filter->view); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function testWhenActiveAndWhenInactiveAliases() |
86
|
|
|
{ |
87
|
|
|
$filter = CrudFilter::name('my_filter') |
88
|
|
|
->whenActive(function () { |
|
|
|
|
89
|
|
|
return true; |
90
|
|
|
}) |
91
|
|
|
->whenInactive(function () { |
92
|
|
|
return true; |
93
|
|
|
}); |
94
|
|
|
|
95
|
|
|
$this->assertCount(1, $this->crudPanel->filters()); |
96
|
|
|
$this->assertTrue(is_callable($filter->fallbackLogic)); |
97
|
|
|
$this->assertTrue(is_callable($filter->logic)); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function testWhenNotActiveAlias() |
101
|
|
|
{ |
102
|
|
|
$filter = CrudFilter::name('my_filter')->whenNotActive(function () { |
|
|
|
|
103
|
|
|
return true; |
104
|
|
|
}); |
105
|
|
|
$this->assertCount(1, $this->crudPanel->filters()); |
106
|
|
|
$this->assertTrue(is_callable($filter->fallbackLogic)); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function testIfNotActiveAlias() |
110
|
|
|
{ |
111
|
|
|
$filter = CrudFilter::name('my_filter')->ifNotActive(function () { |
|
|
|
|
112
|
|
|
return true; |
113
|
|
|
}); |
114
|
|
|
$this->assertCount(1, $this->crudPanel->filters()); |
115
|
|
|
$this->assertTrue(is_callable($filter->fallbackLogic)); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function testElseAlias() |
119
|
|
|
{ |
120
|
|
|
$filter = CrudFilter::name('my_filter')->else(function () { |
|
|
|
|
121
|
|
|
return true; |
122
|
|
|
}); |
123
|
|
|
$this->assertCount(1, $this->crudPanel->filters()); |
124
|
|
|
$this->assertTrue(is_callable($filter->fallbackLogic)); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function testItCanAddAFilterBeforeOtherFilter() |
128
|
|
|
{ |
129
|
|
|
$this->crudPanel->addFilter(...$this->testFilter); |
|
|
|
|
130
|
|
|
CrudFilter::name('test_filter_2')->label('just_an_hack_before_fix_gets_merged')->before('my_filter'); |
|
|
|
|
131
|
|
|
$filterCollection = $this->crudPanel->filters(); |
132
|
|
|
$this->assertCount(2, $filterCollection); |
133
|
|
|
$firstFilter = $filterCollection[0]; |
134
|
|
|
$this->assertEquals('test_filter_2', $firstFilter->name); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function testItCanAddAFilterAfterOtherFilter() |
138
|
|
|
{ |
139
|
|
|
$this->crudPanel->addFilter(...$this->testFilter); |
|
|
|
|
140
|
|
|
$this->crudPanel->addFilter(...$this->testFilter_2); |
141
|
|
|
CrudFilter::name('test_filter_2')->label('just_an_hack_before_fix_gets_merged')->after('my_filter'); |
142
|
|
|
$filterCollection = $this->crudPanel->filters(); |
143
|
|
|
$this->assertCount(3, $filterCollection); |
144
|
|
|
$secondFilter = $filterCollection[1]; |
145
|
|
|
$this->assertEquals('test_filter_2', $secondFilter->name); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function testItCanMakeAFilterFirst() |
149
|
|
|
{ |
150
|
|
|
$this->crudPanel->addFilter(...$this->testFilter); |
|
|
|
|
151
|
|
|
CrudFilter::name('test_filter_2')->label('just_an_hack_before_fix_gets_merged')->makeFirst(); |
152
|
|
|
$filterCollection = $this->crudPanel->filters(); |
153
|
|
|
$this->assertCount(2, $filterCollection); |
154
|
|
|
$firstFilter = $filterCollection[0]; |
155
|
|
|
$this->assertEquals('test_filter_2', $firstFilter->name); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function testItCanMakeAFilterLast() |
159
|
|
|
{ |
160
|
|
|
$this->crudPanel->addFilter(...$this->testFilter); |
|
|
|
|
161
|
|
|
$this->crudPanel->addFilter(...$this->testFilter_2); |
162
|
|
|
CrudFilter::name('my_filter')->makeLast(); |
|
|
|
|
163
|
|
|
$filterCollection = $this->crudPanel->filters(); |
164
|
|
|
$this->assertCount(2, $filterCollection); |
165
|
|
|
$this->assertEquals(['my_filter_2', 'my_filter'], $filterCollection->pluck('name')->toArray()); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function testItCanRemoveAFilter() |
169
|
|
|
{ |
170
|
|
|
$this->crudPanel->addFilter(...$this->testFilter); |
|
|
|
|
171
|
|
|
$this->crudPanel->addFilter(...$this->testFilter_2); |
172
|
|
|
CrudFilter::name('my_filter')->remove(); |
|
|
|
|
173
|
|
|
$filterCollection = $this->crudPanel->filters(); |
174
|
|
|
$this->assertCount(1, $filterCollection); |
175
|
|
|
$this->assertEquals(['my_filter_2'], $filterCollection->pluck('name')->toArray()); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
public function testItCanRemoveAFilterAttribute() |
179
|
|
|
{ |
180
|
|
|
$this->crudPanel->addFilter(...$this->testFilter); |
|
|
|
|
181
|
|
|
$this->crudPanel->addFilter(...$this->testFilter_2); |
182
|
|
|
CrudFilter::name('my_filter')->forget('type'); |
|
|
|
|
183
|
|
|
$filterCollection = $this->crudPanel->filters(); |
184
|
|
|
$this->assertCount(2, $filterCollection); |
185
|
|
|
$this->assertFalse($filterCollection[0]->type); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
public function testItCanGetTheViewWithNamespace() |
189
|
|
|
{ |
190
|
|
|
$this->crudPanel->addFilter(...$this->testFilter); |
|
|
|
|
191
|
|
|
$namespacedFilterView = CrudFilter::name('my_filter')->getViewWithNamespace(); |
|
|
|
|
192
|
|
|
$filterCollection = $this->crudPanel->filters(); |
193
|
|
|
$this->assertCount(1, $filterCollection); |
194
|
|
|
$this->assertEquals($filterCollection[0]->viewNamespace.'.'.$filterCollection[0]->view, $namespacedFilterView); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
public function testItCanGetAllFilterViewNamespacesWithFallbacks() |
198
|
|
|
{ |
199
|
|
|
Config::set('backpack.crud.view_namespaces', ['filters' => ['pro::filters']]); |
200
|
|
|
$this->crudPanel->addFilter(...$this->testFilter); |
|
|
|
|
201
|
|
|
$filterNamespaceWithFallback = CrudFilter::name('my_filter')->getNamespacedViewWithFallbacks(); |
|
|
|
|
202
|
|
|
$this->assertEquals(['pro::filters.simple'], $filterNamespaceWithFallback); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
public function testItCanCheckIfFilterWasApplied() |
206
|
|
|
{ |
207
|
|
|
$this->crudPanel->addFilter(...$this->testFilter); |
|
|
|
|
208
|
|
|
$filter = CrudFilter::name('my_filter'); |
209
|
|
|
$this->assertTrue($filter->wasApplied()); |
|
|
|
|
210
|
|
|
$this->assertFalse($filter->wasNotApplied()); |
|
|
|
|
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|