diff --git a/src/enums/MDNodaRepository.php b/src/enums/MDNodaRepository.php index a9476b9..5f84ebc 100644 --- a/src/enums/MDNodaRepository.php +++ b/src/enums/MDNodaRepository.php @@ -117,8 +117,9 @@ enum MDNodaRepository implements MDValueEnumInterface, JsonSerializable { } /** - * Attempts to get a repository based on a provided link. This function is rather expensive - * and should be avoided as much as possible. + * Attempts to get a repository based on a provided link. + * This function is rather expensive and should be avoided + * as much as possible. * * @param string $input Input to get a value from. * @@ -130,7 +131,12 @@ enum MDNodaRepository implements MDValueEnumInterface, JsonSerializable { foreach ($cases as $case) { if ($case === self::allgemein) continue; - $output = $case->validateId($input); + try { + $output = $case->validateId($input); + } + catch (MDInvalidNodaLinkException $e) { + continue; + } if ($output !== false) return $case; } @@ -396,8 +402,11 @@ enum MDNodaRepository implements MDValueEnumInterface, JsonSerializable { if (filter_var($id, FILTER_VALIDATE_URL) !== false) { $id = strtr($id, ['http://iconclass.org/rkd/' => '', 'http://iconclass.org/' => '', 'https://iconclass.org/' => '']); } + $id = trim($id, "/"); - if (preg_match("/^[0-9a-z\/]*$/", $id) === false) return false; + if (!empty(trim($id, '0123456789abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ'))) { + return false; + } return $id; @@ -647,7 +656,7 @@ enum MDNodaRepository implements MDValueEnumInterface, JsonSerializable { return false; } - return $id; + return 'mp' . $id; } @@ -752,7 +761,7 @@ enum MDNodaRepository implements MDValueEnumInterface, JsonSerializable { ]), self::ndp_ikmk => self::validateNdpIkmkForPlaces($id), self::ndp_ikmk_persons => self::validateNumericId($id, ['https://ikmk.smb.museum/ndp/person/']), - self::nomisma => str_replace('http://nomisma.org/id/', '', $id), + self::nomisma => self::validateNumericId($id, ['http://nomisma.org/id/']), self::npg => self::validateNpgId($id), self::oberbegriffsdatei => self::validateNumericId($id, ['https://term.museum-digital.de/oberbegriffsdatei/tag/']), self::orcid => self::validateOrcidId($id), diff --git a/tests/MDNodaRepositoryTest.php b/tests/MDNodaRepositoryTest.php index 989fa8f..ecfb8e1 100644 --- a/tests/MDNodaRepositoryTest.php +++ b/tests/MDNodaRepositoryTest.php @@ -96,7 +96,8 @@ final class MDNodaRepositoryTest extends TestCase { yield "4114170-2, trailing content in brackets" => ["4114170-2", "4114170-2 (Fenster (Motiv)"]; yield "4114170-2, trailing content in brackets, with newline" => ["4114170-2", "4114170-2" . PHP_EOL . " (Fenster (Motiv)"]; yield "4113617-2, with newline" => ["4113617-2", "4113617-2" . PHP_EOL . "Zauberin (Motiv)"]; - yield "Broken ID" => [false, "fkld;s102423008"]; + yield "Broken ID; fkld;s102423008" => [false, "fkld;s102423008"]; + yield "Broken ID; s102423008-x" => [false, "s102423008-x"]; } @@ -139,6 +140,7 @@ final class MDNodaRepositoryTest extends TestCase { yield "Full URL, http / mp01751" => ["mp01751", "http://www.npg.org.uk/collections/search/person/mp01751"]; yield "Full URL, https / mp01751" => ["mp01751", "https://www.npg.org.uk/collections/search/person/mp01751"]; yield "ID / mp01751" => ["mp01751", "mp01751"]; + yield "incomplete ID without prefix / 01751 > mp01751" => ["mp01751", "01751"]; yield "Broken input / https://www.npg.org.uk/collections/search/person/mp017;51" => [false, "https://www.npg.org.uk/collections/search/person/mp017;51"]; } @@ -228,6 +230,7 @@ final class MDNodaRepositoryTest extends TestCase { yield "Full URL, https / record > auth/PIM71684" => ["PIM71684", "https://resolver.pim.hu/auth/PIM71684"]; yield "ID / PIM71684" => ["PIM71684", "PIM71684"]; yield "Broken input / PIM7168;;4" => [false, "PIM7168;;4"]; + yield "Broken input / 7168a" => [false, "7168a"]; } @@ -291,6 +294,8 @@ final class MDNodaRepositoryTest extends TestCase { yield "region/123 ; url" => ["region/123", "https://ikmk.smb.museum/ndp/region/123"]; yield "land/123 ; ID" => ["land/123", "land/123"]; yield "Broken input / 123123" => [false, "123123"]; + yield "Broken input / a/123123" => [false, "a/123123"]; + yield "Broken input / land/123123-a" => [false, "land/123123-a"]; } @@ -365,4 +370,83 @@ final class MDNodaRepositoryTest extends TestCase { self::assertEquals($expected, MDNodaRepository::mindatorg->validateId($inputString)); } + + /** + * Data provider for validating references to orcid. + * + * @return Generator + */ + public static function orcidIdForValidationProvider():Generator { + + yield "0000-0000-0000-0000 ; url, http" => ["0000-0000-0000-0000", "http://orcid.org/0000-0000-0000-0000"]; + yield "0000-0000-0000-0000 ; url, https" => ["0000-0000-0000-0000", "https://orcid.org/0000-0000-0000-0000"]; + yield "0000-0000-0000-0000 ; ID" => ["0000-0000-0000-0000", "0000-0000-0000-0000"]; + yield "Broken input / adfdasjfklasjl" => [false, "adfdasjfklasjl"]; + + } + + /** + * Ensure validating orcid references. + * + * @param string|false $expected Expected output. + * @param string $inputString Input string. + * + * @return void + */ + #[DataProvider('orcidIdForValidationProvider')] + public function testOrcidIdValidation(string|false $expected, string $inputString):void { + + self::assertEquals($expected, MDNodaRepository::orcid->validateId($inputString)); + + } + + /** + * Data provider for validating references to iconclass. + * + * @return Generator + */ + public static function iconclassIdForValidationProvider():Generator { + + yield "45C221 ; url, http" => ["45C221", "http://iconclass.org/rkd/45C221"]; + yield "45C221 ; url, https" => ["45C221", "http://iconclass.org/rkd//45C221"]; + yield "45C221 ; ID" => ["45C221", "45C221"]; + yield "Broken input / adfdasjfklasäj%l" => [false, "adfdasjfklasäj%l"]; + + } + + /** + * Ensure validating iconclass references. + * + * @param string|false $expected Expected output. + * @param string $inputString Input string. + * + * @return void + */ + #[DataProvider('iconclassIdForValidationProvider')] + public function testIconclassIdValidation(string|false $expected, string $inputString):void { + + self::assertEquals($expected, MDNodaRepository::iconclass->validateId($inputString)); + + } + + /** + * Tests conversion of already validated and set up entries to strings. + * + * @return void + */ + public function testConversionWorks():void { + + $entry = MDNodaRepository::fromUrl("http://www.wikidata.org/w/index.php?title=Q834961&oldid=2256125706"); + + self::assertEquals("wikidata", $entry->toDbName()); + self::assertEquals("Wikidata", $entry->toFullName()); + self::assertEquals("https://www.wikidata.org/entity/", $entry->getUrlPrefix()); + self::assertEquals("https://www.wikidata.org/entity/a", $entry->getEntityLink("a")); + self::assertEquals("https://www.wikidata.org/w/index.php?search=&search=a&ns0=1&ns120=1", $entry->getSearchLink("a")); + + self::assertEquals('"wikidata"', json_encode($entry)); + + self::assertNotEmpty(MDNodaRepository::caseNames()); + + } }