3 namespace Drupal\KernelTests\Core\Routing;
5 use Drupal\Core\Cache\MemoryBackend;
6 use Drupal\Core\KeyValueStore\KeyValueMemoryFactory;
7 use Drupal\Core\Lock\NullLockBackend;
8 use Drupal\Core\State\State;
9 use Drupal\KernelTests\KernelTestBase;
10 use Symfony\Component\Routing\Route;
11 use Symfony\Component\Routing\RouteCollection;
12 use Drupal\Core\Database\Database;
13 use Drupal\Core\Routing\MatcherDumper;
14 use Drupal\Tests\Core\Routing\RoutingFixtures;
17 * Confirm that the matcher dumper is functioning properly.
21 class MatcherDumperTest extends KernelTestBase {
24 * A collection of shared fixture data for tests.
26 * @var \Drupal\Tests\Core\Routing\RoutingFixtures
33 * @var \Drupal\Core\State\StateInterface
37 protected function setUp() {
40 $this->fixtures = new RoutingFixtures();
41 $this->state = new State(new KeyValueMemoryFactory(), new MemoryBackend('test'), new NullLockBackend());
45 * Confirms that the dumper can be instantiated successfully.
47 public function testCreate() {
48 $connection = Database::getConnection();
49 $dumper = new MatcherDumper($connection, $this->state);
51 $class_name = 'Drupal\Core\Routing\MatcherDumper';
52 $this->assertTrue($dumper instanceof $class_name, 'Dumper created successfully');
56 * Confirms that we can add routes to the dumper.
58 public function testAddRoutes() {
59 $connection = Database::getConnection();
60 $dumper = new MatcherDumper($connection, $this->state);
62 $route = new Route('test');
63 $collection = new RouteCollection();
64 $collection->add('test_route', $route);
66 $dumper->addRoutes($collection);
68 $dumper_routes = $dumper->getRoutes()->all();
69 $collection_routes = $collection->all();
71 foreach ($dumper_routes as $name => $route) {
72 $this->assertEqual($route->getPath(), $collection_routes[$name]->getPath(), 'Routes match');
77 * Confirms that we can add routes to the dumper when it already has some.
79 public function testAddAdditionalRoutes() {
80 $connection = Database::getConnection();
81 $dumper = new MatcherDumper($connection, $this->state);
83 $route = new Route('test');
84 $collection = new RouteCollection();
85 $collection->add('test_route', $route);
86 $dumper->addRoutes($collection);
88 $route = new Route('test2');
89 $collection2 = new RouteCollection();
90 $collection2->add('test_route2', $route);
91 $dumper->addRoutes($collection2);
93 // Merge the two collections together so we can test them.
94 $collection->addCollection(clone $collection2);
96 $dumper_routes = $dumper->getRoutes()->all();
97 $collection_routes = $collection->all();
100 foreach ($collection_routes as $name => $route) {
101 if (empty($dumper_routes[$name])) {
103 $this->fail(t('Not all routes found in the dumper.'));
108 $this->pass('All routes found in the dumper.');
113 * Confirm that we can dump a route collection to the database.
115 public function testDump() {
116 $connection = Database::getConnection();
117 $dumper = new MatcherDumper($connection, $this->state, 'test_routes');
119 $route = new Route('/test/{my}/path');
120 $route->setOption('compiler_class', 'Drupal\Core\Routing\RouteCompiler');
121 $collection = new RouteCollection();
122 $collection->add('test_route', $route);
124 $dumper->addRoutes($collection);
126 $this->fixtures->createTables($connection);
128 $dumper->dump(['provider' => 'test']);
130 $record = $connection->query("SELECT * FROM {test_routes} WHERE name= :name", [':name' => 'test_route'])->fetchObject();
132 $loaded_route = unserialize($record->route);
134 $this->assertEqual($record->name, 'test_route', 'Dumped route has correct name.');
135 $this->assertEqual($record->path, '/test/{my}/path', 'Dumped route has correct pattern.');
136 $this->assertEqual($record->pattern_outline, '/test/%/path', 'Dumped route has correct pattern outline.');
137 $this->assertEqual($record->fit, 5 /* 101 in binary */, 'Dumped route has correct fit.');
138 $this->assertTrue($loaded_route instanceof Route, 'Route object retrieved successfully.');
142 * Tests the determination of the masks generation.
144 public function testMenuMasksGeneration() {
145 $connection = Database::getConnection();
146 $dumper = new MatcherDumper($connection, $this->state, 'test_routes');
148 $collection = new RouteCollection();
149 $collection->add('test_route_1', new Route('/test-length-3/{my}/path'));
150 $collection->add('test_route_2', new Route('/test-length-3/hello/path'));
151 $collection->add('test_route_3', new Route('/test-length-5/{my}/path/marvin/magrathea'));
152 $collection->add('test_route_4', new Route('/test-length-7/{my}/path/marvin/magrathea/earth/ursa-minor'));
154 $dumper->addRoutes($collection);
156 $this->fixtures->createTables($connection);
158 $dumper->dump(['provider' => 'test']);
159 // Using binary for readability, we expect a 0 at any wildcard slug. They
160 // should be ordered from longest to shortest.
167 $this->assertEqual($this->state->get('routing.menu_masks.test_routes'), $expected);