You are here

class ContentTypeHeaderMatcherTest in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/tests/Drupal/Tests/Core/Routing/ContentTypeHeaderMatcherTest.php \Drupal\Tests\Core\Routing\ContentTypeHeaderMatcherTest

Confirm that the content types partial matcher is functioning properly.

@group Routing

Hierarchy

Expanded class hierarchy of ContentTypeHeaderMatcherTest

File

core/tests/Drupal/Tests/Core/Routing/ContentTypeHeaderMatcherTest.php, line 19
Contains \Drupal\Tests\Core\Routing\ContentTypeHeaderMatcherTest.

Namespace

Drupal\Tests\Core\Routing
View source
class ContentTypeHeaderMatcherTest extends UnitTestCase {

  /**
   * A collection of shared fixture data for tests.
   *
   * @var RoutingFixtures
   */
  protected $fixtures;

  /**
   * The matcher object that is going to be tested.
   *
   * @var \Drupal\Core\Routing\ContentTypeHeaderMatcher
   */
  protected $matcher;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this->fixtures = new RoutingFixtures();
    $this->matcher = new ContentTypeHeaderMatcher();
  }

  /**
   * Tests that routes are not filtered on GET requests.
   */
  public function testGetRequestFilter() {
    $collection = $this->fixtures
      ->sampleRouteCollection();
    $collection
      ->addCollection($this->fixtures
      ->contentRouteCollection());
    $request = Request::create('path/two', 'GET');
    $routes = $this->matcher
      ->filter($collection, $request);
    $this
      ->assertEquals(count($routes), 7, 'The correct number of routes was found.');
  }

  /**
   * Tests that XML-restricted routes get filtered out on JSON requests.
   */
  public function testJsonRequest() {
    $collection = $this->fixtures
      ->sampleRouteCollection();
    $collection
      ->addCollection($this->fixtures
      ->contentRouteCollection());
    $request = Request::create('path/two', 'POST');
    $request->headers
      ->set('Content-type', 'application/json');
    $routes = $this->matcher
      ->filter($collection, $request);
    $this
      ->assertEquals(count($routes), 6, 'The correct number of routes was found.');
    $this
      ->assertNotNull($routes
      ->get('route_f'), 'The json route was found.');
    $this
      ->assertNull($routes
      ->get('route_g'), 'The xml route was not found.');
    foreach ($routes as $name => $route) {
      $this
        ->assertEquals($name, 'route_f', 'The json route is the first one in the collection.');
      break;
    }
  }

  /**
   * Tests route filtering on POST form submission requests.
   */
  public function testPostForm() {
    $collection = $this->fixtures
      ->sampleRouteCollection();
    $collection
      ->addCollection($this->fixtures
      ->contentRouteCollection());

    // Test that all XML and JSON restricted routes get filtered out on a POST
    // form submission.
    $request = Request::create('path/two', 'POST');
    $request->headers
      ->set('Content-type', 'application/www-form-urlencoded');
    $routes = $this->matcher
      ->filter($collection, $request);
    $this
      ->assertEquals(count($routes), 5, 'The correct number of routes was found.');
    $this
      ->assertNull($routes
      ->get('route_f'), 'The json route was found.');
    $this
      ->assertNull($routes
      ->get('route_g'), 'The xml route was not found.');
  }

  /**
   * Confirms that the matcher throws an exception for no-route.
   *
   * @expectedException \Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException
   * @expectedExceptionMessage No route found that matches the Content-Type header.
   */
  public function testNoRouteFound() {
    $matcher = new ContentTypeHeaderMatcher();
    $routes = $this->fixtures
      ->contentRouteCollection();
    $request = Request::create('path/two', 'POST');
    $request->headers
      ->set('Content-type', 'application/hal+json');
    $matcher
      ->filter($routes, $request);
    $this
      ->fail('No exception was thrown.');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ContentTypeHeaderMatcherTest::$fixtures protected property A collection of shared fixture data for tests.
ContentTypeHeaderMatcherTest::$matcher protected property The matcher object that is going to be tested.
ContentTypeHeaderMatcherTest::setUp protected function Overrides UnitTestCase::setUp
ContentTypeHeaderMatcherTest::testGetRequestFilter public function Tests that routes are not filtered on GET requests.
ContentTypeHeaderMatcherTest::testJsonRequest public function Tests that XML-restricted routes get filtered out on JSON requests.
ContentTypeHeaderMatcherTest::testNoRouteFound public function Confirms that the matcher throws an exception for no-route.
ContentTypeHeaderMatcherTest::testPostForm public function Tests route filtering on POST form submission requests.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root.
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName protected function Mocks a block with a block plugin.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed in array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.