Bug 15252 - Add tests
[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 => 19;
21
22 use C4::Context;
23 use C4::Branch;
24 use C4::Members;
25
26 use C4::Members::Attributes;
27 use C4::Members::AttributeTypes;
28
29 use t::lib::Mocks;
30
31 use_ok( "C4::Utils::DataTables::Members" );
32
33 my $dbh = C4::Context->dbh;
34
35 # Start transaction
36 $dbh->{AutoCommit} = 0;
37 $dbh->{RaiseError} = 1;
38
39 # Pick a categorycode from the DB
40 my @categories   = C4::Category->all;
41 my $categorycode = $categories[0]->categorycode;
42 # Add a new branch so we control what borrowers it has
43 my $branchcode   = "UNC";
44 my $branch_data = {
45     add            => 1,
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 ModBranch( $branch_data );
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 $john_doe{borrowernumber} = AddMember( %john_doe );
102 warn "Error adding John Doe, check your tests" unless $john_doe{borrowernumber};
103 $john_smith{borrowernumber} = AddMember( %john_smith );
104 warn "Error adding John Smith, check your tests" unless $john_smith{borrowernumber};
105 $jane_doe{borrowernumber} = AddMember( %jane_doe );
106 warn "Error adding Jane Doe, check your tests" unless $jane_doe{borrowernumber};
107 $jeanpaul_dupont{borrowernumber} = AddMember( %jeanpaul_dupont );
108 warn "Error adding Jean Paul Dupont, check your tests" unless $jeanpaul_dupont{borrowernumber};
109
110 # Set common datatables params
111 my %dt_params = (
112     iDisplayLength   => 10,
113     iDisplayStart    => 0
114 );
115
116 # Search "John Doe"
117 my $search_results = C4::Utils::DataTables::Members::search({
118     searchmember     => "John Doe",
119     searchfieldstype => 'standard',
120     searchtype       => 'contain',
121     branchcode       => $branchcode,
122     dt_params        => \%dt_params
123 });
124
125 is( $search_results->{ iTotalDisplayRecords }, 1,
126     "John Doe has only one match on $branchcode (Bug 12595)");
127
128 ok( $search_results->{ patrons }[0]->{ cardnumber } eq $john_doe{ cardnumber }
129     && ! $search_results->{ patrons }[1],
130     "John Doe is the only match (Bug 12595)");
131
132 # Search "Jane Doe"
133 $search_results = C4::Utils::DataTables::Members::search({
134     searchmember     => "Jane Doe",
135     searchfieldstype => 'standard',
136     searchtype       => 'contain',
137     branchcode       => $branchcode,
138     dt_params        => \%dt_params
139 });
140
141 is( $search_results->{ iTotalDisplayRecords }, 1,
142     "Jane Doe has only one match on $branchcode (Bug 12595)");
143
144 is( $search_results->{ patrons }[0]->{ cardnumber },
145     $jane_doe{ cardnumber },
146     "Jane Doe is the only match (Bug 12595)");
147
148 # Search "John"
149 $search_results = C4::Utils::DataTables::Members::search({
150     searchmember     => "John",
151     searchfieldstype => 'standard',
152     searchtype       => 'contain',
153     branchcode       => $branchcode,
154     dt_params        => \%dt_params
155 });
156
157 is( $search_results->{ iTotalDisplayRecords }, 2,
158     "There are two John at $branchcode");
159
160 is( $search_results->{ patrons }[0]->{ cardnumber },
161     $john_doe{ cardnumber },
162     "John Doe is the first result");
163
164 is( $search_results->{ patrons }[1]->{ cardnumber },
165     $john_smith{ cardnumber },
166     "John Smith is the second result");
167
168 # Search "Doe"
169 $search_results = C4::Utils::DataTables::Members::search({
170     searchmember     => "Doe",
171     searchfieldstype => 'standard',
172     searchtype       => 'contain',
173     branchcode       => $branchcode,
174     dt_params        => \%dt_params
175 });
176
177 is( $search_results->{ iTotalDisplayRecords }, 2,
178     "There are two Doe at $branchcode");
179
180 is( $search_results->{ patrons }[0]->{ cardnumber },
181     $john_doe{ cardnumber },
182     "John Doe is the first result");
183
184 is( $search_results->{ patrons }[1]->{ cardnumber },
185     $jane_doe{ cardnumber },
186     "Jane Doe is the second result");
187
188 # Search by userid
189 $search_results = C4::Utils::DataTables::Members::search({
190     searchmember     => "john.doe",
191     searchfieldstype => 'standard',
192     searchtype       => 'contain',
193     branchcode       => $branchcode,
194     dt_params        => \%dt_params
195 });
196
197 is( $search_results->{ iTotalDisplayRecords }, 1,
198     "John Doe is found by userid, standard search (Bug 14782)");
199
200 $search_results = C4::Utils::DataTables::Members::search({
201     searchmember     => "john.doe",
202     searchfieldstype => 'userid',
203     searchtype       => 'contain',
204     branchcode       => $branchcode,
205     dt_params        => \%dt_params
206 });
207
208 is( $search_results->{ iTotalDisplayRecords }, 1,
209     "John Doe is found by userid, userid search (Bug 14782)");
210
211 my $attribute_type = C4::Members::AttributeTypes->new( 'ATM_1', 'my attribute type' );
212 $attribute_type->{staff_searchable} = 1;
213 $attribute_type->store;
214
215
216 C4::Members::Attributes::SetBorrowerAttributes(
217     $john_doe{borrowernumber}, [ { code => $attribute_type->{code}, value => 'the default value for a common user' } ]
218 );
219 C4::Members::Attributes::SetBorrowerAttributes(
220     $jane_doe{borrowernumber}, [ { code => $attribute_type->{code}, value => 'the default value for another common user' } ]
221 );
222
223 t::lib::Mocks::mock_preference('ExtendedPatronAttributes', 1);
224 $search_results = C4::Utils::DataTables::Members::search({
225     searchmember     => "common user",
226     searchfieldstype => 'standard',
227     searchtype       => 'contain',
228     branchcode       => $branchcode,
229     dt_params        => \%dt_params
230 });
231
232 is( $search_results->{ iTotalDisplayRecords}, 2, "There are 2 common users" );
233
234 t::lib::Mocks::mock_preference('ExtendedPatronAttributes', 0);
235 $search_results = C4::Utils::DataTables::Members::search({
236     searchmember     => "common user",
237     searchfieldstype => 'standard',
238     searchtype       => 'contain',
239     branchcode       => $branchcode,
240     dt_params        => \%dt_params
241 });
242 is( $search_results->{ iTotalDisplayRecords}, 0, "There are still 2 common users, but the patron attribute is not searchable " );
243
244 $search_results = C4::Utils::DataTables::Members::search({
245     searchmember     => "Jean Paul",
246     searchfieldstype => 'standard',
247     searchtype       => 'start_with',
248     branchcode       => $branchcode,
249     dt_params        => \%dt_params
250 });
251
252 is( $search_results->{ iTotalDisplayRecords }, 1,
253     "Jean Paul Dupont is found using start with and two terms search 'Jean Paul' (Bug 15252)");
254
255 $search_results = C4::Utils::DataTables::Members::search({
256     searchmember     => "Jean Pau",
257     searchfieldstype => 'standard',
258     searchtype       => 'start_with',
259     branchcode       => $branchcode,
260     dt_params        => \%dt_params
261 });
262
263 is( $search_results->{ iTotalDisplayRecords }, 1,
264     "Jean Paul Dupont is found using start with and two terms search 'Jean Pau' (Bug 15252)");
265
266 $search_results = C4::Utils::DataTables::Members::search({
267     searchmember     => "Jea Pau",
268     searchfieldstype => 'standard',
269     searchtype       => 'start_with',
270     branchcode       => $branchcode,
271     dt_params        => \%dt_params
272 });
273
274 is( $search_results->{ iTotalDisplayRecords }, 0,
275     "Jean Paul Dupont is not found using start with and two terms search 'Jea Pau' (Bug 15252)");
276
277 $search_results = C4::Utils::DataTables::Members::search({
278     searchmember     => "Jea Pau",
279     searchfieldstype => 'standard',
280     searchtype       => 'contain',
281     branchcode       => $branchcode,
282     dt_params        => \%dt_params
283 });
284
285 is( $search_results->{ iTotalDisplayRecords }, 1,
286     "Jean Paul Dupont is found using contains and two terms search 'Jea Pau' (Bug 15252)");
287
288 # End
289 $dbh->rollback;
290
291 1;