You are here

public function ConditionalFieldEntityReferenceTagsTest::testVisibleValueWidget in Conditional Fields 4.x

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

Tests creating Conditional Field: Visible if has value from widget.

File

tests/src/FunctionalJavascript/ConditionalFieldEntityReferenceTagsTest.php, line 94

Class

ConditionalFieldEntityReferenceTagsTest
Test Conditional Fields Entity Reference Tags Plugin.

Namespace

Drupal\Tests\conditional_fields\FunctionalJavascript

Code

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

  // Create a node that we will use in reference field.
  $node = Node::create([
    'type' => 'article',
    'title' => 'Referenced node',
  ]);
  $node
    ->save();
  $referenced_format_valid = sprintf("%s (%d)", $node
    ->label(), $node
    ->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-tags-add-filed-conditions.png');

  // Set up conditions.
  $data = [
    '[name="condition"]' => 'value',
    '[name="values_set"]' => ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_WIDGET,
    $this->fieldSelector => $referenced_format_valid,
    '[name="grouping"]' => 'AND',
    '[name="state"]' => 'visible',
    '[name="effect"]' => 'show',
  ];
  foreach ($data as $selector => $value) {
    $this
      ->changeField($selector, $value);
  }
  $this
    ->getSession()
    ->wait(1000, '!jQuery.active');
  $this
    ->getSession()
    ->executeScript("jQuery('#conditional-field-edit-form').submit();");
  $this
    ->createScreenshot($this->screenshotPath . '02-entity-reference-tags-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-tags-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-tags-body-invisible-when-controlled-field-has-no-value.png');
  $this
    ->waitUntilHidden('.field--name-body', 50, 'Article Body field is not 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-tags-body-invisible-when-controlled-field-has-wrong-value.png');
  $this
    ->waitUntilHidden('.field--name-body', 50, 'Article Body field is not visible');

  // Change an entity reference field in format 'Node title (nid)' to show the
  // body.
  $this
    ->changeField($this->fieldSelector, $referenced_format_valid);
  $this
    ->createScreenshot($this->screenshotPath . '06-entity-reference-tags-body-visible-when-controlled-field-has-valid-value.png');
  $this
    ->waitUntilVisible('.field--name-body', 50, 'Article Body field is 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-tags-body-invisible-when-controlled-field-has-value-in-wrong-format.png');
  $this
    ->waitUntilHidden('.field--name-body', 50, 'Article Body field is not visible');

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