Bug 20582: Fix PSGI file when behind a reverse proxy
[koha.git] / t / Matcher.t
blobe7dbeee6a3babcf0869c9e398c022a8ac78c588e
1 #!/usr/bin/perl
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
18 use Modern::Perl;
20 use Test::More;
21 use Test::MockModule;
22 use Test::Warn;
24 use MARC::Record;
26 use Module::Load::Conditional qw/check_install/;
28 BEGIN {
29 if ( check_install( module => 'Test::DBIx::Class' ) ) {
30 plan tests => 12;
31 } else {
32 plan skip_all => "Need Test::DBIx::Class"
36 use Test::DBIx::Class;
38 my $db = Test::MockModule->new('Koha::Database');
39 $db->mock( _new_schema => sub { return Schema(); } );
41 use_ok('C4::Matcher');
43 fixtures_ok [
44 MarcMatcher => [
45 [ 'matcher_id', 'code', 'description', 'record_type', 'threshold' ],
46 [ 1, 'ISBN', 'ISBN', 'red', 1 ],
47 [ 2, 'ISSN', 'ISSN', 'blue', 0 ]
49 ], 'add fixtures';
51 my @matchers = C4::Matcher::GetMatcherList();
53 is( $matchers[0]->{'matcher_id'}, 1, 'First matcher_id value is 1' );
55 is( $matchers[1]->{'matcher_id'}, 2, 'Second matcher_id value is 2' );
57 my $matcher_id = C4::Matcher::GetMatcherId('ISBN');
59 is( $matcher_id, 1, 'testing getmatcherid' );
61 my $testmatcher;
63 ok( $testmatcher = C4::Matcher->new( 'red', 1 ), 'testing matcher new' );
65 ok( $testmatcher = C4::Matcher->new( 'blue', 0 ), 'testing matcher new' );
67 $testmatcher->threshold(1000);
69 is( $testmatcher->threshold(), 1000, 'testing threshhold accessor method' );
71 $testmatcher->_id(53);
73 is( $testmatcher->_id(), 53, 'testing _id accessor' );
75 $testmatcher->code('match on ISBN');
77 is( $testmatcher->code(), 'match on ISBN', 'testing code accessor' );
79 $testmatcher->description('match on ISSN');
81 is( $testmatcher->description(), 'match on ISSN', 'testing code accessor' );
83 subtest '_get_match_keys() tests' => sub {
85 plan tests => 20;
87 my $matchpoint = get_title_matchpoint({
88 length => 0,
89 norms => [ 'legacy_default' ],
90 offset => 0
91 });
93 my $record = MARC::Record->new();
94 $record->append_fields(
95 MARC::Field->new('020', '1', ' ',
96 a => '978-1451697216 (alk. paper)'),
97 MARC::Field->new('020', '1', ' ',
98 a => '145169721X (alk. paper)'),
99 MARC::Field->new('020', '1', ' ',
100 a => '1NOTISBN3'),
101 MARC::Field->new('100', '1', ' ',
102 a => 'King, Stephen',
103 d => 'd1947-'),
104 MARC::Field->new('245', ' ', ' ',
105 a => ' .; thE t[]:,aliS(m)/An\'"',
106 c => 'Stephen King, Peter Straub.' ),
107 MARC::Field->new('700', ' ', ' ',
108 a => 'Straub, Peter',
109 d => '1943-')
112 my @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
114 is( $keys[0], 'THE TALISMAN STEPHEN KING PETER STRAUB',
115 'Match key correctly calculated with no $norms');
117 $matchpoint = get_title_matchpoint({
118 length => 9,
119 norms => [ 'legacy_default' ],
120 offset => 0
122 @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
123 is( $keys[0], 'THE',
124 'Match key correctly calculated with length 9');
126 $matchpoint = get_title_matchpoint({
127 length => 9,
128 norms => [ 'legacy_default' ],
129 offset => 1
131 @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
132 is( $keys[0], 'THE T',
133 'Match key correctly calculated with length 9 and offset 1');
135 $matchpoint = get_title_matchpoint({
136 length => 9,
137 norms => [ 'legacy_default' ],
138 offset => 2
140 @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
141 is( $keys[0], 'THE T',
142 'Match key correctly calculated with length 9 and offset 2, should not remove space');
144 $matchpoint = get_authors_matchpoint({
145 length => 0,
146 norms => [ 'legacy_default' ],
147 offset => 0
149 @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
150 is( $keys[0], 'STRAUB PETER KING STEPHEN',
151 'Match key correctly calculated with multiple components');
153 $matchpoint = get_authors_matchpoint({
154 length => 9,
155 norms => [ 'legacy_default' ],
156 offset => 0
158 @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
159 is( $keys[0], 'STRAUB P KING STE',
160 'Match key correctly calculated with multiple components, length 9');
162 $matchpoint = get_authors_matchpoint({
163 length => 10,
164 norms => [ 'legacy_default' ],
165 offset => 0
167 @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
168 is( $keys[0], 'STRAUB PE KING STEP',
169 'Match key correctly calculated with multiple components, length 10');
171 $matchpoint = get_authors_matchpoint({
172 length => 10,
173 norms => [ 'legacy_default' ],
174 offset => 2
176 @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
177 is( $keys[0], 'RAUB PETE NG STEPHE',
178 'Match key correctly calculated with multiple components, length 10, offset 1');
180 $matchpoint = get_title_matchpoint({
181 length => 0,
182 norms => [ 'none', 'none' ],
183 offset => 0
185 @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
186 is( $keys[0], ' .; thE t[]:,aliS(m)/An\'" Stephen King, Peter Straub.',
187 'Match key intact if \'none\' specified, length 0 and offset 0' );
189 $matchpoint = get_authors_matchpoint({
190 length => 0,
191 norms => [ 'upper_case' ],
192 offset => 0
194 @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
195 is( $keys[0], 'STRAUB, PETER KING, STEPHEN',
196 'Match key correctly calculated with multiple components, \'upper_case\' norm');
198 $matchpoint = get_authors_matchpoint({
199 length => 0,
200 norms => [ 'lower_case' ],
201 offset => 0
203 @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
204 is( $keys[0], 'straub, peter king, stephen',
205 'Match key correctly calculated with multiple components, \'lower_case\' norm');
207 $matchpoint = get_authors_matchpoint({
208 length => 0,
209 norms => [ 'remove_spaces' ],
210 offset => 0
212 @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
213 is( $keys[0], 'Straub,Peter King,Stephen',
214 'Match key correctly calculated with multiple components, \'remove_spaces\' norm');
216 $matchpoint = get_authors_matchpoint({
217 length => 0,
218 norms => [ 'remove_spaces', 'lower_case' ],
219 offset => 0
221 @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
222 is( $keys[0], 'straub,peter king,stephen',
223 'Match key correctly calculated with multiple components, \'remove_spaces\' and \'lower_case\' norm');
225 my $norm = 'unknown_norm';
226 $matchpoint = get_title_matchpoint({
227 length => 0,
228 norms => [ $norm ],
229 offset => 0
231 warning_is
232 { @keys = C4::Matcher::_get_match_keys( $record, $matchpoint ) }
233 qq{Invalid normalization routine required ($norm)},
234 'Passing an invalid normalization routine name raises a warning';
236 is( $keys[0], ' .; thE t[]:,aliS(m)/An\'" Stephen King, Peter Straub.',
237 'Match key intact if invalid normalization routine specified' );
239 $matchpoint = get_title_matchpoint({
240 length => 0,
241 norms => [ $norm, 'upper_case' ],
242 offset => 0
244 warning_is
245 { @keys = C4::Matcher::_get_match_keys( $record, $matchpoint ) }
246 qq{Invalid normalization routine required ($norm)},
247 'Passing an invalid normalization routine name raises a warning';
249 is( $keys[0], ' .; THE T[]:,ALIS(M)/AN\'" STEPHEN KING, PETER STRAUB.',
250 'Match key correctly normalized if invalid normalization routine specified' );
252 $matchpoint = get_isbn_matchpoint({
253 length => 0,
254 norms => [ 'ISBN' ],
255 offset => 0
257 @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
258 is( $keys[0], '9781451697216',
259 'Match key correctly calculated as ISBN13 when ISBN normalizer used');
260 is( $keys[1], '9781451697216',
261 'Match key correctly calculated as ISBN13 when ISBN normalizer used');
262 is( $keys[2], '1NOTISBN3',
263 'Match key passed through if not an isbn when ISBN normalizer used');
267 sub get_title_matchpoint {
269 my $params = shift;
271 my $length = $params->{length} // 0;
272 my $norms = $params->{norms} // [];
273 my $offset = $params->{offset} // 0;
275 my $matchpoint = {
276 components => [
278 length => $length,
279 norms => $norms,
280 offset => $offset,
281 subfields =>
283 a => 1,
284 c => 1
286 tag => '245'
289 index => "title",
290 score => 1000
293 return $matchpoint;
296 sub get_authors_matchpoint {
298 my $params = shift;
300 my $length = $params->{length} // 0;
301 my $norms = $params->{norms} // [];
302 my $offset = $params->{offset} // 0;
304 my $matchpoint = {
305 components => [
307 length => $length,
308 norms => $norms,
309 offset => $offset,
310 subfields =>
312 a => 1
314 tag => '700'
317 length => $length,
318 norms => $norms,
319 offset => $offset,
320 subfields =>
322 a => 1
324 tag => '100'
327 index => "author",
328 score => 1000
331 return $matchpoint;
334 sub get_isbn_matchpoint {
336 my $params = shift;
338 my $length = $params->{length} // 0;
339 my $norms = $params->{norms} // [];
340 my $offset = $params->{offset} // 0;
342 my $matchpoint = {
343 components => [
345 length => $length,
346 norms => $norms,
347 offset => $offset,
348 subfields =>
350 a => 1
352 tag => '020'
355 index => "isbn",
356 score => 1000
359 return $matchpoint;