function StorageCopyTraitTest::testReplaceStorageContents

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Core/Config/StorageCopyTraitTest.php \Drupal\Tests\Core\Config\StorageCopyTraitTest::testReplaceStorageContents()
  2. 8.9.x core/tests/Drupal/Tests/Core/Config/StorageCopyTraitTest.php \Drupal\Tests\Core\Config\StorageCopyTraitTest::testReplaceStorageContents()
  3. 10 core/tests/Drupal/Tests/Core/Config/StorageCopyTraitTest.php \Drupal\Tests\Core\Config\StorageCopyTraitTest::testReplaceStorageContents()

@covers ::replaceStorageContents

@dataProvider providerTestReplaceStorageContents

File

core/tests/Drupal/Tests/Core/Config/StorageCopyTraitTest.php, line 29

Class

StorageCopyTraitTest
@coversDefaultClass <a href="/api/drupal/core%21lib%21Drupal%21Core%21Config%21StorageCopyTrait.php/trait/StorageCopyTrait/11.x" title="Utility trait to copy configuration from one storage to another." class="local">\Drupal\Core\Config\StorageCopyTrait</a> @group Config

Namespace

Drupal\Tests\Core\Config

Code

public function testReplaceStorageContents($source_collections, $target_collections) : void {
    $source = new MemoryStorage();
    $target = new MemoryStorage();
    // Empty the storage should be the same.
    $this->assertEquals(self::toArray($source), self::toArray($target));
    // When the source is populated, they are not the same any more.
    $this->generateRandomData($source, $source_collections);
    $this->assertNotEquals(self::toArray($source), self::toArray($target));
    // When the target is filled with random data they are also not the same.
    $this->generateRandomData($target, $target_collections);
    $this->assertNotEquals(self::toArray($source), self::toArray($target));
    // Set the active collection to a random one on both source and 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();
    // After copying they are the same, this asserts that items not present
    // in the source get removed from the target.
    self::replaceStorageContents($source, $target);
    $this->assertEquals($source_data, self::toArray($target));
    // Assert that the copy method did indeed not change the source.
    $this->assertEquals($source_data, self::toArray($source));
    // Assert that the active collection is the same as the original source.
    $this->assertEquals($source_name, $source->getCollectionName());
    $this->assertEquals($source_name, $target->getCollectionName());
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.