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 => 50;
25 use C4::Members::Attributes;
26 use C4::Members::AttributeTypes;
29 use Koha::Patron::Categories;
32 use t::lib::TestBuilder;
36 use_ok( "C4::Utils::DataTables::Members" );
38 my $schema = Koha::Database->new->schema;
39 $schema->storage->txn_begin;
41 my $builder = t::lib::TestBuilder->new;
43 my $library = $builder->build({
47 my $branchcode=$library->{branchcode};
49 my $john_doe = $builder->build({
52 cardnumber => '123456',
55 branchcode => $branchcode,
56 dateofbirth => '1983-03-01',
61 my $john_smith = $builder->build({
64 cardnumber => '234567',
67 branchcode => $branchcode,
68 dateofbirth => '1982-02-01',
69 userid => 'john.smith'
73 my $jane_doe = $builder->build({
76 cardnumber => '345678',
79 branchcode => $branchcode,
80 dateofbirth => '1983-03-01',
84 my $jeanpaul_dupont = $builder->build({
87 cardnumber => '456789',
88 firstname => 'Jean Paul',
90 branchcode => $branchcode,
91 dateofbirth => '1982-02-01',
92 userid => 'jeanpaul.dupont'
95 my $dupont_brown = $builder->build({
98 cardnumber => '567890',
99 firstname => 'Dupont',
101 branchcode => $branchcode,
102 dateofbirth => '1979-01-01',
103 userid => 'dupont.brown'
107 # Set common datatables params
109 iDisplayLength => 10,
114 my $search_results = C4::Utils::DataTables::Members::search({
115 searchmember => "John Doe",
116 searchfieldstype => 'standard',
117 searchtype => 'contain',
118 branchcode => $branchcode,
119 dt_params => \%dt_params
122 is( $search_results->{ iTotalDisplayRecords }, 1,
123 "John Doe has only one match on $branchcode (Bug 12595)");
125 ok( $search_results->{ patrons }[0]->{ cardnumber } eq $john_doe->{ cardnumber }
126 && ! $search_results->{ patrons }[1],
127 "John Doe is the only match (Bug 12595)");
130 $search_results = C4::Utils::DataTables::Members::search({
131 searchmember => "Jane Doe",
132 searchfieldstype => 'standard',
133 searchtype => 'contain',
134 branchcode => $branchcode,
135 dt_params => \%dt_params
138 is( $search_results->{ iTotalDisplayRecords }, 1,
139 "Jane Doe has only one match on $branchcode (Bug 12595)");
141 is( $search_results->{ patrons }[0]->{ cardnumber },
142 $jane_doe->{ cardnumber },
143 "Jane Doe is the only match (Bug 12595)");
146 $search_results = C4::Utils::DataTables::Members::search({
147 searchmember => "John",
148 searchfieldstype => 'standard',
149 searchtype => 'contain',
150 branchcode => $branchcode,
151 dt_params => \%dt_params
154 is( $search_results->{ iTotalDisplayRecords }, 2,
155 "There are two John at $branchcode");
157 is( $search_results->{ patrons }[0]->{ cardnumber },
158 $john_doe->{ cardnumber },
159 "John Doe is the first result");
161 is( $search_results->{ patrons }[1]->{ cardnumber },
162 $john_smith->{ cardnumber },
163 "John Smith is the second result");
166 $search_results = C4::Utils::DataTables::Members::search({
167 searchmember => "Doe",
168 searchfieldstype => 'standard',
169 searchtype => 'contain',
170 branchcode => $branchcode,
171 dt_params => \%dt_params
174 is( $search_results->{ iTotalDisplayRecords }, 2,
175 "There are two Doe at $branchcode");
177 is( $search_results->{ patrons }[0]->{ cardnumber },
178 $john_doe->{ cardnumber },
179 "John Doe is the first result");
181 is( $search_results->{ patrons }[1]->{ cardnumber },
182 $jane_doe->{ cardnumber },
183 "Jane Doe is the second result");
185 # Search "Smith" as surname - there is only one occurrence of Smith
186 $search_results = C4::Utils::DataTables::Members::search({
187 searchmember => "Smith",
188 searchfieldstype => 'surname',
189 searchtype => 'contain',
190 branchcode => $branchcode,
191 dt_params => \%dt_params
194 is( $search_results->{ iTotalDisplayRecords }, 1,
195 "There is one Smith at $branchcode when searching for surname");
197 is( $search_results->{ patrons }[0]->{ cardnumber },
198 $john_smith->{ cardnumber },
199 "John Smith is the first result");
201 # Search "Dupont" as surname - Dupont is used both as firstname and surname, we
202 # Should only fin d the user with Dupont as surname
203 $search_results = C4::Utils::DataTables::Members::search({
204 searchmember => "Dupont",
205 searchfieldstype => 'surname',
206 searchtype => 'contain',
207 branchcode => $branchcode,
208 dt_params => \%dt_params
211 is( $search_results->{ iTotalDisplayRecords }, 1,
212 "There is one Dupont at $branchcode when searching for surname");
214 is( $search_results->{ patrons }[0]->{ cardnumber },
215 $jeanpaul_dupont->{ cardnumber },
216 "Jean Paul Dupont is the first result");
218 # Search "Doe" as surname - Doe is used twice as surname
219 $search_results = C4::Utils::DataTables::Members::search({
220 searchmember => "Doe",
221 searchfieldstype => 'surname',
222 searchtype => 'contain',
223 branchcode => $branchcode,
224 dt_params => \%dt_params
227 is( $search_results->{ iTotalDisplayRecords }, 2,
228 "There are two Doe at $branchcode when searching for surname");
230 is( $search_results->{ patrons }[0]->{ cardnumber },
231 $john_doe->{ cardnumber },
232 "John Doe is the first result");
234 is( $search_results->{ patrons }[1]->{ cardnumber },
235 $jane_doe->{ cardnumber },
236 "Jane Doe is the second result");
239 $search_results = C4::Utils::DataTables::Members::search({
240 searchmember => "john.doe",
241 searchfieldstype => 'standard',
242 searchtype => 'contain',
243 branchcode => $branchcode,
244 dt_params => \%dt_params
247 is( $search_results->{ iTotalDisplayRecords }, 1,
248 "John Doe is found by userid, standard search (Bug 14782)");
250 $search_results = C4::Utils::DataTables::Members::search({
251 searchmember => "john.doe",
252 searchfieldstype => 'userid',
253 searchtype => 'contain',
254 branchcode => $branchcode,
255 dt_params => \%dt_params
258 is( $search_results->{ iTotalDisplayRecords }, 1,
259 "John Doe is found by userid, userid search (Bug 14782)");
261 $search_results = C4::Utils::DataTables::Members::search({
262 searchmember => "john.doe",
263 searchfieldstype => 'surname',
264 searchtype => 'contain',
265 branchcode => $branchcode,
266 dt_params => \%dt_params
269 is( $search_results->{ iTotalDisplayRecords }, 0,
270 "No members are found by userid, surname search");
272 my $attribute_type = C4::Members::AttributeTypes->new( 'ATM_1', 'my attribute type' );
273 $attribute_type->{staff_searchable} = 1;
274 $attribute_type->store;
277 C4::Members::Attributes::SetBorrowerAttributes(
278 $john_doe->{borrowernumber}, [ { code => $attribute_type->{code}, value => 'the default value for a common user' } ]
280 C4::Members::Attributes::SetBorrowerAttributes(
281 $jane_doe->{borrowernumber}, [ { code => $attribute_type->{code}, value => 'the default value for another common user' } ]
283 C4::Members::Attributes::SetBorrowerAttributes(
284 $john_smith->{borrowernumber}, [ { code => $attribute_type->{code}, value => 'Attribute which not appears even if contains "Dupont"' } ]
287 t::lib::Mocks::mock_preference('ExtendedPatronAttributes', 1);
288 $search_results = C4::Utils::DataTables::Members::search({
289 searchmember => "common user",
290 searchfieldstype => 'standard',
291 searchtype => 'contain',
292 branchcode => $branchcode,
293 dt_params => \%dt_params
296 is( $search_results->{ iTotalDisplayRecords}, 2, "There are 2 common users" );
298 t::lib::Mocks::mock_preference('ExtendedPatronAttributes', 0);
299 $search_results = C4::Utils::DataTables::Members::search({
300 searchmember => "common user",
301 searchfieldstype => 'standard',
302 searchtype => 'contain',
303 branchcode => $branchcode,
304 dt_params => \%dt_params
306 is( $search_results->{ iTotalDisplayRecords}, 0, "There are still 2 common users, but the patron attribute is not searchable " );
308 $search_results = C4::Utils::DataTables::Members::search({
309 searchmember => "Jean Paul",
310 searchfieldstype => 'standard',
311 searchtype => 'start_with',
312 branchcode => $branchcode,
313 dt_params => \%dt_params
316 is( $search_results->{ iTotalDisplayRecords }, 1,
317 "Jean Paul Dupont is found using start with and two terms search 'Jean Paul' (Bug 15252)");
319 $search_results = C4::Utils::DataTables::Members::search({
320 searchmember => "Jean Pau",
321 searchfieldstype => 'standard',
322 searchtype => 'start_with',
323 branchcode => $branchcode,
324 dt_params => \%dt_params
327 is( $search_results->{ iTotalDisplayRecords }, 1,
328 "Jean Paul Dupont is found using start with and two terms search 'Jean Pau' (Bug 15252)");
330 $search_results = C4::Utils::DataTables::Members::search({
331 searchmember => "Jea Pau",
332 searchfieldstype => 'standard',
333 searchtype => 'start_with',
334 branchcode => $branchcode,
335 dt_params => \%dt_params
338 is( $search_results->{ iTotalDisplayRecords }, 0,
339 "Jean Paul Dupont is not found using start with and two terms search 'Jea Pau' (Bug 15252)");
341 $search_results = C4::Utils::DataTables::Members::search({
342 searchmember => "Jea Pau",
343 searchfieldstype => 'standard',
344 searchtype => 'contain',
345 branchcode => $branchcode,
346 dt_params => \%dt_params
349 is( $search_results->{ iTotalDisplayRecords }, 1,
350 "Jean Paul Dupont is found using contains and two terms search 'Jea Pau' (Bug 15252)");
352 my @datetimeprefs = ("dmydot","iso","metric","us");
353 my %dates_in_pref = (
354 dmydot => ["01.02.1982","01.03.1983","01.01.1979","01.01.1988"],
355 iso => ["1982-02-01","1983-03-01","1979-01-01","1988-01-01"],
356 metric => ["01/02/1982","01/03/1983","01/01/1979","01/01/1988"],
357 us => ["02/01/1982","03/01/1983","01/01/1979","01/01/1988"],
359 foreach my $dateformloo (@datetimeprefs){
360 t::lib::Mocks::mock_preference('dateformat', $dateformloo);
361 t::lib::Mocks::mock_preference('DefaultPatronSearchFields', 'surname,firstname,othernames,userid,dateofbirth');
362 $search_results = C4::Utils::DataTables::Members::search({
363 searchmember => $dates_in_pref{$dateformloo}[0],
364 searchfieldstype => 'standard',
365 searchtype => 'contain',
366 branchcode => $branchcode,
367 dt_params => \%dt_params
370 is( $search_results->{ iTotalDisplayRecords }, 2,
371 "dateformat: $dateformloo Two borrowers have dob $dates_in_pref{$dateformloo}[0], standard search fields plus dob works");
373 $search_results = C4::Utils::DataTables::Members::search({
374 searchmember => $dates_in_pref{$dateformloo}[2],
375 searchfieldstype => 'standard',
376 searchtype => 'contain',
377 branchcode => $branchcode,
378 dt_params => \%dt_params
381 is( $search_results->{ iTotalDisplayRecords }, 1,
382 "dateformat: $dateformloo One borrower has dob $dates_in_pref{$dateformloo}[2], standard search fields plus dob works");
384 $search_results = C4::Utils::DataTables::Members::search({
385 searchmember => $dates_in_pref{$dateformloo}[1],
386 searchfieldstype => 'dateofbirth',
387 searchtype => 'contain',
388 branchcode => $branchcode,
389 dt_params => \%dt_params
392 is( $search_results->{ iTotalDisplayRecords }, 2,
393 "dateformat: $dateformloo Two borrowers have dob $dates_in_pref{$dateformloo}[1], dateofbirth search field works");
395 $search_results = C4::Utils::DataTables::Members::search({
396 searchmember => $dates_in_pref{$dateformloo}[3],
397 searchfieldstype => 'dateofbirth',
398 searchtype => 'contain',
399 branchcode => $branchcode,
400 dt_params => \%dt_params
403 is( $search_results->{ iTotalDisplayRecords }, 0,
404 "dateformat: $dateformloo No borrowers have dob $dates_in_pref{$dateformloo}[3], dateofbirth search field works");
406 $search_results = C4::Utils::DataTables::Members::search({
407 searchmember => $dates_in_pref{$dateformloo}[3],
408 searchfieldstype => 'standard',
409 searchtype => 'contain',
410 branchcode => $branchcode,
411 dt_params => \%dt_params
414 is( $search_results->{ iTotalDisplayRecords }, 0,
415 "dateformat: $dateformloo No borrowers have dob $dates_in_pref{$dateformloo}[3], standard search fields plus dob works");
418 # Date of birth formatting
419 t::lib::Mocks::mock_preference('dateformat', 'metric');
420 $search_results = C4::Utils::DataTables::Members::search({
421 searchmember => "01/02/1982",
422 searchfieldstype => 'dateofbirth',
423 dt_params => \%dt_params
425 is( $search_results->{ iTotalDisplayRecords }, 2,
426 "Sarching by date of birth should handle date formatted given the dateformat pref");
427 $search_results = C4::Utils::DataTables::Members::search({
428 searchmember => "1982-02-01",
429 searchfieldstype => 'dateofbirth',
430 dt_params => \%dt_params
432 is( $search_results->{ iTotalDisplayRecords }, 2,
433 "Sarching by date of birth should handle date formatted in iso");
435 subtest 'ExtendedPatronAttributes' => sub {
437 t::lib::Mocks::mock_preference('ExtendedPatronAttributes', 1);
438 $search_results = C4::Utils::DataTables::Members::search({
439 searchmember => "Dupont",
440 searchfieldstype => 'standard',
441 searchtype => 'contain',
442 branchcode => $branchcode,
443 dt_params => \%dt_params
446 is( $search_results->{ iTotalDisplayRecords }, 3,
447 "'Dupont' is contained in 2 surnames and a patron attribute. Patron attribute one should be displayed if searching in all fields (Bug 18094)");
449 $search_results = C4::Utils::DataTables::Members::search({
450 searchmember => "Dupont",
451 searchfieldstype => 'surname',
452 searchtype => 'contain',
453 branchcode => $branchcode,
454 dt_params => \%dt_params
457 is( $search_results->{ iTotalDisplayRecords }, 1,
458 "'Dupont' is contained in 2 surnames and a patron attribute. Patron attribute one should not be displayed if searching in specific fields (Bug 18094)");
462 $schema->storage->txn_rollback;