feca4f88fca85abe6a39e384c34fb3dbbf99eeea
[koha.git] / t / db_dependent / Utils / Datatables_Members.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
20 use Test::More tests => 51;
21
22 use C4::Context;
23 use C4::Members;
24
25 use C4::Members::Attributes;
26 use C4::Members::AttributeTypes;
27
28 use Koha::Library;
29 use Koha::Patron::Categories;
30
31 use t::lib::Mocks;
32 use t::lib::TestBuilder;
33
34 use Koha::Database;
35
36 use_ok( "C4::Utils::DataTables::Members" );
37
38 my $schema = Koha::Database->new->schema;
39 $schema->storage->txn_begin;
40
41 my $builder = t::lib::TestBuilder->new;
42
43 my $library = $builder->build({
44     source => "Branch",
45 });
46
47 my $patron = $builder->build_object({ class => 'Koha::Patrons', value => { flags => 1 } });
48 t::lib::Mocks::mock_userenv({ patron => $patron });
49
50 my $branchcode=$library->{branchcode};
51
52 my $john_doe = $builder->build({
53         source => "Borrower",
54         value => {
55             cardnumber   => '123456',
56             firstname    => 'John',
57             surname      => 'Doe',
58             branchcode   => $branchcode,
59             dateofbirth  => '1983-03-01',
60             userid       => 'john.doe',
61             initials     => 'pacman'
62         },
63 });
64
65 my $john_smith = $builder->build({
66         source => "Borrower",
67         value => {
68             cardnumber   => '234567',
69             firstname    => 'John',
70             surname      => 'Smith',
71             branchcode   => $branchcode,
72             dateofbirth  => '1982-02-01',
73             userid       => 'john.smith'
74         },
75 });
76
77 my $jane_doe = $builder->build({
78         source => "Borrower",
79         value => {
80             cardnumber   => '345678',
81             firstname    => 'Jane',
82             surname      => 'Doe',
83             branchcode   => $branchcode,
84             dateofbirth  => '1983-03-01',
85             userid       => 'jane.doe'
86         },
87 });
88 my $jeanpaul_dupont = $builder->build({
89         source => "Borrower",
90         value => {
91             cardnumber   => '456789',
92             firstname    => 'Jean Paul',
93             surname      => 'Dupont',
94             branchcode   => $branchcode,
95             dateofbirth  => '1982-02-01',
96             userid       => 'jeanpaul.dupont'
97         },
98 });
99 my $dupont_brown = $builder->build({
100         source => "Borrower",
101         value => {
102             cardnumber   => '567890',
103             firstname    => 'Dupont',
104             surname      => 'Brown',
105             branchcode   => $branchcode,
106             dateofbirth  => '1979-01-01',
107             userid       => 'dupont.brown'
108         },
109 });
110
111 # Set common datatables params
112 my %dt_params = (
113     iDisplayLength   => 10,
114     iDisplayStart    => 0
115 );
116
117 t::lib::Mocks::mock_preference('DefaultPatronSearchFields', '');
118
119 # Search "John Doe"
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
126 });
127
128 is( $search_results->{ iTotalDisplayRecords }, 1,
129     "John Doe has only one match on $branchcode (Bug 12595)");
130
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)");
134
135 # Search "Jane Doe"
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
142 });
143
144 is( $search_results->{ iTotalDisplayRecords }, 1,
145     "Jane Doe has only one match on $branchcode (Bug 12595)");
146
147 is( $search_results->{ patrons }[0]->{ cardnumber },
148     $jane_doe->{ cardnumber },
149     "Jane Doe is the only match (Bug 12595)");
150
151 # Search "John"
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
158 });
159
160 is( $search_results->{ iTotalDisplayRecords }, 2,
161     "There are two John at $branchcode");
162
163 is( $search_results->{ patrons }[0]->{ cardnumber },
164     $john_doe->{ cardnumber },
165     "John Doe is the first result");
166
167 is( $search_results->{ patrons }[1]->{ cardnumber },
168     $john_smith->{ cardnumber },
169     "John Smith is the second result");
170
171 # Search "Doe"
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
178 });
179
180 is( $search_results->{ iTotalDisplayRecords }, 2,
181     "There are two Doe at $branchcode");
182
183 is( $search_results->{ patrons }[0]->{ cardnumber },
184     $john_doe->{ cardnumber },
185     "John Doe is the first result");
186
187 is( $search_results->{ patrons }[1]->{ cardnumber },
188     $jane_doe->{ cardnumber },
189     "Jane Doe is the second result");
190
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
198 });
199
200 is( $search_results->{ iTotalDisplayRecords }, 1,
201     "There is one Smith at $branchcode when searching for surname");
202
203 is( $search_results->{ patrons }[0]->{ cardnumber },
204     $john_smith->{ cardnumber },
205     "John Smith is the first result");
206
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
215 });
216
217 is( $search_results->{ iTotalDisplayRecords }, 1,
218     "There is one Dupont at $branchcode when searching for surname");
219
220 is( $search_results->{ patrons }[0]->{ cardnumber },
221     $jeanpaul_dupont->{ cardnumber },
222     "Jean Paul Dupont is the first result");
223
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
231 });
232
233 is( $search_results->{ iTotalDisplayRecords }, 2,
234     "There are two Doe at $branchcode when searching for surname");
235
236 is( $search_results->{ patrons }[0]->{ cardnumber },
237     $john_doe->{ cardnumber },
238     "John Doe is the first result");
239
240 is( $search_results->{ patrons }[1]->{ cardnumber },
241     $jane_doe->{ cardnumber },
242     "Jane Doe is the second result");
243
244 # Search by userid
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
251 });
252
253 is( $search_results->{ iTotalDisplayRecords }, 1,
254     "John Doe is found by userid, standard search (Bug 14782)");
255
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
262 });
263
264 is( $search_results->{ iTotalDisplayRecords }, 1,
265     "John Doe is found by userid, userid search (Bug 14782)");
266
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
273 });
274
275 is( $search_results->{ iTotalDisplayRecords }, 0,
276     "No members are found by userid, surname search");
277
278 my $attribute_type = C4::Members::AttributeTypes->new( 'ATM_1', 'my attribute type' );
279 $attribute_type->{staff_searchable} = 1;
280 $attribute_type->store;
281
282
283 C4::Members::Attributes::SetBorrowerAttributes(
284     $john_doe->{borrowernumber}, [ { code => $attribute_type->{code}, value => 'the default value for a common user' } ]
285 );
286 C4::Members::Attributes::SetBorrowerAttributes(
287     $jane_doe->{borrowernumber}, [ { code => $attribute_type->{code}, value => 'the default value for another common user' } ]
288 );
289 C4::Members::Attributes::SetBorrowerAttributes(
290     $john_smith->{borrowernumber}, [ { code => $attribute_type->{code}, value => 'Attribute which not appears even if contains "Dupont"' } ]
291 );
292
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
300 });
301
302 is( $search_results->{ iTotalDisplayRecords}, 2, "There are 2 common users" );
303
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
311 });
312 is( $search_results->{ iTotalDisplayRecords}, 0, "There are still 2 common users, but the patron attribute is not searchable " );
313
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
320 });
321
322 is( $search_results->{ iTotalDisplayRecords }, 1,
323     "Jean Paul Dupont is found using start with and two terms search 'Jean Paul' (Bug 15252)");
324
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
331 });
332
333 is( $search_results->{ iTotalDisplayRecords }, 1,
334     "Jean Paul Dupont is found using start with and two terms search 'Jean Pau' (Bug 15252)");
335
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
342 });
343
344 is( $search_results->{ iTotalDisplayRecords }, 0,
345     "Jean Paul Dupont is not found using start with and two terms search 'Jea Pau' (Bug 15252)");
346
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
353 });
354
355 is( $search_results->{ iTotalDisplayRecords }, 1,
356     "Jean Paul Dupont is found using contains and two terms search 'Jea Pau' (Bug 15252)");
357
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"],
364         );
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
374     });
375
376     is( $search_results->{ iTotalDisplayRecords }, 2,
377         "dateformat: $dateformloo Two borrowers have dob $dates_in_pref{$dateformloo}[0], standard search fields plus dob works");
378
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
385     });
386
387     is( $search_results->{ iTotalDisplayRecords }, 1,
388         "dateformat: $dateformloo One borrower has dob $dates_in_pref{$dateformloo}[2], standard search fields plus dob works");
389
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
396     });
397
398     is( $search_results->{ iTotalDisplayRecords }, 2,
399         "dateformat: $dateformloo Two borrowers have dob $dates_in_pref{$dateformloo}[1], dateofbirth search field works");
400
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
407     });
408
409     is( $search_results->{ iTotalDisplayRecords }, 0,
410         "dateformat: $dateformloo No borrowers have dob $dates_in_pref{$dateformloo}[3], dateofbirth search field works");
411
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
418     });
419
420     is( $search_results->{ iTotalDisplayRecords }, 0,
421         "dateformat: $dateformloo No borrowers have dob $dates_in_pref{$dateformloo}[3], standard search fields plus dob works");
422 }
423
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
430 });
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
437 });
438 is( $search_results->{ iTotalDisplayRecords }, 2,
439     "Sarching by date of birth should handle date formatted in iso");
440
441 subtest 'ExtendedPatronAttributes' => sub {
442     plan tests => 2;
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
450     });
451
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)");
454
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
461     });
462
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)");
465 };
466
467 subtest 'Search by any borrowers field (bug 17374)' => sub {
468     plan tests => 2;
469
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
476     });
477     is( $search_results->{ iTotalDisplayRecords }, 1, "We find only 1 patron when searching for initials 'pacman'" );
478
479     is( $search_results->{ patrons }[0]->{ cardnumber }, $john_doe->{cardnumber}, "We find the correct patron when sesrching by initials" )
480 };
481
482 # End
483 $schema->storage->txn_rollback;