From 719ee17cebaf739696fd5e5752baa79e4c6b5a26 Mon Sep 17 00:00:00 2001 From: Demian Katz <demian.katz@villanova.edu> Date: Fri, 26 Sep 2014 14:26:24 -0400 Subject: [PATCH] Fixed config validation bug; simplified code. - Includes tests. --- module/VuFind/src/VuFind/Auth/ChoiceAuth.php | 5 +- .../src/VuFindTest/Auth/ChoiceAuthTest.php | 128 ++++++++++++++++++ 2 files changed, 130 insertions(+), 3 deletions(-) create mode 100644 module/VuFind/tests/unit-tests/src/VuFindTest/Auth/ChoiceAuthTest.php diff --git a/module/VuFind/src/VuFind/Auth/ChoiceAuth.php b/module/VuFind/src/VuFind/Auth/ChoiceAuth.php index 4e7decdcb2c..da179177f1f 100644 --- a/module/VuFind/src/VuFind/Auth/ChoiceAuth.php +++ b/module/VuFind/src/VuFind/Auth/ChoiceAuth.php @@ -94,8 +94,7 @@ class ChoiceAuth extends AbstractBase */ protected function validateConfig() { - if (!isset($this->config->ChoiceAuth) - || !isset($this->config->ChoiceAuth->choice_order) + if (!isset($this->config->ChoiceAuth->choice_order) || !strlen($this->config->ChoiceAuth->choice_order) ) { throw new AuthException( @@ -117,7 +116,7 @@ class ChoiceAuth extends AbstractBase { parent::setConfig($config); $this->strategies = array_map( - 'trim', explode(',', $config->ChoiceAuth->choice_order) + 'trim', explode(',', $this->getConfig()->ChoiceAuth->choice_order) ); } diff --git a/module/VuFind/tests/unit-tests/src/VuFindTest/Auth/ChoiceAuthTest.php b/module/VuFind/tests/unit-tests/src/VuFindTest/Auth/ChoiceAuthTest.php new file mode 100644 index 00000000000..8d05282301e --- /dev/null +++ b/module/VuFind/tests/unit-tests/src/VuFindTest/Auth/ChoiceAuthTest.php @@ -0,0 +1,128 @@ +<?php +/** + * ChoiceAuth test class. + * + * PHP version 5 + * + * Copyright (C) Villanova University 2011. + * + * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * @category VuFind2 + * @package Tests + * @author Demian Katz <demian.katz@villanova.edu> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link http://www.vufind.org Main Page + */ +namespace VuFindTest\Auth; +use VuFind\Auth\ChoiceAuth, VuFind\Auth\PluginManager, + VuFind\Db\Row\User as UserRow, Zend\Config\Config; + +/** + * ChoiceAuth test class. + * + * @category VuFind2 + * @package Tests + * @author Demian Katz <demian.katz@villanova.edu> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link http://www.vufind.org Main Page + */ +class ChoiceAuthTest extends \VuFindTest\Unit\TestCase +{ + /** + * Test config validation + * + * @return void + * @expectedException \VuFind\Exception\Auth + * @expectedExceptionMessage One or more ChoiceAuth parameters are missing. + */ + public function testBadConfiguration() + { + $ca = new ChoiceAuth(); + $ca->setConfig(new Config(array())); + } + + /** + * Test default getPluginManager behavior + * + * @return void + * @expectedException \Exception + * @expectedExceptionMessage Plugin manager missing. + */ + public function testMissingPluginManager() + { + $ca = new ChoiceAuth(); + $ca->getPluginManager(); + } + + /** + * Get a ChoiceAuth object. + * + * @param string $strategies Strategies setting + * + * @return ChoiceAuth + */ + protected function getChoiceAuth($strategies = 'Database,Shibboleth') + { + $ca = new ChoiceAuth(); + $ca->setConfig( + new Config(array('ChoiceAuth' => array('choice_order' => $strategies))) + ); + $ca->setPluginManager($this->getMockPluginManager()); + return $ca; + } + + /** + * Get a mock plugin manager. + * + * @return PluginManager + */ + protected function getMockPluginManager() + { + $pm = new PluginManager(); + $mockDb = $this->getMockBuilder('VuFind\Auth\Database') + ->disableOriginalConstructor() + ->getMock(); + $mockShib = $this->getMockBuilder('VuFind\Auth\Shibboleth') + ->disableOriginalConstructor() + ->getMock(); + $pm->setService('Database', $mockDb); + $pm->setService('Shibboleth', $mockShib); + return $pm; + } + + /** + * Get a mock user object + * + * @return UserRow + */ + protected function getMockUser() + { + return $this->getMockBuilder('VuFind\Db\Row\User') + ->disableOriginalConstructor() + ->getMock(); + } + + /** + * Get a mock request object + * + * @return \Zend\Http\PhpEnvironment\Request + */ + protected function getMockRequest() + { + return $this->getMockBuilder('Zend\Http\PhpEnvironment\Request') + ->disableOriginalConstructor() + ->getMock(); + } +} \ No newline at end of file -- GitLab