- <?php
-
- * @file
- * Tests for options.module.
- */
-
- require_once BACKDROP_ROOT . '/core/modules/field/tests/field.test';
- require_once BACKDROP_ROOT . '/core/modules/field/modules/list/tests/list.test';
-
- class OptionsWidgetsTestCase extends FieldTestCase {
- function setUp() {
- parent::setUp('field_test', 'list_test');
-
-
- $this->card_1 = array(
- 'field_name' => 'card_1',
- 'type' => 'list_integer',
- 'cardinality' => 1,
- 'settings' => array(
- 'allowed_values' => array(
-
- 0 => 'Zero',
- 1 => 'One',
-
- 2 => 'Some <script>dangerous</script> & unescaped <strong>markup</strong>',
-
- 3 => 'Some HTML encoded markup with < & >',
- ),
- ),
- );
- $this->card_1 = field_create_field($this->card_1);
-
-
- $this->card_2 = array(
- 'field_name' => 'card_2',
- 'type' => 'list_integer',
- 'cardinality' => 2,
- 'settings' => array(
- 'allowed_values' => array(
-
- 0 => 'Zero',
- 1 => 'One',
-
- 2 => 'Some <script>dangerous</script> & unescaped <strong>markup</strong>',
- ),
- ),
- );
- $this->card_2 = field_create_field($this->card_2);
-
-
- $this->bool = array(
- 'field_name' => 'bool',
- 'type' => 'list_boolean',
- 'cardinality' => 1,
- 'settings' => array(
- 'allowed_values' => array(
-
- 1 => 'Zero',
-
- 0 => 'Some <script>dangerous</script> & unescaped <strong>markup</strong>',
- ),
- ),
- );
- $this->bool = field_create_field($this->bool);
-
-
- $this->web_user = $this->backdropCreateUser(array('access field_test content', 'administer field_test content', 'administer fields'));
- $this->backdropLogin($this->web_user);
- }
-
-
- * Tests the 'options_buttons' widget (single select).
- */
- function testRadioButtons() {
-
- $instance = array(
- 'field_name' => $this->card_1['field_name'],
- 'entity_type' => 'test_entity',
- 'bundle' => 'test_bundle',
- 'widget' => array(
- 'type' => 'options_buttons',
- ),
- );
- $instance = field_create_instance($instance);
- $langcode = LANGUAGE_NONE;
-
-
- $entity_init = field_test_create_entity();
- $entity = clone $entity_init;
- $entity->is_new = TRUE;
- field_test_entity_save($entity);
-
-
- $this->backdropGet('test-entity/manage/' . $entity->ftid . '/edit');
- $this->assertNoFieldChecked("edit-card-1-$langcode-0");
- $this->assertNoFieldChecked("edit-card-1-$langcode-1");
- $this->assertNoFieldChecked("edit-card-1-$langcode-2");
- $this->assertRaw('Some dangerous & unescaped <strong>markup</strong>', 'Option text was properly filtered.');
-
-
- $edit = array("card_1[$langcode]" => 0);
- $this->backdropPost(NULL, $edit, t('Save'));
- $this->assertFieldValues($entity_init, 'card_1', $langcode, array(0));
-
-
- $this->backdropGet('test-entity/manage/' . $entity->ftid . '/edit');
- $this->assertFieldChecked("edit-card-1-$langcode-0");
- $this->assertNoFieldChecked("edit-card-1-$langcode-1");
- $this->assertNoFieldChecked("edit-card-1-$langcode-2");
-
-
- $edit = array("card_1[$langcode]" => '_none');
- $this->backdropPost(NULL, $edit, t('Save'));
- $this->assertFieldValues($entity_init, 'card_1', $langcode, array());
-
-
- $this->card_1['settings']['allowed_values'] = array(99 => 'Only allowed value');
- field_update_field($this->card_1);
- $instance['required'] = TRUE;
- field_update_instance($instance);
- $this->backdropGet('test-entity/manage/' . $entity->ftid . '/edit');
- $this->assertFieldChecked("edit-card-1-$langcode-99");
- }
-
-
- * Tests the 'options_buttons' widget (multiple select).
- */
- function testCheckBoxes() {
-
- $instance = array(
- 'field_name' => $this->card_2['field_name'],
- 'entity_type' => 'test_entity',
- 'bundle' => 'test_bundle',
- 'widget' => array(
- 'type' => 'options_buttons',
- ),
- );
- $instance = field_create_instance($instance);
- $langcode = LANGUAGE_NONE;
-
-
- $entity_init = field_test_create_entity();
- $entity = clone $entity_init;
- $entity->is_new = TRUE;
- field_test_entity_save($entity);
-
-
- $this->backdropGet('test-entity/manage/' . $entity->ftid . '/edit');
- $this->assertNoFieldChecked("edit-card-2-$langcode-0");
- $this->assertNoFieldChecked("edit-card-2-$langcode-1");
- $this->assertNoFieldChecked("edit-card-2-$langcode-2");
- $this->assertRaw('Some dangerous & unescaped <strong>markup</strong>', 'Option text was properly filtered.');
-
-
- $edit = array(
- "card_2[$langcode][0]" => TRUE,
- "card_2[$langcode][1]" => FALSE,
- "card_2[$langcode][2]" => TRUE,
- );
- $this->backdropPost(NULL, $edit, t('Save'));
- $this->assertFieldValues($entity_init, 'card_2', $langcode, array(0, 2));
-
-
- $this->backdropGet('test-entity/manage/' . $entity->ftid . '/edit');
- $this->assertFieldChecked("edit-card-2-$langcode-0");
- $this->assertNoFieldChecked("edit-card-2-$langcode-1");
- $this->assertFieldChecked("edit-card-2-$langcode-2");
-
-
- $edit = array(
- "card_2[$langcode][0]" => TRUE,
- "card_2[$langcode][1]" => FALSE,
- "card_2[$langcode][2]" => FALSE,
- );
- $this->backdropPost(NULL, $edit, t('Save'));
- $this->assertFieldValues($entity_init, 'card_2', $langcode, array(0));
-
-
- $this->backdropGet('test-entity/manage/' . $entity->ftid . '/edit');
- $this->assertFieldChecked("edit-card-2-$langcode-0");
- $this->assertNoFieldChecked("edit-card-2-$langcode-1");
- $this->assertNoFieldChecked("edit-card-2-$langcode-2");
-
-
- $edit = array(
- "card_2[$langcode][0]" => TRUE,
- "card_2[$langcode][1]" => TRUE,
- "card_2[$langcode][2]" => TRUE,
- );
- $this->backdropPost(NULL, $edit, t('Save'));
- $this->assertText('this field cannot hold more than 2 values', 'Validation error was displayed.');
-
-
- $edit = array(
- "card_2[$langcode][0]" => FALSE,
- "card_2[$langcode][1]" => FALSE,
- "card_2[$langcode][2]" => FALSE,
- );
- $this->backdropPost(NULL, $edit, t('Save'));
-
- $this->assertFieldValues($entity_init, 'card_2', $langcode, array());
-
-
- $this->card_2['settings']['allowed_values'] = array(99 => 'Only allowed value');
- field_update_field($this->card_2);
- $instance['required'] = TRUE;
- field_update_instance($instance);
- $this->backdropGet('test-entity/manage/' . $entity->ftid . '/edit');
- $this->assertFieldChecked("edit-card-2-$langcode-99");
- }
-
-
- * Tests the 'options_select' widget (single select).
- */
- function testSelectListSingle() {
-
- $instance = array(
- 'field_name' => $this->card_1['field_name'],
- 'entity_type' => 'test_entity',
- 'bundle' => 'test_bundle',
- 'required' => TRUE,
- 'widget' => array(
- 'type' => 'options_select',
- ),
- );
- $instance = field_create_instance($instance);
- $langcode = LANGUAGE_NONE;
-
-
- $entity_init = field_test_create_entity();
- $entity = clone $entity_init;
- $entity->is_new = TRUE;
- field_test_entity_save($entity);
-
-
- $this->backdropGet('test-entity/manage/' . $entity->ftid . '/edit');
-
- $this->assertTrue($this->xpath('//select[@id=:id]//option[@value="_none" and text()=:label]', array(':id' => 'edit-card-1-' . $langcode, ':label' => t('- Select a value -'))), 'A required select list has a "Select a value" choice.');
-
-
- $this->assertNoOptionSelected("edit-card-1-$langcode", '_none');
- $this->assertNoOptionSelected("edit-card-1-$langcode", 0);
- $this->assertNoOptionSelected("edit-card-1-$langcode", 1);
- $this->assertNoOptionSelected("edit-card-1-$langcode", 2);
- $this->assertRaw('Some dangerous & unescaped markup', 'Option text was properly filtered.');
- $this->assertRaw('Some HTML encoded markup with < & >', 'HTML entities in option text were properly handled and not double-encoded');
-
-
- $edit = array("card_1[$langcode]" => '_none');
- $this->backdropPost(NULL, $edit, t('Save'));
- $this->assertRaw(t('!title field is required.', array('!title' => $instance['field_name'])), 'Cannot save a required field when selecting "none" from the select list.');
-
-
- $edit = array("card_1[$langcode]" => 0);
- $this->backdropPost(NULL, $edit, t('Save'));
- $this->assertFieldValues($entity_init, 'card_1', $langcode, array(0));
-
-
- $this->backdropGet('test-entity/manage/' . $entity->ftid . '/edit');
-
- $this->assertFalse($this->xpath('//select[@id=:id]//option[@value="_none"]', array(':id' => 'edit-card-1-' . $langcode)), 'A required select list with an actual value has no "none" choice.');
- $this->assertOptionSelected("edit-card-1-$langcode", 0);
- $this->assertNoOptionSelected("edit-card-1-$langcode", 1);
- $this->assertNoOptionSelected("edit-card-1-$langcode", 2);
-
-
- $instance['required'] = FALSE;
- field_update_instance($instance);
-
-
- $this->backdropGet('test-entity/manage/' . $entity->ftid . '/edit');
-
- $this->assertTrue($this->xpath('//select[@id=:id]//option[@value="_none" and text()=:label]', array(':id' => 'edit-card-1-' . $langcode, ':label' => t('- None -'))), 'A non-required select list has a "None" choice.');
-
- $edit = array("card_1[$langcode]" => '_none');
- $this->backdropPost('test-entity/manage/' . $entity->ftid . '/edit', $edit, t('Save'));
- $this->assertFieldValues($entity_init, 'card_1', $langcode, array());
-
-
-
- $this->card_1['settings']['allowed_values'] = array();
- $this->card_1['settings']['allowed_values_function'] = 'list_test_allowed_values_callback';
- field_update_field($this->card_1);
-
-
- $this->backdropGet('test-entity/manage/' . $entity->ftid . '/edit');
- $this->assertNoOptionSelected("edit-card-1-$langcode", 0);
- $this->assertNoOptionSelected("edit-card-1-$langcode", 1);
- $this->assertNoOptionSelected("edit-card-1-$langcode", 2);
- $this->assertRaw('Some dangerous & unescaped markup', 'Option text was properly filtered.');
- $this->assertRaw('Group 1', 'Option groups are displayed.');
-
-
- $edit = array("card_1[$langcode]" => 0);
- $this->backdropPost(NULL, $edit, t('Save'));
- $this->assertFieldValues($entity_init, 'card_1', $langcode, array(0));
-
-
- $this->backdropGet('test-entity/manage/' . $entity->ftid . '/edit');
- $this->assertOptionSelected("edit-card-1-$langcode", 0);
- $this->assertNoOptionSelected("edit-card-1-$langcode", 1);
- $this->assertNoOptionSelected("edit-card-1-$langcode", 2);
-
-
- $edit = array("card_1[$langcode]" => '_none');
- $this->backdropPost('test-entity/manage/' . $entity->ftid . '/edit', $edit, t('Save'));
- $this->assertFieldValues($entity_init, 'card_1', $langcode, array());
- }
-
-
- * Tests the 'options_select' widget (multiple select).
- */
- function testSelectListMultiple() {
-
- $instance = array(
- 'field_name' => $this->card_2['field_name'],
- 'entity_type' => 'test_entity',
- 'bundle' => 'test_bundle',
- 'widget' => array(
- 'type' => 'options_select',
- ),
- );
- $instance = field_create_instance($instance);
- $langcode = LANGUAGE_NONE;
-
-
- $entity_init = field_test_create_entity();
- $entity = clone $entity_init;
- $entity->is_new = TRUE;
- field_test_entity_save($entity);
-
-
- $this->backdropGet('test-entity/manage/' . $entity->ftid . '/edit');
- $this->assertNoOptionSelected("edit-card-2-$langcode", 0);
- $this->assertNoOptionSelected("edit-card-2-$langcode", 1);
- $this->assertNoOptionSelected("edit-card-2-$langcode", 2);
- $this->assertRaw('Some dangerous & unescaped markup', 'Option text was properly filtered.');
-
-
- $edit = array("card_2[$langcode][]" => array(0 => 0, 2 => 2));
- $this->backdropPost(NULL, $edit, t('Save'));
- $this->assertFieldValues($entity_init, 'card_2', $langcode, array(0, 2));
-
-
- $this->backdropGet('test-entity/manage/' . $entity->ftid . '/edit');
- $this->assertOptionSelected("edit-card-2-$langcode", 0);
- $this->assertNoOptionSelected("edit-card-2-$langcode", 1);
- $this->assertOptionSelected("edit-card-2-$langcode", 2);
-
-
- $edit = array("card_2[$langcode][]" => array(0 => 0));
- $this->backdropPost(NULL, $edit, t('Save'));
- $this->assertFieldValues($entity_init, 'card_2', $langcode, array(0));
-
-
- $this->backdropGet('test-entity/manage/' . $entity->ftid . '/edit');
- $this->assertOptionSelected("edit-card-2-$langcode", 0);
- $this->assertNoOptionSelected("edit-card-2-$langcode", 1);
- $this->assertNoOptionSelected("edit-card-2-$langcode", 2);
-
-
- $edit = array("card_2[$langcode][]" => array(0 => 0, 1 => 1, 2 => 2));
- $this->backdropPost(NULL, $edit, t('Save'));
- $this->assertText('this field cannot hold more than 2 values', 'Validation error was displayed.');
-
-
- $edit = array("card_2[$langcode][]" => array());
- $this->backdropPost(NULL, $edit, t('Save'));
- $this->assertFieldValues($entity_init, 'card_2', $langcode, array());
-
-
-
-
-
- $edit = array("card_2[$langcode][]" => array('_none' => '_none', 0 => 0));
- $this->backdropPost('test-entity/manage/' . $entity->ftid . '/edit', $edit, t('Save'));
- $this->assertFieldValues($entity_init, 'card_2', $langcode, array(0));
-
-
- $edit = array("card_2[$langcode][]" => array('_none' => '_none'));
- $this->backdropPost('test-entity/manage/' . $entity->ftid . '/edit', $edit, t('Save'));
- $this->assertFieldValues($entity_init, 'card_2', $langcode, array());
-
-
- $instance['required'] = TRUE;
- field_update_instance($instance);
- $this->backdropGet('test-entity/manage/' . $entity->ftid . '/edit');
- $this->assertFalse($this->xpath('//select[@id=:id]//option[@value=""]', array(':id' => 'edit-card-2-' . $langcode)), 'A required select list does not have an empty key.');
-
-
-
-
-
-
-
- $this->card_2['settings']['allowed_values'] = array();
- $this->card_2['settings']['allowed_values_function'] = 'list_test_allowed_values_callback';
- field_update_field($this->card_2);
- $instance['required'] = FALSE;
- field_update_instance($instance);
-
-
- $this->backdropGet('test-entity/manage/' . $entity->ftid . '/edit');
- $this->assertNoOptionSelected("edit-card-2-$langcode", 0);
- $this->assertNoOptionSelected("edit-card-2-$langcode", 1);
- $this->assertNoOptionSelected("edit-card-2-$langcode", 2);
- $this->assertRaw('Some dangerous & unescaped markup', 'Option text was properly filtered.');
- $this->assertRaw('Group 1', 'Option groups are displayed.');
-
-
- $edit = array("card_2[$langcode][]" => array(0 => 0));
- $this->backdropPost(NULL, $edit, t('Save'));
- $this->assertFieldValues($entity_init, 'card_2', $langcode, array(0));
-
-
- $this->backdropGet('test-entity/manage/' . $entity->ftid . '/edit');
- $this->assertOptionSelected("edit-card-2-$langcode", 0);
- $this->assertNoOptionSelected("edit-card-2-$langcode", 1);
- $this->assertNoOptionSelected("edit-card-2-$langcode", 2);
-
-
- $edit = array("card_2[$langcode][]" => array('_none' => '_none'));
- $this->backdropPost('test-entity/manage/' . $entity->ftid . '/edit', $edit, t('Save'));
- $this->assertFieldValues($entity_init, 'card_2', $langcode, array());
- }
-
-
- * Tests the 'options_onoff' widget.
- */
- function testOnOffCheckbox() {
-
- $instance = array(
- 'field_name' => $this->bool['field_name'],
- 'entity_type' => 'test_entity',
- 'bundle' => 'test_bundle',
- 'widget' => array(
- 'type' => 'options_onoff',
- ),
- );
- $instance = field_create_instance($instance);
- $langcode = LANGUAGE_NONE;
-
-
- $entity_init = field_test_create_entity();
- $entity = clone $entity_init;
- $entity->is_new = TRUE;
- field_test_entity_save($entity);
-
-
- $this->backdropGet('test-entity/manage/' . $entity->ftid . '/edit');
- $this->assertNoFieldChecked("edit-bool-$langcode");
- $this->assertRaw('Some dangerous & unescaped <strong>markup</strong>', 'Option text was properly filtered.');
-
-
- $edit = array("bool[$langcode]" => TRUE);
- $this->backdropPost(NULL, $edit, t('Save'));
- $this->assertFieldValues($entity_init, 'bool', $langcode, array(0));
-
-
- $this->backdropGet('test-entity/manage/' . $entity->ftid . '/edit');
- $this->assertFieldChecked("edit-bool-$langcode");
-
-
- $edit = array("bool[$langcode]" => FALSE);
- $this->backdropPost(NULL, $edit, t('Save'));
- $this->assertFieldValues($entity_init, 'bool', $langcode, array(1));
-
-
- $this->backdropGet('test-entity/manage/' . $entity->ftid . '/edit');
- $this->assertNoFieldChecked("edit-bool-$langcode");
-
-
- $admin_user = $this->backdropCreateUser(array('access content', 'administer content types', 'administer taxonomy', 'administer fields'));
- $this->backdropLogin($admin_user);
-
-
- $fieldUpdate = $this->bool;
- $fieldUpdate['settings']['allowed_values'] = array(0 => 0, 1 => 'MyOnValue');
- field_update_field($fieldUpdate);
- $instance = array(
- 'field_name' => $this->bool['field_name'],
- 'entity_type' => 'node',
- 'bundle' => 'page',
- 'widget' => array(
- 'type' => 'options_onoff',
- 'module' => 'options',
- ),
- );
- field_create_instance($instance);
-
-
- $fieldEditUrl = 'admin/structure/types/manage/page/fields/bool';
- $this->backdropGet($fieldEditUrl);
-
- $this->assertText(
- 'Use field label instead of the "On value" as label ',
- 'Display setting checkbox available.'
- );
-
- $this->assertFieldByXPath(
- '*//label[@for="edit-' . $this->bool['field_name'] . '-und" and text()="MyOnValue "]',
- TRUE,
- 'Default case shows "On value"'
- );
-
-
- $edit = array('instance[widget][settings][display_label]' => 1);
-
- $this->backdropPost($fieldEditUrl, $edit, t('Save settings'));
-
-
-
- $this->backdropGet($fieldEditUrl);
- $this->assertText(
- 'Use field label instead of the "On value" as label ',
- 'Display setting checkbox is available'
- );
- $this->assertFieldChecked(
- 'edit-instance-widget-settings-display-label',
- 'Display settings checkbox checked'
- );
- $this->assertFieldByXPath(
- '*//label[@for="edit-' . $this->bool['field_name'] . '-und" and text()="' . $this->bool['field_name'] . ' "]',
- TRUE,
- 'Display label changes label of the checkbox'
- );
- }
- }
-
- * Test an options select on a list field with a dynamic allowed values function.
- */
- class OptionsSelectDynamicValuesTestCase extends ListDynamicValuesTestCase {
-
- * Tests the 'options_select' widget (single select).
- */
- function testSelectListDynamic() {
-
- $this->entity->is_new = TRUE;
- field_test_entity_save($this->entity);
-
- $web_user = $this->backdropCreateUser(array('access field_test content', 'administer field_test content'));
- $this->backdropLogin($web_user);
-
-
- $expected_values = array(
- 'foo' => $this->randomName(),
- 'bar' => $this->randomName(),
- 'baz' => $this->randomName(),
- );
- state_set('list_test_dynamic_values', $expected_values);
-
-
- $this->backdropGet('test-entity/manage/' . $this->entity->ftid . '/edit');
- $options = $this->xpath('//select[@id="edit-test-list-und"]/option');
- $this->assertEqual(count($options), count($expected_values) + 1, 'Expected number of items found in dynamic select list.');
- foreach ($options as $option) {
- $key = (string) $option['value'];
- $value = (string) $option;
- if ($key !== '_none') {
- $this->assertTrue($expected_values[$key] === $value);
- }
- }
- }
- }