You are here

public function WidgetTest::testBlockSettingsAreStoredCorrectly in Block field 8

Test block settings are stored correctly.

File

tests/src/Functional/WidgetTest.php, line 67

Class

WidgetTest
Test the block field widget.

Namespace

Drupal\Tests\block_field\Functional

Code

public function testBlockSettingsAreStoredCorrectly() {
  $page = $this
    ->getSession()
    ->getPage();
  $assert_session = $this
    ->assertSession();
  $items = $this
    ->createDummyNodes('item', 5);
  $this
    ->drupalGet($this->blockNode
    ->toUrl('edit-form'));
  $assert_session
    ->checkboxChecked('Display title');
  $assert_session
    ->checkboxNotChecked('Override title');
  $items_per_page_element = $page
    ->findField('Items per block');
  $this
    ->assertNotNull($items_per_page_element);
  $this
    ->assertEquals('none', $items_per_page_element
    ->getValue());
  $assert_session
    ->elementContains('named', [
    'field',
    'Items per block',
  ], '1 (default setting)');
  $page
    ->selectFieldOption('Items per block', 10);

  // This view has a contextual filter to exclude the node from the URL from
  // showing up if the context is present. Initially we do not choose that
  // context when placing the block.
  $exclude_element = $page
    ->findField('Exclude');
  $this
    ->assertNotNull($exclude_element);
  $this
    ->assertEmpty($exclude_element
    ->getValue());

  // Save the node and check the view items.
  $page
    ->pressButton('Save');
  $assert_session
    ->pageTextContains("Block node {$this->blockNode->getTitle()} has been updated");
  $css_selector = '.views-element-container';
  foreach ($items as $item) {
    $this
      ->assertSession()
      ->elementContains('css', $css_selector, $item
      ->getTitle());
  }

  // The node we are visiting shows up in the views results.
  $this
    ->assertSession()
    ->elementContains('css', $css_selector, 'Block field test');

  // Select the context to exclude the node from the URL and try again.
  $this
    ->drupalGet($this->blockNode
    ->toUrl('edit-form'));
  $page
    ->selectFieldOption('Exclude', 'Node from URL');
  $page
    ->pressButton('Save');
  $assert_session
    ->pageTextContains("Block node {$this->blockNode->getTitle()} has been updated");
  foreach ($items as $item) {
    $this
      ->assertSession()
      ->elementContains('css', $css_selector, $item
      ->getTitle());
  }

  // The node we are visiting does not show up anymore.
  $this
    ->assertSession()
    ->elementNotContains('css', $css_selector, 'Block field test');
}