f37383d631df3463fe0fb76a51b88be3cbd84f72
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Routing / MatcherDumperTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Routing;
4
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;
15
16 /**
17  * Confirm that the matcher dumper is functioning properly.
18  *
19  * @group Routing
20  */
21 class MatcherDumperTest extends KernelTestBase {
22
23   /**
24    * A collection of shared fixture data for tests.
25    *
26    * @var \Drupal\Tests\Core\Routing\RoutingFixtures
27    */
28   protected $fixtures;
29
30   /**
31    * The state.
32    *
33    * @var \Drupal\Core\State\StateInterface
34    */
35   protected $state;
36
37   protected function setUp() {
38     parent::setUp();
39
40     $this->fixtures = new RoutingFixtures();
41     $this->state = new State(new KeyValueMemoryFactory(), new MemoryBackend('test'), new NullLockBackend());
42   }
43
44   /**
45    * Confirms that the dumper can be instantiated successfully.
46    */
47   public function testCreate() {
48     $connection = Database::getConnection();
49     $dumper = new MatcherDumper($connection, $this->state);
50
51     $class_name = 'Drupal\Core\Routing\MatcherDumper';
52     $this->assertTrue($dumper instanceof $class_name, 'Dumper created successfully');
53   }
54
55   /**
56    * Confirms that we can add routes to the dumper.
57    */
58   public function testAddRoutes() {
59     $connection = Database::getConnection();
60     $dumper = new MatcherDumper($connection, $this->state);
61
62     $route = new Route('test');
63     $collection = new RouteCollection();
64     $collection->add('test_route', $route);
65
66     $dumper->addRoutes($collection);
67
68     $dumper_routes = $dumper->getRoutes()->all();
69     $collection_routes = $collection->all();
70
71     foreach ($dumper_routes as $name => $route) {
72       $this->assertEqual($route->getPath(), $collection_routes[$name]->getPath(), 'Routes match');
73     }
74   }
75
76   /**
77    * Confirms that we can add routes to the dumper when it already has some.
78    */
79   public function testAddAdditionalRoutes() {
80     $connection = Database::getConnection();
81     $dumper = new MatcherDumper($connection, $this->state);
82
83     $route = new Route('test');
84     $collection = new RouteCollection();
85     $collection->add('test_route', $route);
86     $dumper->addRoutes($collection);
87
88     $route = new Route('test2');
89     $collection2 = new RouteCollection();
90     $collection2->add('test_route2', $route);
91     $dumper->addRoutes($collection2);
92
93     // Merge the two collections together so we can test them.
94     $collection->addCollection(clone $collection2);
95
96     $dumper_routes = $dumper->getRoutes()->all();
97     $collection_routes = $collection->all();
98
99     $success = TRUE;
100     foreach ($collection_routes as $name => $route) {
101       if (empty($dumper_routes[$name])) {
102         $success = FALSE;
103         $this->fail(t('Not all routes found in the dumper.'));
104       }
105     }
106
107     if ($success) {
108       $this->pass('All routes found in the dumper.');
109     }
110   }
111
112   /**
113    * Confirm that we can dump a route collection to the database.
114    */
115   public function testDump() {
116     $connection = Database::getConnection();
117     $dumper = new MatcherDumper($connection, $this->state, 'test_routes');
118
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);
123
124     $dumper->addRoutes($collection);
125
126     $this->fixtures->createTables($connection);
127
128     $dumper->dump(['provider' => 'test']);
129
130     $record = $connection->query("SELECT * FROM {test_routes} WHERE name= :name", [':name' => 'test_route'])->fetchObject();
131
132     $loaded_route = unserialize($record->route);
133
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.');
139   }
140
141   /**
142    * Tests the determination of the masks generation.
143    */
144   public function testMenuMasksGeneration() {
145     $connection = Database::getConnection();
146     $dumper = new MatcherDumper($connection, $this->state, 'test_routes');
147
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'));
153
154     $dumper->addRoutes($collection);
155
156     $this->fixtures->createTables($connection);
157
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.
161     $expected = [
162       bindec('1011111'),
163       bindec('10111'),
164       bindec('111'),
165       bindec('101'),
166     ];
167     $this->assertEqual($this->state->get('routing.menu_masks.test_routes'), $expected);
168   }
169
170 }