function testDeletingOrphanedVersions()
 {
     $obj = new VersionedTest_Subclass();
     $obj->ExtraField = 'Foo';
     // ensure that child version table gets written
     $obj->write();
     $obj->publish('Stage', 'Live');
     $obj->ExtraField = 'Bar';
     // ensure that child version table gets written
     $obj->write();
     $obj->publish('Stage', 'Live');
     $versions = DB::query("SELECT COUNT(*) FROM \"VersionedTest_Subclass_versions\" WHERE \"RecordID\" = '{$obj->ID}'")->value();
     $this->assertGreaterThan(0, $versions, 'At least 1 version exists in the history of the page');
     // Force orphaning of all versions created earlier, only on parent record.
     // The child versiones table should still have the correct relationship
     DB::query("DELETE FROM \"VersionedTest_DataObject_versions\" WHERE \"RecordID\" = {$obj->ID}");
     // insert a record with no primary key (ID)
     DB::query("INSERT INTO \"VersionedTest_DataObject_versions\" (\"RecordID\") VALUES ({$obj->ID})");
     // run the script which should clean that up
     $obj->augmentDatabase();
     $versions = DB::query("SELECT COUNT(*) FROM \"VersionedTest_Subclass_versions\" WHERE \"RecordID\" = '{$obj->ID}'")->value();
     $this->assertEquals(0, $versions, 'Orphaned versions on child tables are removed');
     // test that it doesn't delete records that we need
     $obj->write();
     $obj->publish('Stage', 'Live');
     $count = DB::query("SELECT COUNT(*) FROM \"VersionedTest_Subclass_versions\" WHERE \"RecordID\" = '{$obj->ID}'")->value();
     $obj->augmentDatabase();
     $count2 = DB::query("SELECT COUNT(*) FROM \"VersionedTest_Subclass_versions\" WHERE \"RecordID\" = '{$obj->ID}'")->value();
     $this->assertEquals($count, $count2);
 }
 /**
  * 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 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");
 }
예제 #4
0
 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 testLazyLoadFieldsRetrieval()
 {
     // Set reading mode to Stage
     Versioned::set_stage(Versioned::DRAFT);
     // Create object only in reading stage
     $original = new VersionedTest_Subclass();
     $original->ExtraField = 'Foo';
     $original->write();
     // Query for object using base class
     $query = VersionedTest_DataObject::get()->filter('ID', $original->ID);
     // Set reading mode to Live
     Versioned::set_stage(Versioned::LIVE);
     $fetched = $query->first();
     $this->assertTrue($fetched instanceof VersionedTest_Subclass);
     $this->assertEquals($original->ID, $fetched->ID);
     // Eager loaded
     $this->assertEquals($original->ExtraField, $fetched->ExtraField);
     // Lazy loaded
 }