View source
<?php
namespace Drupal\Tests\Core\Config;
use Drupal\Component\Uuid\Php;
use Drupal\Core\Config\StorageComparer;
use Drupal\Tests\UnitTestCase;
class StorageComparerTest extends UnitTestCase {
protected $sourceStorage;
protected $targetStorage;
protected $configManager;
protected $storageComparer;
protected $configData;
protected function setUp() {
$this->sourceStorage = $this
->getMock('Drupal\\Core\\Config\\StorageInterface');
$this->targetStorage = $this
->getMock('Drupal\\Core\\Config\\StorageInterface');
$this->configManager = $this
->getMock('Drupal\\Core\\Config\\ConfigManagerInterface');
$this->storageComparer = new StorageComparer($this->sourceStorage, $this->targetStorage, $this->configManager);
}
protected function getConfigData() {
$uuid = new Php();
$this->configData = array(
'system.site' => array(
'title' => 'Drupal',
'uuid' => $uuid
->generate(),
),
'field.field.node.article.body' => array(
'id' => 'node.article.body',
'uuid' => $uuid
->generate(),
'dependencies' => array(
'config' => array(
'field.storage.node.body',
),
),
),
'field.storage.node.body' => array(
'id' => 'node.body',
'uuid' => $uuid
->generate(),
'dependencies' => array(
'module' => array(
'text',
),
),
),
'views.view.test_view' => array(
'id' => 'test_view',
'uuid' => $uuid
->generate(),
'dependencies' => array(
'module' => array(
'node',
),
),
),
'system.performance' => array(
'stale_file_threshold' => 2592000,
),
);
return $this->configData;
}
public function testCreateChangelistNoChange() {
$config_data = $this
->getConfigData();
$config_files = array_keys($config_data);
$this->sourceStorage
->expects($this
->once())
->method('listAll')
->will($this
->returnValue($config_files));
$this->targetStorage
->expects($this
->once())
->method('listAll')
->will($this
->returnValue($config_files));
$this->sourceStorage
->expects($this
->once())
->method('readMultiple')
->will($this
->returnValue($config_data));
$this->targetStorage
->expects($this
->once())
->method('readMultiple')
->will($this
->returnValue($config_data));
$this->sourceStorage
->expects($this
->once())
->method('getAllCollectionNames')
->will($this
->returnValue(array()));
$this->targetStorage
->expects($this
->once())
->method('getAllCollectionNames')
->will($this
->returnValue(array()));
$this->storageComparer
->createChangelist();
$this
->assertEmpty($this->storageComparer
->getChangelist('create'));
$this
->assertEmpty($this->storageComparer
->getChangelist('delete'));
$this
->assertEmpty($this->storageComparer
->getChangelist('update'));
}
public function testCreateChangelistCreate() {
$target_data = $source_data = $this
->getConfigData();
unset($target_data['field.storage.node.body']);
unset($target_data['field.field.node.article.body']);
unset($target_data['views.view.test_view']);
$this->sourceStorage
->expects($this
->once())
->method('listAll')
->will($this
->returnValue(array_keys($source_data)));
$this->targetStorage
->expects($this
->once())
->method('listAll')
->will($this
->returnValue(array_keys($target_data)));
$this->sourceStorage
->expects($this
->once())
->method('readMultiple')
->will($this
->returnValue($source_data));
$this->targetStorage
->expects($this
->once())
->method('readMultiple')
->will($this
->returnValue($target_data));
$this->sourceStorage
->expects($this
->once())
->method('getAllCollectionNames')
->will($this
->returnValue(array()));
$this->targetStorage
->expects($this
->once())
->method('getAllCollectionNames')
->will($this
->returnValue(array()));
$this->storageComparer
->createChangelist();
$expected = array(
'field.storage.node.body',
'views.view.test_view',
'field.field.node.article.body',
);
$this
->assertEquals($expected, $this->storageComparer
->getChangelist('create'));
$this
->assertEmpty($this->storageComparer
->getChangelist('delete'));
$this
->assertEmpty($this->storageComparer
->getChangelist('update'));
}
public function testCreateChangelistDelete() {
$target_data = $source_data = $this
->getConfigData();
unset($source_data['field.storage.node.body']);
unset($source_data['field.field.node.article.body']);
unset($source_data['views.view.test_view']);
$this->sourceStorage
->expects($this
->once())
->method('listAll')
->will($this
->returnValue(array_keys($source_data)));
$this->targetStorage
->expects($this
->once())
->method('listAll')
->will($this
->returnValue(array_keys($target_data)));
$this->sourceStorage
->expects($this
->once())
->method('readMultiple')
->will($this
->returnValue($source_data));
$this->targetStorage
->expects($this
->once())
->method('readMultiple')
->will($this
->returnValue($target_data));
$this->sourceStorage
->expects($this
->once())
->method('getAllCollectionNames')
->will($this
->returnValue(array()));
$this->targetStorage
->expects($this
->once())
->method('getAllCollectionNames')
->will($this
->returnValue(array()));
$this->storageComparer
->createChangelist();
$expected = array(
'field.field.node.article.body',
'views.view.test_view',
'field.storage.node.body',
);
$this
->assertEquals($expected, $this->storageComparer
->getChangelist('delete'));
$this
->assertEmpty($this->storageComparer
->getChangelist('create'));
$this
->assertEmpty($this->storageComparer
->getChangelist('update'));
}
public function testCreateChangelistUpdate() {
$target_data = $source_data = $this
->getConfigData();
$source_data['system.site']['title'] = 'Drupal New!';
$source_data['field.field.node.article.body']['new_config_key'] = 'new data';
$source_data['field.storage.node.body']['new_config_key'] = 'new data';
$this->sourceStorage
->expects($this
->once())
->method('listAll')
->will($this
->returnValue(array_keys($source_data)));
$this->targetStorage
->expects($this
->once())
->method('listAll')
->will($this
->returnValue(array_keys($target_data)));
$this->sourceStorage
->expects($this
->once())
->method('readMultiple')
->will($this
->returnValue($source_data));
$this->targetStorage
->expects($this
->once())
->method('readMultiple')
->will($this
->returnValue($target_data));
$this->sourceStorage
->expects($this
->once())
->method('getAllCollectionNames')
->will($this
->returnValue(array()));
$this->targetStorage
->expects($this
->once())
->method('getAllCollectionNames')
->will($this
->returnValue(array()));
$this->storageComparer
->createChangelist();
$expected = array(
'field.storage.node.body',
'system.site',
'field.field.node.article.body',
);
$this
->assertEquals($expected, $this->storageComparer
->getChangelist('update'));
$this
->assertEmpty($this->storageComparer
->getChangelist('create'));
$this
->assertEmpty($this->storageComparer
->getChangelist('delete'));
}
}