From 586d88562cb272da0490fc8dee335cb731a1c5b0 Mon Sep 17 00:00:00 2001 From: spronkey Date: Thu, 12 Sep 2013 14:23:42 +1200 Subject: [PATCH 1/2] Added test case for subsites virtual page onAfterWrite issue, plus changed get_from_all_subsites method to immediately eval and return an ArrayList, instead of lazy eval DataList. Fixes #106 --- code/model/Subsite.php | 2 ++ tests/SubsitesVirtualPageTest.php | 47 +++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/code/model/Subsite.php b/code/model/Subsite.php index 2591ea6..3e470a8 100644 --- a/code/model/Subsite.php +++ b/code/model/Subsite.php @@ -649,6 +649,8 @@ JS; $oldState = self::$disable_subsite_filter; self::$disable_subsite_filter = true; $result = DataObject::get($className, $filter, $sort, $join, $limit); + // conversion to ArrayList forces immediate evaluation, respecting subsite filter setting + $result = ArrayList::create($result->toArray()); self::$disable_subsite_filter = $oldState; return $result; } diff --git a/tests/SubsitesVirtualPageTest.php b/tests/SubsitesVirtualPageTest.php index 6da2166..49a6a3a 100644 --- a/tests/SubsitesVirtualPageTest.php +++ b/tests/SubsitesVirtualPageTest.php @@ -137,6 +137,53 @@ class SubsitesVirtualPageTest extends BaseSubsiteTest { $this->assertFalse($vp->IsModifiedOnStage); } + /** + * This test ensures published Subsites Virtual Pages immediately reflect updates + * to their published target pages. Note - this has to happen when the virtual page + * is in a different subsite to the page you are editing and republishing, + * otherwise the test will pass falsely due to current subsite ID being the same. + */ + function testPublishedSubsiteVirtualPagesUpdateIfTargetPageUpdates() + { + // create page + $p = new Page(); + $p->Content = 'Content'; + $p->Title = 'Title'; + $p->writeToStage('Stage'); + $p->publish('Stage', 'Live'); + $this->assertTrue($p->ExistsOnLive); + + // change to subsite + $subsite = $this->objFromFixture('Subsite', 'subsite2'); + Subsite::changeSubsite($subsite->ID); + Subsite::$disable_subsite_filter = false; + + // create svp in subsite + $svp = new SubsitesVirtualPage(); + $svp->CopyContentFromID = $p->ID; + $svp->writeToStage('Stage'); + $svp->publish('Stage', 'Live'); + $this->assertEquals($svp->SubsiteID, $subsite->ID); + $this->assertTrue($svp->ExistsOnLive); + + // change back to original subsite ("Main site") + Subsite::changeSubsite(0); + + // update original page + $p->Title = 'New Title'; + // "save & publish" + $p->writeToStage('Stage'); + $p->publish('Stage', 'Live'); + $this->assertNotEquals($p->SubsiteID, $subsite->ID); + + // reload SVP from database + // can't use DO::get by id because caches. + $svpdb = $svp->get()->byID($svp->ID); + + // ensure title changed + $this->assertEquals($svpdb->Title, $p->Title); + } + function testUnpublishingParentPageUnpublishesSubsiteVirtualPages() { Config::inst()->update('StaticPublisher', 'disable_realtime', true); From 23e9cd40a00252e49de4f1720114d37950ee5e32 Mon Sep 17 00:00:00 2001 From: spronkey Date: Thu, 12 Sep 2013 15:23:21 +1200 Subject: [PATCH 2/2] Better fix for #106 using DataQuery queryParams. Thanks simon_w --- code/extensions/SiteTreeSubsites.php | 3 ++- code/model/Subsite.php | 6 +----- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/code/extensions/SiteTreeSubsites.php b/code/extensions/SiteTreeSubsites.php index 945a14a..ac3e96e 100644 --- a/code/extensions/SiteTreeSubsites.php +++ b/code/extensions/SiteTreeSubsites.php @@ -25,8 +25,9 @@ class SiteTreeSubsites extends DataExtension { /** * Update any requests to limit the results to the current site */ - function augmentSQL(SQLQuery &$query) { + function augmentSQL(SQLQuery &$query, DataQuery &$dataQuery = null) { if(Subsite::$disable_subsite_filter) return; + if($dataQuery->getQueryParam('Subsite.filter') === false) return; // Don't run on delete queries, since they are always tied to // a specific ID. diff --git a/code/model/Subsite.php b/code/model/Subsite.php index 3e470a8..056e013 100644 --- a/code/model/Subsite.php +++ b/code/model/Subsite.php @@ -646,12 +646,8 @@ JS; } static function get_from_all_subsites($className, $filter = "", $sort = "", $join = "", $limit = "") { - $oldState = self::$disable_subsite_filter; - self::$disable_subsite_filter = true; $result = DataObject::get($className, $filter, $sort, $join, $limit); - // conversion to ArrayList forces immediate evaluation, respecting subsite filter setting - $result = ArrayList::create($result->toArray()); - self::$disable_subsite_filter = $oldState; + $result = $result->setDataQueryParam('Subsite.filter', false); return $result; }