Bug 18089 - All XSLT testing singleBranchMode = 0 fails to show even if install has...
[koha.git] / t / db_dependent / Utils / Datatables_Members.t
blob1d590a52797c3a74795e5ced37010421dd58ecac
1 #!/usr/bin/perl
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>.
18 use Modern::Perl;
20 use Test::More tests => 49;
22 use C4::Context;
23 use C4::Members;
25 use C4::Members::Attributes;
26 use C4::Members::AttributeTypes;
28 use Koha::Library;
29 use Koha::Patron::Categories;
31 use t::lib::Mocks;
32 use t::lib::TestBuilder;
34 use Koha::Database;
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({
44 source => "Branch",
45 });
47 my $branchcode=$library->{branchcode};
49 my $john_doe = $builder->build({
50 source => "Borrower",
51 value => {
52 cardnumber => '123456',
53 firstname => 'John',
54 surname => 'Doe',
55 branchcode => $branchcode,
56 dateofbirth => '1983-03-01',
57 userid => 'john.doe'
59 });
61 my $john_smith = $builder->build({
62 source => "Borrower",
63 value => {
64 cardnumber => '234567',
65 firstname => 'John',
66 surname => 'Smith',
67 branchcode => $branchcode,
68 dateofbirth => '1982-02-01',
69 userid => 'john.smith'
71 });
73 my $jane_doe = $builder->build({
74 source => "Borrower",
75 value => {
76 cardnumber => '345678',
77 firstname => 'Jane',
78 surname => 'Doe',
79 branchcode => $branchcode,
80 dateofbirth => '1983-03-01',
81 userid => 'jane.doe'
83 });
84 my $jeanpaul_dupont = $builder->build({
85 source => "Borrower",
86 value => {
87 cardnumber => '456789',
88 firstname => 'Jean Paul',
89 surname => 'Dupont',
90 branchcode => $branchcode,
91 dateofbirth => '1982-02-01',
92 userid => 'jeanpaul.dupont'
94 });
95 my $dupont_brown = $builder->build({
96 source => "Borrower",
97 value => {
98 cardnumber => '567890',
99 firstname => 'Dupont',
100 surname => 'Brown',
101 branchcode => $branchcode,
102 dateofbirth => '1979-01-01',
103 userid => 'dupont.brown'
107 # Set common datatables params
108 my %dt_params = (
109 iDisplayLength => 10,
110 iDisplayStart => 0
113 # Search "John Doe"
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)");
129 # Search "Jane Doe"
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)");
145 # Search "John"
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");
165 # Search "Doe"
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");
238 # Search by userid
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' } ]
284 t::lib::Mocks::mock_preference('ExtendedPatronAttributes', 1);
285 $search_results = C4::Utils::DataTables::Members::search({
286 searchmember => "common user",
287 searchfieldstype => 'standard',
288 searchtype => 'contain',
289 branchcode => $branchcode,
290 dt_params => \%dt_params
293 is( $search_results->{ iTotalDisplayRecords}, 2, "There are 2 common users" );
295 t::lib::Mocks::mock_preference('ExtendedPatronAttributes', 0);
296 $search_results = C4::Utils::DataTables::Members::search({
297 searchmember => "common user",
298 searchfieldstype => 'standard',
299 searchtype => 'contain',
300 branchcode => $branchcode,
301 dt_params => \%dt_params
303 is( $search_results->{ iTotalDisplayRecords}, 0, "There are still 2 common users, but the patron attribute is not searchable " );
305 $search_results = C4::Utils::DataTables::Members::search({
306 searchmember => "Jean Paul",
307 searchfieldstype => 'standard',
308 searchtype => 'start_with',
309 branchcode => $branchcode,
310 dt_params => \%dt_params
313 is( $search_results->{ iTotalDisplayRecords }, 1,
314 "Jean Paul Dupont is found using start with and two terms search 'Jean Paul' (Bug 15252)");
316 $search_results = C4::Utils::DataTables::Members::search({
317 searchmember => "Jean Pau",
318 searchfieldstype => 'standard',
319 searchtype => 'start_with',
320 branchcode => $branchcode,
321 dt_params => \%dt_params
324 is( $search_results->{ iTotalDisplayRecords }, 1,
325 "Jean Paul Dupont is found using start with and two terms search 'Jean Pau' (Bug 15252)");
327 $search_results = C4::Utils::DataTables::Members::search({
328 searchmember => "Jea Pau",
329 searchfieldstype => 'standard',
330 searchtype => 'start_with',
331 branchcode => $branchcode,
332 dt_params => \%dt_params
335 is( $search_results->{ iTotalDisplayRecords }, 0,
336 "Jean Paul Dupont is not found using start with and two terms search 'Jea Pau' (Bug 15252)");
338 $search_results = C4::Utils::DataTables::Members::search({
339 searchmember => "Jea Pau",
340 searchfieldstype => 'standard',
341 searchtype => 'contain',
342 branchcode => $branchcode,
343 dt_params => \%dt_params
346 is( $search_results->{ iTotalDisplayRecords }, 1,
347 "Jean Paul Dupont is found using contains and two terms search 'Jea Pau' (Bug 15252)");
349 my @datetimeprefs = ("dmydot","iso","metric","us");
350 my %dates_in_pref = (
351 dmydot => ["01.02.1982","01.03.1983","01.01.1979","01.01.1988"],
352 iso => ["1982-02-01","1983-03-01","1979-01-01","1988-01-01"],
353 metric => ["01/02/1982","01/03/1983","01/01/1979","01/01/1988"],
354 us => ["02/01/1982","03/01/1983","01/01/1979","01/01/1988"],
356 foreach my $dateformloo (@datetimeprefs){
357 t::lib::Mocks::mock_preference('dateformat', $dateformloo);
358 t::lib::Mocks::mock_preference('DefaultPatronSearchFields', 'surname,firstname,othernames,userid,dateofbirth');
359 $search_results = C4::Utils::DataTables::Members::search({
360 searchmember => $dates_in_pref{$dateformloo}[0],
361 searchfieldstype => 'standard',
362 searchtype => 'contain',
363 branchcode => $branchcode,
364 dt_params => \%dt_params
367 is( $search_results->{ iTotalDisplayRecords }, 2,
368 "dateformat: $dateformloo Two borrowers have dob $dates_in_pref{$dateformloo}[0], standard search fields plus dob works");
370 $search_results = C4::Utils::DataTables::Members::search({
371 searchmember => $dates_in_pref{$dateformloo}[2],
372 searchfieldstype => 'standard',
373 searchtype => 'contain',
374 branchcode => $branchcode,
375 dt_params => \%dt_params
378 is( $search_results->{ iTotalDisplayRecords }, 1,
379 "dateformat: $dateformloo One borrower has dob $dates_in_pref{$dateformloo}[2], standard search fields plus dob works");
381 $search_results = C4::Utils::DataTables::Members::search({
382 searchmember => $dates_in_pref{$dateformloo}[1],
383 searchfieldstype => 'dateofbirth',
384 searchtype => 'contain',
385 branchcode => $branchcode,
386 dt_params => \%dt_params
389 is( $search_results->{ iTotalDisplayRecords }, 2,
390 "dateformat: $dateformloo Two borrowers have dob $dates_in_pref{$dateformloo}[1], dateofbirth search field works");
392 $search_results = C4::Utils::DataTables::Members::search({
393 searchmember => $dates_in_pref{$dateformloo}[3],
394 searchfieldstype => 'dateofbirth',
395 searchtype => 'contain',
396 branchcode => $branchcode,
397 dt_params => \%dt_params
400 is( $search_results->{ iTotalDisplayRecords }, 0,
401 "dateformat: $dateformloo No borrowers have dob $dates_in_pref{$dateformloo}[3], dateofbirth search field works");
403 $search_results = C4::Utils::DataTables::Members::search({
404 searchmember => $dates_in_pref{$dateformloo}[3],
405 searchfieldstype => 'standard',
406 searchtype => 'contain',
407 branchcode => $branchcode,
408 dt_params => \%dt_params
411 is( $search_results->{ iTotalDisplayRecords }, 0,
412 "dateformat: $dateformloo No borrowers have dob $dates_in_pref{$dateformloo}[3], standard search fields plus dob works");
415 # Date of birth formatting
416 t::lib::Mocks::mock_preference('dateformat', 'metric');
417 $search_results = C4::Utils::DataTables::Members::search({
418 searchmember => "01/02/1982",
419 searchfieldstype => 'dateofbirth',
420 dt_params => \%dt_params
422 is( $search_results->{ iTotalDisplayRecords }, 2,
423 "Sarching by date of birth should handle date formatted given the dateformat pref");
424 $search_results = C4::Utils::DataTables::Members::search({
425 searchmember => "1982-02-01",
426 searchfieldstype => 'dateofbirth',
427 dt_params => \%dt_params
429 is( $search_results->{ iTotalDisplayRecords }, 2,
430 "Sarching by date of birth should handle date formatted in iso");
432 # End
433 $schema->storage->txn_rollback;