silverstripe-framework/tests/php/ORM/DataObjectDuplicationTest.php

125 lines
3.8 KiB
PHP
Raw Normal View History

<?php
2016-10-14 03:30:05 +02:00
namespace SilverStripe\ORM\Tests;
use SilverStripe\ORM\DataObject;
use SilverStripe\Dev\SapphireTest;
class DataObjectDuplicationTest extends SapphireTest {
protected $usesDatabase = true;
2014-08-15 08:53:05 +02:00
protected $extraDataObjects = array(
2016-10-14 03:30:05 +02:00
DataObjectDuplicationTest\Class1::class,
DataObjectDuplicationTest\Class2::class,
DataObjectDuplicationTest\Class3::class
);
public function testDuplicate() {
2016-10-14 03:30:05 +02:00
$orig = new DataObjectDuplicationTest\Class1();
$orig->text = 'foo';
$orig->write();
$duplicate = $orig->duplicate();
2016-10-14 03:30:05 +02:00
$this->assertInstanceOf(DataObjectDuplicationTest\Class1::class, $duplicate,
'Creates the correct type'
);
$this->assertNotEquals($duplicate->ID, $orig->ID,
'Creates a unique record'
);
$this->assertEquals('foo', $duplicate->text,
'Copies fields'
);
2016-10-14 03:30:05 +02:00
$this->assertEquals(2, DataObjectDuplicationTest\Class1::get()->Count(),
'Only creates a single duplicate'
);
}
public function testDuplicateHasOne() {
2016-10-14 03:30:05 +02:00
$relationObj = new DataObjectDuplicationTest\Class1();
$relationObj->text = 'class1';
$relationObj->write();
2016-10-14 03:30:05 +02:00
$orig = new DataObjectDuplicationTest\Class2();
$orig->text = 'class2';
$orig->oneID = $relationObj->ID;
$orig->write();
$duplicate = $orig->duplicate();
$this->assertEquals($relationObj->ID, $duplicate->oneID,
'Copies has_one relationship'
);
2016-10-14 03:30:05 +02:00
$this->assertEquals(2, DataObjectDuplicationTest\Class2::get()->Count(),
'Only creates a single duplicate'
);
2016-10-14 03:30:05 +02:00
$this->assertEquals(1, DataObjectDuplicationTest\Class1::get()->Count(),
'Does not create duplicate of has_one relationship'
);
}
public function testDuplicateManyManyClasses() {
//create new test classes below
2016-10-14 03:30:05 +02:00
$one = new DataObjectDuplicationTest\Class1();
$two = new DataObjectDuplicationTest\Class2();
$three = new DataObjectDuplicationTest\Class3();
//set some simple fields
$text1 = "Test Text 1";
$text2 = "Test Text 2";
$text3 = "Test Text 3";
$one->text = $text1;
$two->text = $text2;
$three->text = $text3;
//write the to DB
$one->write();
$two->write();
$three->write();
//create relations
$one->twos()->add($two);
$one->threes()->add($three);
$one = DataObject::get_by_id("DataObjectDuplicateTestClass1", $one->ID);
$two = DataObject::get_by_id("DataObjectDuplicateTestClass2", $two->ID);
$three = DataObject::get_by_id("DataObjectDuplicateTestClass3", $three->ID);
//test duplication
$oneCopy = $one->duplicate();
$twoCopy = $two->duplicate();
$threeCopy = $three->duplicate();
$oneCopy = DataObject::get_by_id("DataObjectDuplicateTestClass1", $oneCopy->ID);
$twoCopy = DataObject::get_by_id("DataObjectDuplicateTestClass2", $twoCopy->ID);
$threeCopy = DataObject::get_by_id("DataObjectDuplicateTestClass3", $threeCopy->ID);
$this->assertNotNull($oneCopy, "Copy of 1 exists");
$this->assertNotNull($twoCopy, "Copy of 2 exists");
$this->assertNotNull($threeCopy, "Copy of 3 exists");
$this->assertEquals($text1, $oneCopy->text);
$this->assertEquals($text2, $twoCopy->text);
$this->assertEquals($text3, $threeCopy->text);
$this->assertNotEquals($one->twos()->Count(), $oneCopy->twos()->Count(),
"Many-to-one relation not copied (has_many)");
$this->assertEquals($one->threes()->Count(), $oneCopy->threes()->Count(),
"Object has the correct number of relations");
$this->assertEquals($three->ones()->Count(), $threeCopy->ones()->Count(),
"Object has the correct number of relations");
$this->assertEquals($one->ID, $twoCopy->one()->ID,
"Match between relation of copy and the original");
$this->assertEquals(0, $oneCopy->twos()->Count(),
"Many-to-one relation not copied (has_many)");
$this->assertEquals($three->ID, $oneCopy->threes()->First()->ID,
"Match between relation of copy and the original");
$this->assertEquals($one->ID, $threeCopy->ones()->First()->ID,
"Match between relation of copy and the original");
}
}