From cea1bdf6632e686e9d83ea4330c51a5637658ad9 Mon Sep 17 00:00:00 2001 From: Demian Katz Date: Wed, 12 Feb 2025 12:16:44 -0500 Subject: [PATCH] BrowZine covers: skip over default images. (#4226) --- config/vufind/BrowZine.ini | 8 ++ .../src/VuFind/Content/Covers/BrowZine.php | 18 ++-- .../VuFind/Content/Covers/BrowZineFactory.php | 5 +- .../fixtures/browzine/cover-default.json | 1 + .../fixtures/browzine/cover-non-default.json | 1 + .../Content/Covers/BrowZineTest.php | 97 +++++++++++++++++++ 6 files changed, 118 insertions(+), 12 deletions(-) create mode 100644 module/VuFind/tests/fixtures/browzine/cover-default.json create mode 100644 module/VuFind/tests/fixtures/browzine/cover-non-default.json create mode 100644 module/VuFind/tests/unit-tests/src/VuFindTest/Content/Covers/BrowZineTest.php diff --git a/config/vufind/BrowZine.ini b/config/vufind/BrowZine.ini index dce09baa526..f37e72dd452 100644 --- a/config/vufind/BrowZine.ini +++ b/config/vufind/BrowZine.ini @@ -30,6 +30,14 @@ load_results_with_js = true ; full Full pagination alike to the one at the bottom of results ;top_paginator = simple +; This section contains settings related to the BrowZine cover image loader. +[Covers] +; Any URLs in this list will be ignored when using the BrowZine cover image; this +; allows "no match" images to be skipped so that other cover providers can fill in +; BrowZine's gaps. If this setting is omitted, the default value below will be used. +; If you want to disable skipping of generic images, set the value to an empty string. +ignored_images[] = "https://assets.thirdiron.com/default-journal-cover.png" + ; This section controls the behavior of the BrowZine DOI handler; see also ; the [DOI] section of config.ini to activate the handler. [DOI] diff --git a/module/VuFind/src/VuFind/Content/Covers/BrowZine.php b/module/VuFind/src/VuFind/Content/Covers/BrowZine.php index 5996d285b83..17fc66a0fe5 100644 --- a/module/VuFind/src/VuFind/Content/Covers/BrowZine.php +++ b/module/VuFind/src/VuFind/Content/Covers/BrowZine.php @@ -32,6 +32,8 @@ use VuFindSearch\Backend\BrowZine\Command\LookupIssnsCommand; use VuFindSearch\Service; +use function in_array; + /** * BrowZine cover content loader. * @@ -43,21 +45,14 @@ */ class BrowZine extends \VuFind\Content\AbstractCover { - /** - * Search service - * - * @var Service - */ - protected $searchService; - /** * Constructor * - * @param Service $searchService Search service + * @param Service $searchService Search service + * @param string[] $ignoreList Cover image URLs to ignore (we don't want to display third-party generic images) */ - public function __construct(Service $searchService) + public function __construct(protected Service $searchService, protected array $ignoreList) { - $this->searchService = $searchService; $this->supportsIssn = true; } @@ -82,6 +77,7 @@ public function getUrl($key, $size, $ids) $command = new LookupIssnsCommand('BrowZine', $ids['issn']); $result = $this->searchService->invoke($command)->getResult(); - return $result['data'][0]['coverImageUrl'] ?? false; + $url = $result['data'][0]['coverImageUrl'] ?? false; + return ($url && in_array($url, $this->ignoreList)) ? false : $url; } } diff --git a/module/VuFind/src/VuFind/Content/Covers/BrowZineFactory.php b/module/VuFind/src/VuFind/Content/Covers/BrowZineFactory.php index 12fa04988eb..8e38a8416d8 100644 --- a/module/VuFind/src/VuFind/Content/Covers/BrowZineFactory.php +++ b/module/VuFind/src/VuFind/Content/Covers/BrowZineFactory.php @@ -69,6 +69,9 @@ public function __invoke( if (!empty($options)) { throw new \Exception('Unexpected options passed to factory.'); } - return new $requestedName($container->get(\VuFindSearch\Service::class)); + $config = $container->get(\VuFind\Config\PluginManager::class)->get('BrowZine')?->toArray() ?? []; + $defaultIgnoreList = ['https://assets.thirdiron.com/default-journal-cover.png']; + $ignoreList = $config['Covers']['ignored_images'] ?? $defaultIgnoreList; + return new $requestedName($container->get(\VuFindSearch\Service::class), $ignoreList); } } diff --git a/module/VuFind/tests/fixtures/browzine/cover-default.json b/module/VuFind/tests/fixtures/browzine/cover-default.json new file mode 100644 index 00000000000..095b8a3384d --- /dev/null +++ b/module/VuFind/tests/fixtures/browzine/cover-default.json @@ -0,0 +1 @@ +{"data":[{"id":1,"type":"journals","title":"Default Cover Journal","issn":"12345678","sjrValue":0,"coverImageUrl":"https:\/\/assets.thirdiron.com\/default-journal-cover.png","browzineEnabled":false,"externalLink":"https:\/\/fake-external-link.com"}]} \ No newline at end of file diff --git a/module/VuFind/tests/fixtures/browzine/cover-non-default.json b/module/VuFind/tests/fixtures/browzine/cover-non-default.json new file mode 100644 index 00000000000..dbcf70ae02d --- /dev/null +++ b/module/VuFind/tests/fixtures/browzine/cover-non-default.json @@ -0,0 +1 @@ +{"data":[{"id":1,"type":"journals","title":"Non-default Cover Journal","issn":"12345678","sjrValue":0,"coverImageUrl":"https:\/\/assets.thirdiron.com\/simulated-real-cover.png","browzineEnabled":false,"externalLink":"https:\/\/fake-external-link.com"}]} \ No newline at end of file diff --git a/module/VuFind/tests/unit-tests/src/VuFindTest/Content/Covers/BrowZineTest.php b/module/VuFind/tests/unit-tests/src/VuFindTest/Content/Covers/BrowZineTest.php new file mode 100644 index 00000000000..8405d382f93 --- /dev/null +++ b/module/VuFind/tests/unit-tests/src/VuFindTest/Content/Covers/BrowZineTest.php @@ -0,0 +1,97 @@ + + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link https://vufind.org + */ + +namespace VuFindTest\Content\Covers; + +use VuFind\Content\Covers\BrowZine; +use VuFind\Content\Covers\BrowZineFactory; +use VuFindTest\Container\MockContainer; + +/** + * Unit tests for BrowZine cover loader. + * + * @category VuFind + * @package Tests + * @author Demian Katz + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link https://vufind.org + */ +class BrowZineTest extends \PHPUnit\Framework\TestCase +{ + use \VuFindTest\Feature\FixtureTrait; + + /** + * Data provider for testCoverLoading. + * + * @return array[] + */ + public static function coverProvider(): array + { + return [ + 'no issn' => [[], null, false], + 'default cover' => [['issn' => '12345678'], 'browzine/cover-default.json', false], + 'non-default cover' => [ + ['issn' => '12345678'], + 'browzine/cover-non-default.json', + 'https://assets.thirdiron.com/simulated-real-cover.png', + ], + ]; + } + + /** + * Test cover loading + * + * @param array $ids Array of IDs to look up + * @param ?string $fixture Fixture to return from backend (null to assume backend will not be called) + * @param string|bool $expected Expected cover URL + * + * @return void + * + * @dataProvider coverProvider + */ + public function testCoverLoading(array $ids, ?string $fixture, string|bool $expected): void + { + $service = $this->createMock(\VuFindSearch\Service::class); + if ($fixture) { + $service->method('invoke')->willReturnCallback( + function ($command) use ($fixture, $ids) { + $this->assertEquals([$ids['issn']], $command->getArguments()); + $fakeCommand = $this->createMock($command::class); + $fakeCommand->method('getResult')->willReturn($this->getJsonFixture($fixture)); + return $fakeCommand; + } + ); + } + $factory = new BrowZineFactory(); + $container = new MockContainer($this); + $container->set(\VuFindSearch\Service::class, $service); + $loader = ($factory)($container, BrowZine::class); + $this->assertEquals($expected, $loader->getUrl('', 'small', $ids)); + } +}