3 namespace Drupal\Tests\block\Functional;
5 use Drupal\Component\Utility\Html;
6 use Drupal\Tests\BrowserTestBase;
9 * Tests blocks are being rendered in order by weight.
13 class BlockRenderOrderTest extends BrowserTestBase {
20 public static $modules = ['node', 'block'];
22 protected function setUp() {
24 // Create a test user.
25 $end_user = $this->drupalCreateUser([
28 $this->drupalLogin($end_user);
32 * Tests the render order of the blocks.
34 public function testBlockRenderOrder() {
35 // Enable test blocks and place them in the same region.
40 'id' => 'stark_powered',
41 'label' => 'Test block A',
46 'label' => 'Test block C',
50 'id' => 'stark_drupal',
51 'label' => 'Test block B',
55 // Place the test blocks.
56 foreach ($test_blocks as $test_block) {
57 $this->drupalPlaceBlock('system_powered_by_block', [
58 'label' => $test_block['label'],
60 'weight' => $test_block['weight'],
61 'id' => $test_block['id'],
66 $test_content = $this->getSession()->getPage()->getContent();
68 $controller = $this->container->get('entity_type.manager')->getStorage('block');
69 foreach ($controller->loadMultiple() as $return_block) {
70 $id = $return_block->id();
71 if ($return_block_weight = $return_block->getWeight()) {
72 $this->assertTrue($test_blocks[$id]['weight'] == $return_block_weight, 'Block weight is set as "' . $return_block_weight . '" for ' . $id . ' block.');
73 $position[$id] = strpos($test_content, Html::getClass('block-' . $test_blocks[$id]['id']));
76 $this->assertTrue($position['stark_powered'] < $position['stark_by'], 'Blocks with different weight are rendered in the correct order.');
77 $this->assertTrue($position['stark_drupal'] < $position['stark_by'], 'Blocks with identical weight are rendered in alphabetical order.');