2010-02-22 05:37:32 +01:00
|
|
|
<?php
|
2016-06-15 06:03:16 +02:00
|
|
|
|
|
|
|
use SilverStripe\ORM\DataObject;
|
2016-06-23 01:37:22 +02:00
|
|
|
use SilverStripe\Security\Member;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
use SilverStripe\Dev\TestOnly;
|
|
|
|
|
2016-06-23 01:37:22 +02:00
|
|
|
|
2010-02-22 05:37:32 +01:00
|
|
|
/**
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2010-02-22 05:37:32 +01:00
|
|
|
* @subpackage tests
|
|
|
|
*/
|
|
|
|
class ComponentSetTest extends SapphireTest {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
protected static $fixture_file = 'ComponentSetTest.yml';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-04-12 10:38:45 +02:00
|
|
|
protected $extraDataObjects = array(
|
|
|
|
'ComponentSetTest_Player',
|
|
|
|
'ComponentSetTest_Team'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testSetByIDListManyMany() {
|
2010-02-22 05:37:32 +01:00
|
|
|
$team1 = $this->objFromFixture('ComponentSetTest_Team', 'team1');
|
|
|
|
$player1_team1 = $this->objFromFixture('ComponentSetTest_Player', 'player1_team1');
|
|
|
|
$player2 = $this->objFromFixture('ComponentSetTest_Player', 'player2');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-02-22 05:37:32 +01:00
|
|
|
$team1->Players()->setByIdList(array(
|
|
|
|
$player1_team1->ID,
|
|
|
|
$player2->ID
|
|
|
|
));
|
|
|
|
$team1->flushCache();
|
|
|
|
$this->assertContains(
|
|
|
|
$player2->ID,
|
|
|
|
$team1->Players()->column('ID'),
|
|
|
|
'Can add new entry'
|
|
|
|
);
|
|
|
|
$this->assertContains(
|
|
|
|
$player1_team1->ID,
|
|
|
|
$team1->Players()->column('ID'),
|
|
|
|
'Can retain existing entry'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-02-22 05:37:32 +01:00
|
|
|
$team1->Players()->setByIdList(array(
|
|
|
|
$player1_team1->ID
|
|
|
|
));
|
|
|
|
$team1->flushCache();
|
|
|
|
$this->assertNotContains(
|
|
|
|
$player2->ID,
|
|
|
|
$team1->Players()->column('ID'),
|
|
|
|
'Can remove existing entry'
|
|
|
|
);
|
|
|
|
$this->assertContains(
|
|
|
|
$player1_team1->ID,
|
|
|
|
$team1->Players()->column('ID'),
|
|
|
|
'Can retain existing entry'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-02-22 05:37:32 +01:00
|
|
|
$team1->Players()->setByIdList(array());
|
|
|
|
$team1->flushCache();
|
|
|
|
$this->assertEquals(0, $team1->Players()->Count(),
|
|
|
|
'Can remove all entries by passing an empty array'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ComponentSetTest_Player extends Member implements TestOnly {
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $belongs_many_many = array(
|
2010-02-22 05:37:32 +01:00
|
|
|
'Teams' => 'ComponentSetTest_Team'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
class ComponentSetTest_Team extends DataObject implements TestOnly {
|
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2014-08-15 08:53:05 +02:00
|
|
|
'Title' => 'Varchar',
|
2010-02-22 05:37:32 +01:00
|
|
|
);
|
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $many_many = array(
|
2010-02-22 05:37:32 +01:00
|
|
|
'Players' => 'ComponentSetTest_Player'
|
|
|
|
);
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|