You are here

public function ViewStorageTest::testCreateDuplicate in Drupal 8

Same name and namespace in other branches
  1. 9 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 317

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
    ->assertNotIdentical($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
    ->assertIdentical($copy
    ->id(), NULL, '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
      ->assertIdentical($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
      ->assertEqual($display, $copy_display[$id], new FormattableMarkup('The @display display has been copied correctly.', [
      '@display' => $id,
    ]));
  }
}