You are here

protected function TestBase::setFieldValue in Select (or other) 8

Same name and namespace in other branches
  1. 4.x tests/src/Functional/TestBase.php \Drupal\Tests\select_or_other\Functional\TestBase::setFieldValue()

Submit the add node form with the selected values.

Parameters

string $field_name: Name of the field.

string $select: Value to set on the select element.

string $other: Value to set for the other element.

2 calls to TestBase::setFieldValue()
ListTest::testIllegalChoice in tests/src/Functional/ListTest.php
Test for illegal choice.
TestBase::testEmptyOption in tests/src/Functional/TestBase.php
Make sure an empty option is present when relevant.

File

tests/src/Functional/TestBase.php, line 189

Class

TestBase
Base test class for select or other widgets.

Namespace

Drupal\Tests\select_or_other\Functional

Code

protected function setFieldValue($field_name, $select, $other = '') {
  $edit = array();
  if ($select !== '') {

    // A node requires a title.
    $edit["title[0][value]"] = $this
      ->randomMachineName(8);
    $this
      ->drupalGet('node/add/' . $this
      ->getFieldContentType($field_name));
  }

  // Set the select value.
  if ($this->fields[$field_name]['cardinality'] == 1) {
    $edit["{$field_name}[select]"] = $select;
  }
  else {

    // We have to treat multiple selection elements differently.
    if ($this->fields[$field_name]['select_type'] === 'select_or_other_select') {

      // We're dealing with a multiple select.
      $edit["{$field_name}[select][]"] = $select;
    }
    else {

      // We're dealing with checkboxes.
      $edit["{$field_name}[select][{$select}]"] = $select;
    }
  }

  // Set the other value.
  $edit["{$field_name}[other]"] = $other;
  if ($select !== '') {

    // Create the node.
    $this
      ->drupalPostForm(NULL, $edit, t('Save'));
  }
  else {
    $this
      ->drupalPostForm(NULL, $edit, t('Preview'));
  }
}