From 02de114401a58e211366c053dbeaf7c5d437a06f Mon Sep 17 00:00:00 2001 From: qadan Date: Thu, 29 May 2014 16:45:16 +0000 Subject: [PATCH] better, simpler drupal filter handling --- islandora.module | 17 ++- tests/includes/islandora_unit_test_case.inc | 22 +--- tests/includes/islandora_web_test_case.inc | 14 +-- tests/includes/utilities.inc | 110 +++++--------------- 4 files changed, 37 insertions(+), 126 deletions(-) diff --git a/islandora.module b/islandora.module index ff077212..3f3d777b 100644 --- a/islandora.module +++ b/islandora.module @@ -1813,18 +1813,13 @@ function islandora_form_simpletest_test_form_alter(array &$form) { * Submit handler for islandora_form_simpletest_test_form_alter(). */ function islandora_repair_drupal_filter() { - - // Grab the config. - $path = drupal_get_path('module', 'islandora'); - if (file_exists("$path/tests/test_config.ini")) { - $configuration = parse_ini_file("$path/tests/test_config.ini"); - } - elseif (file_exists("$path/tests/default.test_config.ini")) { - $configuration = parse_ini_file("$path/tests/default.test_config.ini"); + module_load_include('inc', 'islandora', 'tests/includes/utilities'); + try { + $configuration = islandora_get_test_configuration(); } - else { - drupal_set_message(t('Required default.test_config.ini/test_config.ini file not found'), 'error'); - return FALSE; + catch (Exception $e) { + drupal_set_message("Unable to get the test configuration: $e", 'error'); + return; } // Xpath to the filter 'sql' elements. diff --git a/tests/includes/islandora_unit_test_case.inc b/tests/includes/islandora_unit_test_case.inc index ce9216d8..4cdf30bd 100644 --- a/tests/includes/islandora_unit_test_case.inc +++ b/tests/includes/islandora_unit_test_case.inc @@ -13,13 +13,6 @@ class IslandoraUnitTestCase extends DrupalUnitTestCase { */ protected $deleteObjectsOnTeardown = TRUE; - /** - * An instance of IslandoraDrupalFilterManipulator carried between tests. - * - * @var IslandoraDrupalFilterManipulator - */ - protected $filterManipulator; - /** * Defers to IslandoraTestUtilities for missing methods. * @@ -64,32 +57,19 @@ class IslandoraUnitTestCase extends DrupalUnitTestCase { // It's possible test are running before class autoloading. module_load_include('inc', 'islandora', 'includes/tuque'); module_load_include('inc', 'islandora', 'includes/tuque_wrapper'); - module_load_include('inc', 'islandora', 'tests/includes/utilities'); $this->configuration = islandora_get_test_configuration(); - if ($this->configuration['use_drupal_filter']) { - $this->filterManipulator = new IslandoraDrupalFilterManipulator($this->configuration); - $this->filterManipulator->setUpDrupalFilter(); - } - $this->connection = new RepositoryConnection($this->configuration['fedora_url'], $this->configuration['admin_user'], $this->configuration['admin_pass']); $api = new FedoraApi($this->connection); $this->repository = new FedoraRepository($api, new SimpleCache()); } /** - * Restores the original Drupal filter, frees any allocated resources. - * - * To safeguard against leaving test objects in the repository, tearDown() - * calls deleteUserCreatedObjects() every time by default. This feature can be - * toggled by setting $this->deleteObjectsOnTeardown to TRUE or FALSE. + * Frees any allocated resources. * * @see DrupalWebTestCase::tearDown() */ public function tearDown() { - if ($this->configuration['use_drupal_filter']) { - $this->filterManipulator->restoreDrupalFilter(); - } unset($this->configuration); parent::tearDown(); } diff --git a/tests/includes/islandora_web_test_case.inc b/tests/includes/islandora_web_test_case.inc index 383e2b02..211f0df0 100644 --- a/tests/includes/islandora_web_test_case.inc +++ b/tests/includes/islandora_web_test_case.inc @@ -21,13 +21,6 @@ class IslandoraWebTestCase extends DrupalWebTestCase { */ protected $deleteObjectsOnTeardown = TRUE; - /** - * An instance of IslandoraDrupalFilterManipulator carried between tests. - * - * @var IslandoraDrupalFilterManipulator - */ - protected $filterManipulator; - /** * Defers to IslandoraTestUtilities for missing methods. * @@ -65,7 +58,7 @@ class IslandoraWebTestCase extends DrupalWebTestCase { } /** - * Sets up the Drupal filter to access this test Drupal instances database. + * Sets up the web test case. * * @see DrupalWebTestCase::setUp() */ @@ -83,8 +76,7 @@ class IslandoraWebTestCase extends DrupalWebTestCase { $this->configuration = islandora_get_test_configuration(); if ($this->configuration['use_drupal_filter']) { - $this->filterManipulator = new IslandoraDrupalFilterManipulator($this->configuration); - $this->filterManipulator->setUpDrupalFilter(); + $this->setUpDrupalFilter(); } $this->admin = $this->createAdminUser(); } @@ -147,7 +139,7 @@ class IslandoraWebTestCase extends DrupalWebTestCase { } } if ($this->configuration['use_drupal_filter']) { - $this->filterManipulator->restoreDrupalFilter(); + islandora_repair_drupal_filter(); } unset($this->admin); unset($this->configuration); diff --git a/tests/includes/utilities.inc b/tests/includes/utilities.inc index e0682397..28179c05 100644 --- a/tests/includes/utilities.inc +++ b/tests/includes/utilities.inc @@ -35,89 +35,6 @@ function islandora_get_test_configuration() { throw new Exception('Required default.test_config.ini/test_config.ini file not found'); } -/** - * Methods specifically for working with the Drupal filter for tests. - */ -class IslandoraDrupalFilterManipulator { - - /** - * Parsed test configuration. - * - * @var array - */ - public $configuration; - - /** - * The original contents of the drupal filter. - * - * @var string - */ - protected $originalDrupalFilterContent; - - /** - * Constructs an IslandoraTestUtilities object. - * - * @param array $configuration - * The parsed test configuration. - */ - public function __construct($configuration) { - $this->configuration = $configuration; - $this->backUpDrupalFilter(); - } - - /** - * Stores the content of the Drupal Filter for later restoration. - */ - public function restoreDrupalFilter() { - $file = $this->configuration['drupal_filter_file']; - if (isset($this->originalDrupalFilterContent)) { - file_put_contents($file, $this->originalDrupalFilterContent); - } - elseif (file_exists($file)) { - // Remove if there was never an original. - drupal_unlink($file); - } - } - - /** - * Sets up a drupal filter that can read for the tests users table. - */ - public function setUpDrupalFilter() { - $connection_info = Database::getConnectionInfo('default'); - $drupal_filter_dom = new DomDocument(); - $drupal_filter_dom->loadXML($this->originalDrupalFilterContent); - $server = $connection_info['default']['host']; - $dbname = $connection_info['default']['database']; - $user = $connection_info['default']['username']; - $password = $connection_info['default']['password']; - $port = $connection_info['default']['port'] ? $connection_info['default']['port'] : '3306'; - $prefix = $connection_info['default']['prefix']['default']; - $filter_drupal_connection_node = $drupal_filter_dom->getElementsByTagName('FilterDrupal_Connection')->item(0); - $first_connection_node = $drupal_filter_dom->getElementsByTagName('connection')->item(0); - $connection_node = $filter_drupal_connection_node->insertBefore($drupal_filter_dom->createElement('connection'), $first_connection_node); - $connection_node->setAttributeNode(new DOMAttr('server', $server)); - $connection_node->setAttributeNode(new DOMAttr('dbname', $dbname)); - $connection_node->setAttributeNode(new DOMAttr('user', $user)); - $connection_node->setAttributeNode(new DOMAttr('password', $password)); - $connection_node->setAttributeNode(new DOMAttr('port', $port)); - $sql_node = $connection_node->appendChild(new DOMElement('sql')); - $sql_node->appendChild($drupal_filter_dom->createTextNode("SELECT DISTINCT u.uid AS userid, u.name AS Name, u.pass AS Pass, r.name AS Role FROM ({$prefix}users u LEFT JOIN {$prefix}users_roles ON u.uid={$prefix}users_roles.uid) LEFT JOIN {$prefix}role r ON r.rid={$prefix}users_roles.rid WHERE u.name=? AND u.pass=?;")); - file_put_contents($this->configuration['drupal_filter_file'], $drupal_filter_dom->saveXML()); - } - - /** - * Stores the content of the Drupal Filter for later restoration. - */ - protected function backUpDrupalFilter() { - if (file_exists($this->configuration['drupal_filter_file'])) { - $this->originalDrupalFilterContent = file_get_contents($this->configuration['drupal_filter_file']); - } - else { - throw new Exception('Failed to find the required Drupal Filter configuration file.'); - } - } -} - /** * A result from a utility method; $type defines TRUE/FALSE as pass/fail. */ @@ -266,6 +183,33 @@ class IslandoraTestUtilities extends IslandoraTestUtilityClass { $this->repository = new FedoraRepository($api, new SimpleCache()); } + /** + * Sets up a drupal filter that can read for the tests users table. + */ + public function setUpDrupalFilter() { + $original_drupal_filter_content = file_get_contents($this->configuration['drupal_filter_file']); + $connection_info = Database::getConnectionInfo('default'); + $drupal_filter_dom = new DomDocument(); + $drupal_filter_dom->loadXML($original_drupal_filter_content); + $server = $connection_info['default']['host']; + $dbname = $connection_info['default']['database']; + $user = $connection_info['default']['username']; + $password = $connection_info['default']['password']; + $port = $connection_info['default']['port'] ? $connection_info['default']['port'] : '3306'; + $prefix = $connection_info['default']['prefix']['default']; + $filter_drupal_connection_node = $drupal_filter_dom->getElementsByTagName('FilterDrupal_Connection')->item(0); + $first_connection_node = $drupal_filter_dom->getElementsByTagName('connection')->item(0); + $connection_node = $filter_drupal_connection_node->insertBefore($drupal_filter_dom->createElement('connection'), $first_connection_node); + $connection_node->setAttributeNode(new DOMAttr('server', $server)); + $connection_node->setAttributeNode(new DOMAttr('dbname', $dbname)); + $connection_node->setAttributeNode(new DOMAttr('user', $user)); + $connection_node->setAttributeNode(new DOMAttr('password', $password)); + $connection_node->setAttributeNode(new DOMAttr('port', $port)); + $sql_node = $connection_node->appendChild(new DOMElement('sql')); + $sql_node->appendChild($drupal_filter_dom->createTextNode("SELECT DISTINCT u.uid AS userid, u.name AS Name, u.pass AS Pass, r.name AS Role FROM ({$prefix}users u LEFT JOIN {$prefix}users_roles ON u.uid={$prefix}users_roles.uid) LEFT JOIN {$prefix}role r ON r.rid={$prefix}users_roles.rid WHERE u.name=? AND u.pass=?;")); + file_put_contents($this->configuration['drupal_filter_file'], $drupal_filter_dom->saveXML()); + } + /** * Returns an array of IslandoraTestUtilityResults. *