8615da5865b8e1130d4947b09f1d228226a46951
[yaffs-website] / web / core / modules / config / tests / src / Functional / ConfigInstallProfileOverrideTest.php
1 <?php
2
3 namespace Drupal\Tests\config\Functional;
4
5 use Drupal\Component\Utility\Crypt;
6 use Drupal\Core\Config\InstallStorage;
7 use Drupal\Tests\BrowserTestBase;
8 use Drupal\Core\Config\FileStorage;
9 use Drupal\system\Entity\Action;
10 use Drupal\tour\Entity\Tour;
11
12 /**
13  * Tests installation and removal of configuration objects in install, disable
14  * and uninstall functionality.
15  *
16  * @group config
17  */
18 class ConfigInstallProfileOverrideTest extends BrowserTestBase {
19
20   /**
21    * The profile to install as a basis for testing.
22    *
23    * @var string
24    */
25   protected $profile = 'testing_config_overrides';
26
27   /**
28    * Tests install profile config changes.
29    */
30   public function testInstallProfileConfigOverwrite() {
31     $config_name = 'system.cron';
32     // The expected configuration from the system module.
33     $expected_original_data = [
34       'threshold' => [
35         'requirements_warning' => 172800,
36         'requirements_error' => 1209600,
37       ],
38       'logging' => 1,
39     ];
40     // The expected active configuration altered by the install profile.
41     $expected_profile_data = [
42       'threshold' => [
43         'requirements_warning' => 259200,
44         'requirements_error' => 1209600,
45       ],
46       'logging' => 1,
47     ];
48     $expected_profile_data['_core']['default_config_hash'] = Crypt::hashBase64(serialize($expected_profile_data));
49
50     // Verify that the original data matches. We have to read the module config
51     // file directly, because the install profile default system.cron.yml
52     // configuration file was used to create the active configuration.
53     $config_dir = drupal_get_path('module', 'system') . '/' . InstallStorage::CONFIG_INSTALL_DIRECTORY;
54     $this->assertTrue(is_dir($config_dir));
55     $source_storage = new FileStorage($config_dir);
56     $data = $source_storage->read($config_name);
57     $this->assertIdentical($data, $expected_original_data);
58
59     // Verify that active configuration matches the expected data, which was
60     // created from the testing install profile's system.cron.yml file.
61     $config = $this->config($config_name);
62     $this->assertIdentical($config->get(), $expected_profile_data);
63
64     // Ensure that the configuration entity has the expected dependencies and
65     // overrides.
66     $action = Action::load('user_block_user_action');
67     $this->assertEqual($action->label(), 'Overridden block the selected user(s)');
68     $action = Action::load('user_cancel_user_action');
69     $this->assertEqual($action->label(), 'Cancel the selected user account(s)', 'Default configuration that is not overridden is not affected.');
70
71     // Ensure that optional configuration can be overridden.
72     $tour = Tour::load('language');
73     $this->assertEqual(count($tour->getTips()), 1, 'Optional configuration can be overridden. The language tour only has one tip');
74     $tour = Tour::load('language-add');
75     $this->assertEqual(count($tour->getTips()), 3, 'Optional configuration that is not overridden is not affected.');
76
77     // Ensure that optional configuration from a profile is created if
78     // dependencies are met.
79     $this->assertEqual(Tour::load('testing_config_overrides')->label(), 'Config override test');
80
81     // Ensure that optional configuration from a profile is not created if
82     // dependencies are not met. Cannot use the entity system since the entity
83     // type does not exist.
84     $optional_dir = drupal_get_path('module', 'testing_config_overrides') . '/' . InstallStorage::CONFIG_OPTIONAL_DIRECTORY;
85     $optional_storage = new FileStorage($optional_dir);
86     foreach (['config_test.dynamic.dotted.default', 'config_test.dynamic.override', 'config_test.dynamic.override_unmet'] as $id) {
87       $this->assertTrue(\Drupal::config($id)->isNew(), "The config_test entity $id contained in the profile's optional directory does not exist.");
88       // Make that we don't get false positives from the assertion above.
89       $this->assertTrue($optional_storage->exists($id), "The config_test entity $id does exist in the profile's optional directory.");
90     }
91
92     // Install the config_test module and ensure that the override from the
93     // install profile is used. Optional configuration can override
94     // configuration in a modules config/install directory.
95     $this->container->get('module_installer')->install(['config_test']);
96     $this->rebuildContainer();
97     $config_test_storage = \Drupal::entityManager()->getStorage('config_test');
98     $this->assertEqual($config_test_storage->load('dotted.default')->label(), 'Default install profile override', 'The config_test entity is overridden by the profile optional configuration.');
99     // Test that override of optional configuration does work.
100     $this->assertEqual($config_test_storage->load('override')->label(), 'Override', 'The optional config_test entity is overridden by the profile optional configuration.');
101     // Test that override of optional configuration which introduces an unmet
102     // dependency does not get created.
103     $this->assertNull($config_test_storage->load('override_unmet'), 'The optional config_test entity with unmet dependencies is not created.');
104     $this->assertNull($config_test_storage->load('completely_new'), 'The completely new optional config_test entity with unmet dependencies is not created.');
105
106     // Installing dblog creates the optional configuration.
107     $this->container->get('module_installer')->install(['dblog']);
108     $this->rebuildContainer();
109     $this->assertEqual($config_test_storage->load('override_unmet')->label(), 'Override', 'The optional config_test entity is overridden by the profile optional configuration and is installed when its dependencies are met.');
110     $config_test_new = $config_test_storage->load('completely_new');
111     $this->assertEqual($config_test_new->label(), 'Completely new optional configuration', 'The optional config_test entity is provided by the profile optional configuration and is installed when its dependencies are met.');
112     $config_test_new->delete();
113
114     // Install another module that provides optional configuration and ensure
115     // that deleted profile configuration is not re-created.
116     $this->container->get('module_installer')->install(['config_other_module_config_test']);
117     $this->rebuildContainer();
118     $config_test_storage = \Drupal::entityManager()->getStorage('config_test');
119     $this->assertNull($config_test_storage->load('completely_new'));
120   }
121
122 }