-
Notifications
You must be signed in to change notification settings - Fork 399
Expand file tree
/
Copy pathNewItemsTest.php
More file actions
254 lines (235 loc) · 7.41 KB
/
Copy pathNewItemsTest.php
File metadata and controls
254 lines (235 loc) · 7.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<?php
/**
* New items controller plugin tests.
*
* PHP version 8
*
* Copyright (C) Villanova University 2010.
*
* 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, see
* <https://www.gnu.org/licenses/>.
*
* @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\Controller\Plugin;
use VuFind\Config\Config;
use VuFind\Controller\Plugin\NewItems;
/**
* New items controller plugin 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 NewItemsTest extends \PHPUnit\Framework\TestCase
{
/**
* Test ILS bib ID retrieval.
*
* @return void
*/
public function testGetBibIDsFromCatalog()
{
$flash = $this->createMock(\Laminas\Mvc\Plugin\FlashMessenger\FlashMessenger::class);
$config = new Config(['result_pages' => 10]);
$newItems = new NewItems($config);
$bibs = $newItems->getBibIDsFromCatalog(
$this->getMockCatalog(),
$this->getMockParams(),
10,
'a',
$flash
);
$this->assertEquals([1, 2], $bibs);
}
/**
* Test ILS bib ID retrieval with ID limit.
*
* @return void
*/
public function testGetBibIDsFromCatalogWithIDLimit()
{
$flash = $this->createMock(\Laminas\Mvc\Plugin\FlashMessenger\FlashMessenger::class);
$flash->expects($this->once())->method('addInfoMessage')
->with('too_many_new_items');
$config = new Config(['result_pages' => 10]);
$newItems = new NewItems($config);
$bibs = $newItems->getBibIDsFromCatalog(
$this->getMockCatalog(),
$this->getMockParams(1),
10,
'a',
$flash
);
$this->assertEquals([1], $bibs);
}
/**
* Test default ILS getFunds() behavior.
*
* @return void
*/
public function testGetFundList()
{
$catalog = $this->createMock(\VuFind\ILS\Connection::class);
$catalog->expects($this->once())->method('checkCapability')
->with('getFunds')->willReturn(true);
$catalog->expects($this->once())->method('__call')
->willReturnCallback(
fn ($method) => $method === 'getFunds' ? ['a', 'b', 'c'] : null
);
$controller = $this->createMock(\VuFind\Controller\SearchController::class);
$controller->expects($this->once())->method('getILS')
->willReturn($catalog);
$newItems = new NewItems(new Config([]));
$newItems->setController($controller);
$this->assertEquals(['a', 'b', 'c'], $newItems->getFundList());
}
/**
* Test getFundList() in non-ILS mode.
*
* @return void
*/
public function testGetFundListWithoutILS()
{
$newItems = new NewItems(new Config(['method' => 'solr']));
$this->assertEquals([], $newItems->getFundList());
}
/**
* Test a single hidden filter.
*
* @return void
*/
public function testGetSingleHiddenFilter()
{
$config = new Config(['filter' => 'a:b']);
$newItems = new NewItems($config);
$this->assertEquals(['a:b'], $newItems->getHiddenFilters());
}
/**
* Test a single hidden filter.
*
* @return void
*/
public function testGetMultipleHiddenFilters()
{
$config = new Config(['filter' => ['a:b', 'b:c']]);
$newItems = new NewItems($config);
$this->assertEquals(['a:b', 'b:c'], $newItems->getHiddenFilters());
}
/**
* Test various default values.
*
* @return void
*/
public function testDefaults()
{
$config = new Config([]);
$newItems = new NewItems($config);
$this->assertEquals([], $newItems->getHiddenFilters());
$this->assertEquals('ils', $newItems->getMethod());
$this->assertEquals(30, $newItems->getMaxAge());
$this->assertEquals([1, 5, 30], $newItems->getRanges());
$this->assertEquals(10, $newItems->getResultPages());
}
/**
* Test custom range settings.
*
* @return void
*/
public function testCustomRanges()
{
$config = new Config(['ranges' => '10,150,300']);
$newItems = new NewItems($config);
$this->assertEquals([10, 150, 300], $newItems->getRanges());
}
/**
* Test custom result pages setting.
*
* @return void
*/
public function testCustomResultPages()
{
$config = new Config(['result_pages' => '2']);
$newItems = new NewItems($config);
$this->assertEquals(2, $newItems->getResultPages());
}
/**
* Test illegal result pages setting.
*
* @return void
*/
public function testIllegalResultPages()
{
$config = new Config(['result_pages' => '-2']);
$newItems = new NewItems($config);
// expect a default of 10 if a bad value was passed in
$this->assertEquals(10, $newItems->getResultPages());
}
/**
* Test Solr filter generator.
*
* @return void
*/
public function testGetSolrFilter()
{
$range = 30;
$expected = 'first_indexed:[NOW-' . $range . 'DAYS/DAY TO *]';
$newItems = new NewItems(new Config([]));
$this->assertEquals($expected, $newItems->getSolrFilter($range));
}
/**
* Get a mock catalog object (for use in getBibIDs tests).
*
* @return \VuFind\ILS\Connection
*/
protected function getMockCatalog(): \VuFind\ILS\Connection
{
$catalog = $this->createMock(\VuFind\ILS\Connection::class);
$catalog->expects($this->once())->method('__call')
->willReturnCallback(
function ($method, $args) {
if ($method !== 'getNewItems') {
return null;
}
$this->assertEquals(1, $args[0]);
$this->assertEquals(200, $args[1]);
$this->assertEquals(10, $args[2]);
$this->assertEquals('a', $args[3]);
return ['results' => [['id' => 1], ['id' => 2]]];
}
);
return $catalog;
}
/**
* Get a mock params object.
*
* @param int $idLimit Mock ID limit value
*
* @return \VuFind\Search\Solr\Params
*/
protected function getMockParams($idLimit = 1024)
{
$params = $this->createMock(\VuFind\Search\Solr\Params::class);
$params->expects($this->once())->method('getLimit')
->willReturn(20);
$params->expects($this->once())->method('getQueryIDLimit')
->willReturn($idLimit);
return $params;
}
}