Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / system / tests / modules / entity_test_update / entity_test_update.module
1 <?php
2
3 /**
4  * @file
5  * Provides an entity type for testing definition and schema updates.
6  */
7
8 use Drupal\Core\Entity\EntityInterface;
9 use Drupal\Core\Field\BaseFieldDefinition;
10 use Drupal\Core\StringTranslation\TranslatableMarkup;
11 use Drupal\entity_test_update\Entity\EntityTestUpdate;
12 use Drupal\Core\Entity\EntityTypeInterface;
13
14 /**
15  * Implements hook_entity_base_field_info().
16  */
17 function entity_test_update_entity_base_field_info(EntityTypeInterface $entity_type) {
18   // Add a base field that will be used to test that fields added through
19   // hook_entity_base_field_info() are handled correctly during a schema
20   // conversion (e.g. from non-revisionable to revisionable).
21   if ($entity_type->id() == 'entity_test_update') {
22     $fields = [];
23     $fields['test_entity_base_field_info'] = BaseFieldDefinition::create('string')
24       ->setLabel(new TranslatableMarkup('Field added by hook_entity_base_field_info()'))
25       ->setTranslatable(TRUE)
26       ->setRevisionable(TRUE);
27
28     return $fields;
29   }
30 }
31
32 /**
33  * Implements hook_entity_field_storage_info().
34  */
35 function entity_test_update_entity_field_storage_info(EntityTypeInterface $entity_type) {
36   if ($entity_type->id() == 'entity_test_update') {
37     return \Drupal::state()->get('entity_test_update.additional_field_storage_definitions', []);
38   }
39 }
40
41 /**
42  * Implements hook_entity_type_alter().
43  */
44 function entity_test_update_entity_type_alter(array &$entity_types) {
45   // Allow entity_test_update tests to override the entity type definition.
46   $entity_types['entity_test_update'] = \Drupal::state()->get('entity_test_update.entity_type', $entity_types['entity_test_update']);
47 }
48
49 /**
50  * Implements hook_entity_presave().
51  */
52 function entity_test_update_entity_presave(EntityInterface $entity) {
53   // Simulate an error during the save process of a test entity.
54   if ($entity->getEntityTypeId() === 'entity_test_update' && \Drupal::state()->get('entity_test_update.throw_exception', FALSE)) {
55     throw new \Exception('Peekaboo!');
56   }
57 }
58
59 /**
60  * Creates a given number of 'entity_test_update' entities.
61  *
62  * The 'drupal-8.0.0-rc1-filled.standard.entity_test_update.php.gz' db dump was
63  * created with the following characteristics:
64  * - Drupal 8.0.0-rc1 installed with the Standard profile;
65  * - The 'entity_test_update' module enabled;
66  * - 102 test entities created and saved.
67  *
68  * The 'drupal-8.0.0-rc1-filled.standard.entity_test_update_mul.php.gz' db dump
69  * was created with the following characteristics:
70  * - Drupal 8.0.0-rc1 installed with the Standard profile;
71  * - Manually edited the annotation of the entity_test_update entity type in
72  *   order to make it translatable;
73  * - The entity_test_update and Language module enabled;
74  * - Romanian language added;
75  * - 50 test entities created, translated and saved;
76  * - The Content Translation module enabled and configured for our test entity
77  *   type. This was enabled after the first 50 entities were created in order
78  *   to have NULL values for its translation metadata fields
79  *   (e.g. content_translation_status);
80  * - 52 more test entities (with the IDs 51 - 102) crated, translated and saved.
81  *
82  * The 'drupal-8.0.0-rc1-filled.standard.entity_test_update_mul_rev.php.gz' db
83  * dump was created like the multilingual one described above, with one change:
84  * The annotation of the entity_test_update entity type was also manually edited
85  * in order to make it revisionable.
86  *
87  * @param int $start
88  *   (optional) The entity ID to start from. Defaults to 1.
89  * @param int $end
90  *   (optional) The entity ID to end with. Defaults to 50.
91  * @param bool $add_translation
92  *   (optional) Whether to create a translation for each entity. If the number
93  *   of entities is greater than 50, the default translation (en) of the 51st
94  *   entity and the 52nd translation (ro) are disabled through the
95  *   'content_moderation_status' field. Defaults to FALSE.
96  *
97  * @see drupal-8.0.0-rc1-filled.standard.entity_test_update.php.gz
98  * @see drupal-8.0.0-rc1-filled.standard.entity_test_update_mul.php.gz
99  */
100 function _entity_test_update_create_test_entities($start = 1, $end = 50, $add_translation = FALSE) {
101   for ($i = $start; $i <= $end; $i++) {
102     $entity  = EntityTestUpdate::create([
103       'id' => $i,
104       'name' => $i,
105       'test_single_property' => $i . ' - test single property',
106       'test_multiple_properties' => [
107         [
108           'value1' => $i . ' - test multiple properties - value1',
109           'value2' => $i . ' - test multiple properties - value2',
110         ],
111       ],
112       'test_single_property_multiple_values' => [
113         ['value' => $i . ' - test single property multiple values 0'],
114         ['value' => $i . ' - test single property multiple values 1'],
115       ],
116       'test_multiple_properties_multiple_values' => [
117         [
118           'value1' => $i . ' - test multiple properties multiple values - value1 0',
119           'value2' => $i . ' - test multiple properties multiple values - value2 0',
120         ],
121         [
122           'value1' => $i . ' - test multiple properties multiple values - value1 1',
123           'value2' => $i . ' - test multiple properties multiple values - value2 1',
124         ],
125       ],
126       'test_non_rev_field' => $i . ' - test non-revisionable field',
127       'test_non_mul_field' => $i . ' - test non-translatable field',
128       'test_non_mulrev_field' => $i . ' - test non-translatable and non-revisionable field',
129       'field_test_configurable_field' => [
130         [
131           'value1' => $i . ' - field test configurable field - value1 0',
132           'value2' => $i . ' - field test configurable field - value2 0',
133         ],
134         [
135           'value1' => $i . ' - field test configurable field - value1 1',
136           'value2' => $i . ' - field test configurable field - value2 1',
137         ],
138       ],
139       'test_entity_base_field_info' => $i . ' - test entity base field info',
140     ]);
141
142     if ($add_translation) {
143       $entity->addTranslation('ro', [
144         'name' => $i . ' - ro',
145         'test_single_property' => $i . ' - test single property - ro',
146         'test_multiple_properties' => [
147           [
148             'value1' => $i . ' - test multiple properties - value1 - ro',
149             'value2' => $i . ' - test multiple properties - value2 - ro',
150           ],
151         ],
152         'test_single_property_multiple_values' => [
153           ['value' => $i . ' - test single property multiple values 0 - ro'],
154           ['value' => $i . ' - test single property multiple values 1 - ro'],
155         ],
156         'test_multiple_properties_multiple_values' => [
157           [
158             'value1' => $i . ' - test multiple properties multiple values - value1 0 - ro',
159             'value2' => $i . ' - test multiple properties multiple values - value2 0 - ro',
160           ],
161           [
162             'value1' => $i . ' - test multiple properties multiple values - value1 1 - ro',
163             'value2' => $i . ' - test multiple properties multiple values - value2 1 - ro',
164           ],
165         ],
166         'test_non_rev_field' => $i . ' - test non-revisionable field - ro',
167         'field_test_configurable_field' => [
168           [
169             'value1' => $i . ' - field test configurable field - value1 0 - ro',
170             'value2' => $i . ' - field test configurable field - value2 0 - ro',
171           ],
172           [
173             'value1' => $i . ' - field test configurable field - value1 1 - ro',
174             'value2' => $i . ' - field test configurable field - value2 1 - ro',
175           ],
176         ],
177         'test_entity_base_field_info' => $i . ' - test entity base field info - ro',
178       ]);
179
180       if ($i == 101) {
181         $entity->getTranslation('en')->set('content_translation_status', FALSE);
182       }
183       if ($i == 102) {
184         $entity->getTranslation('ro')->set('content_translation_status', FALSE);
185       }
186     }
187
188     $entity->save();
189   }
190 }