You are here

public function ConditionalFieldEntityReferenceTest::testVisibleValueOr in Conditional Fields 4.x

Same name and namespace in other branches
  1. 8 tests/src/FunctionalJavascript/ConditionalFieldEntityReferenceTest.php \Drupal\Tests\conditional_fields\FunctionalJavascript\ConditionalFieldEntityReferenceTest::testVisibleValueOr()

Target field is Visible when the control field has value with OR condition.

Overrides ConditionalFieldValueInterface::testVisibleValueOr

File

tests/src/FunctionalJavascript/ConditionalFieldEntityReferenceTest.php, line 324

Class

ConditionalFieldEntityReferenceTest
Test Conditional Fields Entity Reference Plugin.

Namespace

Drupal\Tests\conditional_fields\FunctionalJavascript

Code

public function testVisibleValueOr() {
  $this
    ->baseTestSteps();

  // Create a node that we will use in reference field.
  $node = Node::create([
    'type' => 'article',
    'title' => 'Referenced node',
  ]);
  $node
    ->save();
  $node_2 = Node::create([
    'type' => 'article',
    'title' => 'Referenced node 2',
  ]);
  $node_2
    ->save();
  $referenced_format_1 = sprintf("%s (%d)", $node
    ->label(), $node
    ->id());
  $referenced_format_2 = sprintf("%s (%d)", $node_2
    ->label(), $node_2
    ->id());
  $referenced_format_wrong = sprintf("%s ", $node
    ->label());

  // Visit a ConditionalFields configuration page for Content bundles.
  $this
    ->createCondition('body', 'field_' . $this->fieldName, 'visible', 'value');
  $this
    ->createScreenshot($this->screenshotPath . '01-entity-reference-add-filed-conditions.png');

  // Set up conditions.
  $data = [
    'condition' => 'value',
    'values_set' => ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_OR,
    'values' => "{$referenced_format_1}\r\n{$referenced_format_2}",
    'grouping' => 'AND',
    'state' => 'visible',
    'effect' => 'show',
  ];
  $this
    ->submitForm($data, 'Save settings');
  $this
    ->createScreenshot($this->screenshotPath . '02-entity-reference-post-add-list-options-filed-conditions.png');

  // Check if that configuration is saved.
  $this
    ->drupalGet('admin/structure/types/manage/article/conditionals');
  $this
    ->createScreenshot($this->screenshotPath . '03-entity-reference-submit-entity-reference-filed-conditions.png');
  $this
    ->assertSession()
    ->pageTextContains('body field_' . $this->fieldName . ' visible value');

  // Visit Article Add form to check that conditions are applied.
  $this
    ->drupalGet('node/add/article');

  // Check that the field Body is not visible.
  $this
    ->createScreenshot($this->screenshotPath . '04-entity-reference-body-invisible-when-controlled-field-has-no-value.png');
  $this
    ->waitUntilHidden('.field--name-body', 50, '01. Article Body field is visible');

  // Change an entity reference field that should not show the body.
  $this
    ->changeField($this->fieldSelector, $this
    ->randomMachineName());
  $this
    ->createScreenshot($this->screenshotPath . '05-entity-reference-body-invisible-when-controlled-field-has-wrong-value.png');
  $this
    ->waitUntilHidden('.field--name-body', 50, '02. Article Body field is visible');

  // Change an entity reference field in format 'Node title (nid)' to show the
  // body.
  $this
    ->changeField($this->fieldSelector, $referenced_format_1);
  $this
    ->createScreenshot($this->screenshotPath . '06-entity-reference-body-visible-when-controlled-field-has-value-format-1.png');
  $this
    ->waitUntilVisible('.field--name-body', 50, '03. Article Body field is not visible');

  // Set wrong format for an entity reference field to hide the body.
  $this
    ->changeField($this->fieldSelector, $referenced_format_wrong);
  $this
    ->createScreenshot($this->screenshotPath . '07-entity-reference-body-invisible-when-controlled-field-has-value-in-wrong-format.png');
  $this
    ->waitUntilHidden('.field--name-body', 50, '04. Article Body field is visible');

  // Change an entity reference field in format 'Node title' to show the body.
  $this
    ->changeField($this->fieldSelector, $referenced_format_2);
  $this
    ->createScreenshot($this->screenshotPath . '08-entity-reference-body-visible-when-controlled-field-has-value-format-2.png');
  $this
    ->waitUntilVisible('.field--name-body', 50, '05. Article Body field is not visible');

  // Set an empty value to hide body.
  $this
    ->changeField($this->fieldSelector, '');
  $this
    ->createScreenshot($this->screenshotPath . '09-entity-reference-body-invisible-when-controlled-field-has-no-value-again.png');
  $this
    ->waitUntilHidden('.field--name-body', 50, '06. Article Body field is visible');
}