function RenderElementTypesTest::testSystemCompactLink

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Render/Element/RenderElementTypesTest.php \Drupal\KernelTests\Core\Render\Element\RenderElementTypesTest::testSystemCompactLink()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Render/Element/RenderElementTypesTest.php \Drupal\KernelTests\Core\Render\Element\RenderElementTypesTest::testSystemCompactLink()
  3. 10 core/tests/Drupal/KernelTests/Core/Render/Element/RenderElementTypesTest.php \Drupal\KernelTests\Core\Render\Element\RenderElementTypesTest::testSystemCompactLink()

Tests system #type 'system_compact_link'.

File

core/tests/Drupal/KernelTests/Core/Render/Element/RenderElementTypesTest.php, line 197

Class

RenderElementTypesTest
Tests the rendered markup of core render element types.

Namespace

Drupal\KernelTests\Core\Render\Element

Code

public function testSystemCompactLink() : void {
  $elements = [
    [
      'name' => "#type 'system_compact_link' when admin compact mode is off",
      'value' => [
        '#type' => 'system_compact_link',
      ],
      'expected' => '//div[@class="compact-link"]/a[contains(@href, "admin/compact/on?") and text()="Hide descriptions"]',
    ],
    [
      'name' => "#type 'system_compact_link' when adding extra attributes",
      'value' => [
        '#type' => 'system_compact_link',
        '#attributes' => [
          'class' => [
            'kittens-rule',
          ],
        ],
      ],
      'expected' => '//div[@class="compact-link"]/a[contains(@href, "admin/compact/on?") and @class="kittens-rule" and text()="Hide descriptions"]',
    ],
  ];
  foreach ($elements as $element) {
    $xml = new \SimpleXMLElement((string) \Drupal::service('renderer')->renderRoot($element['value']));
    $result = $xml->xpath($element['expected']);
    $this->assertNotEmpty($result, '"' . $element['name'] . '" is rendered correctly.');
  }
  // Set admin compact mode on for additional tests.
  \Drupal::request()->cookies
    ->set('Drupal_visitor_admin_compact_mode', TRUE);
  $element = [
    'name' => "#type 'system_compact_link' when admin compact mode is on",
    'value' => [
      '#type' => 'system_compact_link',
    ],
    'expected' => '//div[@class="compact-link"]/a[contains(@href, "admin/compact?") and text()="Show descriptions"]',
  ];
  $xml = new \SimpleXMLElement((string) \Drupal::service('renderer')->renderRoot($element['value']));
  $result = $xml->xpath($element['expected']);
  $this->assertNotEmpty($result, '"' . $element['name'] . '" is rendered correctly.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.