2010-09-16 05:40:34 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class DataObjectDuplicationTest extends SapphireTest {
|
2013-06-03 14:48:52 +02:00
|
|
|
|
|
|
|
protected $usesDatabase = true;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-23 04:32:24 +01:00
|
|
|
protected $extraDataObjects = array(
|
|
|
|
'DataObjectDuplicateTestClass1',
|
|
|
|
'DataObjectDuplicateTestClass2',
|
|
|
|
'DataObjectDuplicateTestClass3'
|
|
|
|
);
|
2010-09-16 05:40:34 +02:00
|
|
|
|
2013-04-30 18:45:22 +02:00
|
|
|
public function testDuplicate() {
|
2016-12-19 15:01:05 +01:00
|
|
|
SS_Datetime::set_mock_now('2016-01-01 01:01:01');
|
2013-04-30 18:45:22 +02:00
|
|
|
$orig = new DataObjectDuplicateTestClass1();
|
|
|
|
$orig->text = 'foo';
|
|
|
|
$orig->write();
|
2016-12-19 15:01:05 +01:00
|
|
|
SS_Datetime::set_mock_now('2016-01-02 01:01:01');
|
2013-04-30 18:45:22 +02:00
|
|
|
$duplicate = $orig->duplicate();
|
|
|
|
$this->assertInstanceOf('DataObjectDuplicateTestClass1', $duplicate,
|
|
|
|
'Creates the correct type'
|
|
|
|
);
|
|
|
|
$this->assertNotEquals($duplicate->ID, $orig->ID,
|
|
|
|
'Creates a unique record'
|
|
|
|
);
|
|
|
|
$this->assertEquals('foo', $duplicate->text,
|
|
|
|
'Copies fields'
|
|
|
|
);
|
|
|
|
$this->assertEquals(2, DataObjectDuplicateTestClass1::get()->Count(),
|
|
|
|
'Only creates a single duplicate'
|
|
|
|
);
|
2016-12-19 15:01:05 +01:00
|
|
|
$this->assertEquals(SS_Datetime::now()->Nice(), $duplicate->dbObject('Created')->Nice());
|
|
|
|
$this->assertNotEquals($orig->dbObject('Created')->Nice(), $duplicate->dbObject('Created')->Nice());
|
2013-04-30 18:45:22 +02:00
|
|
|
}
|
|
|
|
|
2013-06-03 14:48:52 +02:00
|
|
|
public function testDuplicateHasOne() {
|
|
|
|
$relationObj = new DataObjectDuplicateTestClass1();
|
|
|
|
$relationObj->text = 'class1';
|
|
|
|
$relationObj->write();
|
|
|
|
|
|
|
|
$orig = new DataObjectDuplicateTestClass2();
|
|
|
|
$orig->text = 'class2';
|
|
|
|
$orig->oneID = $relationObj->ID;
|
|
|
|
$orig->write();
|
|
|
|
|
|
|
|
$duplicate = $orig->duplicate();
|
|
|
|
$this->assertEquals($relationObj->ID, $duplicate->oneID,
|
|
|
|
'Copies has_one relationship'
|
|
|
|
);
|
|
|
|
$this->assertEquals(2, DataObjectDuplicateTestClass2::get()->Count(),
|
|
|
|
'Only creates a single duplicate'
|
|
|
|
);
|
|
|
|
$this->assertEquals(1, DataObjectDuplicateTestClass1::get()->Count(),
|
|
|
|
'Does not create duplicate of has_one relationship'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testDuplicateManyManyClasses() {
|
2010-09-16 05:40:34 +02:00
|
|
|
//create new test classes below
|
|
|
|
$one = new DataObjectDuplicateTestClass1();
|
|
|
|
$two = new DataObjectDuplicateTestClass2();
|
|
|
|
$three = new DataObjectDuplicateTestClass3();
|
|
|
|
|
|
|
|
//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);
|
2018-04-17 18:37:02 +02:00
|
|
|
$one->threes()->add($three, array('TestExtra'=>'three'));
|
2010-09-16 05:40:34 +02:00
|
|
|
|
|
|
|
$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);
|
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
$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");
|
2018-04-17 18:37:02 +02:00
|
|
|
|
|
|
|
$this->assertEquals('three', $oneCopy->threes()->First()->TestExtra,
|
|
|
|
"Match between extra field of copy and the original");
|
2010-09-16 05:40:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-23 04:32:24 +01:00
|
|
|
class DataObjectDuplicateTestClass1 extends DataObject implements TestOnly {
|
2010-09-16 05:40:34 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2010-09-16 05:40:34 +02:00
|
|
|
'text' => 'Varchar'
|
|
|
|
);
|
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $has_many = array(
|
2010-09-16 05:40:34 +02:00
|
|
|
'twos' => 'DataObjectDuplicateTestClass2'
|
|
|
|
);
|
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $many_many = array(
|
2010-09-16 05:40:34 +02:00
|
|
|
'threes' => 'DataObjectDuplicateTestClass3'
|
|
|
|
);
|
2018-04-17 18:37:02 +02:00
|
|
|
|
|
|
|
private static $many_many_extraFields = array(
|
|
|
|
'threes' => array(
|
|
|
|
'TestExtra' => 'Varchar'
|
|
|
|
)
|
|
|
|
);
|
2010-09-16 05:40:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-23 04:32:24 +01:00
|
|
|
class DataObjectDuplicateTestClass2 extends DataObject implements TestOnly {
|
2010-09-16 05:40:34 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2010-09-16 05:40:34 +02:00
|
|
|
'text' => 'Varchar'
|
|
|
|
);
|
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $has_one = array(
|
2010-09-16 05:40:34 +02:00
|
|
|
'one' => 'DataObjectDuplicateTestClass1'
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-03-23 04:32:24 +01:00
|
|
|
class DataObjectDuplicateTestClass3 extends DataObject implements TestOnly {
|
2010-09-16 05:40:34 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2010-09-16 05:40:34 +02:00
|
|
|
'text' => 'Varchar'
|
|
|
|
);
|
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $belongs_many_many = array(
|
2010-09-16 05:40:34 +02:00
|
|
|
'ones' => 'DataObjectDuplicateTestClass1'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|