From 942c0257b765ebc46bb0b2c3069721ccb8915ff6 Mon Sep 17 00:00:00 2001 From: Damian Mooyman Date: Fri, 20 Jan 2017 15:57:50 +1300 Subject: [PATCH] API Upgrade to behat 3 --- .travis.yml | 5 +- behat.yml | 26 ++++++ composer.json | 13 ++- docs/en/04_Changelogs/4.0.0.md | 3 + src/Dev/BehatFixtureFactory.php | 4 +- src/Security/Member.php | 2 +- src/Security/Permission.php | 7 +- tests/behat/cms-config.yml | 22 ----- tests/behat/config.yml | 23 ----- .../features/bootstrap/FeatureContext.php | 85 ------------------- tests/behat/features/login.feature | 3 +- tests/behat/features/lostpassword.feature | 4 +- .../Behaviour => src}/CmsFormsContext.php | 21 ++--- .../Test/Behaviour => src}/CmsUiContext.php | 46 ++++------ tests/behat/src/FeatureContext.php | 15 ++++ .../php/Security/SecurityDefaultAdminTest.php | 4 +- 16 files changed, 88 insertions(+), 195 deletions(-) create mode 100644 behat.yml delete mode 100644 tests/behat/cms-config.yml delete mode 100644 tests/behat/config.yml delete mode 100644 tests/behat/features/bootstrap/FeatureContext.php rename tests/behat/{features/bootstrap/SilverStripe/Framework/Test/Behaviour => src}/CmsFormsContext.php (94%) rename tests/behat/{features/bootstrap/SilverStripe/Framework/Test/Behaviour => src}/CmsUiContext.php (94%) create mode 100644 tests/behat/src/FeatureContext.php diff --git a/.travis.yml b/.travis.yml index f85f61c28..705f43493 100644 --- a/.travis.yml +++ b/.travis.yml @@ -68,6 +68,7 @@ before_script: - if [[ $PHPUNIT_TEST == cms ]] || [[ $BEHAT_TEST == cms ]]; then php ./cms/tests/bootstrap/mysite.php; fi # Start behat services + - if [[ $BEHAT_TEST ]]; then echo 'SS_BASE_URL=http://localhost:8080/' >> .env; fi - if [[ $BEHAT_TEST ]]; then sh -e /etc/init.d/xvfb start; sleep 3; fi - if [[ $BEHAT_TEST ]]; then (vendor/bin/selenium-server-standalone > selenium.log 2>&1 &); fi - if [[ $BEHAT_TEST == framework ]]; then (vendor/bin/serve --bootstrap-file tests/behat/serve-bootstrap.php &> serve.log &); fi @@ -76,8 +77,8 @@ before_script: script: - if [[ $PHPUNIT_TEST ]]; then vendor/bin/phpunit --testsuite $PHPUNIT_TEST; fi - if [[ $PHPUNIT_COVERAGE_TEST ]]; then vendor/bin/phpunit --testsuite $PHPUNIT_COVERAGE_TEST --coverage-clover=coverage.xml; fi - - if [[ $BEHAT_TEST == framework ]]; then vendor/bin/behat --config tests/behat/config.yml .; fi - - if [[ $BEHAT_TEST == cms ]]; then vendor/bin/behat @cms --config tests/behat/cms-config.yml; fi + - if [[ $BEHAT_TEST == framework ]]; then vendor/bin/behat @framework; fi + - if [[ $BEHAT_TEST == cms ]]; then vendor/bin/behat @cms; fi - if [[ $PHPCS_TEST ]]; then composer run-script lint; fi - if [[ $PHPCS_TEST ]]; then composer run-script php-peg; fi - if [[ $PHPCS_TEST ]]; then git diff-files --quiet -w --relative=src; fi diff --git a/behat.yml b/behat.yml new file mode 100644 index 000000000..c7c8d9faa --- /dev/null +++ b/behat.yml @@ -0,0 +1,26 @@ +default: + suites: + framework: + paths: + - %paths.modules.framework%/tests/behat/features + contexts: + - SilverStripe\Framework\Tests\Behaviour\FeatureContext + - SilverStripe\Framework\Tests\Behaviour\CmsFormsContext + - SilverStripe\Framework\Tests\Behaviour\CmsUiContext + - SilverStripe\BehatExtension\Context\BasicContext + - SilverStripe\BehatExtension\Context\EmailContext + - SilverStripe\BehatExtension\Context\LoginContext + - + SilverStripe\BehatExtension\Context\FixtureContext: + - %paths.modules.framework%/tests/behat/features/files/ + + extensions: + SilverStripe\BehatExtension\MinkExtension: + default_session: selenium2 + javascript_session: selenium2 + selenium2: + browser: firefox + + SilverStripe\BehatExtension\Extension: + screenshot_path: %paths.base%/tests/behat/artifacts/screenshots + bootstrap_file: "tests/behat/serve-bootstrap.php" diff --git a/composer.json b/composer.json index e4b14df79..f6e3bc737 100644 --- a/composer.json +++ b/composer.json @@ -25,9 +25,9 @@ "silverstripe/assets": "^1@dev", "swiftmailer/swiftmailer": "~5.4", "symfony/cache": "^3.3@dev", - "symfony/config": "^2.8", + "symfony/config": "^3.2", "symfony/translation": "^2.8", - "symfony/yaml": "~2.7", + "symfony/yaml": "~3.2", "vlucas/phpdotenv": "^2.4", "php": ">=5.6.0", @@ -44,9 +44,8 @@ "require-dev": { "phpunit/phpunit": "^5.7", "silverstripe/versioned": "^1.0@dev", - "silverstripe/behat-extension": "^2.1.0", + "silverstripe/behat-extension": "^3", "silverstripe/serve": "dev-master", - "silverstripe/testsession": "^2.0.0-alpha3", "se/selenium-server-standalone": "2.41.0" }, "extra": { @@ -72,13 +71,13 @@ "SilverStripe\\Security\\": "src/Security/", "SilverStripe\\Security\\Tests\\": "tests/php/Security/", "SilverStripe\\View\\": "src/View/", - "SilverStripe\\View\\Tests\\": "tests/php/View/" + "SilverStripe\\View\\Tests\\": "tests/php/View/", + "SilverStripe\\Framework\\Tests\\Behaviour\\": "tests/behat/src/" }, "files": [ "src/Core/Constants.php", "src/Dev/PhpUnitShim.php" - ], - "classmap": ["tests/behat/features/bootstrap"] + ] }, "include-path": [ "src/", diff --git a/docs/en/04_Changelogs/4.0.0.md b/docs/en/04_Changelogs/4.0.0.md index ea1a357bc..362c1d3e4 100644 --- a/docs/en/04_Changelogs/4.0.0.md +++ b/docs/en/04_Changelogs/4.0.0.md @@ -53,6 +53,8 @@ guide developers in preparing existing 3.x code for compatibility with 4.0 * Replaced `Zend_Cache` and the `Cache` API with a PSR-16 implementation (symfony/cache) * _ss_environment.php files have been removed in favour of `.env` and "real" environment variables. * admin has been moved to a new module [silverstripe/admin](https://github.com/silverstripe/silverstripe-admin). +* Behat support updated to behat 3. See the + [behat extension](https://github.com/silverstripe/silverstripe-behat-extension) for more information. ## Upgrading @@ -1336,6 +1338,7 @@ A very small number of methods were chosen for deprecation, and will be removed * Search filter classes (e.g. `ExactMatchFilter`) are now registered with `Injector` via a new `DataListFilter.` prefix convention. see [search filter documentation](/developer_guides/model/searchfilters) for more information. +* `Permission::flush_permission_cache()` renamed to `reset()` and added to `Resettable` interface. * `Versioned` API has some breaking changes: * Versioned constructor now only allows a single string to declare whether staging is enabled or not. The number of names of stages are no longer able to be specified. See below for upgrading notes for models diff --git a/src/Dev/BehatFixtureFactory.php b/src/Dev/BehatFixtureFactory.php index 0c5a7a45e..3a94cf2c2 100644 --- a/src/Dev/BehatFixtureFactory.php +++ b/src/Dev/BehatFixtureFactory.php @@ -2,6 +2,8 @@ namespace SilverStripe\Dev; +use SilverStripe\Assets\File; + class BehatFixtureFactory extends FixtureFactory { public function createObject($name, $identifier, $data = null) @@ -12,7 +14,7 @@ class BehatFixtureFactory extends FixtureFactory // Copy identifier to some visible property unless its already defined. // Exclude files, since they generate their own named based on the file path. - if (!is_a($name, 'SilverStripe\\Assets\\File', true)) { + if (!is_a($name, File::class, true)) { foreach (array('Name', 'Title') as $fieldName) { if (singleton($name)->hasField($fieldName) && !isset($data[$fieldName])) { $data[$fieldName] = $identifier; diff --git a/src/Security/Member.php b/src/Security/Member.php index 4680f8020..0e407c70a 100644 --- a/src/Security/Member.php +++ b/src/Security/Member.php @@ -1042,7 +1042,7 @@ class Member extends DataObject implements TemplateGlobalProvider { parent::onAfterWrite(); - Permission::flush_permission_cache(); + Permission::reset(); if ($this->isChanged('Password')) { MemberPassword::log($this); diff --git a/src/Security/Permission.php b/src/Security/Permission.php index 643e37028..ebdf84f4a 100644 --- a/src/Security/Permission.php +++ b/src/Security/Permission.php @@ -3,6 +3,7 @@ namespace SilverStripe\Security; use SilverStripe\Core\ClassInfo; +use SilverStripe\Core\Resettable; use SilverStripe\Dev\TestOnly; use SilverStripe\ORM\DB; use SilverStripe\ORM\DataObject; @@ -19,7 +20,7 @@ use SilverStripe\View\TemplateGlobalProvider; * @property int GroupID * @method Group Group() */ -class Permission extends DataObject implements TemplateGlobalProvider +class Permission extends DataObject implements TemplateGlobalProvider, Resettable { // the (1) after Type specifies the DB default value which is needed for @@ -148,7 +149,7 @@ class Permission extends DataObject implements TemplateGlobalProvider * Flush the permission cache, for example if you have edited group membership or a permission record. * @todo Call this whenever Group_Members is added to or removed from */ - public static function flush_permission_cache() + public static function reset() { self::$cache_permissions = array(); } @@ -711,7 +712,7 @@ class Permission extends DataObject implements TemplateGlobalProvider parent::onBeforeWrite(); // Just in case we've altered someone's permissions - Permission::flush_permission_cache(); + Permission::reset(); } public static function get_template_global_variables() diff --git a/tests/behat/cms-config.yml b/tests/behat/cms-config.yml deleted file mode 100644 index e65be9705..000000000 --- a/tests/behat/cms-config.yml +++ /dev/null @@ -1,22 +0,0 @@ -# Required to run cms tests as subdirectory within framework module -default: - context: - class: SilverStripe\Cms\Test\Behaviour\FeatureContext - formatter: - name: pretty - parameters: - snippets: false - - extensions: - SilverStripe\BehatExtension\MinkExtension: - base_url: http://localhost:8080/ - files_path: %behat.paths.base%/features/files/ - default_session: selenium2 - javascript_session: selenium2 - selenium2: - browser: firefox - - SilverStripe\BehatExtension\Extension: - screenshot_path: %behat.paths.base%/artifacts/screenshots - framework_path: "../.." - bootstrap_file: "cms/tests/behat/serve-bootstrap.php" diff --git a/tests/behat/config.yml b/tests/behat/config.yml deleted file mode 100644 index a19a0fa0d..000000000 --- a/tests/behat/config.yml +++ /dev/null @@ -1,23 +0,0 @@ -default: - context: - class: SilverStripe\Framework\Test\Behaviour\FeatureContext - filters: - tags: "~@todo" - formatter: - name: pretty - parameters: - snippets: false - - extensions: - SilverStripe\BehatExtension\MinkExtension: - base_url: http://localhost:8080/ - files_path: %behat.paths.base%/features/files/ - default_session: selenium2 - javascript_session: selenium2 - selenium2: - browser: firefox - - SilverStripe\BehatExtension\Extension: - screenshot_path: %behat.paths.base%/artifacts/screenshots - framework_path: "../.." - bootstrap_file: "tests/behat/serve-bootstrap.php" diff --git a/tests/behat/features/bootstrap/FeatureContext.php b/tests/behat/features/bootstrap/FeatureContext.php deleted file mode 100644 index 9746f079e..000000000 --- a/tests/behat/features/bootstrap/FeatureContext.php +++ /dev/null @@ -1,85 +0,0 @@ -useContext('BasicContext', new BasicContext($parameters)); - $this->useContext('LoginContext', new LoginContext($parameters)); - $this->useContext('CmsFormsContext', new CmsFormsContext($parameters)); - $this->useContext('CmsUiContext', new CmsUiContext($parameters)); - $this->useContext('EmailContext', new EmailContext($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('SilverStripe\\Dev\\FixtureBlueprint', 'SilverStripe\\Security\\Member'); - $blueprint->addCallback('beforeCreate', function ($identifier, &$data, &$fixtures) { - if (!isset($data['FirstName'])) { - $data['FirstName'] = $identifier; - } - }); - $factory->define('SilverStripe\\Security\\Member', $blueprint); - } - - public function setMinkParameters(array $parameters) - { - parent::setMinkParameters($parameters); - - if (isset($parameters['files_path'])) { - /** @var FixtureContext $fixtureContext */ - $fixtureContext = $this->getSubcontext('FixtureContext'); - $fixtureContext->setFilesPath($parameters['files_path']); - } - } - - /** - * @return FixtureFactory - */ - public function getFixtureFactory() - { - if (!$this->fixtureFactory) { - $this->fixtureFactory = Injector::inst()->create(BehatFixtureFactory::class); - } - return $this->fixtureFactory; - } - - public function setFixtureFactory(FixtureFactory $factory) - { - $this->fixtureFactory = $factory; - } -} diff --git a/tests/behat/features/login.feature b/tests/behat/features/login.feature index 7f8716b1a..f4ae26290 100644 --- a/tests/behat/features/login.feature +++ b/tests/behat/features/login.feature @@ -15,6 +15,5 @@ Feature: Log in Scenario: /admin/ redirect for not logged in user # disable automatic redirection so we can use the profiler - When I go to "/admin/" without redirection - Then I should be redirected to "/Security/login" + When I go to "/admin/" And I should see a log-in form diff --git a/tests/behat/features/lostpassword.feature b/tests/behat/features/lostpassword.feature index 43c8c57d0..bcfe408bf 100644 --- a/tests/behat/features/lostpassword.feature +++ b/tests/behat/features/lostpassword.feature @@ -12,11 +12,11 @@ Feature: Lost Password When I follow "I've lost my password" And I fill in "admin@test.com" for "Email" And I press the "Send me the password reset link" button - Then I should see "Password reset link sent to 'admin@test.com'" + Then I should see "A reset link has been sent to 'admin@test.com'" And there should be an email to "admin@test.com" titled "Your password reset link" When I click on the "password reset link" link in the email to "admin@test.com" Then I should see "Please enter a new password" When I fill in "newpassword" for "New Password" And I fill in "newpassword" for "Confirm New Password" And I press the "Change Password" button - Then the password for "admin@test.com" should be "newpassword" \ No newline at end of file + Then the password for "admin@test.com" should be "newpassword" diff --git a/tests/behat/features/bootstrap/SilverStripe/Framework/Test/Behaviour/CmsFormsContext.php b/tests/behat/src/CmsFormsContext.php similarity index 94% rename from tests/behat/features/bootstrap/SilverStripe/Framework/Test/Behaviour/CmsFormsContext.php rename to tests/behat/src/CmsFormsContext.php index ffcef1d7a..98a540b7f 100644 --- a/tests/behat/features/bootstrap/SilverStripe/Framework/Test/Behaviour/CmsFormsContext.php +++ b/tests/behat/src/CmsFormsContext.php @@ -1,10 +1,11 @@ context = $parameters; - } + use MainContextAwareTrait; /** * Get Mink session from MinkContext diff --git a/tests/behat/features/bootstrap/SilverStripe/Framework/Test/Behaviour/CmsUiContext.php b/tests/behat/src/CmsUiContext.php similarity index 94% rename from tests/behat/features/bootstrap/SilverStripe/Framework/Test/Behaviour/CmsUiContext.php rename to tests/behat/src/CmsUiContext.php index d24709721..0eeae3ff8 100644 --- a/tests/behat/features/bootstrap/SilverStripe/Framework/Test/Behaviour/CmsUiContext.php +++ b/tests/behat/src/CmsUiContext.php @@ -1,33 +1,23 @@ context = $parameters; - } + use MainContextAwareTrait; + use StepHelper; /** * Get Mink session from MinkContext @@ -148,9 +138,9 @@ class CmsUiContext extends BehatContext */ public function stepIShouldSeeInCmsTree($text) { - $cms_tree_element = $this->getCmsTreeElement(); - - $element = $cms_tree_element->find('named', array('content', "'$text'")); + // Wait until visible + $cmsTreeElement = $this->getCmsTreeElement(); + $element = $cmsTreeElement->find('named', array('content', "'$text'")); assertNotNull($element, sprintf('%s not found', $text)); } @@ -159,9 +149,9 @@ class CmsUiContext extends BehatContext */ public function stepIShouldNotSeeInCmsTree($text) { - $cms_tree_element = $this->getCmsTreeElement(); - - $element = $cms_tree_element->find('named', array('content', "'$text'")); + // Wait until not visible + $cmsTreeElement = $this->getCmsTreeElement(); + $element = $cmsTreeElement->find('named', array('content', "'$text'")); assertNull($element, sprintf('%s found', $text)); } @@ -390,10 +380,8 @@ SCRIPT */ public function iSetTheCmsToMode($mode) { - return array( - new Step\When(sprintf('I fill in the "Change view mode" dropdown with "%s"', $mode)), - new Step\When('I wait for 1 second') // wait for CMS layout to redraw - ); + $this->theIFillInTheDropdownWith('Change view mode', $mode); + sleep(1); } /** @@ -430,7 +418,7 @@ SCRIPT $label->click(); - return new Step\When('I wait for the preview to load'); + $this->iWaitForThePreviewToLoad(); } /** diff --git a/tests/behat/src/FeatureContext.php b/tests/behat/src/FeatureContext.php new file mode 100644 index 000000000..826ccaf14 --- /dev/null +++ b/tests/behat/src/FeatureContext.php @@ -0,0 +1,15 @@ +defaultPassword = Security::default_admin_password(); Security::clear_default_admin(); Security::setDefaultAdmin('admin', 'password'); - Permission::flush_permission_cache(); + Permission::reset(); } protected function tearDown() { Security::setDefaultAdmin($this->defaultUsername, $this->defaultPassword); - Permission::flush_permission_cache(); + Permission::reset(); parent::tearDown(); }