Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created TestModuleContext. #459

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions behat.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ default:
- Drupal\DrupalExtension\Context\MessageContext
- Drupal\DrupalExtension\Context\MinkContext
- Drupal\DrupalExtension\Context\MarkupContext
- Drupal\DrupalExtension\Context\TestModuleContext
filters:
tags: "@blackbox"
extensions:
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"behat/mink-extension": "~2.0",
"behat/mink-goutte-driver": "~1.0",
"behat/mink-selenium2-driver": "~1.1",
"drupal/drupal-driver": "dev-master",
"drupal/drupal-driver": "dev-master#e692d85d56fc613d2601619d7873d157c724112b",
"symfony/dependency-injection": "~2.7|~3.0",
"symfony/event-dispatcher": "~2.7|~3.0"
},
Expand Down
8 changes: 8 additions & 0 deletions doc/contexts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ following contexts:
*BatchContext*
Steps for creating batch items and ensuring a batch is finished processing.

*TestModuleContext*
Allows modules to be automatically installed during a scenario, then
automatically uninstalled afterwards. Scenarios can enable as many modules as
they like using tags; for example, a scenario with the ```@with-module:webform```
tag will install Webform before the scenario begins, then uninstall it after
the scenario ends. Features can also use this tag to enable modules for every
one of their scenarios.

Custom Contexts
---------------

Expand Down
10 changes: 10 additions & 0 deletions features/modules.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@api @d8
Feature: Ensure that test modules are automatically enabled in annotated scenarios
In order to use special test modules during test scenarios
Modules should be automatically enabled

@with-module:migrate
Scenario: Enabling a module
Given I am logged in as a user with the "administer modules" permission
When I visit "/admin/modules"
Then the checkbox "modules[migrate][enable]" should be checked
64 changes: 64 additions & 0 deletions src/Drupal/DrupalExtension/Context/TestModuleContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace Drupal\DrupalExtension\Context;

use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\Behat\Hook\Scope\ScenarioScope;

/**
* Provides the ability to automatically enable and disable test modules.
*/
class TestModuleContext extends RawDrupalContext
{
/**
* Gets all with-module tags from a scenario scope.
*
* @param \Behat\Behat\Hook\Scope\ScenarioScope $scope
* The scenario scope.
*
* @return string[]
* The machine names of the modules to enable during the scenario.
*/
protected function getTestModulesFromTags(ScenarioScope $scope)
{
$modules = array();
$tags = array_merge($scope->getFeature()->getTags(), $scope->getScenario()->getTags());

foreach ($tags as $tag) {
if (strpos($tag, 'with-module:') === 0) {
array_push($modules, substr($tag, 12));
}
}
return array_unique($modules);
}

/**
* Installs test modules for this scenario.
*
* @param \Behat\Behat\Hook\Scope\BeforeScenarioScope $scope
* The scenario scope.
*
* @BeforeScenario
*/
public function installTestModules(BeforeScenarioScope $scope)
{
foreach ($this->getTestModulesFromTags($scope) as $module) {
$this->getDriver()->moduleInstall($module);
}
}

/**
* Unistalls test modules for this scenario.
*
* @param \Behat\Behat\Hook\Scope\BeforeScenarioScope $scope
* The scenario scope.
*
* @AfterScenario
*/
public function uninstallTestModules(BeforeScenarioScope $scope)
{
foreach ($this->getTestModulesFromTags($scope) as $module) {
$this->getDriver()->moduleUninstall($module);
}
}
}