Commit e127cd6d authored by dawehner's avatar dawehner Committed by tim.plunkett

Issue #1792766 by dawehner, tim.plunkett: Split ViewTest between...

Issue #1792766 by dawehner, tim.plunkett: Split ViewTest between ViewStorageTest and ViewExecutableTest.
parent b412ae1a
<?php
/**
* Definition of Drupal\views\Tests\ViewExecutable.
* @file
* Definition of Drupal\views\Tests\ViewExecutableTest.
*/
namespace Drupal\views\Tests;
use Drupal\views\ViewExecutable;
use Drupal\views\Plugin\views\display\DefaultDisplay;
use Drupal\views\Plugin\views\display\Page;
/**
* Tests the ViewExecutable class.
*
* @see Drupal\views\ViewExecutableExecutable
* @see Drupal\views\ViewExecutable
*/
class ViewExecutableTest extends ViewTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('comment');
/**
* Properties that should be stored in the configuration.
*
......@@ -47,6 +59,13 @@ public static function getInfo() {
);
}
/**
* Overrides Drupal\views\Tests\ViewTestBase::getBasicView().
*/
protected function getBasicView() {
return $this->createViewFromConfig('test_destroy');
}
/**
* Tests the generation of the executable object.
*/
......@@ -75,9 +94,9 @@ public function testDisplays() {
$count = count($view->displayHandlers);
$this->assertEqual($count, 3, format_string('Make sure all display handlers got instantiated (@count of @count_expected)', array('@count' => $count, '@count_expected' => 3)));
// Tests the classes of the instances.
$this->assertTrue($view->displayHandlers['default'] instanceof \Drupal\views\Plugin\views\display\DefaultDisplay);
$this->assertTrue($view->displayHandlers['page'] instanceof \Drupal\views\Plugin\views\display\Page);
$this->assertTrue($view->displayHandlers['page_2'] instanceof \Drupal\views\Plugin\views\display\Page);
$this->assertTrue($view->displayHandlers['default'] instanceof DefaultDisplay);
$this->assertTrue($view->displayHandlers['page'] instanceof Page);
$this->assertTrue($view->displayHandlers['page_2'] instanceof Page);
// After initializing the default display is the current used display.
$this->assertEqual($view->current_display, 'default');
......@@ -103,4 +122,81 @@ public function testDisplays() {
}
/**
* Tests the deconstructor to be sure that every kind of heavy objects are removed.
*/
function testDestroy() {
$view = $this->getView();
$view->preview();
$view->destroy();
$this->assertViewDestroy($view);
// Manipulate the display variable to test a previous bug.
$view = $this->getView();
$view->preview();
$view->destroy();
$this->assertViewDestroy($view);
}
function assertViewDestroy($view) {
$this->assertFalse(isset($view->displayHandlers['default']), 'Make sure all displays are destroyed.');
$this->assertFalse(isset($view->displayHandlers['attachment_1']), 'Make sure all displays are destroyed.');
$this->assertFalse(isset($view->filter), 'Make sure all filter handlers are destroyed');
$this->assertFalse(isset($view->field), 'Make sure all field handlers are destroyed');
$this->assertFalse(isset($view->argument), 'Make sure all argument handlers are destroyed');
$this->assertFalse(isset($view->relationship), 'Make sure all relationship handlers are destroyed');
$this->assertFalse(isset($view->sort), 'Make sure all sort handlers are destroyed');
$this->assertFalse(isset($view->area), 'Make sure all area handlers are destroyed');
$keys = array('current_display', 'display_handler', 'field', 'argument', 'filter', 'sort', 'relationship', 'header', 'footer', 'empty', 'query', 'result', 'inited', 'style_plugin', 'plugin_name', 'exposed_data', 'exposed_input', 'many_to_one_tables');
foreach ($keys as $key) {
$this->assertFalse(isset($view->{$key}), $key);
}
$this->assertEqual($view->built, FALSE);
$this->assertEqual($view->executed, FALSE);
$this->assertEqual($view->build_info, array());
$this->assertEqual($view->attachment_before, '');
$this->assertEqual($view->attachment_after, '');
}
/**
* Tests ViewExecutable::viewsHandlerTypes().
*/
public function testViewsHandlerTypes() {
$types = ViewExecutable::viewsHandlerTypes();
foreach (array('field', 'filter', 'argument', 'sort', 'header', 'footer', 'empty') as $type) {
$this->assertTrue(isset($types[$type]));
// @todo The key on the display should be footers, headers and empties
// or something similar instead of the singular, but so long check for
// this special case.
if (isset($types[$type]['type']) && $types[$type]['type'] == 'area') {
$this->assertEqual($types[$type]['plural'], $type);
}
else {
$this->assertEqual($types[$type]['plural'], $type . 's');
}
}
}
function testValidate() {
// Test a view with multiple displays.
// Validating a view shouldn't change the active display.
// @todo Create an extra validation view.
$this->view->setDisplay('page_1');
$this->view->validate();
$this->assertEqual('page_1', $this->view->current_display, "The display should be constant while validating");
// @todo Write real tests for the validation.
// In general the following things could be tested:
// - Deleted displays shouldn't be validated
// - Multiple displays are validating and the errors are merged together.
}
}
......@@ -7,17 +7,19 @@
namespace Drupal\views\Tests;
use Drupal\simpletest\WebTestBase;
use Drupal\views\ViewExecutable;
use Drupal\views\ViewStorageController;
use Drupal\views\ViewStorage;
use Drupal\views\ViewDisplay;
use Drupal\views\Plugin\views\display\Page;
use Drupal\views\Plugin\views\display\DefaultDisplay;
use Drupal\views\Plugin\views\display\Feed;
/**
* Tests the functionality of the ViewStorageController.
* Tests the functionality of ViewStorage and ViewStorageController.
*
* @see Drupal\views\ViewStorage
* @see Drupal\views\ViewStorageController
*/
class ViewStorageTest extends WebTestBase {
class ViewStorageTest extends ViewTestBase {
/**
* Properties that should be stored in the configuration.
......@@ -56,7 +58,7 @@ class ViewStorageTest extends WebTestBase {
*
* @var array
*/
public static $modules = array('views', 'node', 'search', 'comment', 'taxonomy');
public static $modules = array('node', 'search', 'comment', 'taxonomy');
public static function getInfo() {
return array(
......@@ -141,7 +143,7 @@ protected function loadTests() {
// Check that all of these machine names match.
$this->assertIdentical(array_keys($all_configuration_entities), array_map($prefix_map, $all_config), 'All loaded elements match.');
// Make sure that loaded default views get a uuid.
// Make sure that loaded default views get a UUID.
$view = views_get_view('frontpage');
$this->assertTrue($view->storage->uuid());
}
......@@ -175,11 +177,11 @@ protected function createTests() {
$this->assertIdentical($values[$property], $created->{$property}, format_string('Property value: @property matches configuration value.', array('@property' => $property)));
}
// Check the uuid of the loaded View.
// Check the UUID of the loaded View.
$created->set('name', 'glossary_new');
$created->save();
$created_loaded = $this->loadView('glossary_new');
$this->assertIdentical($created->uuid(), $created_loaded->uuid(), 'The created uuid has been saved correctly.');
$this->assertIdentical($created->uuid(), $created_loaded->uuid(), 'The created UUID has been saved correctly.');
}
/**
......@@ -342,12 +344,12 @@ protected function displayMethodTests() {
$random_title = $this->randomName();
$id = $view->addDisplay('page', $random_title);
$this->assertEqual($id, 'page_1', format_string('Make sure the first display (%id_new) has the expected id (%id)', array('%id_new' => $id, '%id' => 'page_1')));
$this->assertEqual($id, 'page_1', format_string('Make sure the first display (%id_new) has the expected ID (%id)', array('%id_new' => $id, '%id' => 'page_1')));
$this->assertEqual($view->display[$id]['display_title'], $random_title);
$random_title = $this->randomName();
$id = $view->addDisplay('page', $random_title);
$this->assertEqual($id, 'page_2', format_string('Make sure the second display (%id_new) has the expected id (%id)', array('%id_new' => $id, '%id' => 'page_2')));
$this->assertEqual($id, 'page_2', format_string('Make sure the second display (%id_new) has the expected ID (%id)', array('%id_new' => $id, '%id' => 'page_2')));
$this->assertEqual($view->display[$id]['display_title'], $random_title);
$id = $view->addDisplay('page');
......@@ -356,29 +358,29 @@ protected function displayMethodTests() {
// Tests Drupal\views\ViewStorage::generateDisplayId().
// @todo Sadly this method is not public so it cannot be tested.
// $view = $this->controller->create(array());
// $this->assertEqual($view->generateDisplayId('default'), 'default', 'The plugin id for default is always default.');
// $this->assertEqual($view->generateDisplayId('feed'), 'feed_1', 'The generated id for the first instance of a plugin type should have an suffix of _1.');
// $this->assertEqual($view->generateDisplayId('default'), 'default', 'The plugin ID for default is always default.');
// $this->assertEqual($view->generateDisplayId('feed'), 'feed_1', 'The generated ID for the first instance of a plugin type should have an suffix of _1.');
// $view->addDisplay('feed', 'feed title');
// $this->assertEqual($view->generateDisplayId('feed'), 'feed_2', 'The generated id for the first instance of a plugin type should have an suffix of _2.');
// $this->assertEqual($view->generateDisplayId('feed'), 'feed_2', 'The generated ID for the first instance of a plugin type should have an suffix of _2.');
// Tests Drupal\views\ViewStorage::newDisplay().
$view = $this->controller->create(array());
$view->newDisplay('default');
$display = $view->newDisplay('page');
$this->assertTrue($display instanceof \Drupal\views\Plugin\views\display\Page);
$this->assertTrue($view->getExecutable()->displayHandlers['page_1'] instanceof \Drupal\views\Plugin\views\display\Page);
$this->assertTrue($view->getExecutable()->displayHandlers['page_1']->default_display instanceof \Drupal\views\Plugin\views\display\DefaultDisplay);
$this->assertTrue($display instanceof Page);
$this->assertTrue($view->getExecutable()->displayHandlers['page_1'] instanceof Page);
$this->assertTrue($view->getExecutable()->displayHandlers['page_1']->default_display instanceof DefaultDisplay);
$display = $view->newDisplay('page');
$this->assertTrue($display instanceof \Drupal\views\Plugin\views\display\Page);
$this->assertTrue($view->getExecutable()->displayHandlers['page_2'] instanceof \Drupal\views\Plugin\views\display\Page);
$this->assertTrue($view->getExecutable()->displayHandlers['page_2']->default_display instanceof \Drupal\views\Plugin\views\display\DefaultDisplay);
$this->assertTrue($display instanceof Page);
$this->assertTrue($view->getExecutable()->displayHandlers['page_2'] instanceof Page);
$this->assertTrue($view->getExecutable()->displayHandlers['page_2']->default_display instanceof DefaultDisplay);
$display = $view->newDisplay('feed');
$this->assertTrue($display instanceof \Drupal\views\Plugin\views\display\Feed);
$this->assertTrue($view->getExecutable()->displayHandlers['feed_1'] instanceof \Drupal\views\Plugin\views\display\Feed);
$this->assertTrue($view->getExecutable()->displayHandlers['feed_1']->default_display instanceof \Drupal\views\Plugin\views\display\DefaultDisplay);
$this->assertTrue($display instanceof Feed);
$this->assertTrue($view->getExecutable()->displayHandlers['feed_1'] instanceof Feed);
$this->assertTrue($view->getExecutable()->displayHandlers['feed_1']->default_display instanceof DefaultDisplay);
// Tests item related methods().
$view = $this->controller->create(array('base_table' => 'views_test_data'));
......@@ -429,4 +431,43 @@ protected function displayMethodTests() {
$this->assertEqual($view->getItems('field', $display_id), $expected_items);
}
/**
* Tests the createDuplicate() View method.
*/
public function testCreateDuplicate() {
$view = views_get_view('archive');
$copy = $view->createDuplicate();
$this->assertTrue($copy instanceof ViewStorage, 'The copied object is a View.');
// 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 ('') 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->storage->{$property}, $copy->{$property}, format_string('@property property is identical.', array('@property' => $property)));
}
// Check the displays are the same.
foreach ($view->storage->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)));
}
}
}
<?php
/**
* @file
* Definition of Drupal\views\Tests\ViewTest.
*/
namespace Drupal\views\Tests;
use Drupal\views\ViewExecutable;
use Drupal\views\ViewStorage;
/**
* Views class tests.
*/
class ViewTest extends ViewTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('comment');
public static function getInfo() {
return array(
'name' => 'Views object',
'description' => 'Tests some functionality of the View class.',
'group' => 'Views',
);
}
/**
* Tests the deconstructor to be sure that every kind of heavy objects are removed.
*/
function testDestroy() {
$view = $this->getView();
$view->preview();
$view->destroy();
$this->assertViewDestroy($view);
// Manipulate the display variable to test a previous bug.
$view = $this->getView();
$view->preview();
$view->destroy();
$this->assertViewDestroy($view);
}
function assertViewDestroy($view) {
$this->assertFalse(isset($view->displayHandlers['default']), 'Make sure all displays are destroyed.');
$this->assertFalse(isset($view->displayHandlers['attachment_1']), 'Make sure all displays are destroyed.');
$this->assertFalse(isset($view->filter), 'Make sure all handlers are destroyed');
$this->assertFalse(isset($view->field), 'Make sure all handlers are destroyed');
$this->assertFalse(isset($view->argument), 'Make sure all handlers are destroyed');
$this->assertFalse(isset($view->relationship), 'Make sure all handlers are destroyed');
$this->assertFalse(isset($view->sort), 'Make sure all handlers are destroyed');
$this->assertFalse(isset($view->area), 'Make sure all handlers are destroyed');
$keys = array('current_display', 'display_handler', 'field', 'argument', 'filter', 'sort', 'relationship', 'header', 'footer', 'empty', 'query', 'result', 'inited', 'style_plugin', 'plugin_name', 'exposed_data', 'exposed_input', 'many_to_one_tables');
foreach ($keys as $key) {
$this->assertFalse(isset($view->{$key}), $key);
}
$this->assertEqual($view->built, FALSE);
$this->assertEqual($view->executed, FALSE);
$this->assertEqual($view->build_info, array());
$this->assertEqual($view->attachment_before, '');
$this->assertEqual($view->attachment_after, '');
}
/**
* Tests ViewExecutable::viewsHandlerTypes().
*/
public function testViewshandlerTypes() {
$types = ViewExecutable::viewsHandlerTypes();
foreach (array('field', 'filter', 'argument', 'sort', 'header', 'footer', 'empty') as $type) {
$this->assertTrue(isset($types[$type]));
// @todo The key on the display should be footers, headers and empties
// or something similar instead of the singular, but so long check for
// this special case.
if (isset($types[$type]['type']) && $types[$type]['type'] == 'area') {
$this->assertEqual($types[$type]['plural'], $type);
}
else {
$this->assertEqual($types[$type]['plural'], $type . 's');
}
}
}
function testValidate() {
// Test a view with multiple displays.
// Validating a view shouldn't change the active display.
// @todo: Create an extra validation view.
$this->view->setDisplay('page_1');
$this->view->validate();
$this->assertEqual('page_1', $this->view->current_display, "The display should be constant while validating");
// @todo: Write real tests for the validation.
// In general the following things could be tested:
// - Deleted displays shouldn't be validated
// - 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 ViewStorage, 'The copied object is a View.');
// 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 ('') 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->storage->{$property}, $copy->{$property}, format_string('@property property is identical.', array('@property' => $property)));
}
// Check the displays are the same.
foreach ($view->storage->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().
*/
protected function getBasicView() {
return $this->createViewFromConfig('test_destroy');
}
}
......@@ -33,5 +33,5 @@ display:
id: default
position: '0'
human_name: ''
name: test_view_argument_validate_numeric
name: test_view_argument_validate_php
tag: ''
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