ConfigIgnorePatternResolverTest.php 5.75 KB
Newer Older
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
<?php

namespace Drupal\Tests\config_ignore\Unit;

use Drupal\config_ignore\EventSubscriber\ConfigIgnoreEventSubscriber;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\ImmutableConfig;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Tests\UnitTestCase;

/**
 * Tests the config ignore pattern resolver.
 *
 * @coversDefaultClass \Drupal\config_ignore\EventSubscriber\ConfigIgnoreEventSubscriber
 *
 * @group config_ignore
 */
class ConfigIgnorePatternResolverTest extends UnitTestCase {

  /**
   * Tests the config ignore pattern resolver with an invalid patterns.
   *
   * @param string $pattern
   *   The pattern to be tested.
   * @param string $expected_exception_message
   *   The expected exception message.
   *
   * @throws \ReflectionException
   *   If the class does not exist.
   *
   * @covers ::getIgnoredConfigs
   * @dataProvider dataProviderTestInvalidPattern
   */
  public function testInvalidPattern($pattern, $expected_exception_message) {
    $this->expectException(\LogicException::class);
    $this->expectExceptionMessage($expected_exception_message);
    $this->getIgnoredConfigs([$pattern], ['foo.bar']);
  }

  /**
   * Provides testing cases for ::testInvalidPattern().
   *
   * @return array
   *   A list of arrays. Each array has two items:
   *   - The config ignore pattern.
   *   - The expected exception message.
   *
   * @see self::testInvalidPattern()
   */
  public function dataProviderTestInvalidPattern() {
    return [
      'tilda & asterisk' => [
        '~foo.bar.*',
        "A config ignore pattern entry cannot contain both, '~' and '*'.",
      ],
      'asterisk in key' => [
        'foo.bar:key.*',
        "The key part of the config ignore pattern cannot contain the wildcard character '*'.",
      ],
    ];
  }

  /**
   * Tests the config ignore pattern resolver.
   *
   * @throws \ReflectionException
   *   If the class does not exist.
   *
   * @covers ::getIgnoredConfigs
   */
  public function testGetIgnoredConfigs() {
    $ignoredConfigs = $this->getIgnoredConfigs(
      // Ignored config patterns.
      [
        // Non-existing simple ignore pattern.
        'non.existing',
        // Simple ignore pattern.
        'foo.bar',
        // Suffix wildcard ignore pattern.
        'foo.bar.*',
        // Excluding foo.bar.suffix4.
        '~foo.bar.suffix4',
        // Prefix wildcard ignore pattern.
        '*.foo.bar',
        // Excluding prefix2.foo.bar.
        '~prefix2.foo.bar',
        // Middle wildcard ignore pattern.
        'foo.*.bar',
        // Excluding foo.middle1.bar.
        '~foo.middle1.bar',
        // Ignore pattern with key.
        'foo.baz.qux:path.to.key',
        // A 2nd key of the same config is appended.
        'foo.baz.qux:a.second.key',
        // Ignore pattern with key when the same config has been already added.
        'foo.bar:some.key',
        // Ignore pattern with key that will be overwritten later with the same
        // config but without key.
        'baz.qux:with.some.key',
        // Only this will be outputted as it covers also the one with a key.
        'baz.qux',
      ],
      // All configs.
      [
        'foo.bar',
        'foo.bar.suffix1',
        'foo.bar.suffix2',
        'foo.bar.suffix3',
        'foo.bar.suffix4',
        'prefix1.foo.bar',
        'prefix2.foo.bar',
        'prefix3.foo.bar',
        'foo.middle1.bar',
        'foo.middle2.bar',
        'foo.middle3.bar',
        'foo.baz.qux',
        'baz.qux',
      ]
    );

    $this->assertSame([
      'foo.bar' => NULL,
      'foo.bar.suffix1' => NULL,
      'foo.bar.suffix2' => NULL,
      'foo.bar.suffix3' => NULL,
      'prefix1.foo.bar' => NULL,
      'prefix3.foo.bar' => NULL,
      'foo.middle2.bar' => NULL,
      'foo.middle3.bar' => NULL,
      'foo.baz.qux' => [
        ['path', 'to', 'key'],
        ['a', 'second', 'key'],
      ],
      'baz.qux' => NULL,
    ], $ignoredConfigs);
  }

  /**
   * Returns all ignored configs by expanding the wildcards.
   *
   * Basically, it provides mocked services and it's a wrapper around the
   * protected method ConfigIgnoreEventSubscriber::getIgnoredConfigs().
   *
   * @param array $ignore_config_patterns
   *   A list of config ignore patterns.
   * @param array $all_configs
   *   A list of names of all configs.
   *
   * @return array
   *   A list of ignored configs as is returned by
   *   ConfigIgnoreEventSubscriber::getIgnoredConfigs()
   *
   * @throws \ReflectionException
   *   If the class does not exist.
   *
   * @see \Drupal\config_ignore\EventSubscriber\ConfigIgnoreEventSubscriber::getIgnoredConfigs()
   */
  protected function getIgnoredConfigs(array $ignore_config_patterns, array $all_configs) {
    $configIgnoreSettings = $this->prophesize(ImmutableConfig::class);
    $configIgnoreSettings->get('ignored_config_entities')->willReturn($ignore_config_patterns);

    $configFactory = $this->prophesize(ConfigFactoryInterface::class);
    $configFactory->get('config_ignore.settings')->willReturn($configIgnoreSettings->reveal());
165 166 167

    $transformation_storage = $this->prophesize(StorageInterface::class);
    $transformation_storage->listAll()->willReturn($all_configs);
168 169 170 171 172 173 174 175 176 177 178 179 180

    $subscriber = new ConfigIgnoreEventSubscriber(
      $configFactory->reveal(),
      $this->prophesize(ModuleHandlerInterface::class)->reveal(),
      $this->prophesize(StorageInterface::class)->reveal(),
      $this->prophesize(StorageInterface::class)->reveal()
    );

    // Make ConfigIgnoreEventSubscriber::getIgnoredConfigs() accessible.
    $class = new \ReflectionClass($subscriber);
    $getIgnoredConfigsMethod = $class->getMethod('getIgnoredConfigs');
    $getIgnoredConfigsMethod->setAccessible(TRUE);

181
    return $getIgnoredConfigsMethod->invokeArgs($subscriber, [$transformation_storage->reveal()]);
182 183 184
  }

}