Newer
Older
Alex Pott
committed
<?php
Lee Rowlands
committed
namespace Drupal\Tests\block\Functional;
Alex Pott
committed
use Drupal\Component\Utility\Html;
Lee Rowlands
committed
use Drupal\Tests\BrowserTestBase;
Alex Pott
committed
/**
* Tests blocks are being rendered in order by weight.
*
* @group block
Alex Pott
committed
*/
Lee Rowlands
committed
class BlockRenderOrderTest extends BrowserTestBase {
Alex Pott
committed
/**
Jennifer Hodgdon
committed
* Modules to install.
Alex Pott
committed
*
* @var array
*/
public static $modules = ['node', 'block'];
Alex Pott
committed
Alex Pott
committed
protected function setUp() {
Alex Pott
committed
parent::setUp();
// Create a test user.
$end_user = $this->drupalCreateUser([
Alex Pott
committed
'access content',
]);
Alex Pott
committed
$this->drupalLogin($end_user);
}
/**
* Tests the render order of the blocks.
*/
public function testBlockRenderOrder() {
Alex Pott
committed
// Enable test blocks and place them in the same region.
$region = 'header';
$test_blocks = [
'stark_powered' => [
Alex Pott
committed
'weight' => '-3',
'id' => 'stark_powered',
'label' => 'Test block A',
],
'stark_by' => [
Alex Pott
committed
'weight' => '3',
'id' => 'stark_by',
'label' => 'Test block C',
],
'stark_drupal' => [
Alex Pott
committed
'weight' => '3',
'id' => 'stark_drupal',
'label' => 'Test block B',
],
];
Alex Pott
committed
// Place the test blocks.
foreach ($test_blocks as $test_block) {
$this->drupalPlaceBlock('system_powered_by_block', [
'label' => $test_block['label'],
Alex Pott
committed
'region' => $region,
'weight' => $test_block['weight'],
'id' => $test_block['id'],
]);
Alex Pott
committed
}
$this->drupalGet('');
$test_content = $this->getRawContent('');
Alex Pott
committed
$controller = $this->container->get('entity_type.manager')->getStorage('block');
Alex Pott
committed
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']));
Alex Pott
committed
}
}
$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.');
Alex Pott
committed
}