function OptionsFieldUITest::testOptionsAllowedValuesFloat

Options (float) : test 'allowed values' input.

File

drupal/core/modules/field/modules/options/lib/Drupal/options/Tests/OptionsFieldUITest.php, line 99
Definition of Drupal\options\Tests\OptionsFieldUITest.

Class

OptionsFieldUITest
Options module UI tests.

Namespace

Drupal\options\Tests

Code

function testOptionsAllowedValuesFloat() {
  $this->field_name = 'field_options_float';
  $this
    ->createOptionsField('list_float');

  // Flat list of textual values.
  $string = "Zero\nOne";
  $array = array(
    '0' => 'Zero',
    '1' => 'One',
  );
  $this
    ->assertAllowedValuesInput($string, $array, 'Unkeyed lists are accepted.');

  // Explicit numeric keys.
  $string = "0|Zero\n.5|Point five";
  $array = array(
    '0' => 'Zero',
    '0.5' => 'Point five',
  );
  $this
    ->assertAllowedValuesInput($string, $array, 'Integer keys are accepted.');

  // Check that values can be added and removed.
  $string = "0|Zero\n.5|Point five\n1.0|One";
  $array = array(
    '0' => 'Zero',
    '0.5' => 'Point five',
    '1' => 'One',
  );
  $this
    ->assertAllowedValuesInput($string, $array, 'Values can be added and removed.');

  // Non-numeric keys.
  $this
    ->assertAllowedValuesInput("abc|abc\n", 'each key must be a valid integer or decimal', 'Non numeric keys are rejected.');

  // Mixed list of keyed and unkeyed values.
  $this
    ->assertAllowedValuesInput("Zero\n1|One\n", 'invalid input', 'Mixed lists are rejected.');

  // Create a node with actual data for the field.
  $settings = array(
    'type' => $this->type,
    $this->field_name => array(
      LANGUAGE_NOT_SPECIFIED => array(
        array(
          'value' => 0.5,
        ),
      ),
    ),
  );
  $node = $this
    ->drupalCreateNode($settings);

  // Check that a flat list of values is rejected once the field has data.
  $this
    ->assertAllowedValuesInput("Zero\nOne", 'invalid input', 'Unkeyed lists are rejected once the field has data.');

  // Check that values can be added but values in use cannot be removed.
  $string = "0|Zero\n.5|Point five\n2|Two";
  $array = array(
    '0' => 'Zero',
    '0.5' => 'Point five',
    '2' => 'Two',
  );
  $this
    ->assertAllowedValuesInput($string, $array, 'Values can be added.');
  $string = "0|Zero\n.5|Point five";
  $array = array(
    '0' => 'Zero',
    '0.5' => 'Point five',
  );
  $this
    ->assertAllowedValuesInput($string, $array, 'Values not in use can be removed.');
  $this
    ->assertAllowedValuesInput("0|Zero", 'some values are being removed while currently in use', 'Values in use cannot be removed.');

  // Delete the node, remove the value.
  node_delete($node->nid);
  $string = "0|Zero";
  $array = array(
    '0' => 'Zero',
  );
  $this
    ->assertAllowedValuesInput($string, $array, 'Values not in use can be removed.');
}