Skip to content

Commit

Permalink
Add test for new tab.
Browse files Browse the repository at this point in the history
  • Loading branch information
demiankatz committed Dec 18, 2024
1 parent bcf3b0f commit 34d7324
Showing 1 changed file with 131 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php

/**
* Channels Test Class
*
* PHP version 8
*
* Copyright (C) Villanova University 2024.
*
* 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\RecordTab;

use Laminas\Http\Request;
use Laminas\Stdlib\Parameters;
use VuFind\ChannelProvider\ChannelLoader;
use VuFind\RecordDriver\AbstractBase as RecordDriver;
use VuFind\RecordTab\Channels;

/**
* Channels 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 ChannelsTest extends \PHPUnit\Framework\TestCase
{
/**
* Get the object to test.
*
* @param array $options Config options
* @param ?ChannelLoader $mockLoader Channel loader (null for default mock)
* @param ?RecordDriver $mockDriver Record driver (null for default mock)
*
* @return Channels
*/
protected function getChannels(
array $options = [],
?ChannelLoader $mockLoader = null,
?RecordDriver $mockDriver = null
): Channels {
$channels = new Channels($mockLoader ?? $this->createMock(ChannelLoader::class), $options);
$channels->setRecordDriver($mockDriver ?? $this->createMock(RecordDriver::class));
return $channels;
}

/**
* Test getDescription().
*
* @return void
*/
public function testGetDescription(): void
{
// Default case:
$this->assertEquals('Channels', $this->getChannels()->getDescription());
// Custom case:
$this->assertEquals('Custom', $this->getChannels(['label' => 'Custom'])->getDescription());
}

/**
* Test supportsAjax().
*
* @return void
*/
public function testSupportsAjax(): void
{
$this->assertFalse($this->getChannels()->supportsAjax());
}

/**
* Test getContext() with default config and no request set.
*
* @return void
*/
public function testGetContextWithDefaultConfigAndWithoutRequest(): void
{
$driver = $this->createMock(RecordDriver::class);
$driver->method('getUniqueID')->willReturn('foo');
$driver->method('getSearchBackendIdentifier')->willReturn('bar');
$loader = $this->createMock(ChannelLoader::class);
$loader->expects($this->once())
->method('getRecordContext')
->with('foo', null, null, 'bar', ['recordTab', 'record'])
->willReturn(['record' => 'context']);
$channels = $this->getChannels([], $loader, $driver);
$this->assertEquals(['displaySearchBox' => false, 'record' => 'context'], $channels->getContext());
}

/**
* Test getContext() with non-default config and a request set.
*
* @return void
*/
public function testGetContextWithConfigAndRequest(): void
{
$driver = $this->createMock(RecordDriver::class);
$driver->method('getUniqueID')->willReturn('foo');
$driver->method('getSearchBackendIdentifier')->willReturn('bar');
$loader = $this->createMock(ChannelLoader::class);
$loader->expects($this->once())
->method('getRecordContext')
->with('foo', 'tok', 'prov', 'bar', ['recordTab', 'record'])
->willReturn(['record' => 'context']);
$channels = $this->getChannels(['include_channels_search_box' => true], $loader, $driver);
$request = new Request();
$request->setQuery(new Parameters(['channelToken' => 'tok', 'channelProvider' => 'prov']));
$channels->setRequest($request);
$this->assertEquals(['displaySearchBox' => true, 'record' => 'context'], $channels->getContext());
}
}

0 comments on commit 34d7324

Please sign in to comment.