Browse Source

better asserting objects

pull/606/head
qadan 10 years ago
parent
commit
d982ac1985
  1. 77
      tests/includes/utilities.inc

77
tests/includes/utilities.inc

@ -112,10 +112,7 @@ class IslandoraTestUtilities extends IslandoraTestUtilityClass {
* TRUE on success, FALSE on fail. * TRUE on success, FALSE on fail.
*/ */
public function assertDatastreams($object, array $datastreams) { public function assertDatastreams($object, array $datastreams) {
if (!self::assertFedoraObject($object)) { if (self::assertFedoraObject($object)) {
$this->addResult(FALSE, "Failed. Object passed in is invalid.", 'Islandora');
}
else {
$missing_datastreams = array_diff_key(array_flip($datastreams), $object->repository->api->a->listDatastreams($object->id)); $missing_datastreams = array_diff_key(array_flip($datastreams), $object->repository->api->a->listDatastreams($object->id));
if (!empty($missing_datastreams)) { if (!empty($missing_datastreams)) {
@ -137,8 +134,8 @@ class IslandoraTestUtilities extends IslandoraTestUtilityClass {
* An array of datastreams to confirm not present. * An array of datastreams to confirm not present.
*/ */
public function assertNoDatastreams($object, array $datastreams) { public function assertNoDatastreams($object, array $datastreams) {
if (!self::assertFedoraObject($object)) { if (self::assertFedoraObject($object, FALSE)) {
$this->addResult(FALSE, "Failed. Object passed in is invalid.", 'Islandora'); $this->addResult("Failed to check the datastreams on the object; the object could not be loaded.");
return; return;
} }
$found_datastreams = array_intersect_key(array_flip($datastreams), $object->repository->api->a->listDatastreams($object->id)); $found_datastreams = array_intersect_key(array_flip($datastreams), $object->repository->api->a->listDatastreams($object->id));
@ -169,8 +166,7 @@ class IslandoraTestUtilities extends IslandoraTestUtilityClass {
* for examples. * for examples.
*/ */
public function validateDatastreams($object, array $datastreams) { public function validateDatastreams($object, array $datastreams) {
if (self::assertFedoraObject($object, FALSE)) {
if (!self::assertFedoraObject($object)) {
$this->addResult(FALSE, "Datastream validation failed; Object passed in is invalid.", 'Islandora'); $this->addResult(FALSE, "Datastream validation failed; Object passed in is invalid.", 'Islandora');
return; return;
} }
@ -347,48 +343,33 @@ QUERY;
} }
/** /**
* Asserts that an object is a FedoraObject or an IslandoraFedoraObject. * Asserts a Fedora object from an object or PID, and loads it if possible.
*
* @param object $object
* The object to assess.
*
* @return bool
* TRUE if it is either of those object types, or FALSE otherwise.
*/
public static function assertFedoraObject($object) {
return ($object instanceof FedoraObject);
}
/**
* Asserts that the given PID represents a Fedora object.
* *
* @param string $pid * @param FedoraObject|string $object
* The PID of the object. * The object to assess, or a string to use as a PID.
* @param bool $exists
* Whether or not the object should exist in the repository, according to
* our assertion. Defaults to TRUE.
* *
* @return bool * @return FedoraObject|bool
* TRUE if the object exists in the repository; FALSE otherwise. * The object in question if it was a FedoraObject or loaded into one, or
* FALSE otherwise.
*/ */
public function assertObjectExists($pid) { public static function assertFedoraObject($object, $exists = TRUE) {
$object = islandora_object_load($pid); if ($object instanceof FedoraObject) {
$exists = $this->assertFedoraObject($object); $this->addResult($exists, "Object loaded from $object->id is a Fedora repository object.");
$this->addResult($exists, "Object $pid exists in the repository."); return $object;
return $exists; }
} elseif (is_string($object)) {
$loaded_object = islandora_object_load($object);
/** if ($loaded_object instanceof FedoraObject) {
* Asserts that the given PID does not represent a Fedora object. $this->addResult($exists, "Successfully loaded object $object from the Fedora repository.");
* return $loaded_object;
* @param string $pid }
* The PID of the object. $this->addResult($exists, "Object identified by $object was not found in the repository.");
* return FALSE;
* @return bool }
* TRUE if the object does not exist in the repository, or FALSE otherwise. $this->addResult($exists, "Object passed in was not found in the repository.");
*/ return FALSE;
public function assertNoObjectExists($pid) {
$object = islandora_object_load($pid);
$exists = $this->assertFedoraObject($object);
$this->addResult(!$exists, "Object $pid does not exist in the repository.");
return !$exists;
} }
} }

Loading…
Cancel
Save