summaryrefslogtreecommitdiffstats
path: root/tests/src/Kernel/DefaultConfigTest.php
blob: 08132e0999dbc2483f6d64cce93baef71226be4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php

namespace Drupal\Tests\agov\Kernel;

use Drupal\Core\Config\FileStorage;
use Drupal\Core\Config\InstallStorage;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\Site\Settings;
use Drupal\KernelTests\AssertConfigTrait;
use Drupal\KernelTests\KernelTestBase;

/**
 * Tests that the installed config matches the default config.
 *
 * @group agov
 */
class DefaultConfigTest extends KernelTestBase {

  use AssertConfigTrait;

  /**
   * Schema issues are caught in the aGov web tests.
   *
   * {@inheritdoc}
   */
  protected $strictConfigSchema = FALSE;

  public static $modules = [
    'system',
    'user',
    'node',
    'field',
    'history',
    'block',
    'breakpoint',
    'ckeditor',
    'color',
    'config',
    'comment',
    'contextual',
    'text',
    'contact',
    'menu_link_content',
    'datetime',
    'block_content',
    'quickedit',
    'editor',
    'entity_reference',
    'help',
    'image',
    'menu_ui',
    'options',
    'path',
    'page_cache',
    'taxonomy',
    'search',
    'shortcut',
    'toolbar',
    'field_ui',
    'file',
    'filter',
    'rdf',
    'views',
    'views_ui',
    'tour',
    'link',
    'layout_discovery',

    'media_entity',
    'media_entity_browser',
    'media_entity_image',
    'video_embed_media',
    'entity_embed',
    'entity_browser',
    'entity_browser_entity_form',
    'entity_browser',

    'ds',
    'link_attributes',
    'fences',
    'twitter_block',
    'ctools',
    'page_manager',
    'pathauto',
    'token',
    'text_icon',
  ];

  /**
   * An array of config objects not to validate.
   *
   * @var array
   */
  protected $skippedConfig = [
    // @TODO figure out what in the installer is allowing config file overrides
    // and why that doesn't work in KTB?
    'node.settings' => TRUE,
    'system.theme' => TRUE,

    // Block status is not set without a theme enabled.
    'block.block.breadcrumbs' => TRUE,
    'block.block.footerquicklinks' => TRUE,
    'block.block.mainnavigation' => TRUE,
    'block.block.mainnavigation_2' => TRUE,
    'block.block.mainnavigation_3' => TRUE,
    'block.block.pagetitle' => TRUE,
    'block.block.quicklinks' => TRUE,
    'block.block.quicklinks_2' => TRUE,
    'block.block.searchform' => TRUE,
    'block.block.searchform_2' => TRUE,
    'block.block.seven_breadcrumbs' => TRUE,
    'block.block.seven_content' => TRUE,
    'block.block.seven_help' => TRUE,
    'block.block.seven_local_actions' => TRUE,
    'block.block.seven_login' => TRUE,
    'block.block.seven_messages' => TRUE,
    'block.block.seven_primary_local_tasks' => TRUE,
    'block.block.seven_secondary_local_tasks' => TRUE,
    'block.block.sitebranding' => TRUE,
    'block.block.tabs' => TRUE,
  ];

  /**
   * Modules that we don't want to validate the config for. Should be none.
   *
   * @var array
   */
  protected $excludedModules = [
    // This causes many issues when installed with KTB so ignore it.
    'agov_default_content',
    // @TODO, move to contrib.
    'text_icon',

    // Optional modules with config are not currently supported.
    'agov_workbench',
    'agov_scheduled_updates',
    'agov_sitemap',

    // Current schema issues.
    'agov_password_policy',
  ];

  /**
   * The name of the profile.
   *
   * @var string
   */
  protected $profile = 'agov';

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();

    // @todo ModuleInstaller calls system_rebuild_module_data which is part of
    //   system.module, see https://www.drupal.org/node/2208429.
    include_once $this->root . '/core/modules/system/system.module';

    // We must set the active profile as so that we can install its default
    // config below.
    $old_settings = Settings::getAll();
    new Settings(['install_profile' => $this->profile] + $old_settings);

    // Set up the state values so we know where to find the files when running
    // drupal_get_filename().
    // @todo Remove as part of https://www.drupal.org/node/2186491
    system_rebuild_module_data();

    // Install the required schema and config for core modules.
    $this->installSchema('system', 'router');
    $this->installEntitySchema('block_content');
    $this->installEntitySchema('block_content_type');
    $this->installEntitySchema('file');
    $this->installConfig(self::$modules);

    // Install the default config for our installation profile.
    $this->container->get('config.installer')->installDefaultConfig('profile', $this->profile);
  }

  /**
   * Test the configuration for the profile specifically.
   */
  public function testProfileConfig() {
    $default_install_path = drupal_get_path('profile', $this->profile) . '/' . InstallStorage::CONFIG_INSTALL_DIRECTORY;
    $this->assertConfig($default_install_path);
  }

  /**
   * Tests if installed config is equal to the exported config.
   *
   * @dataProvider providerTestModuleConfig
   */
  public function testModuleConfig($module) {
    $this->container->get('module_installer')
      ->install([$module]);

    $default_install_path = drupal_get_path('module', $module) . '/' . InstallStorage::CONFIG_INSTALL_DIRECTORY;
    $this->assertConfig($default_install_path);
  }

  /**
   * Assert the configuration is the same as the installed config.
   *
   * @param string $default_install_path
   *   The full path to the install directory of the extension.
   *
   * @throws \Exception
   */
  protected function assertConfig($default_install_path) {
    $module_config_storage = new FileStorage($default_install_path, StorageInterface::DEFAULT_COLLECTION);

    // Compare the installed config with the one in the module directory.
    foreach ($module_config_storage->listAll() as $config_name) {
      $result = $this->container->get('config.manager')
        ->diff($module_config_storage, $this->container->get('config.storage'), $config_name);
      $this->assertConfigDiff($result, $config_name, $this->skippedConfig);
    }
  }

  /**
   * Test data provider for ::testModuleConfig().
   *
   * @return array
   *   An array of module names to test.
   */
  public function providerTestModuleConfig() {
    $module_dirs = array_keys(iterator_to_array(new \FilesystemIterator(__DIR__ . '/../../../modules/custom/')));
    $module_names = array_map(function ($path) {
      return str_replace(__DIR__ . '/../../../modules/custom/', '', $path);
    }, $module_dirs);

    $modules = [];
    foreach ($module_names as $module) {
      $modules[$module] = [$module];
    }
    return array_diff_key($modules, array_flip($this->excludedModules));
  }

}