function ViewStorageTest::testCreateDuplicate

Same name and namespace in other branches
  1. 9 core/modules/views/tests/src/Kernel/ViewStorageTest.php \Drupal\Tests\views\Kernel\ViewStorageTest::testCreateDuplicate()
  2. 8.9.x core/modules/views/tests/src/Kernel/ViewStorageTest.php \Drupal\Tests\views\Kernel\ViewStorageTest::testCreateDuplicate()
  3. 11.x 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() : void {
  $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), "{$property} property is identical.");
  }
  // 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, "The {$id} display has been copied correctly.");
  }
}

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