DevelLayoutInfoTest.php 4.72 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?php

namespace Drupal\Tests\devel\Functional;

use Drupal\Tests\BrowserTestBase;

/**
 * Tests layout info pages and links.
 *
 * @group devel
 */
class DevelLayoutInfoTest extends BrowserTestBase {

  /**
   * {@inheritdoc}
   */
  public static $modules = ['devel', 'block', 'layout_discovery'];

  /**
   * The user for the test.
   *
   * @var \Drupal\user\UserInterface
   */
  protected $develUser;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
30
31
32
33
34
35
    // TODO find a cleaner way to skip layout info tests when running tests on
    // Drupal branch < 8.3.x.
    if (version_compare(\Drupal::VERSION, '8.3', '<')) {
      $this->markTestSkipped('Devel Layout Info Tests only available on version 8.3.x+.');
    }

36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
    parent::setUp();

    $this->drupalPlaceBlock('page_title_block');

    $this->develUser = $this->drupalCreateUser(['access devel information']);
    $this->drupalLogin($this->develUser);
  }

  /**
   * Tests layout info menu link.
   */
  public function testLayoutsInfoMenuLink() {
    $this->drupalPlaceBlock('system_menu_block:devel');
    // Ensures that the layout info link is present on the devel menu and that
    // it points to the correct page.
    $this->drupalGet('');
    $this->clickLink('Layouts Info');
    $this->assertSession()->statusCodeEquals(200);
    $this->assertSession()->addressEquals('/devel/layouts');
    $this->assertSession()->pageTextContains('Layout');
  }

  /**
   * Tests layout info page.
   */
61
  public function testLayoutList() {
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
    $this->drupalGet('/devel/layouts');
    $this->assertSession()->statusCodeEquals(200);
    $this->assertSession()->pageTextContains('Layouts');

    $page = $this->getSession()->getPage();

    // Ensures that the layout table is found.
    $table = $page->find('css', 'table.devel-layout-list');
    $this->assertNotNull($table);

    // Ensures that the expected table headers are found.
    /** @var $headers \Behat\Mink\Element\NodeElement[] */
    $headers = $table->findAll('css', 'thead th');
    $this->assertEquals(6, count($headers));

    $expected_headers = ['Icon', 'Label', 'Description', 'Category', 'Regions', 'Provider'];
    $actual_headers = array_map(function ($element) {
      return $element->getText();
    }, $headers);
    $this->assertSame($expected_headers, $actual_headers);

    // Ensures that all the layouts are listed in the table.
    $layout_manager = \Drupal::service('plugin.manager.core.layout');
    $layouts = $layout_manager->getDefinitions();
    $table_rows = $table->findAll('css', 'tbody tr');
    $this->assertEquals(count($layouts), count($table_rows));

    $index = 0;
    foreach ($layouts as $layout) {
      $cells = $table_rows[$index]->findAll('css', 'td');
      $this->assertEquals(6, count($cells));

      $cell_layout_icon = $cells[0];
      if (empty($layout->getIconPath())) {
        // @todo test that the icon path image is set correctly
      }
      else {
        $this->assertNull($cell_layout_icon->getText());
      }

      $cell_layout_label = $cells[1];
103
      $this->assertEquals($cell_layout_label->getText(), $layout->getLabel());
104
105

      $cell_layout_description = $cells[2];
106
      $this->assertEquals($cell_layout_description->getText(), $layout->getDescription());
107
108

      $cell_layout_category = $cells[3];
109
      $this->assertEquals($cell_layout_category->getText(), $layout->getCategory());
110
111

      $cell_layout_regions = $cells[4];
112
      $this->assertEquals($cell_layout_regions->getText(), implode(', ', $layout->getRegionLabels()));
113
114

      $cell_layout_provider = $cells[5];
115
      $this->assertEquals($cell_layout_provider->getText(), $layout->getProvider());
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135

      $index++;
    }

    // Ensures that the page is accessible only to the users with the adequate
    // permissions.
    $this->drupalLogout();
    $this->drupalGet('devel/layouts');
    $this->assertSession()->statusCodeEquals(403);
  }

  /**
   * Tests the dependency with layout_discovery module.
   */
  public function testLayoutDiscoveryDependency() {
    $this->container->get('module_installer')->uninstall(['layout_discovery']);
    $this->drupalPlaceBlock('system_menu_block:devel');

    // Ensures that the layout info link is not present on the devel menu.
    $this->drupalGet('');
136
    $this->assertSession()->linkNotExists('Layouts Info');
137
138
139
140
141
142
143
144
145
146
147
148
149

    // Ensures that the layouts info page is not available.
    $this->drupalGet('/devel/layouts');
    $this->assertSession()->statusCodeEquals(404);

    // Check a few other devel pages to verify devel module stil works.
    $this->drupalGet('/devel/events');
    $this->assertSession()->statusCodeEquals(200);
    $this->drupalGet('devel/routes');
    $this->assertSession()->statusCodeEquals(200);
    $this->drupalGet('/devel/container/service');
    $this->assertSession()->statusCodeEquals(200);
  }
150

151
}