2009-05-01 05:49:34 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2009-05-01 05:49:34 +02:00
|
|
|
* @subpackage tests
|
|
|
|
*/
|
|
|
|
class ComplexTableFieldTest extends FunctionalTest {
|
|
|
|
|
2011-03-30 08:49:11 +02:00
|
|
|
static $fixture_file = 'ComplexTableFieldTest.yml';
|
2009-10-11 02:07:28 +02:00
|
|
|
static $use_draft_site = true;
|
2009-05-01 05:49:34 +02:00
|
|
|
|
2010-04-12 04:03:16 +02:00
|
|
|
protected $extraDataObjects = array(
|
|
|
|
'ComplexTableFieldTest_Player',
|
|
|
|
'ComplexTableFieldTest_Team',
|
|
|
|
'ComplexTableFieldTest_Sponsor',
|
|
|
|
);
|
|
|
|
|
2009-05-01 05:49:34 +02:00
|
|
|
/**
|
|
|
|
* An instance of {@link Controller} used for
|
|
|
|
* running tests against.
|
|
|
|
*
|
|
|
|
* @var Controller object
|
|
|
|
*/
|
|
|
|
protected $controller;
|
|
|
|
|
2011-03-23 05:08:02 +01:00
|
|
|
protected $autoFollowRedirection = false;
|
|
|
|
|
2009-05-01 05:49:34 +02:00
|
|
|
function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->controller = new ComplexTableFieldTest_Controller();
|
|
|
|
$this->manyManyForm = $this->controller->ManyManyForm();
|
|
|
|
}
|
|
|
|
|
|
|
|
function testCorrectNumberOfRowsInTable() {
|
2012-01-02 15:03:35 +01:00
|
|
|
$field = $this->manyManyForm->Fields()->dataFieldByName('Players');
|
2009-05-01 05:49:34 +02:00
|
|
|
$parser = new CSSContentParser($field->FieldHolder());
|
|
|
|
|
|
|
|
$this->assertEquals(count($parser->getBySelector('tbody tr')), 2, 'There are 2 players (rows) in the table');
|
2011-10-26 08:09:04 +02:00
|
|
|
$this->assertEquals($field->Items()->Count(), 2, 'There are 2 CTF items in the SS_List');
|
2009-05-01 05:49:34 +02:00
|
|
|
}
|
|
|
|
|
2009-05-04 03:20:12 +02:00
|
|
|
function testAddingManyManyNewPlayer() {
|
2010-10-19 05:00:45 +02:00
|
|
|
$this->logInWithPermission('ADMIN');
|
|
|
|
|
2009-09-17 02:03:22 +02:00
|
|
|
$team = DataObject::get_one('ComplexTableFieldTest_Team', "\"Name\" = 'The Awesome People'");
|
2009-05-01 05:49:34 +02:00
|
|
|
|
|
|
|
$this->post('ComplexTableFieldTest_Controller/ManyManyForm/field/Players/AddForm', array(
|
|
|
|
'Name' => 'Bobby Joe',
|
|
|
|
'ctf' => array(
|
|
|
|
'ClassName' => 'ComplexTableFieldTest_Player',
|
|
|
|
'manyManyRelation' => 'Players',
|
|
|
|
'parentClass' => 'ComplexTableFieldTest_Team',
|
|
|
|
'sourceID' => $team->ID
|
|
|
|
)
|
|
|
|
));
|
|
|
|
|
2009-09-17 02:03:22 +02:00
|
|
|
$newPlayer = DataObject::get_one('ComplexTableFieldTest_Player', "\"Name\" = 'Bobby Joe'");
|
2009-05-01 05:49:34 +02:00
|
|
|
$this->assertNotNull($newPlayer, 'A new ComplexTableFieldTest_Player record was created, Name = "Bobby Joe"');
|
|
|
|
$teams = $newPlayer->getManyManyComponents('Teams');
|
|
|
|
$this->assertEquals($teams->Count(), 1, 'Automatic many-many relation was set correctly on the new player');
|
|
|
|
}
|
|
|
|
|
|
|
|
function testAddingHasManyData() {
|
2010-10-19 05:00:45 +02:00
|
|
|
$this->logInWithPermission('ADMIN');
|
|
|
|
|
2009-09-17 02:03:22 +02:00
|
|
|
$team = DataObject::get_one('ComplexTableFieldTest_Team', "\"Name\" = 'The Awesome People'");
|
2009-05-01 05:49:34 +02:00
|
|
|
|
|
|
|
$this->post('ComplexTableFieldTest_Controller/HasManyForm/field/Sponsors/AddForm', array(
|
|
|
|
'Name' => 'Jim Beam',
|
|
|
|
'ctf' => array(
|
|
|
|
'ClassName' => 'ComplexTableFieldTest_Sponsor',
|
|
|
|
'hasManyRelation' => 'Sponsors',
|
|
|
|
'parentClass' => 'ComplexTableFieldTest_Team',
|
|
|
|
'sourceID' => $team->ID
|
|
|
|
)
|
|
|
|
));
|
|
|
|
|
2009-09-17 02:03:22 +02:00
|
|
|
$newSponsor = DataObject::get_one('ComplexTableFieldTest_Sponsor', "\"Name\" = 'Jim Beam'");
|
2009-05-01 05:49:34 +02:00
|
|
|
$this->assertNotNull($newSponsor, 'A new ComplexTableFieldTest_Sponsor record was created, Name = "Jim Beam"');
|
2009-05-26 04:19:50 +02:00
|
|
|
$this->assertEquals($newSponsor->TeamID, $team->ID, 'Automatic has-many/has-one relation was set correctly on the sponsor');
|
|
|
|
$this->assertEquals($newSponsor->getComponent('Team')->ID, $team->ID, 'Automatic has-many/has-one relation was set correctly on the sponsor');
|
2009-05-01 05:49:34 +02:00
|
|
|
|
2009-05-26 04:19:50 +02:00
|
|
|
$team = DataObject::get_by_id('ComplexTableFieldTest_Team', $team->ID);
|
|
|
|
$sponsor = DataObject::get_by_id('ComplexTableFieldTest_Sponsor', $newSponsor->ID);
|
2009-05-01 05:49:34 +02:00
|
|
|
$this->assertEquals($newSponsor->ID, $sponsor->ID, 'The sponsor is the same as the one we added');
|
2009-05-26 04:19:50 +02:00
|
|
|
$foundTeam = $sponsor->getComponent('Team');
|
|
|
|
$this->assertEquals($team->ID, $foundTeam->ID, 'The team ID matches on the other side of the relation');
|
2009-05-01 05:49:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
class ComplexTableFieldTest_Controller extends Controller {
|
|
|
|
|
|
|
|
function Link($action = null) {
|
|
|
|
return "ComplexTableFieldTest_Controller/$action";
|
|
|
|
}
|
|
|
|
|
|
|
|
function ManyManyForm() {
|
2009-09-17 02:03:22 +02:00
|
|
|
$team = DataObject::get_one('ComplexTableFieldTest_Team', "\"Name\" = 'The Awesome People'");
|
2009-05-01 05:49:34 +02:00
|
|
|
|
|
|
|
$playersField = new ComplexTableField(
|
|
|
|
$this,
|
|
|
|
'Players',
|
2011-03-30 03:28:50 +02:00
|
|
|
$team->Players(),
|
2009-05-01 05:49:34 +02:00
|
|
|
ComplexTableFieldTest_Player::$summary_fields,
|
|
|
|
'getCMSFields'
|
|
|
|
);
|
|
|
|
|
|
|
|
$form = new Form(
|
|
|
|
$this,
|
|
|
|
'ManyManyForm',
|
2011-05-11 09:51:54 +02:00
|
|
|
new FieldList(
|
2009-05-01 05:49:34 +02:00
|
|
|
new HiddenField('ID', '', $team->ID),
|
|
|
|
$playersField
|
|
|
|
),
|
2011-05-11 09:51:54 +02:00
|
|
|
new FieldList(
|
2009-05-01 05:49:34 +02:00
|
|
|
new FormAction('doSubmit', 'Submit')
|
|
|
|
)
|
|
|
|
);
|
2009-11-22 07:07:45 +01:00
|
|
|
$form->loadDataFrom($team);
|
2009-05-01 05:49:34 +02:00
|
|
|
|
|
|
|
$form->disableSecurityToken();
|
|
|
|
|
|
|
|
return $form;
|
|
|
|
}
|
|
|
|
|
|
|
|
function HasManyForm() {
|
2009-09-17 02:03:22 +02:00
|
|
|
$team = DataObject::get_one('ComplexTableFieldTest_Team', "\"Name\" = 'The Awesome People'");
|
2009-05-01 05:49:34 +02:00
|
|
|
|
|
|
|
$sponsorsField = new ComplexTableField(
|
|
|
|
$this,
|
|
|
|
'Sponsors',
|
2011-03-30 03:28:50 +02:00
|
|
|
$team->Sponsors(),
|
2009-05-01 05:49:34 +02:00
|
|
|
ComplexTableFieldTest_Sponsor::$summary_fields,
|
|
|
|
'getCMSFields'
|
|
|
|
);
|
|
|
|
|
|
|
|
$form = new Form(
|
|
|
|
$this,
|
|
|
|
'HasManyForm',
|
2011-05-11 09:51:54 +02:00
|
|
|
new FieldList(
|
2009-05-01 05:49:34 +02:00
|
|
|
new HiddenField('ID', '', $team->ID),
|
|
|
|
$sponsorsField
|
|
|
|
),
|
2011-05-11 09:51:54 +02:00
|
|
|
new FieldList(
|
2009-05-01 05:49:34 +02:00
|
|
|
new FormAction('doSubmit', 'Submit')
|
|
|
|
)
|
|
|
|
);
|
2009-11-22 07:07:45 +01:00
|
|
|
$form->loadDataFrom($team);
|
2009-05-01 05:49:34 +02:00
|
|
|
|
|
|
|
$form->disableSecurityToken();
|
|
|
|
|
|
|
|
return $form;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
class ComplexTableFieldTest_Player extends DataObject implements TestOnly {
|
|
|
|
|
|
|
|
public static $db = array(
|
|
|
|
'Name' => 'Varchar(100)'
|
|
|
|
);
|
|
|
|
|
|
|
|
public static $many_many = array(
|
|
|
|
'Teams' => 'ComplexTableFieldTest_Team'
|
|
|
|
);
|
|
|
|
|
|
|
|
public static $many_many_extraFields = array(
|
|
|
|
'Teams' => array(
|
|
|
|
'Role' => 'Varchar(100)',
|
|
|
|
'Position' => "Enum('Admin,Player,Coach','Admin')",
|
|
|
|
'DateJoined' => 'Date'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
class ComplexTableFieldTest_Team extends DataObject implements TestOnly {
|
|
|
|
|
|
|
|
public static $db = array(
|
|
|
|
'Name' => 'Varchar(100)'
|
|
|
|
);
|
|
|
|
|
|
|
|
public static $belongs_many_many = array(
|
|
|
|
'Players' => 'ComplexTableFieldTest_Player'
|
|
|
|
);
|
|
|
|
|
|
|
|
public static $has_many = array(
|
|
|
|
'Sponsors' => 'ComplexTableFieldTest_Sponsor'
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
class ComplexTableFieldTest_Sponsor extends DataObject implements TestOnly {
|
|
|
|
|
|
|
|
public static $db = array(
|
|
|
|
'Name' => 'Varchar(100)'
|
|
|
|
);
|
|
|
|
|
|
|
|
public static $has_one = array(
|
|
|
|
'Team' => 'ComplexTableFieldTest_Team'
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|