2012-03-01 14:32:30 +01:00
|
|
|
<?php
|
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
|
|
|
|
2012-10-04 15:59:36 +02:00
|
|
|
protected $extraDataObjects = array('GridFieldTest_Team', 'GridFieldTest_Player', 'GridFieldTest_Cheerleader');
|
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'));
|
|
|
|
$gridFieldTest_Team = singleton('GridFieldTest_Team');
|
|
|
|
$this->assertEquals(
|
2014-08-15 08:53:05 +02:00
|
|
|
$autoCompleter->scaffoldSearchFields('GridFieldTest_Team'),
|
2012-10-04 15:59:36 +02:00
|
|
|
array(
|
|
|
|
'Name:PartialMatch',
|
|
|
|
'City:StartsWith',
|
|
|
|
'Cheerleaders.Name:StartsWith'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2014-08-15 08:53:05 +02:00
|
|
|
$autoCompleter->scaffoldSearchFields('GridFieldTest_Cheerleader'),
|
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() {
|
2012-03-01 14:32:30 +01:00
|
|
|
$team1 = $this->objFromFixture('GridFieldTest_Team', 'team1');
|
|
|
|
$team2 = $this->objFromFixture('GridFieldTest_Team', 'team2');
|
|
|
|
|
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-05-02 07:37:03 +02:00
|
|
|
$btns = $parser->getBySelector('.ss-gridfield .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));
|
|
|
|
$this->assertEquals(array($team2->ID => 'Team 2'), $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');
|
2012-03-01 14:32:30 +01:00
|
|
|
$team1 = $this->objFromFixture('GridFieldTest_Team', 'team1');
|
|
|
|
$team2 = $this->objFromFixture('GridFieldTest_Team', 'team2');
|
|
|
|
|
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());
|
|
|
|
$items = $parser->getBySelector('.ss-gridfield .ss-gridfield-items .ss-gridfield-item');
|
|
|
|
$this->assertEquals(1, count($items));
|
|
|
|
$this->assertEquals($team1->ID, (int)$items[0]['data-id']);
|
|
|
|
|
2016-05-02 07:37:03 +02:00
|
|
|
$btns = $parser->getBySelector('.ss-gridfield .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());
|
|
|
|
$items = $parser->getBySelector('.ss-gridfield .ss-gridfield-items .ss-gridfield-item');
|
|
|
|
$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-09 00:54:02 +01:00
|
|
|
class GridFieldAddExistingAutocompleterTest_Controller extends Controller implements TestOnly {
|
2012-03-01 14:32:30 +01:00
|
|
|
|
2013-06-20 11:40:55 +02:00
|
|
|
private static $allowed_actions = array('Form');
|
|
|
|
|
2012-03-01 14:32:30 +01:00
|
|
|
protected $template = 'BlankPage';
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Form() {
|
2012-03-01 14:32:30 +01:00
|
|
|
$player = DataObject::get('GridFieldTest_Player')->find('Email', 'player1@test.com');
|
|
|
|
$config = GridFieldConfig::create()->addComponents(
|
2012-10-04 15:59:36 +02:00
|
|
|
$relationComponent = new GridFieldAddExistingAutocompleter('before'),
|
2012-03-09 02:07:40 +01:00
|
|
|
new GridFieldDataColumns()
|
2012-03-01 14:32:30 +01:00
|
|
|
);
|
|
|
|
$field = new GridField('testfield', 'testfield', $player->Teams(), $config);
|
|
|
|
return new Form($this, 'Form', new FieldList($field), new FieldList());
|
|
|
|
}
|
2012-03-30 05:18:14 +02:00
|
|
|
}
|