From 9cf7a1453f6c0e32671c78d1594b4723cb9c87f4 Mon Sep 17 00:00:00 2001 From: Mateusz Uzdowski Date: Thu, 27 Mar 2014 17:11:50 +1300 Subject: [PATCH] BUG Fixes to link rewriting when previewing subsites. * JS error with href-less links. * All forms get injected hidden fields, even though the loop attempts to check for only the ones that submit locally. * Also check for action-less forms. Requires https://github.com/silverstripe/silverstripe-framework/pull/3000 to be merged for the Framework. https://github.com/silverstripe-labs/silverstripe-testsession/pull/11 --- .travis.yml | 12 ++- code/model/Subsite.php | 38 ++++--- javascript/LeftAndMain_Subsites.js | 7 +- .../bootstrap/Context/FeatureContext.php | 100 ++++++++++++++++++ .../behat/features/preview-navigation.feature | 26 +++++ 5 files changed, 163 insertions(+), 20 deletions(-) create mode 100644 tests/behat/features/bootstrap/Context/FeatureContext.php create mode 100644 tests/behat/features/preview-navigation.feature diff --git a/.travis.yml b/.travis.yml index c058b7b..cb1ba3b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,11 +13,19 @@ matrix: include: - php: 5.4 env: DB=MYSQL CORE_RELEASE=master + - php: 5.4 + env: DB=MYSQL CORE_RELEASE=3.1 BEHAT_TEST=1 before_script: + - composer self-update + - phpenv rehash - git clone git://github.com/silverstripe-labs/silverstripe-travis-support.git ~/travis-support - - php ~/travis-support/travis_setup.php --source `pwd` --target ~/builds/ss + - "if [ \"$BEHAT_TEST\" = \"\" ]; then php ~/travis-support/travis_setup.php --source `pwd` --target ~/builds/ss; fi" + - "if [ \"$BEHAT_TEST\" = \"1\" ]; then php ~/travis-support/travis_setup.php --source `pwd` --target ~/builds/ss --require silverstripe/behat-extension; fi" - cd ~/builds/ss + - php ~/travis-support/travis_setup_selenium.php --if-env BEHAT_TEST + - php ~/travis-support/travis_setup_php54_webserver.php --if-env BEHAT_TEST script: - - phpunit subsites/tests/ + - "if [ \"$BEHAT_TEST\" = \"\" ]; then phpunit subsites/tests; fi" + - "if [ \"$BEHAT_TEST\" = \"1\" ]; then vendor/bin/behat @subsites; fi" diff --git a/code/model/Subsite.php b/code/model/Subsite.php index 5be9c62..4d4aab5 100644 --- a/code/model/Subsite.php +++ b/code/model/Subsite.php @@ -153,20 +153,26 @@ class Subsite extends DataObject implements PermissionProvider { * @return int Subsite ID */ public static function getSubsiteIDForDomain($host = null, $checkPermissions = true) { - if($host == null) $host = $_SERVER['HTTP_HOST']; + if($host == null && isset($_SERVER['HTTP_HOST'])) { + $host = $_SERVER['HTTP_HOST']; + } - if(!self::$strict_subdomain_matching) $host = preg_replace('/^www\./', '', $host); + $matchingDomains = null; + $cacheKey = null; + if ($host) { + if(!self::$strict_subdomain_matching) $host = preg_replace('/^www\./', '', $host); - $cacheKey = implode('_', array($host, Member::currentUserID(), self::$check_is_public)); - if(isset(self::$_cache_subsite_for_domain[$cacheKey])) return self::$_cache_subsite_for_domain[$cacheKey]; + $cacheKey = implode('_', array($host, Member::currentUserID(), self::$check_is_public)); + if(isset(self::$_cache_subsite_for_domain[$cacheKey])) return self::$_cache_subsite_for_domain[$cacheKey]; + + $SQL_host = Convert::raw2sql($host); + $matchingDomains = DataObject::get( + "SubsiteDomain", + "'$SQL_host' LIKE replace(\"SubsiteDomain\".\"Domain\",'*','%')", + "\"IsPrimary\" DESC" + )->innerJoin('Subsite', "\"Subsite\".\"ID\" = \"SubsiteDomain\".\"SubsiteID\" AND \"Subsite\".\"IsPublic\"=1"); + } - $SQL_host = Convert::raw2sql($host); - $matchingDomains = DataObject::get( - "SubsiteDomain", - "'$SQL_host' LIKE replace(\"SubsiteDomain\".\"Domain\",'*','%')", - "\"IsPrimary\" DESC" - )->innerJoin('Subsite', "\"Subsite\".\"ID\" = \"SubsiteDomain\".\"SubsiteID\" AND \"Subsite\".\"IsPublic\"=1"); - if($matchingDomains && $matchingDomains->Count()) { $subsiteIDs = array_unique($matchingDomains->column('SubsiteID')); $subsiteDomains = array_unique($matchingDomains->column('Domain')); @@ -177,7 +183,7 @@ class Subsite extends DataObject implements PermissionProvider { implode(',', $subsiteDomains) )); } - + $subsiteID = $subsiteIDs[0]; } else if($default = DataObject::get_one('Subsite', "\"DefaultSite\" = 1")) { // Check for a 'default' subsite @@ -186,9 +192,11 @@ class Subsite extends DataObject implements PermissionProvider { // Default subsite id = 0, the main site $subsiteID = 0; } - - self::$_cache_subsite_for_domain[$cacheKey] = $subsiteID; - + + if ($cacheKey) { + self::$_cache_subsite_for_domain[$cacheKey] = $subsiteID; + } + return $subsiteID; } diff --git a/javascript/LeftAndMain_Subsites.js b/javascript/LeftAndMain_Subsites.js index 805a434..deed482 100644 --- a/javascript/LeftAndMain_Subsites.js +++ b/javascript/LeftAndMain_Subsites.js @@ -181,7 +181,7 @@ $(doc).find('a').each(function() { var href = $(this).attr('href'); - if (!href.match(/^http:\/\//)) { + if (typeof href!=='undefined' && !href.match(/^http:\/\//)) { $(this).attr('href', $.path.addSearchParams(href, { 'SubsiteID': subsiteId @@ -192,9 +192,10 @@ // Inject the SubsiteID as a hidden input into all forms submitting to the local site. $(doc).find('form').each(function() { + var action = $(this).attr('action'); - if (!$(this).attr('action').match(/^http:\/\//)) { - $(doc).find('form').append(''); + if (typeof action!=='undefined' && !action.match(/^http:\/\//)) { + $(this).append(''); } }); diff --git a/tests/behat/features/bootstrap/Context/FeatureContext.php b/tests/behat/features/bootstrap/Context/FeatureContext.php new file mode 100644 index 0000000..4c3b82e --- /dev/null +++ b/tests/behat/features/bootstrap/Context/FeatureContext.php @@ -0,0 +1,100 @@ +useContext('BasicContext', new BasicContext($parameters)); + $this->useContext('LoginContext', new LoginContext($parameters)); + $this->useContext('CmsFormsContext', new CmsFormsContext($parameters)); + $this->useContext('CmsUiContext', new CmsUiContext($parameters)); + + $fixtureContext = new FixtureContext($parameters); + $fixtureContext->setFixtureFactory($this->getFixtureFactory()); + $this->useContext('FixtureContext', $fixtureContext); + + // Use blueprints to set user name from identifier + $factory = $fixtureContext->getFixtureFactory(); + $blueprint = \Injector::inst()->create('FixtureBlueprint', 'Member'); + $blueprint->addCallback('beforeCreate', function($identifier, &$data, &$fixtures) { + if(!isset($data['FirstName'])) $data['FirstName'] = $identifier; + }); + $factory->define('Member', $blueprint); + + // Auto-publish pages + foreach(\ClassInfo::subclassesFor('SiteTree') as $id => $class) { + $blueprint = \Injector::inst()->create('FixtureBlueprint', $class); + $blueprint->addCallback('afterCreate', function($obj, $identifier, &$data, &$fixtures) { + $obj->publish('Stage', 'Live'); + }); + $factory->define($class, $blueprint); + } + } + + public function setMinkParameters(array $parameters) { + parent::setMinkParameters($parameters); + + if(isset($parameters['files_path'])) { + $this->getSubcontext('FixtureContext')->setFilesPath($parameters['files_path']); + } + } + + /** + * @return FixtureFactory + */ + public function getFixtureFactory() { + if(!$this->fixtureFactory) { + $this->fixtureFactory = \Injector::inst()->create('BehatFixtureFactory'); + } + + return $this->fixtureFactory; + } + + public function setFixtureFactory(FixtureFactory $factory) { + $this->fixtureFactory = $factory; + } + + // + // Place your definition and hook methods here: + // + // /** + // * @Given /^I have done something with "([^"]*)"$/ + // */ + // public function iHaveDoneSomethingWith($argument) { + // $container = $this->kernel->getContainer(); + // $container->get('some_service')->doSomethingWith($argument); + // } + // +} diff --git a/tests/behat/features/preview-navigation.feature b/tests/behat/features/preview-navigation.feature new file mode 100644 index 0000000..5e36909 --- /dev/null +++ b/tests/behat/features/preview-navigation.feature @@ -0,0 +1,26 @@ +Feature: Preview navigation + As a CMS user + I can navigate a subsite in the preview pane + In order to preview my content + + Background: + Given a "subsite" "My subsite" + And a "page" "My page" with "URLSegment"="my-page", "Content"="My page content anameahref" and "Subsite"="=>Subsite.My subsite" + And a "page" "Other page" with "URLSegment"="other-page", "Content"="Other page content
" and "Subsite"="=>Subsite.My subsite" + Given a "member" "Joe" belonging to "Admin Group" with "Email"="joe@test.com" and "Password"="Password1" + And the "group" "Admin Group" has permissions "Full administrative rights" + And I log in with "joe@test.com" and "Password1" + + Scenario: I can navigate the subsite preview + When I go to "admin" + And I select "My subsite" from "SubsitesSelect" + And I go to "admin/pages" + And I click on "My page" in the tree + And I wait for 3 seconds + And I set the CMS mode to "Preview mode" + And I follow "ahref" in preview + Then the preview contains "Other page content" + # We are already on the second page, submit the form to return to first one. + When I wait for 3 seconds + And I press "Submit my form" in preview + Then the preview contains "My page content"