Bug 14598: (followup) Remove unused and non-existent C4::ItemType include
[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 => 49;
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
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
291 });
292
293 is( $search_results->{ iTotalDisplayRecords}, 2, "There are 2 common users" );
294
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
302 });
303 is( $search_results->{ iTotalDisplayRecords}, 0, "There are still 2 common users, but the patron attribute is not searchable " );
304
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
311 });
312
313 is( $search_results->{ iTotalDisplayRecords }, 1,
314     "Jean Paul Dupont is found using start with and two terms search 'Jean Paul' (Bug 15252)");
315
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
322 });
323
324 is( $search_results->{ iTotalDisplayRecords }, 1,
325     "Jean Paul Dupont is found using start with and two terms search 'Jean Pau' (Bug 15252)");
326
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
333 });
334
335 is( $search_results->{ iTotalDisplayRecords }, 0,
336     "Jean Paul Dupont is not found using start with and two terms search 'Jea Pau' (Bug 15252)");
337
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
344 });
345
346 is( $search_results->{ iTotalDisplayRecords }, 1,
347     "Jean Paul Dupont is found using contains and two terms search 'Jea Pau' (Bug 15252)");
348
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"],
355         );
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
365     });
366
367     is( $search_results->{ iTotalDisplayRecords }, 2,
368         "dateformat: $dateformloo Two borrowers have dob $dates_in_pref{$dateformloo}[0], standard search fields plus dob works");
369
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
376     });
377
378     is( $search_results->{ iTotalDisplayRecords }, 1,
379         "dateformat: $dateformloo One borrower has dob $dates_in_pref{$dateformloo}[2], standard search fields plus dob works");
380
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
387     });
388
389     is( $search_results->{ iTotalDisplayRecords }, 2,
390         "dateformat: $dateformloo Two borrowers have dob $dates_in_pref{$dateformloo}[1], dateofbirth search field works");
391
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
398     });
399
400     is( $search_results->{ iTotalDisplayRecords }, 0,
401         "dateformat: $dateformloo No borrowers have dob $dates_in_pref{$dateformloo}[3], dateofbirth search field works");
402
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
409     });
410
411     is( $search_results->{ iTotalDisplayRecords }, 0,
412         "dateformat: $dateformloo No borrowers have dob $dates_in_pref{$dateformloo}[3], standard search fields plus dob works");
413 }
414
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
421 });
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
428 });
429 is( $search_results->{ iTotalDisplayRecords }, 2,
430     "Sarching by date of birth should handle date formatted in iso");
431
432 # End
433 $schema->storage->txn_rollback;
434
435 1;