2698e24da3bff57fbed1233725989f6ba799cb3d
[yaffs-website] / web / modules / contrib / devel / tests / src / Functional / DevelElementInfoTest.php
1 <?php
2
3 namespace Drupal\Tests\devel\Functional;
4
5 use Behat\Mink\Element\NodeElement;
6 use Drupal\Core\Url;
7 use Drupal\Tests\BrowserTestBase;
8
9 /**
10  * Tests element info pages and links.
11  *
12  * @group devel
13  */
14 class DevelElementInfoTest extends BrowserTestBase {
15
16   /**
17    * {@inheritdoc}
18    */
19   public static $modules = ['devel', 'block'];
20
21   /**
22    * The user for the test.
23    *
24    * @var \Drupal\user\UserInterface
25    */
26   protected $develUser;
27
28   /**
29    * {@inheritdoc}
30    */
31   protected function setUp() {
32     parent::setUp();
33
34     $this->drupalPlaceBlock('system_menu_block:devel');
35     $this->drupalPlaceBlock('page_title_block');
36
37     $this->develUser = $this->drupalCreateUser(['access devel information']);
38     $this->drupalLogin($this->develUser);
39   }
40
41   /**
42    * Tests element info menu link.
43    */
44   public function testElementInfoMenuLink() {
45     $this->drupalPlaceBlock('system_menu_block:devel');
46     // Ensures that the element info link is present on the devel menu and that
47     // it points to the correct page.
48     $this->drupalGet('');
49     $this->clickLink('Element Info');
50     $this->assertSession()->statusCodeEquals(200);
51     $this->assertSession()->addressEquals('/devel/elements');
52     $this->assertSession()->pageTextContains('Element Info');
53   }
54
55   /**
56    * Tests element list page.
57    */
58   public function testElementList() {
59     $this->drupalGet('/devel/elements');
60     $this->assertSession()->statusCodeEquals(200);
61     $this->assertSession()->pageTextContains('Element Info');
62
63     $page = $this->getSession()->getPage();
64
65     // Ensures that the element list table is found.
66     $table = $page->find('css', 'table.devel-element-list');
67     $this->assertNotNull($table);
68
69     // Ensures that the expected table headers are found.
70     $headers = $table->findAll('css', 'thead th');
71     $this->assertEquals(4, count($headers));
72
73     $expected_headers = ['Name', 'Provider', 'Class', 'Operations'];
74     $actual_headers = array_map(function (NodeElement $element) {
75       return $element->getText();
76     }, $headers);
77     $this->assertSame($expected_headers, $actual_headers);
78
79     // Tests the presence of some (arbitrarily chosen) elements in the table.
80     $expected_elements = [
81       'button' => [
82         'class' => 'Drupal\Core\Render\Element\Button',
83         'provider' => 'core',
84       ],
85       'form' => [
86         'class' => 'Drupal\Core\Render\Element\Form',
87         'provider' => 'core',
88       ],
89       'html' => [
90         'class' => 'Drupal\Core\Render\Element\Html',
91         'provider' => 'core',
92       ],
93     ];
94
95     foreach ($expected_elements as $element_name => $element) {
96       $row = $table->find('css', sprintf('tbody tr:contains("%s")', $element_name));
97       $this->assertNotNull($row);
98
99       /** @var $cells \Behat\Mink\Element\NodeElement[] */
100       $cells = $row->findAll('css', 'td');
101       $this->assertEquals(4, count($cells));
102
103       $cell = $cells[0];
104       $this->assertEquals($element_name, $cell->getText());
105       $this->assertTrue($cell->hasClass('table-filter-text-source'));
106
107       $cell = $cells[1];
108       $this->assertEquals($element['provider'], $cell->getText());
109       $this->assertTrue($cell->hasClass('table-filter-text-source'));
110
111       $cell = $cells[2];
112       $this->assertEquals($element['class'], $cell->getText());
113       $this->assertTrue($cell->hasClass('table-filter-text-source'));
114
115       $cell = $cells[3];
116       $actual_href = $cell->findLink('Devel')->getAttribute('href');
117       $expected_href = Url::fromRoute('devel.elements_page.detail', ['element_name' => $element_name])->toString();
118       $this->assertEquals($expected_href, $actual_href);
119     }
120
121     // Ensures that the page is accessible only to the users with the adequate
122     // permissions.
123     $this->drupalLogout();
124     $this->drupalGet('devel/elements');
125     $this->assertSession()->statusCodeEquals(403);
126   }
127
128   /**
129    * Tests element detail page.
130    */
131   public function testElementDetail() {
132     $element_name = 'button';
133
134     // Ensures that the page works as expected.
135     $this->drupalGet("/devel/elements/$element_name");
136     $this->assertSession()->statusCodeEquals(200);
137     $this->assertSession()->pageTextContains("Element $element_name");
138
139     // Ensures that the page returns a 404 error if the requested element is
140     // not defined.
141     $this->drupalGet('/devel/elements/not_exists');
142     $this->assertSession()->statusCodeEquals(404);
143
144     // Ensures that the page is accessible ony to users with the adequate
145     // permissions.
146     $this->drupalLogout();
147     $this->drupalGet("/devel/elements/$element_name");
148     $this->assertSession()->statusCodeEquals(403);
149   }
150
151 }