Skip to content
BlockRenderOrderTest.php 2.26 KiB
Newer Older
 * Tests blocks are being rendered in order by weight.
 *
 * @group block
class BlockRenderOrderTest extends BrowserTestBase {
  public static $modules = ['node', 'block'];
    $end_user = $this->drupalCreateUser([
    $this->drupalLogin($end_user);
  }

  /**
   * Tests the render order of the blocks.
   */
  public function testBlockRenderOrder() {
    // Enable test blocks and place them in the same region.
    $region = 'header';

    // Place the test blocks.
    foreach ($test_blocks as $test_block) {
      $this->drupalPlaceBlock('system_powered_by_block', [
        'region' => $region,
        'weight' => $test_block['weight'],
    $test_content = $this->getRawContent('');
    $controller = $this->container->get('entity_type.manager')->getStorage('block');
    foreach ($controller->loadMultiple() as $return_block) {
      $id = $return_block->id();
      if ($return_block_weight = $return_block->getWeight()) {
        $this->assertTrue($test_blocks[$id]['weight'] == $return_block_weight, 'Block weight is set as "' . $return_block_weight . '" for ' . $id . ' block.');
        $position[$id] = strpos($test_content, Html::getClass('block-' . $test_blocks[$id]['id']));
    $this->assertTrue($position['stark_powered'] < $position['stark_by'], 'Blocks with different weight are rendered in the correct order.');
    $this->assertTrue($position['stark_drupal'] < $position['stark_by'], 'Blocks with identical weight are rendered in alphabetical order.');