You are here

public function ViewStorageTest::testCreateDuplicate in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/views/tests/src/Kernel/ViewStorageTest.php \Drupal\Tests\views\Kernel\ViewStorageTest::testCreateDuplicate()

Tests the createDuplicate() View method.

File

core/modules/views/tests/src/Kernel/ViewStorageTest.php, line 305

Class

ViewStorageTest
Tests the CRUD functionality for a view.

Namespace

Drupal\Tests\views\Kernel

Code

public function testCreateDuplicate() {
  $view = Views::getView('test_view_storage');
  $copy = $view->storage
    ->createDuplicate();
  $this
    ->assertInstanceOf(View::class, $copy);

  // Check that the original view and the copy have different UUIDs.
  $this
    ->assertNotSame($view->storage
    ->uuid(), $copy
    ->uuid(), 'The copied view has a new UUID.');

  // Check the 'name' (ID) is using the View objects default value (NULL) as it
  // gets unset.
  $this
    ->assertNull($copy
    ->id(), 'The ID has been reset.');

  // Check the other properties.
  // @todo Create a reusable property on the base test class for these?
  $config_properties = [
    'disabled',
    'description',
    'tag',
    'base_table',
    'label',
  ];
  foreach ($config_properties as $property) {
    $this
      ->assertSame($view->storage
      ->get($property), $copy
      ->get($property), new FormattableMarkup('@property property is identical.', [
      '@property' => $property,
    ]));
  }

  // Check the displays are the same.
  $copy_display = $copy
    ->get('display');
  foreach ($view->storage
    ->get('display') as $id => $display) {

    // assertIdentical will not work here.
    $this
      ->assertEquals($copy_display[$id], $display, new FormattableMarkup('The @display display has been copied correctly.', [
      '@display' => $id,
    ]));
  }
}