2010-09-16 05:40:34 +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-06-15 06:03:16 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
class DataObjectDuplicationTest extends SapphireTest
|
|
|
|
{
|
2018-01-30 06:28:28 +01:00
|
|
|
protected static $fixture_file = 'DataObjectDuplicationTest.yml';
|
|
|
|
|
|
|
|
protected static $extra_dataobjects = [
|
|
|
|
DataObjectDuplicationTest\Antelope::class,
|
|
|
|
DataObjectDuplicationTest\Bobcat::class,
|
|
|
|
DataObjectDuplicationTest\Caribou::class,
|
|
|
|
DataObjectDuplicationTest\Dingo::class,
|
|
|
|
DataObjectDuplicationTest\Elephant::class,
|
|
|
|
DataObjectDuplicationTest\Frog::class,
|
|
|
|
DataObjectDuplicationTest\Goat::class,
|
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
public function testDuplicate()
|
|
|
|
{
|
2018-01-30 06:28:28 +01:00
|
|
|
/** @var DataObjectDuplicationTest\Antelope $orig */
|
|
|
|
$orig = $this->objFromFixture(DataObjectDuplicationTest\Antelope::class, 'one');
|
|
|
|
/** @var DataObjectDuplicationTest\Antelope $duplicate */
|
2016-12-16 05:34:21 +01:00
|
|
|
$duplicate = $orig->duplicate();
|
|
|
|
$this->assertInstanceOf(
|
2018-01-30 06:28:28 +01:00
|
|
|
DataObjectDuplicationTest\Antelope::class,
|
2016-12-16 05:34:21 +01:00
|
|
|
$duplicate,
|
|
|
|
'Creates the correct type'
|
|
|
|
);
|
|
|
|
$this->assertNotEquals(
|
|
|
|
$duplicate->ID,
|
|
|
|
$orig->ID,
|
|
|
|
'Creates a unique record'
|
|
|
|
);
|
|
|
|
|
2018-01-30 06:28:28 +01:00
|
|
|
// Check 'bobcats' relation duplicated
|
|
|
|
$twoOne = $this->objFromFixture(DataObjectDuplicationTest\Bobcat::class, 'one');
|
|
|
|
$twoTwo = $this->objFromFixture(DataObjectDuplicationTest\Bobcat::class, 'two');
|
2017-09-27 09:25:37 +02:00
|
|
|
$this->assertListEquals(
|
2018-01-30 06:28:28 +01:00
|
|
|
[
|
|
|
|
['Title' => 'Bobcat two'],
|
|
|
|
['Title' => 'Bobcat three'],
|
|
|
|
],
|
|
|
|
$duplicate->bobcats()
|
|
|
|
);
|
|
|
|
$this->assertEmpty(
|
|
|
|
array_intersect(
|
|
|
|
$orig->bobcats()->getIDList(),
|
|
|
|
$duplicate->bobcats()->getIDList()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
/** @var DataObjectDuplicationTest\Bobcat $twoTwoDuplicate */
|
|
|
|
$twoTwoDuplicate = $duplicate->bobcats()->filter('Title', 'Bobcat two')->first();
|
|
|
|
$this->assertNotEmpty($twoTwoDuplicate);
|
|
|
|
$this->assertNotEquals($twoTwo->ID, $twoTwoDuplicate->ID);
|
|
|
|
|
|
|
|
// Check 'bobcats.self' relation duplicated
|
|
|
|
/** @var DataObjectDuplicationTest\Bobcat $twoOneDuplicate */
|
|
|
|
$twoOneDuplicate = $twoTwoDuplicate->self();
|
|
|
|
$this->assertNotEmpty($twoOneDuplicate);
|
|
|
|
$this->assertNotEquals($twoOne->ID, $twoOneDuplicate->ID);
|
|
|
|
|
|
|
|
// Ensure 'bobcats.seven' instance is not duplicated
|
|
|
|
$sevenOne = $this->objFromFixture(DataObjectDuplicationTest\Goat::class, 'one');
|
|
|
|
$sevenTwo = $this->objFromFixture(DataObjectDuplicationTest\Goat::class, 'two');
|
|
|
|
$this->assertEquals($sevenOne->ID, $twoOneDuplicate->goat()->ID);
|
|
|
|
$this->assertEquals($sevenTwo->ID, $twoTwoDuplicate->goat()->ID);
|
|
|
|
|
|
|
|
// Ensure that 'caribou' many_many list exists on both, but only the mapping table is duplicated
|
|
|
|
// many_many_extraFields are also duplicated
|
|
|
|
$caribouList = [
|
|
|
|
[
|
|
|
|
'Title' => 'Caribou one',
|
|
|
|
'Sort' => 4,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'Title' => 'Caribou two',
|
|
|
|
'Sort' => 5,
|
|
|
|
],
|
|
|
|
];
|
|
|
|
// Original and duplicate lists have the same content
|
2017-09-27 09:25:37 +02:00
|
|
|
$this->assertListEquals(
|
2018-01-30 06:28:28 +01:00
|
|
|
$caribouList,
|
|
|
|
$orig->caribou()
|
2016-10-11 06:50:19 +02:00
|
|
|
);
|
2017-09-27 09:25:37 +02:00
|
|
|
$this->assertListEquals(
|
2018-01-30 06:28:28 +01:00
|
|
|
$caribouList,
|
|
|
|
$duplicate->caribou()
|
2016-10-11 06:50:19 +02:00
|
|
|
);
|
2018-01-30 06:28:28 +01:00
|
|
|
// Ids of each record are the same (only mapping content is duplicated)
|
|
|
|
$this->assertEquals(
|
|
|
|
$orig->caribou()->getIDList(),
|
|
|
|
$duplicate->caribou()->getIDList()
|
|
|
|
);
|
|
|
|
|
|
|
|
// Ensure 'five' belongs_to is duplicated
|
|
|
|
$fiveOne = $this->objFromFixture(DataObjectDuplicationTest\Elephant::class, 'one');
|
|
|
|
$fiveOneDuplicate = $duplicate->elephant();
|
|
|
|
$this->assertNotEmpty($fiveOneDuplicate);
|
|
|
|
$this->assertEquals('Elephant one', $fiveOneDuplicate->Title);
|
|
|
|
$this->assertNotEquals($fiveOne->ID, $fiveOneDuplicate->ID);
|
|
|
|
|
|
|
|
// Ensure 'five.Child' is duplicated
|
|
|
|
$sixOne = $this->objFromFixture(DataObjectDuplicationTest\Frog::class, 'one');
|
|
|
|
$sixOneDuplicate = $fiveOneDuplicate->Child();
|
|
|
|
$this->assertNotEmpty($sixOneDuplicate);
|
|
|
|
$this->assertEquals('Frog one', $sixOneDuplicate->Title);
|
|
|
|
$this->assertNotEquals($sixOne->ID, $sixOneDuplicate->ID);
|
2016-10-11 06:50:19 +02:00
|
|
|
}
|
2010-09-16 05:40:34 +02:00
|
|
|
}
|