You are here

public function SplitFilterTest::testFilterWrite in Configuration Split 8

Test that the filter writes correctly.

File

src/Tests/SplitFilterTest.php, line 261

Class

SplitFilterTest
Test filter plugin.

Namespace

Drupal\config_split\Tests

Code

public function testFilterWrite() {

  // Transparent filter.
  $name = $this
    ->randomMachineName();
  $data = (array) $this
    ->getRandomGenerator()
    ->object();
  try {
    $filter = $this
      ->getFilter();
    $filter
      ->filterWrite($name, $data);
    $this
      ->fail('The filter needs a storage.');
  } catch (\InvalidArgumentException $exception) {
    $this
      ->assertTrue(TRUE, 'Exception thrown.');
  }
  $filter = $this
    ->getFilter(new NullStorage());
  $this
    ->assertEquals($data, $filter
    ->filterWrite($name, $data));

  // Filter with a blacklist.
  $name2 = $this
    ->randomMachineName();
  $filter = $this
    ->getFilter(new NullStorage(), [
    $name2,
  ], [], []);
  $this
    ->assertEquals($data, $filter
    ->filterWrite($name, $data));
  $this
    ->assertNull($filter
    ->filterWrite($name2, $data));

  // Filter with a blacklist and a storage.
  $storage = $this
    ->prophesize(StorageInterface::class);
  $storage
    ->write(Argument::cetera())
    ->willReturn(TRUE);
  $storage
    ->exists($name)
    ->willReturn(FALSE);
  $filter = $this
    ->getFilter($storage
    ->reveal(), [
    $name2,
  ], [], []);
  $this
    ->assertEquals($data, $filter
    ->filterWrite($name, $data));
  $this
    ->assertNull($filter
    ->filterWrite($name2, $data));

  // Filter with a gray list and a storage.
  $name3 = $this
    ->randomMachineName();
  $data3 = (array) $this
    ->getRandomGenerator()
    ->object();
  $storage = $this
    ->prophesize(StorageInterface::class);
  $storage
    ->write(Argument::cetera())
    ->willReturn(TRUE);
  $storage
    ->read($name3)
    ->willReturn($data3);
  $storage
    ->exists($name)
    ->willReturn(TRUE);
  $storage
    ->delete($name)
    ->willReturn(TRUE)
    ->shouldBeCalled();
  $storage = $storage
    ->reveal();
  $filter = $this
    ->getFilter($storage, [
    $name2,
  ], [], [], [
    $name3,
  ]);
  $filter
    ->setSourceStorage($storage);
  $this
    ->assertEquals($data, $filter
    ->filterWrite($name, $data));
  $this
    ->assertNull($filter
    ->filterWrite($name2, $data));
  $this
    ->assertEquals($data3, $filter
    ->filterWrite($name3, $data));

  // Filter with graylist and skipping equal data.
  $primary = $this
    ->prophesize(StorageInterface::class);
  $primary
    ->read($name3)
    ->willReturn($data3);
  $primary = $primary
    ->reveal();
  $secondary = $this
    ->prophesize(StorageInterface::class);
  $secondary
    ->exists($name)
    ->willReturn(FALSE);
  $secondary
    ->write($name2, $data)
    ->willReturn(TRUE);
  $secondary
    ->write($name3, $data)
    ->willReturn(TRUE);
  $secondary
    ->exists($name3)
    ->willReturn(TRUE);
  $secondary
    ->delete($name3)
    ->willReturn(TRUE)
    ->shouldBeCalled();
  $secondary = $secondary
    ->reveal();
  $filter = $this
    ->getFilter($secondary, [
    $name2,
  ], [], [], [
    $name3,
  ], 'test', TRUE);
  $filter
    ->setSourceStorage($primary);
  $this
    ->assertEquals($data, $filter
    ->filterWrite($name, $data));
  $this
    ->assertNull($filter
    ->filterWrite($name2, $data));
  $this
    ->assertEquals($data3, $filter
    ->filterWrite($name3, $data));
  $this
    ->assertEquals($data3, $filter
    ->filterWrite($name3, $data3));

  // Test that extensions are correctly removed.
  $extensions = [
    'module' => [
      'config' => 0,
      'user' => 0,
      'views_ui' => 0,
      'menu_link_content' => 1,
      'views' => 10,
    ],
    'theme' => [
      'stable' => 0,
      'classy' => 0,
    ],
  ];
  $modules = [
    'module1' => 0,
    'module2' => 1,
  ];
  $themes = [
    'custom_theme' => 0,
  ];
  $extensions_extra = [
    'module' => [
      'config' => 0,
      'module1' => 0,
      'user' => 0,
      'views_ui' => 0,
      'menu_link_content' => 1,
      'module2' => 1,
      'views' => 10,
    ],
    'theme' => [
      'stable' => 0,
      'classy' => 0,
      'custom_theme' => 0,
    ],
  ];
  $filter = $this
    ->getFilter(new NullStorage(), [], $modules, $themes);
  $this
    ->assertEquals($extensions, $filter
    ->filterWrite('core.extension', $extensions));
  $this
    ->assertEquals($extensions, $filter
    ->filterWrite('core.extension', $extensions_extra));

  // Test that empty config is not written to the split storage.
  $storage = $this
    ->prophesize(StorageInterface::class);
  $storage
    ->write($name2, [])
    ->shouldNotBeCalled();
  $storage
    ->write($name3, [])
    ->shouldNotBeCalled();
  $filter = $this
    ->getFilter($storage
    ->reveal(), [
    $name2,
  ], [], [], [
    $name3,
  ], 'test', TRUE);
  $this
    ->assertNull($filter
    ->filterWrite($name2, []));
  $this
    ->assertNull($filter
    ->filterWrite($name3, []));
}