<?php
// $Id$
/*
* Tests the functions of the Fedora Repository module.
*/
class FedoraRepositoryTestCase extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'Fedora Repository',
'description' => t('The Fedora repository content models.'),
'group' => t('fedora repository'),
);
}
function setUp() {
parent::setUp('fedora_repository', 'tabs');
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
// Create and login user.
$repository_user = $this->drupalCreateFedoraUser(array('add fedora datastreams',
'edit fedora meta data',
'edit tags datastream',
'ingest new fedora objects',
'purge objects and datastreams',
'view fedora collection',
'view detailed list of content'));
$this->drupalLogin($repository_user);
}
public function testDatastreams() {
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
$pid_list = array();
// Test Add datastream form
$cm_item = fedora_item::ingest_new_item();
$cm_item->add_relationship('hasModel', 'fedora-system:ContentModel-3.0', FEDORA_MODEL_URI);
$cm_item->add_datastream_from_file(drupal_get_path('module', 'fedora_repository').'/content_models/STRICT_PDFCM.xml', 'ISLANDORACM');
$pids_list[] = $cm_item->pid;
// Ingest a new PDF object
$pdf_item = fedora_item::ingest_new_item();
$pdf_item->add_relationship('hasModel', $cm_item->pid, FEDORA_MODEL_URI);
$pids_list[] = $pdf_item->pid;
// Submit the add datastream form.
$add_stream_form = array();
$add_stream_form['pid'] = $pdf_item->pid;
$add_stream_form['stream_label'] = $this->randomName(20);
$rpath = realpath(drupal_get_path('module', 'fedora_repository') . '/tests/test_files/lorem_ipsum.pdf');
$add_stream_form['files[add-stream-file-location]'] = $rpath;
$add_stream_form['stream_id'] = 'OBJ';
$this->drupalPost('fedora/repository/' . $pdf_item->pid, $add_stream_form, 'Add Datastream');
// Test replace datastream form
$this->assertText('The datastream has been uploaded', "Add initial datastream.", 'fedora datastreams');
$this->drupalGet('fedora/repository/'.$pdf_item->pid);
$this->assertText($add_stream_form['stream_label'], 'Datastream appears in DS list.', 'fedora datastreams');
$replace_stream_edit = array();
$replace_stream_rpath = realpath(drupal_get_path('module', 'fedora_repository') . '/tests/test_files/jabberwocky.pdf');
$replace_stream_edit['files[file]'] = $replace_stream_rpath;
$this->drupalPost("fedora/repository/replaceStream/$pdf_item->pid/OBJ/NewLabel", $replace_stream_edit , 'Replace Datastream' );
$this->assertText('NewLabel', "Replacement datastream appears in DS list.", 'fedora datastreams');
$this->outputScreenContents();
}
/**
* Add an item based on a content model. Initially we will assume that the repository
* will be populated with the default content models and collections that are
* created from the admin settings -> Install Demos page.
*
*/
public function testIngest() {
// Check that the 'Strict PDF' content model exists in the repository.
//$pdf_cm = new Fedora_Item('islandora:strict_pdf');
$pid_list = array();
// Create a collection for ingesting PDF content model objects.
//$this->drupalGet('fedora/ingestObject/islandora:top/Islandora%20Top-Level%20Collection');
$ingest_form = array();
$ingest_form['models'] = 'islandora:collectionCModel/ISLANDORACM';
$this->drupalPost('fedora/ingestObject/islandora:top/Islandora%20Top-Level%20Collection', $ingest_form, 'Next');
// Add a sample PDF object via the web ingest form.
// Required fields are file location, dc:title and dc:description
$ingest_form_step_2 = array();
$ingest_form_step_2['dc:title'] = $this->randomName(32);
$ingest_form_step_2['dc:description'] = $this->randomName(256);
$ingest_form_step_2['files[ingest-file-location]'] = realpath(drupal_get_path('module', 'fedora_repository') . '/tests/test_files/lorem_ipsum.pdf');
$this->drupalPost(NULL, $ingest_form_step_2, 'Ingest');
$this->assertText('mimetype (application/pdf) is not associated with this Content Model', 'PDF mime type not accepted in collection content model.', 'message');
$this->outputScreenContents('Initial ingest form submit step 2', 'fedora_repository');
// Now try ingesting a proper collection policy.
$ingest_form = array();
$ingest_form['models'] = 'islandora:collectionCModel/ISLANDORACM';
$this->drupalPost('fedora/ingestObject/islandora:top/Islandora%20Top-Level%20Collection', $ingest_form, 'Next');
// Required fields are file location, dc:title and dc:description
$ingest_form_step_2 = array();
$ingest_title = $this->randomName(32);
$ingest_form_step_2['dc:title'] = $ingest_title;
$ingest_form_step_2['dc:description'] = $this->randomName(256);
$ingest_form_step_2['files[ingest-file-location]'] = realpath(drupal_get_path('module', 'fedora_repository') . '/collection_policies/PDF-COLLECTION POLICY.xml');
$this->drupalPost(NULL, $ingest_form_step_2, 'Ingest');
//$this->outputScreenContents('Initial ingest form submit step 2 - PDF collection', 'fedora_repository');
$this->assertPattern('/Item .* created successfully./', "Verified item created.");
$pid = $this->getIngestedPid();
$this->drupalGet("fedora/repository/$pid");
$this->assertLink('Home');
$this->assertLink('Digital Repository');
$this->assertLink($ingest_title);
$pid_list[] = $pid;
$this->pass('Now attempting to ingest a PDF into the new collection.');
// Now try ingesting a PDF
$ingest_form = array();
$ingest_form['models'] = 'islandora:strict_pdf/ISLANDORACM';
$this->drupalPost("fedora/ingestObject/$pid/", $ingest_form, 'Next');
// Required fields are file location, dc:title and dc:description
$ingest_form_step_2 = array();
$ingest_form_step_2['dc:title'] = "Lorem Ipsum";
$ingest_form_step_2['dc:description'] = $this->randomName(256);
$ingest_form_step_2['files[ingest-file-location]'] = realpath(drupal_get_path('module', 'fedora_repository') . '/tests/test_files/lorem_ipsum.pdf');
$this->drupalPost(NULL, $ingest_form_step_2, 'Ingest');
$pid = $this->getIngestedPid();
$pid_list[] = $pid;
if (!empty($pid)) {
$this->pass("Successfully ingested PDF object $pid.");
}
$this->cleanUpRepository($pid_list);
}
private function cleanUpRepository($pid_list = array()) {
$this->pass("This is the PID list to purge: ". implode(", ", $pid_list) );
foreach ($pid_list as $pid) {
$this->drupalPost("fedora/repository/purgeObject/$pid", array(), 'Purge');
$this->drupalPost(NULL, array(), 'Delete');
}
}
private function getIngestedPid() {
$subject = $this->drupalGetContent();
$pattern = '/">(.*)<\/a> created successfully./';
$matches = array();
$res = preg_match($pattern, $subject, $matches);
return $matches[1];
}
private function outputScreenContents($description = '', $basename = '') {
// This is a hack to get a directory that won't be cleaned up by SimpleTest.
$file_dir = file_directory_path() . '../simpletest_output_pages';
if (!is_dir($file_dir)) {
mkdir($file_dir, 0777, TRUE);
}
$output_path = "$file_dir/$basename.". $this->randomName(10) . '.html';
$rv = file_put_contents($output_path, $this->drupalGetContent());
$this->pass("$description: Contents of result page are ". l('here', $output_path));
}
protected function drupalCreateFedoraUser($permissions = array('access comments', 'access content', 'post comments', 'post comments without approval')) {
// Create a role with the given permission set.
if (!($rid = $this->drupalCreateRole($permissions))) {
return FALSE;
}
// Create a user assigned to that role.
$edit = array();
$edit['name'] = 'simpletestuser';
$edit['mail'] = $edit['name'] . '@example.com';
$edit['roles'] = array($rid => $rid);
$edit['pass'] = 'simpletestpass';
$edit['status'] = 1;
$account = user_save('', $edit);
$this->assertTrue(!empty($account->uid), t('User created with name %name and pass %pass', array('%name' => $edit['name'], '%pass' => $edit['pass'])), t('User login'));
if (empty($account->uid)) {
return FALSE;
}
// Add the raw password so that we can log in as this user.
$account->pass_raw = $edit['pass'];
return $account;
}
}