RedirectResponseSubscriberTest.php 3.42 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
<?php

/**
 * @file
 * Contains \Drupal\Tests\Core\EventSubscriber\RedirectResponseSubscriberTest.
 */

namespace Drupal\Tests\Core\EventSubscriber;

use Drupal\Core\EventSubscriber\RedirectResponseSubscriber;
11
use Drupal\Core\Routing\RequestContext;
12 13 14 15 16 17 18 19 20 21
use Drupal\Tests\UnitTestCase;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\KernelEvents;

/**
 * @coversDefaultClass \Drupal\Core\EventSubscriber\RedirectResponseSubscriber
22
 * @group EventSubscriber
23 24 25 26 27 28 29 30
 */
class RedirectResponseSubscriberTest extends UnitTestCase {

  /**
   * Test destination detection and redirection.
   *
   * @param Request $request
   *   The request object with destination query set.
31 32
   * @param string|bool $expected
   *   The expected target URL or FALSE.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
   *
   * @covers ::checkRedirectUrl
   * @dataProvider providerTestDestinationRedirect
   */
  public function testDestinationRedirect(Request $request, $expected) {
    $dispatcher = new EventDispatcher();
    $kernel = $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface');
    $response = new RedirectResponse('http://example.com/drupal');
    $url_generator = $this->getMockBuilder('Drupal\Core\Routing\UrlGenerator')
      ->disableOriginalConstructor()
      ->setMethods(array('generateFromPath'))
      ->getMock();

    if ($expected) {
      $url_generator
48
        ->expects($this->any())
49
        ->method('generateFromPath')
50 51 52
          ->willReturnMap([
            ['test', ['query' => [], 'fragment' => '', 'absolute' => TRUE], 'http://example.com/drupal/test']
          ]);
53 54
    }

55 56 57 58 59 60 61 62
    $request_context = $this->getMockBuilder('Drupal\Core\Routing\RequestContext')
      ->disableOriginalConstructor()
      ->getMock();
    $request_context->expects($this->any())
      ->method('getCompleteBaseUrl')
      ->willReturn('http://example.com/drupal');

    $listener = new RedirectResponseSubscriber($url_generator, $request_context);
63 64 65 66 67 68
    $dispatcher->addListener(KernelEvents::RESPONSE, array($listener, 'checkRedirectUrl'));
    $event = new FilterResponseEvent($kernel, $request, HttpKernelInterface::SUB_REQUEST, $response);
    $dispatcher->dispatch(KernelEvents::RESPONSE, $event);

    $target_url = $event->getResponse()->getTargetUrl();
    if ($expected) {
69
      $this->assertEquals($expected, $target_url);
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
    }
    else {
      $this->assertEquals('http://example.com/drupal', $target_url);
    }
  }

  /**
   * Data provider for testDestinationRedirect().
   *
   * @see \Drupal\Tests\Core\EventSubscriber\RedirectResponseSubscriberTest::testDestinationRedirect()
   */
  public static function providerTestDestinationRedirect() {
    return array(
      array(new Request(), FALSE),
      array(new Request(array('destination' => 'http://example.com')), FALSE),
      array(new Request(array('destination' => 'http://example.com/foobar')), FALSE),
      array(new Request(array('destination' => 'http://example.ca/drupal')), FALSE),
87 88 89
      array(new Request(array('destination' => 'test')), 'http://example.com/drupal/test'),
      array(new Request(array('destination' => 'http://example.com/drupal/')), 'http://example.com/drupal/'),
      array(new Request(array('destination' => 'http://example.com/drupal/test')), 'http://example.com/drupal/test'),
90 91 92
    );
  }
}