16c29907c885ede74b0b478805805dcb603dbb56
[yaffs-website] / web / core / modules / options / tests / src / Functional / OptionsDynamicValuesTestBase.php
1 <?php
2
3 namespace Drupal\Tests\options\Functional;
4
5 use Drupal\field\Entity\FieldConfig;
6 use Drupal\field\Entity\FieldStorageConfig;
7 use Drupal\entity_test\Entity\EntityTestRev;
8 use Drupal\Tests\field\Functional\FieldTestBase;
9
10 /**
11  * Base class for testing allowed values of options fields.
12  */
13 abstract class OptionsDynamicValuesTestBase extends FieldTestBase {
14
15   /**
16    * Modules to enable.
17    *
18    * @var array
19    */
20   public static $modules = ['options', 'entity_test', 'options_test'];
21
22   /**
23    * The created entity.
24    *
25    * @var \Drupal\Core\Entity\Entity
26    */
27   protected $entity;
28
29   /**
30    * The field storage.
31    *
32    * @var \Drupal\Core\Field\FieldStorageDefinitionInterface
33    */
34   protected $fieldStorage;
35
36   protected function setUp() {
37     parent::setUp();
38
39     $field_name = 'test_options';
40     $this->fieldStorage = FieldStorageConfig::create([
41       'field_name' => $field_name,
42       'entity_type' => 'entity_test_rev',
43       'type' => 'list_string',
44       'cardinality' => 1,
45       'settings' => [
46         'allowed_values_function' => 'options_test_dynamic_values_callback',
47       ],
48     ]);
49     $this->fieldStorage->save();
50
51     $this->field = FieldConfig::create([
52       'field_name' => $field_name,
53       'entity_type' => 'entity_test_rev',
54       'bundle' => 'entity_test_rev',
55       'required' => TRUE,
56     ])->save();
57     entity_get_form_display('entity_test_rev', 'entity_test_rev', 'default')
58       ->setComponent($field_name, [
59         'type' => 'options_select',
60       ])
61       ->save();
62
63     // Create an entity and prepare test data that will be used by
64     // options_test_dynamic_values_callback().
65     $values = [
66       'user_id' => mt_rand(1, 10),
67       'name' => $this->randomMachineName(),
68     ];
69     $this->entity = EntityTestRev::create($values);
70     $this->entity->save();
71     $this->test = [
72       'label' => $this->entity->label(),
73       'uuid' => $this->entity->uuid(),
74       'bundle' => $this->entity->bundle(),
75       'uri' => $this->entity->url(),
76     ];
77   }
78
79 }