Skip to content

Commit

Permalink
BrowZine covers: skip over default images. (#4226)
Browse files Browse the repository at this point in the history
  • Loading branch information
demiankatz authored Feb 12, 2025
1 parent eb27517 commit cea1bdf
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 12 deletions.
8 changes: 8 additions & 0 deletions config/vufind/BrowZine.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
18 changes: 7 additions & 11 deletions module/VuFind/src/VuFind/Content/Covers/BrowZine.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
use VuFindSearch\Backend\BrowZine\Command\LookupIssnsCommand;
use VuFindSearch\Service;

use function in_array;

/**
* BrowZine cover content loader.
*
Expand All @@ -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;
}

Expand All @@ -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;
}
}
5 changes: 4 additions & 1 deletion module/VuFind/src/VuFind/Content/Covers/BrowZineFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
1 change: 1 addition & 0 deletions module/VuFind/tests/fixtures/browzine/cover-default.json
Original file line number Diff line number Diff line change
@@ -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"}]}
Original file line number Diff line number Diff line change
@@ -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"}]}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

/**
* Unit tests for BrowZine cover loader.
*
* PHP version 8
*
* Copyright (C) Villanova University 2025.
*
* 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 <[email protected]>
* @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 <[email protected]>
* @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));
}
}

0 comments on commit cea1bdf

Please sign in to comment.