2012-03-05 23:59:27 +01:00
|
|
|
<?php
|
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\ORM\Tests;
|
|
|
|
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2016-10-14 03:30:05 +02:00
|
|
|
use SilverStripe\ORM\Tests\DataObjectTest\Team;
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2012-03-05 23:59:27 +01:00
|
|
|
class HasManyListTest extends SapphireTest {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-03-05 23:59:27 +01:00
|
|
|
// Borrow the model from DataObjectTest
|
2013-03-21 19:48:54 +01:00
|
|
|
protected static $fixture_file = 'DataObjectTest.yml';
|
2012-03-05 23:59:27 +01:00
|
|
|
|
2016-11-13 08:35:43 +01:00
|
|
|
protected function getExtraDataObjects()
|
|
|
|
{
|
|
|
|
return array_merge(
|
|
|
|
DataObjectTest::$extra_data_objects,
|
|
|
|
ManyManyListTest::$extra_data_objects
|
|
|
|
);
|
2016-05-25 07:09:29 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-03-05 23:59:27 +01:00
|
|
|
public function testRelationshipEmptyOnNewRecords() {
|
|
|
|
// Relies on the fact that (unrelated) comments exist in the fixture file already
|
2016-10-14 03:30:05 +02:00
|
|
|
$newTeam = new Team(); // has_many Comments
|
2012-03-05 23:59:27 +01:00
|
|
|
$this->assertEquals(array(), $newTeam->Comments()->column('ID'));
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-07-12 05:18:06 +02:00
|
|
|
/**
|
|
|
|
* Test that related objects can be removed from a relation
|
|
|
|
*/
|
|
|
|
public function testRemoveRelation() {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-07-12 05:18:06 +02:00
|
|
|
// Check that expected teams exist
|
2016-10-14 03:30:05 +02:00
|
|
|
$list = Team::get();
|
2013-07-12 05:18:06 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
array('Subteam 1', 'Subteam 2', 'Subteam 3', 'Team 1', 'Team 2', 'Team 3'),
|
|
|
|
$list->sort('Title')->column('Title')
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-07-12 05:18:06 +02:00
|
|
|
// Test that each team has the correct fans
|
2016-11-13 08:35:43 +01:00
|
|
|
$team1 = $this->objFromFixture(DataObjectTest\Team::class, 'team1');
|
|
|
|
$team2 = $this->objFromFixture(DataObjectTest\Team::class, 'team2');
|
2013-07-12 05:18:06 +02:00
|
|
|
$this->assertEquals(array('Bob', 'Joe'), $team1->Comments()->sort('Name')->column('Name'));
|
|
|
|
$this->assertEquals(array('Phil'), $team2->Comments()->sort('Name')->column('Name'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-07-12 05:18:06 +02:00
|
|
|
// Test that removing comments from unrelated team has no effect
|
2016-11-13 08:35:43 +01:00
|
|
|
$team1comment = $this->objFromFixture(DataObjectTest\TeamComment::class, 'comment1');
|
|
|
|
$team2comment = $this->objFromFixture(DataObjectTest\TeamComment::class, 'comment3');
|
2013-07-12 05:18:06 +02:00
|
|
|
$team1->Comments()->remove($team2comment);
|
|
|
|
$team2->Comments()->remove($team1comment);
|
|
|
|
$this->assertEquals(array('Bob', 'Joe'), $team1->Comments()->sort('Name')->column('Name'));
|
|
|
|
$this->assertEquals(array('Phil'), $team2->Comments()->sort('Name')->column('Name'));
|
|
|
|
$this->assertEquals($team1->ID, $team1comment->TeamID);
|
|
|
|
$this->assertEquals($team2->ID, $team2comment->TeamID);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-07-12 05:18:06 +02:00
|
|
|
// Test that removing items from the related team resets the has_one relations on the fan
|
2016-11-13 08:35:43 +01:00
|
|
|
$team1comment = $this->objFromFixture(DataObjectTest\TeamComment::class, 'comment1');
|
|
|
|
$team2comment = $this->objFromFixture(DataObjectTest\TeamComment::class, 'comment3');
|
2013-07-12 05:18:06 +02:00
|
|
|
$team1->Comments()->remove($team1comment);
|
|
|
|
$team2->Comments()->remove($team2comment);
|
|
|
|
$this->assertEquals(array('Bob'), $team1->Comments()->sort('Name')->column('Name'));
|
|
|
|
$this->assertEquals(array(), $team2->Comments()->sort('Name')->column('Name'));
|
|
|
|
$this->assertEmpty($team1comment->TeamID);
|
|
|
|
$this->assertEmpty($team2comment->TeamID);
|
|
|
|
}
|
2012-03-05 23:59:27 +01:00
|
|
|
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|