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 => 19;
25 use C4::Members::Attributes;
26 use C4::Members::AttributeTypes;
32 use_ok( "C4::Utils::DataTables::Members" );
34 my $dbh = C4::Context->dbh;
37 $dbh->{AutoCommit} = 0;
38 $dbh->{RaiseError} = 1;
40 # Pick a categorycode from the DB
41 my @categories = C4::Category->all;
42 my $categorycode = $categories[0]->categorycode;
43 # Add a new branch so we control what borrowers it has
44 my $branchcode = "UNC";
46 branchcode => $branchcode,
47 branchname => 'Universidad Nacional de Cordoba',
48 branchaddress1 => 'Haya de la Torre',
49 branchaddress2 => 'S/N',
51 branchcity => 'Cordoba',
52 branchstate => 'Cordoba',
53 branchcountry => 'Argentina'
55 Koha::Library->new( $branch_data )->store;
58 cardnumber => '123456',
61 categorycode => $categorycode,
62 branchcode => $branchcode,
64 dateexpiry => '9999-12-31',
69 cardnumber => '234567',
72 categorycode => $categorycode,
73 branchcode => $branchcode,
75 dateexpiry => '9999-12-31',
76 userid => 'john.smith'
80 cardnumber => '345678',
83 categorycode => $categorycode,
84 branchcode => $branchcode,
86 dateexpiry => '9999-12-31',
90 my %jeanpaul_dupont = (
91 cardnumber => '456789',
92 firstname => 'Jean Paul',
94 categorycode => $categorycode,
95 branchcode => $branchcode,
97 dateexpiry => '9999-12-31',
98 userid => 'jeanpaul.dupont'
101 $john_doe{borrowernumber} = AddMember( %john_doe );
102 warn "Error adding John Doe, check your tests" unless $john_doe{borrowernumber};
103 $john_smith{borrowernumber} = AddMember( %john_smith );
104 warn "Error adding John Smith, check your tests" unless $john_smith{borrowernumber};
105 $jane_doe{borrowernumber} = AddMember( %jane_doe );
106 warn "Error adding Jane Doe, check your tests" unless $jane_doe{borrowernumber};
107 $jeanpaul_dupont{borrowernumber} = AddMember( %jeanpaul_dupont );
108 warn "Error adding Jean Paul Dupont, check your tests" unless $jeanpaul_dupont{borrowernumber};
110 # Set common datatables params
112 iDisplayLength => 10,
117 my $search_results = C4::Utils::DataTables::Members::search({
118 searchmember => "John Doe",
119 searchfieldstype => 'standard',
120 searchtype => 'contain',
121 branchcode => $branchcode,
122 dt_params => \%dt_params
125 is( $search_results->{ iTotalDisplayRecords }, 1,
126 "John Doe has only one match on $branchcode (Bug 12595)");
128 ok( $search_results->{ patrons }[0]->{ cardnumber } eq $john_doe{ cardnumber }
129 && ! $search_results->{ patrons }[1],
130 "John Doe is the only match (Bug 12595)");
133 $search_results = C4::Utils::DataTables::Members::search({
134 searchmember => "Jane Doe",
135 searchfieldstype => 'standard',
136 searchtype => 'contain',
137 branchcode => $branchcode,
138 dt_params => \%dt_params
141 is( $search_results->{ iTotalDisplayRecords }, 1,
142 "Jane Doe has only one match on $branchcode (Bug 12595)");
144 is( $search_results->{ patrons }[0]->{ cardnumber },
145 $jane_doe{ cardnumber },
146 "Jane Doe is the only match (Bug 12595)");
149 $search_results = C4::Utils::DataTables::Members::search({
150 searchmember => "John",
151 searchfieldstype => 'standard',
152 searchtype => 'contain',
153 branchcode => $branchcode,
154 dt_params => \%dt_params
157 is( $search_results->{ iTotalDisplayRecords }, 2,
158 "There are two John at $branchcode");
160 is( $search_results->{ patrons }[0]->{ cardnumber },
161 $john_doe{ cardnumber },
162 "John Doe is the first result");
164 is( $search_results->{ patrons }[1]->{ cardnumber },
165 $john_smith{ cardnumber },
166 "John Smith is the second result");
169 $search_results = C4::Utils::DataTables::Members::search({
170 searchmember => "Doe",
171 searchfieldstype => 'standard',
172 searchtype => 'contain',
173 branchcode => $branchcode,
174 dt_params => \%dt_params
177 is( $search_results->{ iTotalDisplayRecords }, 2,
178 "There are two Doe at $branchcode");
180 is( $search_results->{ patrons }[0]->{ cardnumber },
181 $john_doe{ cardnumber },
182 "John Doe is the first result");
184 is( $search_results->{ patrons }[1]->{ cardnumber },
185 $jane_doe{ cardnumber },
186 "Jane Doe is the second result");
189 $search_results = C4::Utils::DataTables::Members::search({
190 searchmember => "john.doe",
191 searchfieldstype => 'standard',
192 searchtype => 'contain',
193 branchcode => $branchcode,
194 dt_params => \%dt_params
197 is( $search_results->{ iTotalDisplayRecords }, 1,
198 "John Doe is found by userid, standard search (Bug 14782)");
200 $search_results = C4::Utils::DataTables::Members::search({
201 searchmember => "john.doe",
202 searchfieldstype => 'userid',
203 searchtype => 'contain',
204 branchcode => $branchcode,
205 dt_params => \%dt_params
208 is( $search_results->{ iTotalDisplayRecords }, 1,
209 "John Doe is found by userid, userid search (Bug 14782)");
211 my $attribute_type = C4::Members::AttributeTypes->new( 'ATM_1', 'my attribute type' );
212 $attribute_type->{staff_searchable} = 1;
213 $attribute_type->store;
216 C4::Members::Attributes::SetBorrowerAttributes(
217 $john_doe{borrowernumber}, [ { code => $attribute_type->{code}, value => 'the default value for a common user' } ]
219 C4::Members::Attributes::SetBorrowerAttributes(
220 $jane_doe{borrowernumber}, [ { code => $attribute_type->{code}, value => 'the default value for another common user' } ]
223 t::lib::Mocks::mock_preference('ExtendedPatronAttributes', 1);
224 $search_results = C4::Utils::DataTables::Members::search({
225 searchmember => "common user",
226 searchfieldstype => 'standard',
227 searchtype => 'contain',
228 branchcode => $branchcode,
229 dt_params => \%dt_params
232 is( $search_results->{ iTotalDisplayRecords}, 2, "There are 2 common users" );
234 t::lib::Mocks::mock_preference('ExtendedPatronAttributes', 0);
235 $search_results = C4::Utils::DataTables::Members::search({
236 searchmember => "common user",
237 searchfieldstype => 'standard',
238 searchtype => 'contain',
239 branchcode => $branchcode,
240 dt_params => \%dt_params
242 is( $search_results->{ iTotalDisplayRecords}, 0, "There are still 2 common users, but the patron attribute is not searchable " );
244 $search_results = C4::Utils::DataTables::Members::search({
245 searchmember => "Jean Paul",
246 searchfieldstype => 'standard',
247 searchtype => 'start_with',
248 branchcode => $branchcode,
249 dt_params => \%dt_params
252 is( $search_results->{ iTotalDisplayRecords }, 1,
253 "Jean Paul Dupont is found using start with and two terms search 'Jean Paul' (Bug 15252)");
255 $search_results = C4::Utils::DataTables::Members::search({
256 searchmember => "Jean Pau",
257 searchfieldstype => 'standard',
258 searchtype => 'start_with',
259 branchcode => $branchcode,
260 dt_params => \%dt_params
263 is( $search_results->{ iTotalDisplayRecords }, 1,
264 "Jean Paul Dupont is found using start with and two terms search 'Jean Pau' (Bug 15252)");
266 $search_results = C4::Utils::DataTables::Members::search({
267 searchmember => "Jea Pau",
268 searchfieldstype => 'standard',
269 searchtype => 'start_with',
270 branchcode => $branchcode,
271 dt_params => \%dt_params
274 is( $search_results->{ iTotalDisplayRecords }, 0,
275 "Jean Paul Dupont is not found using start with and two terms search 'Jea Pau' (Bug 15252)");
277 $search_results = C4::Utils::DataTables::Members::search({
278 searchmember => "Jea Pau",
279 searchfieldstype => 'standard',
280 searchtype => 'contain',
281 branchcode => $branchcode,
282 dt_params => \%dt_params
285 is( $search_results->{ iTotalDisplayRecords }, 1,
286 "Jean Paul Dupont is found using contains and two terms search 'Jea Pau' (Bug 15252)");