From f348f5fa97754f2c9e19e6d8a5f472699310919e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Werner=20M=2E=20Krau=C3=9F?= Date: Tue, 30 May 2017 17:58:31 +0200 Subject: [PATCH] fix SiteConfigSubsites AugmentSQL: get the right table name to filter --- code/extensions/SiteConfigSubsites.php | 31 +++++++++++++++----------- tests/php/SiteConfigSubsitesTest.php | 6 ++--- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/code/extensions/SiteConfigSubsites.php b/code/extensions/SiteConfigSubsites.php index b886361..8729224 100644 --- a/code/extensions/SiteConfigSubsites.php +++ b/code/extensions/SiteConfigSubsites.php @@ -21,11 +21,14 @@ class SiteConfigSubsites extends DataExtension 'Subsite' => Subsite::class, // The subsite that this page belongs to ]; - /** - * Update any requests to limit the results to the current site - */ - public function augmentSQL(SQLSelect$query, DataQuery $dataQuery = null) { - if(Subsite::$disable_subsite_filter) {return;} + /** + * Update any requests to limit the results to the current site + */ + public function augmentSQL(SQLSelect $query, DataQuery $dataQuery = null) + { + if (Subsite::$disable_subsite_filter) { + return; + } // If you're querying by ID, ignore the sub-site - this is a bit ugly... if ($query->filtersOnID()) { @@ -38,14 +41,16 @@ class SiteConfigSubsites extends DataExtension } } - $subsiteID = (int)Subsite::currentSubsiteID(); + $subsiteID = (int)Subsite::currentSubsiteID(); - $froms=$query->getFrom(); - $froms=array_keys($froms); - $tableName = array_shift($froms); - if($tableName != SiteConfig::class) { return;} - $query->addWhere("\"$tableName\".\"SubsiteID\" IN ($subsiteID)"); - } + $froms = $query->getFrom(); + $froms = array_keys($froms); + $tableName = array_shift($froms); + if ($tableName !== SiteConfig::getSchema()->tableName(SiteConfig::class)) { + return; + } + $query->addWhere("\"$tableName\".\"SubsiteID\" IN ($subsiteID)"); + } public function onBeforeWrite() { @@ -59,7 +64,7 @@ class SiteConfigSubsites extends DataExtension */ public function cacheKeyComponent() { - return 'subsite-'.Subsite::currentSubsiteID(); + return 'subsite-' . Subsite::currentSubsiteID(); } public function updateCMSFields(FieldList $fields) diff --git a/tests/php/SiteConfigSubsitesTest.php b/tests/php/SiteConfigSubsitesTest.php index d2f3e92..0d8a1fe 100644 --- a/tests/php/SiteConfigSubsitesTest.php +++ b/tests/php/SiteConfigSubsitesTest.php @@ -33,11 +33,11 @@ class SiteConfigSubsitesTest extends BaseSubsiteTest $sc->write(); Subsite::changeSubsite(0); - $this->assertEquals(SiteConfig::current_site_config()->Title, 'RootSite'); + $this->assertEquals('RootSite', SiteConfig::current_site_config()->Title); Subsite::changeSubsite($subsite1->ID); - $this->assertEquals(SiteConfig::current_site_config()->Title, 'Subsite1'); + $this->assertEquals('Subsite1', SiteConfig::current_site_config()->Title); Subsite::changeSubsite($subsite2->ID); - $this->assertEquals(SiteConfig::current_site_config()->Title, 'Subsite2'); + $this->assertEquals('Subsite2', SiteConfig::current_site_config()->Title); $keys = SiteConfig::current_site_config()->extend('cacheKeyComponent'); $this->assertContains('subsite-' . $subsite2->ID, $keys);