You are here

public function BlockBlacklistTest::testBlockBlacklistConfigForm in Block Blacklist [Deprecated] 8

Tests the config form.

File

tests/src/Functional/BlockBlacklistTest.php, line 60

Class

BlockBlacklistTest
Tests for the Block Blacklist module.

Namespace

Drupal\Tests\block_blacklist\Functional

Code

public function testBlockBlacklistConfigForm() {

  // Array of configuration options with the rule to use and a block the rule
  // should hide.
  $replace = [
    'system_match' => [
      'label' => 'System Blacklist Match',
      'rule' => 'help',
      'hides' => 'help',
    ],
    'system_prefix' => [
      'label' => 'System Blacklist Prefix',
      'rule' => 'system_menu_block',
      'hides' => 'system_menu_block:admin',
    ],
    'system_regex' => [
      'label' => 'System Blacklist Regex',
      'rule' => '/local_(.*)_block/',
      'hides' => 'local_tasks_block',
    ],
    'layout_match' => [
      'label' => 'Layout Builder Blacklist Match',
      'rule' => 'page_title_block',
      'hides' => 'page_title_block',
    ],
    'layout_prefix' => [
      'label' => 'Layout Builder Blacklist Prefix',
      'rule' => 'inline_block',
      'hides' => 'inline_block:basic',
    ],
    'layout_regex' => [
      'label' => 'Layout Builder Blacklist Regex',
      'rule' => '/system_(.*)_block/',
      'hides' => 'system_powered_by_block',
    ],
  ];

  // Login.
  $this
    ->drupalLogin($this->user);

  // Access config page.
  $this
    ->drupalGet('admin/config/block_blacklist/settings');
  $this
    ->assertResponse(200);

  // Test the form elements exist and have default values.
  $config = $this
    ->config('block_blacklist.settings');
  foreach ($replace as $field => $opt) {
    $this
      ->assertFieldByName($field, $config
      ->get('block_blacklist.settings.' . $field), $field . ' field has the default value');
  }

  // Test form submission.
  $edit = [];
  foreach ($replace as $field => $opt) {
    $edit[$field] = $opt['rule'];
  }
  $this
    ->drupalPostForm(NULL, $edit, t('Save configuration'));
  $this
    ->assertText('The configuration options have been saved.', 'The form was saved correctly.');

  // Test the new values are there.
  $this
    ->drupalGet('admin/config/block_blacklist/settings');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  foreach ($replace as $field => $opt) {
    $string = strtr('//*[@name=":field_name"]', [
      ':field_name' => $field,
    ]);
    $elements = $this
      ->xpath($string);
    $value = count($elements) ? $elements[0]
      ->getValue() : NULL;
    $this
      ->assertEquals($value, $opt['rule']);
  }

  // Test block list results.
  $pages = [
    'system' => 'admin/config/block_blacklist/system-list',
    'layout' => 'admin/config/block_blacklist/layout-list',
  ];
  foreach ($pages as $type => $page) {
    $this
      ->drupalGet($page);
    $this
      ->assertSession()
      ->statusCodeEquals(200);
    foreach ($replace as $field => $opt) {
      switch ($field) {
        case $type . '_match':
        case $type . '_prefix':
        case $type . '_regex':
          $this
            ->assertNoText($opt['hides'], $opt['label'] . ' block was not displayed .');
          break;
      }
    }
  }
  $this
    ->drupalLogout();
}