Skip to content

Commit

Permalink
Merge pull request #12 from openeuropa/OPENEUROPA-2286
Browse files Browse the repository at this point in the history
OPENEUROPA-2286: Check redirect headers instead of waiting for an external resource.
  • Loading branch information
imanoleguskiza authored Sep 11, 2019
2 parents b8c1dc5 + 701bad9 commit 163b901
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 17 deletions.
11 changes: 5 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@
"composer/installers": "~1.5",
"consolidation/robo": "~1.4",
"consolidation/annotated-command": "^2.8.2",
"drupal-composer/drupal-scaffold": "~2.2",
"drupal-composer/drupal-scaffold": "^2.5.2",
"drupal/config_devel": "~1.2",
"drupal/console": "~1.0",
"drupal/drupal-extension": "~4.0.0@beta",
"drush/drush": "~9.0@stable",
"drupal/drupal-extension": "~4.0",
"drush/drush": "~9.0",
"openeuropa/behat-transformation-context" : "~0.1",
"openeuropa/code-review": "~1.0.0-alpha4",
"openeuropa/code-review": "~1.0@beta",
"openeuropa/drupal-core-require-dev": "^8.7",
"openeuropa/task-runner": "~1.0-beta4",
"openeuropa/task-runner": "~1.0@beta",
"openeuropa/oe_multilingual": "^1.1",
"phpunit/phpunit": "~6.0"
},
Expand Down
62 changes: 52 additions & 10 deletions tests/Behat/SearchContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,70 @@

namespace Drupal\Tests\oe_search\Behat;

use Behat\Behat\Hook\Scope\AfterScenarioScope;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Drupal\DrupalExtension\Context\RawDrupalContext;
use PHPUnit\Framework\Assert;

/**
* The main Search context.
*/
class SearchContext extends RawDrupalContext {

/**
* Assert redirect to expected url.
* Prevent redirections for the whole scenario.
*
* @param \Behat\Behat\Hook\Scope\BeforeScenarioScope $scope
* The Hook scope.
*
* @BeforeScenario @no-redirects
*/
public function preventRedirects(BeforeScenarioScope $scope): void {
$this->iDoNotFollowRedirects();
}

/**
* Allow redirections after the scenario.
*
* @param \Behat\Behat\Hook\Scope\AfterScenarioScope $scope
* The Hook scope.
*
* @param string $uri
* Expected redirect url.
* @AfterScenario @no-redirects
*/
public function allowRedirects(AfterScenarioScope $scope): void {
$this->iFollowRedirects();
}

/**
* Prevent redirects so we can check their target before they happen.
*
* @When /^I do not follow redirects$/
*/
public function iDoNotFollowRedirects(): void {
$this->getSession()->getDriver()->getClient()->followRedirects(FALSE);
}

/**
* Allow redirects during tests (usually after we have prevented them first).
*
* @When /^I follow redirects$/
*/
public function iFollowRedirects(): void {
$this->getSession()->getDriver()->getClient()->followRedirects(TRUE);
}

/**
* Assert redirect to expected url.
*
* @Then I should be redirected to :uri
* @param string $expectedUrl
* The expected url.
*
* @throws \Exception
* @Then /^I (?:am|should be) redirected to "([^"]*)"$/
*/
public function assertRedirect(string $uri): void {
$current_uri = $this->getSession()->getCurrentUrl();
if ($current_uri !== $uri) {
throw new \Exception(sprintf('Redirect to "%s" does not expected.', $uri));
}
public function iAmRedirectedTo(string $expectedUrl): void {
$headers = $this->getSession()->getResponseHeaders();
Assert::assertTrue(isset($headers['Location'][0]));
Assert::assertEquals($expectedUrl, $headers['Location'][0]);
}

}
3 changes: 2 additions & 1 deletion tests/features/search-block.feature
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Feature: Search block
As an anonymous user
I want to search on ec.europa.eu by using the search block

@no-redirects
Scenario: I am redirected to the ec.europa.eu search results page when I use the search block
Given the following languages are available:
| languages |
Expand All @@ -13,7 +14,7 @@ Feature: Search block
When I fill in "Search" with "European Commission"
And I press "Search"
Then I should be redirected to "https://ec.europa.eu/search/?QueryText=European%20Commission&swlang=en"

When I am on "the French home page"
When I fill in "Rechercher" with "European Commission"
And I press "Rechercher"
Expand Down

0 comments on commit 163b901

Please sign in to comment.