2013-07-10 02:44:24 +02: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\Fan;
|
|
|
|
use SilverStripe\ORM\Tests\DataObjectTest\Team;
|
|
|
|
|
2013-07-10 02:44:24 +02:00
|
|
|
/**
|
|
|
|
* Tests the PolymorphicHasManyList class
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-07-10 02:44:24 +02:00
|
|
|
* @see PolymorphicHasManyList
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-07-10 02:44:24 +02:00
|
|
|
* @todo Complete
|
|
|
|
*/
|
2016-12-16 05:34:21 +01:00
|
|
|
class PolymorphicHasManyListTest extends SapphireTest
|
|
|
|
{
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
// Borrow the model from DataObjectTest
|
|
|
|
protected static $fixture_file = 'DataObjectTest.yml';
|
2013-07-10 02:44:24 +02:00
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
public static function getExtraDataObjects()
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
|
|
|
return array_merge(
|
|
|
|
DataObjectTest::$extra_data_objects,
|
|
|
|
ManyManyListTest::$extra_data_objects
|
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testRelationshipEmptyOnNewRecords()
|
|
|
|
{
|
|
|
|
// Relies on the fact that (unrelated) comments exist in the fixture file already
|
|
|
|
$newTeam = new Team(); // has_many Comments
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->assertEquals([], $newTeam->Fans()->column('ID'));
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
/**
|
|
|
|
* Test that DataList::relation works with PolymorphicHasManyList
|
|
|
|
*/
|
|
|
|
public function testFilterRelation()
|
|
|
|
{
|
|
|
|
// Check that expected teams exist
|
|
|
|
$list = Team::get();
|
|
|
|
$this->assertEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
['Subteam 1', 'Subteam 2', 'Subteam 3', 'Team 1', 'Team 2', 'Team 3'],
|
2016-12-16 05:34:21 +01:00
|
|
|
$list->sort('Title')->column('Title')
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
// Check that fan list exists
|
|
|
|
$fans = $list->relation('Fans');
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->assertEquals(['Damian', 'Mitch', 'Richard'], $fans->sort('Name')->column('Name'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
// Modify list of fans and retest
|
|
|
|
$team1 = $this->objFromFixture(DataObjectTest\Team::class, 'team1');
|
|
|
|
$subteam1 = $this->objFromFixture(DataObjectTest\SubTeam::class, 'subteam1');
|
|
|
|
$newFan1 = Fan::create();
|
|
|
|
$newFan1->Name = 'Bobby';
|
|
|
|
$newFan1->write();
|
|
|
|
$newFan2 = Fan::create();
|
|
|
|
$newFan2->Name = 'Mindy';
|
|
|
|
$newFan2->write();
|
|
|
|
$team1->Fans()->add($newFan1);
|
|
|
|
$subteam1->Fans()->add($newFan2);
|
|
|
|
$fans = Team::get()->relation('Fans');
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->assertEquals(['Bobby', 'Damian', 'Richard'], $team1->Fans()->sort('Name')->column('Name'));
|
|
|
|
$this->assertEquals(['Mindy', 'Mitch'], $subteam1->Fans()->sort('Name')->column('Name'));
|
|
|
|
$this->assertEquals(['Bobby', 'Damian', 'Mindy', 'Mitch', 'Richard'], $fans->sort('Name')->column('Name'));
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
/**
|
|
|
|
* Test that related objects can be removed from a relation
|
|
|
|
*/
|
|
|
|
public function testRemoveRelation()
|
|
|
|
{
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
// Check that expected teams exist
|
|
|
|
$list = Team::get();
|
|
|
|
$this->assertEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
['Subteam 1', 'Subteam 2', 'Subteam 3', 'Team 1', 'Team 2', 'Team 3'],
|
2016-12-16 05:34:21 +01:00
|
|
|
$list->sort('Title')->column('Title')
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
// Test that each team has the correct fans
|
|
|
|
$team1 = $this->objFromFixture(DataObjectTest\Team::class, 'team1');
|
|
|
|
$subteam1 = $this->objFromFixture(DataObjectTest\SubTeam::class, 'subteam1');
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->assertEquals(['Damian', 'Richard'], $team1->Fans()->sort('Name')->column('Name'));
|
|
|
|
$this->assertEquals(['Mitch'], $subteam1->Fans()->sort('Name')->column('Name'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
// Test that removing items from unrelated team has no effect
|
|
|
|
$team1fan = $this->objFromFixture(DataObjectTest\Fan::class, 'fan1');
|
|
|
|
$subteam1fan = $this->objFromFixture(DataObjectTest\Fan::class, 'fan4');
|
|
|
|
$team1->Fans()->remove($subteam1fan);
|
|
|
|
$subteam1->Fans()->remove($team1fan);
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->assertEquals(['Damian', 'Richard'], $team1->Fans()->sort('Name')->column('Name'));
|
|
|
|
$this->assertEquals(['Mitch'], $subteam1->Fans()->sort('Name')->column('Name'));
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals($team1->ID, $team1fan->FavouriteID);
|
|
|
|
$this->assertEquals(DataObjectTest\Team::class, $team1fan->FavouriteClass);
|
|
|
|
$this->assertEquals($subteam1->ID, $subteam1fan->FavouriteID);
|
|
|
|
$this->assertEquals(DataObjectTest\SubTeam::class, $subteam1fan->FavouriteClass);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
// Test that removing items from the related team resets the has_one relations on the fan
|
|
|
|
$team1fan = $this->objFromFixture(DataObjectTest\Fan::class, 'fan1');
|
|
|
|
$subteam1fan = $this->objFromFixture(DataObjectTest\Fan::class, 'fan4');
|
|
|
|
$team1->Fans()->remove($team1fan);
|
|
|
|
$subteam1->Fans()->remove($subteam1fan);
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->assertEquals(['Richard'], $team1->Fans()->sort('Name')->column('Name'));
|
|
|
|
$this->assertEquals([], $subteam1->Fans()->sort('Name')->column('Name'));
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEmpty($team1fan->FavouriteID);
|
|
|
|
$this->assertEmpty($team1fan->FavouriteClass);
|
|
|
|
$this->assertEmpty($subteam1fan->FavouriteID);
|
|
|
|
$this->assertEmpty($subteam1fan->FavouriteClass);
|
|
|
|
}
|
2022-05-02 08:25:20 +02:00
|
|
|
|
|
|
|
public function testGetForeignClassKey(): void
|
|
|
|
{
|
|
|
|
$team = $this->objFromFixture(DataObjectTest\Team::class, 'team1');
|
|
|
|
$list = $team->Fans();
|
|
|
|
$this->assertSame('FavouriteClass', $list->getForeignClassKey());
|
|
|
|
}
|
2013-07-10 02:44:24 +02:00
|
|
|
}
|