Commit 14bbd98a authored by catch's avatar catch

Issue #1592632 by tim.plunkett, sun, Berdir, Rob Loach, aspilicious: Merge...

Issue #1592632 by tim.plunkett, sun, Berdir, Rob Loach, aspilicious: Merge List field types into Options module.
parent 5511cdda
......@@ -374,3 +374,25 @@ function _update_7000_field_create_instance($field, &$instance) {
->fields($record)
->execute();
}
/**
* @addtogroup updates-7.x-to-8.x
* @{
*/
/**
* Reassign all list.module fields to be controlled by options.module.
*/
function field_update_8001() {
db_update('field_config')
->fields(array(
'module' => 'options',
))
->condition('module', 'list')
->execute();
}
/**
* @} End of "addtogroup updates-7.x-to-8.x".
* The next series of updates should start at 9000.
*/
......@@ -17,7 +17,7 @@ public static function getInfo() {
}
function setUp() {
parent::setUp(array('node', 'field_test', 'list'));
parent::setUp(array('node', 'field_test', 'options'));
$web_user = $this->drupalCreateUser(array('access field_test content', 'administer field_test content'));
$this->drupalLogin($web_user);
......
name = List
description = Defines list field types. Use with Options to create selection lists.
package = Core
version = VERSION
core = 8.x
dependencies[] = field
dependencies[] = options
files[] = tests/list.test
files[] = tests/list_dynamic.test
This diff is collapsed.
......@@ -2,25 +2,27 @@
/**
* @file
* Tests for list.module.
* Definition of Drupal\options\Tests\OptionsDynamicValuesTest.
*/
namespace Drupal\options\Tests;
use Drupal\field\Tests\FieldTestBase;
/**
* Sets up a List field for testing allowed values functions.
* Sets up a Options field for testing allowed values functions.
*/
class ListDynamicValuesTestCase extends FieldTestBase {
class OptionsDynamicValuesTest extends FieldTestBase {
function setUp() {
parent::setUp(array('list', 'field_test', 'list_test'));
parent::setUp(array('options', 'field_test', 'options_test'));
$this->field_name = 'test_list';
$this->field_name = 'test_options';
$this->field = array(
'field_name' => $this->field_name,
'type' => 'list_text',
'cardinality' => 1,
'settings' => array(
'allowed_values_function' => 'list_test_dynamic_values_callback',
'allowed_values_function' => 'options_test_dynamic_values_callback',
),
);
$this->field = field_create_field($this->field);
......@@ -38,7 +40,7 @@ function setUp() {
$this->test = array(
'id' => mt_rand(1, 10),
// Make sure this does not equal the ID so that
// list_test_dynamic_values_callback() always returns 4 values.
// options_test_dynamic_values_callback() always returns 4 values.
'vid' => mt_rand(20, 30),
'bundle' => 'test_bundle',
'label' => $this->randomName(),
......
<?php
/**
* @file
* Definition of Drupal\options\Tests\OptionsDynamicValuesValidationTest.
*/
namespace Drupal\options\Tests;
use Drupal\field\FieldValidationException;
/**
* Tests the Options field allowed values function.
*/
class OptionsDynamicValuesValidationTest extends OptionsDynamicValuesTest {
public static function getInfo() {
return array(
'name' => 'Options field dynamic values',
'description' => 'Test the Options field allowed values function.',
'group' => 'Field types',
);
}
/**
* Test that allowed values function gets the entity.
*/
function testDynamicAllowedValues() {
// Verify that the test passes against every value we had.
foreach ($this->test as $key => $value) {
$this->entity->test_options[LANGUAGE_NOT_SPECIFIED][0]['value'] = $value;
try {
field_attach_validate('test_entity', $this->entity);
$this->pass("$key should pass");
}
catch (FieldValidationException $e) {
// This will display as an exception, no need for a separate error.
throw($e);
}
}
// Now verify that the test does not pass against anything else.
foreach ($this->test as $key => $value) {
$this->entity->test_options[LANGUAGE_NOT_SPECIFIED][0]['value'] = is_numeric($value) ? (100 - $value) : ('X' . $value);
$pass = FALSE;
try {
field_attach_validate('test_entity', $this->entity);
}
catch (FieldValidationException $e) {
$pass = TRUE;
}
$this->assertTrue($pass, $key . ' should not pass');
}
}
}
<?php
/**
* @file
* Definition of Drupal\options\Tests\OptionsFieldTest.
*/
namespace Drupal\options\Tests;
use Drupal\field\FieldException;
use Drupal\field\Tests\FieldTestBase;
/**
* Tests for the 'Options' field types.
*/
class OptionsFieldTest extends FieldTestBase {
public static function getInfo() {
return array(
'name' => 'Options field',
'description' => 'Test the Options field type.',
'group' => 'Field types',
);
}
function setUp() {
parent::setUp(array('options', 'field_test'));
$this->field_name = 'test_options';
$this->field = array(
'field_name' => $this->field_name,
'type' => 'list_integer',
'cardinality' => 1,
'settings' => array(
'allowed_values' => array(1 => 'One', 2 => 'Two', 3 => 'Three'),
),
);
$this->field = field_create_field($this->field);
$this->instance = array(
'field_name' => $this->field_name,
'entity_type' => 'test_entity',
'bundle' => 'test_bundle',
'widget' => array(
'type' => 'options_buttons',
),
);
$this->instance = field_create_instance($this->instance);
}
/**
* Test that allowed values can be updated.
*/
function testUpdateAllowedValues() {
$langcode = LANGUAGE_NOT_SPECIFIED;
// All three options appear.
$entity = field_test_create_stub_entity();
$form = drupal_get_form('field_test_entity_form', $entity);
$this->assertTrue(!empty($form[$this->field_name][$langcode][1]), t('Option 1 exists'));
$this->assertTrue(!empty($form[$this->field_name][$langcode][2]), t('Option 2 exists'));
$this->assertTrue(!empty($form[$this->field_name][$langcode][3]), t('Option 3 exists'));
// Use one of the values in an actual entity, and check that this value
// cannot be removed from the list.
$entity = field_test_create_stub_entity();
$entity->{$this->field_name}[$langcode][0] = array('value' => 1);
field_test_entity_save($entity);
$this->field['settings']['allowed_values'] = array(2 => 'Two');
try {
field_update_field($this->field);
$this->fail(t('Cannot update a list field to not include keys with existing data.'));
}
catch (FieldException $e) {
$this->pass(t('Cannot update a list field to not include keys with existing data.'));
}
// Empty the value, so that we can actually remove the option.
$entity->{$this->field_name}[$langcode] = array();
field_test_entity_save($entity);
// Removed options do not appear.
$this->field['settings']['allowed_values'] = array(2 => 'Two');
field_update_field($this->field);
$entity = field_test_create_stub_entity();
$form = drupal_get_form('field_test_entity_form', $entity);
$this->assertTrue(empty($form[$this->field_name][$langcode][1]), t('Option 1 does not exist'));
$this->assertTrue(!empty($form[$this->field_name][$langcode][2]), t('Option 2 exists'));
$this->assertTrue(empty($form[$this->field_name][$langcode][3]), t('Option 3 does not exist'));
// Completely new options appear.
$this->field['settings']['allowed_values'] = array(10 => 'Update', 20 => 'Twenty');
field_update_field($this->field);
$form = drupal_get_form('field_test_entity_form', $entity);
$this->assertTrue(empty($form[$this->field_name][$langcode][1]), t('Option 1 does not exist'));
$this->assertTrue(empty($form[$this->field_name][$langcode][2]), t('Option 2 does not exist'));
$this->assertTrue(empty($form[$this->field_name][$langcode][3]), t('Option 3 does not exist'));
$this->assertTrue(!empty($form[$this->field_name][$langcode][10]), t('Option 10 exists'));
$this->assertTrue(!empty($form[$this->field_name][$langcode][20]), t('Option 20 exists'));
// Options are reset when a new field with the same name is created.
field_delete_field($this->field_name);
unset($this->field['id']);
$this->field['settings']['allowed_values'] = array(1 => 'One', 2 => 'Two', 3 => 'Three');
$this->field = field_create_field($this->field);
$this->instance = array(
'field_name' => $this->field_name,
'entity_type' => 'test_entity',
'bundle' => 'test_bundle',
'widget' => array(
'type' => 'options_buttons',
),
);
$this->instance = field_create_instance($this->instance);
$entity = field_test_create_stub_entity();
$form = drupal_get_form('field_test_entity_form', $entity);
$this->assertTrue(!empty($form[$this->field_name][$langcode][1]), t('Option 1 exists'));
$this->assertTrue(!empty($form[$this->field_name][$langcode][2]), t('Option 2 exists'));
$this->assertTrue(!empty($form[$this->field_name][$langcode][3]), t('Option 3 exists'));
}
}
......@@ -2,17 +2,15 @@
/**
* @file
* Definition of Drupal\options\OptionsWidgetsTest.
* Definition of Drupal\options\Tests\OptionsSelectDynamicValuesTest.
*/
namespace Drupal\options\Tests;
use ListDynamicValuesTestCase;
/**
* Test an options select on a list field with a dynamic allowed values function.
* Tests an options select with a dynamic allowed values function.
*/
class OptionsSelectDynamicValuesTest extends ListDynamicValuesTestCase {
class OptionsSelectDynamicValuesTest extends OptionsDynamicValuesTest {
public static function getInfo() {
return array(
'name' => 'Options select dynamic values',
......@@ -34,7 +32,7 @@ function testSelectListDynamic() {
// Display form.
$this->drupalGet('test-entity/manage/' . $this->entity->ftid . '/edit');
$options = $this->xpath('//select[@id="edit-test-list-und"]/option');
$options = $this->xpath('//select[@id="edit-test-options-und"]/option');
$this->assertEqual(count($options), count($this->test) + 1);
foreach ($options as $option) {
$value = (string) $option['value'];
......
......@@ -2,7 +2,7 @@
/**
* @file
* Definition of Drupal\options\OptionsWidgetsTest.
* Definition of Drupal\options\Tests\OptionsWidgetsTest.
*/
namespace Drupal\options\Tests;
......@@ -22,7 +22,7 @@ public static function getInfo() {
}
function setUp() {
parent::setUp(array('list', 'field_test', 'list_test', 'taxonomy', 'field_ui'));
parent::setUp(array('options', 'field_test', 'options_test', 'taxonomy', 'field_ui'));
// Field with cardinality 1.
$this->card_1 = array(
......@@ -275,7 +275,7 @@ function testSelectListSingle() {
// Test optgroups.
$this->card_1['settings']['allowed_values'] = array();
$this->card_1['settings']['allowed_values_function'] = 'list_test_allowed_values_callback';
$this->card_1['settings']['allowed_values_function'] = 'options_test_allowed_values_callback';
field_update_field($this->card_1);
// Display form: with no field data, nothing is selected
......@@ -390,7 +390,7 @@ function testSelectListMultiple() {
// Use a callback function defining optgroups.
$this->card_2['settings']['allowed_values'] = array();
$this->card_2['settings']['allowed_values_function'] = 'list_test_allowed_values_callback';
$this->card_2['settings']['allowed_values_function'] = 'options_test_allowed_values_callback';
field_update_field($this->card_2);
$instance['required'] = FALSE;
field_update_instance($instance);
......
......@@ -2,13 +2,13 @@
/**
* @file
* Install, update and uninstall functions for the list module.
* Install, update and uninstall functions for the options module.
*/
/**
* Implements hook_field_schema().
*/
function list_field_schema($field) {
function options_field_schema($field) {
switch ($field['type']) {
case 'list_text':
$columns = array(
......
name = "List test"
description = "Support module for the List module tests."
name = "Options test"
description = "Support module for the Options module tests."
core = 8.x
package = Testing
version = VERSION
......
......@@ -8,7 +8,7 @@
/**
* Allowed values callback.
*/
function list_test_allowed_values_callback($field) {
function options_test_allowed_values_callback($field) {
$values = array(
'Group 1' => array(
0 => 'Zero',
......@@ -25,7 +25,7 @@ function list_test_allowed_values_callback($field) {
/**
* An entity-bound allowed values callback.
*/
function list_test_dynamic_values_callback($field, $instance, $entity_type, $entity, &$cacheable) {
function options_test_dynamic_values_callback($field, $instance, $entity_type, $entity, &$cacheable) {
$cacheable = FALSE;
// We need the values of the entity as keys.
return drupal_map_assoc(array_merge(array($entity->ftlabel), entity_extract_ids($entity_type, $entity)));
......
......@@ -22,7 +22,7 @@ public static function getInfo() {
}
function setUp() {
parent::setUp(array('field_ui', 'field_test', 'text', 'list'));
parent::setUp(array('field_ui', 'field_test', 'text', 'options'));
// Create Article node type.
$this->drupalCreateContentType(array('type' => 'page', 'name' => 'Basic page'));
......
......@@ -10,7 +10,6 @@ dependencies[] = contextual
dependencies[] = dashboard
dependencies[] = help
dependencies[] = image
dependencies[] = list
dependencies[] = menu
dependencies[] = number
dependencies[] = options
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment