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

RedirectContext added to test redirections #15

Open
wants to merge 1 commit into
base: 1.0.x
Choose a base branch
from
Open
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
70 changes: 70 additions & 0 deletions src/DrupalExtension/Context/RedirectContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace NuvoleWeb\Drupal\DrupalExtension\Context;

use Behat\MinkExtension\Context\RawMinkContext;
use Behat\Behat\Context\SnippetAcceptingContext;
use function bovigo\assert\assert;
use function bovigo\assert\predicate\hasKey;
use function bovigo\assert\predicate\equals;

/**
* Class RedirectContext.
*
* Contains step definitions that helps testing redirection.
*
* @package NuvoleWeb\Drupal\DrupalExtension\Context
*/
class RedirectContext extends RawMinkContext implements SnippetAcceptingContext {

/**
* Store follow redirect configuration.
*
* @var null
*/
private $followRedirect = NULL;

/**
* Do not follow redirects.
*
* @When /^I do not follow redirects$/
*/
public function disableFollowRedirects() {
/* @var \Behat\Mink\Driver\GoutteDriver $driver */
$driver = $this->getSession()->getDriver();
$this->followRedirect = $driver->getClient()->isFollowingRedirects();
$driver->getClient()->followRedirects(FALSE);
}

/**
* Reset follow redirect to its original value.
*
* @AfterScenario
*/
public function resetFollowRedirect() {
if ($this->followRedirect !== NULL) {
/* @var \Behat\Mink\Driver\GoutteDriver $driver */
$driver = $this->getSession()->getDriver();
$driver->getClient()->followRedirects($this->followRedirect);
}

$this->followRedirect = NULL;
}

/**
* Checks if I am redirected to $actualPath.
*
* @param string $actualPath
* Path to be redirected to.
*
* @Then /^I (?:am|should be) redirected to "([^"]*)"$/
*/
public function thenIamRedirectedTo($actualPath) {
$headers = $this->getSession()->getResponseHeaders();
assert($headers['Location'], hasKey(0));

$redirectComponents = parse_url($headers['Location'][0]);
assert($redirectComponents['path'], equals($actualPath));
}

}