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>.
20 use Test::More tests => 3;
23 use t::lib::TestBuilder;
27 use C4::Matcher qw( GetMatcherList GetMatcherId );
29 my $schema = Koha::Database->new->schema;
30 my $builder = t::lib::TestBuilder->new;
31 $schema->storage->txn_begin;
33 subtest 'GetMatcherList' => sub {
36 $schema->resultset('MarcMatcher')->delete_all;
37 my $matcher1 = $builder->build({ source => 'MarcMatcher',
38 value => { code => 'ISBN', description => 'ISBN', record_type => 'red', threshold => 1 },
40 my $matcher2 = $builder->build({ source => 'MarcMatcher',
41 value => { code => 'ISSN', description => 'ISSN', record_type => 'blue', threshold => 0 },
44 my @matchers = C4::Matcher::GetMatcherList();
45 is( $matchers[0]->{'matcher_id'}, $matcher1->{matcher_id}, 'First matcher_id value' );
46 is( $matchers[1]->{'matcher_id'}, $matcher2->{matcher_id}, 'Second matcher_id value' );
48 my $matcher_id = C4::Matcher::GetMatcherId('ISBN');
49 is( $matcher_id, $matcher1->{matcher_id}, 'testing getmatcherid' );
52 ok( $testmatcher = C4::Matcher->new( 'red', 1 ), 'testing matcher new' );
53 ok( $testmatcher = C4::Matcher->new( 'blue', 0 ), 'testing matcher new' );
55 $testmatcher->threshold(1000);
56 is( $testmatcher->threshold(), 1000, 'testing threshhold accessor method' );
58 $testmatcher->_id(53);
59 is( $testmatcher->_id(), 53, 'testing _id accessor' );
61 $testmatcher->code('match on ISBN');
62 is( $testmatcher->code(), 'match on ISBN', 'testing code accessor' );
64 $testmatcher->description('match on ISSN');
65 is( $testmatcher->description(), 'match on ISSN', 'testing code accessor' );
68 subtest '_get_match_keys() tests' => sub {
71 my $matchpoint = get_title_matchpoint({
73 norms => [ 'legacy_default' ],
77 my $record = MARC::Record->new();
78 $record->append_fields(
79 MARC::Field->new('020', '1', ' ',
80 a => '978-1451697216 (alk. paper)'),
81 MARC::Field->new('020', '1', ' ',
82 a => '145169721X (alk. paper)'),
83 MARC::Field->new('020', '1', ' ',
85 MARC::Field->new('100', '1', ' ',
88 MARC::Field->new('245', ' ', ' ',
89 a => ' .; thE t[]:,aliS(m)/An\'"',
90 c => 'Stephen King, Peter Straub.' ),
91 MARC::Field->new('700', ' ', ' ',
96 my @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
98 is( $keys[0], 'THE TALISMAN STEPHEN KING PETER STRAUB',
99 'Match key correctly calculated with no $norms');
101 $matchpoint = get_title_matchpoint({
103 norms => [ 'legacy_default' ],
106 @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
108 'Match key correctly calculated with length 9');
110 $matchpoint = get_title_matchpoint({
112 norms => [ 'legacy_default' ],
115 @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
116 is( $keys[0], 'THE T',
117 'Match key correctly calculated with length 9 and offset 1');
119 $matchpoint = get_title_matchpoint({
121 norms => [ 'legacy_default' ],
124 @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
125 is( $keys[0], 'THE T',
126 'Match key correctly calculated with length 9 and offset 2, should not remove space');
128 $matchpoint = get_authors_matchpoint({
130 norms => [ 'legacy_default' ],
133 @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
134 is( $keys[0], 'STRAUB PETER KING STEPHEN',
135 'Match key correctly calculated with multiple components');
137 $matchpoint = get_authors_matchpoint({
139 norms => [ 'legacy_default' ],
142 @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
143 is( $keys[0], 'STRAUB P KING STE',
144 'Match key correctly calculated with multiple components, length 9');
146 $matchpoint = get_authors_matchpoint({
148 norms => [ 'legacy_default' ],
151 @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
152 is( $keys[0], 'STRAUB PE KING STEP',
153 'Match key correctly calculated with multiple components, length 10');
155 $matchpoint = get_authors_matchpoint({
157 norms => [ 'legacy_default' ],
160 @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
161 is( $keys[0], 'RAUB PETE NG STEPHE',
162 'Match key correctly calculated with multiple components, length 10, offset 1');
164 $matchpoint = get_title_matchpoint({
166 norms => [ 'none', 'none' ],
169 @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
170 is( $keys[0], ' .; thE t[]:,aliS(m)/An\'" Stephen King, Peter Straub.',
171 'Match key intact if \'none\' specified, length 0 and offset 0' );
173 $matchpoint = get_authors_matchpoint({
175 norms => [ 'upper_case' ],
178 @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
179 is( $keys[0], 'STRAUB, PETER KING, STEPHEN',
180 'Match key correctly calculated with multiple components, \'upper_case\' norm');
182 $matchpoint = get_authors_matchpoint({
184 norms => [ 'lower_case' ],
187 @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
188 is( $keys[0], 'straub, peter king, stephen',
189 'Match key correctly calculated with multiple components, \'lower_case\' norm');
191 $matchpoint = get_authors_matchpoint({
193 norms => [ 'remove_spaces' ],
196 @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
197 is( $keys[0], 'Straub,Peter King,Stephen',
198 'Match key correctly calculated with multiple components, \'remove_spaces\' norm');
200 $matchpoint = get_authors_matchpoint({
202 norms => [ 'remove_spaces', 'lower_case' ],
205 @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
206 is( $keys[0], 'straub,peter king,stephen',
207 'Match key correctly calculated with multiple components, \'remove_spaces\' and \'lower_case\' norm');
209 my $norm = 'unknown_norm';
210 $matchpoint = get_title_matchpoint({
216 { @keys = C4::Matcher::_get_match_keys( $record, $matchpoint ) }
217 qq{Invalid normalization routine required ($norm)},
218 'Passing an invalid normalization routine name raises a warning';
220 is( $keys[0], ' .; thE t[]:,aliS(m)/An\'" Stephen King, Peter Straub.',
221 'Match key intact if invalid normalization routine specified' );
223 $matchpoint = get_title_matchpoint({
225 norms => [ $norm, 'upper_case' ],
229 { @keys = C4::Matcher::_get_match_keys( $record, $matchpoint ) }
230 qq{Invalid normalization routine required ($norm)},
231 'Passing an invalid normalization routine name raises a warning';
233 is( $keys[0], ' .; THE T[]:,ALIS(M)/AN\'" STEPHEN KING, PETER STRAUB.',
234 'Match key correctly normalized if invalid normalization routine specified' );
236 $matchpoint = get_isbn_matchpoint({
241 @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
242 is( $keys[0], '9781451697216',
243 'Match key correctly calculated as ISBN13 when ISBN normalizer used');
244 is( $keys[1], '9781451697216',
245 'Match key correctly calculated as ISBN13 when ISBN normalizer used');
246 is( $keys[2], '1NOTISBN3',
247 'Match key passed through if not an isbn when ISBN normalizer used');
249 $matchpoint = get_title_matchpoint({
253 delete $matchpoint->{component}->{subfields};
254 @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
255 is( $keys[0], ' .; thE t[]:,aliS(m)/An\'" Stephen King, Peter Straub.', "Match key correctly returns whole field if no subfields specified" )
258 subtest '_get_match_keys() leader tests' => sub {
260 my $record = MARC::Record->new();
261 my $matchpoint = get_leader_matchpoint({
266 my @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
267 is( $keys[0], ' ', 'Match key correctly calculated as " " from LDR6 when no leader available');
269 $record->leader('01344cam a22003014a 4500');
271 @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
272 is( $keys[0], 'a', 'Match key correctly calculated as "a" from LDR6');
275 sub get_title_matchpoint {
278 my $length = $params->{length} // 0;
279 my $norms = $params->{norms} // [];
280 my $offset = $params->{offset} // 0;
303 sub get_authors_matchpoint {
306 my $length = $params->{length} // 0;
307 my $norms = $params->{norms} // [];
308 my $offset = $params->{offset} // 0;
340 sub get_isbn_matchpoint {
343 my $length = $params->{length} // 0;
344 my $norms = $params->{norms} // [];
345 my $offset = $params->{offset} // 0;
367 sub get_leader_matchpoint {
369 my $length = $params->{length} // 0;
370 my $norms = $params->{norms} // [];
371 my $offset = $params->{offset} // 0;
387 $schema->storage->txn_rollback;