You are here

protected function SelectOrOtherTestBase::selectAllOptions in Select (or other) 7.3

Submit the add node form with all options selected for the specified field.

Parameters

string $field_name: Name of the field to select all options for.

string $other_option: Optionally provide a value for the other field. Will default to 999

1 call to SelectOrOtherTestBase::selectAllOptions()
SelectOrOtherTestBase::testCardinalityValidation in tests/SelectOrOtherTestBase.test
Make sure field cardinality is checked.

File

tests/SelectOrOtherTestBase.test, line 240
Contains SelectOrOtherTestBase.

Class

SelectOrOtherTestBase
Class SelectOrOtherTestBase Base class for select_or_other testing.

Code

protected function selectAllOptions($field_name, $other_option = '') {
  $edit = array();

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

  // Set the select value.
  if ($this->fields[$field_name]['cardinality'] == 1) {
    $this
      ->fail('Unable to select multiple options because the cardinality is 1');
  }
  else {

    // We have to treat multiple selection elements differently.
    if ($this
      ->getWidgetType($field_name) === 'select') {

      // Select all options for this select box.
      $xpath = "//select[@id = :id]/option";
      $arguments = array(
        ':id' => "edit-{$field_name}-und-select",
      );
      $options = $this
        ->xpath($xpath, $arguments);

      // Prepare array of values.
      $select = array();
      foreach ($options as $option) {
        $select[] = $option['value'];
      }
      $edit["{$field_name}[und][select][]"] = $select;
    }
    else {

      // Find all checkboxes.
      $xpath = "//input[@id[starts-with(., :id)]]";
      $arguments = array(
        ':id' => "edit-{$field_name}-und-select",
      );
      $options = $this
        ->xpath($xpath, $arguments);
      foreach ($options as $option) {
        $id = reset($option['id']);
        $label = $this
          ->xpath('//label[@for = :id]', array(
          ':id' => $id,
        ));
        $key = reset($option['name']);
        $value = (string) reset($label);
        $edit[$key] = trim($value);
      }
    }
  }

  // Unless the other option is overridden, set a numeric other value because
  // each widget will accept one.
  $edit["{$field_name}[und][other]"] = $other_option == '' ? 999 : $other_option;

  // Create the node.
  $this
    ->drupalPost(NULL, $edit, t('Save'));
}