From 5b05696c7d60bc8541694785b22544563c9a41c6 Mon Sep 17 00:00:00 2001 From: Josef Moravec <josef.moravec@gmail.com> Date: Tue, 11 Feb 2020 13:59:04 +0100 Subject: [PATCH] Use url, if no pdf url found in Unpaywall DOI linker (#1547) - Adds different labels for PDF and non-PDF full text in unpaywall doi linker - Adjusts and enhances unpaywall tests --- .../VuFind/src/VuFind/DoiLinker/Unpaywall.php | 5 ++ .../tests/fixtures/unpaywall/badresponse | 16 ++++ .../fixtures/unpaywall/goodresponseonline | 80 +++++++++++++++++++ .../{goodresponse => goodresponsepdf} | 0 .../VuFindTest/DoiLinker/UnpaywallTest.php | 67 +++++++++++----- 5 files changed, 149 insertions(+), 19 deletions(-) create mode 100644 module/VuFind/tests/fixtures/unpaywall/badresponse create mode 100644 module/VuFind/tests/fixtures/unpaywall/goodresponseonline rename module/VuFind/tests/fixtures/unpaywall/{goodresponse => goodresponsepdf} (100%) diff --git a/module/VuFind/src/VuFind/DoiLinker/Unpaywall.php b/module/VuFind/src/VuFind/DoiLinker/Unpaywall.php index 8e0e53cb720..f1982b0a776 100644 --- a/module/VuFind/src/VuFind/DoiLinker/Unpaywall.php +++ b/module/VuFind/src/VuFind/DoiLinker/Unpaywall.php @@ -101,6 +101,11 @@ class Unpaywall implements DoiLinkerInterface, TranslatorAwareInterface, 'link' => $data['best_oa_location']['url_for_pdf'], 'label' => $this->translate('PDF Full Text'), ]; + } elseif (!empty($data['best_oa_location']['url'])) { + $response[$doi][] = [ + 'link' => $data['best_oa_location']['url'], + 'label' => $this->translate('online_resources'), + ]; } } return $response; diff --git a/module/VuFind/tests/fixtures/unpaywall/badresponse b/module/VuFind/tests/fixtures/unpaywall/badresponse new file mode 100644 index 00000000000..73a86236938 --- /dev/null +++ b/module/VuFind/tests/fixtures/unpaywall/badresponse @@ -0,0 +1,16 @@ +HTTP/1.1 404 NOT FOUND +Connection: keep-alive +Server: gunicorn/19.9.0 +Date: Tue, 11 Feb 2020 11:38:17 GMT +Content-Type: application/json +Content-Length: 157 +Access-Control-Allow-Origin: * +Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE, PATCH +Access-Control-Allow-Headers: origin, content-type, accept, x-requested-with +Via: 1.1 vegur + +{ + "HTTP_status_code": 404, + "error": true, + "message": "'10.1032228/nature12373' is an invalid doi. See https://doi.org/10.1032228/nature12373" +} diff --git a/module/VuFind/tests/fixtures/unpaywall/goodresponseonline b/module/VuFind/tests/fixtures/unpaywall/goodresponseonline new file mode 100644 index 00000000000..fa1ec4b771e --- /dev/null +++ b/module/VuFind/tests/fixtures/unpaywall/goodresponseonline @@ -0,0 +1,80 @@ +HTTP/1.1 200 OK +Connection: keep-alive +Server: gunicorn/19.9.0 +Date: Wed, 06 Nov 2019 20:38:59 GMT +Content-Type: application/json +Content-Length: 2336 +Access-Control-Allow-Origin: * +Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE, PATCH +Access-Control-Allow-Headers: origin, content-type, accept, x-requested-with +Via: 1.1 vegur + +{ + "best_oa_location": { + "endpoint_id": null, + "evidence": "open (via page says license)", + "host_type": "publisher", + "is_best": true, + "license": "cc-by-sa", + "pmh_id": null, + "repository_institution": null, + "updated": "2018-07-24T23:01:04.482481", + "url": "https://doi.org/10.7553/66-4-1434", + "url_for_landing_page": "https://doi.org/10.7553/66-4-1434", + "url_for_pdf": null, + "version": "publishedVersion" + }, + "data_standard": 2, + "doi": "10.7553/66-4-1434", + "doi_url": "https://doi.org/10.7553/66-4-1434", + "genre": "journal-article", + "has_repository_copy": false, + "is_oa": true, + "journal_is_in_doaj": true, + "journal_is_oa": true, + "journal_issn_l": "0256-8861", + "journal_issns": "2304-8263", + "journal_name": "South African Journal of Libraries and Information Science", + "oa_locations": [ + { + "endpoint_id": null, + "evidence": "open (via page says license)", + "host_type": "publisher", + "is_best": true, + "license": "cc-by-sa", + "pmh_id": null, + "repository_institution": null, + "updated": "2018-07-24T23:01:04.482481", + "url": "http://sajlis.journals.ac.za/pub/article/download/1434/1332", + "url_for_landing_page": "https://doi.org/10.7553/66-4-1434", + "url_for_pdf": "http://sajlis.journals.ac.za/pub/article/download/1434/1332", + "version": "publishedVersion" + }, + { + "endpoint_id": null, + "evidence": "oa journal (via doaj)", + "host_type": "publisher", + "is_best": false, + "license": "cc-by-sa", + "pmh_id": null, + "repository_institution": null, + "updated": "2019-11-06T20:38:59.834062", + "url": "https://doi.org/10.7553/66-4-1434", + "url_for_landing_page": "https://doi.org/10.7553/66-4-1434", + "url_for_pdf": null, + "version": "publishedVersion" + } + ], + "oa_status": "gold", + "published_date": "2014-01-26", + "publisher": "Stellenbosch University", + "title": "Pioneers, passionate ladies, and private eyes : dime novels, series books, and paperbacks Sullivan, Larry E. & Cushman Schurman, Lydia eds.", + "updated": "2018-07-24T23:02:00.263680", + "year": 2014, + "z_authors": [ + { + "family": "Sandham", + "given": "Tim" + } + ] +} diff --git a/module/VuFind/tests/fixtures/unpaywall/goodresponse b/module/VuFind/tests/fixtures/unpaywall/goodresponsepdf similarity index 100% rename from module/VuFind/tests/fixtures/unpaywall/goodresponse rename to module/VuFind/tests/fixtures/unpaywall/goodresponsepdf diff --git a/module/VuFind/tests/unit-tests/src/VuFindTest/DoiLinker/UnpaywallTest.php b/module/VuFind/tests/unit-tests/src/VuFindTest/DoiLinker/UnpaywallTest.php index f76d4f161ee..f04a453e305 100644 --- a/module/VuFind/tests/unit-tests/src/VuFindTest/DoiLinker/UnpaywallTest.php +++ b/module/VuFind/tests/unit-tests/src/VuFindTest/DoiLinker/UnpaywallTest.php @@ -63,28 +63,57 @@ class UnpaywallTest extends \VuFindTest\Unit\TestCase public function testApiSuccess() { $adapter = new TestAdapter(); - $file = realpath( - __DIR__ . '/../../../../../tests/fixtures/unpaywall/goodresponse' - ); - $response = file_get_contents($file); - $responseObj = HttpResponse::fromString($response); - $adapter->setResponse($responseObj); - $service = new \VuFindHttp\HttpService(); - $service->setDefaultAdapter($adapter); + $testData = [ + [ + 'filename' => realpath(__DIR__ + . '/../../../../../tests/fixtures/unpaywall/goodresponsepdf' + ), + 'response' => [ + '10.7553/66-4-1434' => [ + [ + 'link' => 'http://sajlis.journals.ac.za/pub/article/download/1434/1332', + 'label' => 'PDF Full Text', + ] + ] + ] + ], + [ + 'filename' => realpath(__DIR__ + . '/../../../../../tests/fixtures/unpaywall/goodresponseonline' + ), + 'response' => [ + '10.7553/66-4-1434' => [ + [ + 'link' => 'https://doi.org/10.7553/66-4-1434', + 'label' => 'online_resources', + ] + ] + ] + ], + [ + 'filename' => realpath(__DIR__ + . '/../../../../../tests/fixtures/unpaywall/badresponse' + ), + 'response' => [] + ], + ]; + $config = [ 'unpaywall_email' => 'foo@myuniversity.edu', ]; $unpaywall = new Unpaywall(new \Zend\Config\Config($config)); - $unpaywall->setHttpService($service); - $this->assertEquals( - ['10.7553/66-4-1434' => [ - [ - 'link' => 'http://sajlis.journals.ac.za/pub/article/download/1434/1332', - 'label' => 'PDF Full Text', - ] - ] - ], - $unpaywall->getLinks(['10.7553/66-4-1434']) - ); + + foreach ($testData as $data) { + $response = file_get_contents($data['filename']); + $responseObj = HttpResponse::fromString($response); + $adapter->setResponse($responseObj); + $service = new \VuFindHttp\HttpService(); + $service->setDefaultAdapter($adapter); + $unpaywall->setHttpService($service); + $this->assertEquals( + $data['response'], + $unpaywall->getLinks(['10.7553/66-4-1434']) + ); + } } } -- GitLab