3a708e5155baf75d01ba0707ea2271489c4bf740
[yaffs-website] / web / modules / contrib / devel / tests / src / Functional / DevelEntityTypeInfoTest.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 entity type info pages and links.
11  *
12  * @group devel
13  */
14 class DevelEntityTypeInfoTest 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 entity info menu link.
43    */
44   public function testEntityInfoMenuLink() {
45     $this->drupalPlaceBlock('system_menu_block:devel');
46     // Ensures that the entity type info link is present on the devel menu and that
47     // it points to the correct page.
48     $this->drupalGet('');
49     $this->clickLink('Entity Info');
50     $this->assertSession()->statusCodeEquals(200);
51     $this->assertSession()->addressEquals('/devel/entity/info');
52     $this->assertSession()->pageTextContains('Entity Info');
53   }
54
55   /**
56    * Tests entity type list page.
57    */
58   public function testEntityTypeList() {
59     $this->drupalGet('/devel/entity/info');
60     $this->assertSession()->statusCodeEquals(200);
61     $this->assertSession()->pageTextContains('Entity Info');
62
63     $page = $this->getSession()->getPage();
64
65     // Ensures that the entity type list table is found.
66     $table = $page->find('css', 'table.devel-entity-type-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(5, count($headers));
72
73     $expected_headers = ['ID', '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) entity types in the table.
80     $expected_types = [
81       'date_format' => [
82         'name' => 'Date format',
83         'class' => 'Drupal\Core\Datetime\Entity\DateFormat',
84         'provider' => 'core',
85       ],
86       'block' => [
87         'name' => 'Block',
88         'class' => 'Drupal\block\Entity\Block',
89         'provider' => 'block',
90       ],
91       'entity_view_mode' => [
92         'name' => 'View mode',
93         'class' => 'Drupal\Core\Entity\Entity\EntityViewMode',
94         'provider' => 'core',
95       ],
96     ];
97
98     foreach ($expected_types as $entity_type_id => $entity_type) {
99       $row = $table->find('css', sprintf('tbody tr:contains("%s")', $entity_type_id));
100       $this->assertNotNull($row);
101
102       /** @var $cells \Behat\Mink\Element\NodeElement[] */
103       $cells = $row->findAll('css', 'td');
104       $this->assertEquals(5, count($cells));
105
106       $cell = $cells[0];
107       $this->assertEquals($entity_type_id, $cell->getText());
108       $this->assertTrue($cell->hasClass('table-filter-text-source'));
109
110       $cell = $cells[1];
111       $this->assertEquals($entity_type['name'], $cell->getText());
112       $this->assertTrue($cell->hasClass('table-filter-text-source'));
113
114       $cell = $cells[2];
115       $this->assertEquals($entity_type['provider'], $cell->getText());
116       $this->assertTrue($cell->hasClass('table-filter-text-source'));
117
118       $cell = $cells[3];
119       $this->assertEquals($entity_type['class'], $cell->getText());
120       $this->assertTrue($cell->hasClass('table-filter-text-source'));
121
122       $cell = $cells[4];
123       $actual_href = $cell->findLink('Devel')->getAttribute('href');
124       $expected_href = Url::fromRoute('devel.entity_info_page.detail', ['entity_type_id' => $entity_type_id])->toString();
125       $this->assertEquals($expected_href, $actual_href);
126     }
127
128     // Ensures that the page is accessible only to the users with the adequate
129     // permissions.
130     $this->drupalLogout();
131     $this->drupalGet('devel/entity/info');
132     $this->assertSession()->statusCodeEquals(403);
133   }
134
135   /**
136    * Tests entity type detail page.
137    */
138   public function testEntityTypeDetail() {
139     $entity_type_id = 'date_format';
140
141     // Ensures that the page works as expected.
142     $this->drupalGet("/devel/entity/info/$entity_type_id");
143     $this->assertSession()->statusCodeEquals(200);
144     $this->assertSession()->pageTextContains("Entity type $entity_type_id");
145
146     // Ensures that the page returns a 404 error if the requested entity type is
147     // not defined.
148     $this->drupalGet('/devel/entity/info/not_exists');
149     $this->assertSession()->statusCodeEquals(404);
150
151     // Ensures that the page is accessible ony to users with the adequate
152     // permissions.
153     $this->drupalLogout();
154     $this->drupalGet("/devel/entity/info/$entity_type_id");
155     $this->assertSession()->statusCodeEquals(403);
156   }
157
158 }