You are here

class CorsResponseEventSubscriberTest in CORS 8

Hierarchy

Expanded class hierarchy of CorsResponseEventSubscriberTest

File

tests/src/Unit/EventSubscriber/CorsResponseEventSubscriberTest.php, line 20
Contains Drupal\Tests\cors\Unit\EventSubscriber\CorsResponseEventSubscriberTest.

Namespace

Drupal\Tests\cors\Unit\EventSubscriber
View source
class CorsResponseEventSubscriberTest extends UnitTestCase {

  /**
   * {@inheritdoc}
   */
  public static function getInfo() {
    return array(
      'name' => 'CORS Response Event Subscriber',
      'description' => 'Tests the CORS response event subscriber',
      'group' => 'CORS',
    );
  }

  /**
   * Tests adding CORS headers to the response.
   */
  public function testAddCorsHeaders() {
    $config_factory = $this
      ->getConfigFactoryStub(array(
      "cors.settings" => array(
        "domains" => array(
          "* | http://example.com",
        ),
      ),
    ));
    $alias_manager = $this
      ->getMock('Drupal\\Core\\Path\\AliasManagerInterface');
    $path_matcher = $this
      ->getMock('Drupal\\Core\\Path\\PathMatcherInterface');
    $path_matcher
      ->expects($this
      ->any())
      ->method('matchPath')
      ->withAnyParameters()
      ->will($this
      ->returnValue(TRUE));

    // Create the response event subscriber.
    $subscriber = new CorsResponseEventSubscriber($config_factory, $alias_manager, $path_matcher);

    // Create the response event.
    $http_kernel = $this
      ->getMock('Symfony\\Component\\HttpKernel\\HttpKernelInterface');
    $request = new Request();
    $response = new Response();
    $event = new FilterResponseEvent($http_kernel, $request, HttpKernelInterface::MASTER_REQUEST, $response);

    // Call the event handler.
    $subscriber
      ->addCorsHeaders($event);
    $this
      ->assertEquals('*', $response->headers
      ->get('access-control-allow-origin'), "The access-control-allow-origin header was set");
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CorsResponseEventSubscriberTest::getInfo public static function
CorsResponseEventSubscriberTest::testAddCorsHeaders public function Tests adding CORS headers to the response.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed 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.
UnitTestCase::setUp protected function 340