Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Routing / RoutingFixtures.php
1 <?php
2
3 namespace Drupal\Tests\Core\Routing;
4
5 use Symfony\Component\Routing\Route;
6 use Symfony\Component\Routing\RouteCollection;
7
8 use Drupal\Core\Database\Connection;
9
10 /**
11  * Utility methods to generate sample data, database configuration, etc.
12  */
13 class RoutingFixtures {
14
15   /**
16    * Create the tables required for the sample data.
17    *
18    * @param \Drupal\Core\Database\Connection $connection
19    *   The connection to use to create the tables.
20    */
21   public function createTables(Connection $connection) {
22     $tables = $this->routingTableDefinition();
23     $schema = $connection->schema();
24
25     foreach ($tables as $name => $table) {
26       $schema->dropTable($name);
27       $schema->createTable($name, $table);
28     }
29   }
30
31   /**
32    * Drop the tables used for the sample data.
33    *
34    * @param \Drupal\Core\Database\Connection $connection
35    *   The connection to use to drop the tables.
36    */
37   public function dropTables(Connection $connection) {
38     $tables = $this->routingTableDefinition();
39     $schema = $connection->schema();
40
41     foreach ($tables as $name => $table) {
42       $schema->dropTable($name);
43     }
44   }
45
46   /**
47    * Returns a static version of the routes.
48    */
49   public function staticSampleRouteCollection() {
50     $routes = [];
51     $routes['route_a'] = [
52       'path' => '/path/one',
53       'methods' => ['GET'],
54     ];
55     $routes['route_b'] = [
56       'path' => '/path/one',
57       'methods' => ['PUT'],
58     ];
59     $routes['route_c'] = [
60       'path' => '/path/two',
61       'methods' => ['GET'],
62       'requirements' => [
63         '_format' => 'json'
64       ],
65     ];
66     $routes['route_d'] = [
67       'path' => '/path/three',
68     ];
69     $routes['route_e'] = [
70       'path' => '/path/two',
71       'methods' => ['GET', 'HEAD'],
72       'requirements' => [
73         '_format' => 'html'
74       ],
75     ];
76
77     return $routes;
78   }
79
80   /**
81    * Returns a standard set of routes for testing.
82    *
83    * @return \Symfony\Component\Routing\RouteCollection
84    */
85   public function sampleRouteCollection() {
86     $collection = new RouteCollection();
87
88     $route = new Route('path/one');
89     $route->setMethods(['GET']);
90     $collection->add('route_a', $route);
91
92     $route = new Route('path/one');
93     $route->setMethods(['PUT']);
94     $collection->add('route_b', $route);
95
96     $route = new Route('path/two');
97     $route->setMethods(['GET']);
98     $route->setRequirement('_format', 'json');
99     $collection->add('route_c', $route);
100
101     $route = new Route('path/three');
102     $collection->add('route_d', $route);
103
104     $route = new Route('path/two');
105     $route->setMethods(['GET', 'HEAD']);
106     $route->setRequirement('_format', 'html');
107     $collection->add('route_e', $route);
108
109     return $collection;
110   }
111
112   /**
113    * Returns a complex set of routes for testing.
114    *
115    * @return \Symfony\Component\Routing\RouteCollection
116    */
117   public function complexRouteCollection() {
118     $collection = new RouteCollection();
119
120     $route = new Route('/path/{thing}/one');
121     $route->setMethods(['GET']);
122     $collection->add('route_a', $route);
123
124     $route = new Route('/path/{thing}/one');
125     $route->setMethods(['PUT']);
126     $collection->add('route_b', $route);
127
128     $route = new Route('/somewhere/{item}/over/the/rainbow');
129     $route->setMethods(['GET']);
130     $collection->add('route_c', $route);
131
132     $route = new Route('/another/{thing}/about/{item}');
133     $collection->add('route_d', $route);
134
135     $route = new Route('/path/add/one');
136     $route->setMethods(['GET', 'HEAD']);
137     $collection->add('route_e', $route);
138
139     return $collection;
140   }
141
142   /**
143    * Returns a complex set of routes for testing.
144    *
145    * @return \Symfony\Component\Routing\RouteCollection
146    */
147   public function mixedCaseRouteCollection() {
148     $collection = new RouteCollection();
149
150     $route = new Route('/path/one');
151     $route->setMethods(['GET']);
152     $collection->add('route_a', $route);
153
154     $route = new Route('/path/{thing}/one');
155     $route->setMethods(['PUT']);
156     $collection->add('route_b', $route);
157
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);
162
163     $route = new Route('/another/{thing}/aboUT/{item}');
164     $collection->add('route_d', $route);
165
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);
170
171     return $collection;
172   }
173
174   /**
175    * Returns a complex set of routes for testing.
176    *
177    * @return \Symfony\Component\Routing\RouteCollection
178    */
179   public function duplicatePathsRouteCollection() {
180     $collection = new RouteCollection();
181
182     $route = new Route('/path/one');
183     $route->setMethods(['GET']);
184     $collection->add('route_b', $route);
185
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);
190
191     $route = new Route('/path/one');
192     $route->setMethods(['GET']);
193     $collection->add('route_c', $route);
194
195     $route = new Route('/path/TWO');
196     $route->setMethods(['GET']);
197     $collection->add('route_d', $route);
198
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);
203
204     $route = new Route('/PLACE/meφω');
205     $collection->add('route_e', $route);
206
207     return $collection;
208   }
209
210   /**
211    * Returns a Content-type restricted set of routes for testing.
212    *
213    * @return \Symfony\Component\Routing\RouteCollection
214    */
215   public function contentRouteCollection() {
216     $collection = new RouteCollection();
217
218     $route = new Route('path/three');
219     $route->setMethods(['POST']);
220     $route->setRequirement('_content_type_format', 'json');
221     $collection->add('route_f', $route);
222
223     $route = new Route('path/three');
224     $route->setMethods(['PATCH']);
225     $route->setRequirement('_content_type_format', 'xml');
226     $collection->add('route_g', $route);
227     return $collection;
228   }
229
230   /**
231    * Returns the table definition for the routing fixtures.
232    *
233    * @return array
234    *   Table definitions.
235    */
236   public function routingTableDefinition() {
237
238     $tables['test_routes'] = [
239       'description' => 'Maps paths to various callbacks (access, page and title)',
240       'fields' => [
241         'name' => [
242           'description' => 'Primary Key: Machine name of this route',
243           'type' => 'varchar_ascii',
244           'length' => 255,
245           'not null' => TRUE,
246           'default' => '',
247         ],
248         'path' => [
249           'description' => 'The path for this URI',
250           'type' => 'varchar',
251           'length' => 255,
252           'not null' => TRUE,
253           'default' => '',
254         ],
255         'pattern_outline' => [
256           'description' => 'The pattern',
257           'type' => 'varchar',
258           'length' => 255,
259           'not null' => TRUE,
260           'default' => '',
261         ],
262         'provider' => [
263           'description' => 'The provider grouping to which a route belongs.',
264           'type' => 'varchar',
265           'length' => 255,
266           'not null' => TRUE,
267           'default' => '',
268         ],
269         'access_callback' => [
270           'description' => 'The callback which determines the access to this router path. Defaults to \Drupal\Core\Session\AccountInterface::hasPermission.',
271           'type' => 'varchar',
272           'length' => 255,
273           'not null' => TRUE,
274           'default' => '',
275         ],
276         'access_arguments' => [
277           'description' => 'A serialized array of arguments for the access callback.',
278           'type' => 'blob',
279           'not null' => FALSE,
280         ],
281         'fit' => [
282           'description' => 'A numeric representation of how specific the path is.',
283           'type' => 'int',
284           'not null' => TRUE,
285           'default' => 0,
286         ],
287         'number_parts' => [
288           'description' => 'Number of parts in this router path.',
289           'type' => 'int',
290           'not null' => TRUE,
291           'default' => 0,
292           'size' => 'small',
293         ],
294         'route' => [
295           'description' => 'A serialized Route object',
296           'type' => 'text',
297         ],
298       ],
299       'indexes' => [
300         'fit' => ['fit'],
301         'pattern_outline' => ['pattern_outline'],
302         'provider' => ['provider'],
303       ],
304       'primary key' => ['name'],
305     ];
306
307     return $tables;
308   }
309
310 }