2012-04-20 00:08:17 +02:00
|
|
|
<?php
|
2016-06-15 06:03:16 +02:00
|
|
|
|
|
|
|
use SilverStripe\ORM\DB;
|
|
|
|
use SilverStripe\ORM\DataList;
|
|
|
|
use SilverStripe\ORM\DataObject;
|
|
|
|
use SilverStripe\ORM\Versioning\Versioned;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
use SilverStripe\Dev\TestOnly;
|
|
|
|
|
2012-04-20 00:08:17 +02:00
|
|
|
/**
|
|
|
|
* @package framework
|
|
|
|
* @subpackage tests
|
|
|
|
*/
|
|
|
|
|
|
|
|
class DataObjectLazyLoadingTest extends SapphireTest {
|
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
protected static $fixture_file = array(
|
2012-10-10 03:59:45 +02:00
|
|
|
'DataObjectTest.yml',
|
|
|
|
'VersionedTest.yml'
|
|
|
|
);
|
2012-04-20 00:08:17 +02:00
|
|
|
|
2012-10-10 03:59:45 +02:00
|
|
|
// These are all defined in DataObjectTest.php and VersionedTest.php
|
2012-04-20 00:08:17 +02:00
|
|
|
protected $extraDataObjects = array(
|
2016-01-22 02:45:05 +01:00
|
|
|
// From DataObjectTest
|
2012-04-20 00:08:17 +02:00
|
|
|
'DataObjectTest_Team',
|
|
|
|
'DataObjectTest_Fixture',
|
|
|
|
'DataObjectTest_SubTeam',
|
|
|
|
'OtherSubclassWithSameField',
|
|
|
|
'DataObjectTest_FieldlessTable',
|
|
|
|
'DataObjectTest_FieldlessSubTable',
|
|
|
|
'DataObjectTest_ValidatedObject',
|
|
|
|
'DataObjectTest_Player',
|
2015-01-24 00:13:17 +01:00
|
|
|
'DataObjectTest_TeamComment',
|
|
|
|
'DataObjectTest_EquipmentCompany',
|
|
|
|
'DataObjectTest_SubEquipmentCompany',
|
2015-12-02 05:33:25 +01:00
|
|
|
'DataObjectTest\NamespacedClass',
|
|
|
|
'DataObjectTest\RelationClass',
|
|
|
|
'DataObjectTest_ExtendedTeamComment',
|
|
|
|
'DataObjectTest_Company',
|
|
|
|
'DataObjectTest_Staff',
|
|
|
|
'DataObjectTest_CEO',
|
|
|
|
'DataObjectTest_Fan',
|
2016-01-22 02:45:05 +01:00
|
|
|
'DataObjectTest_Play',
|
|
|
|
'DataObjectTest_Ploy',
|
|
|
|
'DataObjectTest_Bogey',
|
2016-05-12 17:05:54 +02:00
|
|
|
// From ManyManyListTest
|
2016-05-10 16:56:15 +02:00
|
|
|
'ManyManyListTest_Product',
|
|
|
|
'ManyManyListTest_Category',
|
2016-01-22 02:45:05 +01:00
|
|
|
// From VersionedTest
|
2015-12-02 05:33:25 +01:00
|
|
|
'VersionedTest_DataObject',
|
|
|
|
'VersionedTest_Subclass',
|
|
|
|
'VersionedTest_AnotherSubclass',
|
|
|
|
'VersionedTest_RelatedWithoutVersion',
|
|
|
|
'VersionedTest_SingleStage',
|
|
|
|
'VersionedTest_WithIndexes',
|
|
|
|
'VersionedTest_PublicStage',
|
|
|
|
'VersionedTest_PublicViaExtension',
|
2016-01-22 02:45:05 +01:00
|
|
|
// From DataObjectLazyLoadingTest
|
2015-02-12 14:15:32 +01:00
|
|
|
'VersionedLazy_DataObject',
|
|
|
|
'VersionedLazySub_DataObject',
|
2012-04-20 00:08:17 +02:00
|
|
|
);
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testQueriedColumnsID() {
|
2013-06-21 00:32:08 +02:00
|
|
|
$db = DB::get_conn();
|
2012-04-20 00:08:17 +02:00
|
|
|
$playerList = new DataList('DataObjectTest_SubTeam');
|
|
|
|
$playerList = $playerList->setQueriedColumns(array('ID'));
|
2013-06-21 00:32:08 +02:00
|
|
|
$expected = 'SELECT DISTINCT "DataObjectTest_Team"."ClassName", "DataObjectTest_Team"."LastEdited", ' .
|
|
|
|
'"DataObjectTest_Team"."Created", "DataObjectTest_Team"."ID", CASE WHEN '.
|
2012-04-20 00:08:17 +02:00
|
|
|
'"DataObjectTest_Team"."ClassName" IS NOT NULL THEN "DataObjectTest_Team"."ClassName" ELSE ' .
|
2013-06-21 00:32:08 +02:00
|
|
|
$db->quoteString('DataObjectTest_Team').' END AS "RecordClassName", "DataObjectTest_Team"."Title" '.
|
2012-08-21 06:23:00 +02:00
|
|
|
'FROM "DataObjectTest_Team" ' .
|
2014-04-08 22:46:52 +02:00
|
|
|
'LEFT JOIN "DataObjectTest_SubTeam" ON "DataObjectTest_SubTeam"."ID" = "DataObjectTest_Team"."ID" ' .
|
2013-06-21 00:32:08 +02:00
|
|
|
'WHERE ("DataObjectTest_Team"."ClassName" IN (?))' .
|
2012-08-21 06:23:00 +02:00
|
|
|
' ORDER BY "DataObjectTest_Team"."Title" ASC';
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->assertSQLEquals($expected, $playerList->sql($parameters));
|
2012-04-20 00:08:17 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testQueriedColumnsFromBaseTableAndSubTable() {
|
2013-06-21 00:32:08 +02:00
|
|
|
$db = DB::get_conn();
|
2012-04-20 00:08:17 +02:00
|
|
|
$playerList = new DataList('DataObjectTest_SubTeam');
|
|
|
|
$playerList = $playerList->setQueriedColumns(array('Title', 'SubclassDatabaseField'));
|
2013-06-21 00:32:08 +02:00
|
|
|
$expected = 'SELECT DISTINCT "DataObjectTest_Team"."ClassName", "DataObjectTest_Team"."LastEdited", ' .
|
|
|
|
'"DataObjectTest_Team"."Created", "DataObjectTest_Team"."Title", ' .
|
2012-04-20 00:08:17 +02:00
|
|
|
'"DataObjectTest_SubTeam"."SubclassDatabaseField", "DataObjectTest_Team"."ID", CASE WHEN ' .
|
|
|
|
'"DataObjectTest_Team"."ClassName" IS NOT NULL THEN "DataObjectTest_Team"."ClassName" ELSE ' .
|
2013-06-21 00:32:08 +02:00
|
|
|
$db->quoteString('DataObjectTest_Team').' END AS "RecordClassName" FROM "DataObjectTest_Team" ' .
|
2012-09-26 23:34:00 +02:00
|
|
|
'LEFT JOIN "DataObjectTest_SubTeam" ON "DataObjectTest_SubTeam"."ID" = "DataObjectTest_Team"."ID" WHERE ' .
|
2013-06-21 00:32:08 +02:00
|
|
|
'("DataObjectTest_Team"."ClassName" IN (?)) ' .
|
2012-08-21 06:23:00 +02:00
|
|
|
'ORDER BY "DataObjectTest_Team"."Title" ASC';
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->assertSQLEquals($expected, $playerList->sql($parameters));
|
2012-04-20 00:08:17 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testQueriedColumnsFromBaseTable() {
|
2013-06-21 00:32:08 +02:00
|
|
|
$db = DB::get_conn();
|
2012-04-20 00:08:17 +02:00
|
|
|
$playerList = new DataList('DataObjectTest_SubTeam');
|
|
|
|
$playerList = $playerList->setQueriedColumns(array('Title'));
|
2013-06-21 00:32:08 +02:00
|
|
|
$expected = 'SELECT DISTINCT "DataObjectTest_Team"."ClassName", "DataObjectTest_Team"."LastEdited", ' .
|
|
|
|
'"DataObjectTest_Team"."Created", "DataObjectTest_Team"."Title", "DataObjectTest_Team"."ID", ' .
|
2012-04-20 00:08:17 +02:00
|
|
|
'CASE WHEN "DataObjectTest_Team"."ClassName" IS NOT NULL THEN "DataObjectTest_Team"."ClassName" ELSE ' .
|
2013-06-21 00:32:08 +02:00
|
|
|
$db->quoteString('DataObjectTest_Team').' END AS "RecordClassName" FROM "DataObjectTest_Team" ' .
|
2014-04-08 22:46:52 +02:00
|
|
|
'LEFT JOIN "DataObjectTest_SubTeam" ON "DataObjectTest_SubTeam"."ID" = "DataObjectTest_Team"."ID" WHERE ' .
|
2013-06-21 00:32:08 +02:00
|
|
|
'("DataObjectTest_Team"."ClassName" IN (?)) ' .
|
2012-08-21 06:23:00 +02:00
|
|
|
'ORDER BY "DataObjectTest_Team"."Title" ASC';
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->assertSQLEquals($expected, $playerList->sql($parameters));
|
2012-04-20 00:08:17 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testQueriedColumnsFromSubTable() {
|
2013-06-21 00:32:08 +02:00
|
|
|
$db = DB::get_conn();
|
2012-04-20 00:08:17 +02:00
|
|
|
$playerList = new DataList('DataObjectTest_SubTeam');
|
|
|
|
$playerList = $playerList->setQueriedColumns(array('SubclassDatabaseField'));
|
2013-06-21 00:32:08 +02:00
|
|
|
$expected = 'SELECT DISTINCT "DataObjectTest_Team"."ClassName", "DataObjectTest_Team"."LastEdited", ' .
|
|
|
|
'"DataObjectTest_Team"."Created", "DataObjectTest_SubTeam"."SubclassDatabaseField", ' .
|
2012-04-20 00:08:17 +02:00
|
|
|
'"DataObjectTest_Team"."ID", CASE WHEN "DataObjectTest_Team"."ClassName" IS NOT NULL THEN ' .
|
2013-06-21 00:32:08 +02:00
|
|
|
'"DataObjectTest_Team"."ClassName" ELSE '.$db->quoteString('DataObjectTest_Team').' END ' .
|
2012-09-26 23:34:00 +02:00
|
|
|
'AS "RecordClassName", "DataObjectTest_Team"."Title" ' .
|
|
|
|
'FROM "DataObjectTest_Team" LEFT JOIN "DataObjectTest_SubTeam" ON "DataObjectTest_SubTeam"."ID" = ' .
|
2014-08-15 08:53:05 +02:00
|
|
|
'"DataObjectTest_Team"."ID" WHERE ("DataObjectTest_Team"."ClassName" IN (?)) ' .
|
2012-08-21 06:23:00 +02:00
|
|
|
'ORDER BY "DataObjectTest_Team"."Title" ASC';
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->assertSQLEquals($expected, $playerList->sql($parameters));
|
2012-04-20 00:08:17 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testNoSpecificColumnNamesBaseDataObjectQuery() {
|
2012-04-20 00:08:17 +02:00
|
|
|
// This queries all columns from base table
|
|
|
|
$playerList = new DataList('DataObjectTest_Team');
|
|
|
|
// Shouldn't be a left join in here.
|
2014-08-15 08:53:05 +02:00
|
|
|
$this->assertEquals(0,
|
2013-06-21 00:32:08 +02:00
|
|
|
preg_match(
|
|
|
|
$this->normaliseSQL(
|
|
|
|
'/SELECT DISTINCT "DataObjectTest_Team"."ID" .* LEFT JOIN .* FROM "DataObjectTest_Team"/'
|
|
|
|
),
|
|
|
|
$this->normaliseSQL($playerList->sql($parameters))
|
|
|
|
)
|
|
|
|
);
|
2012-04-20 00:08:17 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testNoSpecificColumnNamesSubclassDataObjectQuery() {
|
2012-04-20 00:08:17 +02:00
|
|
|
// This queries all columns from base table and subtable
|
|
|
|
$playerList = new DataList('DataObjectTest_SubTeam');
|
|
|
|
// Should be a left join.
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->assertEquals(1, preg_match(
|
|
|
|
$this->normaliseSQL('/SELECT DISTINCT .* LEFT JOIN .* /'),
|
|
|
|
$this->normaliseSQL($playerList->sql($parameters))
|
|
|
|
));
|
2012-04-20 00:08:17 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testLazyLoadedFieldsHasField() {
|
2012-04-20 00:08:17 +02:00
|
|
|
$subteam1 = $this->objFromFixture('DataObjectTest_SubTeam', 'subteam1');
|
|
|
|
$teams = DataObject::get('DataObjectTest_Team'); // query parent class
|
|
|
|
$subteam1Lazy = $teams->find('ID', $subteam1->ID);
|
|
|
|
|
|
|
|
// TODO Fix hasField() to exclude *_Lazy
|
|
|
|
// $this->assertFalse($subteam1Lazy->hasField('SubclassDatabaseField_Lazy'));
|
|
|
|
$this->assertTrue($subteam1Lazy->hasField('SubclassDatabaseField'));
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testLazyLoadedFieldsGetField() {
|
2012-04-20 00:08:17 +02:00
|
|
|
$subteam1 = $this->objFromFixture('DataObjectTest_SubTeam', 'subteam1');
|
|
|
|
$teams = DataObject::get('DataObjectTest_Team'); // query parent class
|
|
|
|
$subteam1Lazy = $teams->find('ID', $subteam1->ID);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
$subteam1->getField('SubclassDatabaseField'),
|
|
|
|
$subteam1Lazy->getField('SubclassDatabaseField')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testLazyLoadedFieldsSetField() {
|
2012-04-20 00:08:17 +02:00
|
|
|
$subteam1 = $this->objFromFixture('DataObjectTest_SubTeam', 'subteam1');
|
|
|
|
$subteam1ID = $subteam1->ID;
|
|
|
|
$teams = DataObject::get('DataObjectTest_Team'); // query parent class
|
|
|
|
$subteam1Lazy = $teams->find('ID', $subteam1->ID);
|
|
|
|
|
|
|
|
// Updated lazyloaded field
|
|
|
|
$subteam1Lazy->SubclassDatabaseField = 'Changed';
|
|
|
|
$subteam1Lazy->write();
|
|
|
|
|
|
|
|
// Reload from database
|
|
|
|
DataObject::flush_and_destroy_cache();
|
|
|
|
$subteam1Reloaded = DataObject::get_by_id('DataObjectTest_SubTeam', $subteam1ID);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
'Changed',
|
|
|
|
$subteam1Reloaded->getField('SubclassDatabaseField')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testLazyLoadedFieldsWriteWithUnloadedFields() {
|
2012-04-20 00:08:17 +02:00
|
|
|
$subteam1 = $this->objFromFixture('DataObjectTest_SubTeam', 'subteam1');
|
|
|
|
$subteam1ID = $subteam1->ID;
|
|
|
|
$teams = DataObject::get('DataObjectTest_Team'); // query parent class
|
|
|
|
$subteam1Lazy = $teams->find('ID', $subteam1->ID);
|
|
|
|
|
|
|
|
// Updated lazyloaded field
|
|
|
|
$subteam1Lazy->Title = 'Changed';
|
|
|
|
$subteam1Lazy->write();
|
|
|
|
|
|
|
|
// Reload from database
|
|
|
|
DataObject::flush_and_destroy_cache();
|
|
|
|
$subteam1Reloaded = DataObject::get_by_id('DataObjectTest_SubTeam', $subteam1ID);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
'Subclassed 1',
|
|
|
|
$subteam1Reloaded->getField('SubclassDatabaseField')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testLazyLoadedFieldsWriteNullFields() {
|
2012-04-20 00:08:17 +02:00
|
|
|
$subteam1 = $this->objFromFixture('DataObjectTest_SubTeam', 'subteam1');
|
|
|
|
$subteam1ID = $subteam1->ID;
|
|
|
|
$teams = DataObject::get('DataObjectTest_Team'); // query parent class
|
|
|
|
$subteam1Lazy = $teams->find('ID', $subteam1->ID);
|
|
|
|
|
|
|
|
// Updated lazyloaded field
|
|
|
|
$subteam1Lazy->SubclassDatabaseField = null;
|
|
|
|
$subteam1Lazy->write();
|
|
|
|
|
|
|
|
// Reload from database
|
|
|
|
DataObject::flush_and_destroy_cache();
|
|
|
|
$subteam1Reloaded = DataObject::get_by_id('DataObjectTest_SubTeam', $subteam1ID);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
null,
|
|
|
|
$subteam1Reloaded->getField('SubclassDatabaseField')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testLazyLoadedFieldsGetChangedFields() {
|
2012-04-20 00:08:17 +02:00
|
|
|
$subteam1 = $this->objFromFixture('DataObjectTest_SubTeam', 'subteam1');
|
|
|
|
$teams = DataObject::get('DataObjectTest_Team'); // query parent class
|
|
|
|
$subteam1Lazy = $teams->find('ID', $subteam1->ID);
|
|
|
|
|
|
|
|
// Updated lazyloaded field
|
|
|
|
$subteam1Lazy->SubclassDatabaseField = 'Changed';
|
|
|
|
$this->assertEquals(
|
|
|
|
array('SubclassDatabaseField' => array(
|
|
|
|
'before' => 'Subclassed 1',
|
|
|
|
'after' => 'Changed',
|
|
|
|
'level' => 2
|
|
|
|
)),
|
|
|
|
$subteam1Lazy->getChangedFields()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testLazyLoadedFieldsHasOneRelation() {
|
2012-04-20 00:08:17 +02:00
|
|
|
$subteam1 = $this->objFromFixture('DataObjectTest_SubTeam', 'subteam1');
|
|
|
|
$parentTeam = $this->objFromFixture('DataObjectTest_Team', 'team1');
|
|
|
|
$teams = DataObject::get('DataObjectTest_Team'); // query parent class
|
|
|
|
$subteam1Lazy = $teams->find('ID', $subteam1->ID);
|
|
|
|
|
|
|
|
$parentTeamLazy = $subteam1Lazy->ParentTeam();
|
2012-05-09 12:43:22 +02:00
|
|
|
$this->assertInstanceOf('DataObjectTest_Team', $parentTeamLazy);
|
2012-04-20 00:08:17 +02:00
|
|
|
$this->assertEquals($parentTeam->ID, $parentTeamLazy->ID);
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testLazyLoadedFieldsToMap() {
|
2012-04-20 00:08:17 +02:00
|
|
|
$subteam1 = $this->objFromFixture('DataObjectTest_SubTeam', 'subteam1');
|
|
|
|
$parentTeam = $this->objFromFixture('DataObjectTest_Team', 'team1');
|
|
|
|
$teams = DataObject::get('DataObjectTest_Team'); // query parent class
|
|
|
|
$subteam1Lazy = $teams->find('ID', $subteam1->ID);
|
|
|
|
$mapLazy = $subteam1Lazy->toMap();
|
|
|
|
$this->assertArrayHasKey('SubclassDatabaseField', $mapLazy);
|
|
|
|
$this->assertEquals('Subclassed 1', $mapLazy['SubclassDatabaseField']);
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testLazyLoadedFieldsIsEmpty() {
|
2012-04-20 00:08:17 +02:00
|
|
|
$subteam1 = $this->objFromFixture('DataObjectTest_SubTeam', 'subteam1');
|
|
|
|
$parentTeam = $this->objFromFixture('DataObjectTest_Team', 'team1');
|
|
|
|
$teams = DataObject::get('DataObjectTest_Team'); // query parent class
|
|
|
|
$subteam1Lazy = $teams->find('ID', $subteam1->ID);
|
|
|
|
$subteam1Lazy->Title = '';
|
|
|
|
$subteam1Lazy->DecoratedDatabaseField = '';
|
|
|
|
$subteam1Lazy->ParentTeamID = 0;
|
|
|
|
// Leave $subteam1Lazy->SubclassDatabaseField intact
|
|
|
|
$this->assertFalse($subteam1Lazy->isEmpty());
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testLazyLoadedFieldsDuplicate() {
|
2012-04-20 00:08:17 +02:00
|
|
|
$subteam1 = $this->objFromFixture('DataObjectTest_SubTeam', 'subteam1');
|
|
|
|
$parentTeam = $this->objFromFixture('DataObjectTest_Team', 'team1');
|
|
|
|
$teams = DataObject::get('DataObjectTest_Team'); // query parent class
|
|
|
|
$subteam1Lazy = $teams->find('ID', $subteam1->ID);
|
|
|
|
$subteam1LazyDup = $subteam1Lazy->duplicate();
|
|
|
|
|
|
|
|
$this->assertEquals('Subclassed 1', $subteam1LazyDup->SubclassDatabaseField);
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testLazyLoadedFieldsGetAllFields() {
|
2012-04-20 00:08:17 +02:00
|
|
|
$subteam1 = $this->objFromFixture('DataObjectTest_SubTeam', 'subteam1');
|
|
|
|
$parentTeam = $this->objFromFixture('DataObjectTest_Team', 'team1');
|
|
|
|
$teams = DataObject::get('DataObjectTest_Team'); // query parent class
|
|
|
|
$subteam1Lazy = $teams->find('ID', $subteam1->ID);
|
2012-05-11 01:23:24 +02:00
|
|
|
$this->assertArrayNotHasKey('SubclassDatabaseField_Lazy', $subteam1Lazy->toMap());
|
|
|
|
$this->assertArrayHasKey('SubclassDatabaseField', $subteam1Lazy->toMap());
|
2012-04-20 00:08:17 +02:00
|
|
|
}
|
2012-10-10 03:59:45 +02:00
|
|
|
|
|
|
|
public function testLazyLoadedFieldsOnVersionedRecords() {
|
|
|
|
// Save another record, sanity check that we're getting the right one
|
|
|
|
$obj2 = new VersionedTest_Subclass();
|
|
|
|
$obj2->Name = "test2";
|
|
|
|
$obj2->ExtraField = "foo2";
|
|
|
|
$obj2->write();
|
|
|
|
|
|
|
|
// Save the actual inspected record
|
|
|
|
$obj1 = new VersionedTest_Subclass();
|
|
|
|
$obj1->Name = "test";
|
|
|
|
$obj1->ExtraField = "foo";
|
|
|
|
$obj1->write();
|
|
|
|
$version1 = $obj1->Version;
|
|
|
|
$obj1->Name = "test2";
|
|
|
|
$obj1->ExtraField = "baz";
|
|
|
|
$obj1->write();
|
|
|
|
$version2 = $obj1->Version;
|
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-10-10 03:59:45 +02:00
|
|
|
$reloaded = Versioned::get_version('VersionedTest_Subclass', $obj1->ID, $version1);
|
|
|
|
$this->assertEquals($reloaded->Name, 'test');
|
|
|
|
$this->assertEquals($reloaded->ExtraField, 'foo');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-10-10 03:59:45 +02:00
|
|
|
$reloaded = Versioned::get_version('VersionedTest_Subclass', $obj1->ID, $version2);
|
|
|
|
$this->assertEquals($reloaded->Name, 'test2');
|
|
|
|
$this->assertEquals($reloaded->ExtraField, 'baz');
|
2013-02-12 04:44:32 +01:00
|
|
|
|
|
|
|
$reloaded = Versioned::get_latest_version('VersionedTest_Subclass', $obj1->ID);
|
|
|
|
$this->assertEquals($reloaded->Version, $version2);
|
|
|
|
$this->assertEquals($reloaded->Name, 'test2');
|
|
|
|
$this->assertEquals($reloaded->ExtraField, 'baz');
|
|
|
|
|
|
|
|
$allVersions = Versioned::get_all_versions('VersionedTest_Subclass', $obj1->ID);
|
|
|
|
$this->assertEquals(2, $allVersions->Count());
|
|
|
|
$this->assertEquals($allVersions->First()->Version, $version1);
|
|
|
|
$this->assertEquals($allVersions->First()->Name, 'test');
|
|
|
|
$this->assertEquals($allVersions->First()->ExtraField, 'foo');
|
|
|
|
$this->assertEquals($allVersions->Last()->Version, $version2);
|
|
|
|
$this->assertEquals($allVersions->Last()->Name, 'test2');
|
|
|
|
$this->assertEquals($allVersions->Last()->ExtraField, 'baz');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-10-10 03:59:45 +02:00
|
|
|
$obj1->delete();
|
|
|
|
}
|
2013-02-12 04:44:32 +01:00
|
|
|
|
|
|
|
public function testLazyLoadedFieldsDoNotReferenceVersionsTable() {
|
|
|
|
// Save another record, sanity check that we're getting the right one
|
|
|
|
$obj2 = new VersionedTest_Subclass();
|
|
|
|
$obj2->Name = "test2";
|
|
|
|
$obj2->ExtraField = "foo2";
|
|
|
|
$obj2->write();
|
|
|
|
|
|
|
|
$obj1 = new VersionedLazySub_DataObject();
|
|
|
|
$obj1->PageName = "old-value";
|
|
|
|
$obj1->ExtraField = "old-value";
|
|
|
|
$obj1ID = $obj1->write();
|
2016-04-01 05:27:59 +02:00
|
|
|
$obj1->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
|
2013-02-12 04:44:32 +01:00
|
|
|
|
|
|
|
$obj1 = VersionedLazySub_DataObject::get()->byID($obj1ID);
|
|
|
|
$this->assertEquals(
|
|
|
|
'old-value',
|
|
|
|
$obj1->PageName,
|
|
|
|
"Correct value on base table when fetching base class"
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'old-value',
|
|
|
|
$obj1->ExtraField,
|
|
|
|
"Correct value on sub table when fetching base class"
|
|
|
|
);
|
|
|
|
|
|
|
|
$obj1 = VersionedLazy_DataObject::get()->byID($obj1ID);
|
|
|
|
$this->assertEquals(
|
|
|
|
'old-value',
|
|
|
|
$obj1->PageName,
|
|
|
|
"Correct value on base table when fetching sub class"
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'old-value',
|
|
|
|
$obj1->ExtraField,
|
|
|
|
"Correct value on sub table when fetching sub class"
|
|
|
|
);
|
|
|
|
|
|
|
|
// Force inconsistent state to test behaviour (shouldn't select from *_versions)
|
|
|
|
DB::query(sprintf(
|
2016-10-20 03:49:22 +02:00
|
|
|
"UPDATE \"VersionedLazy_DataObject_Versions\" SET \"PageName\" = 'versioned-value' " .
|
2013-02-12 04:44:32 +01:00
|
|
|
"WHERE \"RecordID\" = %d",
|
|
|
|
$obj1ID
|
|
|
|
));
|
|
|
|
DB::query(sprintf(
|
2016-10-20 03:49:22 +02:00
|
|
|
"UPDATE \"VersionedLazySub_DataObject_Versions\" SET \"ExtraField\" = 'versioned-value' " .
|
2013-02-12 04:44:32 +01:00
|
|
|
"WHERE \"RecordID\" = %d",
|
|
|
|
$obj1ID
|
|
|
|
));
|
|
|
|
|
|
|
|
$obj1 = VersionedLazySub_DataObject::get()->byID($obj1ID);
|
|
|
|
$this->assertEquals(
|
|
|
|
'old-value',
|
|
|
|
$obj1->PageName,
|
|
|
|
"Correct value on base table when fetching base class"
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'old-value',
|
|
|
|
$obj1->ExtraField,
|
|
|
|
"Correct value on sub table when fetching base class"
|
|
|
|
);
|
|
|
|
$obj1 = VersionedLazy_DataObject::get()->byID($obj1ID);
|
|
|
|
$this->assertEquals(
|
|
|
|
'old-value',
|
|
|
|
$obj1->PageName,
|
|
|
|
"Correct value on base table when fetching sub class"
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'old-value',
|
|
|
|
$obj1->ExtraField,
|
|
|
|
"Correct value on sub table when fetching sub class"
|
|
|
|
);
|
|
|
|
|
|
|
|
// Update live table only to test behaviour (shouldn't select from *_versions or stage)
|
|
|
|
DB::query(sprintf(
|
|
|
|
'UPDATE "VersionedLazy_DataObject_Live" SET "PageName" = \'live-value\' WHERE "ID" = %d',
|
|
|
|
$obj1ID
|
|
|
|
));
|
|
|
|
DB::query(sprintf(
|
|
|
|
'UPDATE "VersionedLazySub_DataObject_Live" SET "ExtraField" = \'live-value\' WHERE "ID" = %d',
|
|
|
|
$obj1ID
|
|
|
|
));
|
|
|
|
|
2016-03-02 06:18:10 +01:00
|
|
|
Versioned::set_stage(Versioned::LIVE);
|
2013-02-12 04:44:32 +01:00
|
|
|
$obj1 = VersionedLazy_DataObject::get()->byID($obj1ID);
|
|
|
|
$this->assertEquals(
|
|
|
|
'live-value',
|
|
|
|
$obj1->PageName,
|
|
|
|
"Correct value from base table when fetching base class on live stage"
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'live-value',
|
|
|
|
$obj1->ExtraField,
|
|
|
|
"Correct value from sub table when fetching base class on live stage"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-04-20 00:08:17 +02:00
|
|
|
}
|
2013-02-12 04:44:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
/** Additional classes for versioned lazy loading testing */
|
2015-02-12 14:15:32 +01:00
|
|
|
class VersionedLazy_DataObject extends DataObject implements TestOnly {
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2013-02-12 04:44:32 +01:00
|
|
|
"PageName" => "Varchar"
|
|
|
|
);
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $extensions = array(
|
2016-06-15 06:03:16 +02:00
|
|
|
"SilverStripe\\ORM\\Versioning\\Versioned('Stage', 'Live')"
|
2013-02-12 04:44:32 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
class VersionedLazySub_DataObject extends VersionedLazy_DataObject {
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2013-02-12 04:44:32 +01:00
|
|
|
"ExtraField" => "Varchar",
|
|
|
|
);
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $extensions = array(
|
2016-06-15 06:03:16 +02:00
|
|
|
"SilverStripe\\ORM\\Versioning\\Versioned('Stage', 'Live')"
|
2013-02-12 04:44:32 +01:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
}
|