Drupal 8.0.0 beta 12. More info: https://www.drupal.org/node/2514176

This commit is contained in:
Pantheon Automation 2015-08-17 17:00:26 -07:00 committed by Greg Anderson
commit 9921556621
13277 changed files with 1459781 additions and 0 deletions

View file

@ -0,0 +1,39 @@
<?php
/**
* @file
* Contains \Drupal\options\Tests\OptionsDynamicValuesApiTest.
*/
namespace Drupal\options\Tests;
/**
* Tests the options allowed values api.
*
* @group options
*/
class OptionsDynamicValuesApiTest extends OptionsDynamicValuesTestBase {
/**
* Tests options_allowed_values().
*
* @see options_test_dynamic_values_callback()
*/
public function testOptionsAllowedValues() {
// Test allowed values without passed $items.
$values = options_allowed_values($this->fieldStorage);
$this->assertEqual([], $values);
$values = options_allowed_values($this->fieldStorage, $this->entity);
$expected_values = array(
$this->entity->label(),
$this->entity->url(),
$this->entity->uuid(),
$this->entity->bundle(),
);
$expected_values = array_combine($expected_values, $expected_values);
$this->assertEqual($expected_values, $values);
}
}

View file

@ -0,0 +1,80 @@
<?php
/**
* @file
* Contains \Drupal\options\Tests\OptionsDynamicValuesTestBase.
*/
namespace Drupal\options\Tests;
use Drupal\field\Tests\FieldTestBase;
/**
* Base class for testing allowed values of options fields.
*/
abstract class OptionsDynamicValuesTestBase extends FieldTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = ['options', 'entity_test', 'options_test'];
/**
* The created entity.
*
* @var \Drupal\Core\Entity\Entity
*/
protected $entity;
/**
* The field storage.
*
* @var \Drupal\Core\Field\FieldStorageDefinitionInterface
*/
protected $fieldStorage;
protected function setUp() {
parent::setUp();
$field_name = 'test_options';
$this->fieldStorage = entity_create('field_storage_config', [
'field_name' => $field_name,
'entity_type' => 'entity_test_rev',
'type' => 'list_string',
'cardinality' => 1,
'settings' => [
'allowed_values_function' => 'options_test_dynamic_values_callback',
],
]);
$this->fieldStorage->save();
$this->field = entity_create('field_config', [
'field_name' => $field_name,
'entity_type' => 'entity_test_rev',
'bundle' => 'entity_test_rev',
'required' => TRUE,
])->save();
entity_get_form_display('entity_test_rev', 'entity_test_rev', 'default')
->setComponent($field_name, [
'type' => 'options_select',
])
->save();
// Create an entity and prepare test data that will be used by
// options_test_dynamic_values_callback().
$values = [
'user_id' => mt_rand(1, 10),
'name' => $this->randomMachineName(),
];
$this->entity = entity_create('entity_test_rev', $values);
$this->entity->save();
$this->test = [
'label' => $this->entity->label(),
'uuid' => $this->entity->uuid(),
'bundle' => $this->entity->bundle(),
'uri' => $this->entity->url(),
];
}
}

View file

@ -0,0 +1,35 @@
<?php
/**
* @file
* Contains \Drupal\options\Tests\OptionsDynamicValuesValidationTest.
*/
namespace Drupal\options\Tests;
/**
* Tests the Options field allowed values function.
*
* @group options
*/
class OptionsDynamicValuesValidationTest extends OptionsDynamicValuesTestBase {
/**
* Test that allowed values function gets the entity.
*/
function testDynamicAllowedValues() {
// Verify that validation passes against every value we had.
foreach ($this->test as $key => $value) {
$this->entity->test_options->value = $value;
$violations = $this->entity->test_options->validate();
$this->assertEqual(count($violations), 0, "$key is a valid value");
}
// Now verify that validation does not pass against anything else.
foreach ($this->test as $key => $value) {
$this->entity->test_options->value = is_numeric($value) ? (100 - $value) : ('X' . $value);
$violations = $this->entity->test_options->validate();
$this->assertEqual(count($violations), 1, "$key is not a valid value");
}
}
}

View file

@ -0,0 +1,100 @@
<?php
/**
* @file
* Contains \Drupal\options\Tests\OptionsFieldTest.
*/
namespace Drupal\options\Tests;
use Drupal\Core\Entity\Exception\FieldStorageDefinitionUpdateForbiddenException;
/**
* Tests for the 'Options' field types.
*
* @group options
*/
class OptionsFieldTest extends OptionsFieldUnitTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('options');
/**
* Test that allowed values can be updated.
*/
function testUpdateAllowedValues() {
// All three options appear.
$entity = entity_create('entity_test');
$form = \Drupal::service('entity.form_builder')->getForm($entity);
$this->assertTrue(!empty($form[$this->fieldName]['widget'][1]), 'Option 1 exists');
$this->assertTrue(!empty($form[$this->fieldName]['widget'][2]), 'Option 2 exists');
$this->assertTrue(!empty($form[$this->fieldName]['widget'][3]), 'Option 3 exists');
// Use one of the values in an actual entity, and check that this value
// cannot be removed from the list.
$entity = entity_create('entity_test');
$entity->{$this->fieldName}->value = 1;
$entity->save();
$this->fieldStorage->setSetting('allowed_values', [2 => 'Two']);
try {
$this->fieldStorage->save();
$this->fail(t('Cannot update a list field storage to not include keys with existing data.'));
}
catch (FieldStorageDefinitionUpdateForbiddenException $e) {
$this->pass(t('Cannot update a list field storage to not include keys with existing data.'));
}
// Empty the value, so that we can actually remove the option.
unset($entity->{$this->fieldName});
$entity->save();
// Removed options do not appear.
$this->fieldStorage->setSetting('allowed_values', [2 => 'Two']);
$this->fieldStorage->save();
$entity = entity_create('entity_test');
$form = \Drupal::service('entity.form_builder')->getForm($entity);
$this->assertTrue(empty($form[$this->fieldName]['widget'][1]), 'Option 1 does not exist');
$this->assertTrue(!empty($form[$this->fieldName]['widget'][2]), 'Option 2 exists');
$this->assertTrue(empty($form[$this->fieldName]['widget'][3]), 'Option 3 does not exist');
// Completely new options appear.
$this->fieldStorage->setSetting('allowed_values', [10 => 'Update', 20 => 'Twenty']);
$this->fieldStorage->save();
// The entity holds an outdated field object with the old allowed values
// setting, so we need to reinitialize the entity object.
$entity = entity_create('entity_test');
$form = \Drupal::service('entity.form_builder')->getForm($entity);
$this->assertTrue(empty($form[$this->fieldName]['widget'][1]), 'Option 1 does not exist');
$this->assertTrue(empty($form[$this->fieldName]['widget'][2]), 'Option 2 does not exist');
$this->assertTrue(empty($form[$this->fieldName]['widget'][3]), 'Option 3 does not exist');
$this->assertTrue(!empty($form[$this->fieldName]['widget'][10]), 'Option 10 exists');
$this->assertTrue(!empty($form[$this->fieldName]['widget'][20]), 'Option 20 exists');
// Options are reset when a new field with the same name is created.
$this->fieldStorage->delete();
entity_create('field_storage_config', $this->fieldStorageDefinition)->save();
entity_create('field_config', array(
'field_name' => $this->fieldName,
'entity_type' => 'entity_test',
'bundle' => 'entity_test',
))->save();
entity_get_form_display('entity_test', 'entity_test', 'default')
->setComponent($this->fieldName, array(
'type' => 'options_buttons',
))
->save();
$entity = entity_create('entity_test');
$form = \Drupal::service('entity.form_builder')->getForm($entity);
$this->assertTrue(!empty($form[$this->fieldName]['widget'][1]), 'Option 1 exists');
$this->assertTrue(!empty($form[$this->fieldName]['widget'][2]), 'Option 2 exists');
$this->assertTrue(!empty($form[$this->fieldName]['widget'][3]), 'Option 3 exists');
// Test the generateSampleValue() method.
$entity = entity_create('entity_test');
$entity->{$this->fieldName}->generateSampleItems();
$this->entityValidateAndSave($entity);
}
}

View file

@ -0,0 +1,358 @@
<?php
/**
* @file
* Contains \Drupal\options\Tests\OptionsFieldUITest.
*/
namespace Drupal\options\Tests;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field\Tests\FieldTestBase;
/**
* Tests the Options field UI functionality.
*
* @group options
*/
class OptionsFieldUITest extends FieldTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('node', 'options', 'field_test', 'taxonomy', 'field_ui');
/**
* The name of the created content type.
*
* @var string
*/
protected $typeName;
/**
* Machine name of the created content type.
*
* @var string
*/
protected $type;
/**
* Name of the option field.
*
* @var string
*/
protected $fieldName;
/**
* Admin path to manage field storage settings.
*
* @var string
*/
protected $adminPath;
protected function setUp() {
parent::setUp();
// Create test user.
$admin_user = $this->drupalCreateUser(['access content', 'administer taxonomy', 'access administration pages', 'administer site configuration', 'administer content types', 'administer nodes', 'bypass node access', 'administer node fields', 'administer node display']);
$this->drupalLogin($admin_user);
// Create content type, with underscores.
$this->typeName = 'test_' . strtolower($this->randomMachineName());
$type = $this->drupalCreateContentType(['name' => $this->typeName, 'type' => $this->typeName]);
$this->type = $type->id();
}
/**
* Options (integer) : test 'allowed values' input.
*/
function testOptionsAllowedValuesInteger() {
$this->fieldName = 'field_options_integer';
$this->createOptionsField('list_integer');
// Flat list of textual values.
$string = "Zero\nOne";
$array = array('0' => 'Zero', '1' => 'One');
$this->assertAllowedValuesInput($string, $array, 'Unkeyed lists are accepted.');
// Explicit integer keys.
$string = "0|Zero\n2|Two";
$array = array('0' => 'Zero', '2' => 'Two');
$this->assertAllowedValuesInput($string, $array, 'Integer keys are accepted.');
// Check that values can be added and removed.
$string = "0|Zero\n1|One";
$array = array('0' => 'Zero', '1' => 'One');
$this->assertAllowedValuesInput($string, $array, 'Values can be added and removed.');
// Non-integer keys.
$this->assertAllowedValuesInput("1.1|One", 'keys must be integers', 'Non integer keys are rejected.');
$this->assertAllowedValuesInput("abc|abc", 'keys must be integers', 'Non integer keys are rejected.');
// Mixed list of keyed and unkeyed values.
$this->assertAllowedValuesInput("Zero\n1|One", 'invalid input', 'Mixed lists are rejected.');
// Create a node with actual data for the field.
$settings = array(
'type' => $this->type,
$this->fieldName => array(array('value' => 1)),
);
$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\n1|One\n2|Two";
$array = array('0' => 'Zero', '1' => 'One', '2' => 'Two');
$this->assertAllowedValuesInput($string, $array, 'Values can be added.');
$string = "0|Zero\n1|One";
$array = array('0' => 'Zero', '1' => 'One');
$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();
$string = "0|Zero";
$array = array('0' => 'Zero');
$this->assertAllowedValuesInput($string, $array, 'Values not in use can be removed.');
// Check that the same key can only be used once.
$string = "0|Zero\n0|One";
$array = array('0' => 'One');
$this->assertAllowedValuesInput($string, $array, 'Same value cannot be used multiple times.');
}
/**
* Options (float) : test 'allowed values' input.
*/
function testOptionsAllowedValuesFloat() {
$this->fieldName = '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->fieldName => array(array('value' => .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();
$string = "0|Zero";
$array = array('0' => 'Zero');
$this->assertAllowedValuesInput($string, $array, 'Values not in use can be removed.');
// Check that the same key can only be used once.
$string = "0.5|Point five\n0.5|Half";
$array = array('0.5' => 'Half');
$this->assertAllowedValuesInput($string, $array, 'Same value cannot be used multiple times.');
// Check that different forms of the same float value cannot be used.
$string = "0|Zero\n.5|Point five\n0.5|Half";
$array = array('0' => 'Zero', '0.5' => 'Half');
$this->assertAllowedValuesInput($string, $array, 'Different forms of the same value cannot be used.');
}
/**
* Options (text) : test 'allowed values' input.
*/
function testOptionsAllowedValuesText() {
$this->fieldName = 'field_options_text';
$this->createOptionsField('list_string');
// 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->randomMachineName(256) . "|One", 'each key must be a string at most 255 characters long', 'Overly long keys are rejected.');
// Create a node with actual data for the field.
$settings = array(
'type' => $this->type,
$this->fieldName => array(array('value' => 'One')),
);
$node = $this->drupalCreateNode($settings);
// Check that flat lists of values are still accepted once the field has
// data.
$string = "Zero\nOne";
$array = array('Zero' => 'Zero', 'One' => '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 = "Zero\nOne\nTwo";
$array = array('Zero' => 'Zero', 'One' => 'One', 'Two' => 'Two');
$this->assertAllowedValuesInput($string, $array, 'Values can be added.');
$string = "Zero\nOne";
$array = array('Zero' => 'Zero', 'One' => '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();
$string = "Zero";
$array = array('Zero' => 'Zero');
$this->assertAllowedValuesInput($string, $array, 'Values not in use can be removed.');
// Check that string values with dots can be used.
$string = "Zero\nexample.com|Example";
$array = array('Zero' => 'Zero', 'example.com' => 'Example');
$this->assertAllowedValuesInput($string, $array, 'String value with dot is supported.');
// Check that the same key can only be used once.
$string = "zero|Zero\nzero|One";
$array = array('zero' => 'One');
$this->assertAllowedValuesInput($string, $array, 'Same value cannot be used multiple times.');
}
/**
* Options (text) : test 'trimmed values' input.
*/
function testOptionsTrimmedValuesText() {
$this->fieldName = 'field_options_trimmed_text';
$this->createOptionsField('list_string');
// Explicit keys.
$string = "zero |Zero\none | One";
$array = array('zero' => 'Zero', 'one' => 'One');
$this->assertAllowedValuesInput($string, $array, 'Explicit keys are accepted and trimmed.');
}
/**
* Helper function to create list field of a given type.
*
* @param string $type
* 'list_integer', 'list_float' or 'list_string'
*/
protected function createOptionsField($type) {
// Create a field.
entity_create('field_storage_config', array(
'field_name' => $this->fieldName,
'entity_type' => 'node',
'type' => $type,
))->save();
entity_create('field_config', array(
'field_name' => $this->fieldName,
'entity_type' => 'node',
'bundle' => $this->type,
))->save();
entity_get_form_display('node', $this->type, 'default')->setComponent($this->fieldName)->save();
$this->adminPath = 'admin/structure/types/manage/' . $this->type . '/fields/node.' . $this->type . '.' . $this->fieldName . '/storage';
}
/**
* Tests a string input for the 'allowed values' form element.
*
* @param $input_string
* The input string, in the pipe-linefeed format expected by the form
* element.
* @param $result
* Either an expected resulting array in
* $field->getSetting('allowed_values'), or an expected error message.
* @param $message
* Message to display.
*/
function assertAllowedValuesInput($input_string, $result, $message) {
$edit = array('settings[allowed_values]' => $input_string);
$this->drupalPostForm($this->adminPath, $edit, t('Save field settings'));
$this->assertNoRaw('&amp;lt;', 'The page does not have double escaped HTML tags.');
if (is_string($result)) {
$this->assertText($result, $message);
}
else {
$field_storage = FieldStorageConfig::loadByName('node', $this->fieldName);
$this->assertIdentical($field_storage->getSetting('allowed_values'), $result, $message);
}
}
/**
* Tests normal and key formatter display on node display.
*/
function testNodeDisplay() {
$this->fieldName = strtolower($this->randomMachineName());
$this->createOptionsField('list_integer');
$node = $this->drupalCreateNode(array('type' => $this->type));
$on = $this->randomMachineName();
$off = $this->randomMachineName();
$edit = array(
'settings[allowed_values]' =>
"1|$on
0|$off",
);
$this->drupalPostForm($this->adminPath, $edit, t('Save field settings'));
$this->assertText(format_string('Updated field !field_name field settings.', array('!field_name' => $this->fieldName)), "The 'On' and 'Off' form fields work for boolean fields.");
// Select a default value.
$edit = array(
$this->fieldName => '1',
);
$this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save and keep published'));
// Check the node page and see if the values are correct.
$file_formatters = array('list_default', 'list_key');
foreach ($file_formatters as $formatter) {
$edit = array(
"fields[$this->fieldName][type]" => $formatter,
);
$this->drupalPostForm('admin/structure/types/manage/' . $this->typeName . '/display', $edit, t('Save'));
$this->drupalGet('node/' . $node->id());
if ($formatter == 'list_default') {
$output = $on;
}
else {
$output = '1';
}
$elements = $this->xpath('//div[text()="' . $output . '"]');
$this->assertEqual(count($elements), 1, 'Correct options found.');
}
}
}

View file

@ -0,0 +1,85 @@
<?php
/**
* @file
* Contains \Drupal\options\Tests\OptionsFieldUnitTestBase.
*/
namespace Drupal\options\Tests;
use Drupal\field\Tests\FieldUnitTestBase;
/**
* Base class for Options module integration tests.
*/
abstract class OptionsFieldUnitTestBase extends FieldUnitTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('options');
/**
* The field name used in the test.
*
* @var string
*/
protected $fieldName = 'test_options';
/**
* The field storage definition used to created the field storage.
*
* @var array
*/
protected $fieldStorageDefinition;
/**
* The list field storage used in the test.
*
* @var \Drupal\field\Entity\FieldStorageConfig
*/
protected $fieldStorage;
/**
* The list field used in the test.
*
* @var \Drupal\field\Entity\FieldConfig
*/
protected $field;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->container->get('router.builder')->rebuild();
$this->fieldStorageDefinition = array(
'field_name' => $this->fieldName,
'entity_type' => 'entity_test',
'type' => 'list_integer',
'cardinality' => 1,
'settings' => array(
'allowed_values' => array(1 => 'One', 2 => 'Two', 3 => 'Three'),
),
);
$this->fieldStorage = entity_create('field_storage_config', $this->fieldStorageDefinition);
$this->fieldStorage->save();
$this->field = entity_create('field_config', array(
'field_storage' => $this->fieldStorage,
'bundle' => 'entity_test',
));
$this->field->save();
entity_get_form_display('entity_test', 'entity_test', 'default')
->setComponent($this->fieldName, array(
'type' => 'options_buttons',
))
->save();
}
}

View file

@ -0,0 +1,77 @@
<?php
/**
* @file
* Contains \Drupal\options\Tests\OptionsFloatFieldImportTest.
*/
namespace Drupal\options\Tests;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field\Tests\FieldTestBase;
/**
* Tests option fields can be updated and created through config synchronization.
*
* @group options
*/
class OptionsFloatFieldImportTest extends FieldTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('node', 'options', 'field_ui', 'config', 'options_config_install_test');
protected function setUp() {
parent::setUp();
// Create test user.
$admin_user = $this->drupalCreateUser(array('synchronize configuration', 'access content', 'access administration pages', 'administer site configuration', 'administer content types', 'administer nodes', 'bypass node access', 'administer node fields', 'administer node display'));
$this->drupalLogin($admin_user);
}
/**
* Tests that importing list_float fields works.
*/
public function testImport() {
$field_name = 'field_options_float';
$type = 'options_install_test';
// Test the results on installing options_config_install_test. All the
// necessary configuration for this test is created by installing that
// module.
$field_storage = FieldStorageConfig::loadByName('node', $field_name);
$this->assertIdentical($field_storage->getSetting('allowed_values'), $array = array('0' => 'Zero', '0.5' => 'Point five'));
$admin_path = 'admin/structure/types/manage/' . $type . '/fields/node.' . $type . '.' . $field_name . '/storage';
// Export active config to staging
$this->copyConfig($this->container->get('config.storage'), $this->container->get('config.storage.staging'));
// Set the active to not use dots in the allowed values key names.
$edit = array('settings[allowed_values]' => "0|Zero\n1|One");
$this->drupalPostForm($admin_path, $edit, t('Save field settings'));
$field_storage = FieldStorageConfig::loadByName('node', $field_name);
$this->assertIdentical($field_storage->getSetting('allowed_values'), $array = array('0' => 'Zero', '1' => 'One'));
// Import configuration with dots in the allowed values key names. This
// tests \Drupal\Core\Config\Entity\ConfigEntityStorage::importUpdate().
$this->drupalGet('admin/config/development/configuration');
$this->drupalPostForm(NULL, array(), t('Import all'));
$field_storage = FieldStorageConfig::loadByName('node', $field_name);
$this->assertIdentical($field_storage->getSetting('allowed_values'), $array = array('0' => 'Zero', '0.5' => 'Point five'));
// Delete field to test creation. This tests
// \Drupal\Core\Config\Entity\ConfigEntityStorage::importCreate().
FieldConfig::loadByName('node', $type, $field_name)->delete();
$this->drupalGet('admin/config/development/configuration');
$this->drupalPostForm(NULL, array(), t('Import all'));
$field_storage = FieldStorageConfig::loadByName('node', $field_name);
$this->assertIdentical($field_storage->getSetting('allowed_values'), $array = array('0' => 'Zero', '0.5' => 'Point five'));
}
}

View file

@ -0,0 +1,44 @@
<?php
/**
* @file
* Contains \Drupal\options\Tests\OptionsFormattersTest.
*/
namespace Drupal\options\Tests;
/**
* Tests the Options field type formatters.
*
* @group options
* @see \Drupal\options\Plugin\field\formatter\OptionsDefaultFormatter
* @see \Drupal\options\Plugin\field\formatter\OptionsKeyFormatter
*/
class OptionsFormattersTest extends OptionsFieldUnitTestBase {
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
}
/**
* Tests the formatters.
*/
public function testFormatter() {
$entity = entity_create('entity_test');
$entity->{$this->fieldName}->value = 1;
$items = $entity->get($this->fieldName);
$build = $items->view();
$this->assertEqual($build['#formatter'], 'list_default', 'Ensure to fall back to the default formatter.');
$this->assertEqual($build[0]['#markup'], 'One');
$build = $items->view(array('type' => 'list_key'));
$this->assertEqual($build['#formatter'], 'list_key', 'The chosen formatter is used.');
$this->assertEqual($build[0]['#markup'], 1);
}
}

View file

@ -0,0 +1,38 @@
<?php
/**
* @file
* Contains \Drupal\options\Tests\OptionsSelectDynamicValuesTest.
*/
namespace Drupal\options\Tests;
/**
* Tests an options select with a dynamic allowed values function.
*
* @group options
*/
class OptionsSelectDynamicValuesTest extends OptionsDynamicValuesTestBase {
/**
* Tests the 'options_select' widget (single select).
*/
function testSelectListDynamic() {
// Create an entity.
$this->entity->save();
// Create a web user.
$web_user = $this->drupalCreateUser(array('view test entity', 'administer entity_test content'));
$this->drupalLogin($web_user);
// Display form.
$this->drupalGet('entity_test_rev/manage/' . $this->entity->id());
$options = $this->xpath('//select[@id="edit-test-options"]/option');
$this->assertEqual(count($options), count($this->test) + 1);
foreach ($options as $option) {
$value = (string) $option['value'];
if ($value != '_none') {
$this->assertTrue(array_search($value, $this->test));
}
}
}
}

View file

@ -0,0 +1,496 @@
<?php
/**
* @file
* Contains \Drupal\options\Tests\OptionsWidgetsTest.
*/
namespace Drupal\options\Tests;
use Drupal\field\Tests\FieldTestBase;
/**
* Tests the Options widgets.
*
* @group options
*/
class OptionsWidgetsTest extends FieldTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = ['node', 'options', 'entity_test', 'options_test', 'taxonomy', 'field_ui'];
/**
* A field storage with cardinality 1 to use in this test class.
*
* @var \Drupal\field\Entity\FieldStorageConfig
*/
protected $card1;
/**
* A field storage with cardinality 2 to use in this test class.
*
* @var \Drupal\field\Entity\FieldStorageConfig
*/
protected $card2;
protected function setUp() {
parent::setUp();
// Field storage with cardinality 1.
$this->card1 = entity_create('field_storage_config', [
'field_name' => 'card_1',
'entity_type' => 'entity_test',
'type' => 'list_integer',
'cardinality' => 1,
'settings' => [
'allowed_values' => [
// Make sure that 0 works as an option.
0 => 'Zero',
1 => 'One',
// Make sure that option text is properly sanitized.
2 => 'Some <script>dangerous</script> & unescaped <strong>markup</strong>',
// Make sure that HTML entities in option text are not double-encoded.
3 => 'Some HTML encoded markup with &lt; &amp; &gt;',
],
],
]);
$this->card1->save();
// Field storage with cardinality 2.
$this->card2 = entity_create('field_storage_config', [
'field_name' => 'card_2',
'entity_type' => 'entity_test',
'type' => 'list_integer',
'cardinality' => 2,
'settings' => [
'allowed_values' => [
// Make sure that 0 works as an option.
0 => 'Zero',
1 => 'One',
// Make sure that option text is properly sanitized.
2 => 'Some <script>dangerous</script> & unescaped <strong>markup</strong>',
],
],
]);
$this->card2->save();
// Create a web user.
$this->drupalLogin($this->drupalCreateUser(['view test entity', 'administer entity_test content']));
}
/**
* Tests the 'options_buttons' widget (single select).
*/
function testRadioButtons() {
// Create an instance of the 'single value' field.
$field = entity_create('field_config', [
'field_storage' => $this->card1,
'bundle' => 'entity_test',
]);
$field->save();
entity_get_form_display('entity_test', 'entity_test', 'default')
->setComponent($this->card1->getName(), [
'type' => 'options_buttons',
])
->save();
// Create an entity.
$entity = entity_create('entity_test', [
'user_id' => 1,
'name' => $this->randomMachineName(),
]);
$entity->save();
$entity_init = clone $entity;
// With no field data, no buttons are checked.
$this->drupalGet('entity_test/manage/' . $entity->id());
$this->assertNoFieldChecked('edit-card-1-0');
$this->assertNoFieldChecked('edit-card-1-1');
$this->assertNoFieldChecked('edit-card-1-2');
$this->assertRaw('Some dangerous &amp; unescaped <strong>markup</strong>', 'Option text was properly filtered.');
$this->assertRaw('Some HTML encoded markup with &lt; &amp; &gt;');
// Select first option.
$edit = array('card_1' => 0);
$this->drupalPostForm(NULL, $edit, t('Save'));
$this->assertFieldValues($entity_init, 'card_1', array(0));
// Check that the selected button is checked.
$this->drupalGet('entity_test/manage/' . $entity->id());
$this->assertFieldChecked('edit-card-1-0');
$this->assertNoFieldChecked('edit-card-1-1');
$this->assertNoFieldChecked('edit-card-1-2');
// Unselect option.
$edit = array('card_1' => '_none');
$this->drupalPostForm(NULL, $edit, t('Save'));
$this->assertFieldValues($entity_init, 'card_1', array());
// Check that required radios with one option is auto-selected.
$this->card1->setSetting('allowed_values', [99 => 'Only allowed value']);
$this->card1->save();
$field->setRequired(TRUE);
$field->save();
$this->drupalGet('entity_test/manage/' . $entity->id());
$this->assertFieldChecked('edit-card-1-99');
}
/**
* Tests the 'options_buttons' widget (multiple select).
*/
function testCheckBoxes() {
// Create an instance of the 'multiple values' field.
$field = entity_create('field_config', array(
'field_storage' => $this->card2,
'bundle' => 'entity_test',
));
$field->save();
entity_get_form_display('entity_test', 'entity_test', 'default')
->setComponent($this->card2->getName(), array(
'type' => 'options_buttons',
))
->save();
// Create an entity.
$entity = entity_create('entity_test', array(
'user_id' => 1,
'name' => $this->randomMachineName(),
));
$entity->save();
$entity_init = clone $entity;
// Display form: with no field data, nothing is checked.
$this->drupalGet('entity_test/manage/' . $entity->id());
$this->assertNoFieldChecked('edit-card-2-0');
$this->assertNoFieldChecked('edit-card-2-1');
$this->assertNoFieldChecked('edit-card-2-2');
$this->assertRaw('Some dangerous &amp; unescaped <strong>markup</strong>', 'Option text was properly filtered.');
// Submit form: select first and third options.
$edit = array(
'card_2[0]' => TRUE,
'card_2[1]' => FALSE,
'card_2[2]' => TRUE,
);
$this->drupalPostForm(NULL, $edit, t('Save'));
$this->assertFieldValues($entity_init, 'card_2', array(0, 2));
// Display form: check that the right options are selected.
$this->drupalGet('entity_test/manage/' . $entity->id());
$this->assertFieldChecked('edit-card-2-0');
$this->assertNoFieldChecked('edit-card-2-1');
$this->assertFieldChecked('edit-card-2-2');
// Submit form: select only first option.
$edit = array(
'card_2[0]' => TRUE,
'card_2[1]' => FALSE,
'card_2[2]' => FALSE,
);
$this->drupalPostForm(NULL, $edit, t('Save'));
$this->assertFieldValues($entity_init, 'card_2', array(0));
// Display form: check that the right options are selected.
$this->drupalGet('entity_test/manage/' . $entity->id());
$this->assertFieldChecked('edit-card-2-0');
$this->assertNoFieldChecked('edit-card-2-1');
$this->assertNoFieldChecked('edit-card-2-2');
// Submit form: select the three options while the field accepts only 2.
$edit = array(
'card_2[0]' => TRUE,
'card_2[1]' => TRUE,
'card_2[2]' => TRUE,
);
$this->drupalPostForm(NULL, $edit, t('Save'));
$this->assertText('this field cannot hold more than 2 values', 'Validation error was displayed.');
// Submit form: uncheck all options.
$edit = array(
'card_2[0]' => FALSE,
'card_2[1]' => FALSE,
'card_2[2]' => FALSE,
);
$this->drupalPostForm(NULL, $edit, t('Save'));
// Check that the value was saved.
$this->assertFieldValues($entity_init, 'card_2', array());
// Required checkbox with one option is auto-selected.
$this->card2->setSetting('allowed_values', [99 => 'Only allowed value']);
$this->card2->save();
$field->setRequired(TRUE);
$field->save();
$this->drupalGet('entity_test/manage/' . $entity->id());
$this->assertFieldChecked('edit-card-2-99');
}
/**
* Tests the 'options_select' widget (single select).
*/
function testSelectListSingle() {
// Create an instance of the 'single value' field.
$field = entity_create('field_config', array(
'field_storage' => $this->card1,
'bundle' => 'entity_test',
'required' => TRUE,
));
$field->save();
entity_get_form_display('entity_test', 'entity_test', 'default')
->setComponent($this->card1->getName(), array(
'type' => 'options_select',
))
->save();
// Create an entity.
$entity = entity_create('entity_test', array(
'user_id' => 1,
'name' => $this->randomMachineName(),
));
$entity->save();
$entity_init = clone $entity;
// Display form.
$this->drupalGet('entity_test/manage/' . $entity->id());
// A required field without any value has a "none" option.
$this->assertTrue($this->xpath('//select[@id=:id]//option[@value="_none" and text()=:label]', array(':id' => 'edit-card-1', ':label' => t('- Select a value -'))), 'A required select list has a "Select a value" choice.');
// With no field data, nothing is selected.
$this->assertNoOptionSelected('edit-card-1', '_none');
$this->assertNoOptionSelected('edit-card-1', 0);
$this->assertNoOptionSelected('edit-card-1', 1);
$this->assertNoOptionSelected('edit-card-1', 2);
$this->assertRaw('Some dangerous &amp; unescaped markup', 'Option text was properly filtered.');
// Submit form: select invalid 'none' option.
$edit = array('card_1' => '_none');
$this->drupalPostForm(NULL, $edit, t('Save'));
$this->assertRaw(t('!title field is required.', array('!title' => $field->getName())), 'Cannot save a required field when selecting "none" from the select list.');
// Submit form: select first option.
$edit = array('card_1' => 0);
$this->drupalPostForm(NULL, $edit, t('Save'));
$this->assertFieldValues($entity_init, 'card_1', array(0));
// Display form: check that the right options are selected.
$this->drupalGet('entity_test/manage/' . $entity->id());
// A required field with a value has no 'none' option.
$this->assertFalse($this->xpath('//select[@id=:id]//option[@value="_none"]', array(':id' => 'edit-card-1')), 'A required select list with an actual value has no "none" choice.');
$this->assertOptionSelected('edit-card-1', 0);
$this->assertNoOptionSelected('edit-card-1', 1);
$this->assertNoOptionSelected('edit-card-1', 2);
// Make the field non required.
$field->setRequired(FALSE);
$field->save();
// Display form.
$this->drupalGet('entity_test/manage/' . $entity->id());
// A non-required field has a 'none' option.
$this->assertTrue($this->xpath('//select[@id=:id]//option[@value="_none" and text()=:label]', array(':id' => 'edit-card-1', ':label' => t('- None -'))), 'A non-required select list has a "None" choice.');
// Submit form: Unselect the option.
$edit = array('card_1' => '_none');
$this->drupalPostForm('entity_test/manage/' . $entity->id(), $edit, t('Save'));
$this->assertFieldValues($entity_init, 'card_1', array());
// Test optgroups.
$this->card1->setSetting('allowed_values', []);
$this->card1->setSetting('allowed_values_function', 'options_test_allowed_values_callback');
$this->card1->save();
// Display form: with no field data, nothing is selected
$this->drupalGet('entity_test/manage/' . $entity->id());
$this->assertNoOptionSelected('edit-card-1', 0);
$this->assertNoOptionSelected('edit-card-1', 1);
$this->assertNoOptionSelected('edit-card-1', 2);
$this->assertRaw('Some dangerous &amp; unescaped markup', 'Option text was properly filtered.');
$this->assertRaw('More &lt;script&gt;dangerous&lt;/script&gt; markup', 'Option group text was properly filtered.');
$this->assertRaw('Group 1', 'Option groups are displayed.');
// Submit form: select first option.
$edit = array('card_1' => 0);
$this->drupalPostForm(NULL, $edit, t('Save'));
$this->assertFieldValues($entity_init, 'card_1', array(0));
// Display form: check that the right options are selected.
$this->drupalGet('entity_test/manage/' . $entity->id());
$this->assertOptionSelected('edit-card-1', 0);
$this->assertNoOptionSelected('edit-card-1', 1);
$this->assertNoOptionSelected('edit-card-1', 2);
// Submit form: Unselect the option.
$edit = array('card_1' => '_none');
$this->drupalPostForm('entity_test/manage/' . $entity->id(), $edit, t('Save'));
$this->assertFieldValues($entity_init, 'card_1', array());
}
/**
* Tests the 'options_select' widget (multiple select).
*/
function testSelectListMultiple() {
// Create an instance of the 'multiple values' field.
$field = entity_create('field_config', array(
'field_storage' => $this->card2,
'bundle' => 'entity_test',
));
$field->save();
entity_get_form_display('entity_test', 'entity_test', 'default')
->setComponent($this->card2->getName(), array(
'type' => 'options_select',
))
->save();
// Create an entity.
$entity = entity_create('entity_test', array(
'user_id' => 1,
'name' => $this->randomMachineName(),
));
$entity->save();
$entity_init = clone $entity;
// Display form: with no field data, nothing is selected.
$this->drupalGet('entity_test/manage/' . $entity->id());
$this->assertOptionSelected("edit-card-2", '_none');
$this->assertNoOptionSelected('edit-card-2', 0);
$this->assertNoOptionSelected('edit-card-2', 1);
$this->assertNoOptionSelected('edit-card-2', 2);
$this->assertRaw('Some dangerous &amp; unescaped markup', 'Option text was properly filtered.');
// Submit form: select first and third options.
$edit = array('card_2[]' => array(0 => 0, 2 => 2));
$this->drupalPostForm(NULL, $edit, t('Save'));
$this->assertFieldValues($entity_init, 'card_2', array(0, 2));
// Display form: check that the right options are selected.
$this->drupalGet('entity_test/manage/' . $entity->id());
$this->assertOptionSelected('edit-card-2', 0);
$this->assertNoOptionSelected('edit-card-2', 1);
$this->assertOptionSelected('edit-card-2', 2);
// Submit form: select only first option.
$edit = array('card_2[]' => array(0 => 0));
$this->drupalPostForm(NULL, $edit, t('Save'));
$this->assertFieldValues($entity_init, 'card_2', array(0));
// Display form: check that the right options are selected.
$this->drupalGet('entity_test/manage/' . $entity->id());
$this->assertOptionSelected('edit-card-2', 0);
$this->assertNoOptionSelected('edit-card-2', 1);
$this->assertNoOptionSelected('edit-card-2', 2);
// Submit form: select the three options while the field accepts only 2.
$edit = array('card_2[]' => array(0 => 0, 1 => 1, 2 => 2));
$this->drupalPostForm(NULL, $edit, t('Save'));
$this->assertText('this field cannot hold more than 2 values', 'Validation error was displayed.');
// Submit form: uncheck all options.
$edit = array('card_2[]' => array());
$this->drupalPostForm(NULL, $edit, t('Save'));
$this->assertFieldValues($entity_init, 'card_2', array());
// Test the 'None' option.
// Check that the 'none' option has no effect if actual options are selected
// as well.
$edit = array('card_2[]' => array('_none' => '_none', 0 => 0));
$this->drupalPostForm('entity_test/manage/' . $entity->id(), $edit, t('Save'));
$this->assertFieldValues($entity_init, 'card_2', array(0));
// Check that selecting the 'none' option empties the field.
$edit = array('card_2[]' => array('_none' => '_none'));
$this->drupalPostForm('entity_test/manage/' . $entity->id(), $edit, t('Save'));
$this->assertFieldValues($entity_init, 'card_2', array());
// A required select list does not have an empty key.
$field->setRequired(TRUE);
$field->save();
$this->drupalGet('entity_test/manage/' . $entity->id());
$this->assertFalse($this->xpath('//select[@id=:id]//option[@value=""]', array(':id' => 'edit-card-2')), 'A required select list does not have an empty key.');
// We do not have to test that a required select list with one option is
// auto-selected because the browser does it for us.
// Test optgroups.
// Use a callback function defining optgroups.
$this->card2->setSetting('allowed_values', []);
$this->card2->setSetting('allowed_values_function', 'options_test_allowed_values_callback');
$this->card2->save();
$field->setRequired(FALSE);
$field->save();
// Display form: with no field data, nothing is selected.
$this->drupalGet('entity_test/manage/' . $entity->id());
$this->assertNoOptionSelected('edit-card-2', 0);
$this->assertNoOptionSelected('edit-card-2', 1);
$this->assertNoOptionSelected('edit-card-2', 2);
$this->assertRaw('Some dangerous &amp; unescaped markup', 'Option text was properly filtered.');
$this->assertRaw('More &lt;script&gt;dangerous&lt;/script&gt; markup', 'Option group text was properly filtered.');
$this->assertRaw('Group 1', 'Option groups are displayed.');
// Submit form: select first option.
$edit = array('card_2[]' => array(0 => 0));
$this->drupalPostForm(NULL, $edit, t('Save'));
$this->assertFieldValues($entity_init, 'card_2', array(0));
// Display form: check that the right options are selected.
$this->drupalGet('entity_test/manage/' . $entity->id());
$this->assertOptionSelected('edit-card-2', 0);
$this->assertNoOptionSelected('edit-card-2', 1);
$this->assertNoOptionSelected('edit-card-2', 2);
// Submit form: Unselect the option.
$edit = array('card_2[]' => array('_none' => '_none'));
$this->drupalPostForm('entity_test/manage/' . $entity->id(), $edit, t('Save'));
$this->assertFieldValues($entity_init, 'card_2', array());
}
/**
* Tests the 'options_select' and 'options_button' widget for empty value.
*/
function testEmptyValue() {
// Create an instance of the 'single value' field.
$field = entity_create('field_config', [
'field_storage' => $this->card1,
'bundle' => 'entity_test',
]);
$field->save();
// Change it to the check boxes/radio buttons widget.
entity_get_form_display('entity_test', 'entity_test', 'default')
->setComponent($this->card1->getName(), [
'type' => 'options_buttons',
])
->save();
// Create an entity.
$entity = entity_create('entity_test', [
'user_id' => 1,
'name' => $this->randomMachineName(),
]);
$entity->save();
// Display form: check that _none options are present and has label.
$this->drupalGet('entity_test/manage/' . $entity->id());
$this->assertTrue($this->xpath('//div[@id=:id]//input[@value=:value]', array(':id' => 'edit-card-1', ':value' => '_none')), 'A test radio button has a "None" choice.');
$this->assertTrue($this->xpath('//div[@id=:id]//label[@for=:for and text()=:label]', array(':id' => 'edit-card-1', ':for' => 'edit-card-1-none', ':label' => 'N/A')), 'A test radio button has a "N/A" choice.');
// Change it to the select widget.
entity_get_form_display('entity_test', 'entity_test', 'default')
->setComponent($this->card1->getName(), array(
'type' => 'options_select',
))
->save();
// Display form: check that _none options are present and has label.
$this->drupalGet('entity_test/manage/' . $entity->id());
// A required field without any value has a "none" option.
$this->assertTrue($this->xpath('//select[@id=:id]//option[@value="_none" and text()=:label]', array(':id' => 'edit-card-1', ':label' => t('- None -'))), 'A test select has a "None" choice.');
}
}

View file

@ -0,0 +1,54 @@
<?php
/**
* @file
* Contains \Drupal\options\Tests\Views\OptionsListArgumentTest.
*/
namespace Drupal\options\Tests\Views;
use Drupal\views\Views;
/**
* Tests options list argument for views.
*
* @see \Drupal\options\Plugin\views\argument\NumberListField.
* @group views
*/
class OptionsListArgumentTest extends OptionsTestBase {
/**
* Views used by this test.
*
* @var array
*/
public static $testViews = ['test_options_list_argument_numeric', 'test_options_list_argument_string'];
/**
* Tests the options field argument.
*/
public function testViewsTestOptionsListArgument() {
$view = Views::getView('test_options_list_argument_numeric');
$this->executeView($view, [1]);
$resultset = [
['nid' => $this->nodes[0]->nid->value],
['nid' => $this->nodes[1]->nid->value],
];
$column_map = ['nid' => 'nid'];
$this->assertIdenticalResultset($view, $resultset, $column_map);
$view = Views::getView('test_options_list_argument_string');
$this->executeView($view, ['man', 'woman']);
$resultset = [
['nid' => $this->nodes[0]->nid->value],
['nid' => $this->nodes[1]->nid->value],
];
$column_map = ['nid' => 'nid'];
$this->assertIdenticalResultset($view, $resultset, $column_map);
}
}

View file

@ -0,0 +1,43 @@
<?php
/**
* @file
* Contains \Drupal\options\Tests\Views\OptionsListFilterTest.
*/
namespace Drupal\options\Tests\Views;
use Drupal\views\Views;
/**
* Tests options list filter for views.
*
* @see \Drupal\field\Plugin\views\filter\ListField.
* @group views
*/
class OptionsListFilterTest extends OptionsTestBase {
/**
* Views used by this test.
*
* @var array
*/
public static $testViews = ['test_options_list_filter'];
/**
* Tests options list field filter.
*/
public function testViewsTestOptionsListFilter() {
$view = Views::getView('test_options_list_filter');
$this->executeView($view);
$resultset = [
['nid' => $this->nodes[0]->nid->value],
['nid' => $this->nodes[1]->nid->value],
];
$column_map = ['nid' => 'nid'];
$this->assertIdenticalResultset($view, $resultset, $column_map);
}
}

View file

@ -0,0 +1,125 @@
<?php
/**
* @file
* Contains \Drupal\options\Tests\Views\OptionsTestBase.
*/
namespace Drupal\options\Tests\Views;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\views\Tests\ViewTestBase;
use Drupal\views\Tests\ViewTestData;
use Drupal\views\Tests\ViewUnitTestBase;
/**
* Base class for options views tests.
*/
abstract class OptionsTestBase extends ViewUnitTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = ['options', 'options_test_views', 'node', 'user', 'field'];
/**
* Stores the nodes used for the different tests.
*
* @var array
*/
protected $nodes = [];
/**
* Stores the field values used for the different tests.
*
* @var array
*/
protected $fieldValues = [];
/**
* The used field names.
*
* @var string[]
*/
protected $fieldNames;
protected function setUp() {
parent::setUp();
$this->mockStandardInstall();
ViewTestData::createTestViews(get_class($this), ['options_test_views']);
$settings = [];
$settings['type'] = 'article';
$settings['field_test_list_string'][]['value'] = $this->fieldValues[0];
$settings['field_test_list_integer'][]['value'] = 0;
$node = Node::create($settings);
$node->save();
$this->nodes[] = $node;
$node = $node->createDuplicate();
$node->save();
$this->nodes[] = $node;
}
/**
* Provides a workaround for the inability to use the standard profile.
*
* @see https://www.drupal.org/node/1708692
*/
protected function mockStandardInstall() {
$this->installEntitySchema('user');
$this->installEntitySchema('node');
NodeType::create(
['type' => 'article']
)->save();
$this->fieldValues = [
$this->randomMachineName(),
$this->randomMachineName(),
];
$this->fieldNames = ['field_test_list_string', 'field_test_list_integer'];
// Create two field entities.
FieldStorageConfig::create([
'field_name' => $this->fieldNames[0],
'entity_type' => 'node',
'type' => 'list_string',
'cardinality' => 1,
'settings' => [
'allowed_values' => [
$this->fieldValues[0] => $this->fieldValues[0],
$this->fieldValues[1] => $this->fieldValues[1],
],
],
])->save();
FieldStorageConfig::create([
'field_name' => $this->fieldNames[1],
'entity_type' => 'node',
'type' => 'list_integer',
'cardinality' => 1,
'settings' => [
'allowed_values' => [
$this->fieldValues[0],
$this->fieldValues[1],
],
],
])->save();
foreach ($this->fieldNames as $field_name) {
FieldConfig::create([
'field_name' => $field_name,
'entity_type' => 'node',
'label' => 'Test options list field',
'bundle' => 'article',
])->save();
}
}
}

View file

@ -0,0 +1,66 @@
<?php
/**
* @file
* Contains \Drupal\options\Tests\Views\ViewsDataTest.
*/
namespace Drupal\options\Tests\Views;
/**
* Test to ensure views data is properly created for the Options module.
*
* @group views
*/
class ViewsDataTest extends OptionsTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['options', 'options_test', 'entity_test', 'views'];
/**
* The field storage.
*
* @var \Drupal\Core\Field\FieldStorageDefinitionInterface
*/
protected $fieldStorage;
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
$field_name = 'test_options';
$this->fieldStorage = entity_create('field_storage_config', [
'field_name' => $field_name,
'entity_type' => 'entity_test',
'type' => 'list_string',
'cardinality' => 1,
'settings' => [
'allowed_values_function' => 'options_test_dynamic_values_callback',
],
]);
$this->fieldStorage->save();
$this->field = entity_create('field_config', [
'field_name' => $field_name,
'entity_type' => 'entity_test',
'bundle' => 'entity_test',
'required' => TRUE,
])->save();
}
/**
* Tests the option module's implementation of hook_field_views_data().
*/
public function testOptionsFieldViewsData() {
$field_data = \Drupal::service('views.views_data')->get('entity_test__test_options');
// Check that the options module has properly overridden default views data.
$test_options_field = $field_data['test_options_value'];
$this->assertEqual($test_options_field['argument']['id'], 'string_list_field', 'Argument handler is properly set for fields with allowed value callbacks.');
$this->assertEqual($test_options_field['filter']['id'], 'list_field', 'Filter handler is properly set for fields with allowed value callbacks.');
}
}