You are here

public function DoubleFieldWidgetsTestCase::testRequired in Double Field 7.2

Test 'required' functionality.

File

tests/double_field_widgets.test, line 71
Widgets tests for double field module.

Class

DoubleFieldWidgetsTestCase
Tests double field widgets.

Code

public function testRequired() {
  $widget = new DoubleFieldWidget('textfield_&_select', $this->field['settings']);
  $this->instance['widget'] = array(
    'module' => 'double_field',
    'type' => $widget
      ->getType(),
  );
  $this->instance['required'] = TRUE;
  field_update_instance($this->instance);
  $widget
    ->setRequiredOptions(TRUE, FALSE);
  $input = $widget
    ->getSettingsFormInput();
  $this
    ->drupalPost($this->field_settings_page, $input, t('Save settings'));

  // This is a regression test for #1918064.
  $this
    ->assertNoRaw(t('%field field is required.', array(
    '%field' => $this->instance['label'],
  )), '"Field is required message" was not found.');

  // Let's try to play with node form.
  $input = array(
    'title' => 'Test',
  ) + $widget
    ->getFormInput($this->field_name);
  $this
    ->drupalPost('node/add/' . $this->type_name, $input, t('Save'));
  $this
    ->assertRaw(t('@type %title has been created.', array(
    '@type' => $this->type_name,
    '%title' => $input['title'],
  )), 'The node was created successfully.');
  unset($input[$this
    ->getSubfieldName('first')]);
  $this
    ->drupalPost('node/add/' . $this->type_name, $input, t('Save'));
  $this
    ->assertNoRaw(t('@type %title has been created.', array(
    '@type' => $this->type_name,
    '%title' => $input['title'],
  )), 'The node was not created successfully.');
  $this
    ->assertRaw(t('%field field is required.', array(
    '%field' => $this->instance['label'],
  )), 'Field is required message found.');

  // A non required field without any value has an "none" option.
  $this
    ->assertTrue($this
    ->xpath('//select[@name=:name]//option[@value="" and text()=:label]', array(
    ':name' => $this
      ->getSubfieldName('second'),
    ':label' => t('- None -'),
  )), 'A required select list has a "None" choice.');

  // Verify error mapping.
  $this
    ->assertTrue($this
    ->xpath('//input[@name=:name and contains(@class, "error")]', array(
    ':name' => $this
      ->getSubfieldName('first'),
  )), 'First subfield has error class.');
  $this
    ->assertFalse($this
    ->xpath('//select[@name=:name and contains(@class, "error")]', array(
    ':name' => $this
      ->getSubfieldName('second'),
  )), 'Second subfield has not error class.');
  $widget
    ->setRequiredOptions(FALSE, TRUE);
  $input = $widget
    ->getSettingsFormInput();
  $this
    ->drupalPost($this->field_settings_page, $input, t('Save settings'));
  $input = array(
    'title' => 'Test',
  ) + $widget
    ->getFormInput($this->field_name);
  $this
    ->drupalPost('node/add/' . $this->type_name, $input, t('Save'));
  $this
    ->assertRaw(t('@type %title has been created.', array(
    '@type' => $this->type_name,
    '%title' => $input['title'],
  )), 'The node was created successfully.');
  unset($input[$this
    ->getSubfieldName('second')]);
  $this
    ->drupalPost('node/add/' . $this->type_name, $input, t('Save'));
  $this
    ->assertRaw(t('%field field is required.', array(
    '%field' => $this->instance['label'],
  )), '"Field is required message" was found.');

  // A required field without any value has an empty option.
  $this
    ->assertTrue($this
    ->xpath('//select[@name=:name]//option[@value="" and text()=:label]', array(
    ':name' => $this
      ->getSubfieldName('second'),
    ':label' => t('- Select a value -'),
  )), 'Required select list has a "Select a value" choice.');

  // Verify error mapping.
  $this
    ->assertFalse($this
    ->xpath('//input[@name=:name and contains(@class, "error")]', array(
    ':name' => $this
      ->getSubfieldName('first'),
  )), t('First subfield has not the error class.'));
  $this
    ->assertTrue($this
    ->xpath('//select[@name=:name and contains(@class, "error")]', array(
    ':name' => $this
      ->getSubfieldName('second'),
  )), t('Second subfield has the error class.'));
  $widget
    ->setRequiredOptions(FALSE, FALSE);
  $input = $widget
    ->getSettingsFormInput();
  $this
    ->drupalPost($this->field_settings_page, $input, t('Save settings'));
  $input = array(
    'title' => 'Test',
  ) + $widget
    ->getFormInput($this->field_name);
  unset($input[$this
    ->getSubfieldName('first')]);
  unset($input[$this
    ->getSubfieldName('second')]);
  $this
    ->drupalPost('node/add/' . $this->type_name, $input, t('Save'));

  // Verify error mapping.
  $this
    ->assertTrue($this
    ->xpath('//input[@name=:name and contains(@class, "error")]', array(
    ':name' => $this
      ->getSubfieldName('first'),
  )), t('First subfield has the error class.'));
  $this
    ->assertTrue($this
    ->xpath('//select[@name=:name and contains(@class, "error")]', array(
    ':name' => $this
      ->getSubfieldName('second'),
  )), t('Second subfield has the error class.'));

  // Disable global 'required' option.
  $this->instance['required'] = FALSE;
  field_update_instance($this->instance);
  $this
    ->drupalPost('node/add/' . $this->type_name, $input, t('Save'));
  $this
    ->assertRaw(t('@type %title has been created.', array(
    '@type' => $this->type_name,
    '%title' => $input['title'],
  )), 'The node was created successfully.');
}