2012-11-03 04:16:51 +01:00
|
|
|
<?php
|
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\ORM\Tests;
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
use SilverStripe\ORM\RelationList;
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
class UnsavedRelationListTest extends SapphireTest
|
|
|
|
{
|
|
|
|
protected static $fixture_file = 'UnsavedRelationListTest.yml';
|
|
|
|
|
2017-03-24 12:17:26 +01:00
|
|
|
protected static $extra_dataobjects = [
|
2016-12-16 05:34:21 +01:00
|
|
|
UnsavedRelationListTest\TestObject::class
|
|
|
|
];
|
|
|
|
|
|
|
|
public function testReturnedList()
|
|
|
|
{
|
|
|
|
$object = new UnsavedRelationListTest\TestObject();
|
|
|
|
$children = $object->Children();
|
|
|
|
$siblings = $object->Siblings();
|
|
|
|
$this->assertEquals(
|
|
|
|
$children,
|
|
|
|
$object->Children(),
|
|
|
|
'Returned UnsavedRelationList should be the same.'
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
$siblings,
|
|
|
|
$object->Siblings(),
|
|
|
|
'Returned UnsavedRelationList should be the same.'
|
|
|
|
);
|
|
|
|
|
|
|
|
$object->write();
|
|
|
|
$this->assertInstanceOf(RelationList::class, $object->Children());
|
|
|
|
$this->assertNotEquals(
|
|
|
|
$children,
|
|
|
|
$object->Children(),
|
|
|
|
'Return should be a RelationList after first write'
|
|
|
|
);
|
|
|
|
$this->assertInstanceOf(RelationList::class, $object->Siblings());
|
|
|
|
$this->assertNotEquals(
|
|
|
|
$siblings,
|
|
|
|
$object->Siblings(),
|
|
|
|
'Return should be a RelationList after first write'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testHasManyExisting()
|
|
|
|
{
|
|
|
|
$object = new UnsavedRelationListTest\TestObject();
|
|
|
|
|
|
|
|
$children = $object->Children();
|
|
|
|
$children->add($this->objFromFixture(UnsavedRelationListTest\TestObject::class, 'ObjectA'));
|
|
|
|
$children->add($this->objFromFixture(UnsavedRelationListTest\TestObject::class, 'ObjectB'));
|
|
|
|
$children->add($this->objFromFixture(UnsavedRelationListTest\TestObject::class, 'ObjectC'));
|
|
|
|
|
|
|
|
$children = $object->Children();
|
|
|
|
|
2017-09-27 09:25:37 +02:00
|
|
|
$this->assertListEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['Name' => 'A'],
|
|
|
|
['Name' => 'B'],
|
|
|
|
['Name' => 'C']
|
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
$children
|
|
|
|
);
|
|
|
|
|
|
|
|
$object->write();
|
|
|
|
|
|
|
|
$this->assertNotEquals($children, $object->Children());
|
|
|
|
|
2017-09-27 09:25:37 +02:00
|
|
|
$this->assertListEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['Name' => 'A'],
|
|
|
|
['Name' => 'B'],
|
|
|
|
['Name' => 'C']
|
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
$object->Children()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testManyManyExisting()
|
|
|
|
{
|
|
|
|
$object = new UnsavedRelationListTest\TestObject();
|
|
|
|
|
|
|
|
$Siblings = $object->Siblings();
|
|
|
|
$Siblings->add($this->objFromFixture(UnsavedRelationListTest\TestObject::class, 'ObjectA'));
|
|
|
|
$Siblings->add($this->objFromFixture(UnsavedRelationListTest\TestObject::class, 'ObjectB'));
|
|
|
|
$Siblings->add($this->objFromFixture(UnsavedRelationListTest\TestObject::class, 'ObjectC'));
|
|
|
|
|
|
|
|
$siblings = $object->Siblings();
|
|
|
|
|
2017-09-27 09:25:37 +02:00
|
|
|
$this->assertListEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['Name' => 'A'],
|
|
|
|
['Name' => 'B'],
|
|
|
|
['Name' => 'C']
|
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
$siblings
|
|
|
|
);
|
|
|
|
|
|
|
|
$object->write();
|
|
|
|
|
|
|
|
$this->assertNotEquals($siblings, $object->Siblings());
|
|
|
|
|
2017-09-27 09:25:37 +02:00
|
|
|
$this->assertListEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['Name' => 'A'],
|
|
|
|
['Name' => 'B'],
|
|
|
|
['Name' => 'C']
|
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
$object->Siblings()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testHasManyNew()
|
|
|
|
{
|
|
|
|
$object = new UnsavedRelationListTest\TestObject();
|
|
|
|
|
|
|
|
$children = $object->Children();
|
2020-04-20 19:58:09 +02:00
|
|
|
$children->add(new UnsavedRelationListTest\TestObject(['Name' => 'A']));
|
|
|
|
$children->add(new UnsavedRelationListTest\TestObject(['Name' => 'B']));
|
|
|
|
$children->add(new UnsavedRelationListTest\TestObject(['Name' => 'C']));
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$children = $object->Children();
|
|
|
|
|
2017-09-27 09:25:37 +02:00
|
|
|
$this->assertListEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['Name' => 'A'],
|
|
|
|
['Name' => 'B'],
|
|
|
|
['Name' => 'C']
|
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
$children
|
|
|
|
);
|
|
|
|
|
|
|
|
$object->write();
|
|
|
|
|
|
|
|
$this->assertNotEquals($children, $object->Children());
|
|
|
|
|
2017-09-27 09:25:37 +02:00
|
|
|
$this->assertListEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['Name' => 'A'],
|
|
|
|
['Name' => 'B'],
|
|
|
|
['Name' => 'C']
|
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
$object->Children()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testHasManyPolymorphic()
|
|
|
|
{
|
|
|
|
$object = new UnsavedRelationListTest\TestObject();
|
|
|
|
|
|
|
|
$children = $object->RelatedObjects();
|
2020-04-20 19:58:09 +02:00
|
|
|
$children->add(new UnsavedRelationListTest\TestObject(['Name' => 'A']));
|
|
|
|
$children->add(new UnsavedRelationListTest\TestObject(['Name' => 'B']));
|
|
|
|
$children->add(new UnsavedRelationListTest\TestObject(['Name' => 'C']));
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$children = $object->RelatedObjects();
|
|
|
|
|
2017-09-27 09:25:37 +02:00
|
|
|
$this->assertListEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['Name' => 'A'],
|
|
|
|
['Name' => 'B'],
|
|
|
|
['Name' => 'C']
|
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
$children
|
|
|
|
);
|
|
|
|
|
|
|
|
$object->write();
|
|
|
|
|
|
|
|
$this->assertNotEquals($children, $object->RelatedObjects());
|
|
|
|
|
2017-09-27 09:25:37 +02:00
|
|
|
$this->assertListEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['Name' => 'A'],
|
|
|
|
['Name' => 'B'],
|
|
|
|
['Name' => 'C']
|
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
$object->RelatedObjects()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testManyManyNew()
|
|
|
|
{
|
|
|
|
$object = new UnsavedRelationListTest\TestObject();
|
|
|
|
|
|
|
|
$Siblings = $object->Siblings();
|
2020-04-20 19:58:09 +02:00
|
|
|
$Siblings->add(new UnsavedRelationListTest\TestObject(['Name' => 'A']));
|
|
|
|
$Siblings->add(new UnsavedRelationListTest\TestObject(['Name' => 'B']));
|
|
|
|
$Siblings->add(new UnsavedRelationListTest\TestObject(['Name' => 'C']));
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$siblings = $object->Siblings();
|
|
|
|
|
2017-09-27 09:25:37 +02:00
|
|
|
$this->assertListEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['Name' => 'A'],
|
|
|
|
['Name' => 'B'],
|
|
|
|
['Name' => 'C']
|
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
$siblings
|
|
|
|
);
|
|
|
|
|
|
|
|
$object->write();
|
|
|
|
|
|
|
|
$this->assertNotEquals($siblings, $object->Siblings());
|
|
|
|
|
2017-09-27 09:25:37 +02:00
|
|
|
$this->assertListEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['Name' => 'A'],
|
|
|
|
['Name' => 'B'],
|
|
|
|
['Name' => 'C']
|
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
$object->Siblings()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testManyManyExtraFields()
|
|
|
|
{
|
|
|
|
$object = new UnsavedRelationListTest\TestObject();
|
|
|
|
|
|
|
|
$Siblings = $object->Siblings();
|
2020-04-20 19:58:09 +02:00
|
|
|
$Siblings->add(new UnsavedRelationListTest\TestObject(['Name' => 'A']), ['Number' => 1]);
|
|
|
|
$Siblings->add(new UnsavedRelationListTest\TestObject(['Name' => 'B']), ['Number' => 2]);
|
|
|
|
$Siblings->add(new UnsavedRelationListTest\TestObject(['Name' => 'C']), ['Number' => 3]);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$siblings = $object->Siblings();
|
|
|
|
|
2017-09-27 09:25:37 +02:00
|
|
|
$this->assertListEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['Name' => 'A', 'Number' => 1],
|
|
|
|
['Name' => 'B', 'Number' => 2],
|
|
|
|
['Name' => 'C', 'Number' => 3]
|
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
$siblings
|
|
|
|
);
|
|
|
|
|
|
|
|
$object->write();
|
|
|
|
|
|
|
|
$this->assertNotEquals($siblings, $object->Siblings());
|
|
|
|
|
2017-09-27 09:25:37 +02:00
|
|
|
$this->assertListEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['Name' => 'A', 'Number' => 1],
|
|
|
|
['Name' => 'B', 'Number' => 2],
|
|
|
|
['Name' => 'C', 'Number' => 3]
|
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
$object->Siblings()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetIDList()
|
|
|
|
{
|
|
|
|
$object = new UnsavedRelationListTest\TestObject();
|
|
|
|
|
|
|
|
$children = $object->Children();
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->assertEquals($children->getIDList(), []);
|
|
|
|
$children->add($child1 = new UnsavedRelationListTest\TestObject(['Name' => 'A']));
|
|
|
|
$children->add($child2 = new UnsavedRelationListTest\TestObject(['Name' => 'B']));
|
|
|
|
$children->add($child3 = new UnsavedRelationListTest\TestObject(['Name' => 'C']));
|
2016-12-16 05:34:21 +01:00
|
|
|
$children->add($child1);
|
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->assertEquals($children->getIDList(), []);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$child1->write();
|
|
|
|
$this->assertEquals(
|
|
|
|
$children->getIDList(),
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
$child1->ID => $child1->ID
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$child2->write();
|
|
|
|
$child3->write();
|
|
|
|
$this->assertEquals(
|
|
|
|
$children->getIDList(),
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
$child1->ID => $child1->ID,
|
|
|
|
$child2->ID => $child2->ID,
|
|
|
|
$child3->ID => $child3->ID
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testColumn()
|
|
|
|
{
|
|
|
|
$object = new UnsavedRelationListTest\TestObject();
|
|
|
|
|
|
|
|
$children = $object->Children();
|
2020-04-20 19:58:09 +02:00
|
|
|
$children->add(new UnsavedRelationListTest\TestObject(['Name' => 'A']));
|
|
|
|
$children->add(new UnsavedRelationListTest\TestObject(['Name' => 'B']));
|
|
|
|
$children->add(new UnsavedRelationListTest\TestObject(['Name' => 'C']));
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$children = $object->Children();
|
|
|
|
|
2017-09-27 09:25:37 +02:00
|
|
|
$this->assertListEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['Name' => 'A'],
|
|
|
|
['Name' => 'B'],
|
|
|
|
['Name' => 'C']
|
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
$children
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
$children->column('Name'),
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'A',
|
|
|
|
'B',
|
|
|
|
'C'
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
}
|
2012-11-03 04:16:51 +01:00
|
|
|
}
|