diff --git a/module/VuFind/src/VuFind/XSLT/Import/VuFind.php b/module/VuFind/src/VuFind/XSLT/Import/VuFind.php index 2f5f6d97cfd9267cbfab54bf323bb0d12117cb34..575407fd728b50ceeda989bf49fe34ca4e61a249 100644 --- a/module/VuFind/src/VuFind/XSLT/Import/VuFind.php +++ b/module/VuFind/src/VuFind/XSLT/Import/VuFind.php @@ -393,16 +393,11 @@ class VuFind */ public static function xmlAsText($in) { - // Ensure that $in is an array: - if (!is_array($in)) { - $in = [$in]; - } - // Start building return value: $text = ''; // Extract all text: - foreach ($in as $current) { + foreach ((array)$in as $current) { // Convert DOMElement to SimpleXML: $xml = simplexml_import_dom($current); @@ -427,12 +422,7 @@ class VuFind */ public static function removeTagAndReturnXMLasText($in, $tag) { - // Ensure that $in is an array: - if (!is_array($in)) { - $in = [$in]; - } - - foreach ($in as $current) { + foreach ((array)$in as $current) { $matches = $current->getElementsByTagName($tag); foreach ($matches as $match) { $current->removeChild($match); diff --git a/module/VuFind/tests/unit-tests/src/VuFindTest/XSLT/Import/VuFindTest.php b/module/VuFind/tests/unit-tests/src/VuFindTest/XSLT/Import/VuFindTest.php new file mode 100644 index 0000000000000000000000000000000000000000..2757350182fff677bd57ddcc0a77275e343f6f01 --- /dev/null +++ b/module/VuFind/tests/unit-tests/src/VuFindTest/XSLT/Import/VuFindTest.php @@ -0,0 +1,161 @@ +<?php +/** + * XSLT helper tests. + * + * PHP version 7 + * + * Copyright (C) Villanova University 2019. + * + * 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\XSLT\Import; + +use VuFind\XSLT\Import\VuFind; + +/** + * XSLT helper tests. + * + * @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 VuFindTest extends \VuFindTest\Unit\DbTestCase +{ + /** + * Test the getChangeTracker helper. + * + * @return void + */ + public function testGetChangeTracker() + { + VuFind::setServiceLocator($this->getServiceManager()); + $this->assertEquals( + \VuFind\Db\Table\ChangeTracker::class, + get_class(VuFind::getChangeTracker()) + ); + } + + /** + * Test the getConfig helper. + * + * @return void + */ + public function testGetConfig() + { + VuFind::setServiceLocator($this->getServiceManager()); + $this->assertEquals( + \Zend\Config\Config::class, get_class(VuFind::getConfig()) + ); + } + + /** + * Test the harvestTextFile helper. + * + * @return void + */ + public function testHarvestTextFile() + { + $this->assertEquals( + file_get_contents(__FILE__), + VuFind::harvestTextFile('file://' . __FILE__) + ); + } + + /** + * Test the stripBadChars helper. + * + * @return void + */ + public function testStripBadChars() + { + $this->assertEquals('f oo', VuFind::stripBadChars('f' . chr(8) . 'oo')); + } + + /** + * Test the mapString helper. + * + * @return void + */ + public function testMapString() + { + $this->assertEquals( + 'CD', VuFind::mapString('SoundDisc', 'format_map.properties') + ); + } + + /** + * Test the stripArticles helper. + * + * @return void + */ + public function testStripArticles() + { + $this->assertEquals('title', VuFind::stripArticles('The Title')); + $this->assertEquals('title', VuFind::stripArticles('A Title')); + $this->assertEquals('odd title', VuFind::stripArticles('An Odd Title')); + } + + /** + * Test the xmlAsText helper. + * + * @return void + */ + public function testXmlAsText() + { + $doc = new \DOMDocument('1.0'); + $node = new \DOMElement('bar', 'foo'); + $doc->appendChild($node); + $expected = '<?xml version="1.0"?>' . "\n<bar>foo</bar>\n"; + $this->assertEquals($expected, VuFind::xmlAsText([$node])); + } + + /** + * Test the removeTagAndReturnXMLasText helper. + * + * @return void + */ + public function testRemoveTagAndReturnXMLasText() + { + $doc = new \DOMDocument('1.0'); + $node = new \DOMElement('bar', 'foo'); + $doc->appendChild($node); + $node->appendChild(new \DOMElement('xyzzy', 'baz')); + $expected = '<?xml version="1.0"?>' . "\n<bar>foo</bar>\n"; + $this->assertEquals( + $expected, VuFind::removeTagAndReturnXMLasText([$node], 'xyzzy') + ); + } + + /** + * Test the explode helper. + * + * @return void + */ + public function testExplode() + { + $expected = '<?xml version="1.0" encoding="utf-8"?>' + . "\n<part>a</part>\n<part>b</part>\n"; + $this->assertEquals( + $expected, simplexml_import_dom(VuFind::explode(',', 'a,b'))->asXml() + ); + } +}