Commit 595b233e authored by damiankloip's avatar damiankloip Committed by tim.plunkett

Issue #1777466 by damiankloip, tim.plunkett: Fixed View copy method doesn't work.

parent 25416c1e
......@@ -105,6 +105,45 @@ function testValidate() {
// - Multiple displays are validating and the errors are merged together.
}
/**
* Tests the createDuplicate() View method.
*/
public function testCreateDuplicate() {
$view = views_get_view('archive');
$copy = $view->createDuplicate();
$this->assertTrue($copy instanceof View, 'The copied object is a View.');
// Check that the original view and the copy have different uuids.
$this->assertNotIdentical($view->uuid(), $copy->uuid(), 'The copied view has a new uuid.');
// Check the 'name' (id) is using the View objects default value ('') as it
// gets unset.
$this->assertIdentical($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 = array(
'disabled',
'api_version',
'description',
'tag',
'base_table',
'human_name',
'core',
);
foreach ($config_properties as $property) {
$this->assertIdentical($view->{$property}, $copy->{$property}, format_string('@property property is identical.', array('@property' => $property)));
}
// Check the displays are the same.
foreach ($view->display as $id => $display) {
// assertIdentical will not work here.
$this->assertEqual($display, $copy->display[$id], format_string('The @display display has been copied correctly.', array('@display' => $id)));
}
}
/**
* Overrides Drupal\views\Tests\ViewTestBase::getBasicView().
*/
......
......@@ -1907,15 +1907,19 @@ public function endQueryCapture() {
}
/**
* Make a copy of this view that has been sanitized of all database IDs
* and handlers and other stuff.
* Overrides Drupal\entity\Entity::createDuplicate().
*
* I'd call this clone() but it's reserved.
* Makes a copy of this view that has been sanitized of handlers, any runtime
* data, ID, and UUID.
*/
public function copy() {
$code = $this->export();
eval($code);
return $view;
public function createDuplicate() {
$data = config('views.view.' . $this->id())->get();
// Reset the name and UUID.
unset($data['name']);
unset($data['uuid']);
return entity_create('view', $data);
}
/**
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment