Bug 18458: Add a subtest in Merge.t
[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 => 50;
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 $branchcode=$library->{branchcode};
48
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'
58         },
59 });
60
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'
70         },
71 });
72
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'
82         },
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'
93         },
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'
104         },
105 });
106
107 # Set common datatables params
108 my %dt_params = (
109     iDisplayLength   => 10,
110     iDisplayStart    => 0
111 );
112
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
120 });
121
122 is( $search_results->{ iTotalDisplayRecords }, 1,
123     "John Doe has only one match on $branchcode (Bug 12595)");
124
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)");
128
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
136 });
137
138 is( $search_results->{ iTotalDisplayRecords }, 1,
139     "Jane Doe has only one match on $branchcode (Bug 12595)");
140
141 is( $search_results->{ patrons }[0]->{ cardnumber },
142     $jane_doe->{ cardnumber },
143     "Jane Doe is the only match (Bug 12595)");
144
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
152 });
153
154 is( $search_results->{ iTotalDisplayRecords }, 2,
155     "There are two John at $branchcode");
156
157 is( $search_results->{ patrons }[0]->{ cardnumber },
158     $john_doe->{ cardnumber },
159     "John Doe is the first result");
160
161 is( $search_results->{ patrons }[1]->{ cardnumber },
162     $john_smith->{ cardnumber },
163     "John Smith is the second result");
164
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
172 });
173
174 is( $search_results->{ iTotalDisplayRecords }, 2,
175     "There are two Doe at $branchcode");
176
177 is( $search_results->{ patrons }[0]->{ cardnumber },
178     $john_doe->{ cardnumber },
179     "John Doe is the first result");
180
181 is( $search_results->{ patrons }[1]->{ cardnumber },
182     $jane_doe->{ cardnumber },
183     "Jane Doe is the second result");
184
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
192 });
193
194 is( $search_results->{ iTotalDisplayRecords }, 1,
195     "There is one Smith at $branchcode when searching for surname");
196
197 is( $search_results->{ patrons }[0]->{ cardnumber },
198     $john_smith->{ cardnumber },
199     "John Smith is the first result");
200
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
209 });
210
211 is( $search_results->{ iTotalDisplayRecords }, 1,
212     "There is one Dupont at $branchcode when searching for surname");
213
214 is( $search_results->{ patrons }[0]->{ cardnumber },
215     $jeanpaul_dupont->{ cardnumber },
216     "Jean Paul Dupont is the first result");
217
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
225 });
226
227 is( $search_results->{ iTotalDisplayRecords }, 2,
228     "There are two Doe at $branchcode when searching for surname");
229
230 is( $search_results->{ patrons }[0]->{ cardnumber },
231     $john_doe->{ cardnumber },
232     "John Doe is the first result");
233
234 is( $search_results->{ patrons }[1]->{ cardnumber },
235     $jane_doe->{ cardnumber },
236     "Jane Doe is the second result");
237
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
245 });
246
247 is( $search_results->{ iTotalDisplayRecords }, 1,
248     "John Doe is found by userid, standard search (Bug 14782)");
249
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
256 });
257
258 is( $search_results->{ iTotalDisplayRecords }, 1,
259     "John Doe is found by userid, userid search (Bug 14782)");
260
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
267 });
268
269 is( $search_results->{ iTotalDisplayRecords }, 0,
270     "No members are found by userid, surname search");
271
272 my $attribute_type = C4::Members::AttributeTypes->new( 'ATM_1', 'my attribute type' );
273 $attribute_type->{staff_searchable} = 1;
274 $attribute_type->store;
275
276
277 C4::Members::Attributes::SetBorrowerAttributes(
278     $john_doe->{borrowernumber}, [ { code => $attribute_type->{code}, value => 'the default value for a common user' } ]
279 );
280 C4::Members::Attributes::SetBorrowerAttributes(
281     $jane_doe->{borrowernumber}, [ { code => $attribute_type->{code}, value => 'the default value for another common user' } ]
282 );
283 C4::Members::Attributes::SetBorrowerAttributes(
284     $john_smith->{borrowernumber}, [ { code => $attribute_type->{code}, value => 'Attribute which not appears even if contains "Dupont"' } ]
285 );
286
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
294 });
295
296 is( $search_results->{ iTotalDisplayRecords}, 2, "There are 2 common users" );
297
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
305 });
306 is( $search_results->{ iTotalDisplayRecords}, 0, "There are still 2 common users, but the patron attribute is not searchable " );
307
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
314 });
315
316 is( $search_results->{ iTotalDisplayRecords }, 1,
317     "Jean Paul Dupont is found using start with and two terms search 'Jean Paul' (Bug 15252)");
318
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
325 });
326
327 is( $search_results->{ iTotalDisplayRecords }, 1,
328     "Jean Paul Dupont is found using start with and two terms search 'Jean Pau' (Bug 15252)");
329
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
336 });
337
338 is( $search_results->{ iTotalDisplayRecords }, 0,
339     "Jean Paul Dupont is not found using start with and two terms search 'Jea Pau' (Bug 15252)");
340
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
347 });
348
349 is( $search_results->{ iTotalDisplayRecords }, 1,
350     "Jean Paul Dupont is found using contains and two terms search 'Jea Pau' (Bug 15252)");
351
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"],
358         );
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
368     });
369
370     is( $search_results->{ iTotalDisplayRecords }, 2,
371         "dateformat: $dateformloo Two borrowers have dob $dates_in_pref{$dateformloo}[0], standard search fields plus dob works");
372
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
379     });
380
381     is( $search_results->{ iTotalDisplayRecords }, 1,
382         "dateformat: $dateformloo One borrower has dob $dates_in_pref{$dateformloo}[2], standard search fields plus dob works");
383
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
390     });
391
392     is( $search_results->{ iTotalDisplayRecords }, 2,
393         "dateformat: $dateformloo Two borrowers have dob $dates_in_pref{$dateformloo}[1], dateofbirth search field works");
394
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
401     });
402
403     is( $search_results->{ iTotalDisplayRecords }, 0,
404         "dateformat: $dateformloo No borrowers have dob $dates_in_pref{$dateformloo}[3], dateofbirth search field works");
405
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
412     });
413
414     is( $search_results->{ iTotalDisplayRecords }, 0,
415         "dateformat: $dateformloo No borrowers have dob $dates_in_pref{$dateformloo}[3], standard search fields plus dob works");
416 }
417
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
424 });
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
431 });
432 is( $search_results->{ iTotalDisplayRecords }, 2,
433     "Sarching by date of birth should handle date formatted in iso");
434
435 subtest 'ExtendedPatronAttributes' => sub {
436     plan tests => 2;
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
444     });
445
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)");
448
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
455     });
456
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)");
459 };
460
461 # End
462 $schema->storage->txn_rollback;
463