From fee13c04876e0cf7d8f88fcc099d2978d840df53 Mon Sep 17 00:00:00 2001
From: damiankloip <damian@damoweb.co.uk>
Date: Mon, 27 Aug 2012 17:57:38 +0200
Subject: [PATCH] Refactored ViewStorageTest into helper functions

---
 lib/Drupal/views/Tests/ViewStorageTest.php | 131 +++++++++++++++------
 1 file changed, 92 insertions(+), 39 deletions(-)

diff --git a/lib/Drupal/views/Tests/ViewStorageTest.php b/lib/Drupal/views/Tests/ViewStorageTest.php
index a4e6cdced998..27ccb6fe5f58 100644
--- a/lib/Drupal/views/Tests/ViewStorageTest.php
+++ b/lib/Drupal/views/Tests/ViewStorageTest.php
@@ -53,20 +53,30 @@ public static function getInfo() {
    * Tests CRUD operations.
    */
   function testConfigurableCRUD() {
-
     // Get the Configurable information and controller.
-    $info = entity_get_info('view');
-    $controller = entity_get_controller('view');
+    $this->info = entity_get_info('view');
+    $this->controller = entity_get_controller('view');
 
     // Confirm that an info array has been returned.
-    $this->assertTrue(!empty($info) && is_array($info), 'The View  info array is loaded.');
+    $this->assertTrue(!empty($this->info) && is_array($this->info), 'The View  info array is loaded.');
 
     // Confirm we have the correct controller class.
-    $this->assertTrue($controller instanceof ViewStorageController, 'The correct controller is loaded.');
+    $this->assertTrue($this->controller instanceof ViewStorageController, 'The correct controller is loaded.');
+
+    // CRUD tests.
+    $this->loadTests();
+    $this->createTests();
+    $this->saveTests();
+    $this->deleteTests();
+    $this->displayTests();
+    $this->statusTests();
+  }
 
-    // Load a single Configurable object from the controller.
-    $load = $controller->load(array('archive'));
-    $view = reset($load);
+  /**
+   * Tests loading configurables.
+   */
+  protected function loadTests() {
+    $view = $this->loadView('archive');
 
     // Confirm that an actual view object is loaded and that it returns all of
     // expected properties.
@@ -89,7 +99,7 @@ function testConfigurableCRUD() {
     }
 
     // Fetch data for all Configurable objects and default view configurations.
-    $all_configurables = $controller->load();
+    $all_configurables = $this->controller->load();
     $all_config = config_get_storage_names_with_prefix('views.view');
 
     // Remove the 'views.view.' prefix from config names for comparision with
@@ -105,23 +115,29 @@ function testConfigurableCRUD() {
 
     // Check that all of these machine names match.
     $this->assertIdentical(array_keys($all_configurables), array_map($prefix_map, $all_config), 'All loaded elements match.');
+  }
 
+  /**
+   * Tests creating configurables.
+   */
+  protected function createTests() {
     // Create a new View instance with empty values.
-    $created = $controller->create(array());
+    $created = $this->controller->create(array());
 
     $this->assertTrue($created instanceof View, 'Created object is a View.');
     // Check that the View contains all of the properties.
     foreach ($this->config_properties as $property) {
-      $this->assertTrue(isset($view->{$property}), format_string('Property: @property created on View.', array('@property' => $property)));
+      $this->assertTrue(property_exists($created, $property), format_string('Property: @property created on View.', array('@property' => $property)));
     }
 
     // Create a new View instance with config values.
-    $values = config('views.view.archive')->get();
-    $created = $controller->create($values);
+    $values = config('views.view.glossary')->get();
+    $created = $this->controller->create($values);
 
     $this->assertTrue($created instanceof View, 'Created object is a View.');
     // Check that the View contains all of the properties.
     $properties = $this->config_properties;
+    // Remove display from list.
     array_pop($properties);
 
     // Test all properties except displays.
@@ -135,38 +151,39 @@ function testConfigurableCRUD() {
       $this->assertTrue($display instanceof ViewDisplay, format_string('Display @display is an instance of ViewDisplay.', array('@display' => $key)));
     }
 
+  }
+
+  /**
+   * Tests saving configurables.
+   */
+  protected function saveTests() {
+    $view = $this->loadView('archive');
+
     // Save the newly created view, but modify the name.
-    $created->set('name', 'archive_copy');
-    $created->set('tag', 'changed');
-    $created->save();
+    $view->set('name', 'archive_copy');
+    $view->set('tag', 'changed');
+    $view->save();
 
     // Load the newly saved config.
     $config = config('views.view.archive_copy');
     $this->assertFalse($config->isNew(), 'New config has been loaded.');
 
-    $this->assertEqual($created->tag, $config->get('tag'), 'A changed value has been saved.');
+    $this->assertEqual($view->tag, $config->get('tag'), 'A changed value has been saved.');
 
     // Change a value and save.
     $view->tag = 'changed';
     $view->save();
 
-    // Check value have been written to config.
-    $config = config('views.view.archive')->get();
+    // Check values have been written to config.
+    $config = config('views.view.archive_copy')->get();
     $this->assertEqual($view->tag, $config['tag'], 'View property saved to config.');
 
-    // Delete the config.
-    $created->delete();
-    $config = config('views.view.archive_copy');
-
-    $this->assertTrue($config->isNew(), 'Deleted config is now new.');
-
     // Check whether load, save and load produce the same kind of view.
     $values = config('views.view.archive')->get();
-    $created = $controller->create($values);
+    $created = $this->controller->create($values);
 
     $created->save();
-    $loaded_entities = $controller->load(array($created->id()));
-    $created_loaded = reset($loaded_entities);
+    $created_loaded = $this->loadView($created->id());
     $values_loaded = config('views.view.archive')->get();
 
     $this->assertTrue(isset($created_loaded->display['default']->display_options), 'Make sure that the display options exist.');
@@ -174,28 +191,52 @@ function testConfigurableCRUD() {
 
     $this->assertEqual($values, $values_loaded, 'The loaded config is the same as the original loaded one.');
 
+  }
+
+  /**
+   * Tests deleting configurables.
+   */
+  protected function deleteTests() {
+    $view = $this->loadView('tracker');
+
+    // Delete the config.
+    $view->delete();
+    $config = config('views.view.tracker');
+
+    $this->assertTrue($config->isNew(), 'Deleted config is now new.');
+  }
+
+  /**
+   * Tests adding/saving/loading displays on configurables.
+   */
+  protected function displayTests() {
     // Check whether a display can be added and saved to a View.
-    $created = $controller->create($values);
-    $created->new_display('page', 'Test', 'test');
+    $view = $this->loadView('frontpage');
+
+    $view->new_display('page', 'Test', 'test');
 
-    $new_display = $created->display['test'];
+    $new_display = $view->display['test'];
     $this->assertTrue($new_display instanceof ViewDisplay, 'New page display "test" created.');
 
     // Take sure the right display_plugin is created/instantiated.
     $this->assertEqual($new_display->display_plugin, 'page', 'New page display "test" uses the right display plugin.');
-    $created->init_display();
+    $view->init_display();
     $this->assertTrue($new_display->handler instanceof Page, 'New page display "test" uses the right display plugin.');
 
 
-    $created->set('name', 'archive_new_display');
-    $created->save();
-    $values = config('views.view.archive_new_display')->get();
+    $view->set('name', 'frontpage_new');
+    $view->save();
+    $values = config('views.view.frontpage_new')->get();
 
     $this->assertTrue(isset($values['display']['test']) && is_array($values['display']['test']), 'New display was saved.');
+  }
 
+  /**
+   * Tests statuses of configurables.
+   */
+  protected function statusTests() {
     // Test a View can be enabled and disabled again (with a new view).
-    $load = $controller->load(array('frontpage'));
-    $view = reset($load);
+    $view = $this->loadView('backlinks');
 
     // The view should already be disabled.
     $view->enable();
@@ -203,7 +244,7 @@ function testConfigurableCRUD() {
 
     // Check the saved values.
     $view->save();
-    $config = config('views.view.frontpage')->get();
+    $config = config('views.view.backlinks')->get();
     $this->assertFalse($config['disabled'], 'The changed disabled property was saved.');
 
     // Disable the view.
@@ -212,8 +253,20 @@ function testConfigurableCRUD() {
 
     // Check the saved values.
     $view->save();
-    $config = config('views.view.frontpage')->get();
+    $config = config('views.view.backlinks')->get();
     $this->assertTrue($config['disabled'], 'The changed disabled property was saved.');
   }
 
+  /**
+   * Load a single Configurable object from the controller.
+   *
+   * @param string $view_name
+   *
+   * @return object Drupal\views\View.
+   */
+  protected function loadView($view_name) {
+    $load = $this->controller->load(array($view_name));
+    return reset($load);
+  }
+
 }
-- 
GitLab