You are here

public function BlockContentTypeTest::testsBlockContentAddTypes in Drupal 9

Same name and namespace in other branches
  1. 8 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 192

Class

BlockContentTypeTest
Ensures that custom block type functions work correctly.

Namespace

Drupal\Tests\block_content\Functional

Code

public function testsBlockContentAddTypes() {

  // Now create an initial block-type.
  $this
    ->createBlockContentType('basic', TRUE);
  $this
    ->drupalLogin($this->adminUser);

  // Create two block types programmatically.
  $this
    ->createBlockContentType('foo');
  $this
    ->createBlockContentType('bar');

  // Get the custom block storage.
  $storage = $this->container
    ->get('entity_type.manager')
    ->getStorage('block_content');

  // Install all themes.
  \Drupal::service('theme_installer')
    ->install([
    'bartik',
    'seven',
    'stark',
  ]);
  $theme_settings = $this
    ->config('system.theme');
  foreach ([
    'bartik',
    'seven',
    'stark',
  ] 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 ([
      'bartik',
      'seven',
      'stark',
    ] 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 custom block');

      // The seven theme has markup inside the link, we cannot use clickLink().
      if ($default_theme == 'seven') {
        $options = $theme != $default_theme ? [
          'query' => [
            'theme' => $theme,
          ],
        ] : [];
        $this
          ->assertSession()
          ->linkByHrefExists(Url::fromRoute('block_content.add_form', [
          'block_content_type' => 'foo',
        ], $options)
          ->toString());
        $this
          ->drupalGet('block/add/foo', $options);
      }
      else {
        $this
          ->clickLink('foo');
      }

      // Create a new block.
      $edit = [
        'info[0][value]' => $this
          ->randomMachineName(8),
      ];
      $this
        ->submitForm($edit, 'Save');
      $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' => Html::getClass($edit['info[0][value]']),
          ],
        ]));
      }
      else {
        $this
          ->fail('Could not load created block.');
      }
    }
  }

  // Test that adding a block from the 'custom blocks list' doesn't send you
  // to the block configure form.
  $this
    ->drupalGet('admin/structure/block/block-content');
  $this
    ->clickLink('Add custom 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.');
  }
}