Browse Source

better, simpler drupal filter handling

pull/507/head
qadan 11 years ago
parent
commit
02de114401
  1. 17
      islandora.module
  2. 22
      tests/includes/islandora_unit_test_case.inc
  3. 14
      tests/includes/islandora_web_test_case.inc
  4. 110
      tests/includes/utilities.inc

17
islandora.module

@ -1813,18 +1813,13 @@ function islandora_form_simpletest_test_form_alter(array &$form) {
* Submit handler for islandora_form_simpletest_test_form_alter(). * Submit handler for islandora_form_simpletest_test_form_alter().
*/ */
function islandora_repair_drupal_filter() { function islandora_repair_drupal_filter() {
module_load_include('inc', 'islandora', 'tests/includes/utilities');
// Grab the config. try {
$path = drupal_get_path('module', 'islandora'); $configuration = islandora_get_test_configuration();
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");
} }
else { catch (Exception $e) {
drupal_set_message(t('Required default.test_config.ini/test_config.ini file not found'), 'error'); drupal_set_message("Unable to get the test configuration: $e", 'error');
return FALSE; return;
} }
// Xpath to the filter 'sql' elements. // Xpath to the filter 'sql' elements.

22
tests/includes/islandora_unit_test_case.inc

@ -13,13 +13,6 @@ class IslandoraUnitTestCase extends DrupalUnitTestCase {
*/ */
protected $deleteObjectsOnTeardown = TRUE; protected $deleteObjectsOnTeardown = TRUE;
/**
* An instance of IslandoraDrupalFilterManipulator carried between tests.
*
* @var IslandoraDrupalFilterManipulator
*/
protected $filterManipulator;
/** /**
* Defers to IslandoraTestUtilities for missing methods. * Defers to IslandoraTestUtilities for missing methods.
* *
@ -64,32 +57,19 @@ class IslandoraUnitTestCase extends DrupalUnitTestCase {
// It's possible test are running before class autoloading. // It's possible test are running before class autoloading.
module_load_include('inc', 'islandora', 'includes/tuque'); module_load_include('inc', 'islandora', 'includes/tuque');
module_load_include('inc', 'islandora', 'includes/tuque_wrapper'); module_load_include('inc', 'islandora', 'includes/tuque_wrapper');
module_load_include('inc', 'islandora', 'tests/includes/utilities');
$this->configuration = islandora_get_test_configuration(); $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']); $this->connection = new RepositoryConnection($this->configuration['fedora_url'], $this->configuration['admin_user'], $this->configuration['admin_pass']);
$api = new FedoraApi($this->connection); $api = new FedoraApi($this->connection);
$this->repository = new FedoraRepository($api, new SimpleCache()); $this->repository = new FedoraRepository($api, new SimpleCache());
} }
/** /**
* Restores the original Drupal filter, frees any allocated resources. * 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.
* *
* @see DrupalWebTestCase::tearDown() * @see DrupalWebTestCase::tearDown()
*/ */
public function tearDown() { public function tearDown() {
if ($this->configuration['use_drupal_filter']) {
$this->filterManipulator->restoreDrupalFilter();
}
unset($this->configuration); unset($this->configuration);
parent::tearDown(); parent::tearDown();
} }

14
tests/includes/islandora_web_test_case.inc

@ -21,13 +21,6 @@ class IslandoraWebTestCase extends DrupalWebTestCase {
*/ */
protected $deleteObjectsOnTeardown = TRUE; protected $deleteObjectsOnTeardown = TRUE;
/**
* An instance of IslandoraDrupalFilterManipulator carried between tests.
*
* @var IslandoraDrupalFilterManipulator
*/
protected $filterManipulator;
/** /**
* Defers to IslandoraTestUtilities for missing methods. * 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() * @see DrupalWebTestCase::setUp()
*/ */
@ -83,8 +76,7 @@ class IslandoraWebTestCase extends DrupalWebTestCase {
$this->configuration = islandora_get_test_configuration(); $this->configuration = islandora_get_test_configuration();
if ($this->configuration['use_drupal_filter']) { if ($this->configuration['use_drupal_filter']) {
$this->filterManipulator = new IslandoraDrupalFilterManipulator($this->configuration); $this->setUpDrupalFilter();
$this->filterManipulator->setUpDrupalFilter();
} }
$this->admin = $this->createAdminUser(); $this->admin = $this->createAdminUser();
} }
@ -147,7 +139,7 @@ class IslandoraWebTestCase extends DrupalWebTestCase {
} }
} }
if ($this->configuration['use_drupal_filter']) { if ($this->configuration['use_drupal_filter']) {
$this->filterManipulator->restoreDrupalFilter(); islandora_repair_drupal_filter();
} }
unset($this->admin); unset($this->admin);
unset($this->configuration); unset($this->configuration);

110
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'); 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. * 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()); $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. * Returns an array of IslandoraTestUtilityResults.
* *

Loading…
Cancel
Save