You are here

public function BlockFieldPluginTest::testBlockAccess in Display Suite 8.3

Same name and namespace in other branches
  1. 8.4 tests/src/Functional/BlockFieldPluginTest.php \Drupal\Tests\ds\Functional\BlockFieldPluginTest::testBlockAccess()

Ensure block is not rendered if block disallows access.

File

tests/src/Functional/BlockFieldPluginTest.php, line 104

Class

BlockFieldPluginTest
Tests for managing custom code, and block fields.

Namespace

Drupal\Tests\ds\Functional

Code

public function testBlockAccess() {
  $block_field_id = mb_strtolower($this
    ->randomMachineName());
  $entity_type = 'node';
  $edit = [
    'name' => $this
      ->randomString(),
    'id' => $block_field_id,
    'entities[' . $entity_type . ']' => TRUE,
    'block' => 'ds_test_block',
  ];
  $this
    ->dsCreateBlockField($edit);
  $fields['fields[dynamic_block_field:' . $entity_type . '-' . $block_field_id . '][region]'] = 'left';
  $this
    ->dsSelectLayout();
  $this
    ->dsConfigureUI($fields);
  $settings['type'] = 'article';
  $node = $this
    ->drupalCreateNode($settings);

  // Check block is not visible.
  \Drupal::state()
    ->set('ds_test_block__access', FALSE);
  $this
    ->drupalGet($node
    ->toUrl());
  $this
    ->assertSession()
    ->responseNotContains(DsTestBlock::BODY_TEXT);

  // Reset page cache.
  $this
    ->resetAll();

  // Check block is visible.
  \Drupal::state()
    ->set('ds_test_block__access', TRUE);
  $this
    ->drupalGet($node
    ->toUrl());
  $this
    ->assertSession()
    ->responseContains(DsTestBlock::BODY_TEXT);
}