2012-03-01 14:32:30 +01:00
|
|
|
<?php
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\Forms\Tests\GridField;
|
|
|
|
|
2020-08-04 12:02:20 +02:00
|
|
|
use SilverStripe\Control\HTTPRequest;
|
2017-06-22 12:50:45 +02:00
|
|
|
use SilverStripe\Core\Convert;
|
|
|
|
use SilverStripe\Dev\CSSContentParser;
|
|
|
|
use SilverStripe\Dev\FunctionalTest;
|
2020-08-04 12:02:20 +02:00
|
|
|
use SilverStripe\Forms\GridField\GridField;
|
2017-06-22 12:50:45 +02:00
|
|
|
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter;
|
2020-08-04 12:02:20 +02:00
|
|
|
use SilverStripe\Forms\GridField\GridFieldConfig;
|
|
|
|
use SilverStripe\Forms\GridField\GridFieldDataColumns;
|
2016-10-14 03:30:05 +02:00
|
|
|
use SilverStripe\Forms\Tests\GridField\GridFieldAddExistingAutocompleterTest\TestController;
|
|
|
|
use SilverStripe\Forms\Tests\GridField\GridFieldTest\Cheerleader;
|
|
|
|
use SilverStripe\Forms\Tests\GridField\GridFieldTest\Permissions;
|
|
|
|
use SilverStripe\Forms\Tests\GridField\GridFieldTest\Player;
|
2022-03-22 04:17:37 +01:00
|
|
|
use SilverStripe\Forms\Tests\GridField\GridFieldTest\Stadium;
|
2016-10-14 03:30:05 +02:00
|
|
|
use SilverStripe\Forms\Tests\GridField\GridFieldTest\Team;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\ArrayList;
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
class GridFieldAddExistingAutocompleterTest extends FunctionalTest
|
|
|
|
{
|
|
|
|
|
|
|
|
protected static $fixture_file = 'GridFieldTest.yml';
|
|
|
|
|
2017-03-24 12:17:26 +01:00
|
|
|
protected static $extra_dataobjects = [
|
2016-12-16 05:34:21 +01:00
|
|
|
Team::class,
|
|
|
|
Cheerleader::class,
|
|
|
|
Player::class,
|
2022-03-22 04:17:37 +01:00
|
|
|
Permissions::class,
|
|
|
|
Stadium::class,
|
2016-12-16 05:34:21 +01:00
|
|
|
];
|
|
|
|
|
2017-03-24 12:17:26 +01:00
|
|
|
protected static $extra_controllers = [
|
|
|
|
TestController::class,
|
2016-12-16 05:34:21 +01:00
|
|
|
];
|
|
|
|
|
2017-02-22 04:15:08 +01:00
|
|
|
public function testScaffoldSearchFields()
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
2020-04-20 19:58:09 +02:00
|
|
|
$autoCompleter = new GridFieldAddExistingAutocompleter($targetFragment = 'before', ['Test']);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2022-03-22 04:17:37 +01:00
|
|
|
'Name:StartsWith',
|
|
|
|
'City:EndsWith',
|
|
|
|
'Country:ExactMatch',
|
|
|
|
'Type:Fulltext'
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
2022-03-22 04:17:37 +01:00
|
|
|
$autoCompleter->scaffoldSearchFields(Stadium::class)
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function testSearch()
|
|
|
|
{
|
|
|
|
$team2 = $this->objFromFixture(Team::class, 'team2');
|
|
|
|
|
|
|
|
$response = $this->get('GridFieldAddExistingAutocompleterTest_Controller');
|
|
|
|
$this->assertFalse($response->isError());
|
|
|
|
$parser = new CSSContentParser($response->getBody());
|
|
|
|
$btns = $parser->getBySelector('.grid-field .action_gridfield_relationfind');
|
|
|
|
|
|
|
|
$response = $this->post(
|
|
|
|
'GridFieldAddExistingAutocompleterTest_Controller/Form/field/testfield/search'
|
|
|
|
. '/?gridfield_relationsearch=Team 2',
|
2020-04-20 19:58:09 +02:00
|
|
|
[(string)$btns[0]['name'] => 1]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
$this->assertFalse($response->isError());
|
2022-04-14 03:12:59 +02:00
|
|
|
$result = json_decode($response->getBody() ?? '', true);
|
|
|
|
$this->assertEquals(1, count($result ?? []));
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
[[
|
2016-12-16 05:34:21 +01:00
|
|
|
'label' => 'Team 2',
|
|
|
|
'value' => 'Team 2',
|
|
|
|
'id' => $team2->ID,
|
2020-04-20 19:58:09 +02:00
|
|
|
]],
|
2016-12-16 05:34:21 +01:00
|
|
|
$result
|
|
|
|
);
|
|
|
|
|
|
|
|
$response = $this->post(
|
|
|
|
'GridFieldAddExistingAutocompleterTest_Controller/Form/field/testfield/'
|
|
|
|
. 'search/?gridfield_relationsearch=Heather',
|
2020-04-20 19:58:09 +02:00
|
|
|
[(string)$btns[0]['name'] => 1]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
$this->assertFalse($response->isError());
|
2022-04-14 03:12:59 +02:00
|
|
|
$result = json_decode($response->getBody() ?? '', true);
|
|
|
|
$this->assertEquals(1, count($result ?? []), "The relational filter did not work");
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$response = $this->post(
|
|
|
|
'GridFieldAddExistingAutocompleterTest_Controller/Form/field/testfield/search'
|
|
|
|
. '/?gridfield_relationsearch=Unknown',
|
2020-04-20 19:58:09 +02:00
|
|
|
[(string)$btns[0]['name'] => 1]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
$this->assertFalse($response->isError());
|
2022-04-14 03:12:59 +02:00
|
|
|
$result = json_decode($response->getBody() ?? '', true);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEmpty($result, 'The output is either an empty array or boolean FALSE');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAdd()
|
|
|
|
{
|
|
|
|
$this->logInWithPermission('ADMIN');
|
|
|
|
$team1 = $this->objFromFixture(Team::class, 'team1');
|
|
|
|
$team2 = $this->objFromFixture(Team::class, 'team2');
|
|
|
|
|
|
|
|
$response = $this->get('GridFieldAddExistingAutocompleterTest_Controller');
|
|
|
|
$this->assertFalse($response->isError());
|
|
|
|
$parser = new CSSContentParser($response->getBody());
|
|
|
|
$items = $parser->getBySelector('.grid-field .ss-gridfield-items .ss-gridfield-item');
|
2022-04-14 03:12:59 +02:00
|
|
|
$this->assertEquals(1, count($items ?? []));
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals($team1->ID, (int)$items[0]['data-id']);
|
|
|
|
|
|
|
|
$btns = $parser->getBySelector('.grid-field .action_gridfield_relationadd');
|
|
|
|
$response = $this->post(
|
|
|
|
'GridFieldAddExistingAutocompleterTest_Controller/Form/field/testfield',
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'relationID' => $team2->ID,
|
|
|
|
(string)$btns[0]['name'] => 1
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
$this->assertFalse($response->isError());
|
|
|
|
$parser = new CSSContentParser($response->getBody());
|
|
|
|
$items = $parser->getBySelector('.grid-field .ss-gridfield-items .ss-gridfield-item');
|
2022-04-14 03:12:59 +02:00
|
|
|
$this->assertEquals(2, count($items ?? []));
|
2017-09-27 09:25:37 +02:00
|
|
|
$this->assertListEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['ID' => (int)$items[0]['data-id']],
|
|
|
|
['ID' => (int)$items[1]['data-id']],
|
|
|
|
],
|
|
|
|
new ArrayList([$team1, $team2])
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
}
|
2020-08-04 12:02:20 +02:00
|
|
|
|
|
|
|
public function testRetainsCustomSort()
|
|
|
|
{
|
|
|
|
$component = new GridFieldAddExistingAutocompleter($targetFragment = 'before', ['Test']);
|
|
|
|
$component->setSearchFields(['Name']);
|
|
|
|
|
|
|
|
$grid = $this->getGridFieldForComponent($component);
|
|
|
|
$grid->setList(Team::get()->filter('Name', 'force-empty-list'));
|
|
|
|
|
|
|
|
$component->setSearchList(Team::get());
|
|
|
|
$request = new HTTPRequest('GET', '', ['gridfield_relationsearch' => 'Team']);
|
|
|
|
$response = $component->doSearch($grid, $request);
|
|
|
|
$this->assertFalse($response->isError());
|
2022-04-14 03:12:59 +02:00
|
|
|
$result = json_decode($response->getBody() ?? '', true);
|
2020-08-04 12:02:20 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
['Team 1', 'Team 2', 'Team 3', 'Team 4'],
|
2021-05-20 22:30:43 +02:00
|
|
|
array_map(
|
|
|
|
function ($item) {
|
|
|
|
return $item['label'];
|
|
|
|
},
|
2022-04-14 03:12:59 +02:00
|
|
|
$result ?? []
|
2021-05-20 22:30:43 +02:00
|
|
|
)
|
2020-08-04 12:02:20 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$component->setSearchList(Team::get()->sort('Name', 'DESC'));
|
|
|
|
$request = new HTTPRequest('GET', '', ['gridfield_relationsearch' => 'Team']);
|
|
|
|
$response = $component->doSearch($grid, $request);
|
|
|
|
$this->assertFalse($response->isError());
|
2022-04-14 03:12:59 +02:00
|
|
|
$result = json_decode($response->getBody() ?? '', true);
|
2020-08-04 12:02:20 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
['Team 4', 'Team 3', 'Team 2', 'Team 1'],
|
2021-05-20 22:30:43 +02:00
|
|
|
array_map(
|
|
|
|
function ($item) {
|
|
|
|
return $item['label'];
|
|
|
|
},
|
2022-04-14 03:12:59 +02:00
|
|
|
$result ?? []
|
2021-05-20 22:30:43 +02:00
|
|
|
)
|
2020-08-04 12:02:20 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getGridFieldForComponent($component)
|
|
|
|
{
|
|
|
|
$config = GridFieldConfig::create()->addComponents(
|
|
|
|
$component,
|
|
|
|
new GridFieldDataColumns()
|
|
|
|
);
|
|
|
|
|
|
|
|
return (new GridField('testfield', 'testfield'))
|
|
|
|
->setConfig($config);
|
|
|
|
}
|
2012-03-30 05:18:14 +02:00
|
|
|
}
|