2016-09-26 07:22:19 +02:00
|
|
|
<?php
|
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\ORM\Tests;
|
|
|
|
|
2016-09-26 07:22:19 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
use SilverStripe\ORM\DataObject;
|
|
|
|
use SilverStripe\ORM\ManyManyThroughList;
|
2016-10-14 03:30:05 +02:00
|
|
|
use InvalidArgumentException;
|
2016-09-26 07:22:19 +02:00
|
|
|
|
|
|
|
class ManyManyThroughListTest extends SapphireTest
|
|
|
|
{
|
2016-12-16 05:34:21 +01:00
|
|
|
protected static $fixture_file = 'ManyManyThroughListTest.yml';
|
|
|
|
|
2017-03-24 12:17:26 +01:00
|
|
|
protected static $extra_dataobjects = [
|
2016-12-16 05:34:21 +01:00
|
|
|
ManyManyThroughListTest\Item::class,
|
|
|
|
ManyManyThroughListTest\JoinObject::class,
|
2017-03-21 04:22:23 +01:00
|
|
|
ManyManyThroughListTest\TestObject::class
|
2016-12-16 05:34:21 +01:00
|
|
|
];
|
|
|
|
|
2017-03-24 04:00:54 +01:00
|
|
|
protected function setUp()
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
DataObject::reset();
|
|
|
|
}
|
|
|
|
|
2017-03-24 04:00:54 +01:00
|
|
|
protected function tearDown()
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
|
|
|
DataObject::reset();
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSelectJoin()
|
|
|
|
{
|
2017-03-21 04:22:23 +01:00
|
|
|
/** @var ManyManyThroughListTest\TestObject $parent */
|
2016-12-16 05:34:21 +01:00
|
|
|
$parent = $this->objFromFixture(ManyManyThroughListTest\TestObject::class, 'parent1');
|
|
|
|
$this->assertDOSEquals(
|
|
|
|
[
|
|
|
|
['Title' => 'item 1'],
|
|
|
|
['Title' => 'item 2']
|
|
|
|
],
|
|
|
|
$parent->Items()
|
|
|
|
);
|
|
|
|
// Check filters on list work
|
|
|
|
$item1 = $parent->Items()->filter('Title', 'item 1')->first();
|
|
|
|
$this->assertNotNull($item1);
|
|
|
|
$this->assertNotNull($item1->getJoin());
|
|
|
|
$this->assertEquals('join 1', $item1->getJoin()->Title);
|
|
|
|
$this->assertInstanceOf(
|
|
|
|
ManyManyThroughListTest\JoinObject::class,
|
|
|
|
$item1->ManyManyThroughListTest_JoinObject
|
|
|
|
);
|
|
|
|
$this->assertEquals('join 1', $item1->ManyManyThroughListTest_JoinObject->Title);
|
|
|
|
|
|
|
|
// Check filters on list work
|
|
|
|
$item2 = $parent->Items()->filter('Title', 'item 2')->first();
|
|
|
|
$this->assertNotNull($item2);
|
|
|
|
$this->assertNotNull($item2->getJoin());
|
|
|
|
$this->assertEquals('join 2', $item2->getJoin()->Title);
|
|
|
|
$this->assertEquals('join 2', $item2->ManyManyThroughListTest_JoinObject->Title);
|
|
|
|
|
|
|
|
// To filter on join table need to use some raw sql
|
|
|
|
$item2 = $parent->Items()->where(['"ManyManyThroughListTest_JoinObject"."Title"' => 'join 2'])->first();
|
|
|
|
$this->assertNotNull($item2);
|
|
|
|
$this->assertEquals('item 2', $item2->Title);
|
|
|
|
$this->assertNotNull($item2->getJoin());
|
|
|
|
$this->assertEquals('join 2', $item2->getJoin()->Title);
|
|
|
|
$this->assertEquals('join 2', $item2->ManyManyThroughListTest_JoinObject->Title);
|
|
|
|
|
|
|
|
// Test sorting on join table
|
|
|
|
$items = $parent->Items()->sort('"ManyManyThroughListTest_JoinObject"."Sort"');
|
|
|
|
$this->assertDOSEquals(
|
|
|
|
[
|
|
|
|
['Title' => 'item 2'],
|
|
|
|
['Title' => 'item 1'],
|
|
|
|
],
|
|
|
|
$items
|
|
|
|
);
|
|
|
|
|
|
|
|
$items = $parent->Items()->sort('"ManyManyThroughListTest_JoinObject"."Sort" ASC');
|
|
|
|
$this->assertDOSEquals(
|
|
|
|
[
|
|
|
|
['Title' => 'item 1'],
|
|
|
|
['Title' => 'item 2'],
|
|
|
|
],
|
|
|
|
$items
|
|
|
|
);
|
|
|
|
$items = $parent->Items()->sort('"ManyManyThroughListTest_JoinObject"."Title" DESC');
|
|
|
|
$this->assertDOSEquals(
|
|
|
|
[
|
|
|
|
['Title' => 'item 2'],
|
|
|
|
['Title' => 'item 1'],
|
|
|
|
],
|
|
|
|
$items
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAdd()
|
|
|
|
{
|
2017-03-21 04:22:23 +01:00
|
|
|
/** @var ManyManyThroughListTest\TestObject $parent */
|
2016-12-16 05:34:21 +01:00
|
|
|
$parent = $this->objFromFixture(ManyManyThroughListTest\TestObject::class, 'parent1');
|
|
|
|
$newItem = new ManyManyThroughListTest\Item();
|
|
|
|
$newItem->Title = 'my new item';
|
|
|
|
$newItem->write();
|
|
|
|
$parent->Items()->add($newItem, ['Title' => 'new join record']);
|
|
|
|
|
|
|
|
// Check select
|
|
|
|
$newItem = $parent->Items()->filter(['Title' => 'my new item'])->first();
|
|
|
|
$this->assertNotNull($newItem);
|
|
|
|
$this->assertEquals('my new item', $newItem->Title);
|
|
|
|
$this->assertInstanceOf(
|
|
|
|
ManyManyThroughListTest\JoinObject::class,
|
|
|
|
$newItem->getJoin()
|
|
|
|
);
|
|
|
|
$this->assertInstanceOf(
|
|
|
|
ManyManyThroughListTest\JoinObject::class,
|
|
|
|
$newItem->ManyManyThroughListTest_JoinObject
|
|
|
|
);
|
|
|
|
$this->assertEquals('new join record', $newItem->ManyManyThroughListTest_JoinObject->Title);
|
|
|
|
}
|
2016-09-26 07:22:19 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testRemove()
|
|
|
|
{
|
2017-03-21 04:22:23 +01:00
|
|
|
/** @var ManyManyThroughListTest\TestObject $parent */
|
2016-12-16 05:34:21 +01:00
|
|
|
$parent = $this->objFromFixture(ManyManyThroughListTest\TestObject::class, 'parent1');
|
|
|
|
$this->assertDOSEquals(
|
|
|
|
[
|
|
|
|
['Title' => 'item 1'],
|
|
|
|
['Title' => 'item 2']
|
|
|
|
],
|
|
|
|
$parent->Items()
|
|
|
|
);
|
|
|
|
$item1 = $parent->Items()->filter(['Title' => 'item 1'])->first();
|
|
|
|
$parent->Items()->remove($item1);
|
|
|
|
$this->assertDOSEquals(
|
|
|
|
[['Title' => 'item 2']],
|
|
|
|
$parent->Items()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test validation
|
|
|
|
*/
|
|
|
|
public function testValidateModelValidatesJoinType()
|
|
|
|
{
|
|
|
|
DataObject::reset();
|
|
|
|
ManyManyThroughListTest\Item::config()->update(
|
|
|
|
'db',
|
|
|
|
[
|
|
|
|
ManyManyThroughListTest\JoinObject::class => 'Text'
|
|
|
|
]
|
|
|
|
);
|
|
|
|
$this->setExpectedException(InvalidArgumentException::class);
|
|
|
|
DataObject::getSchema()->manyManyComponent(ManyManyThroughListTest\TestObject::class, 'Items');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRelationParsing()
|
|
|
|
{
|
|
|
|
$schema = DataObject::getSchema();
|
|
|
|
|
|
|
|
// Parent components
|
|
|
|
$this->assertEquals(
|
|
|
|
[
|
2017-05-17 07:40:13 +02:00
|
|
|
'relationClass' => ManyManyThroughList::class,
|
|
|
|
'parentClass' => ManyManyThroughListTest\TestObject::class,
|
|
|
|
'childClass' => ManyManyThroughListTest\Item::class,
|
|
|
|
'parentField' => 'ParentID',
|
|
|
|
'childField' => 'ChildID',
|
|
|
|
'join' => ManyManyThroughListTest\JoinObject::class
|
2016-12-16 05:34:21 +01:00
|
|
|
],
|
|
|
|
$schema->manyManyComponent(ManyManyThroughListTest\TestObject::class, 'Items')
|
|
|
|
);
|
|
|
|
|
|
|
|
// Belongs_many_many is the same, but with parent/child substituted
|
|
|
|
$this->assertEquals(
|
|
|
|
[
|
2017-05-17 07:40:13 +02:00
|
|
|
'relationClass' => ManyManyThroughList::class,
|
|
|
|
'parentClass' => ManyManyThroughListTest\Item::class,
|
|
|
|
'childClass' => ManyManyThroughListTest\TestObject::class,
|
|
|
|
'parentField' => 'ChildID',
|
|
|
|
'childField' => 'ParentID',
|
|
|
|
'join' => ManyManyThroughListTest\JoinObject::class
|
2016-12-16 05:34:21 +01:00
|
|
|
],
|
|
|
|
$schema->manyManyComponent(ManyManyThroughListTest\Item::class, 'Objects')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|