From 6f70e085ae13879e00b15cb36200eab568d3b6b4 Mon Sep 17 00:00:00 2001 From: haogatyp Date: Fri, 6 Oct 2023 15:17:09 +0200 Subject: [PATCH] #766 Adjust option getters and setters for NULL values. --- modules/oai/models/DefaultServer.php | 104 +++++++++++------- .../oai/views/helpers/ListMetaDataFormats.php | 13 +++ tests/modules/oai/models/BaseServerTest.php | 42 +++++++ 3 files changed, 119 insertions(+), 40 deletions(-) diff --git a/modules/oai/models/DefaultServer.php b/modules/oai/models/DefaultServer.php index 7e005f1be..73762aee2 100644 --- a/modules/oai/models/DefaultServer.php +++ b/modules/oai/models/DefaultServer.php @@ -238,8 +238,7 @@ protected function handleRequestIntern($oaiRequest, $requestUri) $this->loadStyleSheet(); $this->setupProcessor(); - $metadataPrefixPath = $this->getScriptPath() . DIRECTORY_SEPARATOR . 'prefixes'; - $resumptionPath = $this->getResumptionTokenPath(); + $resumptionPath = $this->getResumptionTokenPath(); $request = new Oai_Model_Request(); @@ -703,6 +702,8 @@ private function addSpecInformation(DOMNode $document, $information) */ private function addFrontdoorUrlAttribute(DOMNode $document, $docid) { + $this->checkBaseUrl(); + $url = $this->getBaseUrl() . '/frontdoor/index/index/docId/' . $docid; $owner = $document->ownerDocument; @@ -720,6 +721,8 @@ private function addFrontdoorUrlAttribute(DOMNode $document, $docid) */ private function addFileUrlAttribute($file, $docid, $filename) { + $this->checkBaseUrl(); + $url = $this->getBaseUrl() . '/files/' . $docid . '/' . rawurlencode($filename); $owner = $file->ownerDocument; @@ -736,6 +739,8 @@ private function addFileUrlAttribute($file, $docid, $filename) */ private function addDdbTransferElement(DOMNode $document, $docid) { + $this->checkBaseUrl(); + $url = $this->getBaseUrl() . '/oai/container/index/docId/' . $docid; $fileElement = $document->ownerDocument->createElement('TransferUrl'); @@ -876,7 +881,7 @@ public function setOaiConfig($oaiConfig) } /** - * @return string + * @return string|false */ public function getScriptPath() { @@ -884,7 +889,7 @@ public function getScriptPath() } /** - * @param string $scriptPath + * @param string|false $scriptPath */ public function setScriptPath($scriptPath) { @@ -892,7 +897,7 @@ public function setScriptPath($scriptPath) } /** - * @return string + * @return string|null */ public function getBaseUrl() { @@ -900,7 +905,7 @@ public function getBaseUrl() } /** - * @param string $baseUrl + * @param string|null $baseUrl */ public function setBaseUrl($baseUrl) { @@ -908,7 +913,7 @@ public function setBaseUrl($baseUrl) } /** - * @return string + * @return string|null */ public function getBaseUri() { @@ -916,7 +921,7 @@ public function getBaseUri() } /** - * @param string $baseUri + * @param string|null $baseUri */ public function setBaseUri($baseUri) { @@ -952,7 +957,7 @@ public function getResumptionTokenPath() /** * Sets the temporary path for resumption tokens. * - * @param string $path + * @param string|null $path */ public function setResumptionTokenPath($path) { @@ -972,7 +977,7 @@ public function getEmailContact() /** * Sets the contact email address. * - * @param string $email + * @param string|null $email */ public function setEmailContact($email) { @@ -999,7 +1004,7 @@ public function getOaiBaseUrl() /** * Sets the OAI base url. * - * @param string $url + * @param string|null $url */ public function setOaiBaseUrl($url) { @@ -1019,7 +1024,7 @@ public function getRepositoryName() /** * Sets the repository name. * - * @param string $repoName + * @param string|null $repoName */ public function setRepositoryName($repoName) { @@ -1039,7 +1044,7 @@ public function getRepositoryIdentifier() /** * Sets the repository identifier. * - * @param string $repoId + * @param string|null $repoId */ public function setRepositoryIdentifier($repoId) { @@ -1059,7 +1064,7 @@ public function getSampleIdentifier() /** * Sets the sample identifier. * - * @param string $sampleId + * @param string|null $sampleId */ public function setSampleIdentifier($sampleId) { @@ -1073,7 +1078,7 @@ public function setSampleIdentifier($sampleId) */ public function getMaxListIdentifiers() { - return $this->maxListIdentifiers ?? 0; + return $this->maxListIdentifiers; } /** @@ -1093,7 +1098,7 @@ public function setMaxListIdentifiers($maxListIds) */ public function getMaxListRecords() { - return $this->maxListRecords ?? 0; + return $this->maxListRecords; } /** @@ -1109,17 +1114,17 @@ public function setMaxListRecords($maxListRecs) /** * Return xslt file name / file path. * - * @return string + * @return string|null */ public function getXsltFile() { - return $this->xsltFile ?? ''; + return $this->xsltFile; } /** * Sets the xslt file name / file path. * - * @param string $file + * @param string|null $file */ public function setXsltFile($file) { @@ -1144,16 +1149,18 @@ public function getViewHelpers() /** * Sets the viewHelpers * - * @param array|string $viewHelpers + * @param array|string|null $viewHelpers */ public function setViewHelpers($viewHelpers) { if (is_string($viewHelpers)) { - $viewHelpers = array_map('trim', explode(',', $viewHelpers)); + $viewHelpers = $viewHelpers !== '' ? array_map('trim', explode(',', $viewHelpers)) : []; } - // listMetadataFormats ist part of basic OAI functionality. - $viewHelpers = array_values(array_diff($viewHelpers, ['listMetadataFormats'])); + if (is_array($viewHelpers)) { + // listMetadataFormats ist part of basic OAI functionality. + $viewHelpers = array_values(array_diff($viewHelpers, ['listMetadataFormats'])); + } $this->viewHelpers = $viewHelpers; } @@ -1185,19 +1192,19 @@ public function setCheckEmbargo($checkEmbargo) */ public function getDocumentTypesAllowed() { - return $this->documentTypesAllowed; + return $this->documentTypesAllowed ?? []; } /** * Sets the allowed document types. * - * @param array|string $documentTypesAllowed + * @param array|string|null $documentTypesAllowed */ public function setDocumentTypesAllowed($documentTypesAllowed) { if (is_string($documentTypesAllowed)) { - $this->documentTypesAllowed = [$documentTypesAllowed]; - } elseif (is_array($documentTypesAllowed)) { + $this->documentTypesAllowed = $documentTypesAllowed !== '' ? [$documentTypesAllowed] : []; + } elseif (is_array($documentTypesAllowed) || $documentTypesAllowed === null) { $this->documentTypesAllowed = $documentTypesAllowed; } } @@ -1209,19 +1216,19 @@ public function setDocumentTypesAllowed($documentTypesAllowed) */ public function getDocumentStatesAllowed() { - return $this->documentStatesAllowed; + return $this->documentStatesAllowed ?? []; } /** * Sets the allowed document states * - * @param array|string $documentStatesAllowed + * @param array|string|null $documentStatesAllowed */ public function setDocumentStatesAllowed($documentStatesAllowed) { if (is_string($documentStatesAllowed)) { - $this->documentStatesAllowed = [$documentStatesAllowed]; - } elseif (is_array($documentStatesAllowed)) { + $this->documentStatesAllowed = $documentStatesAllowed !== '' ? [$documentStatesAllowed] : []; + } elseif (is_array($documentStatesAllowed) || $documentStatesAllowed === null) { $this->documentStatesAllowed = $documentStatesAllowed; } } @@ -1243,7 +1250,7 @@ public function setNotEmbargoedOn($notEmbargoedOn) } /** - * @return string + * @return string|null */ public function getIdentifierExists() { @@ -1251,7 +1258,7 @@ public function getIdentifierExists() } /** - * @param string $identifierExists + * @param string|null $identifierExists */ public function setIdentifierExists($identifierExists) { @@ -1275,7 +1282,7 @@ public function setHasFilesVisibleInOai($hasFilesVisibleInOai) } /** - * @return string + * @return string|null */ public function getPrefixLabel() { @@ -1283,7 +1290,7 @@ public function getPrefixLabel() } /** - * @param string $prefixLabel + * @param string|null $prefixLabel */ public function setPrefixLabel($prefixLabel) { @@ -1291,7 +1298,7 @@ public function setPrefixLabel($prefixLabel) } /** - * @return string + * @return string|null */ public function getSchemaUrl() { @@ -1299,7 +1306,7 @@ public function getSchemaUrl() } /** - * @param string $schemaUrl + * @param string|null $schemaUrl */ public function setSchemaUrl($schemaUrl) { @@ -1307,7 +1314,7 @@ public function setSchemaUrl($schemaUrl) } /** - * @return string + * @return string|null */ public function getMetadataNamespaceUrl() { @@ -1315,7 +1322,7 @@ public function getMetadataNamespaceUrl() } /** - * @param string $metadataNamespaceUrl + * @param string|null $metadataNamespaceUrl */ public function setMetadataNamespaceUrl($metadataNamespaceUrl) { @@ -1331,7 +1338,7 @@ public function isAdminOnly() } /** - * @param mixed $adminOnly + * @param bool|string $adminOnly */ public function setAdminOnly($adminOnly) { @@ -1420,4 +1427,21 @@ public function getFinder($metadataPrefix) return $finder; } + + /** + * Checks if a base url has been set. + * + * @return void + * @throws Zend_Exception + */ + private function checkBaseUrl() + { + $baseUrl = $this->getBaseUrl(); + + if (empty($baseUrl)) { + $message = 'No base url set.'; + Log::get()->err($message); + throw new Exception($message); + } + } } diff --git a/modules/oai/views/helpers/ListMetaDataFormats.php b/modules/oai/views/helpers/ListMetaDataFormats.php index ab4d21726..0e8a2196d 100644 --- a/modules/oai/views/helpers/ListMetaDataFormats.php +++ b/modules/oai/views/helpers/ListMetaDataFormats.php @@ -29,6 +29,7 @@ * @license http://www.gnu.org/licenses/gpl.html General Public License */ +use Opus\Common\Log; use Opus\Common\Security\Realm; /** @@ -60,6 +61,18 @@ public function listMetadataFormats() if ($server->isVisible() && (! $server->isAdminOnly() || Realm::getInstance()->checkModule('admin'))) { if ($prefix) { + if (empty($schemaUrl)) { + $message = 'No schema url set.'; + Log::get()->err($message); + throw new Exception($message); + } + + if (empty($metadataNamespaceUrl)) { + $message = 'No metadata namespace url set.'; + Log::get()->err($message); + throw new Exception($message); + } + $output .= '' . "$prefix" . "$schemaUrl" diff --git a/tests/modules/oai/models/BaseServerTest.php b/tests/modules/oai/models/BaseServerTest.php index daa959a0e..7298b8f1c 100644 --- a/tests/modules/oai/models/BaseServerTest.php +++ b/tests/modules/oai/models/BaseServerTest.php @@ -63,4 +63,46 @@ public function testGetOptionNotInOptionsArray() $baseServer->setOptions(['repositoryIdentifier' => '']); $this->assertEquals('', $baseServer->getRepositoryIdentifier()); } + + public function testSetViewHelpersWithNull() + { + $baseServer = new Oai_Model_DefaultServer(); + $baseServer->setViewHelpers(null); + $this->assertEquals(['listMetadataFormats'], $baseServer->getViewHelpers()); + } + + public function testSetViewHelpersWithEmptyString() + { + $baseServer = new Oai_Model_DefaultServer(); + $baseServer->setViewHelpers(''); + $this->assertEquals(['listMetadataFormats'], $baseServer->getViewHelpers()); + } + + public function testSetDocumentStatesAllowedWithNull() + { + $baseServer = new Oai_Model_DefaultServer(); + $baseServer->setDocumentStatesAllowed(null); + $this->assertEquals([], $baseServer->getDocumentStatesAllowed()); + } + + public function testSetDocumentStatesAllowedWithEmptyString() + { + $baseServer = new Oai_Model_DefaultServer(); + $baseServer->setDocumentStatesAllowed(''); + $this->assertEquals([], $baseServer->getDocumentStatesAllowed()); + } + + public function testSetDocumentTypesAllowedWithNull() + { + $baseServer = new Oai_Model_DefaultServer(); + $baseServer->setDocumentTypesAllowed(null); + $this->assertEquals([], $baseServer->getDocumentTypesAllowed()); + } + + public function testSetDocumentTypesAllowedWithEmptyString() + { + $baseServer = new Oai_Model_DefaultServer(); + $baseServer->setDocumentTypesAllowed(''); + $this->assertEquals([], $baseServer->getDocumentTypesAllowed()); + } }