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_ok('C4::Matcher');
38 use Test::DBIx::Class {
39 schema_class => 'Koha::Schema',
40 connect_info => ['dbi:SQLite:dbname=:memory:','',''],
41 connect_opts => { name_sep => '.', quote_char => '`', },
42 fixture_class => '::Populate',
47 [ 'matcher_id', 'code', 'description', 'record_type', 'threshold' ],
48 [ 1, 'ISBN', 'ISBN', 'red', 1 ],
49 [ 2, 'ISSN', 'ISSN', 'blue', 0 ]
53 my $db = Test::MockModule->new('Koha::Database');
54 $db->mock( _new_schema => sub { return Schema(); } );
56 my @matchers = C4::Matcher::GetMatcherList();
58 is( $matchers[0]->{'matcher_id'}, 1, 'First matcher_id value is 1' );
60 is( $matchers[1]->{'matcher_id'}, 2, 'Second matcher_id value is 2' );
62 my $matcher_id = C4::Matcher::GetMatcherId('ISBN');
64 is( $matcher_id, 1, 'testing getmatcherid' );
68 ok( $testmatcher = C4::Matcher->new( 'red', 1 ), 'testing matcher new' );
70 ok( $testmatcher = C4::Matcher->new( 'blue', 0 ), 'testing matcher new' );
72 $testmatcher->threshold(1000);
74 is( $testmatcher->threshold(), 1000, 'testing threshhold accessor method' );
76 $testmatcher->_id(53);
78 is( $testmatcher->_id(), 53, 'testing _id accessor' );
80 $testmatcher->code('match on ISBN');
82 is( $testmatcher->code(), 'match on ISBN', 'testing code accessor' );
84 $testmatcher->description('match on ISSN');
86 is( $testmatcher->description(), 'match on ISSN', 'testing code accessor' );
88 subtest '_get_match_keys() tests' => sub {
92 my $matchpoint = get_title_matchpoint({
94 norms => [ 'legacy_default' ],
98 my $record = MARC::Record->new();
99 $record->append_fields(
100 MARC::Field->new('100', '1', ' ',
101 a => 'King, Stephen',
103 MARC::Field->new('245', ' ', ' ',
104 a => ' .; thE t[]:,aliS(m)/An\'"',
105 c => 'Stephen King, Peter Straub.' ),
106 MARC::Field->new('700', ' ', ' ',
107 a => 'Straub, Peter',
111 my @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
113 is( $keys[0], 'THE TALISMAN STEPHEN KING PETER STRAUB',
114 'Match key correctly calculated with no $norms');
116 $matchpoint = get_title_matchpoint({
118 norms => [ 'legacy_default' ],
121 @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
123 'Match key correctly calculated with length 9');
125 $matchpoint = get_title_matchpoint({
127 norms => [ 'legacy_default' ],
130 @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
131 is( $keys[0], 'THE T',
132 'Match key correctly calculated with length 9 and offset 1');
134 $matchpoint = get_title_matchpoint({
136 norms => [ 'legacy_default' ],
139 @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
140 is( $keys[0], 'THE T',
141 'Match key correctly calculated with length 9 and offset 2, should not remove space');
143 $matchpoint = get_authors_matchpoint({
145 norms => [ 'legacy_default' ],
148 @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
149 is( $keys[0], 'STRAUB PETER KING STEPHEN',
150 'Match key correctly calculated with multiple components');
152 $matchpoint = get_authors_matchpoint({
154 norms => [ 'legacy_default' ],
157 @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
158 is( $keys[0], 'STRAUB P KING STE',
159 'Match key correctly calculated with multiple components, length 9');
161 $matchpoint = get_authors_matchpoint({
163 norms => [ 'legacy_default' ],
166 @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
167 is( $keys[0], 'STRAUB PE KING STEP',
168 'Match key correctly calculated with multiple components, length 10');
170 $matchpoint = get_authors_matchpoint({
172 norms => [ 'legacy_default' ],
175 @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
176 is( $keys[0], 'RAUB PETE NG STEPHE',
177 'Match key correctly calculated with multiple components, length 10, offset 1');
179 $matchpoint = get_title_matchpoint({
181 norms => [ 'none', 'none' ],
184 @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
185 is( $keys[0], ' .; thE t[]:,aliS(m)/An\'" Stephen King, Peter Straub.',
186 'Match key intact if \'none\' specified, length 0 and offset 0' );
188 $matchpoint = get_authors_matchpoint({
190 norms => [ 'upper_case' ],
193 @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
194 is( $keys[0], 'STRAUB, PETER KING, STEPHEN',
195 'Match key correctly calculated with multiple components, \'upper_case\' norm');
197 $matchpoint = get_authors_matchpoint({
199 norms => [ 'lower_case' ],
202 @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
203 is( $keys[0], 'straub, peter king, stephen',
204 'Match key correctly calculated with multiple components, \'lower_case\' norm');
206 $matchpoint = get_authors_matchpoint({
208 norms => [ 'remove_spaces' ],
211 @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
212 is( $keys[0], 'Straub,Peter King,Stephen',
213 'Match key correctly calculated with multiple components, \'remove_spaces\' norm');
215 $matchpoint = get_authors_matchpoint({
217 norms => [ 'remove_spaces', 'lower_case' ],
220 @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
221 is( $keys[0], 'straub,peter king,stephen',
222 'Match key correctly calculated with multiple components, \'remove_spaces\' and \'lower_case\' norm');
224 my $norm = 'unknown_norm';
225 $matchpoint = get_title_matchpoint({
231 { @keys = C4::Matcher::_get_match_keys( $record, $matchpoint ) }
232 qq{Invalid normalization routine required ($norm)},
233 'Passing an invalid normalization routine name raises a warning';
235 is( $keys[0], ' .; thE t[]:,aliS(m)/An\'" Stephen King, Peter Straub.',
236 'Match key intact if invalid normalization routine specified' );
238 $matchpoint = get_title_matchpoint({
240 norms => [ $norm, 'upper_case' ],
244 { @keys = C4::Matcher::_get_match_keys( $record, $matchpoint ) }
245 qq{Invalid normalization routine required ($norm)},
246 'Passing an invalid normalization routine name raises a warning';
248 is( $keys[0], ' .; THE T[]:,ALIS(M)/AN\'" STEPHEN KING, PETER STRAUB.',
249 'Match key correctly normalized if invalid normalization routine specified' );
252 sub get_title_matchpoint {
256 my $length = $params->{length} // 0;
257 my $norms = $params->{norms} // [];
258 my $offset = $params->{offset} // 0;
281 sub get_authors_matchpoint {
285 my $length = $params->{length} // 0;
286 my $norms = $params->{norms} // [];
287 my $offset = $params->{offset} // 0;