From fee488745d5a6ed2eb59686350260fe28f9f1c01 Mon Sep 17 00:00:00 2001 From: Demian Katz <demian.katz@villanova.edu> Date: Wed, 15 Jan 2020 13:04:41 -0500 Subject: [PATCH] Add test and fix bug. --- .../MetadataVocabulary/AbstractBase.php | 6 +- .../View/Helper/Root/MetadataTest.php | 111 ++++++++++++++++++ 2 files changed, 114 insertions(+), 3 deletions(-) create mode 100644 module/VuFind/tests/unit-tests/src/VuFindTest/View/Helper/Root/MetadataTest.php diff --git a/module/VuFind/src/VuFind/MetadataVocabulary/AbstractBase.php b/module/VuFind/src/VuFind/MetadataVocabulary/AbstractBase.php index 796039a346b..71b43487593 100644 --- a/module/VuFind/src/VuFind/MetadataVocabulary/AbstractBase.php +++ b/module/VuFind/src/VuFind/MetadataVocabulary/AbstractBase.php @@ -60,9 +60,9 @@ abstract class AbstractBase implements MetadataVocabularyInterface return [ 'author' => array_unique( array_merge( - $driver->tryMethod('getPrimaryAuthors'), - $driver->tryMethod('getSecondaryAuthors'), - $driver->tryMethod('getCorporateAuthors') + $driver->tryMethod('getPrimaryAuthors') ?? [], + $driver->tryMethod('getSecondaryAuthors') ?? [], + $driver->tryMethod('getCorporateAuthors') ?? [] ) ), 'container_title' => $driver->tryMethod('getContainerTitle'), diff --git a/module/VuFind/tests/unit-tests/src/VuFindTest/View/Helper/Root/MetadataTest.php b/module/VuFind/tests/unit-tests/src/VuFindTest/View/Helper/Root/MetadataTest.php new file mode 100644 index 00000000000..62b714f89c6 --- /dev/null +++ b/module/VuFind/tests/unit-tests/src/VuFindTest/View/Helper/Root/MetadataTest.php @@ -0,0 +1,111 @@ +<?php +/** + * Metadata 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\View\Helper\Root; + +use VuFind\MetadataVocabulary\PluginManager; +use VuFind\MetadataVocabulary\PRISM; +use VuFind\View\Helper\Root\Metadata; +use VuFindTest\RecordDriver\TestHarness; +use Zend\Config\Config; +use Zend\View\Helper\HeadMeta; + +/** + * Metadata 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 MetadataTest extends \VuFindTest\Unit\TestCase +{ + /** + * Get a fake record driver + * + * @param array $data Test data + * + * @return TestHarness + */ + protected function getDriver($data) + { + $driver = new TestHarness(); + $driver->setRawData($data); + return $driver; + } + + /** + * Get a mock HeadMeta helper + * + * @return HeadMeta + */ + protected function getMetaHelper() + { + $mock = $this->getMockBuilder(HeadMeta::class) + ->disableOriginalConstructor() + ->setMethods(['appendName']) + ->getMock(); + $mock->expects($this->once())->method('appendName') + ->with($this->equalTo('prism.title'), $this->equalTo('Fake Title')); + return $mock; + } + + /** + * Get a mock plugin manager + * + * @return PluginManager + */ + protected function getPluginManager() + { + $mock = $this->getMockBuilder(PluginManager::class) + ->disableOriginalConstructor() + ->setMethods(['get']) + ->getMock(); + $mock->expects($this->once())->method('get') + ->with($this->equalTo('PRISM')) + ->will($this->returnValue(new PRISM())); + return $mock; + } + + /** + * Test basic functionality of the helper. + * + * @return void + */ + public function testMetadata() + { + $helper = new Metadata( + $this->getPluginManager(), + new Config(['Vocabularies' => [TestHarness::class => ['PRISM']]]), + $this->getMetaHelper() + ); + $helper->generateMetatags( + $this->getDriver(['Title' => 'Fake Title']) + ); + } +} -- GitLab