summaryrefslogtreecommitdiffstats
path: root/core/modules/config/src/Tests/ConfigInstallProfileUnmetDependenciesTest.php
blob: 6683580eab74418fae3f6b445a8d4037a08f75fb (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
<?php

namespace Drupal\config\Tests;

use Drupal\Core\Config\InstallStorage;
use Drupal\Core\Serialization\Yaml;
use Drupal\simpletest\InstallerTestBase;

/**
 * Tests install profile config overrides can not add unmet dependencies.
 *
 * @group Config
 */
class ConfigInstallProfileUnmetDependenciesTest extends InstallerTestBase {

  /**
   * The installation profile to install.
   *
   * @var string
   */
  protected $profile = 'testing_config_overrides';

  /**
   * Set to TRUE if the expected exception is thrown.
   *
   * @var bool
   */
  protected $expectedException = FALSE;

  protected function setUp() {
    // Copy the testing_config_overrides install profile so we can change the
    // configuration to include a dependency that can not be met. File API
    // functions are not available yet.
    $dest = $this->siteDirectory . '/profiles/testing_config_overrides';
    mkdir($dest, 0777, TRUE);
    $source = DRUPAL_ROOT . '/core/profiles/testing_config_overrides';
    $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($source, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST);
    foreach ($iterator as $item) {
      if ($item->isDir()) {
        mkdir($dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName());
      }
      else {
        copy($item, $dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName());
      }
    }

    // Add a dependency that can not be met because User is installed before
    // Action.
    $config_file = $dest . DIRECTORY_SEPARATOR . InstallStorage::CONFIG_INSTALL_DIRECTORY . DIRECTORY_SEPARATOR . 'system.action.user_block_user_action.yml';
    $action = Yaml::decode(file_get_contents($config_file));
    $action['dependencies']['module'][] = 'action';
    file_put_contents($config_file, Yaml::encode($action));

    parent::setUp();
  }

  /**
   * {@inheritdoc}
   *
   * Override the error method so we can test for the expected exception.
   */
  protected function error($message = '', $group = 'Other', array $caller = NULL) {
    if ($group == 'User notice') {
      // Since 'User notice' is set by trigger_error() which is used for debug
      // set the message to a status of 'debug'.
      return $this->assert('debug', $message, 'Debug', $caller);
    }
    if ($group == 'Drupal\Core\Config\UnmetDependenciesException') {
      $this->expectedException = TRUE;
      return FALSE;
    }
    return $this->assert('exception', $message, $group, $caller);
  }

  /**
   * {@inheritdoc}
   */
  protected function setUpSite() {
    // This step is not reached due to the exception.
  }


  /**
   * Confirms that the installation succeeded.
   */
  public function testInstalled() {
    if ($this->expectedException) {
      $this->pass('Expected Drupal\Core\Config\UnmetDependenciesException exception thrown');
    }
    else {
      $this->fail('Expected Drupal\Core\Config\UnmetDependenciesException exception thrown');
    }
    $this->assertErrorLogged('Configuration objects provided by <em class="placeholder">user</em> have unmet dependencies: <em class="placeholder">system.action.user_block_user_action (action)</em>');
  }

}