annotate core/modules/options/tests/src/Kernel/OptionsFieldTest.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\options\Kernel;
Chris@0 4
Chris@0 5 use Drupal\Core\Entity\Exception\FieldStorageDefinitionUpdateForbiddenException;
Chris@0 6 use Drupal\entity_test\Entity\EntityTest;
Chris@0 7 use Drupal\field\Entity\FieldConfig;
Chris@0 8 use Drupal\field\Entity\FieldStorageConfig;
Chris@0 9
Chris@0 10 /**
Chris@0 11 * Tests for the 'Options' field types.
Chris@0 12 *
Chris@0 13 * @group options
Chris@0 14 */
Chris@0 15 class OptionsFieldTest extends OptionsFieldUnitTestBase {
Chris@0 16
Chris@0 17 /**
Chris@0 18 * Modules to enable.
Chris@0 19 *
Chris@0 20 * @var array
Chris@0 21 */
Chris@0 22 public static $modules = ['options'];
Chris@0 23
Chris@0 24 /**
Chris@0 25 * Test that allowed values can be updated.
Chris@0 26 */
Chris@0 27 public function testUpdateAllowedValues() {
Chris@0 28 // All three options appear.
Chris@0 29 $entity = EntityTest::create();
Chris@0 30 $form = \Drupal::service('entity.form_builder')->getForm($entity);
Chris@0 31 $this->assertTrue(!empty($form[$this->fieldName]['widget'][1]), 'Option 1 exists');
Chris@0 32 $this->assertTrue(!empty($form[$this->fieldName]['widget'][2]), 'Option 2 exists');
Chris@0 33 $this->assertTrue(!empty($form[$this->fieldName]['widget'][3]), 'Option 3 exists');
Chris@0 34
Chris@0 35 // Use one of the values in an actual entity, and check that this value
Chris@0 36 // cannot be removed from the list.
Chris@0 37 $entity = EntityTest::create();
Chris@0 38 $entity->{$this->fieldName}->value = 1;
Chris@0 39 $entity->save();
Chris@0 40 $this->fieldStorage->setSetting('allowed_values', [2 => 'Two']);
Chris@0 41 try {
Chris@0 42 $this->fieldStorage->save();
Chris@0 43 $this->fail(t('Cannot update a list field storage to not include keys with existing data.'));
Chris@0 44 }
Chris@0 45 catch (FieldStorageDefinitionUpdateForbiddenException $e) {
Chris@0 46 $this->pass(t('Cannot update a list field storage to not include keys with existing data.'));
Chris@0 47 }
Chris@0 48 // Empty the value, so that we can actually remove the option.
Chris@0 49 unset($entity->{$this->fieldName});
Chris@0 50 $entity->save();
Chris@0 51
Chris@0 52 // Removed options do not appear.
Chris@0 53 $this->fieldStorage->setSetting('allowed_values', [2 => 'Two']);
Chris@0 54 $this->fieldStorage->save();
Chris@0 55 $entity = EntityTest::create();
Chris@0 56 $form = \Drupal::service('entity.form_builder')->getForm($entity);
Chris@0 57 $this->assertTrue(empty($form[$this->fieldName]['widget'][1]), 'Option 1 does not exist');
Chris@0 58 $this->assertTrue(!empty($form[$this->fieldName]['widget'][2]), 'Option 2 exists');
Chris@0 59 $this->assertTrue(empty($form[$this->fieldName]['widget'][3]), 'Option 3 does not exist');
Chris@0 60
Chris@0 61 // Completely new options appear.
Chris@0 62 $this->fieldStorage->setSetting('allowed_values', [10 => 'Update', 20 => 'Twenty']);
Chris@0 63 $this->fieldStorage->save();
Chris@0 64 // The entity holds an outdated field object with the old allowed values
Chris@0 65 // setting, so we need to reinitialize the entity object.
Chris@0 66 $entity = EntityTest::create();
Chris@0 67 $form = \Drupal::service('entity.form_builder')->getForm($entity);
Chris@0 68 $this->assertTrue(empty($form[$this->fieldName]['widget'][1]), 'Option 1 does not exist');
Chris@0 69 $this->assertTrue(empty($form[$this->fieldName]['widget'][2]), 'Option 2 does not exist');
Chris@0 70 $this->assertTrue(empty($form[$this->fieldName]['widget'][3]), 'Option 3 does not exist');
Chris@0 71 $this->assertTrue(!empty($form[$this->fieldName]['widget'][10]), 'Option 10 exists');
Chris@0 72 $this->assertTrue(!empty($form[$this->fieldName]['widget'][20]), 'Option 20 exists');
Chris@0 73
Chris@0 74 // Options are reset when a new field with the same name is created.
Chris@0 75 $this->fieldStorage->delete();
Chris@0 76 FieldStorageConfig::create($this->fieldStorageDefinition)->save();
Chris@0 77 FieldConfig::create([
Chris@0 78 'field_name' => $this->fieldName,
Chris@0 79 'entity_type' => 'entity_test',
Chris@0 80 'bundle' => 'entity_test',
Chris@0 81 'required' => TRUE,
Chris@0 82 ])->save();
Chris@0 83 entity_get_form_display('entity_test', 'entity_test', 'default')
Chris@0 84 ->setComponent($this->fieldName, [
Chris@0 85 'type' => 'options_buttons',
Chris@0 86 ])
Chris@0 87 ->save();
Chris@0 88 $entity = EntityTest::create();
Chris@0 89 $form = \Drupal::service('entity.form_builder')->getForm($entity);
Chris@0 90 $this->assertTrue(!empty($form[$this->fieldName]['widget'][1]), 'Option 1 exists');
Chris@0 91 $this->assertTrue(!empty($form[$this->fieldName]['widget'][2]), 'Option 2 exists');
Chris@0 92 $this->assertTrue(!empty($form[$this->fieldName]['widget'][3]), 'Option 3 exists');
Chris@0 93
Chris@0 94 // Test the generateSampleValue() method.
Chris@0 95 $entity = EntityTest::create();
Chris@0 96 $entity->{$this->fieldName}->generateSampleItems();
Chris@0 97 $this->entityValidateAndSave($entity);
Chris@0 98 }
Chris@0 99
Chris@0 100 }