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>.
25 use Module
::Load
::Conditional qw
/check_install/;
28 if ( check_install
( module
=> 'Test::DBIx::Class' ) ) {
31 plan skip_all
=> "Need Test::DBIx::Class"
35 use_ok
('C4::Matcher');
37 use Test
::DBIx
::Class
{
38 schema_class
=> 'Koha::Schema',
39 connect_info
=> ['dbi:SQLite:dbname=:memory:','',''],
40 connect_opts
=> { name_sep
=> '.', quote_char
=> '`', },
41 fixture_class
=> '::Populate',
46 [ 'matcher_id', 'code', 'description', 'record_type', 'threshold' ],
47 [ 1, 'ISBN', 'ISBN', 'red', 1 ],
48 [ 2, 'ISSN', 'ISSN', 'blue', 0 ]
52 my $db = Test
::MockModule
->new('Koha::Database');
53 $db->mock( _new_schema
=> sub { return Schema
(); } );
55 my @matchers = C4
::Matcher
::GetMatcherList
();
57 is
( $matchers[0]->{'matcher_id'}, 1, 'First matcher_id value is 1' );
59 is
( $matchers[1]->{'matcher_id'}, 2, 'Second matcher_id value is 2' );
61 my $matcher_id = C4
::Matcher
::GetMatcherId
('ISBN');
63 is
( $matcher_id, 1, 'testing getmatcherid' );
67 ok
( $testmatcher = C4
::Matcher
->new( 'red', 1 ), 'testing matcher new' );
69 ok
( $testmatcher = C4
::Matcher
->new( 'blue', 0 ), 'testing matcher new' );
71 $testmatcher->threshold(1000);
73 is
( $testmatcher->threshold(), 1000, 'testing threshhold accessor method' );
75 $testmatcher->_id(53);
77 is
( $testmatcher->_id(), 53, 'testing _id accessor' );
79 $testmatcher->code('match on ISBN');
81 is
( $testmatcher->code(), 'match on ISBN', 'testing code accessor' );
83 $testmatcher->description('match on ISSN');
85 is
( $testmatcher->description(), 'match on ISSN', 'testing code accessor' );
87 subtest
'_get_match_keys() tests' => sub {
91 my $matchpoint = get_title_matchpoint
({
97 my $record = MARC
::Record
->new();
98 $record->append_fields(
99 MARC
::Field
->new('100', '1', ' ',
100 a
=> 'King, Stephen',
102 MARC
::Field
->new('245', ' ', ' ',
103 a
=> ' .; thE t[]:,aliS(m)/An\'"',
104 c
=> 'Stephen King, Peter Straub.' ),
105 MARC
::Field
->new('700', ' ', ' ',
106 a
=> 'Straub, Peter',
110 my @keys = C4
::Matcher
::_get_match_keys
( $record, $matchpoint );
112 is
( $keys[0], 'THE TALISMAN STEPHEN KING PETER STRAUB',
113 'Match key correctly calculated with no $norms');
115 $matchpoint = get_title_matchpoint
({
120 @keys = C4
::Matcher
::_get_match_keys
( $record, $matchpoint );
122 'Match key correctly calculated with length 9');
124 $matchpoint = get_title_matchpoint
({
129 @keys = C4
::Matcher
::_get_match_keys
( $record, $matchpoint );
131 'Match key correctly calculated with length 9 and offset 1');
133 $matchpoint = get_title_matchpoint
({
138 @keys = C4
::Matcher
::_get_match_keys
( $record, $matchpoint );
139 is
( $keys[0], 'THE T',
140 'Match key correctly calculated with length 9 and offset 2, should not remove space');
142 $matchpoint = get_authors_matchpoint
({
147 @keys = C4
::Matcher
::_get_match_keys
( $record, $matchpoint );
148 is
( $keys[0], 'STRAUB PETER KING STEPHEN',
149 'Match key correctly calculated with multiple components');
151 $matchpoint = get_authors_matchpoint
({
156 @keys = C4
::Matcher
::_get_match_keys
( $record, $matchpoint );
157 is
( $keys[0], 'STRAUB KING ST',
158 'Match key correctly calculated with multiple components, length 9');
160 $matchpoint = get_authors_matchpoint
({
165 @keys = C4
::Matcher
::_get_match_keys
( $record, $matchpoint );
166 is
( $keys[0], 'STRAUB P KING STE',
167 'Match key correctly calculated with multiple components, length 10');
169 $matchpoint = get_authors_matchpoint
({
174 @keys = C4
::Matcher
::_get_match_keys
( $record, $matchpoint );
175 is
( $keys[0], 'TRAUB PET ING STEPH',
176 'Match key correctly calculated with multiple components, length 10, offset 1');
179 sub get_title_matchpoint
{
183 my $length = $params->{length} // 0;
184 my $norms = $params->{norms
} // [];
185 my $offset = $params->{offset
} // 0;
208 sub get_authors_matchpoint
{
212 my $length = $params->{length} // 0;
213 my $norms = $params->{norms
} // [];
214 my $offset = $params->{offset
} // 0;