function testVersionedFieldsAdded() { $obj = new VersionedTest_DataObject(); // Check that the Version column is added as a full-fledged column $this->assertType('Int', $obj->dbObject('Version')); $obj2 = new VersionedTest_Subclass(); // Check that the Version column is added as a full-fledged column $this->assertType('Int', $obj2->dbObject('Version')); }
/** * Tests DataObject::hasOwnTableDatabaseField */ public function testHasOwnTableDatabaseFieldWithVersioned() { $noversion = new DataObject(); $versioned = new VersionedTest_DataObject(); $versionedSub = new VersionedTest_Subclass(); $versionField = new VersionedTest_UnversionedWithField(); $this->assertFalse((bool) $noversion->hasOwnTableDatabaseField('Version'), 'Plain models have no version field.'); $this->assertEquals('Int', $versioned->hasOwnTableDatabaseField('Version'), 'The versioned ext adds an Int version field.'); $this->assertEquals('Int', $versionedSub->hasOwnTableDatabaseField('Version'), 'Sub-classes of a versioned model have a Version field.'); $this->assertEquals('Varchar', $versionField->hasOwnTableDatabaseField('Version'), 'Models w/o Versioned can have their own Version field.'); }
/** * Tests that multi-table dataobjects are correctly versioned */ public function testWriteToStage() { // Test subclass with versioned extension directly added $record = VersionedTest_Subclass::create(); $record->Title = "Test A"; $record->ExtraField = "Test A"; $record->writeToStage("Stage"); $this->assertRecordHasLatestVersion($record, 1); $record->publish("Stage", "Live"); $this->assertRecordHasLatestVersion($record, 1); $record->Title = "Test A2"; $record->ExtraField = "Test A2"; $record->writeToStage("Stage"); $this->assertRecordHasLatestVersion($record, 2); // Test subclass without changes to base class $record = VersionedTest_Subclass::create(); $record->ExtraField = "Test B"; $record->writeToStage("Stage"); $this->assertRecordHasLatestVersion($record, 1); $record->publish("Stage", "Live"); $this->assertRecordHasLatestVersion($record, 1); $record->ExtraField = "Test B2"; $record->writeToStage("Stage"); $this->assertRecordHasLatestVersion($record, 2); // Test subclass without changes to sub class $record = VersionedTest_Subclass::create(); $record->Title = "Test C"; $record->writeToStage("Stage"); $this->assertRecordHasLatestVersion($record, 1); $record->publish("Stage", "Live"); $this->assertRecordHasLatestVersion($record, 1); $record->Title = "Test C2"; $record->writeToStage("Stage"); $this->assertRecordHasLatestVersion($record, 2); // Test subclass with versioned extension only added to the base clases $record = VersionedTest_AnotherSubclass::create(); $record->Title = "Test A"; $record->AnotherField = "Test A"; $record->writeToStage("Stage"); $this->assertRecordHasLatestVersion($record, 1); $record->publish("Stage", "Live"); $this->assertRecordHasLatestVersion($record, 1); $record->Title = "Test A2"; $record->AnotherField = "Test A2"; $record->writeToStage("Stage"); $this->assertRecordHasLatestVersion($record, 2); // Test subclass without changes to base class $record = VersionedTest_AnotherSubclass::create(); $record->AnotherField = "Test B"; $record->writeToStage("Stage"); $this->assertRecordHasLatestVersion($record, 1); $record->publish("Stage", "Live"); $this->assertRecordHasLatestVersion($record, 1); $record->AnotherField = "Test B2"; $record->writeToStage("Stage"); $this->assertRecordHasLatestVersion($record, 2); // Test subclass without changes to sub class $record = VersionedTest_AnotherSubclass::create(); $record->Title = "Test C"; $record->writeToStage("Stage"); $this->assertRecordHasLatestVersion($record, 1); $record->publish("Stage", "Live"); $this->assertRecordHasLatestVersion($record, 1); $record->Title = "Test C2"; $record->writeToStage("Stage"); $this->assertRecordHasLatestVersion($record, 2); }
/** * Test that publishing processes respects lazy loaded fields */ public function testLazyLoadFields() { $originalMode = Versioned::get_reading_mode(); // Generate staging record and retrieve it from stage in live mode Versioned::reading_stage('Stage'); $obj = new VersionedTest_Subclass(); $obj->Name = 'bob'; $obj->ExtraField = 'Field Value'; $obj->write(); $objID = $obj->ID; $filter = sprintf('"VersionedTest_DataObject"."ID" = \'%d\'', Convert::raw2sql($objID)); Versioned::reading_stage('Live'); // Check fields are unloaded prior to access $objLazy = Versioned::get_one_by_stage('VersionedTest_DataObject', 'Stage', $filter, false); $lazyFields = $objLazy->getQueriedDatabaseFields(); $this->assertTrue(isset($lazyFields['ExtraField_Lazy'])); $this->assertEquals('VersionedTest_Subclass', $lazyFields['ExtraField_Lazy']); // Check lazy loading works when viewing a Stage object in Live mode $this->assertEquals('Field Value', $objLazy->ExtraField); // Test that writeToStage respects lazy loaded fields $objLazy = Versioned::get_one_by_stage('VersionedTest_DataObject', 'Stage', $filter, false); $objLazy->writeToStage('Live'); $objLive = Versioned::get_one_by_stage('VersionedTest_DataObject', 'Live', $filter, false); $liveLazyFields = $objLive->getQueriedDatabaseFields(); // Check fields are unloaded prior to access $this->assertTrue(isset($liveLazyFields['ExtraField_Lazy'])); $this->assertEquals('VersionedTest_Subclass', $liveLazyFields['ExtraField_Lazy']); // Check that live record has original value $this->assertEquals('Field Value', $objLive->ExtraField); Versioned::set_reading_mode($originalMode); }
public function testAllVersions() { // In 2005 this file was created SS_Datetime::set_mock_now('2005-01-01 00:00:00'); $testPage = new VersionedTest_Subclass(); $testPage->Title = 'Archived page'; $testPage->Content = 'This is the content from 2005'; $testPage->ExtraField = '2005'; $testPage->write(); // In 2007 we updated it SS_Datetime::set_mock_now('2007-01-01 00:00:00'); $testPage->Content = "It's 2007 already!"; $testPage->ExtraField = '2007'; $testPage->write(); // Check both versions are returned $versions = Versioned::get_all_versions('VersionedTest_Subclass', $testPage->ID); $content = array(); $extraFields = array(); foreach ($versions as $version) { $content[] = $version->Content; $extraFields[] = $version->ExtraField; } $this->assertEquals($versions->Count(), 2, 'All versions returned'); $this->assertEquals($content, array('This is the content from 2005', "It's 2007 already!"), 'Version fields returned'); $this->assertEquals($extraFields, array('2005', '2007'), 'Version fields returned'); // In 2009 we updated it again SS_Datetime::set_mock_now('2009-01-01 00:00:00'); $testPage->Content = "I'm enjoying 2009"; $testPage->ExtraField = '2009'; $testPage->write(); // End mock, back to the present day:) SS_Datetime::clear_mock_now(); $versions = Versioned::get_all_versions('VersionedTest_Subclass', $testPage->ID); $content = array(); $extraFields = array(); foreach ($versions as $version) { $content[] = $version->Content; $extraFields[] = $version->ExtraField; } $this->assertEquals($versions->Count(), 3, 'Additional all versions returned'); $this->assertEquals($content, array('This is the content from 2005', "It's 2007 already!", "I'm enjoying 2009"), 'Additional version fields returned'); $this->assertEquals($extraFields, array('2005', '2007', '2009'), 'Additional version fields returned'); }
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(); $obj1->publish('Stage', 'Live'); $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("UPDATE \"VersionedLazy_DataObject_versions\" SET \"PageName\" = 'versioned-value' " . "WHERE \"RecordID\" = %d", $obj1ID)); DB::query(sprintf("UPDATE \"VersionedLazySub_DataObject_versions\" SET \"ExtraField\" = 'versioned-value' " . "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)); Versioned::reading_stage('Live'); $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"); }
/** * Values that are overwritten with null are saved to the _versions table correctly. */ public function testWriteNullValueToVersion() { $record = VersionedTest_Subclass::create(); $record->Title = "Test A"; $record->write(); $version = Versioned::get_latest_version($record->ClassName, $record->ID); $this->assertEquals(1, $version->Version); $this->assertEquals($record->Title, $version->Title); $record->Title = null; $record->write(); $version = Versioned::get_latest_version($record->ClassName, $record->ID); $this->assertEquals(2, $version->Version); $this->assertEquals($record->Title, $version->Title); }