Skip to content

Commit

Permalink
Add more profile tests
Browse files Browse the repository at this point in the history
  • Loading branch information
whikloj committed Apr 12, 2024
1 parent 6c15b36 commit c57f1e0
Show file tree
Hide file tree
Showing 7 changed files with 835 additions and 348 deletions.
53 changes: 30 additions & 23 deletions src/Bag.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,12 @@ class Bag
* A map of some supported extensions to their serialization MIME types.
*/
private const SERIALIZATION_MAPPING = [
'bz' => 'application/x-bzip',
'bz2' => 'application/x-bzip2',
'gz' => 'application/gzip',
'tgz' => 'application/gzip',
'tar' => 'application/x-tar',
'zip' => 'application/zip',
'.bz' => 'application/x-bzip',
'.bz2' => 'application/x-bzip2',
'.gz' => 'application/gzip',
'.tgz' => 'application/gzip',
'.tar' => 'application/x-tar',
'.zip' => 'application/zip',
];

/**
Expand Down Expand Up @@ -249,10 +249,10 @@ class Bag
private bool $loaded;

/**
* The serialization extension of the bag or null if not serialized.
* The serialization mime-type of the bag or null if not serialized.
* @var string|null
*/
private ?string $serialization_extension = null;
private ?string $serialization = null;

/**
* Bag constructor.
Expand All @@ -262,7 +262,7 @@ class Bag
* @param boolean $new
* Are we making a new bag?
* @param string|null $extension
* The compressed extension of the bag (if compressed) or null if not.
* The extension of the bag (if it was serialized) or null if not.
*
* @throws FilesystemException
* Problems accessing a file.
Expand All @@ -288,7 +288,7 @@ private function __construct(string $rootPath, bool $new = true, ?string $extens
if ($new) {
$this->createNewBag();
} else {
$this->serialization_extension = $extension;
$this->serialization = is_null($extension) ? null : $this->determineSerializationMimetype($extension);
$this->loadBag();
}
}
Expand Down Expand Up @@ -321,15 +321,14 @@ public static function create(string $rootPath): Bag
public static function load(string $rootPath): Bag
{
$rootPath = BagUtils::getAbsolute($rootPath, true);
$serialized_extension = null;
$extension = null;
if (is_file($rootPath)) {
$extension = self::getExtension($rootPath);
if (self::hasExtension($extension, array_merge(self::ZIP_EXTENSIONS, self::TAR_EXTENSIONS))) {
$serialized_extension = $extension;
$rootPath = self::uncompressBag($rootPath, $serialized_extension);
$rootPath = self::uncompressBag($rootPath, $extension);
}
}
return new Bag($rootPath, false, $serialized_extension);
return new Bag($rootPath, false, $extension);
}

/**
Expand Down Expand Up @@ -1227,20 +1226,12 @@ public function upgrade(): void
$this->update();
}

/**
* @return bool True if the bag was loaded from a serialized format.
*/
public function hasSerialization(): bool
{
return $this->serialization_extension !== null;
}

/**
* @return string|null The serialization format if the bag was loaded from a serialized format or null.
*/
public function getSerializationMimeType(): ?string
{
return self::SERIALIZATION_MAPPING[$this->serialization_extension] ?? null;
return $this->serialization;
}

/*
Expand Down Expand Up @@ -2456,4 +2447,20 @@ private function mergeWarnings(array $newWarnings): void
{
$this->bagWarnings = array_merge($this->bagWarnings, $newWarnings);
}

/**
* Determine the serialization mimetype from the extension.
* @param string $extension The extension.
* @return string The serialization mimetype.
* @throws BagItException If the serialization mimetype cannot be determined.
*/
private function determineSerializationMimetype(string $extension): string
{
foreach (self::SERIALIZATION_MAPPING as $extension => $mimetype) {
if (str_ends_with($extension, $extension)) {
return $mimetype;
}
}
throw new BagItException("Unable to determine serialization mimetype for extension ($extension).");
}
}
37 changes: 37 additions & 0 deletions tests/BagInternalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,4 +300,41 @@ public function testCheckFilePathEncoding(): void
$methodCall->invokeArgs($payload, ["succeed-for-%0D-filename.txt", 1]);
$this->assertCount(2, $loadIssues->getValue($payload)['error']);
}

/**
* @group Internal
* @covers \whikloj\BagItTools\Bag::hasExtension
*/
public function testHasExtension(): void
{
$bag = Bag::create($this->tmpdir);
$class = new ReflectionClass('whikloj\BagItTools\Bag');
$methodCall = $class->getMethod('hasExtension');
$methodCall->setAccessible(true);

$this->assertTrue($methodCall->invokeArgs($bag, ['file.txt', ['.txt', '.jpg']]));
$this->assertTrue($methodCall->invokeArgs($bag, ['file.old.txt', ['.txt', '.jpg']]));
$this->assertTrue($methodCall->invokeArgs($bag, ['file.jpg', ['.txt', '.jpg']]));
$this->assertTrue($methodCall->invokeArgs($bag, ['file.jpg.txt', ['.txt', '.jpg']]));

$this->assertFalse($methodCall->invokeArgs($bag, ['file.jpg.old', ['.txt', '.jpg']]));
$this->assertFalse($methodCall->invokeArgs($bag, ['file.old', ['.txt', '.jpg']]));
}

/**
* @group Internal
* @covers \whikloj\BagItTools\Bag::getExtension
*/
public function testGetExtension(): void
{
$bag = Bag::create($this->tmpdir);
$class = new ReflectionClass('whikloj\BagItTools\Bag');
$methodCall = $class->getMethod('getExtension');
$methodCall->setAccessible(true);

$this->assertEquals('.txt', $methodCall->invokeArgs($bag, ['file.txt']));
$this->assertEquals('.old.txt', $methodCall->invokeArgs($bag, ['file.old.txt']));
$this->assertEquals('.old.txt.jpg', $methodCall->invokeArgs($bag, ['file.old.txt.jpg']));
$this->assertEquals('.old.txt.jpg', $methodCall->invokeArgs($bag, ['file_something.old.txt.jpg']));
}
}
159 changes: 159 additions & 0 deletions tests/Profiles/BagItProfileBarTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
<?php

namespace Profiles;

use whikloj\BagItTools\Profiles\BagItProfile;
use whikloj\BagItTools\Test\BagItTestFramework;
use whikloj\BagItTools\Test\Profiles\ProfileTestFramework;

/**
* Test BagItProfile against the specifications foo
* @package Profiles
* @coversDefaultClass \whikloj\BagItTools\Profiles\BagItProfile
*/
class BagItProfileBarTest extends ProfileTestFramework
{
protected function getProfileFilename(): string
{
return self::TEST_RESOURCES . '/profiles/bagProfileBar.json';
}

protected function getProfileUri(): string
{
return self::$remote_urls[1];
}

protected function getProfileValues(): array
{
return [
'identifier' => 'http://canadiana.org/standards/bagit/tdr_ingest.json',
'spec_version' => '1.2.0',
'version' => '1.2',
'source_organization' => 'Candiana.org',
'external_description' => 'BagIt Profile for ingesting content into the C.O. TDR loading dock.',
'contact_name' => 'William Wueppelmann',
'contact_email' => 'tdr@canadiana.com',
'contact_phone' => null,
'bag_info_tags' => [
"Source-Organization" => [
"required" => true,
"values" => [
"Simon Fraser University",
"York University"
],
'repeatable' => true,
'description' => '',
],
"Organization-Address" => [
"required" => true,
"values" => [
"8888 University Drive Burnaby, B.C. V5A 1S6 Canada",
"4700 Keele Street Toronto, Ontario M3J 1P3 Canada"
],
'repeatable' => true,
'description' => '',
],
"Contact-Name" => [
"required" => true,
"values" => [
"Mark Jordan",
"Nick Ruest"
],
'repeatable' => true,
'description' => '',
],
"Contact-Phone" => [
"required" => false,
'values' => [],
'repeatable' => true,
'description' => '',
],
"Contact-Email" => [
"required" => true,
'values' => [],
'repeatable' => true,
'description' => '',
],
"External-Description" => [
"required" => true,
'values' => [],
'repeatable' => true,
'description' => '',
],
"External-Identifier" => [
"required" => false,
'values' => [],
'repeatable' => true,
'description' => '',
],
"Bag-Size" => [
"required" => true,
'values' => [],
'repeatable' => true,
'description' => '',
],
"Bag-Group-Identifier" => [
"required" => false,
'values' => [],
'repeatable' => true,
'description' => '',
],
"Bag-Count" => [
"required" => true,
'values' => [],
'repeatable' => true,
'description' => '',
],
"Internal-Sender-Identifier" => [
"required" => false,
'values' => [],
'repeatable' => true,
'description' => '',
],
"Internal-Sender-Description" => [
"required" => false,
'values' => [],
'repeatable' => true,
'description' => '',
],
"Bagging-Date" => [
"required" => true,
'values' => [],
'repeatable' => true,
'description' => '',
],
"Payload-Oxum" => [
"required" => true,
'values' => [],
'repeatable' => true,
'description' => '',
],
],
'manifests_required' => [
"md5",
],
'manifests_allowed' => [],
'allow_fetch_txt' => false,
'require_fetch_txt' => false,
'data_empty' => false,
'serialization' => 'optional',
'accept_serialization' => [
"application/zip",
],
'accept_bagit_version' => [
"0.96",
],
'tag_manifests_required' => [
"md5"
],
'tag_manifests_allowed' => [],
'tag_files_required' => [
"DPN/dpnFirstNode.txt",
"DPN/dpnRegistry",
],
'tag_files_allowed' => [],
'payload_files_required' => [],
'payload_files_allowed' => [],
];
}
}
82 changes: 82 additions & 0 deletions tests/Profiles/BagItProfileFooTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace Profiles;

use whikloj\BagItTools\Test\Profiles\ProfileTestFramework;

/**
* Test BagItProfile against the specifications foo
* @package Profiles
* @coversDefaultClass \whikloj\BagItTools\Profiles\BagItProfile
*/
class BagItProfileFooTest extends ProfileTestFramework
{
protected function getProfileFilename(): string
{
return self::TEST_RESOURCES . '/profiles/bagProfileFoo.json';
}

protected function getProfileUri(): string
{
return self::$remote_urls[0];
}

protected function getProfileValues(): array
{
return [
'identifier' => 'http://www.library.yale.edu/mssa/bagitprofiles/disk_images.json',
'spec_version' => '1.1.0',
'version' => '0.3',
'source_organization' => 'Yale University',
'external_description' => 'BagIt Profile for packaging disk images',
'contact_name' => 'Mark Matienzo',
'contact_email' => null,
'contact_phone' => null,
'bag_info_tags' => [
'Bagging-Date' => [
'required' => true,
'values' => [],
'repeatable' => true,
'description' => '',
],
'Source-Organization' => [
'required' => true,
'values' => [
'Simon Fraser University',
'York University',
],
'repeatable' => true,
'description' => '',
],
"Contact-Phone" => [
"required" => true,
"values" => [],
'repeatable' => true,
'description' => '',
],
],
'manifests_required' => [
"md5",
],
'manifests_allowed' => [],
'allow_fetch_txt' => false,
'require_fetch_txt' => false,
'data_empty' => false,
'serialization' => 'required',
'accept_serialization' => [
"application/tar",
"application/zip",
],
'accept_bagit_version' => [
"0.96",
"0.97",
],
'tag_manifests_required' => [],
'tag_manifests_allowed' => [],
'tag_files_required' => [],
'tag_files_allowed' => [],
'payload_files_required' => [],
'payload_files_allowed' => [],
];
}
}
Loading

0 comments on commit c57f1e0

Please sign in to comment.