2009-05-06 08:36:16 +02:00
|
|
|
<?php
|
|
|
|
|
2013-05-10 11:27:36 +02:00
|
|
|
/**
|
|
|
|
* @package framework
|
|
|
|
* @subpackage tests
|
|
|
|
*/
|
2009-05-06 08:36:16 +02:00
|
|
|
class VersionedTest extends SapphireTest {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
protected static $fixture_file = 'VersionedTest.yml';
|
2010-04-12 04:03:16 +02:00
|
|
|
|
|
|
|
protected $extraDataObjects = array(
|
|
|
|
'VersionedTest_DataObject',
|
2013-05-07 01:57:49 +02:00
|
|
|
'VersionedTest_Subclass',
|
2014-09-01 23:14:08 +02:00
|
|
|
'VersionedTest_AnotherSubclass',
|
2013-10-24 17:09:17 +02:00
|
|
|
'VersionedTest_RelatedWithoutVersion',
|
2013-11-26 21:57:11 +01:00
|
|
|
'VersionedTest_SingleStage',
|
|
|
|
'VersionedTest_WithIndexes',
|
2015-12-02 05:33:25 +01:00
|
|
|
'VersionedTest_PublicStage',
|
|
|
|
'VersionedTest_PublicViaExtension',
|
2010-04-12 04:03:16 +02:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-19 05:01:51 +02:00
|
|
|
protected $requiredExtensions = array(
|
2013-11-26 21:57:11 +01:00
|
|
|
"VersionedTest_DataObject" => array('Versioned'),
|
|
|
|
"VersionedTest_WithIndexes" => array('Versioned'),
|
2010-10-19 05:01:51 +02:00
|
|
|
);
|
|
|
|
|
2013-11-26 21:57:11 +01:00
|
|
|
public function testUniqueIndexes() {
|
2013-06-21 00:32:08 +02:00
|
|
|
$tableExpectations = array(
|
2013-11-26 21:57:11 +01:00
|
|
|
'VersionedTest_WithIndexes' =>
|
2013-06-21 00:32:08 +02:00
|
|
|
array('value' => true, 'message' => 'Unique indexes are unique in main table'),
|
2013-11-26 21:57:11 +01:00
|
|
|
'VersionedTest_WithIndexes_versions' =>
|
2013-06-21 00:32:08 +02:00
|
|
|
array('value' => false, 'message' => 'Unique indexes are no longer unique in _versions table'),
|
2013-11-26 21:57:11 +01:00
|
|
|
'VersionedTest_WithIndexes_Live' =>
|
2013-06-21 00:32:08 +02:00
|
|
|
array('value' => false, 'message' => 'Unique indexes are no longer unique in _Live table'),
|
2013-11-26 21:57:11 +01:00
|
|
|
);
|
2015-12-09 02:53:21 +01:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
// Test each table's performance
|
|
|
|
foreach ($tableExpectations as $tableName => $expectation) {
|
|
|
|
$indexes = DB::get_schema()->indexList($tableName);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
// Check for presence of all unique indexes
|
|
|
|
$indexColumns = array_map(function($index) {
|
|
|
|
return $index['value'];
|
|
|
|
}, $indexes);
|
|
|
|
sort($indexColumns);
|
|
|
|
$expectedColumns = array('"UniqA"', '"UniqS"');
|
|
|
|
$this->assertEquals(
|
|
|
|
array_values($expectedColumns),
|
|
|
|
array_values(array_intersect($indexColumns, $expectedColumns)),
|
|
|
|
"$tableName has both indexes");
|
|
|
|
|
|
|
|
// Check unique -> non-unique conversion
|
|
|
|
foreach ($indexes as $indexKey => $indexSpec) {
|
|
|
|
if (in_array($indexSpec['value'], $expectedColumns)) {
|
|
|
|
$isUnique = $indexSpec['type'] === 'unique';
|
|
|
|
$this->assertEquals($isUnique, $expectation['value'], $expectation['message']);
|
2014-09-01 23:14:08 +02:00
|
|
|
}
|
2013-11-26 21:57:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testDeletingOrphanedVersions() {
|
2010-10-19 05:01:51 +02:00
|
|
|
$obj = new VersionedTest_Subclass();
|
|
|
|
$obj->ExtraField = 'Foo'; // ensure that child version table gets written
|
|
|
|
$obj->write();
|
|
|
|
$obj->publish('Stage', 'Live');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-19 05:01:51 +02:00
|
|
|
$obj->ExtraField = 'Bar'; // ensure that child version table gets written
|
|
|
|
$obj->write();
|
|
|
|
$obj->publish('Stage', 'Live');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
$versions = DB::query("SELECT COUNT(*) FROM \"VersionedTest_Subclass_versions\""
|
|
|
|
. " WHERE \"RecordID\" = '$obj->ID'")->value();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-19 05:01:51 +02:00
|
|
|
$this->assertGreaterThan(0, $versions, 'At least 1 version exists in the history of the page');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-19 05:01:51 +02:00
|
|
|
// 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");
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-19 05:01:51 +02:00
|
|
|
// insert a record with no primary key (ID)
|
2010-10-19 05:06:15 +02:00
|
|
|
DB::query("INSERT INTO \"VersionedTest_DataObject_versions\" (\"RecordID\") VALUES ($obj->ID)");
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-19 05:01:51 +02:00
|
|
|
// run the script which should clean that up
|
|
|
|
$obj->augmentDatabase();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
$versions = DB::query("SELECT COUNT(*) FROM \"VersionedTest_Subclass_versions\""
|
|
|
|
. " WHERE \"RecordID\" = '$obj->ID'")->value();
|
2010-10-19 05:01:51 +02:00
|
|
|
$this->assertEquals(0, $versions, 'Orphaned versions on child tables are removed');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-19 05:01:51 +02:00
|
|
|
// test that it doesn't delete records that we need
|
|
|
|
$obj->write();
|
|
|
|
$obj->publish('Stage', 'Live');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
$count = DB::query("SELECT COUNT(*) FROM \"VersionedTest_Subclass_versions\""
|
|
|
|
. " WHERE \"RecordID\" = '$obj->ID'")->value();
|
2010-10-19 05:01:51 +02:00
|
|
|
$obj->augmentDatabase();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
$count2 = DB::query("SELECT COUNT(*) FROM \"VersionedTest_Subclass_versions\""
|
|
|
|
. " WHERE \"RecordID\" = '$obj->ID'")->value();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-19 05:01:51 +02:00
|
|
|
$this->assertEquals($count, $count2);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-08-09 23:54:30 +02:00
|
|
|
public function testDuplicate() {
|
|
|
|
$obj1 = new VersionedTest_Subclass();
|
|
|
|
$obj1->ExtraField = 'Foo';
|
|
|
|
$obj1->write(); // version 1
|
|
|
|
$obj1->publish('Stage', 'Live');
|
|
|
|
$obj1->ExtraField = 'Foo2';
|
|
|
|
$obj1->write(); // version 2
|
|
|
|
|
|
|
|
// Make duplicate
|
|
|
|
$obj2 = $obj1->duplicate();
|
|
|
|
|
|
|
|
// Check records differ
|
|
|
|
$this->assertNotEquals($obj1->ID, $obj2->ID);
|
|
|
|
$this->assertEquals(2, $obj1->Version);
|
|
|
|
$this->assertEquals(1, $obj2->Version);
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testForceChangeUpdatesVersion() {
|
2010-10-12 23:42:45 +02:00
|
|
|
$obj = new VersionedTest_DataObject();
|
|
|
|
$obj->Name = "test";
|
|
|
|
$obj->write();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-12 23:42:45 +02:00
|
|
|
$oldVersion = $obj->Version;
|
|
|
|
$obj->forceChange();
|
|
|
|
$obj->write();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-05-27 02:09:23 +02:00
|
|
|
$this->assertTrue(
|
2010-10-12 23:42:45 +02:00
|
|
|
($obj->Version > $oldVersion),
|
2009-05-27 02:09:23 +02:00
|
|
|
"A object Version is increased when just calling forceChange() without any other changes"
|
|
|
|
);
|
|
|
|
}
|
2010-10-19 05:01:51 +02:00
|
|
|
|
2009-05-06 08:36:16 +02:00
|
|
|
/**
|
|
|
|
* Test Versioned::get_including_deleted()
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testGetIncludingDeleted() {
|
2012-12-17 05:15:26 +01:00
|
|
|
// Get all ids of pages
|
2013-01-11 09:59:33 +01:00
|
|
|
$allPageIDs = DataObject::get(
|
2014-08-15 08:53:05 +02:00
|
|
|
'VersionedTest_DataObject',
|
2013-01-11 09:59:33 +01:00
|
|
|
"\"ParentID\" = 0", "\"VersionedTest_DataObject\".\"ID\" ASC"
|
|
|
|
)->column('ID');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-17 05:15:26 +01:00
|
|
|
// Modify a page, ensuring that the Version ID and Record ID will differ,
|
|
|
|
// and then subsequently delete it
|
|
|
|
$targetPage = $this->objFromFixture('VersionedTest_DataObject', 'page3');
|
|
|
|
$targetPage->Content = 'To be deleted';
|
|
|
|
$targetPage->write();
|
|
|
|
$targetPage->delete();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-05-06 08:36:16 +02:00
|
|
|
// Get all items, ignoring deleted
|
2012-09-26 23:34:00 +02:00
|
|
|
$remainingPages = DataObject::get("VersionedTest_DataObject", "\"ParentID\" = 0",
|
|
|
|
"\"VersionedTest_DataObject\".\"ID\" ASC");
|
2009-05-06 08:36:16 +02:00
|
|
|
// Check that page 3 has gone
|
2009-05-14 07:26:47 +02:00
|
|
|
$this->assertNotNull($remainingPages);
|
2016-01-22 02:45:05 +01:00
|
|
|
$this->assertEquals(array("Page 1", "Page 2", "Subclass Page 1"), $remainingPages->column('Title'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-05-06 08:36:16 +02:00
|
|
|
// Get all including deleted
|
2012-09-26 23:34:00 +02:00
|
|
|
$allPages = Versioned::get_including_deleted("VersionedTest_DataObject", "\"ParentID\" = 0",
|
|
|
|
"\"VersionedTest_DataObject\".\"ID\" ASC");
|
2009-05-06 08:36:16 +02:00
|
|
|
// Check that page 3 is still there
|
2016-01-22 02:45:05 +01:00
|
|
|
$this->assertEquals(array("Page 1", "Page 2", "Page 3", "Subclass Page 1"), $allPages->column('Title'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-17 05:15:26 +01:00
|
|
|
// Check that the returned pages have the correct IDs
|
|
|
|
$this->assertEquals($allPageIDs, $allPages->column('ID'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-05-06 08:36:16 +02:00
|
|
|
// Check that this still works if we switch to reading the other stage
|
|
|
|
Versioned::reading_stage("Live");
|
2012-09-26 23:34:00 +02:00
|
|
|
$allPages = Versioned::get_including_deleted("VersionedTest_DataObject", "\"ParentID\" = 0",
|
|
|
|
"\"VersionedTest_DataObject\".\"ID\" ASC");
|
2016-01-22 02:45:05 +01:00
|
|
|
$this->assertEquals(array("Page 1", "Page 2", "Page 3", "Subclass Page 1"), $allPages->column('Title'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-17 05:15:26 +01:00
|
|
|
// Check that the returned pages still have the correct IDs
|
|
|
|
$this->assertEquals($allPageIDs, $allPages->column('ID'));
|
2009-05-06 08:36:16 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testVersionedFieldsAdded() {
|
2009-06-22 04:42:42 +02:00
|
|
|
$obj = new VersionedTest_DataObject();
|
|
|
|
// Check that the Version column is added as a full-fledged column
|
2012-05-09 12:43:22 +02:00
|
|
|
$this->assertInstanceOf('Int', $obj->dbObject('Version'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-06-22 04:42:42 +02:00
|
|
|
$obj2 = new VersionedTest_Subclass();
|
|
|
|
// Check that the Version column is added as a full-fledged column
|
2012-05-09 12:43:22 +02:00
|
|
|
$this->assertInstanceOf('Int', $obj2->dbObject('Version'));
|
2009-06-22 04:42:42 +02:00
|
|
|
}
|
|
|
|
|
2013-05-10 11:27:36 +02:00
|
|
|
public function testVersionedFieldsNotInCMS() {
|
|
|
|
$obj = new VersionedTest_DataObject();
|
|
|
|
|
|
|
|
// the version field in cms causes issues with Versioned::augmentWrite()
|
|
|
|
$this->assertNull($obj->getCMSFields()->dataFieldByName('Version'));
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testPublishCreateNewVersion() {
|
2011-03-23 04:32:24 +01:00
|
|
|
$page1 = $this->objFromFixture('VersionedTest_DataObject', 'page1');
|
2010-10-13 03:04:38 +02:00
|
|
|
$page1->Content = 'orig';
|
|
|
|
$page1->write();
|
2016-02-22 06:42:48 +01:00
|
|
|
$firstVersion = $page1->Version;
|
2010-10-13 03:04:38 +02:00
|
|
|
$page1->publish('Stage', 'Live', false);
|
2016-02-22 06:42:48 +01:00
|
|
|
$this->assertEquals(
|
|
|
|
$firstVersion,
|
|
|
|
$page1->Version,
|
|
|
|
'publish() with $createNewVersion=FALSE does not create a new version'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-13 03:04:38 +02:00
|
|
|
$page1->Content = 'changed';
|
|
|
|
$page1->write();
|
2016-02-22 06:42:48 +01:00
|
|
|
$secondVersion = $page1->Version;
|
|
|
|
$this->assertTrue($firstVersion < $secondVersion, 'write creates new version');
|
|
|
|
|
2010-10-13 03:04:38 +02:00
|
|
|
$page1->publish('Stage', 'Live', true);
|
2016-02-22 06:42:48 +01:00
|
|
|
$thirdVersion = Versioned::get_latest_version('VersionedTest_DataObject', $page1->ID)->Version;
|
|
|
|
$liveVersion = Versioned::get_versionnumber_by_stage('VersionedTest_DataObject', 'Live', $page1->ID);
|
|
|
|
$stageVersion = Versioned::get_versionnumber_by_stage('VersionedTest_DataObject', 'Stage', $page1->ID);
|
|
|
|
$this->assertTrue(
|
|
|
|
$secondVersion < $thirdVersion,
|
|
|
|
'publish() with $createNewVersion=TRUE creates a new version'
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
$liveVersion,
|
|
|
|
$thirdVersion,
|
|
|
|
'publish() with $createNewVersion=TRUE publishes to live'
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
$stageVersion,
|
|
|
|
$secondVersion,
|
|
|
|
'publish() with $createNewVersion=TRUE does not affect stage'
|
|
|
|
);
|
2010-10-13 03:04:38 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testRollbackTo() {
|
2016-01-22 02:45:05 +01:00
|
|
|
$page1 = $this->objFromFixture('VersionedTest_AnotherSubclass', 'subclass1');
|
2010-10-13 03:04:38 +02:00
|
|
|
$page1->Content = 'orig';
|
|
|
|
$page1->write();
|
|
|
|
$page1->publish('Stage', 'Live');
|
|
|
|
$origVersion = $page1->Version;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-13 03:04:38 +02:00
|
|
|
$page1->Content = 'changed';
|
|
|
|
$page1->write();
|
|
|
|
$page1->publish('Stage', 'Live');
|
|
|
|
$changedVersion = $page1->Version;
|
|
|
|
|
|
|
|
$page1->doRollbackTo($origVersion);
|
2016-02-22 06:42:48 +01:00
|
|
|
$page1 = Versioned::get_one_by_stage('VersionedTest_DataObject', 'Stage', array(
|
|
|
|
'"VersionedTest_DataObject"."ID" = ?' => $page1->ID
|
|
|
|
));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-02-22 06:42:48 +01:00
|
|
|
$this->assertTrue($page1->Version == $changedVersion + 1, 'Create a new higher version number');
|
2010-10-13 03:04:38 +02:00
|
|
|
$this->assertEquals('orig', $page1->Content, 'Copies the content from the old version');
|
2016-01-22 02:45:05 +01:00
|
|
|
|
|
|
|
// check db entries
|
|
|
|
$version = DB::prepared_query("SELECT MAX(\"Version\") FROM \"VersionedTest_DataObject_versions\" WHERE \"RecordID\" = ?",
|
|
|
|
array($page1->ID)
|
|
|
|
)->value();
|
|
|
|
$this->assertEquals($page1->Version, $version, 'Correct entry in VersionedTest_DataObject_versions');
|
|
|
|
|
|
|
|
$version = DB::prepared_query("SELECT MAX(\"Version\") FROM \"VersionedTest_AnotherSubclass_versions\" WHERE \"RecordID\" = ?",
|
|
|
|
array($page1->ID)
|
|
|
|
)->value();
|
|
|
|
$this->assertEquals($page1->Version, $version, 'Correct entry in VersionedTest_AnotherSubclass_versions');
|
2010-10-13 03:04:38 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testDeleteFromStage() {
|
2011-03-23 04:32:24 +01:00
|
|
|
$page1 = $this->objFromFixture('VersionedTest_DataObject', 'page1');
|
2010-10-15 04:55:45 +02:00
|
|
|
$pageID = $page1->ID;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 04:55:45 +02:00
|
|
|
$page1->Content = 'orig';
|
|
|
|
$page1->write();
|
|
|
|
$page1->publish('Stage', 'Live');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->assertEquals(1,
|
|
|
|
DB::query('SELECT COUNT(*) FROM "VersionedTest_DataObject" WHERE "ID" = '.$pageID)->value());
|
|
|
|
$this->assertEquals(1,
|
|
|
|
DB::query('SELECT COUNT(*) FROM "VersionedTest_DataObject_Live" WHERE "ID" = '.$pageID)->value());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 04:55:45 +02:00
|
|
|
$page1->deleteFromStage('Live');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 04:55:45 +02:00
|
|
|
// Confirm that deleteFromStage() doesn't manipulate the original record
|
|
|
|
$this->assertEquals($pageID, $page1->ID);
|
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->assertEquals(1,
|
|
|
|
DB::query('SELECT COUNT(*) FROM "VersionedTest_DataObject" WHERE "ID" = '.$pageID)->value());
|
|
|
|
$this->assertEquals(0,
|
|
|
|
DB::query('SELECT COUNT(*) FROM "VersionedTest_DataObject_Live" WHERE "ID" = '.$pageID)->value());
|
2010-10-15 04:55:45 +02:00
|
|
|
|
|
|
|
$page1->delete();
|
|
|
|
|
|
|
|
$this->assertEquals(0, $page1->ID);
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->assertEquals(0,
|
|
|
|
DB::query('SELECT COUNT(*) FROM "VersionedTest_DataObject" WHERE "ID" = '.$pageID)->value());
|
|
|
|
$this->assertEquals(0,
|
|
|
|
DB::query('SELECT COUNT(*) FROM "VersionedTest_DataObject_Live" WHERE "ID" = '.$pageID)->value());
|
2010-10-15 04:55:45 +02:00
|
|
|
}
|
2010-10-13 03:04:38 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testWritingNewToStage() {
|
2010-10-15 05:53:14 +02:00
|
|
|
$origStage = Versioned::current_stage();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 05:53:14 +02:00
|
|
|
Versioned::reading_stage("Stage");
|
2011-03-23 04:32:24 +01:00
|
|
|
$page = new VersionedTest_DataObject();
|
2010-10-15 05:53:14 +02:00
|
|
|
$page->Title = "testWritingNewToStage";
|
|
|
|
$page->URLSegment = "testWritingNewToStage";
|
|
|
|
$page->write();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
$live = Versioned::get_by_stage('VersionedTest_DataObject', 'Live', array(
|
|
|
|
'"VersionedTest_DataObject_Live"."ID"' => $page->ID
|
|
|
|
));
|
2009-11-22 06:16:38 +01:00
|
|
|
$this->assertEquals(0, $live->count());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
$stage = Versioned::get_by_stage('VersionedTest_DataObject', 'Stage',array(
|
|
|
|
'"VersionedTest_DataObject"."ID"' => $page->ID
|
|
|
|
));
|
2009-11-22 06:16:38 +01:00
|
|
|
$this->assertEquals(1, $stage->count());
|
2010-10-15 05:53:14 +02:00
|
|
|
$this->assertEquals($stage->First()->Title, 'testWritingNewToStage');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 05:53:14 +02:00
|
|
|
Versioned::reading_stage($origStage);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* This tests for the situation described in the ticket #5596.
|
2012-09-26 23:34:00 +02:00
|
|
|
* Writing new Page to live first creates a row in VersionedTest_DataObject table (to get the new ID),
|
|
|
|
* then "changes it's mind" in Versioned and writes VersionedTest_DataObject_Live. It does not remove
|
|
|
|
* the VersionedTest_DataObject record though.
|
2014-08-15 08:53:05 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testWritingNewToLive() {
|
2010-10-15 05:53:14 +02:00
|
|
|
$origStage = Versioned::current_stage();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 05:53:14 +02:00
|
|
|
Versioned::reading_stage("Live");
|
2011-03-23 04:32:24 +01:00
|
|
|
$page = new VersionedTest_DataObject();
|
2010-10-15 05:53:14 +02:00
|
|
|
$page->Title = "testWritingNewToLive";
|
|
|
|
$page->URLSegment = "testWritingNewToLive";
|
|
|
|
$page->write();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
$live = Versioned::get_by_stage('VersionedTest_DataObject', 'Live',array(
|
|
|
|
'"VersionedTest_DataObject_Live"."ID"' => $page->ID
|
|
|
|
));
|
2009-11-22 06:16:38 +01:00
|
|
|
$this->assertEquals(1, $live->count());
|
2010-10-15 05:53:14 +02:00
|
|
|
$this->assertEquals($live->First()->Title, 'testWritingNewToLive');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
$stage = Versioned::get_by_stage('VersionedTest_DataObject', 'Stage',array(
|
|
|
|
'"VersionedTest_DataObject"."ID"' => $page->ID
|
|
|
|
));
|
2009-11-22 06:16:38 +01:00
|
|
|
$this->assertEquals(0, $stage->count());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 05:53:14 +02:00
|
|
|
Versioned::reading_stage($origStage);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-19 00:53:19 +02:00
|
|
|
/**
|
2010-10-19 02:58:40 +02:00
|
|
|
* Tests DataObject::hasOwnTableDatabaseField
|
2010-10-19 00:53:19 +02:00
|
|
|
*/
|
|
|
|
public function testHasOwnTableDatabaseFieldWithVersioned() {
|
|
|
|
$noversion = new DataObject();
|
|
|
|
$versioned = new VersionedTest_DataObject();
|
|
|
|
$versionedSub = new VersionedTest_Subclass();
|
2016-01-22 02:45:05 +01:00
|
|
|
$versionedAno = new VersionedTest_AnotherSubclass();
|
2010-10-19 00:53:19 +02:00
|
|
|
$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(
|
2016-01-22 02:45:05 +01:00
|
|
|
null,
|
|
|
|
$versionedSub->hasOwnTableDatabaseField('Version'),
|
|
|
|
'Sub-classes of a versioned model don\'t have a Version field.'
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
null,
|
|
|
|
$versionedAno->hasOwnTableDatabaseField('Version'),
|
|
|
|
'Sub-classes of a versioned model don\'t have a Version field.'
|
2010-10-19 00:53:19 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'Varchar', $versionField->hasOwnTableDatabaseField('Version'),
|
|
|
|
'Models w/o Versioned can have their own Version field.'
|
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-30 07:06:33 +02:00
|
|
|
/**
|
2014-12-03 21:20:39 +01:00
|
|
|
* Test that SQLQuery::queriedTables() applies the version-suffixes properly.
|
2011-03-30 07:06:33 +02:00
|
|
|
*/
|
|
|
|
public function testQueriedTables() {
|
2012-12-08 12:20:20 +01:00
|
|
|
Versioned::reading_stage('Live');
|
2011-03-30 07:06:33 +02:00
|
|
|
|
2012-12-08 12:20:20 +01:00
|
|
|
$this->assertEquals(array(
|
|
|
|
'VersionedTest_DataObject_Live',
|
|
|
|
'VersionedTest_Subclass_Live',
|
|
|
|
), DataObject::get('VersionedTest_Subclass')->dataQuery()->query()->queriedTables());
|
2011-03-30 07:06:33 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-06-28 03:00:40 +02:00
|
|
|
public function testGetVersionWhenClassnameChanged() {
|
|
|
|
$obj = new VersionedTest_DataObject;
|
|
|
|
$obj->Name = "test";
|
|
|
|
$obj->write();
|
|
|
|
$obj->Name = "test2";
|
|
|
|
$obj->ClassName = "VersionedTest_Subclass";
|
|
|
|
$obj->write();
|
|
|
|
$subclassVersion = $obj->Version;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-06-28 03:00:40 +02:00
|
|
|
$obj->Name = "test3";
|
|
|
|
$obj->ClassName = "VersionedTest_DataObject";
|
|
|
|
$obj->write();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-06-28 03:00:40 +02:00
|
|
|
// We should be able to pass the subclass and still get the correct class back
|
|
|
|
$obj2 = Versioned::get_version("VersionedTest_Subclass", $obj->ID, $subclassVersion);
|
|
|
|
$this->assertInstanceOf("VersionedTest_Subclass", $obj2);
|
|
|
|
$this->assertEquals("test2", $obj2->Name);
|
|
|
|
|
|
|
|
$obj3 = Versioned::get_latest_version("VersionedTest_Subclass", $obj->ID);
|
|
|
|
$this->assertEquals("test3", $obj3->Name);
|
|
|
|
$this->assertInstanceOf("VersionedTest_DataObject", $obj3);
|
|
|
|
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-08-20 03:24:28 +02:00
|
|
|
public function testArchiveVersion() {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-08-20 03:24:28 +02:00
|
|
|
// 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();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-08-20 03:24:28 +02:00
|
|
|
// 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();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-08-20 03:24:28 +02:00
|
|
|
// 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();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-08-20 03:24:28 +02:00
|
|
|
// End mock, back to the present day:)
|
|
|
|
SS_Datetime::clear_mock_now();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-08-20 03:24:28 +02:00
|
|
|
// Test 1 - 2006 Content
|
|
|
|
singleton('VersionedTest_Subclass')->flushCache(true);
|
|
|
|
Versioned::set_reading_mode('Archive.2006-01-01 00:00:00');
|
|
|
|
$testPage2006 = DataObject::get('VersionedTest_Subclass')->filter(array('Title' => 'Archived page'))->first();
|
|
|
|
$this->assertInstanceOf("VersionedTest_Subclass", $testPage2006);
|
|
|
|
$this->assertEquals("2005", $testPage2006->ExtraField);
|
|
|
|
$this->assertEquals("This is the content from 2005", $testPage2006->Content);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-08-20 03:24:28 +02:00
|
|
|
// Test 2 - 2008 Content
|
|
|
|
singleton('VersionedTest_Subclass')->flushCache(true);
|
|
|
|
Versioned::set_reading_mode('Archive.2008-01-01 00:00:00');
|
|
|
|
$testPage2008 = DataObject::get('VersionedTest_Subclass')->filter(array('Title' => 'Archived page'))->first();
|
|
|
|
$this->assertInstanceOf("VersionedTest_Subclass", $testPage2008);
|
|
|
|
$this->assertEquals("2007", $testPage2008->ExtraField);
|
|
|
|
$this->assertEquals("It's 2007 already!", $testPage2008->Content);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-08-20 03:24:28 +02:00
|
|
|
// Test 3 - Today
|
|
|
|
singleton('VersionedTest_Subclass')->flushCache(true);
|
|
|
|
Versioned::set_reading_mode('Stage.Stage');
|
2012-09-26 23:34:00 +02:00
|
|
|
$testPageCurrent = DataObject::get('VersionedTest_Subclass')->filter(array('Title' => 'Archived page'))
|
|
|
|
->first();
|
2012-08-20 03:24:28 +02:00
|
|
|
$this->assertInstanceOf("VersionedTest_Subclass", $testPageCurrent);
|
|
|
|
$this->assertEquals("2009", $testPageCurrent->ExtraField);
|
|
|
|
$this->assertEquals("I'm enjoying 2009", $testPageCurrent->Content);
|
|
|
|
}
|
2012-08-20 03:44:56 +02:00
|
|
|
|
|
|
|
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();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-08-20 03:44:56 +02:00
|
|
|
// 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();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-08-20 03:48:48 +02:00
|
|
|
// 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;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-08-20 03:48:48 +02:00
|
|
|
$this->assertEquals($versions->Count(), 2, 'All versions returned');
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->assertEquals($content, array('This is the content from 2005', "It's 2007 already!"),
|
|
|
|
'Version fields returned');
|
2012-08-20 03:48:48 +02:00
|
|
|
$this->assertEquals($extraFields, array('2005', '2007'), 'Version fields returned');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-08-20 03:44:56 +02:00
|
|
|
// 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();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-08-20 03:44:56 +02:00
|
|
|
// End mock, back to the present day:)
|
|
|
|
SS_Datetime::clear_mock_now();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-08-20 03:44:56 +02:00
|
|
|
$versions = Versioned::get_all_versions('VersionedTest_Subclass', $testPage->ID);
|
|
|
|
$content = array();
|
|
|
|
$extraFields = array();
|
|
|
|
foreach($versions as $version)
|
|
|
|
{
|
|
|
|
$content[] = $version->Content;
|
|
|
|
$extraFields[] = $version->ExtraField;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-08-20 03:48:48 +02:00
|
|
|
$this->assertEquals($versions->Count(), 3, 'Additional all versions returned');
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->assertEquals($content,
|
|
|
|
array('This is the content from 2005', "It's 2007 already!", "I'm enjoying 2009"),
|
|
|
|
'Additional version fields returned');
|
2012-08-20 03:48:48 +02:00
|
|
|
$this->assertEquals($extraFields, array('2005', '2007', '2009'), 'Additional version fields returned');
|
2012-08-20 03:44:56 +02:00
|
|
|
}
|
2013-05-07 01:57:49 +02:00
|
|
|
|
|
|
|
public function testArchiveRelatedDataWithoutVersioned() {
|
|
|
|
SS_Datetime::set_mock_now('2009-01-01 00:00:00');
|
|
|
|
|
|
|
|
$relatedData = new VersionedTest_RelatedWithoutVersion();
|
|
|
|
$relatedData->Name = 'Related Data';
|
|
|
|
$relatedDataId = $relatedData->write();
|
|
|
|
|
|
|
|
$testData = new VersionedTest_DataObject();
|
|
|
|
$testData->Title = 'Test';
|
|
|
|
$testData->Content = 'Before Content';
|
|
|
|
$testData->Related()->add($relatedData);
|
|
|
|
$id = $testData->write();
|
|
|
|
|
|
|
|
SS_Datetime::set_mock_now('2010-01-01 00:00:00');
|
|
|
|
$testData->Content = 'After Content';
|
|
|
|
$testData->write();
|
|
|
|
|
|
|
|
Versioned::reading_archived_date('2009-01-01 19:00:00');
|
|
|
|
|
|
|
|
$fetchedData = VersionedTest_DataObject::get()->byId($id);
|
|
|
|
$this->assertEquals('Before Content', $fetchedData->Content, 'We see the correct content of the older version');
|
|
|
|
|
|
|
|
$relatedData = VersionedTest_RelatedWithoutVersion::get()->byId($relatedDataId);
|
|
|
|
$this->assertEquals(
|
|
|
|
1,
|
|
|
|
$relatedData->Related()->count(),
|
|
|
|
'We have a relation, with no version table, querying it still works'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-10-24 17:09:17 +02:00
|
|
|
public function testVersionedWithSingleStage() {
|
2013-06-21 00:32:08 +02:00
|
|
|
$tables = DB::table_list();
|
2013-10-24 17:09:17 +02:00
|
|
|
$this->assertContains(
|
2014-08-15 08:53:05 +02:00
|
|
|
'versionedtest_singlestage',
|
|
|
|
array_keys($tables),
|
2013-10-24 17:09:17 +02:00
|
|
|
'Contains base table'
|
|
|
|
);
|
|
|
|
$this->assertContains(
|
2014-08-15 08:53:05 +02:00
|
|
|
'versionedtest_singlestage_versions',
|
|
|
|
array_keys($tables),
|
2013-10-24 17:09:17 +02:00
|
|
|
'Contains versions table'
|
|
|
|
);
|
|
|
|
$this->assertNotContains(
|
2014-08-15 08:53:05 +02:00
|
|
|
'versionedtest_singlestage_live',
|
|
|
|
array_keys($tables),
|
2013-10-24 17:09:17 +02:00
|
|
|
'Does not contain separate table with _Live suffix'
|
|
|
|
);
|
|
|
|
$this->assertNotContains(
|
2014-08-15 08:53:05 +02:00
|
|
|
'versionedtest_singlestage_stage',
|
|
|
|
array_keys($tables),
|
2013-10-24 17:09:17 +02:00
|
|
|
'Does not contain separate table with _Stage suffix'
|
|
|
|
);
|
|
|
|
|
|
|
|
Versioned::reading_stage("Stage");
|
|
|
|
$obj = new VersionedTest_SingleStage(array('Name' => 'MyObj'));
|
|
|
|
$obj->write();
|
|
|
|
$this->assertNotNull(
|
|
|
|
VersionedTest_SingleStage::get()->byID($obj->ID),
|
|
|
|
'Writes to and reads from default stage if its set explicitly'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-10-24 17:09:17 +02:00
|
|
|
Versioned::reading_stage("Live");
|
|
|
|
$obj = new VersionedTest_SingleStage(array('Name' => 'MyObj'));
|
|
|
|
$obj->write();
|
|
|
|
$this->assertNotNull(
|
|
|
|
VersionedTest_SingleStage::get()->byID($obj->ID),
|
|
|
|
'Writes to and reads from default stage even if a non-matching stage is set'
|
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-03-04 02:48:04 +01:00
|
|
|
/**
|
|
|
|
* Test that publishing processes respects lazy loaded fields
|
|
|
|
*/
|
|
|
|
public function testLazyLoadFields() {
|
|
|
|
$originalMode = Versioned::get_reading_mode();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-03-04 02:48:04 +01:00
|
|
|
// 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');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-03-04 02:48:04 +01:00
|
|
|
// 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']);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-03-04 02:48:04 +01:00
|
|
|
// Check lazy loading works when viewing a Stage object in Live mode
|
|
|
|
$this->assertEquals('Field Value', $objLazy->ExtraField);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-03-04 02:48:04 +01:00
|
|
|
// 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();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-03-04 02:48:04 +01:00
|
|
|
// Check fields are unloaded prior to access
|
|
|
|
$this->assertTrue(isset($liveLazyFields['ExtraField_Lazy']));
|
|
|
|
$this->assertEquals('VersionedTest_Subclass', $liveLazyFields['ExtraField_Lazy']);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-03-04 02:48:04 +01:00
|
|
|
// Check that live record has original value
|
|
|
|
$this->assertEquals('Field Value', $objLive->ExtraField);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-03-04 02:48:04 +01:00
|
|
|
Versioned::set_reading_mode($originalMode);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-03-04 05:43:17 +01:00
|
|
|
/**
|
|
|
|
* Tests that reading mode persists between requests
|
|
|
|
*/
|
|
|
|
public function testReadingPersistent() {
|
2014-06-19 00:38:26 +02:00
|
|
|
$session = Injector::inst()->create('Session', array());
|
2015-12-07 05:25:27 +01:00
|
|
|
$adminID = $this->logInWithPermission('ADMIN');
|
|
|
|
$session->inst_set('loggedInAs', $adminID);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-03-04 05:43:17 +01:00
|
|
|
// Set to stage
|
|
|
|
Director::test('/?stage=Stage', null, $session);
|
|
|
|
$this->assertEquals(
|
2015-12-02 05:33:25 +01:00
|
|
|
'Stage.Stage',
|
|
|
|
$session->inst_get('readingMode'),
|
|
|
|
'Check querystring changes reading mode to Stage'
|
2014-03-04 05:43:17 +01:00
|
|
|
);
|
|
|
|
Director::test('/', null, $session);
|
|
|
|
$this->assertEquals(
|
2015-12-02 05:33:25 +01:00
|
|
|
'Stage.Stage',
|
|
|
|
$session->inst_get('readingMode'),
|
|
|
|
'Check that subsequent requests in the same session remain in Stage mode'
|
2014-03-04 05:43:17 +01:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-03-04 05:43:17 +01:00
|
|
|
// Test live persists
|
|
|
|
Director::test('/?stage=Live', null, $session);
|
|
|
|
$this->assertEquals(
|
2015-12-02 05:33:25 +01:00
|
|
|
'Stage.Live',
|
|
|
|
$session->inst_get('readingMode'),
|
|
|
|
'Check querystring changes reading mode to Live'
|
2014-03-04 05:43:17 +01:00
|
|
|
);
|
|
|
|
Director::test('/', null, $session);
|
|
|
|
$this->assertEquals(
|
2015-12-02 05:33:25 +01:00
|
|
|
'Stage.Live',
|
|
|
|
$session->inst_get('readingMode'),
|
|
|
|
'Check that subsequent requests in the same session remain in Live mode'
|
2014-03-04 05:43:17 +01:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-06-11 06:14:56 +02:00
|
|
|
// Test that session doesn't redundantly store the default stage if it doesn't need to
|
2014-06-19 00:38:26 +02:00
|
|
|
$session2 = Injector::inst()->create('Session', array());
|
2015-12-07 05:25:27 +01:00
|
|
|
$session2->inst_set('loggedInAs', $adminID);
|
2014-06-11 06:14:56 +02:00
|
|
|
Director::test('/', null, $session2);
|
2015-12-07 05:25:27 +01:00
|
|
|
$this->assertArrayNotHasKey('readingMode', $session2->inst_changedData());
|
2014-06-11 06:14:56 +02:00
|
|
|
Director::test('/?stage=Live', null, $session2);
|
2015-12-07 05:25:27 +01:00
|
|
|
$this->assertArrayNotHasKey('readingMode', $session2->inst_changedData());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-05-19 23:47:34 +02:00
|
|
|
// Test choose_site_stage
|
2016-01-20 02:49:46 +01:00
|
|
|
unset($_GET['stage']);
|
|
|
|
unset($_GET['archiveDate']);
|
2014-05-19 23:47:34 +02:00
|
|
|
Session::set('readingMode', 'Stage.Stage');
|
|
|
|
Versioned::choose_site_stage();
|
|
|
|
$this->assertEquals('Stage.Stage', Versioned::get_reading_mode());
|
|
|
|
Session::set('readingMode', 'Archive.2014-01-01');
|
|
|
|
Versioned::choose_site_stage();
|
|
|
|
$this->assertEquals('Archive.2014-01-01', Versioned::get_reading_mode());
|
|
|
|
Session::clear('readingMode');
|
|
|
|
Versioned::choose_site_stage();
|
|
|
|
$this->assertEquals('Stage.Live', Versioned::get_reading_mode());
|
2014-03-04 05:43:17 +01:00
|
|
|
}
|
2013-10-24 17:09:17 +02:00
|
|
|
|
2015-12-07 05:25:27 +01:00
|
|
|
/**
|
|
|
|
* Test that stage parameter is blocked by non-administrative users
|
|
|
|
*/
|
|
|
|
public function testReadingModeSecurity() {
|
2015-12-22 05:45:02 +01:00
|
|
|
$this->setExpectedException('SS_HTTPResponse_Exception');
|
2015-12-07 05:25:27 +01:00
|
|
|
$session = Injector::inst()->create('Session', array());
|
|
|
|
$result = Director::test('/?stage=Stage', null, $session);
|
|
|
|
}
|
|
|
|
|
2014-09-01 23:14:08 +02:00
|
|
|
/**
|
|
|
|
* Ensures that the latest version of a record is the expected value
|
|
|
|
*
|
2016-01-20 02:49:46 +01:00
|
|
|
* @param DataObject $record
|
|
|
|
* @param int $version
|
2014-09-01 23:14:08 +02:00
|
|
|
*/
|
|
|
|
protected function assertRecordHasLatestVersion($record, $version) {
|
|
|
|
foreach(ClassInfo::ancestry(get_class($record), true) as $table) {
|
|
|
|
$versionForClass = DB::prepared_query(
|
|
|
|
$sql = "SELECT MAX(\"Version\") FROM \"{$table}_versions\" WHERE \"RecordID\" = ?",
|
|
|
|
array($record->ID)
|
|
|
|
)->value();
|
|
|
|
$this->assertEquals($version, $versionForClass, "That the table $table has the latest version $version");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
2015-02-08 06:42:36 +01:00
|
|
|
public function testVersionedHandlesRenamedDataObjectFields(){
|
|
|
|
Config::inst()->remove('VersionedTest_RelatedWithoutVersion','db','Name','Varchar');
|
|
|
|
|
|
|
|
Config::inst()->update('VersionedTest_RelatedWithoutVersion','db',array(
|
|
|
|
"NewField" => "Varchar",
|
|
|
|
));
|
|
|
|
|
|
|
|
VersionedTest_RelatedWithoutVersion::add_extension("Versioned('Stage', 'Live')");
|
|
|
|
$this->resetDBSchema(true);
|
|
|
|
$testData = new VersionedTest_RelatedWithoutVersion();
|
|
|
|
$testData->NewField = 'Test';
|
|
|
|
$testData->write();
|
|
|
|
}
|
2015-12-02 05:33:25 +01:00
|
|
|
|
|
|
|
public function testCanView() {
|
|
|
|
$public1ID = $this->idFromFixture('VersionedTest_PublicStage', 'public1');
|
|
|
|
$public2ID = $this->idFromFixture('VersionedTest_PublicViaExtension', 'public2');
|
|
|
|
$privateID = $this->idFromFixture('VersionedTest_DataObject', 'page1');
|
2016-03-21 15:41:21 +01:00
|
|
|
$singleID = $this->idFromFixture('VersionedTest_SingleStage', 'single');
|
2015-12-02 05:33:25 +01:00
|
|
|
|
|
|
|
// Test that all (and only) public pages are viewable in stage mode
|
|
|
|
Session::clear("loggedInAs");
|
|
|
|
Versioned::reading_stage('Stage');
|
|
|
|
$public1 = Versioned::get_one_by_stage('VersionedTest_PublicStage', 'Stage', array('"ID"' => $public1ID));
|
|
|
|
$public2 = Versioned::get_one_by_stage('VersionedTest_PublicViaExtension', 'Stage', array('"ID"' => $public2ID));
|
|
|
|
$private = Versioned::get_one_by_stage('VersionedTest_DataObject', 'Stage', array('"ID"' => $privateID));
|
2016-03-21 15:41:21 +01:00
|
|
|
// Also test an object that has just a single-stage (eg. is only versioned)
|
|
|
|
$single = Versioned::get_one_by_stage('VersionedTest_SingleStage', 'Stage', array('"ID"' => $singleID));
|
|
|
|
|
2015-12-02 05:33:25 +01:00
|
|
|
|
|
|
|
$this->assertTrue($public1->canView());
|
|
|
|
$this->assertTrue($public2->canView());
|
|
|
|
$this->assertFalse($private->canView());
|
2016-03-21 15:41:21 +01:00
|
|
|
$this->assertFalse($single->canView());
|
2015-12-02 05:33:25 +01:00
|
|
|
|
|
|
|
// Adjusting the current stage should not allow objects loaded in stage to be viewable
|
|
|
|
Versioned::reading_stage('Live');
|
|
|
|
$this->assertTrue($public1->canView());
|
|
|
|
$this->assertTrue($public2->canView());
|
|
|
|
$this->assertFalse($private->canView());
|
2016-03-21 15:41:21 +01:00
|
|
|
$this->assertFalse($single->canView());
|
2015-12-02 05:33:25 +01:00
|
|
|
|
|
|
|
// Writing the private page to live should be fine though
|
|
|
|
$private->publish("Stage", "Live");
|
|
|
|
$privateLive = Versioned::get_one_by_stage('VersionedTest_DataObject', 'Live', array('"ID"' => $privateID));
|
|
|
|
$this->assertTrue($private->canView());
|
|
|
|
$this->assertTrue($privateLive->canView());
|
|
|
|
|
|
|
|
// But if the private version becomes different to the live version, it's once again disallowed
|
|
|
|
Versioned::reading_stage('Stage');
|
|
|
|
$private->Title = 'Secret Title';
|
|
|
|
$private->write();
|
|
|
|
$this->assertFalse($private->canView());
|
|
|
|
$this->assertTrue($privateLive->canView());
|
|
|
|
|
|
|
|
// And likewise, viewing a live page (when mode is draft) should be ok
|
|
|
|
Versioned::reading_stage('Stage');
|
|
|
|
$this->assertFalse($private->canView());
|
|
|
|
$this->assertTrue($privateLive->canView());
|
|
|
|
|
|
|
|
// Logging in as admin should allow all permissions
|
|
|
|
$this->logInWithPermission('ADMIN');
|
|
|
|
Versioned::reading_stage('Stage');
|
|
|
|
$this->assertTrue($public1->canView());
|
|
|
|
$this->assertTrue($public2->canView());
|
|
|
|
$this->assertTrue($private->canView());
|
2016-03-21 15:41:21 +01:00
|
|
|
$this->assertTrue($single->canView());
|
2015-12-02 05:33:25 +01:00
|
|
|
}
|
2015-12-09 02:53:21 +01:00
|
|
|
|
|
|
|
|
|
|
|
public function testCanViewStage() {
|
|
|
|
$public = $this->objFromFixture('VersionedTest_PublicStage', 'public1');
|
|
|
|
$private = $this->objFromFixture('VersionedTest_DataObject', 'page1');
|
|
|
|
Session::clear("loggedInAs");
|
|
|
|
Versioned::reading_stage('Stage');
|
|
|
|
|
|
|
|
// Test that all (and only) public pages are viewable in stage mode
|
|
|
|
// Unpublished records are not viewable in live regardless of permissions
|
|
|
|
$this->assertTrue($public->canViewStage('Stage'));
|
|
|
|
$this->assertFalse($private->canViewStage('Stage'));
|
|
|
|
$this->assertFalse($public->canViewStage('Live'));
|
|
|
|
$this->assertFalse($private->canViewStage('Live'));
|
|
|
|
|
|
|
|
// Writing records to live should make both stage and live modes viewable
|
|
|
|
$private->publish("Stage", "Live");
|
|
|
|
$public->publish("Stage", "Live");
|
|
|
|
$this->assertTrue($public->canViewStage('Stage'));
|
|
|
|
$this->assertTrue($private->canViewStage('Stage'));
|
|
|
|
$this->assertTrue($public->canViewStage('Live'));
|
|
|
|
$this->assertTrue($private->canViewStage('Live'));
|
|
|
|
|
|
|
|
// If the draft mode changes, the live mode remains public, although the updated
|
|
|
|
// draft mode is secured for non-public records.
|
|
|
|
$private->Title = 'Secret Title';
|
|
|
|
$private->write();
|
|
|
|
$public->Title = 'Public Title';
|
|
|
|
$public->write();
|
|
|
|
$this->assertTrue($public->canViewStage('Stage'));
|
|
|
|
$this->assertFalse($private->canViewStage('Stage'));
|
|
|
|
$this->assertTrue($public->canViewStage('Live'));
|
|
|
|
$this->assertTrue($private->canViewStage('Live'));
|
|
|
|
}
|
2017-03-03 13:39:31 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
2009-06-22 04:42:42 +02:00
|
|
|
}
|
|
|
|
|
2013-05-10 11:27:36 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @package framework
|
|
|
|
* @subpackage tests
|
|
|
|
*/
|
2010-04-12 04:03:16 +02:00
|
|
|
class VersionedTest_DataObject extends DataObject implements TestOnly {
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2009-06-22 04:42:42 +02:00
|
|
|
"Name" => "Varchar",
|
2011-03-23 04:32:24 +01:00
|
|
|
'Title' => 'Varchar',
|
|
|
|
'Content' => 'HTMLText'
|
2009-06-22 04:42:42 +02:00
|
|
|
);
|
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $extensions = array(
|
2009-06-22 04:42:42 +02:00
|
|
|
"Versioned('Stage', 'Live')"
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $has_one = array(
|
2011-03-23 04:32:24 +01:00
|
|
|
'Parent' => 'VersionedTest_DataObject'
|
|
|
|
);
|
2013-05-07 01:57:49 +02:00
|
|
|
|
|
|
|
private static $many_many = array(
|
|
|
|
'Related' => 'VersionedTest_RelatedWithoutVersion'
|
|
|
|
);
|
|
|
|
|
2015-12-02 05:33:25 +01:00
|
|
|
|
|
|
|
public function canView($member = null) {
|
|
|
|
$extended = $this->extendedCan(__FUNCTION__, $member);
|
|
|
|
if($extended !== null) {
|
|
|
|
return $extended;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2013-05-07 01:57:49 +02:00
|
|
|
}
|
|
|
|
|
2013-11-26 21:57:11 +01:00
|
|
|
class VersionedTest_WithIndexes extends DataObject implements TestOnly {
|
|
|
|
|
|
|
|
private static $db = array(
|
|
|
|
'UniqA' => 'Int',
|
|
|
|
'UniqS' => 'Int',
|
|
|
|
);
|
|
|
|
private static $extensions = array(
|
|
|
|
"Versioned('Stage', 'Live')"
|
|
|
|
);
|
|
|
|
private static $indexes = array(
|
|
|
|
'UniqS_idx' => 'unique ("UniqS")',
|
|
|
|
'UniqA_idx' => array('type' => 'unique', 'name' => 'UniqA_idx', 'value' => '"UniqA"',),
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-10 11:27:36 +02:00
|
|
|
/**
|
|
|
|
* @package framework
|
|
|
|
* @subpackage tests
|
|
|
|
*/
|
2013-05-07 01:57:49 +02:00
|
|
|
class VersionedTest_RelatedWithoutVersion extends DataObject implements TestOnly {
|
|
|
|
|
|
|
|
private static $db = array(
|
|
|
|
'Name' => 'Varchar'
|
|
|
|
);
|
|
|
|
|
|
|
|
private static $belongs_many_many = array(
|
|
|
|
'Related' => 'VersionedTest_DataObject'
|
|
|
|
);
|
|
|
|
|
2009-06-22 04:42:42 +02:00
|
|
|
}
|
2009-05-06 08:36:16 +02:00
|
|
|
|
2013-05-10 11:27:36 +02:00
|
|
|
/**
|
|
|
|
* @package framework
|
|
|
|
* @subpackage tests
|
|
|
|
*/
|
2010-04-12 04:03:16 +02:00
|
|
|
class VersionedTest_Subclass extends VersionedTest_DataObject implements TestOnly {
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2009-06-22 04:42:42 +02:00
|
|
|
"ExtraField" => "Varchar",
|
|
|
|
);
|
|
|
|
}
|
2010-10-19 00:53:19 +02:00
|
|
|
|
2014-09-01 23:14:08 +02:00
|
|
|
/**
|
|
|
|
* @package framework
|
|
|
|
* @subpackage tests
|
|
|
|
*/
|
|
|
|
class VersionedTest_AnotherSubclass extends VersionedTest_DataObject implements TestOnly {
|
|
|
|
private static $db = array(
|
|
|
|
"AnotherField" => "Varchar"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2010-10-19 00:53:19 +02:00
|
|
|
/**
|
2013-05-10 11:27:36 +02:00
|
|
|
* @package framework
|
|
|
|
* @subpackage tests
|
2010-10-19 00:53:19 +02:00
|
|
|
*/
|
|
|
|
class VersionedTest_UnversionedWithField extends DataObject implements TestOnly {
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array('Version' => 'Varchar(255)');
|
2010-10-19 00:53:19 +02:00
|
|
|
}
|
2013-10-24 17:09:17 +02:00
|
|
|
|
|
|
|
class VersionedTest_SingleStage extends DataObject implements TestOnly {
|
|
|
|
private static $db = array(
|
|
|
|
'Name' => 'Varchar'
|
|
|
|
);
|
|
|
|
|
|
|
|
private static $extensions = array(
|
|
|
|
'Versioned("Stage")'
|
|
|
|
);
|
2013-06-21 00:32:08 +02:00
|
|
|
}
|
2015-12-02 05:33:25 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Versioned dataobject with public stage mode
|
|
|
|
*/
|
|
|
|
class VersionedTest_PublicStage extends DataObject implements TestOnly {
|
|
|
|
private static $db = array(
|
|
|
|
'Title' => 'Varchar'
|
|
|
|
);
|
|
|
|
|
|
|
|
private static $extensions = array(
|
|
|
|
"Versioned('Stage', 'Live')"
|
|
|
|
);
|
|
|
|
|
|
|
|
public function canView($member = null) {
|
|
|
|
$extended = $this->extendedCan(__FUNCTION__, $member);
|
|
|
|
if($extended !== null) {
|
|
|
|
return $extended;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function canViewVersioned($member = null) {
|
|
|
|
// All non-live modes are public
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Public access is provided via extension rather than overriding canViewVersioned
|
|
|
|
*/
|
|
|
|
class VersionedTest_PublicViaExtension extends DataObject implements TestOnly {
|
|
|
|
|
|
|
|
public function canView($member = null) {
|
|
|
|
$extended = $this->extendedCan(__FUNCTION__, $member);
|
|
|
|
if($extended !== null) {
|
|
|
|
return $extended;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static $db = array(
|
|
|
|
'Title' => 'Varchar'
|
|
|
|
);
|
|
|
|
|
|
|
|
private static $extensions = array(
|
|
|
|
"Versioned('Stage', 'Live')",
|
|
|
|
"VersionedTest_PublicExtension"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Alters stage mode of extended object to be public
|
|
|
|
*/
|
|
|
|
class VersionedTest_PublicExtension extends DataExtension implements TestOnly {
|
|
|
|
public function canViewNonLive($member = null) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|