Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / modules / contrib / devel / tests / src / Functional / DevelContainerInfoTest.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 container info pages and links.
10  *
11  * @group devel
12  */
13 class DevelContainerInfoTest extends BrowserTestBase {
14
15   use DevelWebAssertHelper;
16
17   /**
18    * {@inheritdoc}
19    */
20   public static $modules = ['devel', 'devel_test', 'block'];
21
22   /**
23    * The user for tests.
24    *
25    * @var \Drupal\user\UserInterface
26    */
27   protected $develUser;
28
29   /**
30    * {@inheritdoc}
31    */
32   protected function setUp() {
33     parent::setUp();
34
35     $this->drupalPlaceBlock('local_tasks_block');
36     $this->drupalPlaceBlock('page_title_block');
37
38     $this->develUser = $this->drupalCreateUser(['access devel information']);
39     $this->drupalLogin($this->develUser);
40   }
41
42   /**
43    * Tests container info menu link.
44    */
45   public function testContainerInfoMenuLink() {
46     $this->drupalPlaceBlock('system_menu_block:devel');
47     // Ensures that the events info link is present on the devel menu and that
48     // it points to the correct page.
49     $this->drupalGet('');
50     $this->clickLink('Container Info');
51     $this->assertSession()->statusCodeEquals(200);
52     $this->assertSession()->addressEquals('/devel/container/service');
53     $this->assertSession()->pageTextContains('Container services');
54   }
55
56   /**
57    * Tests service list page.
58    */
59   public function testServiceList() {
60     $this->drupalGet('/devel/container/service');
61     $this->assertSession()->statusCodeEquals(200);
62     $this->assertSession()->pageTextContains('Container services');
63     $this->assertContainerInfoLocalTasks();
64
65     $page = $this->getSession()->getPage();
66
67     // Ensures that the services table is found.
68     $table = $page->find('css', 'table.devel-service-list');
69     $this->assertNotNull($table);
70
71     // Ensures that the expected table headers are found.
72     /** @var $headers \Behat\Mink\Element\NodeElement[] */
73     $headers = $table->findAll('css', 'thead th');
74     $this->assertEquals(4, count($headers));
75
76     $expected_headers = ['ID', 'Class', 'Alias', 'Operations'];
77     $actual_headers = array_map(function ($element) {
78       return $element->getText();
79     }, $headers);
80     $this->assertSame($expected_headers, $actual_headers);
81
82     // Ensures that all the serivices are listed in the table.
83     $cached_definition = \Drupal::service('kernel')->getCachedContainerDefinition();
84     $this->assertNotNull($cached_definition);
85     $rows = $table->findAll('css', 'tbody tr');
86     $this->assertEquals(count($cached_definition['services']), count($rows));
87
88     // Tests the presence of some (arbitrarily chosen) services in the table.
89     $expected_services = [
90       'config.factory' => [
91         'class' => 'Drupal\Core\Config\ConfigFactory',
92         'alias' => '',
93       ],
94       'devel.route_subscriber' => [
95         'class' => 'Drupal\devel\Routing\RouteSubscriber',
96         'alias' => '',
97       ],
98       'plugin.manager.element_info' => [
99         'class' => 'Drupal\Core\Render\ElementInfoManager',
100         'alias' => 'element_info',
101       ],
102     ];
103
104     foreach ($expected_services as $service_id => $expected) {
105       $row = $table->find('css', sprintf('tbody tr:contains("%s")', $service_id));
106       $this->assertNotNull($row);
107
108       /** @var $cells \Behat\Mink\Element\NodeElement[] */
109       $cells = $row->findAll('css', 'td');
110       $this->assertEquals(4, count($cells));
111
112       $cell_service_id = $cells[0];
113       $this->assertEquals($service_id, $cell_service_id->getText());
114       $this->assertTrue($cell_service_id->hasClass('table-filter-text-source'));
115
116       $cell_class = $cells[1];
117       $this->assertEquals($expected['class'], $cell_class->getText());
118       $this->assertTrue($cell_class->hasClass('table-filter-text-source'));
119
120       $cell_alias = $cells[2];
121       $this->assertEquals($expected['alias'], $cell_alias->getText());
122       $this->assertTrue($cell_class->hasClass('table-filter-text-source'));
123
124       $cell_operations = $cells[3];
125       $actual_href = $cell_operations->findLink('Devel')->getAttribute('href');
126       $expected_href = Url::fromRoute('devel.container_info.service.detail', ['service_id' => $service_id])->toString();
127       $this->assertEquals($expected_href, $actual_href);
128     }
129
130     // Ensures that the page is accessible ony to users with the adequate
131     // permissions.
132     $this->drupalLogout();
133     $this->drupalGet('devel/container/service');
134     $this->assertSession()->statusCodeEquals(403);
135   }
136
137   /**
138    * Tests service detail page.
139    */
140   public function testServiceDetail() {
141     $service_id = 'devel.dumper';
142
143     // Ensures that the page works as expected.
144     $this->drupalGet("/devel/container/service/$service_id");
145     $this->assertSession()->statusCodeEquals(200);
146     $this->assertSession()->pageTextContains("Service $service_id detail");
147
148     // Ensures that the page returns a 404 error if the requested service is
149     // not defined.
150     $this->drupalGet('/devel/container/service/not.exists');
151     $this->assertSession()->statusCodeEquals(404);
152
153     // Ensures that the page is accessible ony to users with the adequate
154     // permissions.
155     $this->drupalLogout();
156     $this->drupalGet("devel/container/service/$service_id");
157     $this->assertSession()->statusCodeEquals(403);
158   }
159
160   /**
161    * Tests parameter list page.
162    */
163   public function testParameterList() {
164     // Ensures that the page works as expected.
165     $this->drupalGet('/devel/container/parameter');
166     $this->assertSession()->statusCodeEquals(200);
167     $this->assertSession()->pageTextContains('Container parameters');
168     $this->assertContainerInfoLocalTasks();
169
170     $page = $this->getSession()->getPage();
171
172     // Ensures that the parameters table is found.
173     $table = $page->find('css', 'table.devel-parameter-list');
174     $this->assertNotNull($table);
175
176     // Ensures that the expected table headers are found.
177     /** @var $headers \Behat\Mink\Element\NodeElement[] */
178     $headers = $table->findAll('css', 'thead th');
179     $this->assertEquals(2, count($headers));
180
181     $expected_headers = ['Name', 'Operations'];
182     $actual_headers = array_map(function ($element) {
183       return $element->getText();
184     }, $headers);
185     $this->assertSame($expected_headers, $actual_headers);
186
187     // Ensures that all the parameters are listed in the table.
188     $cached_definition = \Drupal::service('kernel')->getCachedContainerDefinition();
189     $this->assertNotNull($cached_definition);
190     $rows = $table->findAll('css', 'tbody tr');
191     $this->assertEquals(count($cached_definition['parameters']), count($rows));
192
193     // Tests the presence of some parameters in the table.
194     $expected_parameters = [
195       'container.modules',
196       'cache_bins',
197       'factory.keyvalue',
198       'twig.config',
199     ];
200
201     foreach ($expected_parameters as $parameter_name) {
202       $row = $table->find('css', sprintf('tbody tr:contains("%s")', $parameter_name));
203       $this->assertNotNull($row);
204
205       /** @var $cells \Behat\Mink\Element\NodeElement[] */
206       $cells = $row->findAll('css', 'td');
207       $this->assertEquals(2, count($cells));
208
209       $cell_parameter_name = $cells[0];
210       $this->assertEquals($parameter_name, $cell_parameter_name->getText());
211       $this->assertTrue($cell_parameter_name->hasClass('table-filter-text-source'));
212
213       $cell_operations = $cells[1];
214       $actual_href = $cell_operations->findLink('Devel')->getAttribute('href');
215       $expected_href = Url::fromRoute('devel.container_info.parameter.detail', ['parameter_name' => $parameter_name])->toString();
216       $this->assertEquals($expected_href, $actual_href);
217     }
218
219     // Ensures that the page is accessible ony to users with the adequate
220     // permissions.
221     $this->drupalLogout();
222     $this->drupalGet('devel/container/service');
223     $this->assertSession()->statusCodeEquals(403);
224   }
225
226   /**
227    * Tests parameter detail page.
228    */
229   public function testParameterDetail() {
230     $parameter_name = 'cache_bins';
231
232     // Ensures that the page works as expected.
233     $this->drupalGet("/devel/container/parameter/$parameter_name");
234     $this->assertSession()->statusCodeEquals(200);
235     $this->assertSession()->pageTextContains("Parameter $parameter_name value");
236
237     // Ensures that the page returns a 404 error if the requested parameter is
238     // not defined.
239     $this->drupalGet('/devel/container/parameter/not_exists');
240     $this->assertSession()->statusCodeEquals(404);
241
242     // Ensures that the page is accessible ony to users with the adequate
243     // permissions.
244     $this->drupalLogout();
245     $this->drupalGet("devel/container/service/$parameter_name");
246     $this->assertSession()->statusCodeEquals(403);
247   }
248
249   /**
250    * Asserts that container info local tasks are present.
251    */
252   protected function assertContainerInfoLocalTasks() {
253     $expected_local_tasks = [
254       ['devel.container_info.service', []],
255       ['devel.container_info.parameter', []],
256     ];
257
258     $this->assertLocalTasks($expected_local_tasks);
259   }
260
261 }