function BlockContentTypeTest::testsBlockContentAddTypes

Same name and namespace in other branches
  1. 9 core/modules/block_content/tests/src/Functional/BlockContentTypeTest.php \Drupal\Tests\block_content\Functional\BlockContentTypeTest::testsBlockContentAddTypes()
  2. 8.9.x core/modules/block_content/tests/src/Functional/BlockContentTypeTest.php \Drupal\Tests\block_content\Functional\BlockContentTypeTest::testsBlockContentAddTypes()
  3. 10 core/modules/block_content/tests/src/Functional/BlockContentTypeTest.php \Drupal\Tests\block_content\Functional\BlockContentTypeTest::testsBlockContentAddTypes()

Tests that redirects work as expected when multiple block types exist.

File

core/modules/block_content/tests/src/Functional/BlockContentTypeTest.php, line 214

Class

BlockContentTypeTest
Ensures that block type functions work correctly.

Namespace

Drupal\Tests\block_content\Functional

Code

public function testsBlockContentAddTypes() : void {
  // Now create an initial block-type.
  $this->createBlockContentType([
    'id' => 'basic',
  ], TRUE);
  $this->drupalLogin($this->adminUser);
  // Create two block types programmatically.
  $this->createBlockContentType([
    'id' => 'foo',
  ]);
  $this->createBlockContentType([
    'id' => 'bar',
  ]);
  // Get the content block storage.
  $storage = $this->container
    ->get('entity_type.manager')
    ->getStorage('block_content');
  // Install all themes.
  $themes = [
    'olivero',
    'stark',
    'claro',
  ];
  \Drupal::service('theme_installer')->install($themes);
  $theme_settings = $this->config('system.theme');
  foreach ($themes as $default_theme) {
    // Change the default theme.
    $theme_settings->set('default', $default_theme)
      ->save();
    $this->drupalPlaceBlock('local_actions_block');
    // For each installed theme, go to its block page and test the redirects.
    foreach ($themes as $theme) {
      // Test that adding a block from the 'place blocks' form sends you to
      // the block configure form.
      $path = $theme == $default_theme ? 'admin/structure/block' : "admin/structure/block/list/{$theme}";
      $this->drupalGet($path);
      $this->clickLink('Place block');
      $this->clickLink('Add content block');
      $this->clickLink('foo');
      // Create a new block.
      $edit = [
        'info[0][value]' => $this->randomMachineName(8),
      ];
      $this->submitForm($edit, 'Save and configure');
      $blocks = $storage->loadByProperties([
        'info' => $edit['info[0][value]'],
      ]);
      if (!empty($blocks)) {
        $block = reset($blocks);
        $this->assertSession()
          ->addressEquals(Url::fromRoute('block.admin_add', [
          'plugin_id' => 'block_content:' . $block->uuid(),
          'theme' => $theme,
        ]));
        $this->submitForm([
          'region' => 'content',
        ], 'Save block');
        $this->assertSession()
          ->addressEquals(Url::fromRoute('block.admin_display_theme', [
          'theme' => $theme,
        ], [
          'query' => [
            'block-placement' => $theme . '-' . Html::getClass($edit['info[0][value]']),
          ],
        ]));
      }
      else {
        $this->fail('Could not load created block.');
      }
    }
  }
  // Test that adding a block from the 'content blocks list' doesn't send you
  // to the block configure form.
  $this->drupalGet('admin/content/block');
  $this->clickLink('Add content block');
  $this->clickLink('foo');
  $edit = [
    'info[0][value]' => $this->randomMachineName(8),
  ];
  $this->submitForm($edit, 'Save');
  $blocks = $storage->loadByProperties([
    'info' => $edit['info[0][value]'],
  ]);
  if (!empty($blocks)) {
    $this->assertSession()
      ->addressEquals(Url::fromRoute('entity.block_content.collection'));
  }
  else {
    $this->fail('Could not load created block.');
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.