diff --git a/config/vufind/BrowZine.ini b/config/vufind/BrowZine.ini index f0c213374e386dc87e6c1b2f2f2d79c336a8a983..50aa879520191e8af74bb2c909c8debd1e071a59 100644 --- a/config/vufind/BrowZine.ini +++ b/config/vufind/BrowZine.ini @@ -15,6 +15,18 @@ timeout = 30 ; should be one of the options present in the [Views] section below. default_view = list +; This section controls the behavior of the BrowZine DOI handler; see also +; the [DOI] section of config.ini to activate the handler. +[DOI] +; Can be set to "include" to include only the options listed in the "filter" +; setting, or to "exclude" to filter out the options listed in the "filter" +; setting. The default is "none," which performs no filtering. +;filterType = exclude +; This repeatable section can be used to filter based on link type; legal +; options are "browzineWebLink" and "fullTextFile" +;filter[] = "browzineWebLink" +;filter[] = "fullTextFile" + ; This section defines the view options available on standard search results. ; If only one view is required, set default_view under [General] above, and ; leave this section commented out. diff --git a/module/VuFind/src/VuFind/DoiLinker/BrowZine.php b/module/VuFind/src/VuFind/DoiLinker/BrowZine.php index 1366466a2f68312c31deb9ecdc806ecefca01896..5fa9de39bbbc5928b98d91b76518d8f85b30f63b 100644 --- a/module/VuFind/src/VuFind/DoiLinker/BrowZine.php +++ b/module/VuFind/src/VuFind/DoiLinker/BrowZine.php @@ -50,14 +50,47 @@ class BrowZine implements DoiLinkerInterface, TranslatorAwareInterface */ protected $connector; + /** + * Configuration options + * + * @var array + */ + protected $config; + /** * Constructor * * @param Connector $connector Connector + * @param array $config Configuration settings */ - public function __construct(Connector $connector) + public function __construct(Connector $connector, array $config = []) { $this->connector = $connector; + $this->config = $config; + } + + /** + * Check if an array key is available in the data and allowed by filter settings. + * + * @param string $key Key to check + * @param array $data Available data + * + * @return bool + */ + protected function arrayKeyAvailable(string $key, array $data): bool + { + if (empty($data[$key])) { + return false; + } + switch (strtolower(trim($this->config['filterType'] ?? 'none'))) { + case 'include': + return in_array($key, (array)($this->config['filter'] ?? [])); + case 'exclude': + return !in_array($key, (array)($this->config['filter'] ?? [])); + default: + } + // If we got this far, no filter setting is applied, so the option is legal: + return true; } /** @@ -76,7 +109,7 @@ class BrowZine implements DoiLinkerInterface, TranslatorAwareInterface $response = []; foreach ($doiArray as $doi) { $data = $this->connector->lookupDoi($doi)['data'] ?? null; - if (!empty($data['browzineWebLink'])) { + if ($this->arrayKeyAvailable('browzineWebLink', $data)) { $response[$doi][] = [ 'link' => $data['browzineWebLink'], 'label' => $this->translate('View Complete Issue'), @@ -84,7 +117,7 @@ class BrowZine implements DoiLinkerInterface, TranslatorAwareInterface 'data' => $data, ]; } - if (!empty($data['fullTextFile'])) { + if ($this->arrayKeyAvailable('fullTextFile', $data)) { $response[$doi][] = [ 'link' => $data['fullTextFile'], 'label' => $this->translate('PDF Full Text'), diff --git a/module/VuFind/src/VuFind/DoiLinker/BrowZineFactory.php b/module/VuFind/src/VuFind/DoiLinker/BrowZineFactory.php index 7ad88eac62333a33e97afe3277c5b7224ffad624..86ddc76058bbf8b3acd0d5415e3a9375434dc9bb 100644 --- a/module/VuFind/src/VuFind/DoiLinker/BrowZineFactory.php +++ b/module/VuFind/src/VuFind/DoiLinker/BrowZineFactory.php @@ -64,6 +64,9 @@ class BrowZineFactory implements \Laminas\ServiceManager\Factory\FactoryInterfac } $backend = $container->get(\VuFind\Search\BackendManager::class) ->get('BrowZine'); - return new $requestedName($backend->getConnector()); + $fullConfig = $container->get(\VuFind\Config\PluginManager::class) + ->get('BrowZine'); + $config = isset($fullConfig->DOI) ? $fullConfig->DOI->toArray() : []; + return new $requestedName($backend->getConnector(), $config); } } diff --git a/module/VuFind/tests/fixtures/browzine/doi.json b/module/VuFind/tests/fixtures/browzine/doi.json new file mode 100644 index 0000000000000000000000000000000000000000..d0c255590eca5f888ca458a199c73070c5edb631 --- /dev/null +++ b/module/VuFind/tests/fixtures/browzine/doi.json @@ -0,0 +1 @@ +{"data":{"id":377796791,"type":"articles","title":"Comparison of Functional Movement Screen, Star Excursion Balance Test, and Physical Fitness in Junior Athletes with Different Sports Injury Risk","date":"2020-03-26","authors":"Chang, Wen-Dien; Chou, Li-Wei; Chang, Nai-Jen; Chen, Shuya","inPress":false,"doi":"10.1155/2020/8690540","ILLURL":"https://ill","pmid":"32309441","fullTextFile":"https://fulltext","contentLocation":"https://content","availableThroughBrowzine":true,"startPage":"1","endPage":"8","browzineWebLink":"https://weblink"}} \ No newline at end of file diff --git a/module/VuFind/tests/unit-tests/src/VuFindTest/DoiLinker/BrowZineTest.php b/module/VuFind/tests/unit-tests/src/VuFindTest/DoiLinker/BrowZineTest.php new file mode 100644 index 0000000000000000000000000000000000000000..acdee46d1f0573d608414c6009700cbdf1653715 --- /dev/null +++ b/module/VuFind/tests/unit-tests/src/VuFindTest/DoiLinker/BrowZineTest.php @@ -0,0 +1,134 @@ +<?php +/** + * BrowZine Test Class + * + * PHP version 7 + * + * Copyright (C) Villanova University 2020. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * @category VuFind + * @package Tests + * @author Demian Katz <demian.katz@villanova.edu> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link https://vufind.org/wiki/development:testing:unit_tests Wiki + */ +namespace VuFindTest\DoiLinker; + +use VuFind\DoiLinker\BrowZine; +use VuFindSearch\Backend\BrowZine\Connector; + +/** + * BrowZine Test Class + * + * @category VuFind + * @package Tests + * @author Demian Katz <demian.katz@villanova.edu> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link https://vufind.org/wiki/development:testing:unit_tests Wiki + */ +class BrowZineTest extends \VuFindTest\Unit\TestCase +{ + /** + * Get a mock connector + * + * @param string $doi DOI expected by connector + * @param array $response Response for connector to return + * + * @return void + */ + protected function getMockConnector($doi, $response) + { + $connector = $this->getMockBuilder(Connector::class) + ->disableOriginalConstructor() + ->getMock(); + $connector->expects($this->once()) + ->method('lookupDoi') + ->with($this->equalTo($doi)) + ->will($this->returnValue($response)); + return $connector; + } + + /** + * Test an API response. + * + * @return void + */ + public function testApiSuccess() + { + $fixture = realpath( + __DIR__ + . '/../../../../../tests/fixtures/browzine/doi.json' + ); + $rawData = json_decode(file_get_contents($fixture), true); + $testData = [ + [ + 'config' => [], + 'response' => [ + '10.1155/2020/8690540' => [ + [ + 'link' => 'https://weblink', + 'label' => 'View Complete Issue', + 'icon' => 'https://assets.thirdiron.com/images/integrations/browzine-open-book-icon.svg', + 'data' => $rawData['data'], + ], + [ + 'link' => 'https://fulltext', + 'label' => 'PDF Full Text', + 'icon' => 'https://assets.thirdiron.com/images/integrations/browzine-pdf-download-icon.svg', + 'data' => $rawData['data'], + ] + ] + ] + ], + [ + 'config' => ['filterType' => 'exclude', 'filter' => ['browzineWebLink']], + 'response' => [ + '10.1155/2020/8690540' => [ + [ + 'link' => 'https://fulltext', + 'label' => 'PDF Full Text', + 'icon' => 'https://assets.thirdiron.com/images/integrations/browzine-pdf-download-icon.svg', + 'data' => $rawData['data'], + ] + ] + ] + ], + [ + 'config' => ['filterType' => 'include', 'filter' => ['browzineWebLink']], + 'response' => [ + '10.1155/2020/8690540' => [ + [ + 'link' => 'https://weblink', + 'label' => 'View Complete Issue', + 'icon' => 'https://assets.thirdiron.com/images/integrations/browzine-open-book-icon.svg', + 'data' => $rawData['data'], + ] + ] + ] + ], + ]; + + foreach ($testData as $data) { + $dois = array_keys($data['response']); + $connector = $this->getMockConnector($dois[0], $rawData); + $browzine = new BrowZine($connector, $data['config']); + $this->assertEquals( + $data['response'], + $browzine->getLinks($dois) + ); + } + } +}