1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\Forms\Tests\GridField; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Control\HTTPRequest; |
6
|
|
|
use SilverStripe\Dev\SapphireTest; |
7
|
|
|
use SilverStripe\Forms\FieldList; |
8
|
|
|
use SilverStripe\Forms\Form; |
9
|
|
|
use SilverStripe\Forms\GridField\GridField; |
10
|
|
|
use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor; |
11
|
|
|
use SilverStripe\Forms\GridField\GridFieldFilterHeader; |
12
|
|
|
use SilverStripe\Forms\Tests\GridField\GridFieldFilterHeaderTest\Cheerleader; |
13
|
|
|
use SilverStripe\Forms\Tests\GridField\GridFieldFilterHeaderTest\CheerleaderHat; |
14
|
|
|
use SilverStripe\Forms\Tests\GridField\GridFieldFilterHeaderTest\Mom; |
15
|
|
|
use SilverStripe\Forms\Tests\GridField\GridFieldFilterHeaderTest\Team; |
16
|
|
|
use SilverStripe\Forms\Tests\GridField\GridFieldFilterHeaderTest\TeamGroup; |
17
|
|
|
use SilverStripe\Forms\Tests\GridField\GridFieldFilterHeaderTest\TestController; |
|
|
|
|
18
|
|
|
use SilverStripe\ORM\DataList; |
19
|
|
|
|
20
|
|
|
class GridFieldFilterHeaderTest extends SapphireTest |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var ArrayList |
|
|
|
|
25
|
|
|
*/ |
26
|
|
|
protected $list; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var GridField |
30
|
|
|
*/ |
31
|
|
|
protected $gridField; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var Form |
35
|
|
|
*/ |
36
|
|
|
protected $form; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var GridFieldFilterHeader |
40
|
|
|
*/ |
41
|
|
|
protected $component; |
42
|
|
|
|
43
|
|
|
protected static $fixture_file = 'GridFieldFilterHeaderTest.yml'; |
44
|
|
|
|
45
|
|
|
protected static $extra_dataobjects = [ |
46
|
|
|
Team::class, |
47
|
|
|
TeamGroup::class, |
48
|
|
|
Cheerleader::class, |
49
|
|
|
CheerleaderHat::class, |
50
|
|
|
Mom::class, |
51
|
|
|
]; |
52
|
|
|
|
53
|
|
|
protected function setUp(): void |
54
|
|
|
{ |
55
|
|
|
parent::setUp(); |
56
|
|
|
$this->list = new DataList(Team::class); |
|
|
|
|
57
|
|
|
$config = GridFieldConfig_RecordEditor::create()->addComponent(new GridFieldFilterHeader()); |
58
|
|
|
$this->gridField = new GridField('testfield', 'testfield', $this->list, $config); |
59
|
|
|
$this->form = new Form(null, 'Form', new FieldList([$this->gridField]), new FieldList()); |
60
|
|
|
$this->component = $this->gridField->getConfig()->getComponentByType(GridFieldFilterHeader::class); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Tests that the appropriate filter headers are generated |
65
|
|
|
* |
66
|
|
|
* @skipUpgrade |
67
|
|
|
*/ |
68
|
|
|
public function testRenderHeaders() |
69
|
|
|
{ |
70
|
|
|
$htmlFragment = $this->component->getHTMLFragments($this->gridField); |
71
|
|
|
|
72
|
|
|
// Check that the output is the new search field |
73
|
|
|
$this->assertStringContainsString('<div class="search-holder grid-field__search-holder grid-field__search-holder--hidden"', $htmlFragment['before']); |
74
|
|
|
$this->assertStringContainsString('Open search and filter', $htmlFragment['buttons-before-right']); |
75
|
|
|
|
76
|
|
|
$this->gridField->getConfig()->removeComponentsByType(GridFieldFilterHeader::class); |
77
|
|
|
$this->gridField->getConfig()->addComponent(new GridFieldFilterHeader(true)); |
78
|
|
|
$this->component = $this->gridField->getConfig()->getComponentByType(GridFieldFilterHeader::class); |
79
|
|
|
$htmlFragment = $this->component->getHTMLFragments($this->gridField); |
|
|
|
|
80
|
|
|
|
81
|
|
|
// Check that the output is the legacy filter header |
82
|
|
|
$this->assertStringContainsString( |
83
|
|
|
'<tr class="grid-field__filter-header grid-field__search-holder--hidden">', |
84
|
|
|
$htmlFragment['header'] |
85
|
|
|
); |
86
|
|
|
$this->assertFalse(array_key_exists('buttons-before-right', $htmlFragment ?? [])); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function testSearchFieldSchema() |
90
|
|
|
{ |
91
|
|
|
$searchSchema = json_decode($this->component->getSearchFieldSchema($this->gridField) ?? ''); |
92
|
|
|
|
93
|
|
|
$this->assertEquals('field/testfield/schema/SearchForm', $searchSchema->formSchemaUrl); |
94
|
|
|
$this->assertEquals('Name', $searchSchema->name); |
95
|
|
|
$this->assertEquals('Search "Teams"', $searchSchema->placeholder); |
96
|
|
|
$this->assertEquals(new \stdClass, $searchSchema->filters); |
97
|
|
|
|
98
|
|
|
$request = new HTTPRequest( |
99
|
|
|
'POST', |
100
|
|
|
'field/testfield', |
101
|
|
|
[], |
102
|
|
|
[ |
103
|
|
|
'filter' => [ |
104
|
|
|
'testfield' => [ |
105
|
|
|
'Name' => 'test', |
106
|
|
|
'City' => 'place' |
107
|
|
|
] |
108
|
|
|
], |
109
|
|
|
] |
110
|
|
|
); |
111
|
|
|
$this->gridField->setRequest($request); |
112
|
|
|
$searchSchema = json_decode($this->component->getSearchFieldSchema($this->gridField) ?? ''); |
113
|
|
|
|
114
|
|
|
$this->assertEquals('field/testfield/schema/SearchForm', $searchSchema->formSchemaUrl); |
115
|
|
|
$this->assertEquals('Name', $searchSchema->name); |
116
|
|
|
$this->assertEquals('Search "Teams"', $searchSchema->placeholder); |
117
|
|
|
$this->assertEquals('test', $searchSchema->filters->Search__Name); |
118
|
|
|
$this->assertEquals('place', $searchSchema->filters->Search__City); |
119
|
|
|
$this->assertEquals('testfield', $searchSchema->gridfield); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function testHandleActionReset() |
123
|
|
|
{ |
124
|
|
|
// Init Grid state with some pre-existing filters |
125
|
|
|
$state = $this->gridField->State; |
126
|
|
|
$state->GridFieldFilterHeader = []; |
|
|
|
|
127
|
|
|
$state->GridFieldFilterHeader->Columns = []; |
|
|
|
|
128
|
|
|
$state->GridFieldFilterHeader->Columns->Name = 'test'; |
129
|
|
|
|
130
|
|
|
$this->component->handleAction( |
131
|
|
|
$this->gridField, |
132
|
|
|
'reset', |
133
|
|
|
[], |
134
|
|
|
'{"GridFieldFilterHeader":{"Columns":{"Name":"test"}}}' |
|
|
|
|
135
|
|
|
); |
136
|
|
|
|
137
|
|
|
$this->assertEmpty( |
138
|
|
|
$state->GridFieldFilterHeader->Columns->toArray(), |
139
|
|
|
'GridFieldFilterHeader::handleAction resets the gridstate filter when the user resets the search.' |
140
|
|
|
); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function testGetSearchForm() |
144
|
|
|
{ |
145
|
|
|
$searchForm = $this->component->getSearchForm($this->gridField); |
146
|
|
|
|
147
|
|
|
$this->assertTrue($searchForm instanceof Form); |
148
|
|
|
$this->assertEquals('Search__Name', $searchForm->fields[0]->Name); |
|
|
|
|
149
|
|
|
$this->assertEquals('Search__City', $searchForm->fields[1]->Name); |
150
|
|
|
$this->assertEquals('Search__Cheerleader__Hat__Colour', $searchForm->fields[2]->Name); |
151
|
|
|
$this->assertEquals('TeamsSearchForm', $searchForm->Name); |
|
|
|
|
152
|
|
|
$this->assertEquals('cms-search-form', $searchForm->extraClasses['cms-search-form']); |
153
|
|
|
|
154
|
|
|
foreach ($searchForm->fields as $field) { |
155
|
|
|
$this->assertEquals('stacked', $field->extraClasses['stacked']); |
156
|
|
|
$this->assertEquals('no-change-track', $field->extraClasses['no-change-track']); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths