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>.
20 use Test::More tests => 27;
25 use C4::Members::Attributes;
26 use C4::Members::AttributeTypes;
32 use_ok( "C4::Utils::DataTables::Members" );
34 my $dbh = C4::Context->dbh;
37 $dbh->{AutoCommit} = 0;
38 $dbh->{RaiseError} = 1;
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";
46 branchcode => $branchcode,
47 branchname => 'Universidad Nacional de Cordoba',
48 branchaddress1 => 'Haya de la Torre',
49 branchaddress2 => 'S/N',
51 branchcity => 'Cordoba',
52 branchstate => 'Cordoba',
53 branchcountry => 'Argentina'
55 Koha::Library->new( $branch_data )->store;
58 cardnumber => '123456',
61 categorycode => $categorycode,
62 branchcode => $branchcode,
64 dateexpiry => '9999-12-31',
69 cardnumber => '234567',
72 categorycode => $categorycode,
73 branchcode => $branchcode,
75 dateexpiry => '9999-12-31',
76 userid => 'john.smith'
80 cardnumber => '345678',
83 categorycode => $categorycode,
84 branchcode => $branchcode,
86 dateexpiry => '9999-12-31',
90 my %jeanpaul_dupont = (
91 cardnumber => '456789',
92 firstname => 'Jean Paul',
94 categorycode => $categorycode,
95 branchcode => $branchcode,
97 dateexpiry => '9999-12-31',
98 userid => 'jeanpaul.dupont'
102 cardnumber => '567890',
103 firstname => 'Dupont',
105 categorycode => $categorycode,
106 branchcode => $branchcode,
108 dateexpiry => '9999-12-31',
109 userid => 'dupont.brown'
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};
123 # Set common datatables params
125 iDisplayLength => 10,
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
138 is( $search_results->{ iTotalDisplayRecords }, 1,
139 "John Doe has only one match on $branchcode (Bug 12595)");
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)");
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
154 is( $search_results->{ iTotalDisplayRecords }, 1,
155 "Jane Doe has only one match on $branchcode (Bug 12595)");
157 is( $search_results->{ patrons }[0]->{ cardnumber },
158 $jane_doe{ cardnumber },
159 "Jane Doe is the only match (Bug 12595)");
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
170 is( $search_results->{ iTotalDisplayRecords }, 2,
171 "There are two John at $branchcode");
173 is( $search_results->{ patrons }[0]->{ cardnumber },
174 $john_doe{ cardnumber },
175 "John Doe is the first result");
177 is( $search_results->{ patrons }[1]->{ cardnumber },
178 $john_smith{ cardnumber },
179 "John Smith is the second result");
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
190 is( $search_results->{ iTotalDisplayRecords }, 2,
191 "There are two Doe at $branchcode");
193 is( $search_results->{ patrons }[0]->{ cardnumber },
194 $john_doe{ cardnumber },
195 "John Doe is the first result");
197 is( $search_results->{ patrons }[1]->{ cardnumber },
198 $jane_doe{ cardnumber },
199 "Jane Doe is the second result");
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
210 is( $search_results->{ iTotalDisplayRecords }, 1,
211 "There is one Smith at $branchcode when searching for surname");
213 is( $search_results->{ patrons }[0]->{ cardnumber },
214 $john_smith{ cardnumber },
215 "John Smith is the first result");
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
227 is( $search_results->{ iTotalDisplayRecords }, 1,
228 "There is one Dupont at $branchcode when searching for surname");
230 is( $search_results->{ patrons }[0]->{ cardnumber },
231 $jeanpaul_dupont{ cardnumber },
232 "Jean Paul Dupont is the first result");
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
243 is( $search_results->{ iTotalDisplayRecords }, 2,
244 "There are two Doe at $branchcode when searching for surname");
246 is( $search_results->{ patrons }[0]->{ cardnumber },
247 $john_doe{ cardnumber },
248 "John Doe is the first result");
250 is( $search_results->{ patrons }[1]->{ cardnumber },
251 $jane_doe{ cardnumber },
252 "Jane Doe is the second result");
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
263 is( $search_results->{ iTotalDisplayRecords }, 1,
264 "John Doe is found by userid, standard search (Bug 14782)");
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
274 is( $search_results->{ iTotalDisplayRecords }, 1,
275 "John Doe is found by userid, userid search (Bug 14782)");
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
285 is( $search_results->{ iTotalDisplayRecords }, 0,
286 "No members are found by userid, surname search");
288 my $attribute_type = C4::Members::AttributeTypes->new( 'ATM_1', 'my attribute type' );
289 $attribute_type->{staff_searchable} = 1;
290 $attribute_type->store;
293 C4::Members::Attributes::SetBorrowerAttributes(
294 $john_doe{borrowernumber}, [ { code => $attribute_type->{code}, value => 'the default value for a common user' } ]
296 C4::Members::Attributes::SetBorrowerAttributes(
297 $jane_doe{borrowernumber}, [ { code => $attribute_type->{code}, value => 'the default value for another common user' } ]
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
309 is( $search_results->{ iTotalDisplayRecords}, 2, "There are 2 common users" );
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
319 is( $search_results->{ iTotalDisplayRecords}, 0, "There are still 2 common users, but the patron attribute is not searchable " );
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
329 is( $search_results->{ iTotalDisplayRecords }, 1,
330 "Jean Paul Dupont is found using start with and two terms search 'Jean Paul' (Bug 15252)");
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
340 is( $search_results->{ iTotalDisplayRecords }, 1,
341 "Jean Paul Dupont is found using start with and two terms search 'Jean Pau' (Bug 15252)");
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
351 is( $search_results->{ iTotalDisplayRecords }, 0,
352 "Jean Paul Dupont is not found using start with and two terms search 'Jea Pau' (Bug 15252)");
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
362 is( $search_results->{ iTotalDisplayRecords }, 1,
363 "Jean Paul Dupont is found using contains and two terms search 'Jea Pau' (Bug 15252)");