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>.
26 use Module::Load::Conditional qw/check_install/;
29 if ( check_install( module => 'Test::DBIx::Class' ) ) {
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', qw( GetMatcherList GetMatcherId ));
45 [ 'matcher_id', 'code', 'description', 'record_type', 'threshold' ],
46 [ 1, 'ISBN', 'ISBN', 'red', 1 ],
47 [ 2, 'ISSN', 'ISSN', 'blue', 0 ]
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' );
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 {
87 my $matchpoint = get_title_matchpoint({
89 norms => [ 'legacy_default' ],
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', ' ',
101 MARC::Field->new('100', '1', ' ',
102 a => 'King, Stephen',
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',
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({
119 norms => [ 'legacy_default' ],
122 @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
124 'Match key correctly calculated with length 9');
126 $matchpoint = get_title_matchpoint({
128 norms => [ 'legacy_default' ],
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({
137 norms => [ 'legacy_default' ],
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({
146 norms => [ 'legacy_default' ],
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({
155 norms => [ 'legacy_default' ],
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({
164 norms => [ 'legacy_default' ],
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({
173 norms => [ 'legacy_default' ],
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({
182 norms => [ 'none', 'none' ],
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({
191 norms => [ 'upper_case' ],
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({
200 norms => [ 'lower_case' ],
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({
209 norms => [ 'remove_spaces' ],
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({
218 norms => [ 'remove_spaces', 'lower_case' ],
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({
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({
241 norms => [ $norm, 'upper_case' ],
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({
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');
265 $matchpoint = get_title_matchpoint({
269 delete $matchpoint->{component}->{subfields};
270 @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
271 is( $keys[0], ' .; thE t[]:,aliS(m)/An\'" Stephen King, Peter Straub.', "Match key correctly returns whole field if no subfields specified" )
274 subtest '_get_match_keys() leader tests' => sub {
276 my $record = MARC::Record->new();
277 my $matchpoint = get_leader_matchpoint({
282 my @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
283 is( $keys[0], ' ', 'Match key correctly calculated as " " from LDR6 when no leader available');
285 $record->leader('01344cam a22003014a 4500');
287 @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
288 is( $keys[0], 'a', 'Match key correctly calculated as "a" from LDR6');
291 sub get_title_matchpoint {
295 my $length = $params->{length} // 0;
296 my $norms = $params->{norms} // [];
297 my $offset = $params->{offset} // 0;
320 sub get_authors_matchpoint {
324 my $length = $params->{length} // 0;
325 my $norms = $params->{norms} // [];
326 my $offset = $params->{offset} // 0;
358 sub get_isbn_matchpoint {
362 my $length = $params->{length} // 0;
363 my $norms = $params->{norms} // [];
364 my $offset = $params->{offset} // 0;
386 sub get_leader_matchpoint {
388 my $length = $params->{length} // 0;
389 my $norms = $params->{norms} // [];
390 my $offset = $params->{offset} // 0;