Bug 13264: Follow up: in opac_utf8.t insert also delete of biblio
[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 => 13;
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 $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};
96
97 # Set common datatables params
98 my %dt_params = (
99     iDisplayLength   => 10,
100     iDisplayStart    => 0
101 );
102
103 # Search "John Doe"
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
110 });
111
112 is( $search_results->{ iTotalDisplayRecords }, 1,
113     "John Doe has only one match on $branchcode (Bug 12595)");
114
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)");
118
119 # Search "Jane Doe"
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
126 });
127
128 is( $search_results->{ iTotalDisplayRecords }, 1,
129     "Jane Doe has only one match on $branchcode (Bug 12595)");
130
131 is( $search_results->{ patrons }[0]->{ cardnumber },
132     $jane_doe{ cardnumber },
133     "Jane Doe is the only match (Bug 12595)");
134
135 # Search "John"
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
142 });
143
144 is( $search_results->{ iTotalDisplayRecords }, 2,
145     "There are two John at $branchcode");
146
147 is( $search_results->{ patrons }[0]->{ cardnumber },
148     $john_doe{ cardnumber },
149     "John Doe is the first result");
150
151 is( $search_results->{ patrons }[1]->{ cardnumber },
152     $john_smith{ cardnumber },
153     "John Smith is the second result");
154
155 # Search "Doe"
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
162 });
163
164 is( $search_results->{ iTotalDisplayRecords }, 2,
165     "There are two Doe at $branchcode");
166
167 is( $search_results->{ patrons }[0]->{ cardnumber },
168     $john_doe{ cardnumber },
169     "John Doe is the first result");
170
171 is( $search_results->{ patrons }[1]->{ cardnumber },
172     $jane_doe{ cardnumber },
173     "Jane Doe is the second result");
174
175 my $attribute_type = C4::Members::AttributeTypes->new( 'ATM_1', 'my attribute type' );
176 $attribute_type->{staff_searchable} = 1;
177 $attribute_type->store;
178
179
180 C4::Members::Attributes::SetBorrowerAttributes(
181     $john_doe{borrowernumber}, [ { code => $attribute_type->{code}, value => 'the default value for a common user' } ]
182 );
183 C4::Members::Attributes::SetBorrowerAttributes(
184     $jane_doe{borrowernumber}, [ { code => $attribute_type->{code}, value => 'the default value for another common user' } ]
185 );
186
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
194 });
195
196 is( $search_results->{ iTotalDisplayRecords}, 2, "There are 2 common users" );
197
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
205 });
206 is( $search_results->{ iTotalDisplayRecords}, 0, "There are still 2 common users, but the patron attribute is not searchable " );
207
208 $dbh->rollback;
209
210 1;