BlockHtmlTest.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. namespace Drupal\Tests\block\Functional;
  3. use Drupal\Tests\BrowserTestBase;
  4. /**
  5. * Tests block HTML ID validity.
  6. *
  7. * @group block
  8. */
  9. class BlockHtmlTest extends BrowserTestBase {
  10. /**
  11. * Modules to install.
  12. *
  13. * @var array
  14. */
  15. public static $modules = ['block', 'block_test'];
  16. protected function setUp() {
  17. parent::setUp();
  18. $this->drupalLogin($this->rootUser);
  19. // Enable the test_html block, to test HTML ID and attributes.
  20. \Drupal::state()->set('block_test.attributes', ['data-custom-attribute' => 'foo']);
  21. \Drupal::state()->set('block_test.content', $this->randomMachineName());
  22. $this->drupalPlaceBlock('test_html', ['id' => 'test_html_block']);
  23. // Enable a menu block, to test more complicated HTML.
  24. $this->drupalPlaceBlock('system_menu_block:admin');
  25. }
  26. /**
  27. * Tests for valid HTML for a block.
  28. */
  29. public function testHtml() {
  30. $this->drupalGet('');
  31. // Ensure that a block's ID is converted to an HTML valid ID, and that
  32. // block-specific attributes are added to the same DOM element.
  33. $this->assertFieldByXPath('//div[@id="block-test-html-block" and @data-custom-attribute="foo"]', NULL, 'HTML ID and attributes for test block are valid and on the same DOM element.');
  34. // Ensure expected markup for a menu block.
  35. $elements = $this->xpath('//nav[contains(@class, :nav-class)]/ul[contains(@class, :ul-class)]/li', [':nav-class' => 'block-menu', ':ul-class' => 'menu']);
  36. $this->assertTrue(!empty($elements), 'The proper block markup was found.');
  37. }
  38. }