View source
<?php
namespace Drupal\Tests\Core\Routing;
use Drupal\Core\Routing\RoutePreloader;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
class RoutePreloaderTest extends UnitTestCase {
protected $routeProvider;
protected $state;
protected $preloader;
protected $cache;
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);
}
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());
}
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());
}
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',
)));
$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());
}
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);
}
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);
}
}