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