diff --git a/core/tests/Drupal/Tests/Core/Config/StorageComparerTest.php b/core/tests/Drupal/Tests/Core/Config/StorageComparerTest.php new file mode 100644 index 0000000..36915d8 --- /dev/null +++ b/core/tests/Drupal/Tests/Core/Config/StorageComparerTest.php @@ -0,0 +1,232 @@ + '', + 'name' => '\Drupal\Core\Config\StorageComparer unit test', + 'group' => 'Configuration', + ); + } + + public function setUp() { + $this->sourceStorage = $this->getMock('Drupal\Core\Config\StorageInterface'); + $this->targetStorage = $this->getMock('Drupal\Core\Config\StorageInterface'); + $this->storageComparer = new StorageComparer($this->sourceStorage, $this->targetStorage); + } + + protected function getConfigData() { + $uuid = new Php(); + // Mock data using minimal data to use ConfigDependencyManger. + $this->configData = array( + // Simple config that controls configuration sync. + 'system.site' => array( + 'title' => 'Drupal', + 'uuid' => $uuid->generate(), + ), + // Config entity which requires another config entity. + 'field.instance.node.article.body' => array( + 'id' => 'node.article.body', + 'uuid' => $uuid->generate(), + 'dependencies' => array( + 'entity' => array( + 'field.field.node.body' + ), + ), + ), + // Config entity which is required by another config entity. + 'field.field.node.body' => array( + 'id' => 'node.body', + 'uuid' => $uuid->generate(), + 'dependencies' => array( + 'module' => array( + 'text', + ), + ), + ), + // Config entity not which has no dependencies on configuration. + 'views.view.frontpage' => array( + 'id' => 'frontpage', + 'uuid' => $uuid->generate(), + 'dependencies' => array( + 'module' => array( + 'node', + ), + ), + ), + // Simple config. + 'system.performance' => array( + 'stale_file_threshold' => 2592000 + ), + + ); + return $this->configData; + } + + /** + * @covers ::createChangelist + */ + 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->storageComparer->createChangelist(); + $this->assertEmpty($this->storageComparer->getChangelist('create')); + $this->assertEmpty($this->storageComparer->getChangelist('delete')); + $this->assertEmpty($this->storageComparer->getChangelist('update')); + } + + /** + * @covers ::createChangelist + */ + public function testCreateChangelistCreate() { + $target_data = $source_data = $this->getConfigData(); + unset($target_data['field.field.node.body']); + unset($target_data['field.instance.node.article.body']); + unset($target_data['views.view.frontpage']); + + $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->storageComparer->createChangelist(); + $expected = array( + 'field.field.node.body', + 'views.view.frontpage', + 'field.instance.node.article.body', + ); + $this->assertEquals($expected, $this->storageComparer->getChangelist('create')); + $this->assertEmpty($this->storageComparer->getChangelist('delete')); + $this->assertEmpty($this->storageComparer->getChangelist('update')); + } + + /** + * @covers ::createChangelist + */ + public function testCreateChangelistDelete() { + $target_data = $source_data = $this->getConfigData(); + unset($source_data['field.field.node.body']); + unset($source_data['field.instance.node.article.body']); + unset($source_data['views.view.frontpage']); + + $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->storageComparer->createChangelist(); + $expected = array( + 'field.instance.node.article.body', + 'views.view.frontpage', + 'field.field.node.body', + ); + $this->assertEquals($expected, $this->storageComparer->getChangelist('delete')); + $this->assertEmpty($this->storageComparer->getChangelist('create')); + $this->assertEmpty($this->storageComparer->getChangelist('update')); + } + + /** + * @covers ::createChangelist + */ + public function testCreateChangelistUpdate() { + $target_data = $source_data = $this->getConfigData(); + $source_data['system.site']['title'] = 'Drupal New!'; + $source_data['field.instance.node.article.body']['new_config_key'] = 'new data'; + $source_data['field.field.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->storageComparer->createChangelist(); + $expected = array( + 'field.field.node.body', + 'system.site', + 'field.instance.node.article.body', + ); + $this->assertEquals($expected, $this->storageComparer->getChangelist('update')); + $this->assertEmpty($this->storageComparer->getChangelist('create')); + $this->assertEmpty($this->storageComparer->getChangelist('delete')); + } + +}