Security update to Drupal 8.4.6
[yaffs-website] / web / core / modules / config / tests / src / Functional / ConfigDependencyWebTest.php
1 <?php
2
3 namespace Drupal\Tests\config\Functional;
4
5 use Drupal\Core\Config\Entity\ConfigEntityStorage;
6 use Drupal\Tests\BrowserTestBase;
7
8 /**
9  * Tests configuration entities.
10  *
11  * @group config
12  */
13 class ConfigDependencyWebTest extends BrowserTestBase {
14
15   /**
16    * The maximum length for the entity storage used in this test.
17    */
18   const MAX_ID_LENGTH = ConfigEntityStorage::MAX_ID_LENGTH;
19
20   /**
21    * Modules to enable.
22    *
23    * @var array
24    */
25   public static $modules = ['config_test'];
26
27   /**
28    * Tests ConfigDependencyDeleteFormTrait.
29    *
30    * @see \Drupal\Core\Config\Entity\ConfigDependencyDeleteFormTrait
31    */
32   public function testConfigDependencyDeleteFormTrait() {
33     $this->drupalLogin($this->drupalCreateUser(['administer site configuration']));
34
35     /** @var \Drupal\Core\Config\Entity\ConfigEntityStorage $storage */
36     $storage = $this->container->get('entity.manager')->getStorage('config_test');
37     // Entity1 will be deleted by the test.
38     $entity1 = $storage->create(
39       [
40         'id' => 'entity1',
41         'label' => 'Entity One',
42       ]
43     );
44     $entity1->save();
45
46     // Entity2 has a dependency on Entity1 but it can be fixed because
47     // \Drupal\config_test\Entity::onDependencyRemoval() will remove the
48     // dependency before config entities are deleted.
49     $entity2 = $storage->create(
50       [
51         'id' => 'entity2',
52         'dependencies' => [
53           'enforced' => [
54             'config' => [$entity1->getConfigDependencyName()],
55           ],
56         ],
57       ]
58     );
59     $entity2->save();
60
61     $this->drupalGet($entity2->urlInfo('delete-form'));
62     $this->assertNoText(t('Configuration updates'), 'No configuration updates found.');
63     $this->assertNoText(t('Configuration deletions'), 'No configuration deletes found.');
64     $this->drupalGet($entity1->urlInfo('delete-form'));
65     $this->assertNoText(t('Configuration updates'), 'No configuration updates found.');
66     $this->assertText(t('Configuration deletions'), 'Configuration deletions found.');
67     $this->assertText($entity2->id(), 'Entity2 id found');
68     $this->drupalPostForm($entity1->urlInfo('delete-form'), [], 'Delete');
69     $storage->resetCache();
70     $this->assertFalse($storage->loadMultiple([$entity1->id(), $entity2->id()]), 'Test entities deleted');
71
72     // Set a more complicated test where dependencies will be fixed.
73     // Entity1 will be deleted by the test.
74     $entity1 = $storage->create(
75       [
76         'id' => 'entity1',
77       ]
78     );
79     $entity1->save();
80     \Drupal::state()->set('config_test.fix_dependencies', [$entity1->getConfigDependencyName()]);
81
82     // Entity2 has a dependency on Entity1 but it can be fixed because
83     // \Drupal\config_test\Entity::onDependencyRemoval() will remove the
84     // dependency before config entities are deleted.
85     $entity2 = $storage->create(
86       [
87         'id' => 'entity2',
88         'label' => 'Entity Two',
89         'dependencies' => [
90           'enforced' => [
91             'config' => [$entity1->getConfigDependencyName()],
92           ],
93         ],
94       ]
95     );
96     $entity2->save();
97
98     // Entity3 will be unchanged because it is dependent on Entity2 which can
99     // be fixed.
100     $entity3 = $storage->create(
101       [
102         'id' => 'entity3',
103         'dependencies' => [
104           'enforced' => [
105             'config' => [$entity2->getConfigDependencyName()],
106           ],
107         ],
108       ]
109     );
110     $entity3->save();
111
112     $this->drupalGet($entity1->urlInfo('delete-form'));
113     $this->assertText(t('Configuration updates'), 'Configuration updates found.');
114     $this->assertNoText(t('Configuration deletions'), 'No configuration deletions found.');
115     $this->assertNoText($entity2->id(), 'Entity2 id not found');
116     $this->assertText($entity2->label(), 'Entity2 label not found');
117     $this->assertNoText($entity3->id(), 'Entity3 id not found');
118     $this->drupalPostForm($entity1->urlInfo('delete-form'), [], 'Delete');
119     $storage->resetCache();
120     $this->assertFalse($storage->load('entity1'), 'Test entity 1 deleted');
121     $entity2 = $storage->load('entity2');
122     $this->assertTrue($entity2, 'Entity 2 not deleted');
123     $this->assertEqual($entity2->calculateDependencies()->getDependencies()['config'], [], 'Entity 2 dependencies updated to remove dependency on Entity1.');
124     $entity3 = $storage->load('entity3');
125     $this->assertTrue($entity3, 'Entity 3 not deleted');
126     $this->assertEqual($entity3->calculateDependencies()->getDependencies()['config'], [$entity2->getConfigDependencyName()], 'Entity 3 still depends on Entity 2.');
127
128   }
129
130 }