Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / config / tests / config_test / src / Entity / ConfigTest.php
1 <?php
2
3 namespace Drupal\config_test\Entity;
4
5 use Drupal\Core\Config\Entity\ConfigEntityBase;
6 use Drupal\config_test\ConfigTestInterface;
7 use Drupal\Core\Config\Entity\ConfigEntityInterface;
8 use Drupal\Core\Entity\EntityStorageInterface;
9
10 /**
11  * Defines the ConfigTest configuration entity.
12  *
13  * @ConfigEntityType(
14  *   id = "config_test",
15  *   label = @Translation("Test configuration"),
16  *   handlers = {
17  *     "storage" = "Drupal\config_test\ConfigTestStorage",
18  *     "list_builder" = "Drupal\config_test\ConfigTestListBuilder",
19  *     "form" = {
20  *       "default" = "Drupal\config_test\ConfigTestForm",
21  *       "delete" = "Drupal\Core\Entity\EntityDeleteForm"
22  *     },
23  *     "access" = "Drupal\config_test\ConfigTestAccessControlHandler"
24  *   },
25  *   config_prefix = "dynamic",
26  *   entity_keys = {
27  *     "id" = "id",
28  *     "label" = "label",
29  *     "status" = "status"
30  *   },
31  *   links = {
32  *     "edit-form" = "/admin/structure/config_test/manage/{config_test}",
33  *     "delete-form" = "/admin/structure/config_test/manage/{config_test}/delete",
34  *     "enable" = "/admin/structure/config_test/manage/{config_test}/enable",
35  *     "disable" = "/admin/structure/config_test/manage/{config_test}/disable",
36  *     "collection" = "/admin/structure/config_test",
37  *   }
38  * )
39  */
40 class ConfigTest extends ConfigEntityBase implements ConfigTestInterface {
41
42   /**
43    * The machine name for the configuration entity.
44    *
45    * @var string
46    */
47   protected $id;
48
49   /**
50    * The human-readable name of the configuration entity.
51    *
52    * @var string
53    */
54   public $label;
55
56   /**
57    * The weight of the configuration entity.
58    *
59    * @var int
60    */
61   public $weight = 0;
62
63   /**
64    * The image style to use.
65    *
66    * @var string
67    */
68   public $style;
69
70   /**
71    * A protected property of the configuration entity.
72    *
73    * @var string
74    */
75   protected $protected_property;
76
77   /**
78    * {@inheritdoc}
79    */
80   public static function sort(ConfigEntityInterface $a, ConfigEntityInterface $b) {
81     \Drupal::state()->set('config_entity_sort', TRUE);
82     return parent::sort($a, $b);
83   }
84
85   /**
86    * {@inheritdoc}
87    */
88   public function postSave(EntityStorageInterface $storage, $update = TRUE) {
89     // Used to test secondary writes during config sync.
90     if ($this->id() == 'primary') {
91       $secondary = $storage->create([
92         'id' => 'secondary',
93         'label' => 'Secondary Default',
94       ]);
95       $secondary->save();
96     }
97     if ($this->id() == 'deleter') {
98       $deletee = $storage->load('deletee');
99       $deletee->delete();
100     }
101   }
102
103   /**
104    * {@inheritdoc}
105    */
106   public static function postDelete(EntityStorageInterface $storage, array $entities) {
107     parent::postDelete($storage, $entities);
108     foreach ($entities as $entity) {
109       if ($entity->id() == 'deleter') {
110         $deletee = $storage->load('deletee');
111         $deletee->delete();
112       }
113     }
114   }
115
116   /**
117    * {@inheritdoc}
118    */
119   public function onDependencyRemoval(array $dependencies) {
120     // Record which entities have this method called on and what dependencies
121     // are passed.
122     $called = \Drupal::state()->get('config_test.on_dependency_removal_called', []);
123     $called[$this->id()] = $dependencies;
124     $called[$this->id()]['config'] = array_keys($called[$this->id()]['config']);
125     $called[$this->id()]['content'] = array_keys($called[$this->id()]['content']);
126     \Drupal::state()->set('config_test.on_dependency_removal_called', $called);
127
128     $changed = parent::onDependencyRemoval($dependencies);
129     if (!isset($this->dependencies['enforced']['config'])) {
130       return $changed;
131     }
132     $fix_deps = \Drupal::state()->get('config_test.fix_dependencies', []);
133     foreach ($dependencies['config'] as $entity) {
134       if (in_array($entity->getConfigDependencyName(), $fix_deps)) {
135         $key = array_search($entity->getConfigDependencyName(), $this->dependencies['enforced']['config']);
136         if ($key !== FALSE) {
137           $changed = TRUE;
138           unset($this->dependencies['enforced']['config'][$key]);
139         }
140       }
141     }
142     // If any of the dependencies removed still exists, return FALSE.
143     if (array_intersect_key(array_flip($this->dependencies['enforced']['config']), $dependencies['config'])) {
144       return FALSE;
145     }
146     return $changed;
147   }
148
149   /**
150    * Sets the enforced dependencies.
151    *
152    * @param array $dependencies
153    *   A config dependency array.
154    *
155    * @return $this
156    *
157    * @see \Drupal\Core\Config\Entity\ConfigDependencyManager
158    */
159   public function setEnforcedDependencies(array $dependencies) {
160     $this->dependencies['enforced'] = $dependencies;
161     return $this;
162   }
163
164   /**
165    * {@inheritdoc}
166    */
167   public function isInstallable() {
168     return $this->id != 'isinstallable' || \Drupal::state()->get('config_test.isinstallable');
169   }
170
171 }