3 namespace Drupal\Tests\block\Functional;
5 use Drupal\Tests\BrowserTestBase;
8 * Tests block HTML ID validity.
12 class BlockHtmlTest extends BrowserTestBase {
19 public static $modules = ['block', 'block_test'];
21 protected function setUp() {
24 $this->drupalLogin($this->rootUser);
26 // Enable the test_html block, to test HTML ID and attributes.
27 \Drupal::state()->set('block_test.attributes', ['data-custom-attribute' => 'foo']);
28 \Drupal::state()->set('block_test.content', $this->randomMachineName());
29 $this->drupalPlaceBlock('test_html', ['id' => 'test_html_block']);
31 // Enable a menu block, to test more complicated HTML.
32 $this->drupalPlaceBlock('system_menu_block:admin');
36 * Tests for valid HTML for a block.
38 public function testHtml() {
41 // Ensure that a block's ID is converted to an HTML valid ID, and that
42 // block-specific attributes are added to the same DOM element.
43 $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.');
45 // Ensure expected markup for a menu block.
46 $elements = $this->xpath('//nav[contains(@class, :nav-class)]/ul[contains(@class, :ul-class)]/li', [':nav-class' => 'block-menu', ':ul-class' => 'menu']);
47 $this->assertTrue(!empty($elements), 'The proper block markup was found.');