You are here

public function ConditionalFieldSelectTest::testVisibleValueWidget in Conditional Fields 8

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

The target field is Visible when the control field has value from Widget.

Overrides ConditionalFieldValueInterface::testVisibleValueWidget

File

tests/src/FunctionalJavascript/ConditionalFieldSelectTest.php, line 131

Class

ConditionalFieldSelectTest
Test Conditional Fields Select Plugin.

Namespace

Drupal\Tests\conditional_fields\FunctionalJavascript

Code

public function testVisibleValueWidget() {
  $this
    ->baseTestSteps();
  $correct_values = [
    'select_single_entity_reference' => 1,
    'select_single_list_integer' => 1,
    'select_single_list_float' => 1.5,
    'select_single_list_string' => 'one',
  ];
  $wrong_values = [
    'select_single_entity_reference' => 3,
    'select_single_list_integer' => 3,
    'select_single_list_float' => 3.5,
    'select_single_list_string' => 'three',
  ];

  // Visit a ConditionalFields configuration page for Content bundles.
  foreach ($this->fieldNames as $fieldName) {
    $this
      ->createCondition('body', $fieldName, 'visible', 'value');
    $this
      ->createScreenshot($this->screenshotPath . '01-' . $fieldName . '_' . __FUNCTION__ . '.png');

    // Set up conditions.
    $data = [
      '[name="condition"]' => 'value',
      '[name="values_set"]' => ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_WIDGET,
      $this->fieldSelectors[$fieldName] => $correct_values[$fieldName],
      '[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-' . $fieldName . '_' . __FUNCTION__ . '.png');

    // Check if that configuration is saved.
    $this
      ->drupalGet('admin/structure/types/manage/article/conditionals');
    $this
      ->createScreenshot($this->screenshotPath . '03-' . $fieldName . '_' . __FUNCTION__ . '.png');
    $this
      ->assertSession()
      ->pageTextContains('body ' . $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-' . $fieldName . '_' . __FUNCTION__ . '.png');
    $this
      ->waitUntilHidden('.field--name-body', 50, 'Article Body field is visible');

    // Change a select value set that should not show the body.
    $this
      ->changeField($this->fieldSelectors[$fieldName], $wrong_values[$fieldName]);
    $this
      ->createScreenshot($this->screenshotPath . '05-' . $fieldName . '_' . __FUNCTION__ . '.png');
    $this
      ->waitUntilHidden('.field--name-body', 50, 'Article Body field is visible');

    // Change a select value set to show the body.
    $this
      ->changeField($this->fieldSelectors[$fieldName], $correct_values[$fieldName]);
    $this
      ->createScreenshot($this->screenshotPath . '06-' . $fieldName . '_' . __FUNCTION__ . '.png');
    $this
      ->waitUntilVisible('.field--name-body', 50, 'Article Body field is not visible');

    // Change a select value set to hide the body again.
    $this
      ->changeField($this->fieldSelectors[$fieldName], '_none');
    $this
      ->createScreenshot($this->screenshotPath . '07-' . $fieldName . '_' . __FUNCTION__ . '.png');
    $this
      ->waitUntilHidden('.field--name-body', 50, 'Article Body field is visible');

    // Return back to ConditionalFields configuration page for Article CT.
    $this
      ->drupalGet('admin/structure/conditional_fields/node/article');

    // Delete previous condition.
    $this
      ->click('li > button > .dropbutton-arrow');
    $this
      ->clickLink('Delete');
    $this
      ->submitForm([], 'Confirm');
    $this
      ->createScreenshot($this->screenshotPath . '08-' . $fieldName . '_' . __FUNCTION__ . '.png');
  }
}