Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / block / tests / src / Functional / BlockRenderOrderTest.php
1 <?php
2
3 namespace Drupal\Tests\block\Functional;
4
5 use Drupal\Component\Utility\Html;
6 use Drupal\Tests\BrowserTestBase;
7
8 /**
9  * Tests blocks are being rendered in order by weight.
10  *
11  * @group block
12  */
13 class BlockRenderOrderTest extends BrowserTestBase {
14
15   /**
16    * Modules to install.
17    *
18    * @var array
19    */
20   public static $modules = ['node', 'block'];
21
22   protected function setUp() {
23     parent::setUp();
24     // Create a test user.
25     $end_user = $this->drupalCreateUser([
26       'access content',
27     ]);
28     $this->drupalLogin($end_user);
29   }
30
31   /**
32    * Tests the render order of the blocks.
33    */
34   public function testBlockRenderOrder() {
35     // Enable test blocks and place them in the same region.
36     $region = 'header';
37     $test_blocks = [
38       'stark_powered' => [
39         'weight' => '-3',
40         'id' => 'stark_powered',
41         'label' => 'Test block A',
42       ],
43       'stark_by' => [
44         'weight' => '3',
45         'id' => 'stark_by',
46         'label' => 'Test block C',
47       ],
48       'stark_drupal' => [
49         'weight' => '3',
50         'id' => 'stark_drupal',
51         'label' => 'Test block B',
52       ],
53     ];
54
55     // Place the test blocks.
56     foreach ($test_blocks as $test_block) {
57       $this->drupalPlaceBlock('system_powered_by_block', [
58         'label' => $test_block['label'],
59         'region' => $region,
60         'weight' => $test_block['weight'],
61         'id' => $test_block['id'],
62       ]);
63     }
64
65     $this->drupalGet('');
66     $test_content = $this->getSession()->getPage()->getContent();
67
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']));
74       }
75     }
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.');
78   }
79
80 }