Bug 9259: Use is instead of is_deeply
[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 => 27;
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
30 use t::lib::Mocks;
31
32 use_ok( "C4::Utils::DataTables::Members" );
33
34 my $dbh = C4::Context->dbh;
35
36 # Start transaction
37 $dbh->{AutoCommit} = 0;
38 $dbh->{RaiseError} = 1;
39
40 # Pick a categorycode from the DB
41 my @categories   = C4::Category->all;
42 my $categorycode = $categories[0]->categorycode;
43 # Add a new branch so we control what borrowers it has
44 my $branchcode   = "UNC";
45 my $branch_data = {
46     branchcode     => $branchcode,
47     branchname     => 'Universidad Nacional de Cordoba',
48     branchaddress1 => 'Haya de la Torre',
49     branchaddress2 => 'S/N',
50     branchzip      => '5000',
51     branchcity     => 'Cordoba',
52     branchstate    => 'Cordoba',
53     branchcountry  => 'Argentina'
54 };
55 Koha::Library->new( $branch_data )->store;
56
57 my %john_doe = (
58     cardnumber   => '123456',
59     firstname    => 'John',
60     surname      => 'Doe',
61     categorycode => $categorycode,
62     branchcode   => $branchcode,
63     dateofbirth  => '',
64     dateexpiry   => '9999-12-31',
65     userid       => 'john.doe'
66 );
67
68 my %john_smith = (
69     cardnumber   => '234567',
70     firstname    =>  'John',
71     surname      => 'Smith',
72     categorycode => $categorycode,
73     branchcode   => $branchcode,
74     dateofbirth  => '',
75     dateexpiry   => '9999-12-31',
76     userid       => 'john.smith'
77 );
78
79 my %jane_doe = (
80     cardnumber   => '345678',
81     firstname    =>  'Jane',
82     surname      => 'Doe',
83     categorycode => $categorycode,
84     branchcode   => $branchcode,
85     dateofbirth  => '',
86     dateexpiry   => '9999-12-31',
87     userid       => 'jane.doe'
88 );
89
90 my %jeanpaul_dupont = (
91     cardnumber   => '456789',
92     firstname    => 'Jean Paul',
93     surname      => 'Dupont',
94     categorycode => $categorycode,
95     branchcode   => $branchcode,
96     dateofbirth  => '',
97     dateexpiry   => '9999-12-31',
98     userid       => 'jeanpaul.dupont'
99 );
100
101 my %dupont_brown = (
102     cardnumber   => '567890',
103     firstname    => 'Dupont',
104     surname      => 'Brown',
105     categorycode => $categorycode,
106     branchcode   => $branchcode,
107     dateofbirth  => '',
108     dateexpiry   => '9999-12-31',
109     userid       => 'dupont.brown'
110 );
111
112 $john_doe{borrowernumber} = AddMember( %john_doe );
113 warn "Error adding John Doe, check your tests" unless $john_doe{borrowernumber};
114 $john_smith{borrowernumber} = AddMember( %john_smith );
115 warn "Error adding John Smith, check your tests" unless $john_smith{borrowernumber};
116 $jane_doe{borrowernumber} = AddMember( %jane_doe );
117 warn "Error adding Jane Doe, check your tests" unless $jane_doe{borrowernumber};
118 $jeanpaul_dupont{borrowernumber} = AddMember( %jeanpaul_dupont );
119 warn "Error adding Jean Paul Dupont, check your tests" unless $jeanpaul_dupont{borrowernumber};
120 $dupont_brown{borrowernumber} = AddMember( %dupont_brown );
121 warn "Error adding Dupont Brown, check your tests" unless $dupont_brown{borrowernumber};
122
123 # Set common datatables params
124 my %dt_params = (
125     iDisplayLength   => 10,
126     iDisplayStart    => 0
127 );
128
129 # Search "John Doe"
130 my $search_results = C4::Utils::DataTables::Members::search({
131     searchmember     => "John Doe",
132     searchfieldstype => 'standard',
133     searchtype       => 'contain',
134     branchcode       => $branchcode,
135     dt_params        => \%dt_params
136 });
137
138 is( $search_results->{ iTotalDisplayRecords }, 1,
139     "John Doe has only one match on $branchcode (Bug 12595)");
140
141 ok( $search_results->{ patrons }[0]->{ cardnumber } eq $john_doe{ cardnumber }
142     && ! $search_results->{ patrons }[1],
143     "John Doe is the only match (Bug 12595)");
144
145 # Search "Jane Doe"
146 $search_results = C4::Utils::DataTables::Members::search({
147     searchmember     => "Jane Doe",
148     searchfieldstype => 'standard',
149     searchtype       => 'contain',
150     branchcode       => $branchcode,
151     dt_params        => \%dt_params
152 });
153
154 is( $search_results->{ iTotalDisplayRecords }, 1,
155     "Jane Doe has only one match on $branchcode (Bug 12595)");
156
157 is( $search_results->{ patrons }[0]->{ cardnumber },
158     $jane_doe{ cardnumber },
159     "Jane Doe is the only match (Bug 12595)");
160
161 # Search "John"
162 $search_results = C4::Utils::DataTables::Members::search({
163     searchmember     => "John",
164     searchfieldstype => 'standard',
165     searchtype       => 'contain',
166     branchcode       => $branchcode,
167     dt_params        => \%dt_params
168 });
169
170 is( $search_results->{ iTotalDisplayRecords }, 2,
171     "There are two John at $branchcode");
172
173 is( $search_results->{ patrons }[0]->{ cardnumber },
174     $john_doe{ cardnumber },
175     "John Doe is the first result");
176
177 is( $search_results->{ patrons }[1]->{ cardnumber },
178     $john_smith{ cardnumber },
179     "John Smith is the second result");
180
181 # Search "Doe"
182 $search_results = C4::Utils::DataTables::Members::search({
183     searchmember     => "Doe",
184     searchfieldstype => 'standard',
185     searchtype       => 'contain',
186     branchcode       => $branchcode,
187     dt_params        => \%dt_params
188 });
189
190 is( $search_results->{ iTotalDisplayRecords }, 2,
191     "There are two Doe at $branchcode");
192
193 is( $search_results->{ patrons }[0]->{ cardnumber },
194     $john_doe{ cardnumber },
195     "John Doe is the first result");
196
197 is( $search_results->{ patrons }[1]->{ cardnumber },
198     $jane_doe{ cardnumber },
199     "Jane Doe is the second result");
200
201 # Search "Smith" as surname - there is only one occurrence of Smith
202 $search_results = C4::Utils::DataTables::Members::search({
203     searchmember     => "Smith",
204     searchfieldstype => 'surname',
205     searchtype       => 'contain',
206     branchcode       => $branchcode,
207     dt_params        => \%dt_params
208 });
209
210 is( $search_results->{ iTotalDisplayRecords }, 1,
211     "There is one Smith at $branchcode when searching for surname");
212
213 is( $search_results->{ patrons }[0]->{ cardnumber },
214     $john_smith{ cardnumber },
215     "John Smith is the first result");
216
217 # Search "Dupont" as surname - Dupont is used both as firstname and surname, we
218 # Should only fin d the user with Dupont as surname
219 $search_results = C4::Utils::DataTables::Members::search({
220     searchmember     => "Dupont",
221     searchfieldstype => 'surname',
222     searchtype       => 'contain',
223     branchcode       => $branchcode,
224     dt_params        => \%dt_params
225 });
226
227 is( $search_results->{ iTotalDisplayRecords }, 1,
228     "There is one Dupont at $branchcode when searching for surname");
229
230 is( $search_results->{ patrons }[0]->{ cardnumber },
231     $jeanpaul_dupont{ cardnumber },
232     "Jean Paul Dupont is the first result");
233
234 # Search "Doe" as surname - Doe is used twice as surname
235 $search_results = C4::Utils::DataTables::Members::search({
236     searchmember     => "Doe",
237     searchfieldstype => 'surname',
238     searchtype       => 'contain',
239     branchcode       => $branchcode,
240     dt_params        => \%dt_params
241 });
242
243 is( $search_results->{ iTotalDisplayRecords }, 2,
244     "There are two Doe at $branchcode when searching for surname");
245
246 is( $search_results->{ patrons }[0]->{ cardnumber },
247     $john_doe{ cardnumber },
248     "John Doe is the first result");
249
250 is( $search_results->{ patrons }[1]->{ cardnumber },
251     $jane_doe{ cardnumber },
252     "Jane Doe is the second result");
253
254 # Search by userid
255 $search_results = C4::Utils::DataTables::Members::search({
256     searchmember     => "john.doe",
257     searchfieldstype => 'standard',
258     searchtype       => 'contain',
259     branchcode       => $branchcode,
260     dt_params        => \%dt_params
261 });
262
263 is( $search_results->{ iTotalDisplayRecords }, 1,
264     "John Doe is found by userid, standard search (Bug 14782)");
265
266 $search_results = C4::Utils::DataTables::Members::search({
267     searchmember     => "john.doe",
268     searchfieldstype => 'userid',
269     searchtype       => 'contain',
270     branchcode       => $branchcode,
271     dt_params        => \%dt_params
272 });
273
274 is( $search_results->{ iTotalDisplayRecords }, 1,
275     "John Doe is found by userid, userid search (Bug 14782)");
276
277 $search_results = C4::Utils::DataTables::Members::search({
278     searchmember     => "john.doe",
279     searchfieldstype => 'surname',
280     searchtype       => 'contain',
281     branchcode       => $branchcode,
282     dt_params        => \%dt_params
283 });
284
285 is( $search_results->{ iTotalDisplayRecords }, 0,
286     "No members are found by userid, surname search");
287
288 my $attribute_type = C4::Members::AttributeTypes->new( 'ATM_1', 'my attribute type' );
289 $attribute_type->{staff_searchable} = 1;
290 $attribute_type->store;
291
292
293 C4::Members::Attributes::SetBorrowerAttributes(
294     $john_doe{borrowernumber}, [ { code => $attribute_type->{code}, value => 'the default value for a common user' } ]
295 );
296 C4::Members::Attributes::SetBorrowerAttributes(
297     $jane_doe{borrowernumber}, [ { code => $attribute_type->{code}, value => 'the default value for another common user' } ]
298 );
299
300 t::lib::Mocks::mock_preference('ExtendedPatronAttributes', 1);
301 $search_results = C4::Utils::DataTables::Members::search({
302     searchmember     => "common user",
303     searchfieldstype => 'standard',
304     searchtype       => 'contain',
305     branchcode       => $branchcode,
306     dt_params        => \%dt_params
307 });
308
309 is( $search_results->{ iTotalDisplayRecords}, 2, "There are 2 common users" );
310
311 t::lib::Mocks::mock_preference('ExtendedPatronAttributes', 0);
312 $search_results = C4::Utils::DataTables::Members::search({
313     searchmember     => "common user",
314     searchfieldstype => 'standard',
315     searchtype       => 'contain',
316     branchcode       => $branchcode,
317     dt_params        => \%dt_params
318 });
319 is( $search_results->{ iTotalDisplayRecords}, 0, "There are still 2 common users, but the patron attribute is not searchable " );
320
321 $search_results = C4::Utils::DataTables::Members::search({
322     searchmember     => "Jean Paul",
323     searchfieldstype => 'standard',
324     searchtype       => 'start_with',
325     branchcode       => $branchcode,
326     dt_params        => \%dt_params
327 });
328
329 is( $search_results->{ iTotalDisplayRecords }, 1,
330     "Jean Paul Dupont is found using start with and two terms search 'Jean Paul' (Bug 15252)");
331
332 $search_results = C4::Utils::DataTables::Members::search({
333     searchmember     => "Jean Pau",
334     searchfieldstype => 'standard',
335     searchtype       => 'start_with',
336     branchcode       => $branchcode,
337     dt_params        => \%dt_params
338 });
339
340 is( $search_results->{ iTotalDisplayRecords }, 1,
341     "Jean Paul Dupont is found using start with and two terms search 'Jean Pau' (Bug 15252)");
342
343 $search_results = C4::Utils::DataTables::Members::search({
344     searchmember     => "Jea Pau",
345     searchfieldstype => 'standard',
346     searchtype       => 'start_with',
347     branchcode       => $branchcode,
348     dt_params        => \%dt_params
349 });
350
351 is( $search_results->{ iTotalDisplayRecords }, 0,
352     "Jean Paul Dupont is not found using start with and two terms search 'Jea Pau' (Bug 15252)");
353
354 $search_results = C4::Utils::DataTables::Members::search({
355     searchmember     => "Jea Pau",
356     searchfieldstype => 'standard',
357     searchtype       => 'contain',
358     branchcode       => $branchcode,
359     dt_params        => \%dt_params
360 });
361
362 is( $search_results->{ iTotalDisplayRecords }, 1,
363     "Jean Paul Dupont is found using contains and two terms search 'Jea Pau' (Bug 15252)");
364
365 # End
366 $dbh->rollback;
367
368 1;