diff --git a/core/modules/block/tests/src/Functional/BlockRenderOrderTest.php b/core/modules/block/tests/src/Functional/BlockRenderOrderTest.php index 2fd1ab8925..09c8db855b 100644 --- a/core/modules/block/tests/src/Functional/BlockRenderOrderTest.php +++ b/core/modules/block/tests/src/Functional/BlockRenderOrderTest.php @@ -77,4 +77,60 @@ public function testBlockRenderOrder() { $this->assertTrue($position['stark_drupal'] < $position['stark_by'], 'Blocks with identical weight are rendered in alphabetical order.'); } + /** + * Tests the render order of the blocks after block deletion. + */ + public function testBlockRenderOrderAfterDeletion() { + $blocks_num = 40; + $blocks = array_fill(0, $blocks_num, [ + 'weight' => $blocks_num, + 'id' => 'stark_powered', + 'label' => 'Test block', + ]); + array_walk($blocks, [$this, 'prepareWeight']); + // Enable test blocks and place them in the same region. + $region = 'header'; + // Place the test blocks. + foreach ($blocks as $test_block) { + $this->drupalPlaceBlock('system_powered_by_block', [ + 'label' => $test_block['label'], + 'region' => $region, + 'weight' => $test_block['weight'], + 'id' => $test_block['id'], + ]); + } + + $this->drupalGet(''); + + $initial_weight_map = $weight_map = []; + /** @var \Drupal\Core\Entity\EntityStorageInterface $controller */ + $controller = $this->container->get('entity_type.manager')->getStorage('block'); + /** @var \Drupal\block\BlockInterface $return_block */ + foreach ($controller->loadByProperties(['region' => $region]) as $return_block) { + $initial_weight_map[$return_block->id()] = $return_block->getWeight(); + } + + $keys = array_keys($initial_weight_map); + $id_to_delete = $keys[$blocks_num / 2]; + $controller->load($id_to_delete)->delete(); + unset($initial_weight_map[$id_to_delete]); + + foreach ($controller->loadByProperties(['region' => $region]) as $return_block) { + $weight_map[$return_block->id()] = $return_block->getWeight(); + } + $this->assertEquals($initial_weight_map, $weight_map, 'Blocks save their weight values after block delete.'); + } + + /** + * Preparation function. + * + * @param array $item + * Block item. + * @param int $key + * Array key. + */ + public function prepareWeight(array &$item, $key) { + $item['id'] .= $key; + } + }