Upgraded drupal core with security updates
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Path / PathMatcherTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Path;
4
5 use Drupal\Core\Path\PathMatcher;
6 use Drupal\Tests\UnitTestCase;
7
8 /**
9  * @coversDefaultClass \Drupal\Core\Path\PathMatcher
10  * @group Path
11  */
12 class PathMatcherTest extends UnitTestCase {
13
14   /**
15    * The path matcher under test.
16    *
17    * @var \Drupal\Core\Path\PathMatcher
18    */
19   protected $pathMatcher;
20
21   /**
22    * {@inheritdoc}
23    */
24   protected function setUp() {
25     // Create a stub config factory with all config settings that will be
26     // checked during this test.
27     $config_factory_stub = $this->getConfigFactoryStub(
28       [
29         'system.site' => [
30           'page.front' => '/dummy',
31         ],
32       ]
33     );
34     $route_match = $this->getMock('Drupal\Core\Routing\RouteMatchInterface');
35     $this->pathMatcher = new PathMatcher($config_factory_stub, $route_match);
36   }
37
38   /**
39    * Test that standard paths works with multiple patterns.
40    *
41    * @dataProvider getMatchPathData
42    */
43   public function testMatchPath($patterns, $paths) {
44     foreach ($paths as $path => $expected_result) {
45       $actual_result = $this->pathMatcher->matchPath($path, $patterns);
46       $this->assertEquals($actual_result, $expected_result, "Tried matching the path '$path' to the pattern '$patterns'.");
47     }
48   }
49
50   /**
51    * Provides test path data.
52    *
53    * @return array
54    *   A nested array of pattern arrays and path arrays.
55    */
56   public function getMatchPathData() {
57     return [
58       [
59         // Single absolute paths.
60         '/example/1',
61         [
62           '/example/1' => TRUE,
63           '/example/2' => FALSE,
64           '/test' => FALSE,
65         ],
66       ],
67       [
68         // Single paths with wildcards.
69         '/example/*',
70         [
71           '/example/1' => TRUE,
72           '/example/2' => TRUE,
73           '/example/3/edit' => TRUE,
74           '/example/' => TRUE,
75           '/example' => FALSE,
76           '/test' => FALSE,
77         ],
78       ],
79       [
80         // Single paths with multiple wildcards.
81         '/node/*/revisions/*',
82         [
83           '/node/1/revisions/3' => TRUE,
84           '/node/345/revisions/test' => TRUE,
85           '/node/23/edit' => FALSE,
86           '/test' => FALSE,
87         ],
88       ],
89       [
90         // Single paths with '<front>'.
91         "<front>",
92         [
93           '/dummy' => TRUE,
94           "/dummy/" => FALSE,
95           "/dummy/edit" => FALSE,
96           '/node' => FALSE,
97           '' => FALSE,
98         ],
99       ],
100       [
101         // Paths with both '<front>' and wildcards (should not work).
102         "<front>/*",
103         [
104           '/dummy' => FALSE,
105           '/dummy/' => FALSE,
106           '/dummy/edit' => FALSE,
107           '/node/12' => FALSE,
108           '/' => FALSE,
109         ],
110       ],
111       [
112         // Multiple paths with the \n delimiter.
113         "/node/*\n/node/*/edit",
114         [
115           '/node/1' => TRUE,
116           '/node/view' => TRUE,
117           '/node/32/edit' => TRUE,
118           '/node/delete/edit' => TRUE,
119           '/node/50/delete' => TRUE,
120           '/test/example' => FALSE,
121         ],
122       ],
123       [
124         // Multiple paths with the \r delimiter.
125         "/user/*\r/example/*",
126         [
127           '/user/1' => TRUE,
128           '/example/1' => TRUE,
129           '/user/1/example/1' => TRUE,
130           '/user/example' => TRUE,
131           '/test/example' => FALSE,
132           '/user' => FALSE,
133           '/example' => FALSE,
134         ],
135       ],
136       [
137         // Multiple paths with the \r\n delimiter.
138         "/test\r\n<front>",
139         [
140           '/test' => TRUE,
141           '/dummy' => TRUE,
142           '/example' => FALSE,
143         ],
144       ],
145       [
146         // Test existing regular expressions (should be escaped).
147         '[^/]+?/[0-9]',
148         [
149           '/test/1' => FALSE,
150           '[^/]+?/[0-9]' => TRUE,
151         ],
152       ],
153     ];
154   }
155
156 }