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