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;
|
|
|
|
use SilverStripe\ORM\Versioning\Versioned;
|
2016-10-14 03:30:05 +02:00
|
|
|
use InvalidArgumentException;
|
2016-09-26 07:22:19 +02:00
|
|
|
|
|
|
|
class ManyManyThroughListTest extends SapphireTest
|
|
|
|
{
|
|
|
|
protected static $fixture_file = 'ManyManyThroughListTest.yml';
|
|
|
|
|
|
|
|
protected $extraDataObjects = [
|
2016-10-14 03:30:05 +02:00
|
|
|
ManyManyThroughListTest\Item::class,
|
|
|
|
ManyManyThroughListTest\JoinObject::class,
|
|
|
|
ManyManyThroughListTest\TestObject::class,
|
|
|
|
ManyManyThroughListTest\VersionedItem::class,
|
|
|
|
ManyManyThroughListTest\VersionedJoinObject::class,
|
|
|
|
ManyManyThroughListTest\VersionedObject::class,
|
2016-09-26 07:22:19 +02:00
|
|
|
];
|
|
|
|
|
2016-10-03 03:51:34 +02:00
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
DataObject::reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown() {
|
|
|
|
DataObject::reset();
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
2016-09-26 07:22:19 +02:00
|
|
|
public function testSelectJoin() {
|
2016-10-14 03:30:05 +02:00
|
|
|
/** @var \SilverStripe\ORM\Tests\ManyManyThroughListTest\ManyManyThroughListTest_Object $parent */
|
|
|
|
$parent = $this->objFromFixture(ManyManyThroughListTest\TestObject::class, 'parent1');
|
2016-09-26 07:22:19 +02:00
|
|
|
$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);
|
2016-10-03 03:51:34 +02:00
|
|
|
$this->assertInstanceOf(
|
2016-10-14 03:30:05 +02:00
|
|
|
ManyManyThroughListTest\JoinObject::class,
|
2016-10-03 03:51:34 +02:00
|
|
|
$item1->ManyManyThroughListTest_JoinObject
|
|
|
|
);
|
|
|
|
$this->assertEquals('join 1', $item1->ManyManyThroughListTest_JoinObject->Title);
|
2016-09-26 07:22:19 +02:00
|
|
|
|
|
|
|
// 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);
|
2016-10-03 03:51:34 +02:00
|
|
|
$this->assertEquals('join 2', $item2->ManyManyThroughListTest_JoinObject->Title);
|
2016-09-26 07:22:19 +02:00
|
|
|
|
|
|
|
// To filter on join table need to use some raw sql
|
2016-10-03 03:51:34 +02:00
|
|
|
$item2 = $parent->Items()->where(['"ManyManyThroughListTest_JoinObject"."Title"' => 'join 2'])->first();
|
2016-09-26 07:22:19 +02:00
|
|
|
$this->assertNotNull($item2);
|
|
|
|
$this->assertEquals('item 2', $item2->Title);
|
|
|
|
$this->assertNotNull($item2->getJoin());
|
|
|
|
$this->assertEquals('join 2', $item2->getJoin()->Title);
|
2016-10-03 03:51:34 +02:00
|
|
|
$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
|
|
|
|
);
|
2016-09-26 07:22:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAdd() {
|
2016-10-14 03:30:05 +02:00
|
|
|
/** @var \SilverStripe\ORM\Tests\ManyManyThroughListTest\ManyManyThroughListTest_Object $parent */
|
|
|
|
$parent = $this->objFromFixture(ManyManyThroughListTest\TestObject::class, 'parent1');
|
|
|
|
$newItem = new ManyManyThroughListTest\Item();
|
2016-09-26 07:22:19 +02:00
|
|
|
$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);
|
2016-10-03 03:51:34 +02:00
|
|
|
$this->assertInstanceOf(
|
2016-10-14 03:30:05 +02:00
|
|
|
ManyManyThroughListTest\JoinObject::class,
|
2016-10-03 03:51:34 +02:00
|
|
|
$newItem->getJoin()
|
|
|
|
);
|
|
|
|
$this->assertInstanceOf(
|
2016-10-14 03:30:05 +02:00
|
|
|
ManyManyThroughListTest\JoinObject::class,
|
2016-10-03 03:51:34 +02:00
|
|
|
$newItem->ManyManyThroughListTest_JoinObject
|
|
|
|
);
|
|
|
|
$this->assertEquals('new join record', $newItem->ManyManyThroughListTest_JoinObject->Title);
|
2016-09-26 07:22:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testRemove() {
|
2016-10-14 03:30:05 +02:00
|
|
|
/** @var \SilverStripe\ORM\Tests\ManyManyThroughListTest\ManyManyThroughListTest_Object $parent */
|
|
|
|
$parent = $this->objFromFixture(ManyManyThroughListTest\TestObject::class, 'parent1');
|
2016-09-26 07:22:19 +02:00
|
|
|
$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()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testPublishing() {
|
2016-10-14 03:30:05 +02:00
|
|
|
/** @var \SilverStripe\ORM\Tests\ManyManyThroughListTest\ManyManyThroughListTest_VersionedObject $draftParent */
|
|
|
|
$draftParent = $this->objFromFixture(ManyManyThroughListTest\VersionedObject::class, 'parent1');
|
2016-09-26 07:22:19 +02:00
|
|
|
$draftParent->publishRecursive();
|
|
|
|
|
|
|
|
// Modify draft stage
|
|
|
|
$item1 = $draftParent->Items()->filter(['Title' => 'versioned item 1'])->first();
|
|
|
|
$item1->Title = 'new versioned item 1';
|
|
|
|
$item1->getJoin()->Title = 'new versioned join 1';
|
|
|
|
$item1->write(false, false, false, true); // Write joined components
|
|
|
|
$draftParent->Title = 'new versioned title';
|
|
|
|
$draftParent->write();
|
|
|
|
|
|
|
|
// Check owned objects on stage
|
|
|
|
$draftOwnedObjects = $draftParent->findOwned(true);
|
|
|
|
$this->assertDOSEquals(
|
|
|
|
[
|
|
|
|
['Title' => 'new versioned join 1'],
|
|
|
|
['Title' => 'versioned join 2'],
|
|
|
|
['Title' => 'new versioned item 1'],
|
|
|
|
['Title' => 'versioned item 2'],
|
|
|
|
],
|
|
|
|
$draftOwnedObjects
|
|
|
|
);
|
|
|
|
|
|
|
|
// Check live record is still old values
|
|
|
|
// This tests that both the join table and many_many tables
|
|
|
|
// inherit the necessary query parameters from the parent object.
|
2016-10-14 03:30:05 +02:00
|
|
|
/** @var \SilverStripe\ORM\Tests\ManyManyThroughListTest\ManyManyThroughListTest_VersionedObject $liveParent */
|
2016-09-26 07:22:19 +02:00
|
|
|
$liveParent = Versioned::get_by_stage(
|
2016-10-14 03:30:05 +02:00
|
|
|
ManyManyThroughListTest\VersionedObject::class,
|
2016-09-26 07:22:19 +02:00
|
|
|
Versioned::LIVE
|
|
|
|
)->byID($draftParent->ID);
|
|
|
|
$liveOwnedObjects = $liveParent->findOwned(true);
|
|
|
|
$this->assertDOSEquals(
|
|
|
|
[
|
|
|
|
['Title' => 'versioned join 1'],
|
|
|
|
['Title' => 'versioned join 2'],
|
|
|
|
['Title' => 'versioned item 1'],
|
|
|
|
['Title' => 'versioned item 2'],
|
|
|
|
],
|
|
|
|
$liveOwnedObjects
|
|
|
|
);
|
|
|
|
|
|
|
|
// Publish draft changes
|
|
|
|
$draftParent->publishRecursive();
|
|
|
|
$liveParent = Versioned::get_by_stage(
|
2016-10-14 03:30:05 +02:00
|
|
|
ManyManyThroughListTest\VersionedObject::class,
|
2016-09-26 07:22:19 +02:00
|
|
|
Versioned::LIVE
|
|
|
|
)->byID($draftParent->ID);
|
|
|
|
$liveOwnedObjects = $liveParent->findOwned(true);
|
|
|
|
$this->assertDOSEquals(
|
|
|
|
[
|
|
|
|
['Title' => 'new versioned join 1'],
|
|
|
|
['Title' => 'versioned join 2'],
|
|
|
|
['Title' => 'new versioned item 1'],
|
|
|
|
['Title' => 'versioned item 2'],
|
|
|
|
],
|
|
|
|
$liveOwnedObjects
|
|
|
|
);
|
|
|
|
}
|
2016-10-03 03:51:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test validation
|
|
|
|
*/
|
|
|
|
public function testValidateModelValidatesJoinType() {
|
|
|
|
DataObject::reset();
|
2016-10-14 03:30:05 +02:00
|
|
|
ManyManyThroughListTest\Item::config()->update('db', [
|
|
|
|
ManyManyThroughListTest\JoinObject::class => 'Text'
|
2016-10-03 03:51:34 +02:00
|
|
|
]);
|
|
|
|
$this->setExpectedException(InvalidArgumentException::class);
|
2016-10-14 03:30:05 +02:00
|
|
|
DataObject::getSchema()->manyManyComponent(ManyManyThroughListTest\TestObject::class, 'Items');
|
2016-10-06 06:31:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testRelationParsing() {
|
|
|
|
$schema = DataObject::getSchema();
|
|
|
|
|
|
|
|
// Parent components
|
|
|
|
$this->assertEquals(
|
|
|
|
[
|
|
|
|
ManyManyThroughList::class,
|
2016-10-14 03:30:05 +02:00
|
|
|
ManyManyThroughListTest\TestObject::class,
|
|
|
|
ManyManyThroughListTest\Item::class,
|
2016-10-06 06:31:38 +02:00
|
|
|
'ParentID',
|
|
|
|
'ChildID',
|
2016-10-14 03:30:05 +02:00
|
|
|
ManyManyThroughListTest\JoinObject::class
|
2016-10-06 06:31:38 +02:00
|
|
|
],
|
2016-10-14 03:30:05 +02:00
|
|
|
$schema->manyManyComponent(ManyManyThroughListTest\TestObject::class, 'Items')
|
2016-10-06 06:31:38 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
// Belongs_many_many is the same, but with parent/child substituted
|
|
|
|
$this->assertEquals(
|
|
|
|
[
|
|
|
|
ManyManyThroughList::class,
|
2016-10-14 03:30:05 +02:00
|
|
|
ManyManyThroughListTest\Item::class,
|
|
|
|
ManyManyThroughListTest\TestObject::class,
|
2016-10-06 06:31:38 +02:00
|
|
|
'ChildID',
|
|
|
|
'ParentID',
|
2016-10-14 03:30:05 +02:00
|
|
|
ManyManyThroughListTest\JoinObject::class
|
2016-10-06 06:31:38 +02:00
|
|
|
],
|
2016-10-14 03:30:05 +02:00
|
|
|
$schema->manyManyComponent(ManyManyThroughListTest\Item::class, 'Objects')
|
2016-10-06 06:31:38 +02:00
|
|
|
);
|
2016-10-03 03:51:34 +02:00
|
|
|
}
|
2016-09-26 07:22:19 +02:00
|
|
|
}
|
|
|
|
|