1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\Forms\Tests\GridField; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Control\HTTPRequest; |
6
|
|
|
use SilverStripe\Core\Convert; |
7
|
|
|
use SilverStripe\Dev\CSSContentParser; |
8
|
|
|
use SilverStripe\Dev\FunctionalTest; |
9
|
|
|
use SilverStripe\Forms\GridField\GridField; |
10
|
|
|
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter; |
11
|
|
|
use SilverStripe\Forms\GridField\GridFieldConfig; |
12
|
|
|
use SilverStripe\Forms\GridField\GridFieldDataColumns; |
13
|
|
|
use SilverStripe\Forms\Tests\GridField\GridFieldAddExistingAutocompleterTest\TestController; |
14
|
|
|
use SilverStripe\Forms\Tests\GridField\GridFieldTest\Cheerleader; |
15
|
|
|
use SilverStripe\Forms\Tests\GridField\GridFieldTest\Permissions; |
16
|
|
|
use SilverStripe\Forms\Tests\GridField\GridFieldTest\Player; |
17
|
|
|
use SilverStripe\Forms\Tests\GridField\GridFieldTest\Team; |
18
|
|
|
use SilverStripe\ORM\ArrayList; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @skipUpgrade |
22
|
|
|
*/ |
23
|
|
|
class GridFieldAddExistingAutocompleterTest extends FunctionalTest |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
protected static $fixture_file = 'GridFieldTest.yml'; |
27
|
|
|
|
28
|
|
|
protected static $extra_dataobjects = [ |
29
|
|
|
Team::class, |
30
|
|
|
Cheerleader::class, |
31
|
|
|
Player::class, |
32
|
|
|
Permissions::class |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
protected static $extra_controllers = [ |
36
|
|
|
TestController::class, |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
public function testScaffoldSearchFields() |
40
|
|
|
{ |
41
|
|
|
$autoCompleter = new GridFieldAddExistingAutocompleter($targetFragment = 'before', ['Test']); |
42
|
|
|
$this->assertEquals( |
43
|
|
|
[ |
44
|
|
|
'Name:PartialMatch', |
45
|
|
|
'City:StartsWith', |
46
|
|
|
'Cheerleaders.Name:StartsWith' |
47
|
|
|
], |
48
|
|
|
$autoCompleter->scaffoldSearchFields(Team::class) |
49
|
|
|
); |
50
|
|
|
$this->assertEquals( |
51
|
|
|
[ 'Name:StartsWith' ], |
52
|
|
|
$autoCompleter->scaffoldSearchFields(Cheerleader::class) |
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
function testSearch() |
57
|
|
|
{ |
58
|
|
|
$team2 = $this->objFromFixture(Team::class, 'team2'); |
59
|
|
|
|
60
|
|
|
$response = $this->get('GridFieldAddExistingAutocompleterTest_Controller'); |
61
|
|
|
$this->assertFalse($response->isError()); |
62
|
|
|
$parser = new CSSContentParser($response->getBody()); |
63
|
|
|
$btns = $parser->getBySelector('.grid-field .action_gridfield_relationfind'); |
64
|
|
|
|
65
|
|
|
$response = $this->post( |
66
|
|
|
'GridFieldAddExistingAutocompleterTest_Controller/Form/field/testfield/search' |
67
|
|
|
. '/?gridfield_relationsearch=Team 2', |
68
|
|
|
[(string)$btns[0]['name'] => 1] |
69
|
|
|
); |
70
|
|
|
$this->assertFalse($response->isError()); |
71
|
|
|
$result = json_decode($response->getBody(), true); |
72
|
|
|
$this->assertEquals(1, count($result)); |
73
|
|
|
$this->assertEquals( |
74
|
|
|
[[ |
75
|
|
|
'label' => 'Team 2', |
76
|
|
|
'value' => 'Team 2', |
77
|
|
|
'id' => $team2->ID, |
78
|
|
|
]], |
79
|
|
|
$result |
80
|
|
|
); |
81
|
|
|
|
82
|
|
|
$response = $this->post( |
83
|
|
|
'GridFieldAddExistingAutocompleterTest_Controller/Form/field/testfield/' |
84
|
|
|
. 'search/?gridfield_relationsearch=Heather', |
85
|
|
|
[(string)$btns[0]['name'] => 1] |
86
|
|
|
); |
87
|
|
|
$this->assertFalse($response->isError()); |
88
|
|
|
$result = json_decode($response->getBody(), true); |
89
|
|
|
$this->assertEquals(1, count($result), "The relational filter did not work"); |
90
|
|
|
|
91
|
|
|
$response = $this->post( |
92
|
|
|
'GridFieldAddExistingAutocompleterTest_Controller/Form/field/testfield/search' |
93
|
|
|
. '/?gridfield_relationsearch=Unknown', |
94
|
|
|
[(string)$btns[0]['name'] => 1] |
95
|
|
|
); |
96
|
|
|
$this->assertFalse($response->isError()); |
97
|
|
|
$result = json_decode($response->getBody(), true); |
98
|
|
|
$this->assertEmpty($result, 'The output is either an empty array or boolean FALSE'); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function testAdd() |
102
|
|
|
{ |
103
|
|
|
$this->logInWithPermission('ADMIN'); |
104
|
|
|
$team1 = $this->objFromFixture(Team::class, 'team1'); |
105
|
|
|
$team2 = $this->objFromFixture(Team::class, 'team2'); |
106
|
|
|
|
107
|
|
|
$response = $this->get('GridFieldAddExistingAutocompleterTest_Controller'); |
108
|
|
|
$this->assertFalse($response->isError()); |
109
|
|
|
$parser = new CSSContentParser($response->getBody()); |
110
|
|
|
$items = $parser->getBySelector('.grid-field .ss-gridfield-items .ss-gridfield-item'); |
111
|
|
|
$this->assertEquals(1, count($items)); |
112
|
|
|
$this->assertEquals($team1->ID, (int)$items[0]['data-id']); |
113
|
|
|
|
114
|
|
|
$btns = $parser->getBySelector('.grid-field .action_gridfield_relationadd'); |
115
|
|
|
$response = $this->post( |
116
|
|
|
'GridFieldAddExistingAutocompleterTest_Controller/Form/field/testfield', |
117
|
|
|
[ |
118
|
|
|
'relationID' => $team2->ID, |
119
|
|
|
(string)$btns[0]['name'] => 1 |
120
|
|
|
] |
121
|
|
|
); |
122
|
|
|
$this->assertFalse($response->isError()); |
123
|
|
|
$parser = new CSSContentParser($response->getBody()); |
124
|
|
|
$items = $parser->getBySelector('.grid-field .ss-gridfield-items .ss-gridfield-item'); |
125
|
|
|
$this->assertEquals(2, count($items)); |
126
|
|
|
$this->assertListEquals( |
127
|
|
|
[ |
128
|
|
|
['ID' => (int)$items[0]['data-id']], |
129
|
|
|
['ID' => (int)$items[1]['data-id']], |
130
|
|
|
], |
131
|
|
|
new ArrayList([$team1, $team2]) |
132
|
|
|
); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function testRetainsCustomSort() |
136
|
|
|
{ |
137
|
|
|
$component = new GridFieldAddExistingAutocompleter($targetFragment = 'before', ['Test']); |
138
|
|
|
$component->setSearchFields(['Name']); |
139
|
|
|
|
140
|
|
|
$grid = $this->getGridFieldForComponent($component); |
141
|
|
|
$grid->setList(Team::get()->filter('Name', 'force-empty-list')); |
142
|
|
|
|
143
|
|
|
$component->setSearchList(Team::get()); |
144
|
|
|
$request = new HTTPRequest('GET', '', ['gridfield_relationsearch' => 'Team']); |
145
|
|
|
$response = $component->doSearch($grid, $request); |
146
|
|
|
$this->assertFalse($response->isError()); |
147
|
|
|
$result = json_decode($response->getBody(), true); |
148
|
|
|
$this->assertEquals( |
149
|
|
|
['Team 1', 'Team 2', 'Team 3', 'Team 4'], |
150
|
|
|
array_map( |
151
|
|
|
function ($item) { |
152
|
|
|
return $item['label']; |
153
|
|
|
}, |
154
|
|
|
$result |
155
|
|
|
) |
156
|
|
|
); |
157
|
|
|
|
158
|
|
|
$component->setSearchList(Team::get()->sort('Name', 'DESC')); |
159
|
|
|
$request = new HTTPRequest('GET', '', ['gridfield_relationsearch' => 'Team']); |
160
|
|
|
$response = $component->doSearch($grid, $request); |
161
|
|
|
$this->assertFalse($response->isError()); |
162
|
|
|
$result = json_decode($response->getBody(), true); |
163
|
|
|
$this->assertEquals( |
164
|
|
|
['Team 4', 'Team 3', 'Team 2', 'Team 1'], |
165
|
|
|
array_map( |
166
|
|
|
function ($item) { |
167
|
|
|
return $item['label']; |
168
|
|
|
}, |
169
|
|
|
$result |
170
|
|
|
) |
171
|
|
|
); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
protected function getGridFieldForComponent($component) |
175
|
|
|
{ |
176
|
|
|
$config = GridFieldConfig::create()->addComponents( |
177
|
|
|
$component, |
178
|
|
|
new GridFieldDataColumns() |
179
|
|
|
); |
180
|
|
|
|
181
|
|
|
return (new GridField('testfield', 'testfield')) |
182
|
|
|
->setConfig($config); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|