diff --git a/includes/solution_packs.inc b/includes/solution_packs.inc index 7cf0351a..28e22b5d 100644 --- a/includes/solution_packs.inc +++ b/includes/solution_packs.inc @@ -318,6 +318,14 @@ function islandora_install_solution_pack($module, $op = 'install', $force = FALS return; } + // Login as user 1 if the installation is coming from the command line (e.g. + // drush). + if (drupal_is_cli()) { + global $user; + $user = user_load(1); + user_login_finalize(); + } + $t = get_t(); // Some general replacements. @@ -412,6 +420,14 @@ function islandora_install_solution_pack($module, $op = 'install', $force = FALS * directly for each solution pack. */ function islandora_uninstall_solution_pack($module, $force = FALSE) { + // Login as user 1 if the uninstall is coming from the command line (e.g. + // drush). + if (drupal_is_cli()) { + global $user; + $user = user_load(1); + user_login_finalize(); + } + $t = get_t(); drupal_load('module', 'islandora'); module_load_include('inc', 'islandora', 'includes/utilities'); diff --git a/includes/utilities.inc b/includes/utilities.inc index 91988918..971cfaac 100644 --- a/includes/utilities.inc +++ b/includes/utilities.inc @@ -912,3 +912,27 @@ function islandora_as_renderable_array(&$markup_array) { } unset($value); } + +/** + * Creates and returns a singleton of tuque if one does not already exist. + * + * @param object $user + * User who created the request. + * @param object $url + * URL to Fedora. + * + * @return IslandoraTuque + * An IslandoraTuque instance. + */ +function islandora_make_tuque($user, $url) { + if (IslandoraTuque::exists()) { + try { + return new IslandoraTuque($user, $url); + } + catch (Exception $e) { + drupal_set_message(t('Unable to connect to the repository %e', array('%e' => $e)), 'error'); + } + } + + return NULL; +} diff --git a/islandora.info b/islandora.info index 36ed0dff..bbcf2987 100644 --- a/islandora.info +++ b/islandora.info @@ -20,4 +20,5 @@ files[] = tests/islandora_manage_permissions.test files[] = tests/datastream_versions.test files[] = tests/datastream_cache.test files[] = tests/derivatives.test +files[] = tests/islandora_tuque.test php = 5.3 diff --git a/islandora.module b/islandora.module index 8fa20898..a241b6df 100644 --- a/islandora.module +++ b/islandora.module @@ -1012,7 +1012,7 @@ function islandora_default_islandora_printer_object($object, $alter) { * * Includes some very basic error logging. * - * @param object $user + * @param object $new_user * The user to connect as. * @param string $url * The URL to connect to. @@ -1020,19 +1020,47 @@ function islandora_default_islandora_printer_object($object, $alter) { * @return IslandoraTuque * A IslandoraTuque instance */ -function islandora_get_tuque_connection($user = NULL, $url = NULL) { +function islandora_get_tuque_connection($new_user = NULL, $url = NULL) { + module_load_include('inc', 'islandora', 'includes/utilities'); + $tuque = &drupal_static(__FUNCTION__); + + global $user; + + // If tuque is NULL. if (!$tuque) { - if (IslandoraTuque::exists()) { - try { - $tuque = new IslandoraTuque($user, $url); + // If the provided user is not null, pass it along to Tuque. + if ($new_user) { + $tuque = islandora_make_tuque($new_user, $url); + } + // Otherwise give Tuque the global user. + else { + $tuque = islandora_make_tuque($user, $url); + } + } + // Else, tuque already exists. So only make it if the users are different. + else { + if ($new_user) { + // Get the name of the new user. If it's '', then it's actually + // 'anonymous' as far as Tuque is concerned. + $new_user_name = $new_user->name; + if (empty($new_user_name)) { + $new_user_name = 'anonymous'; } - catch (Exception $e) { - drupal_set_message(t('Unable to connect to the repository %e', array('%e' => $e)), 'error'); + if ($new_user_name != $tuque->connection->username) { + $tuque = islandora_make_tuque($new_user); } } else { - return NULL; + // Get the name of the global user. If it's '', then it's actually + // 'anonymous' as far as Tuque is concerned. + $user_name = $user->name; + if (empty($user_name)) { + $user_name = 'anonymous'; + } + if ($user->name != $tuque->connection->username) { + $tuque = islandora_make_tuque($user); + } } } return $tuque; diff --git a/tests/islandora_tuque.test b/tests/islandora_tuque.test new file mode 100644 index 00000000..ca3b5612 --- /dev/null +++ b/tests/islandora_tuque.test @@ -0,0 +1,147 @@ + 'Islandora Tuque', + 'description' => 'Tests basic tuque functionality.', + 'group' => 'Islandora', + ); + } + + /** + * Prepares enviroment for testing. + * + * @see IslandoraWebTestCase::setUp() + */ + public function setUp() { + parent::setUp(array('islandora')); + } + + /** + * Tests that a new tuque instance is created when a new user is provided. + */ + public function testTuqueUserChange() { + global $user; + + $tuque = islandora_get_tuque_connection($user); + + $old_user_name = $tuque->connection->username; + + $new_user = user_load(0); + + $tuque = islandora_get_tuque_connection($new_user); + + $new_user_name = $tuque->connection->username; + + $this->assertNotEqual($old_user_name, $new_user_name, "User in tuque's connection changes when a new user is provided to islandora_get_tuque_connection()."); + + $old_user_name = $new_user_name; + + $tuque = islandora_get_tuque_connection(); + + $new_user_name = $tuque->connection->username; + + $this->assertEqual($old_user_name, $new_user_name, "User in tuque's connection does not change when no user is provided to islandora_get_tuque_connection()."); + } + + /** + * Tests that tuque does not exist and the user is not null. + */ + public function testTuqueDoesNotExistAndUserIsNotNull() { + $tuque = &drupal_static(__FUNCTION__); + $tuque = NULL; + + $user_to_provide = user_load(0); + + $new_tuque = islandora_get_tuque_connection($user_to_provide); + $new_tuque_user_name = $new_tuque->connection->username; + + assertNotNull($new_tuque); + assertNotNull($tuque); + assertEqual($user_to_provide->name, $new_tuque_user_name); + } + + /** + * Tests that tuque does not exist and the user is null. + */ + public function testTuqueDoesNotExistAndUserIsNull() { + $tuque = &drupal_static(__FUNCTION__); + $tuque = NULL; + + $user_to_provide = NULL; + + $new_tuque = islandora_get_tuque_connection($user_to_provide); + $new_tuque_user_name = $new_tuque->connection->username; + + global $user; + + assertNotNull($new_tuque); + assertNotNull($tuque); + assertEqual($new_tuque_user_name, $user); + } + + /** + * Test that tuque exists, the user is not null, the user is not tuque's user. + */ + public function testTuqueExistsAndUserIsNotNullAndUserIsNotTuquesUser() { + $tuque = &drupal_static(__FUNCTION__); + $user_to_provide = user_load(0); + $new_tuque = islandora_get_tuque_connection($user_to_provide); + + assertNotNull($new_tuque); + assertNotNull($tuque); + assertNotEqual($user_to_provide->name, $new_tuque->connection->username); + } + + /** + * Tests that tuque exists, the user is not null, the user is tuque's user. + */ + public function testTuqueExistsAndUserIsNotNullAndUserIsTuquesUser() { + $tuque = &drupal_static(__FUNCTION__); + $new_tuque = islandora_get_tuque_connection($tuque->connection->username); + + assertNotNull($new_tuque); + assertNotNull($tuque); + assertEqual($tuque->connection->username, $new_tuque->connection->username); + } + + /** + * Tests that tuque exists, the user is null, global user is not tuque's user. + */ + public function testTuqueExistsAndUserIsNullAndGlobalUserIsNotTuquesUser() { + $tuque = &drupal_static(__FUNCTION__); + global $user; + $user = NULL; + $new_tuque = islandora_get_tuque_connection($user); + + assertNotNull($new_tuque); + assertNotNull($tuque); + assertNotEqual($new_tuque->connection->username, $user); + } + + /** + * Tests that tuque exists, the user is null, global user is tuque's user. + */ + public function testTuqueExistsAndUserIsNullAndGlobalUserIsTuquesUser() { + $tuque = &drupal_static(__FUNCTION__); + global $_islandora_user; + $_islandora_user = $tuque->connection->username; + $new_tuque = islandora_get_tuque_connection($user); + + assertNotNull($new_tuque); + assertNotNull($tuque); + assertEqual($new_tuque->connection->username, $user); + } +}