Bug 21336: Introduce administrative lockout
[koha.git] / t / Matcher.t
blob3a5494e26e44fbf57fc298356de40ab023005197
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 => 17;
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('100', '1', ' ',
96 a => 'King, Stephen',
97 d => 'd1947-'),
98 MARC::Field->new('245', ' ', ' ',
99 a => ' .; thE t[]:,aliS(m)/An\'"',
100 c => 'Stephen King, Peter Straub.' ),
101 MARC::Field->new('700', ' ', ' ',
102 a => 'Straub, Peter',
103 d => '1943-')
106 my @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
108 is( $keys[0], 'THE TALISMAN STEPHEN KING PETER STRAUB',
109 'Match key correctly calculated with no $norms');
111 $matchpoint = get_title_matchpoint({
112 length => 9,
113 norms => [ 'legacy_default' ],
114 offset => 0
116 @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
117 is( $keys[0], 'THE',
118 'Match key correctly calculated with length 9');
120 $matchpoint = get_title_matchpoint({
121 length => 9,
122 norms => [ 'legacy_default' ],
123 offset => 1
125 @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
126 is( $keys[0], 'THE T',
127 'Match key correctly calculated with length 9 and offset 1');
129 $matchpoint = get_title_matchpoint({
130 length => 9,
131 norms => [ 'legacy_default' ],
132 offset => 2
134 @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
135 is( $keys[0], 'THE T',
136 'Match key correctly calculated with length 9 and offset 2, should not remove space');
138 $matchpoint = get_authors_matchpoint({
139 length => 0,
140 norms => [ 'legacy_default' ],
141 offset => 0
143 @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
144 is( $keys[0], 'STRAUB PETER KING STEPHEN',
145 'Match key correctly calculated with multiple components');
147 $matchpoint = get_authors_matchpoint({
148 length => 9,
149 norms => [ 'legacy_default' ],
150 offset => 0
152 @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
153 is( $keys[0], 'STRAUB P KING STE',
154 'Match key correctly calculated with multiple components, length 9');
156 $matchpoint = get_authors_matchpoint({
157 length => 10,
158 norms => [ 'legacy_default' ],
159 offset => 0
161 @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
162 is( $keys[0], 'STRAUB PE KING STEP',
163 'Match key correctly calculated with multiple components, length 10');
165 $matchpoint = get_authors_matchpoint({
166 length => 10,
167 norms => [ 'legacy_default' ],
168 offset => 2
170 @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
171 is( $keys[0], 'RAUB PETE NG STEPHE',
172 'Match key correctly calculated with multiple components, length 10, offset 1');
174 $matchpoint = get_title_matchpoint({
175 length => 0,
176 norms => [ 'none', 'none' ],
177 offset => 0
179 @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
180 is( $keys[0], ' .; thE t[]:,aliS(m)/An\'" Stephen King, Peter Straub.',
181 'Match key intact if \'none\' specified, length 0 and offset 0' );
183 $matchpoint = get_authors_matchpoint({
184 length => 0,
185 norms => [ 'upper_case' ],
186 offset => 0
188 @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
189 is( $keys[0], 'STRAUB, PETER KING, STEPHEN',
190 'Match key correctly calculated with multiple components, \'upper_case\' norm');
192 $matchpoint = get_authors_matchpoint({
193 length => 0,
194 norms => [ 'lower_case' ],
195 offset => 0
197 @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
198 is( $keys[0], 'straub, peter king, stephen',
199 'Match key correctly calculated with multiple components, \'lower_case\' norm');
201 $matchpoint = get_authors_matchpoint({
202 length => 0,
203 norms => [ 'remove_spaces' ],
204 offset => 0
206 @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
207 is( $keys[0], 'Straub,Peter King,Stephen',
208 'Match key correctly calculated with multiple components, \'remove_spaces\' norm');
210 $matchpoint = get_authors_matchpoint({
211 length => 0,
212 norms => [ 'remove_spaces', 'lower_case' ],
213 offset => 0
215 @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
216 is( $keys[0], 'straub,peter king,stephen',
217 'Match key correctly calculated with multiple components, \'remove_spaces\' and \'lower_case\' norm');
219 my $norm = 'unknown_norm';
220 $matchpoint = get_title_matchpoint({
221 length => 0,
222 norms => [ $norm ],
223 offset => 0
225 warning_is
226 { @keys = C4::Matcher::_get_match_keys( $record, $matchpoint ) }
227 qq{Invalid normalization routine required ($norm)},
228 'Passing an invalid normalization routine name raises a warning';
230 is( $keys[0], ' .; thE t[]:,aliS(m)/An\'" Stephen King, Peter Straub.',
231 'Match key intact if invalid normalization routine specified' );
233 $matchpoint = get_title_matchpoint({
234 length => 0,
235 norms => [ $norm, 'upper_case' ],
236 offset => 0
238 warning_is
239 { @keys = C4::Matcher::_get_match_keys( $record, $matchpoint ) }
240 qq{Invalid normalization routine required ($norm)},
241 'Passing an invalid normalization routine name raises a warning';
243 is( $keys[0], ' .; THE T[]:,ALIS(M)/AN\'" STEPHEN KING, PETER STRAUB.',
244 'Match key correctly normalized if invalid normalization routine specified' );
247 sub get_title_matchpoint {
249 my $params = shift;
251 my $length = $params->{length} // 0;
252 my $norms = $params->{norms} // [];
253 my $offset = $params->{offset} // 0;
255 my $matchpoint = {
256 components => [
258 length => $length,
259 norms => $norms,
260 offset => $offset,
261 subfields =>
263 a => 1,
264 c => 1
266 tag => '245'
269 index => "title",
270 score => 1000
273 return $matchpoint;
276 sub get_authors_matchpoint {
278 my $params = shift;
280 my $length = $params->{length} // 0;
281 my $norms = $params->{norms} // [];
282 my $offset = $params->{offset} // 0;
284 my $matchpoint = {
285 components => [
287 length => $length,
288 norms => $norms,
289 offset => $offset,
290 subfields =>
292 a => 1
294 tag => '700'
297 length => $length,
298 norms => $norms,
299 offset => $offset,
300 subfields =>
302 a => 1
304 tag => '100'
307 index => "author",
308 score => 1000
311 return $matchpoint;