You are here

public function ConditionalFieldEntityReferenceTest::testVisibleValueXor in Conditional Fields 8

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

The target field is Visible when the control field has value with XOR condition.

Overrides ConditionalFieldValueInterface::testVisibleValueXor

File

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

Class

ConditionalFieldEntityReferenceTest
Test Conditional Fields Entity Reference Plugin.

Namespace

Drupal\Tests\conditional_fields\FunctionalJavascript

Code

public function testVisibleValueXor() {
  $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_XOR,
    '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 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, '02. 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');
}