You are here

public function ConditionalFieldSelectTest::testVisibleValueOr 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::testVisibleValueOr()

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

Overrides ConditionalFieldValueInterface::testVisibleValueOr

File

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

Class

ConditionalFieldSelectTest
Test Conditional Fields Select Plugin.

Namespace

Drupal\Tests\conditional_fields\FunctionalJavascript

Code

public function testVisibleValueOr() {
  $this
    ->baseTestSteps();
  $allowed_values = [
    'select_single_entity_reference' => "1\n2",
    'select_single_list_integer' => "1\n2",
    'select_single_list_float' => "1.5\n2.5",
    'select_single_list_string' => "one\ntwo",
  ];
  $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 = [
      'condition' => 'value',
      'values_set' => ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_OR,
      'values' => $allowed_values[$fieldName],
      'grouping' => 'AND',
      'state' => 'visible',
      'effect' => 'show',
    ];
    $this
      ->submitForm($data, 'Save settings');
    $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, '04.' . $fieldName . '. Article Body field is visible');

    // Change a select value set that should be visible the body.
    $this
      ->changeField($this->fieldSelectors[$fieldName], $correct_values[$fieldName]);
    $this
      ->createScreenshot($this->screenshotPath . '05-' . $fieldName . '_' . __FUNCTION__ . '.png');
    $this
      ->waitUntilVisible('.field--name-body', 50, '05.' . $fieldName . '. Article Body field is not 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, '05.' . $fieldName . '. Article Body field is 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, '07.' . $fieldName . '. 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');
  }
}