Commit 6e7d0d98 authored by catch's avatar catch
Browse files

Issue #3133355 by longwave, mondrake, ravi.shankar, jungle, xjm: Introduce...

Issue #3133355 by longwave, mondrake, ravi.shankar, jungle, xjm: Introduce PHPUnit-compliant WebAssert::responseHeaderExists() method, and its negative
parent b6ced219
Loading
Loading
Loading
Loading
+15 −0
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@
use Drupal\Core\Render\Markup;
use Drupal\Core\Url;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpException;

/**
@@ -95,6 +96,20 @@ public function error() {
    ];
  }

  /**
   * Sets an HTTP header,
   *
   * @param string $name
   *   The header name.
   * @param string $value
   *   (optional) The header value ot set.
   */
  public function setHeader($name, $value = NULL) {
    $response = new Response();
    $response->headers->set($name, $value);
    return $response;
  }

  /**
   * Renders a page with encoded markup.
   *
+8 −0
Original line number Diff line number Diff line
@@ -59,6 +59,14 @@ test_page_test.error:
  requirements:
    _access: 'TRUE'

test_page_test.null_header:
  path: '/test-null-header'
  defaults:
    _controller: '\Drupal\test_page_test\Controller\Test::setHeader'
    name: Null-Header
  requirements:
    _access: 'TRUE'

test_page_test.encoded:
  path: '/test-encoded'
  defaults:
+12 −0
Original line number Diff line number Diff line
@@ -57,6 +57,7 @@ public function testGoTo() {
    $this->assertStringNotContainsString('</html>', $text);

    // Response includes cache tags that we can assert.
    $this->assertSession()->responseHeaderExists('X-Drupal-Cache-Tags');
    $this->assertSession()->responseHeaderEquals('X-Drupal-Cache-Tags', 'http_response rendered');

    // Test that we can read the JS settings.
@@ -75,6 +76,7 @@ public function testGoTo() {
    $this->drupalGet('system-test/header', [], [
      'Test-Header' => 'header value',
    ]);
    $this->assertSession()->responseHeaderExists('Test-Header');
    $returned_header = $this->getSession()->getResponseHeader('Test-Header');
    $this->assertSame('header value', $returned_header);
  }
@@ -227,6 +229,16 @@ public function testLinkNotExistsExact() {
    $this->assertSession()->linkNotExistsExact('foo|bar');
  }

  /**
   * Tests responseHeaderDoesNotExist() functionality.
   *
   * @see \Drupal\Tests\WebAssert::responseHeaderDoesNotExist()
   */
  public function testResponseHeaderDoesNotExist() {
    $this->drupalGet('test-pipe-char');
    $this->assertSession()->responseHeaderDoesNotExist('Foo-Bar');
  }

  /**
   * Tests linkNotExistsExact() functionality fail.
   *
+56 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\FunctionalTests;

use Drupal\Tests\BrowserTestBase;
use PHPUnit\Framework\AssertionFailedError;

/**
 * Tests WebAssert functionality.
 *
 * @group browsertestbase
 * @coversDefaultClass \Drupal\Tests\WebAssert
 */
class WebAssertTest extends BrowserTestBase {

  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'test_page_test',
  ];

  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

  /**
   * Tests WebAssert::responseHeaderExists().
   *
   * @covers ::responseHeaderExists
   */
  public function testResponseHeaderExists() {
    $this->drupalGet('test-null-header');
    $this->assertSession()->responseHeaderExists('Null-Header');

    $this->expectException(AssertionFailedError::class);
    $this->expectExceptionMessage("Failed asserting that the response has a 'does-not-exist' header.");
    $this->assertSession()->responseHeaderExists('does-not-exist');
  }

  /**
   * Tests WebAssert::responseHeaderDoesNotExist().
   *
   * @covers ::responseHeaderDoesNotExist
   */
  public function testResponseHeaderDoesNotExist() {
    $this->drupalGet('test-null-header');
    $this->assertSession()->responseHeaderDoesNotExist('does-not-exist');

    $this->expectException(AssertionFailedError::class);
    $this->expectExceptionMessage("Failed asserting that the response does not have a 'Null-Header' header.");
    $this->assertSession()->responseHeaderDoesNotExist('Null-Header');
  }

}
+39 −0
Original line number Diff line number Diff line
@@ -10,6 +10,9 @@
use Behat\Mink\Session;
use Drupal\Component\Utility\Html;
use Drupal\Core\Url;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\Constraint\ArrayHasKey;
use PHPUnit\Framework\Constraint\LogicalNot;

/**
 * Defines a class with methods for asserting presence of elements during tests.
@@ -55,6 +58,42 @@ protected function cleanUrl($url) {
    return parent::cleanUrl($url);
  }

  /**
   * Asserts that the current response header has a specific entry.
   *
   * @param string $name
   *   The name of the header entry to check existence of.
   * @param string $message
   *   The failure message.
   */
  public function responseHeaderExists(string $name, string $message = ''): void {
    if ($message === '') {
      $message = "Failed asserting that the response has a '$name' header.";
    }
    $headers = $this->session->getResponseHeaders();
    $constraint = new ArrayHasKey($name);
    Assert::assertThat($headers, $constraint, $message);
  }

  /**
   * Asserts that the current response header does not have a specific entry.
   *
   * @param string $name
   *   The name of the header entry to check existence of.
   * @param string $message
   *   The failure message.
   */
  public function responseHeaderDoesNotExist(string $name, string $message = ''): void {
    if ($message === '') {
      $message = "Failed asserting that the response does not have a '$name' header.";
    }
    $headers = $this->session->getResponseHeaders();
    $constraint = new LogicalNot(
      new ArrayHasKey($name)
    );
    Assert::assertThat($headers, $constraint, $message);
  }

  /**
   * Checks that specific button exists on the current page.
   *