You are here

class StorageComparerTest in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/tests/Drupal/Tests/Core/Config/StorageComparerTest.php \Drupal\Tests\Core\Config\StorageComparerTest

@coversDefaultClass \Drupal\Core\Config\StorageComparer @group Config

Hierarchy

Expanded class hierarchy of StorageComparerTest

File

core/tests/Drupal/Tests/Core/Config/StorageComparerTest.php, line 18
Contains \Drupal\Tests\Core\Config\StorageComparerTest.

Namespace

Drupal\Tests\Core\Config
View source
class StorageComparerTest extends UnitTestCase {

  /**
   * @var \Drupal\Core\Config\StorageInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $sourceStorage;

  /**
   * @var \Drupal\Core\Config\StorageInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $targetStorage;

  /**
   * @var \Drupal\Core\Config\ConfigManager|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $configManager;

  /**
   * The storage comparer to test.
   *
   * @var \Drupal\Core\Config\StorageComparer
   */
  protected $storageComparer;

  /**
   * An array of test configuration data keyed by configuration name.
   *
   * @var array
   */
  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();

    // 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.field.node.article.body' => array(
        'id' => 'node.article.body',
        'uuid' => $uuid
          ->generate(),
        'dependencies' => array(
          'config' => array(
            'field.storage.node.body',
          ),
        ),
      ),
      // Config entity which is required by another config entity.
      'field.storage.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.test_view' => array(
        'id' => 'test_view',
        '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->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'));
  }

  /**
   * @covers ::createChangelist
   */
  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'));
  }

  /**
   * @covers ::createChangelist
   */
  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'));
  }

  /**
   * @covers ::createChangelist
   */
  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'));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
StorageComparerTest::$configData protected property An array of test configuration data keyed by configuration name.
StorageComparerTest::$configManager protected property
StorageComparerTest::$sourceStorage protected property
StorageComparerTest::$storageComparer protected property The storage comparer to test.
StorageComparerTest::$targetStorage protected property
StorageComparerTest::getConfigData protected function
StorageComparerTest::setUp protected function Overrides UnitTestCase::setUp
StorageComparerTest::testCreateChangelistCreate public function @covers ::createChangelist
StorageComparerTest::testCreateChangelistDelete public function @covers ::createChangelist
StorageComparerTest::testCreateChangelistNoChange public function @covers ::createChangelist
StorageComparerTest::testCreateChangelistUpdate public function @covers ::createChangelist
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root.
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName protected function Mocks a block with a block plugin.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed in array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.