1 list.test ListFieldUITestCase::testListAllowedValuesText()

List (text) : test 'allowed values' input.

File

core/modules/field/modules/list/tests/list.test, line 369
Tests for list.module.

Class

ListFieldUITestCase
List module UI tests.

Code

function testListAllowedValuesText() {
  $this->field_name = 'field_list_text';
  $this->createListField('list_text');

  // Flat list of textual values.
  $string = "Zero\nOne";
  $array = array('Zero' => 'Zero', 'One' => 'One');
  $this->assertAllowedValuesInput($string, $array, 'Unkeyed lists are accepted.');
  // Explicit keys.
  $string = "zero|Zero\none|One";
  $array = array('zero' => 'Zero', 'one' => 'One');
  $this->assertAllowedValuesInput($string, $array, 'Explicit keys are accepted.');
  // Check that values can be added and removed.
  $string = "zero|Zero\ntwo|Two";
  $array = array('zero' => 'Zero', 'two' => 'Two');
  $this->assertAllowedValuesInput($string, $array, 'Values can be added and removed.');
  // Mixed list of keyed and unkeyed values.
  $string = "zero|Zero\nOne\n";
  $array = array('zero' => 'Zero', 'One' => 'One');
  $this->assertAllowedValuesInput($string, $array, 'Mixed lists are accepted.');
  // Overly long keys.
  $this->assertAllowedValuesInput("zero|Zero\n" . $this->randomName(256) . "|One", 'each key must be a string at most 255 characters long', 'Overly long keys are rejected.');
  // Set with explicit keys so rest of checks can use them.
  $string = "1|Zero\n2|One";
  $array = array('1' => 'Zero', '2' => 'One');
  $this->assertAllowedValuesInput($string, $array, 'Explicit keys are accepted.');

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

  // Check that flat lists of values are still accepted once the field has
  // data.
  $string = "Zero\nOne";
  $array = array('1' => 'Zero', '2' => 'One');
  $this->assertAllowedValuesInput($string, $array, 'Unkeyed lists are still accepted once the field has data.');

  // Check that values can be added but values in use cannot be removed.
  $string = "1|Zero\n2|One\n3|Two";
  $array = array('1' => 'Zero', '2' => 'One', '3' => 'Two');
  $this->assertAllowedValuesInput($string, $array, 'Values can be added.');
  $string = "1|Zero\n2|One";
  $array = array('1' => 'Zero', '2' => 'One');
  $this->assertAllowedValuesInput($string, $array, 'Values not in use can be removed.');
  $this->assertAllowedValuesInput("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 = "Zero";
  $array = array('Zero' => 'Zero');
  $this->assertAllowedValuesInput($string, $array, 'Values not in use can be removed.');
}