f42bf20f80d5071a514ddbb6b06980e32b70e1fa
[yaffs-website] / web / core / modules / contextual / tests / src / Kernel / ContextualUnitTest.php
1 <?php
2
3 namespace Drupal\Tests\contextual\Kernel;
4
5 use Drupal\KernelTests\KernelTestBase;
6
7 /**
8  * Tests all edge cases of converting from #contextual_links to ids and vice
9  * versa.
10  *
11  * @group contextual
12  */
13 class ContextualUnitTest extends KernelTestBase {
14
15   /**
16    * Modules to enable.
17    *
18    * @var array
19    */
20   public static $modules = ['contextual'];
21
22   /**
23    * Provides testcases for testContextualLinksToId() and
24    */
25   public function _contextual_links_id_testcases() {
26     // Test branch conditions:
27     // - one group.
28     // - one dynamic path argument.
29     // - no metadata.
30     $tests[] = [
31       'links' => [
32         'node' => [
33           'route_parameters' => [
34             'node' => '14031991',
35           ],
36           'metadata' => ['langcode' => 'en'],
37         ],
38       ],
39       'id' => 'node:node=14031991:langcode=en',
40     ];
41
42     // Test branch conditions:
43     // - one group.
44     // - multiple dynamic path arguments.
45     // - no metadata.
46     $tests[] = [
47       'links' => [
48         'foo' => [
49           'route_parameters' => [
50             'bar',
51             'key' => 'baz',
52             'qux',
53           ],
54           'metadata' => ['langcode' => 'en'],
55         ],
56       ],
57       'id' => 'foo:0=bar&key=baz&1=qux:langcode=en',
58     ];
59
60     // Test branch conditions:
61     // - one group.
62     // - one dynamic path argument.
63     // - metadata.
64     $tests[] = [
65       'links' => [
66         'views_ui_edit' => [
67           'route_parameters' => [
68             'view' => 'frontpage'
69           ],
70           'metadata' => [
71             'location' => 'page',
72             'display' => 'page_1',
73             'langcode' => 'en',
74           ],
75         ],
76       ],
77       'id' => 'views_ui_edit:view=frontpage:location=page&display=page_1&langcode=en',
78     ];
79
80     // Test branch conditions:
81     // - multiple groups.
82     // - multiple dynamic path arguments.
83     $tests[] = [
84       'links' => [
85         'node' => [
86           'route_parameters' => [
87             'node' => '14031991',
88           ],
89           'metadata' => ['langcode' => 'en'],
90         ],
91         'foo' => [
92           'route_parameters' => [
93             'bar',
94             'key' => 'baz',
95             'qux',
96           ],
97           'metadata' => ['langcode' => 'en'],
98         ],
99         'edge' => [
100           'route_parameters' => ['20011988'],
101           'metadata' => ['langcode' => 'en'],
102         ],
103       ],
104       'id' => 'node:node=14031991:langcode=en|foo:0=bar&key=baz&1=qux:langcode=en|edge:0=20011988:langcode=en',
105     ];
106
107     return $tests;
108   }
109
110   /**
111    * Tests _contextual_links_to_id().
112    */
113   public function testContextualLinksToId() {
114     $tests = $this->_contextual_links_id_testcases();
115     foreach ($tests as $test) {
116       $this->assertIdentical(_contextual_links_to_id($test['links']), $test['id']);
117     }
118   }
119
120   /**
121    * Tests _contextual_id_to_links().
122    */
123   public function testContextualIdToLinks() {
124     $tests = $this->_contextual_links_id_testcases();
125     foreach ($tests as $test) {
126       $this->assertIdentical(_contextual_id_to_links($test['id']), $test['links']);
127     }
128   }
129
130 }