protected function WebTestBase::drupalPlaceBlock

Creates a block instance based on default settings.

Note: Until this can be done programmatically, the active user account must have permission to administer blocks.

@todo Add support for creating custom block instances.

Parameters

string $plugin_id: The plugin ID of the block type for this block instance.

array $settings: (optional) An associative array of settings for the block entity. Override the defaults by specifying the key and value in the array, for example:

$this
  ->drupalPlaceBlock('system_powered_by_block', array(
  'label' => t('Hello, world!'),
));

The following defaults are provided:

  • label: Random string.
  • machine_name: Random string.
  • region: 'sidebar_first'.
  • theme: The default theme.
  • visibility: Empty array.

Return value

\Drupal\block\Plugin\Core\Entity\Block The block entity.

41 calls to WebTestBase::drupalPlaceBlock()
AccessDeniedTest::testAccessDenied in drupal/core/modules/system/lib/Drupal/system/Tests/System/AccessDeniedTest.php
AggregatorRenderingTest::testBlockLinks in drupal/core/modules/aggregator/lib/Drupal/aggregator/Tests/AggregatorRenderingTest.php
Adds a feed block to the page and checks its links.
BasicTest::testViewsWizardAndListing in drupal/core/modules/views/lib/Drupal/views/Tests/Wizard/BasicTest.php
BlockCacheTest::setUp in drupal/core/modules/block/lib/Drupal/block/Tests/BlockCacheTest.php
Sets up a Drupal site for running functional and integration tests.
BlockHiddenRegionTest::setUp in drupal/core/modules/block/lib/Drupal/block/Tests/BlockHiddenRegionTest.php
Sets up a Drupal site for running functional and integration tests.

... See full list

File

drupal/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php, line 374
Definition of Drupal\simpletest\WebTestBase.

Class

WebTestBase
Test case for typical Drupal tests.

Namespace

Drupal\simpletest

Code

protected function drupalPlaceBlock($plugin_id, array $settings = array()) {
  $settings += array(
    'plugin' => $plugin_id,
    'region' => 'sidebar_first',
    'machine_name' => strtolower($this
      ->randomName(8)),
    'theme' => config('system.theme')
      ->get('default'),
    'label' => $this
      ->randomName(8),
    'visibility' => array(),
  );
  foreach (array(
    'region',
    'machine_name',
    'theme',
    'plugin',
    'visibility',
  ) as $key) {
    $values[$key] = $settings[$key];
    unset($settings[$key]);
  }
  $values['settings'] = $settings;

  // Build the ID out of the theme and machine_name.
  $values['id'] = $values['theme'] . '.' . $values['machine_name'];
  $block = entity_create('block', $values);
  $block
    ->save();
  return $block;
}