You are here

class RoutePreloaderTest in Zircon Profile 8

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

@coversDefaultClass \Drupal\Core\Routing\RoutePreloader @group Routing

Hierarchy

Expanded class hierarchy of RoutePreloaderTest

File

core/tests/Drupal/Tests/Core/Routing/RoutePreloaderTest.php, line 21
Contains \Drupal\Tests\Core\Routing\RoutePreloaderTest.

Namespace

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

  /**
   * The mocked route provider.
   *
   * @var \Drupal\Core\Routing\RouteProviderInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $routeProvider;

  /**
   * The mocked state.
   *
   * @var \Drupal\Core\State\StateInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $state;

  /**
   * The tested preloader.
   *
   * @var \Drupal\Core\Routing\RoutePreloader
   */
  protected $preloader;

  /**
   * The mocked cache.
   *
   * @var \Drupal\Core\Cache\CacheBackendInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $cache;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    $this->routeProvider = $this
      ->getMock('Drupal\\Core\\Routing\\PreloadableRouteProviderInterface');
    $this->state = $this
      ->getMock('\\Drupal\\Core\\State\\StateInterface');
    $this->cache = $this
      ->getMock('Drupal\\Core\\Cache\\CacheBackendInterface');
    $this->preloader = new RoutePreloader($this->routeProvider, $this->state, $this->cache);
  }

  /**
   * Tests onAlterRoutes with just admin routes.
   */
  public function testOnAlterRoutesWithAdminRoutes() {
    $event = $this
      ->getMockBuilder('Drupal\\Core\\Routing\\RouteBuildEvent')
      ->disableOriginalConstructor()
      ->getMock();
    $route_collection = new RouteCollection();
    $route_collection
      ->add('test', new Route('/admin/foo', array(
      '_controller' => 'Drupal\\ExampleController',
    )));
    $route_collection
      ->add('test2', new Route('/admin/bar', array(
      '_controller' => 'Drupal\\ExampleController',
    )));
    $event
      ->expects($this
      ->once())
      ->method('getRouteCollection')
      ->will($this
      ->returnValue($route_collection));
    $this->state
      ->expects($this
      ->once())
      ->method('set')
      ->with('routing.non_admin_routes', array());
    $this->preloader
      ->onAlterRoutes($event);
    $this->preloader
      ->onFinishedRoutes(new Event());
  }

  /**
   * Tests onAlterRoutes with "admin" appearing in the path.
   */
  public function testOnAlterRoutesWithAdminPathNoAdminRoute() {
    $event = $this
      ->getMockBuilder('Drupal\\Core\\Routing\\RouteBuildEvent')
      ->disableOriginalConstructor()
      ->getMock();
    $route_collection = new RouteCollection();
    $route_collection
      ->add('test', new Route('/foo/admin/foo', array(
      '_controller' => 'Drupal\\ExampleController',
    )));
    $route_collection
      ->add('test2', new Route('/bar/admin/bar', array(
      '_controller' => 'Drupal\\ExampleController',
    )));
    $route_collection
      ->add('test3', new Route('/administrator/a', array(
      '_controller' => 'Drupal\\ExampleController',
    )));
    $route_collection
      ->add('test4', new Route('/admin', array(
      '_controller' => 'Drupal\\ExampleController',
    )));
    $event
      ->expects($this
      ->once())
      ->method('getRouteCollection')
      ->will($this
      ->returnValue($route_collection));
    $this->state
      ->expects($this
      ->once())
      ->method('set')
      ->with('routing.non_admin_routes', array(
      'test',
      'test2',
      'test3',
    ));
    $this->preloader
      ->onAlterRoutes($event);
    $this->preloader
      ->onFinishedRoutes(new Event());
  }

  /**
   * Tests onAlterRoutes with admin routes and non admin routes.
   */
  public function testOnAlterRoutesWithNonAdminRoutes() {
    $event = $this
      ->getMockBuilder('Drupal\\Core\\Routing\\RouteBuildEvent')
      ->disableOriginalConstructor()
      ->getMock();
    $route_collection = new RouteCollection();
    $route_collection
      ->add('test', new Route('/admin/foo', array(
      '_controller' => 'Drupal\\ExampleController',
    )));
    $route_collection
      ->add('test2', new Route('/bar', array(
      '_controller' => 'Drupal\\ExampleController',
    )));

    // Non content routes, like ajax callbacks should be ignored.
    $route_collection
      ->add('test3', new Route('/bar', array(
      '_controller' => 'Drupal\\ExampleController',
    )));
    $event
      ->expects($this
      ->once())
      ->method('getRouteCollection')
      ->will($this
      ->returnValue($route_collection));
    $this->state
      ->expects($this
      ->once())
      ->method('set')
      ->with('routing.non_admin_routes', array(
      'test2',
      'test3',
    ));
    $this->preloader
      ->onAlterRoutes($event);
    $this->preloader
      ->onFinishedRoutes(new Event());
  }

  /**
   * Tests onRequest on a non html request.
   */
  public function testOnRequestNonHtml() {
    $event = $this
      ->getMockBuilder('\\Symfony\\Component\\HttpKernel\\Event\\KernelEvent')
      ->disableOriginalConstructor()
      ->getMock();
    $request = new Request();
    $request
      ->setRequestFormat('non-html');
    $event
      ->expects($this
      ->any())
      ->method('getRequest')
      ->will($this
      ->returnValue($request));
    $this->routeProvider
      ->expects($this
      ->never())
      ->method('getRoutesByNames');
    $this->state
      ->expects($this
      ->never())
      ->method('get');
    $this->preloader
      ->onRequest($event);
  }

  /**
   * Tests onRequest on a html request.
   */
  public function testOnRequestOnHtml() {
    $event = $this
      ->getMockBuilder('\\Symfony\\Component\\HttpKernel\\Event\\KernelEvent')
      ->disableOriginalConstructor()
      ->getMock();
    $request = new Request();
    $request
      ->setRequestFormat('html');
    $event
      ->expects($this
      ->any())
      ->method('getRequest')
      ->will($this
      ->returnValue($request));
    $this->routeProvider
      ->expects($this
      ->once())
      ->method('preLoadRoutes')
      ->with([
      'test2',
    ]);
    $this->state
      ->expects($this
      ->once())
      ->method('get')
      ->with('routing.non_admin_routes')
      ->will($this
      ->returnValue(array(
      'test2',
    )));
    $this->preloader
      ->onRequest($event);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RoutePreloaderTest::$cache protected property The mocked cache.
RoutePreloaderTest::$preloader protected property The tested preloader.
RoutePreloaderTest::$routeProvider protected property The mocked route provider.
RoutePreloaderTest::$state protected property The mocked state.
RoutePreloaderTest::setUp protected function Overrides UnitTestCase::setUp
RoutePreloaderTest::testOnAlterRoutesWithAdminPathNoAdminRoute public function Tests onAlterRoutes with "admin" appearing in the path.
RoutePreloaderTest::testOnAlterRoutesWithAdminRoutes public function Tests onAlterRoutes with just admin routes.
RoutePreloaderTest::testOnAlterRoutesWithNonAdminRoutes public function Tests onAlterRoutes with admin routes and non admin routes.
RoutePreloaderTest::testOnRequestNonHtml public function Tests onRequest on a non html request.
RoutePreloaderTest::testOnRequestOnHtml public function Tests onRequest on a html request.
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.