Skip to content
Snippets Groups Projects
Commit 2e0ecd55 authored by Angie Byron's avatar Angie Byron
Browse files

Issue #1839064 by Berdir: Implement the new entity field API for the list field type.

parent 98e1d459
No related branches found
No related tags found
No related merge requests found
......@@ -8,19 +8,19 @@
namespace Drupal\options\Tests;
use Drupal\field\FieldException;
use Drupal\field\Tests\FieldTestBase;
use Drupal\field\Tests\FieldItemUnitTestBase;
/**
* Tests for the 'Options' field types.
*/
class OptionsFieldTest extends FieldTestBase {
class OptionsFieldTest extends FieldItemUnitTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('options', 'field_test');
public static $modules = array('options', 'entity_test');
public static function getInfo() {
return array(
......@@ -33,6 +33,10 @@ public static function getInfo() {
function setUp() {
parent::setUp();
$this->installSchema('system', 'menu_router');
$this->installSchema('system', 'variable');
$this->field_name = 'test_options';
$this->field = array(
'field_name' => $this->field_name,
......@@ -46,8 +50,8 @@ function setUp() {
$this->instance = array(
'field_name' => $this->field_name,
'entity_type' => 'test_entity',
'bundle' => 'test_bundle',
'entity_type' => 'entity_test',
'bundle' => 'entity_test',
'widget' => array(
'type' => 'options_buttons',
),
......@@ -62,7 +66,7 @@ function testUpdateAllowedValues() {
$langcode = LANGUAGE_NOT_SPECIFIED;
// All three options appear.
$entity = field_test_create_entity();
$entity = entity_create('entity_test', array());
$form = entity_get_form($entity);
$this->assertTrue(!empty($form[$this->field_name][$langcode][1]), 'Option 1 exists');
$this->assertTrue(!empty($form[$this->field_name][$langcode][2]), 'Option 2 exists');
......@@ -70,9 +74,9 @@ function testUpdateAllowedValues() {
// Use one of the values in an actual entity, and check that this value
// cannot be removed from the list.
$entity = field_test_create_entity();
$entity->{$this->field_name}[$langcode][0] = array('value' => 1);
field_test_entity_save($entity);
$entity = entity_create('entity_test', array());
$entity->{$this->field_name}->value = 1;
$entity->save();
$this->field['settings']['allowed_values'] = array(2 => 'Two');
try {
field_update_field($this->field);
......@@ -82,13 +86,13 @@ function testUpdateAllowedValues() {
$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);
unset($entity->{$this->field_name});
$entity->save();
// Removed options do not appear.
$this->field['settings']['allowed_values'] = array(2 => 'Two');
field_update_field($this->field);
$entity = field_test_create_entity();
$entity = entity_create('entity_test', array());
$form = entity_get_form($entity);
$this->assertTrue(empty($form[$this->field_name][$langcode][1]), 'Option 1 does not exist');
$this->assertTrue(!empty($form[$this->field_name][$langcode][2]), 'Option 2 exists');
......@@ -111,14 +115,14 @@ function testUpdateAllowedValues() {
$this->field = field_create_field($this->field);
$this->instance = array(
'field_name' => $this->field_name,
'entity_type' => 'test_entity',
'bundle' => 'test_bundle',
'entity_type' => 'entity_test',
'bundle' => 'entity_test',
'widget' => array(
'type' => 'options_buttons',
),
);
$this->instance = field_create_instance($this->instance);
$entity = field_test_create_entity();
$entity = entity_create('entity_test', array());
$form = entity_get_form($entity);
$this->assertTrue(!empty($form[$this->field_name][$langcode][1]), 'Option 1 exists');
$this->assertTrue(!empty($form[$this->field_name][$langcode][2]), 'Option 2 exists');
......
......@@ -4,3 +4,6 @@ package = Core
version = VERSION
core = 8.x
dependencies[] = field
dependencies[] = text
dependencies[] = number
......@@ -44,6 +44,7 @@ function options_field_info() {
'settings' => array('allowed_values' => array(), 'allowed_values_function' => ''),
'default_widget' => 'options_select',
'default_formatter' => 'list_default',
'field item class' => '\Drupal\number\Type\IntegerItem',
),
'list_float' => array(
'label' => t('List (float)'),
......@@ -51,6 +52,7 @@ function options_field_info() {
'settings' => array('allowed_values' => array(), 'allowed_values_function' => ''),
'default_widget' => 'options_select',
'default_formatter' => 'list_default',
'field item class' => '\Drupal\number\Type\FloatItem',
),
'list_text' => array(
'label' => t('List (text)'),
......@@ -58,6 +60,7 @@ function options_field_info() {
'settings' => array('allowed_values' => array(), 'allowed_values_function' => ''),
'default_widget' => 'options_select',
'default_formatter' => 'list_default',
'field item class' => '\Drupal\text\Type\TextItem',
),
'list_boolean' => array(
'label' => t('Boolean'),
......@@ -65,6 +68,7 @@ function options_field_info() {
'settings' => array('allowed_values' => array(), 'allowed_values_function' => ''),
'default_widget' => 'options_buttons',
'default_formatter' => 'list_default',
'field item class' => '\Drupal\number\Type\IntegerItem',
),
);
}
......
......@@ -136,9 +136,10 @@ function testModuleEnableOrder() {
// module_test creates a dependency chain:
// - forum depends on taxonomy, comment, history, and ban (via module_test)
// - taxonomy depends on options
// - options depends on number
// - ban depends on php (via module_test)
// The correct enable order is:
$expected_order = array('php', 'ban', 'comment', 'history', 'options', 'taxonomy', 'forum');
$expected_order = array('php', 'ban', 'comment', 'history', 'number', 'options', 'taxonomy', 'forum');
// Enable the modules through the UI, verifying that the dependency chain
// is correct.
......@@ -146,15 +147,16 @@ function testModuleEnableOrder() {
$edit['modules[Core][forum][enable]'] = 'forum';
$this->drupalPost('admin/modules', $edit, t('Save configuration'));
$this->assertModules(array('forum'), FALSE);
$this->assertText(t('You must enable the History, Taxonomy, Options, Comment, Ban, PHP Filter modules to install Forum.'));
$this->assertText(t('You must enable the History, Taxonomy, Options, Number, Comment, Ban, PHP Filter modules to install Forum.'));
$edit['modules[Core][history][enable]'] = 'history';
$edit['modules[Core][options][enable]'] = 'options';
$edit['modules[Core][number][enable]'] = 'number';
$edit['modules[Core][taxonomy][enable]'] = 'taxonomy';
$edit['modules[Core][comment][enable]'] = 'comment';
$edit['modules[Core][ban][enable]'] = 'ban';
$edit['modules[Core][php][enable]'] = 'php';
$this->drupalPost('admin/modules', $edit, t('Save configuration'));
$this->assertModules(array('forum', 'ban', 'php', 'comment', 'history', 'taxonomy', 'options'), TRUE);
$this->assertModules(array('forum', 'ban', 'php', 'comment', 'history', 'taxonomy', 'options', 'number'), TRUE);
// Check the actual order which is saved by module_test_modules_enabled().
$module_order = state()->get('system_test.module_enable_order') ?: array();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment