View source
<?php
namespace Drupal\Tests\Core\Config;
use Drupal\Core\Config\MemoryStorage;
use Drupal\Core\Config\StorageCopyTrait;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Logger\LoggerChannelInterface;
use Drupal\Tests\UnitTestCase;
use Prophecy\Argument;
class StorageCopyTraitTest extends UnitTestCase {
use StorageCopyTrait;
public function testReplaceStorageContents($source_collections, $target_collections) {
$source = new MemoryStorage();
$target = new MemoryStorage();
$this
->assertEquals(self::toArray($source), self::toArray($target));
$this
->generateRandomData($source, $source_collections);
$this
->assertNotEquals(self::toArray($source), self::toArray($target));
$this
->generateRandomData($target, $target_collections);
$this
->assertNotEquals(self::toArray($source), self::toArray($target));
if ($source_collections) {
$collections = $source
->getAllCollectionNames();
$source = $source
->createCollection($collections[array_rand($collections)]);
}
if ($target_collections) {
$collections = $target
->getAllCollectionNames();
$target = $target
->createCollection($collections[array_rand($collections)]);
}
$source_data = self::toArray($source);
$source_name = $source
->getCollectionName();
self::replaceStorageContents($source, $target);
$this
->assertEquals($source_data, self::toArray($target));
$this
->assertEquals($source_data, self::toArray($source));
$this
->assertEquals($source_name, $source
->getCollectionName());
$this
->assertEquals($source_name, $target
->getCollectionName());
}
public function providerTestReplaceStorageContents() {
$data = [];
$data[] = [
TRUE,
TRUE,
];
$data[] = [
TRUE,
FALSE,
];
$data[] = [
FALSE,
TRUE,
];
$data[] = [
FALSE,
FALSE,
];
return $data;
}
protected static function toArray(MemoryStorage $storage) {
$reflection = new \ReflectionObject($storage);
$property = $reflection
->getProperty('config');
$property
->setAccessible(TRUE);
return $property
->getValue($storage)
->getArrayCopy();
}
protected function generateRandomData(StorageInterface $storage, $collections = TRUE) {
$generator = $this
->getRandomGenerator();
for ($i = 0; $i < rand(2, 10); $i++) {
$storage
->write($this
->randomMachineName(), (array) $generator
->object());
}
if ($collections) {
for ($i = 0; $i < rand(1, 5); $i++) {
$collection = $storage
->createCollection($this
->randomMachineName());
for ($i = 0; $i < rand(2, 10); $i++) {
$collection
->write($this
->randomMachineName(), (array) $generator
->object());
}
}
}
}
public function testWithInvalidConfiguration() {
$source = new TestStorage();
$this
->generateRandomData($source);
$names = $source
->listAll();
$test_name = reset($names);
$source
->setValue($test_name, FALSE);
$logger_factory = $this
->prophesize(LoggerChannelFactoryInterface::class);
$container = new ContainerBuilder();
$container
->set('logger.factory', $logger_factory
->reveal());
\Drupal::setContainer($container);
$channel = $this
->prophesize(LoggerChannelInterface::class);
$logger_factory
->get('config')
->willReturn($channel
->reveal());
$channel
->notice('Missing required data for configuration: %config', Argument::withEntry('%config', $test_name))
->shouldBeCalled();
$target = new TestStorage();
self::replaceStorageContents($source, $target);
foreach ($names as $name) {
if ($name === $test_name) {
$this
->assertFalse($source
->read($name));
$this
->assertFalse($target
->exists($name));
}
else {
$this
->assertEquals($source
->read($name), $target
->read($name));
}
}
$this
->assertContains($test_name, $source
->listAll());
$this
->assertNotContains($test_name, $target
->listAll());
}
}
class TestStorage extends MemoryStorage {
public function setValue($name, $value) {
$this->config[$this->collection][$name] = $value;
}
}