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');
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('100', '1', ' ',
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',
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({
113 norms => [ 'legacy_default' ],
116 @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
118 'Match key correctly calculated with length 9');
120 $matchpoint = get_title_matchpoint({
122 norms => [ 'legacy_default' ],
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({
131 norms => [ 'legacy_default' ],
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({
140 norms => [ 'legacy_default' ],
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({
149 norms => [ 'legacy_default' ],
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({
158 norms => [ 'legacy_default' ],
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({
167 norms => [ 'legacy_default' ],
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({
176 norms => [ 'none', 'none' ],
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({
185 norms => [ 'upper_case' ],
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({
194 norms => [ 'lower_case' ],
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({
203 norms => [ 'remove_spaces' ],
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({
212 norms => [ 'remove_spaces', 'lower_case' ],
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({
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({
235 norms => [ $norm, 'upper_case' ],
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 {
251 my $length = $params->{length} // 0;
252 my $norms = $params->{norms} // [];
253 my $offset = $params->{offset} // 0;
276 sub get_authors_matchpoint {
280 my $length = $params->{length} // 0;
281 my $norms = $params->{norms} // [];
282 my $offset = $params->{offset} // 0;