Hi,

It's becoming standard for us to remove access to the default /node page on new sites. I want to write a Behat test that checks to make sure this page does not exist, but this is difficult because "I visit" requires a 200 response.

  Scenario: I can't see the node page
    Given I am not logged in
    When I visit "node"
    Then the response status code should be 404
  Scenario: I can't see the node page           # features/security.feature:6
    Given I am not logged in                    # Drupal\DrupalExtension\Context\DrupalContext::assertAnonymousUser()
    When I visit "node"                         # Drupal\DrupalExtension\Context\MinkContext::assertAtPath()
      Current response status code is 404, but 200 expected. (Behat\Mink\Exception\ExpectationException)
    Then the response status code should be 404 # Drupal\DrupalExtension\Context\MinkContext::assertResponseStatus()

This test errors out on 'When I visit "node"' because it expects "I visit" to return a 200. I've written a patch to MinkContext.php as a starting point for conversation. It adds a new function, requestPath and supplies "I request" in addition to "I visit". The implied behaviour is "I visit" should succeed at all times and "I request" may not.

With this patch applied, the following works:

  Scenario: I can't see the node page
    Given I am not logged in
    When I request "node"
    Then the response status code should be 404
  Scenario: I can't see the node page           # features/security.feature:6
    Given I am not logged in                    # Drupal\DrupalExtension\Context\DrupalContext::assertAnonymousUser()
    When I request "node"                       # Drupal\DrupalExtension\Context\MinkContext::requestPath()
    Then the response status code should be 404 # Drupal\DrupalExtension\Context\MinkContext::assertResponseStatus()

1 scenario (1 passed)
3 steps (3 passed)

If this is something you're interested in I'm happy to have the patch merged, if not I'd appreciate advice on how to request any URL and test for any response code. In the future I'll need to test redirects as one example of requesting a URL and checking the response.

CommentFileSizeAuthor
behat-drupalextension-request.diff1.07 KBserenecloud
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

pfrenssen’s picture

+++ b/src/Drupal/DrupalExtension/Context/MinkContext.php
@@ -56,6 +56,18 @@ class MinkContext extends MinkExtension implements TranslatableContext {
+   * @Given I try to visit :path
+   * @When I request :path

"@Given I try to visit :path" is a bit weird. It would be better to use "@When" instead for this step definition.

Visiting a path is a state change, so this is best expressed with the "@When" keyword. "@Given" should be reserved for steps that describe the initial state of the scenario (eg. "@Given I am on path :path"). I agree it is quite a subtle distinction :)

Also, as explained on the project page, feature requests should be filed on the GitHub issue queue, so I would suggest to open an issue there.

jhedstrom’s picture

Unless I'm missing something here, you should be able to use the step

Given /^(?:|I )am on "(?P<page>[^"]+)"$/

that comes with the Mink Extension for asserting non-200 HTTP responses. The Drupal Extension adds the extra check for a 200 response code in the 'visit' step since the original Mink Extension did not.

serenecloud’s picture

@pfrenssen thanks for the clarification - that makes more sense, I wasn't sure which to use so I put both in there.

@jhedstrom that's what I needed, you're absolutely right. I've changed to using Given I am on and it's working fine. Thanks for pointing that out - this makes my patch unnecessary.

  Scenario: I can't see the node page           # features/security.feature:6
    Given I am on "node"                        # Drupal\DrupalExtension\Context\MinkContext::visit()
    Then the response status code should be 404 # Drupal\DrupalExtension\Context\MinkContext::assertResponseStatus()

1 scenario (1 passed)
2 steps (2 passed)
0m3.13s (42.30Mb)

serenecloud’s picture

Status: Active » Closed (works as designed)

Closing this issue as there's no need for my patch now I know I can use "Given I am on".