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