Browse Source

Add some basic tests for the hooked access callback.

pull/352/head
Adam Vessey 12 years ago
parent
commit
023a410c71
  1. 1
      islandora.info
  2. 181
      tests/hooked_access.test
  3. 7
      tests/islandora_hooked_access_test.info
  4. 31
      tests/islandora_hooked_access_test.module

1
islandora.info

@ -15,5 +15,6 @@ files[] = tests/islandora_web_test_case.inc
files[] = tests/authtokens.test files[] = tests/authtokens.test
files[] = tests/hooks.test files[] = tests/hooks.test
files[] = tests/ingest.test files[] = tests/ingest.test
files[] = tests/hooked_access.test
files[] = tests/islandora_manage_permissions.test files[] = tests/islandora_manage_permissions.test
php = 5.3 php = 5.3

181
tests/hooked_access.test

@ -0,0 +1,181 @@
<?php
/**
* @file
* Tests to see if the hooks get called when appropriate.
*
* In the test module 'islandora_hooked_access_test' there are implementations
* of hooks being tested. These implementations modifies the session, and
* that's how we test if the hook gets called.
*
* To make sense of these tests reference islandora_hooked_access_test.module.
*/
class IslandoraHookedAccessTestCase extends IslandoraWebTestCase {
/**
* Gets info to display to describe this test.
*
* @see IslandoraWebTestCase::getInfo()
*/
public static function getInfo() {
return array(
'name' => 'Islandora Hooked Access Callback',
'description' => 'Ensure that the hooked access callback returns appropriate results.',
'group' => 'Islandora',
);
}
/**
* Creates an admin user and a connection to a fedora repository.
*
* @see IslandoraWebTestCase::setUp()
*/
public function setUp() {
parent::setUp('islandora_hooked_access_test');
$this->repository = $this->admin->repository;
$this->objects = array(
'test:testAccessHook',
);
$this->op = FEDORA_VIEW_OBJECTS;
$this->other_op = FEDORA_INGEST;
$this->denied_op = FEDORA_PURGE;
$this->purgeTestObjects();
$this->dsid = 'asdf';
$this->createTestObjects();
$this->object = $this->repository->getObject('test:testAccessHook');
}
/**
* Free any objects/resources created for this test.
*
* @see IslandoraWebTestCase::tearDown()
*/
public function tearDown() {
$this->purgeTestObjects();
unset($this->repository);
unset($_SESSION['islandora_hooked_access_test']);
parent::tearDown();
}
public function createTestObjects() {
foreach ($this->objects as $object_id) {
$object = $this->repository->constructObject($object_id);
$object->label = $object_id;
$datastream = $object->constructDatastream($this->dsid, 'M');
$datastream->label = 'fdsa';
$datastream->mimetype = 'text/plain';
$datastream->content = 'Some kinda awesome content stuffs...';
$object->ingestDatastream($datastream);
$this->repository->ingestObject($object);
}
}
/**
* Purge any objects created by the test's in this class.
*/
public function purgeTestObjects() {
foreach ($this->objects as $object) {
try {
$object = $this->repository->getObject($object);
$this->repository->purgeObject($object->id);
}
catch (Exception $e) {
// Meh... Either it didn't exist or the purge failed.
}
}
}
/**
* Deny an object permission check without an object.
*/
public function testDenyBadObject() {
$this->assertFalse(islandora_object_access($this->op, 'this is not an object'), 'Deny bad objects.');
}
/**
* Deny a datastream permission check without a datastream.
*/
public function testDenyBadDatastream() {
$this->assertFalse(islandora_datastream_access($this->op, 'this is not a datastream'), 'Deny bad datastreams.');
}
/**
* Allow operation on object.
*/
public function testAllowObject() {
$user = $this->drupalCreateUser(array($this->op));
$_SESSION['islandora_hooked_access_test'] = array(
$this->op,
$this->object,
$user,
);
$this->assertTrue(islandora_object_access($this->op, $this->object, $user), 'Allow object access.');
}
/**
* Allow operation on datastream.
*/
public function testAllowDatastream() {
$user = $this->drupalCreateUser(array($this->op));
$_SESSION['islandora_hooked_access_test'] = array(
$this->op,
$this->object['asdf'],
$user,
);
$this->assertTrue(islandora_datastream_access($this->op, $this->object['asdf'], $user), 'Allow datastream access.');
}
/**
* Deny an operation which was not explicitly allowed on an object.
*/
public function testDenyObjectImplicit() {
$user = $this->drupalCreateUser(array($this->other_op));
// This variable forces either a TRUE or FALSE from Islandora's
// implementation of hook_islandora_object_access().
variable_set('islandora_strict_user_access_enforcement', FALSE);
$_SESSION['islandora_hooked_access_test'] = array(
$this->other_op,
$this->object,
$user,
);
$this->assertFalse(islandora_object_access($this->op, $this->object, $user), 'Implied denial of object access.');
}
/**
* Deny an operation which was not explicitly allowed on a datastream.
*/
public function testDenyDatastreamImplicit() {
$user = $this->drupalCreateUser(array($this->other_op));
// This variable forces either a TRUE or FALSE from Islandora's
// implementation of hook_islandora_object_access().
variable_set('islandora_strict_user_access_enforcement', FALSE);
$_SESSION['islandora_hooked_access_test'] = array(
$this->other_op,
$this->object['asdf'],
$user,
);
$this->assertFalse(islandora_datastream_access($this->op, $this->object['asdf'], $user), 'Implied denial of datastream access.');
}
/**
* Deny an operation which was not explicitly allowed on an object.
*/
public function testDenyObjectExplicit() {
$this->assertFalse(islandora_object_access($this->denied_op, $this->object), 'Explicit denial of object access.');
}
/**
* Deny an operation which was not explicitly allowed on a datastream.
*/
public function testDenyDatastreamExplicit() {
$this->assertFalse(islandora_datastream_access($this->denied_op, $this->object['asdf']), 'Explicit denial of datastream access.');
}
}

7
tests/islandora_hooked_access_test.info

@ -0,0 +1,7 @@
name = Islandora Hooked Access Callback testing
description = Tests callback hooks. Do not enable.
core = 7.x
package = Testing
hidden = TRUE
files[] = islandora_hooks_test.module
dependencies[] = islandora

31
tests/islandora_hooked_access_test.module

@ -0,0 +1,31 @@
<?php
/**
* @file
* Hook implementations tested in hooked_access.test
*/
/**
* Implements hook_islandora_object_access().
*/
function islandora_hooked_access_test_islandora_object_access($op, $object, $user) {
if ($op == FEDORA_PURGE) {
return FALSE;
}
if (isset($_SESSION['islandora_hooked_access_test']) && $_SESSION['islandora_hooked_access_test'] === func_get_args()) {
return TRUE;
}
return NULL;
}
/**
* Implements hook_islandora_datastream_access().
*/
function islandora_hooked_access_test_islandora_datastream_access($op, $datastream, $user) {
if (isset($_SESSION['islandora_hooked_access_test']) && $_SESSION['islandora_hooked_access_test'] === func_get_args()) {
return TRUE;
}
return NULL;
}
Loading…
Cancel
Save