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;
|
|
|
|
|
|
|
|
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;
|
|
|
|
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
|
|
|
use SilverStripe\Core\Convert;
|
|
|
|
use SilverStripe\Dev\CSSContentParser;
|
|
|
|
use SilverStripe\Dev\FunctionalTest;
|
|
|
|
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter;
|
|
|
|
|
2012-03-09 00:54:02 +01:00
|
|
|
class GridFieldAddExistingAutocompleterTest extends FunctionalTest {
|
2012-03-01 14:32:30 +01:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
protected static $fixture_file = 'GridFieldTest.yml';
|
2012-03-01 14:32:30 +01:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
protected $extraDataObjects = [
|
|
|
|
Team::class,
|
|
|
|
Cheerleader::class,
|
|
|
|
Player::class,
|
|
|
|
Permissions::class
|
|
|
|
];
|
|
|
|
|
|
|
|
protected $extraControllers = [
|
|
|
|
TestController::class
|
|
|
|
];
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-10-04 15:59:36 +02:00
|
|
|
function testScaffoldSearchFields() {
|
|
|
|
$autoCompleter = new GridFieldAddExistingAutocompleter($targetFragment = 'before', array('Test'));
|
|
|
|
$this->assertEquals(
|
2016-10-14 03:30:05 +02:00
|
|
|
$autoCompleter->scaffoldSearchFields(Team::class),
|
2012-10-04 15:59:36 +02:00
|
|
|
array(
|
|
|
|
'Name:PartialMatch',
|
|
|
|
'City:StartsWith',
|
|
|
|
'Cheerleaders.Name:StartsWith'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2016-10-14 03:30:05 +02:00
|
|
|
$autoCompleter->scaffoldSearchFields(Cheerleader::class),
|
2012-10-04 15:59:36 +02:00
|
|
|
array(
|
|
|
|
'Name:StartsWith'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-10-04 15:59:36 +02:00
|
|
|
function testSearch() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$team2 = $this->objFromFixture(Team::class, 'team2');
|
2012-03-01 14:32:30 +01:00
|
|
|
|
2012-03-09 00:54:02 +01:00
|
|
|
$response = $this->get('GridFieldAddExistingAutocompleterTest_Controller');
|
2012-03-01 14:32:30 +01:00
|
|
|
$this->assertFalse($response->isError());
|
|
|
|
$parser = new CSSContentParser($response->getBody());
|
2016-07-01 03:37:29 +02:00
|
|
|
$btns = $parser->getBySelector('.grid-field .action_gridfield_relationfind');
|
2012-03-01 14:32:30 +01:00
|
|
|
|
|
|
|
$response = $this->post(
|
2012-09-26 23:34:00 +02:00
|
|
|
'GridFieldAddExistingAutocompleterTest_Controller/Form/field/testfield/search'
|
|
|
|
. '/?gridfield_relationsearch=Team 2',
|
2012-10-04 15:59:36 +02:00
|
|
|
array((string)$btns[0]['name'] => 1)
|
2012-03-01 14:32:30 +01:00
|
|
|
);
|
|
|
|
$this->assertFalse($response->isError());
|
|
|
|
$result = Convert::json2array($response->getBody());
|
|
|
|
$this->assertEquals(1, count($result));
|
2015-08-15 00:10:58 +02:00
|
|
|
$this->assertEquals(array(array(
|
|
|
|
'label' => 'Team 2',
|
|
|
|
'value' => 'Team 2',
|
|
|
|
'id' => $team2->ID,
|
|
|
|
)), $result);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-10-04 15:59:36 +02:00
|
|
|
$response = $this->post(
|
|
|
|
'GridFieldAddExistingAutocompleterTest_Controller/Form/field/testfield/'
|
|
|
|
. 'search/?gridfield_relationsearch=Heather',
|
|
|
|
array((string)$btns[0]['name'] => 1)
|
|
|
|
);
|
|
|
|
$this->assertFalse($response->isError());
|
|
|
|
$result = Convert::json2array($response->getBody());
|
|
|
|
$this->assertEquals(1, count($result), "The relational filter did not work");
|
2012-03-01 14:32:30 +01:00
|
|
|
|
|
|
|
$response = $this->post(
|
2012-09-26 23:34:00 +02:00
|
|
|
'GridFieldAddExistingAutocompleterTest_Controller/Form/field/testfield/search'
|
|
|
|
. '/?gridfield_relationsearch=Unknown',
|
2012-10-04 15:59:36 +02:00
|
|
|
array((string)$btns[0]['name'] => 1)
|
2012-03-01 14:32:30 +01:00
|
|
|
);
|
|
|
|
$this->assertFalse($response->isError());
|
|
|
|
$result = Convert::json2array($response->getBody());
|
2012-03-30 05:18:14 +02:00
|
|
|
$this->assertEmpty($result, 'The output is either an empty array or boolean FALSE');
|
2012-03-01 14:32:30 +01:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testAdd() {
|
2012-03-08 01:58:53 +01:00
|
|
|
$this->logInWithPermission('ADMIN');
|
2016-10-14 03:30:05 +02:00
|
|
|
$team1 = $this->objFromFixture(Team::class, 'team1');
|
|
|
|
$team2 = $this->objFromFixture(Team::class, 'team2');
|
2012-03-01 14:32:30 +01:00
|
|
|
|
2012-03-09 00:54:02 +01:00
|
|
|
$response = $this->get('GridFieldAddExistingAutocompleterTest_Controller');
|
2012-03-01 14:32:30 +01:00
|
|
|
$this->assertFalse($response->isError());
|
|
|
|
$parser = new CSSContentParser($response->getBody());
|
2016-07-01 03:37:29 +02:00
|
|
|
$items = $parser->getBySelector('.grid-field .ss-gridfield-items .ss-gridfield-item');
|
2012-03-01 14:32:30 +01:00
|
|
|
$this->assertEquals(1, count($items));
|
|
|
|
$this->assertEquals($team1->ID, (int)$items[0]['data-id']);
|
|
|
|
|
2016-07-01 03:37:29 +02:00
|
|
|
$btns = $parser->getBySelector('.grid-field .action_gridfield_relationadd');
|
2012-03-01 14:32:30 +01:00
|
|
|
$response = $this->post(
|
2012-03-09 00:54:02 +01:00
|
|
|
'GridFieldAddExistingAutocompleterTest_Controller/Form/field/testfield',
|
2012-03-01 14:32:30 +01:00
|
|
|
array(
|
|
|
|
'relationID' => $team2->ID,
|
|
|
|
(string)$btns[0]['name'] => 1
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertFalse($response->isError());
|
|
|
|
$parser = new CSSContentParser($response->getBody());
|
2016-07-01 03:37:29 +02:00
|
|
|
$items = $parser->getBySelector('.grid-field .ss-gridfield-items .ss-gridfield-item');
|
2012-03-01 14:32:30 +01:00
|
|
|
$this->assertEquals(2, count($items));
|
2012-04-16 04:30:13 +02:00
|
|
|
$this->assertDOSEquals(array(
|
|
|
|
array('ID' => (int)$items[0]['data-id']),
|
|
|
|
array('ID' => (int)$items[1]['data-id']),
|
|
|
|
), new ArrayList(array($team1, $team2)));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-03-01 14:32:30 +01:00
|
|
|
}
|
2012-03-30 05:18:14 +02:00
|
|
|
}
|