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 => 13;
26 use C4::Members::Attributes;
27 use C4::Members::AttributeTypes;
31 use_ok( "C4::Utils::DataTables::Members" );
33 my $dbh = C4::Context->dbh;
36 $dbh->{AutoCommit} = 0;
37 $dbh->{RaiseError} = 1;
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";
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 ModBranch( $branch_data );
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 $john_doe{borrowernumber} = AddMember( %john_doe );
91 warn "Error adding John Doe, check your tests" unless $john_doe{borrowernumber};
92 $john_smith{borrowernumber} = AddMember( %john_smith );
93 warn "Error adding John Smith, check your tests" unless $john_smith{borrowernumber};
94 $jane_doe{borrowernumber} = AddMember( %jane_doe );
95 warn "Error adding Jane Doe, check your tests" unless $jane_doe{borrowernumber};
97 # Set common datatables params
104 my $search_results = C4::Utils::DataTables::Members::search({
105 searchmember => "John Doe",
106 searchfieldstype => 'standard',
107 searchtype => 'contains',
108 branchcode => $branchcode,
109 dt_params => \%dt_params
112 is( $search_results->{ iTotalDisplayRecords }, 1,
113 "John Doe has only one match on $branchcode (Bug 12595)");
115 ok( $search_results->{ patrons }[0]->{ cardnumber } eq $john_doe{ cardnumber }
116 && ! $search_results->{ patrons }[1],
117 "John Doe is the only match (Bug 12595)");
120 $search_results = C4::Utils::DataTables::Members::search({
121 searchmember => "Jane Doe",
122 searchfieldstype => 'standard',
123 searchtype => 'contains',
124 branchcode => $branchcode,
125 dt_params => \%dt_params
128 is( $search_results->{ iTotalDisplayRecords }, 1,
129 "Jane Doe has only one match on $branchcode (Bug 12595)");
131 is( $search_results->{ patrons }[0]->{ cardnumber },
132 $jane_doe{ cardnumber },
133 "Jane Doe is the only match (Bug 12595)");
136 $search_results = C4::Utils::DataTables::Members::search({
137 searchmember => "John",
138 searchfieldstype => 'standard',
139 searchtype => 'contains',
140 branchcode => $branchcode,
141 dt_params => \%dt_params
144 is( $search_results->{ iTotalDisplayRecords }, 2,
145 "There are two John at $branchcode");
147 is( $search_results->{ patrons }[0]->{ cardnumber },
148 $john_doe{ cardnumber },
149 "John Doe is the first result");
151 is( $search_results->{ patrons }[1]->{ cardnumber },
152 $john_smith{ cardnumber },
153 "John Smith is the second result");
156 $search_results = C4::Utils::DataTables::Members::search({
157 searchmember => "Doe",
158 searchfieldstype => 'standard',
159 searchtype => 'contains',
160 branchcode => $branchcode,
161 dt_params => \%dt_params
164 is( $search_results->{ iTotalDisplayRecords }, 2,
165 "There are two Doe at $branchcode");
167 is( $search_results->{ patrons }[0]->{ cardnumber },
168 $john_doe{ cardnumber },
169 "John Doe is the first result");
171 is( $search_results->{ patrons }[1]->{ cardnumber },
172 $jane_doe{ cardnumber },
173 "Jane Doe is the second result");
175 my $attribute_type = C4::Members::AttributeTypes->new( 'ATM_1', 'my attribute type' );
176 $attribute_type->{staff_searchable} = 1;
177 $attribute_type->store;
180 C4::Members::Attributes::SetBorrowerAttributes(
181 $john_doe{borrowernumber}, [ { code => $attribute_type->{code}, value => 'the default value for a common user' } ]
183 C4::Members::Attributes::SetBorrowerAttributes(
184 $jane_doe{borrowernumber}, [ { code => $attribute_type->{code}, value => 'the default value for another common user' } ]
187 t::lib::Mocks::mock_preference('ExtendedPatronAttributes', 1);
188 $search_results = C4::Utils::DataTables::Members::search({
189 searchmember => "common user",
190 searchfieldstype => 'standard',
191 searchtype => 'contains',
192 branchcode => $branchcode,
193 dt_params => \%dt_params
196 is( $search_results->{ iTotalDisplayRecords}, 2, "There are 2 common users" );
198 t::lib::Mocks::mock_preference('ExtendedPatronAttributes', 0);
199 $search_results = C4::Utils::DataTables::Members::search({
200 searchmember => "common user",
201 searchfieldstype => 'standard',
202 searchtype => 'contains',
203 branchcode => $branchcode,
204 dt_params => \%dt_params
206 is( $search_results->{ iTotalDisplayRecords}, 0, "There are still 2 common users, but the patron attribute is not searchable " );