Skip to content
This repository
Browse code

BUGFIX: Allow Versioned::get_latest_version() and Version::get_versio…

…n() to return results if the classname has changed.

Without this bugfix, if you had a Page that used to be a SiteTree, and you tried to use Versiond::get_version() or Versioned::get_latest_version() to return the older SiteTree version, nothing would be returned, because the results were being filtered by ClassName.  This caused bugs in the history panel for certain combinbations of page classname alteration.
  • Loading branch information...
commit b654b9547260e29a0c582a1461bf437c5925c63d 1 parent 0b31234
Sam Minnée authored June 28, 2012
4  model/Versioned.php
@@ -991,7 +991,7 @@ function doRollbackTo($version) {
991 991
 	 */
992 992
 	static function get_latest_version($class, $id) {
993 993
 		$baseClass = ClassInfo::baseDataClass($class);
994  
-		$list = DataList::create($class)->where("\"$baseClass\".\"RecordID\" = $id");
  994
+		$list = DataList::create($baseClass)->where("\"$baseClass\".\"RecordID\" = $id");
995 995
 		$list->dataQuery()->setQueryParam("Versioned.mode", "latest_versions");
996 996
 		return $list->First();
997 997
 	}
@@ -1033,7 +1033,7 @@ static function get_including_deleted($class, $filter = "", $sort = "") {
1033 1033
 	 */
1034 1034
 	static function get_version($class, $id, $version) {
1035 1035
 		$baseClass = ClassInfo::baseDataClass($class);
1036  
-		$list = DataList::create($class)->where("\"$baseClass\".\"RecordID\" = $id")->where("\"$baseClass\".\"Version\" = " . (int)$version);
  1036
+		$list = DataList::create($baseClass)->where("\"$baseClass\".\"RecordID\" = $id")->where("\"$baseClass\".\"Version\" = " . (int)$version);
1037 1037
 		$list->dataQuery()->setQueryParam('Versioned.mode', 'all_versions');
1038 1038
 		return $list->First();
1039 1039
 	}
24  tests/model/VersionedTest.php
@@ -242,6 +242,30 @@ public function testQueriedTables() {
242 242
 	        'VersionedTest_Subclass_Live',
243 243
 	    ), DataObject::get('VersionedTest_Subclass')->dataQuery()->query()->queriedTables());
244 244
 	}
  245
+	
  246
+	public function testGetVersionWhenClassnameChanged() {
  247
+		$obj = new VersionedTest_DataObject;
  248
+		$obj->Name = "test";
  249
+		$obj->write();
  250
+		$obj->Name = "test2";
  251
+		$obj->ClassName = "VersionedTest_Subclass";
  252
+		$obj->write();
  253
+		$subclassVersion = $obj->Version;
  254
+		
  255
+		$obj->Name = "test3";
  256
+		$obj->ClassName = "VersionedTest_DataObject";
  257
+		$obj->write();
  258
+		
  259
+		// We should be able to pass the subclass and still get the correct class back
  260
+		$obj2 = Versioned::get_version("VersionedTest_Subclass", $obj->ID, $subclassVersion);
  261
+		$this->assertInstanceOf("VersionedTest_Subclass", $obj2);
  262
+		$this->assertEquals("test2", $obj2->Name);
  263
+
  264
+		$obj3 = Versioned::get_latest_version("VersionedTest_Subclass", $obj->ID);
  265
+		$this->assertEquals("test3", $obj3->Name);
  266
+		$this->assertInstanceOf("VersionedTest_DataObject", $obj3);
  267
+
  268
+	}
245 269
 }
246 270
 
247 271
 class VersionedTest_DataObject extends DataObject implements TestOnly {

0 notes on commit b654b95

Please sign in to comment.
Something went wrong with that request. Please try again.