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 => 51;
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 $patron = $builder->build_object({ class => 'Koha::Patrons', value => { flags => 1 } });
48 t::lib::Mocks::mock_userenv({ patron => $patron });
50 my $branchcode=$library->{branchcode};
52 my $john_doe = $builder->build({
55 cardnumber => '123456',
58 branchcode => $branchcode,
59 dateofbirth => '1983-03-01',
65 my $john_smith = $builder->build({
68 cardnumber => '234567',
71 branchcode => $branchcode,
72 dateofbirth => '1982-02-01',
73 userid => 'john.smith'
77 my $jane_doe = $builder->build({
80 cardnumber => '345678',
83 branchcode => $branchcode,
84 dateofbirth => '1983-03-01',
88 my $jeanpaul_dupont = $builder->build({
91 cardnumber => '456789',
92 firstname => 'Jean Paul',
94 branchcode => $branchcode,
95 dateofbirth => '1982-02-01',
96 userid => 'jeanpaul.dupont'
99 my $dupont_brown = $builder->build({
100 source => "Borrower",
102 cardnumber => '567890',
103 firstname => 'Dupont',
105 branchcode => $branchcode,
106 dateofbirth => '1979-01-01',
107 userid => 'dupont.brown'
111 # Set common datatables params
113 iDisplayLength => 10,
117 t::lib::Mocks::mock_preference('DefaultPatronSearchFields', '');
120 my $search_results = C4::Utils::DataTables::Members::search({
121 searchmember => "John Doe",
122 searchfieldstype => 'standard',
123 searchtype => 'contain',
124 branchcode => $branchcode,
125 dt_params => \%dt_params
128 is( $search_results->{ iTotalDisplayRecords }, 1,
129 "John Doe has only one match on $branchcode (Bug 12595)");
131 ok( $search_results->{ patrons }[0]->{ cardnumber } eq $john_doe->{ cardnumber }
132 && ! $search_results->{ patrons }[1],
133 "John Doe is the only match (Bug 12595)");
136 $search_results = C4::Utils::DataTables::Members::search({
137 searchmember => "Jane Doe",
138 searchfieldstype => 'standard',
139 searchtype => 'contain',
140 branchcode => $branchcode,
141 dt_params => \%dt_params
144 is( $search_results->{ iTotalDisplayRecords }, 1,
145 "Jane Doe has only one match on $branchcode (Bug 12595)");
147 is( $search_results->{ patrons }[0]->{ cardnumber },
148 $jane_doe->{ cardnumber },
149 "Jane Doe is the only match (Bug 12595)");
152 $search_results = C4::Utils::DataTables::Members::search({
153 searchmember => "John",
154 searchfieldstype => 'standard',
155 searchtype => 'contain',
156 branchcode => $branchcode,
157 dt_params => \%dt_params
160 is( $search_results->{ iTotalDisplayRecords }, 2,
161 "There are two John at $branchcode");
163 is( $search_results->{ patrons }[0]->{ cardnumber },
164 $john_doe->{ cardnumber },
165 "John Doe is the first result");
167 is( $search_results->{ patrons }[1]->{ cardnumber },
168 $john_smith->{ cardnumber },
169 "John Smith is the second result");
172 $search_results = C4::Utils::DataTables::Members::search({
173 searchmember => "Doe",
174 searchfieldstype => 'standard',
175 searchtype => 'contain',
176 branchcode => $branchcode,
177 dt_params => \%dt_params
180 is( $search_results->{ iTotalDisplayRecords }, 2,
181 "There are two Doe at $branchcode");
183 is( $search_results->{ patrons }[0]->{ cardnumber },
184 $john_doe->{ cardnumber },
185 "John Doe is the first result");
187 is( $search_results->{ patrons }[1]->{ cardnumber },
188 $jane_doe->{ cardnumber },
189 "Jane Doe is the second result");
191 # Search "Smith" as surname - there is only one occurrence of Smith
192 $search_results = C4::Utils::DataTables::Members::search({
193 searchmember => "Smith",
194 searchfieldstype => 'surname',
195 searchtype => 'contain',
196 branchcode => $branchcode,
197 dt_params => \%dt_params
200 is( $search_results->{ iTotalDisplayRecords }, 1,
201 "There is one Smith at $branchcode when searching for surname");
203 is( $search_results->{ patrons }[0]->{ cardnumber },
204 $john_smith->{ cardnumber },
205 "John Smith is the first result");
207 # Search "Dupont" as surname - Dupont is used both as firstname and surname, we
208 # Should only fin d the user with Dupont as surname
209 $search_results = C4::Utils::DataTables::Members::search({
210 searchmember => "Dupont",
211 searchfieldstype => 'surname',
212 searchtype => 'contain',
213 branchcode => $branchcode,
214 dt_params => \%dt_params
217 is( $search_results->{ iTotalDisplayRecords }, 1,
218 "There is one Dupont at $branchcode when searching for surname");
220 is( $search_results->{ patrons }[0]->{ cardnumber },
221 $jeanpaul_dupont->{ cardnumber },
222 "Jean Paul Dupont is the first result");
224 # Search "Doe" as surname - Doe is used twice as surname
225 $search_results = C4::Utils::DataTables::Members::search({
226 searchmember => "Doe",
227 searchfieldstype => 'surname',
228 searchtype => 'contain',
229 branchcode => $branchcode,
230 dt_params => \%dt_params
233 is( $search_results->{ iTotalDisplayRecords }, 2,
234 "There are two Doe at $branchcode when searching for surname");
236 is( $search_results->{ patrons }[0]->{ cardnumber },
237 $john_doe->{ cardnumber },
238 "John Doe is the first result");
240 is( $search_results->{ patrons }[1]->{ cardnumber },
241 $jane_doe->{ cardnumber },
242 "Jane Doe is the second result");
245 $search_results = C4::Utils::DataTables::Members::search({
246 searchmember => "john.doe",
247 searchfieldstype => 'standard',
248 searchtype => 'contain',
249 branchcode => $branchcode,
250 dt_params => \%dt_params
253 is( $search_results->{ iTotalDisplayRecords }, 1,
254 "John Doe is found by userid, standard search (Bug 14782)");
256 $search_results = C4::Utils::DataTables::Members::search({
257 searchmember => "john.doe",
258 searchfieldstype => 'userid',
259 searchtype => 'contain',
260 branchcode => $branchcode,
261 dt_params => \%dt_params
264 is( $search_results->{ iTotalDisplayRecords }, 1,
265 "John Doe is found by userid, userid search (Bug 14782)");
267 $search_results = C4::Utils::DataTables::Members::search({
268 searchmember => "john.doe",
269 searchfieldstype => 'surname',
270 searchtype => 'contain',
271 branchcode => $branchcode,
272 dt_params => \%dt_params
275 is( $search_results->{ iTotalDisplayRecords }, 0,
276 "No members are found by userid, surname search");
278 my $attribute_type = C4::Members::AttributeTypes->new( 'ATM_1', 'my attribute type' );
279 $attribute_type->{staff_searchable} = 1;
280 $attribute_type->store;
283 C4::Members::Attributes::SetBorrowerAttributes(
284 $john_doe->{borrowernumber}, [ { code => $attribute_type->{code}, value => 'the default value for a common user' } ]
286 C4::Members::Attributes::SetBorrowerAttributes(
287 $jane_doe->{borrowernumber}, [ { code => $attribute_type->{code}, value => 'the default value for another common user' } ]
289 C4::Members::Attributes::SetBorrowerAttributes(
290 $john_smith->{borrowernumber}, [ { code => $attribute_type->{code}, value => 'Attribute which not appears even if contains "Dupont"' } ]
293 t::lib::Mocks::mock_preference('ExtendedPatronAttributes', 1);
294 $search_results = C4::Utils::DataTables::Members::search({
295 searchmember => "common user",
296 searchfieldstype => 'standard',
297 searchtype => 'contain',
298 branchcode => $branchcode,
299 dt_params => \%dt_params
302 is( $search_results->{ iTotalDisplayRecords}, 2, "There are 2 common users" );
304 t::lib::Mocks::mock_preference('ExtendedPatronAttributes', 0);
305 $search_results = C4::Utils::DataTables::Members::search({
306 searchmember => "common user",
307 searchfieldstype => 'standard',
308 searchtype => 'contain',
309 branchcode => $branchcode,
310 dt_params => \%dt_params
312 is( $search_results->{ iTotalDisplayRecords}, 0, "There are still 2 common users, but the patron attribute is not searchable " );
314 $search_results = C4::Utils::DataTables::Members::search({
315 searchmember => "Jean Paul",
316 searchfieldstype => 'standard',
317 searchtype => 'start_with',
318 branchcode => $branchcode,
319 dt_params => \%dt_params
322 is( $search_results->{ iTotalDisplayRecords }, 1,
323 "Jean Paul Dupont is found using start with and two terms search 'Jean Paul' (Bug 15252)");
325 $search_results = C4::Utils::DataTables::Members::search({
326 searchmember => "Jean Pau",
327 searchfieldstype => 'standard',
328 searchtype => 'start_with',
329 branchcode => $branchcode,
330 dt_params => \%dt_params
333 is( $search_results->{ iTotalDisplayRecords }, 1,
334 "Jean Paul Dupont is found using start with and two terms search 'Jean Pau' (Bug 15252)");
336 $search_results = C4::Utils::DataTables::Members::search({
337 searchmember => "Jea Pau",
338 searchfieldstype => 'standard',
339 searchtype => 'start_with',
340 branchcode => $branchcode,
341 dt_params => \%dt_params
344 is( $search_results->{ iTotalDisplayRecords }, 0,
345 "Jean Paul Dupont is not found using start with and two terms search 'Jea Pau' (Bug 15252)");
347 $search_results = C4::Utils::DataTables::Members::search({
348 searchmember => "Jea Pau",
349 searchfieldstype => 'standard',
350 searchtype => 'contain',
351 branchcode => $branchcode,
352 dt_params => \%dt_params
355 is( $search_results->{ iTotalDisplayRecords }, 1,
356 "Jean Paul Dupont is found using contains and two terms search 'Jea Pau' (Bug 15252)");
358 my @datetimeprefs = ("dmydot","iso","metric","us");
359 my %dates_in_pref = (
360 dmydot => ["01.02.1982","01.03.1983","01.01.1979","01.01.1988"],
361 iso => ["1982-02-01","1983-03-01","1979-01-01","1988-01-01"],
362 metric => ["01/02/1982","01/03/1983","01/01/1979","01/01/1988"],
363 us => ["02/01/1982","03/01/1983","01/01/1979","01/01/1988"],
365 foreach my $dateformloo (@datetimeprefs){
366 t::lib::Mocks::mock_preference('dateformat', $dateformloo);
367 t::lib::Mocks::mock_preference('DefaultPatronSearchFields', 'surname,firstname,othernames,userid,dateofbirth');
368 $search_results = C4::Utils::DataTables::Members::search({
369 searchmember => $dates_in_pref{$dateformloo}[0],
370 searchfieldstype => 'standard',
371 searchtype => 'contain',
372 branchcode => $branchcode,
373 dt_params => \%dt_params
376 is( $search_results->{ iTotalDisplayRecords }, 2,
377 "dateformat: $dateformloo Two borrowers have dob $dates_in_pref{$dateformloo}[0], standard search fields plus dob works");
379 $search_results = C4::Utils::DataTables::Members::search({
380 searchmember => $dates_in_pref{$dateformloo}[2],
381 searchfieldstype => 'standard',
382 searchtype => 'contain',
383 branchcode => $branchcode,
384 dt_params => \%dt_params
387 is( $search_results->{ iTotalDisplayRecords }, 1,
388 "dateformat: $dateformloo One borrower has dob $dates_in_pref{$dateformloo}[2], standard search fields plus dob works");
390 $search_results = C4::Utils::DataTables::Members::search({
391 searchmember => $dates_in_pref{$dateformloo}[1],
392 searchfieldstype => 'dateofbirth',
393 searchtype => 'contain',
394 branchcode => $branchcode,
395 dt_params => \%dt_params
398 is( $search_results->{ iTotalDisplayRecords }, 2,
399 "dateformat: $dateformloo Two borrowers have dob $dates_in_pref{$dateformloo}[1], dateofbirth search field works");
401 $search_results = C4::Utils::DataTables::Members::search({
402 searchmember => $dates_in_pref{$dateformloo}[3],
403 searchfieldstype => 'dateofbirth',
404 searchtype => 'contain',
405 branchcode => $branchcode,
406 dt_params => \%dt_params
409 is( $search_results->{ iTotalDisplayRecords }, 0,
410 "dateformat: $dateformloo No borrowers have dob $dates_in_pref{$dateformloo}[3], dateofbirth search field works");
412 $search_results = C4::Utils::DataTables::Members::search({
413 searchmember => $dates_in_pref{$dateformloo}[3],
414 searchfieldstype => 'standard',
415 searchtype => 'contain',
416 branchcode => $branchcode,
417 dt_params => \%dt_params
420 is( $search_results->{ iTotalDisplayRecords }, 0,
421 "dateformat: $dateformloo No borrowers have dob $dates_in_pref{$dateformloo}[3], standard search fields plus dob works");
424 # Date of birth formatting
425 t::lib::Mocks::mock_preference('dateformat', 'metric');
426 $search_results = C4::Utils::DataTables::Members::search({
427 searchmember => "01/02/1982",
428 searchfieldstype => 'dateofbirth',
429 dt_params => \%dt_params
431 is( $search_results->{ iTotalDisplayRecords }, 2,
432 "Sarching by date of birth should handle date formatted given the dateformat pref");
433 $search_results = C4::Utils::DataTables::Members::search({
434 searchmember => "1982-02-01",
435 searchfieldstype => 'dateofbirth',
436 dt_params => \%dt_params
438 is( $search_results->{ iTotalDisplayRecords }, 2,
439 "Sarching by date of birth should handle date formatted in iso");
441 subtest 'ExtendedPatronAttributes' => sub {
443 t::lib::Mocks::mock_preference('ExtendedPatronAttributes', 1);
444 $search_results = C4::Utils::DataTables::Members::search({
445 searchmember => "Dupont",
446 searchfieldstype => 'standard',
447 searchtype => 'contain',
448 branchcode => $branchcode,
449 dt_params => \%dt_params
452 is( $search_results->{ iTotalDisplayRecords }, 3,
453 "'Dupont' is contained in 2 surnames and a patron attribute. Patron attribute one should be displayed if searching in all fields (Bug 18094)");
455 $search_results = C4::Utils::DataTables::Members::search({
456 searchmember => "Dupont",
457 searchfieldstype => 'surname',
458 searchtype => 'contain',
459 branchcode => $branchcode,
460 dt_params => \%dt_params
463 is( $search_results->{ iTotalDisplayRecords }, 1,
464 "'Dupont' is contained in 2 surnames and a patron attribute. Patron attribute one should not be displayed if searching in specific fields (Bug 18094)");
467 subtest 'Search by any borrowers field (bug 17374)' => sub {
470 my $search_results = C4::Utils::DataTables::Members::search({
471 searchmember => "pacman",
472 searchfieldstype => 'initials',
473 searchtype => 'contain',
474 branchcode => $branchcode,
475 dt_params => \%dt_params
477 is( $search_results->{ iTotalDisplayRecords }, 1, "We find only 1 patron when searching for initials 'pacman'" );
479 is( $search_results->{ patrons }[0]->{ cardnumber }, $john_doe->{cardnumber}, "We find the correct patron when sesrching by initials" )
483 $schema->storage->txn_rollback;