3 namespace Drupal\Tests\Core\Routing;
5 use Symfony\Component\Routing\Route;
6 use Symfony\Component\Routing\RouteCollection;
8 use Drupal\Core\Database\Connection;
11 * Utility methods to generate sample data, database configuration, etc.
13 class RoutingFixtures {
16 * Create the tables required for the sample data.
18 * @param \Drupal\Core\Database\Connection $connection
19 * The connection to use to create the tables.
21 public function createTables(Connection $connection) {
22 $tables = $this->routingTableDefinition();
23 $schema = $connection->schema();
25 foreach ($tables as $name => $table) {
26 $schema->dropTable($name);
27 $schema->createTable($name, $table);
32 * Drop the tables used for the sample data.
34 * @param \Drupal\Core\Database\Connection $connection
35 * The connection to use to drop the tables.
37 public function dropTables(Connection $connection) {
38 $tables = $this->routingTableDefinition();
39 $schema = $connection->schema();
41 foreach ($tables as $name => $table) {
42 $schema->dropTable($name);
47 * Returns a static version of the routes.
49 public function staticSampleRouteCollection() {
51 $routes['route_a'] = [
52 'path' => '/path/one',
55 $routes['route_b'] = [
56 'path' => '/path/one',
59 $routes['route_c'] = [
60 'path' => '/path/two',
66 $routes['route_d'] = [
67 'path' => '/path/three',
69 $routes['route_e'] = [
70 'path' => '/path/two',
71 'methods' => ['GET', 'HEAD'],
81 * Returns a standard set of routes for testing.
83 * @return \Symfony\Component\Routing\RouteCollection
85 public function sampleRouteCollection() {
86 $collection = new RouteCollection();
88 $route = new Route('path/one');
89 $route->setMethods(['GET']);
90 $collection->add('route_a', $route);
92 $route = new Route('path/one');
93 $route->setMethods(['PUT']);
94 $collection->add('route_b', $route);
96 $route = new Route('path/two');
97 $route->setMethods(['GET']);
98 $route->setRequirement('_format', 'json');
99 $collection->add('route_c', $route);
101 $route = new Route('path/three');
102 $collection->add('route_d', $route);
104 $route = new Route('path/two');
105 $route->setMethods(['GET', 'HEAD']);
106 $route->setRequirement('_format', 'html');
107 $collection->add('route_e', $route);
113 * Returns a complex set of routes for testing.
115 * @return \Symfony\Component\Routing\RouteCollection
117 public function complexRouteCollection() {
118 $collection = new RouteCollection();
120 $route = new Route('/path/{thing}/one');
121 $route->setMethods(['GET']);
122 $collection->add('route_a', $route);
124 $route = new Route('/path/{thing}/one');
125 $route->setMethods(['PUT']);
126 $collection->add('route_b', $route);
128 $route = new Route('/somewhere/{item}/over/the/rainbow');
129 $route->setMethods(['GET']);
130 $collection->add('route_c', $route);
132 $route = new Route('/another/{thing}/about/{item}');
133 $collection->add('route_d', $route);
135 $route = new Route('/path/add/one');
136 $route->setMethods(['GET', 'HEAD']);
137 $collection->add('route_e', $route);
143 * Returns a complex set of routes for testing.
145 * @return \Symfony\Component\Routing\RouteCollection
147 public function mixedCaseRouteCollection() {
148 $collection = new RouteCollection();
150 $route = new Route('/path/one');
151 $route->setMethods(['GET']);
152 $collection->add('route_a', $route);
154 $route = new Route('/path/{thing}/one');
155 $route->setMethods(['PUT']);
156 $collection->add('route_b', $route);
158 // Uses Hebrew letter QOF (U+05E7)
159 $route = new Route('/somewhere/{item}/over/the/קainbow');
160 $route->setMethods(['GET']);
161 $collection->add('route_c', $route);
163 $route = new Route('/another/{thing}/aboUT/{item}');
164 $collection->add('route_d', $route);
166 // Greek letters lower case phi (U+03C6) and lower case omega (U+03C9)
167 $route = new Route('/place/meφω');
168 $route->setMethods(['GET', 'HEAD']);
169 $collection->add('route_e', $route);
175 * Returns a complex set of routes for testing.
177 * @return \Symfony\Component\Routing\RouteCollection
179 public function duplicatePathsRouteCollection() {
180 $collection = new RouteCollection();
182 $route = new Route('/path/one');
183 $route->setMethods(['GET']);
184 $collection->add('route_b', $route);
186 // Add the routes not in order by route name.
187 $route = new Route('/path/one');
188 $route->setMethods(['GET']);
189 $collection->add('route_a', $route);
191 $route = new Route('/path/one');
192 $route->setMethods(['GET']);
193 $collection->add('route_c', $route);
195 $route = new Route('/path/TWO');
196 $route->setMethods(['GET']);
197 $collection->add('route_d', $route);
199 // Greek letters lower case phi (U+03C6) and lower case omega (U+03C9)
200 $route = new Route('/place/meφω');
201 $route->setMethods(['GET', 'HEAD']);
202 $collection->add('route_f', $route);
204 $route = new Route('/PLACE/meφω');
205 $collection->add('route_e', $route);
211 * Returns a Content-type restricted set of routes for testing.
213 * @return \Symfony\Component\Routing\RouteCollection
215 public function contentRouteCollection() {
216 $collection = new RouteCollection();
218 $route = new Route('path/three');
219 $route->setMethods(['POST']);
220 $route->setRequirement('_content_type_format', 'json');
221 $collection->add('route_f', $route);
223 $route = new Route('path/three');
224 $route->setMethods(['PATCH']);
225 $route->setRequirement('_content_type_format', 'xml');
226 $collection->add('route_g', $route);
231 * Returns the table definition for the routing fixtures.
236 public function routingTableDefinition() {
238 $tables['test_routes'] = [
239 'description' => 'Maps paths to various callbacks (access, page and title)',
242 'description' => 'Primary Key: Machine name of this route',
243 'type' => 'varchar_ascii',
249 'description' => 'The path for this URI',
255 'pattern_outline' => [
256 'description' => 'The pattern',
263 'description' => 'The provider grouping to which a route belongs.',
269 'access_callback' => [
270 'description' => 'The callback which determines the access to this router path. Defaults to \Drupal\Core\Session\AccountInterface::hasPermission.',
276 'access_arguments' => [
277 'description' => 'A serialized array of arguments for the access callback.',
282 'description' => 'A numeric representation of how specific the path is.',
288 'description' => 'Number of parts in this router path.',
295 'description' => 'A serialized Route object',
301 'pattern_outline' => ['pattern_outline'],
302 'provider' => ['provider'],
304 'primary key' => ['name'],