3 namespace Drupal\Tests\block\Functional;
5 use Drupal\block\Entity\Block;
6 use Drupal\Component\Render\FormattableMarkup;
9 * Provides test assertions for testing block appearance.
11 * Can be used by test classes that extend \Drupal\Tests\BrowserTestBase.
13 trait AssertBlockAppearsTrait {
16 * Checks to see whether a block appears on the page.
18 * @param \Drupal\block\Entity\Block $block
19 * The block entity to find on the page.
21 protected function assertBlockAppears(Block $block) {
22 $result = $this->findBlockInstance($block);
23 $this->assertTrue(!empty($result), new FormattableMarkup('The block @id appears on the page', ['@id' => $block->id()]));
27 * Checks to see whether a block does not appears on the page.
29 * @param \Drupal\block\Entity\Block $block
30 * The block entity to find on the page.
32 protected function assertNoBlockAppears(Block $block) {
33 $result = $this->findBlockInstance($block);
34 $this->assertFalse(!empty($result), new FormattableMarkup('The block @id does not appear on the page', ['@id' => $block->id()]));
38 * Find a block instance on the page.
40 * @param \Drupal\block\Entity\Block $block
41 * The block entity to find on the page.
44 * The result from the xpath query.
46 protected function findBlockInstance(Block $block) {
47 return $this->xpath('//div[@id = :id]', [':id' => 'block-' . $block->id()]);