Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / modules / contrib / devel / tests / src / Functional / DevelRouteInfoTest.php
1 <?php
2
3 namespace Drupal\Tests\devel\Functional;
4
5 use Drupal\Core\Url;
6 use Drupal\Tests\BrowserTestBase;
7
8 /**
9  * Tests routes info pages and links.
10  *
11  * @group devel
12  */
13 class DevelRouteInfoTest extends BrowserTestBase {
14
15   /**
16    * {@inheritdoc}
17    */
18   public static $modules = ['devel', 'devel_test', 'block'];
19
20   /**
21    * The user for the test.
22    *
23    * @var \Drupal\user\UserInterface
24    */
25   protected $develUser;
26
27   /**
28    * {@inheritdoc}
29    */
30   protected function setUp() {
31     parent::setUp();
32
33     $this->drupalPlaceBlock('system_menu_block:devel');
34     $this->drupalPlaceBlock('page_title_block');
35
36     $this->develUser = $this->drupalCreateUser(['access devel information']);
37     $this->drupalLogin($this->develUser);
38   }
39
40   /**
41    * Tests routes info.
42    */
43   public function testRouteList() {
44     // Ensures that the routes info link is present on the devel menu and that
45     // it points to the correct page.
46     $this->drupalGet('');
47     $this->clickLink('Routes Info');
48     $this->assertSession()->statusCodeEquals(200);
49     $this->assertSession()->addressEquals('/devel/routes');
50     $this->assertSession()->pageTextContains('Routes');
51
52     $page = $this->getSession()->getPage();
53
54     // Ensures that the expected table headers are found.
55     /** @var $headers \Behat\Mink\Element\NodeElement[] */
56     $headers = $page->findAll('css', 'table.devel-route-list thead th');
57     $this->assertEquals(4, count($headers));
58
59     $expected_items = ['Route Name', 'Path', 'Allowed Methods', 'Operations'];
60     foreach ($headers as $key => $element) {
61       $this->assertSame($element->getText(), $expected_items[$key]);
62     }
63
64     // Ensures that all the routes are listed in the table.
65     $routes = \Drupal::service('router.route_provider')->getAllRoutes();
66     $rows = $page->findAll('css', 'table.devel-route-list tbody tr');
67     $this->assertEquals(count($routes), count($rows));
68
69     // Tests the presence of some (arbitrarily chosen) routes in the table.
70     $expected_routes = [
71       '<current>' => [
72         'path' => '/<current>',
73         'methods' => ['GET', 'POST'],
74         'dynamic' => FALSE,
75       ],
76       'user.login' => [
77         'path' => '/user/login',
78         'methods' => ['GET', 'POST'],
79         'dynamic' => FALSE,
80       ],
81       'entity.user.canonical' => [
82         'path' => '/user/{user}',
83         'methods' => ['GET', 'POST'],
84         'dynamic' => TRUE,
85       ],
86       'entity.user.devel_load' => [
87         'path' => '/devel/user/{user}',
88         'methods' => ['ANY'],
89         'dynamic' => TRUE,
90       ],
91     ];
92
93     foreach ($expected_routes as $route_name => $expected) {
94       $row = $page->find('css', sprintf('table.devel-route-list tbody tr:contains("%s")', $route_name));
95       $this->assertNotNull($row);
96
97       /** @var $cells \Behat\Mink\Element\NodeElement[] */
98       $cells = $row->findAll('css', 'td');
99       $this->assertEquals(4, count($cells));
100
101       $cell_route_name = $cells[0];
102       $this->assertEquals($route_name, $cell_route_name->getText());
103       $this->assertTrue($cell_route_name->hasClass('table-filter-text-source'));
104
105       $cell_path = $cells[1];
106       $this->assertEquals($expected['path'], $cell_path->getText());
107       $this->assertTrue($cell_path->hasClass('table-filter-text-source'));
108
109       $cell_methods = $cells[2];
110       $this->assertEquals(implode('', $expected['methods']), $cell_methods->getText());
111
112       $cell_operations = $cells[3];
113       $actual_href = $cell_operations->findLink('Devel')->getAttribute('href');
114       if ($expected['dynamic']) {
115         $parameters = ['query' => ['route_name' => $route_name]];
116       }
117       else {
118         $parameters = ['query' => ['path' => $expected['path']]];
119       }
120       $expected_href = Url::fromRoute('devel.route_info.item', [], $parameters)->toString();
121       $this->assertEquals($expected_href, $actual_href);
122     }
123
124     // Ensures that the page is accessible only to the users with the adequate
125     // permissions.
126     $this->drupalLogout();
127     $this->drupalGet('devel/routes');
128     $this->assertSession()->statusCodeEquals(403);
129   }
130
131   /**
132    * Tests route detail page.
133    */
134   public function testRouteDetail() {
135     $expected_title = 'Route detail';
136     $xpath_warning_messages = '//div[contains(@class, "messages--warning")]';
137
138     // Ensures that devel route detail link in the menu works properly.
139     $url = $this->develUser->toUrl();
140     $path = '/' . $url->getInternalPath();
141
142     $this->drupalGet($url);
143     $this->clickLink('Current route info');
144     $this->assertSession()->statusCodeEquals(200);
145     $this->assertSession()->pageTextContains($expected_title);
146     $expected_url = Url::fromRoute('devel.route_info.item', [], ['query' => ['path' => $path]]);
147     $this->assertSession()->addressEquals($expected_url);
148     $this->assertSession()->elementNotExists('xpath', $xpath_warning_messages);
149
150     // Ensures that devel route detail works properly even when dynamic cache
151     // is enabled.
152     $url = Url::fromRoute('devel.simple_page');
153     $path = '/' . $url->getInternalPath();
154
155     $this->drupalGet($url);
156     $this->clickLink('Current route info');
157     $this->assertSession()->statusCodeEquals(200);
158     $this->assertSession()->pageTextContains($expected_title);
159     $expected_url = Url::fromRoute('devel.route_info.item', [], ['query' => ['path' => $path]]);
160     $this->assertSession()->addressEquals($expected_url);
161     $this->assertSession()->elementNotExists('xpath', $xpath_warning_messages);
162
163     // Ensures that if a non existent path is passed as input, a warning
164     // message is shown.
165     $this->drupalGet('devel/routes/item', ['query' => ['path' => '/undefined']]);
166     $this->assertSession()->statusCodeEquals(200);
167     $this->assertSession()->pageTextContains($expected_title);
168     $this->assertSession()->elementExists('xpath', $xpath_warning_messages);
169
170     // Ensures that the route detail page works properly when a valid route
171     // name input is passed.
172     $this->drupalGet('devel/routes/item', ['query' => ['route_name' => 'devel.simple_page']]);
173     $this->assertSession()->statusCodeEquals(200);
174     $this->assertSession()->pageTextContains($expected_title);
175     $this->assertSession()->elementNotExists('xpath', $xpath_warning_messages);
176
177     // Ensures that if a non existent route name is passed as input a warning
178     // message is shown.
179     $this->drupalGet('devel/routes/item', ['query' => ['route_name' => 'not.exists']]);
180     $this->assertSession()->statusCodeEquals(200);
181     $this->assertSession()->pageTextContains($expected_title);
182     $this->assertSession()->elementExists('xpath', $xpath_warning_messages);
183
184     // Ensures that if no 'path' nor 'name' query string is passed as input,
185     // devel route detail page does not return errors.
186     $this->drupalGet('devel/routes/item');
187     $this->assertSession()->statusCodeEquals(200);
188     $this->assertSession()->pageTextContains($expected_title);
189
190     // Ensures that the page is accessible ony to the users with the adequate
191     // permissions.
192     $this->drupalLogout();
193     $this->drupalGet('devel/routes/item');
194     $this->assertSession()->statusCodeEquals(403);
195   }
196
197 }