Bug 12595: (regression 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 => 11;
21
22 use C4::Context;
23 use C4::Branch;
24 use C4::Members;
25
26 use_ok( "C4::Utils::DataTables::Members" );
27
28 my $dbh = C4::Context->dbh;
29 my $res;
30
31 # Start transaction
32 $dbh->{AutoCommit} = 0;
33 $dbh->{RaiseError} = 1;
34
35 # Pick a categorycode from the DB
36 my @categories   = C4::Category->all;
37 my $categorycode = $categories[0]->categorycode;
38 # Add a new branch so we control what borrowers it has
39 my $branchcode   = "UNC";
40 my $branch_data = {
41     add            => 1,
42     branchcode     => $branchcode,
43     branchname     => 'Universidad Nacional de Cordoba',
44     branchaddress1 => 'Haya de la Torre',
45     branchaddress2 => 'S/N',
46     branchzip      => '5000',
47     branchcity     => 'Cordoba',
48     branchstate    => 'Cordoba',
49     branchcountry  => 'Argentina'
50 };
51 ModBranch( $branch_data );
52
53 my %john_doe = (
54     cardnumber   => '123456',
55     firstname    => 'John',
56     surname      => 'Doe',
57     categorycode => $categorycode,
58     branchcode   => $branchcode,
59     dateofbirth  => '',
60     dateexpiry   => '9999-12-31',
61     userid       => 'john.doe'
62 );
63
64 my %john_smith = (
65     cardnumber   => '234567',
66     firstname    =>  'John',
67     surname      => 'Smith',
68     categorycode => $categorycode,
69     branchcode   => $branchcode,
70     dateofbirth  => '',
71     dateexpiry   => '9999-12-31',
72     userid       => 'john.smith'
73 );
74
75 my %jane_doe = (
76     cardnumber   => '345678',
77     firstname    =>  'Jane',
78     surname      => 'Doe',
79     categorycode => $categorycode,
80     branchcode   => $branchcode,
81     dateofbirth  => '',
82     dateexpiry   => '9999-12-31',
83     userid       => 'jane.doe'
84 );
85
86 $res = AddMember( %john_doe );
87 warn "Error adding John Doe, check your tests" unless $res;
88 $res = AddMember( %john_smith );
89 warn "Error adding John Smith, check your tests" unless $res;
90 $res = AddMember( %jane_doe );
91 warn "Error adding Jane Doe, check your tests" unless $res;
92
93 # Set common datatables params
94 my %dt_params = (
95     iDisplayLength   => 10,
96     iDisplayStart    => 0
97 );
98
99 # Search "John Doe"
100 my $search_results = C4::Utils::DataTables::Members::search({
101     searchmember     => "John Doe",
102     searchfieldstype => 'standard',
103     searchtype       => 'contains',
104     branchcode       => $branchcode,
105     dt_params        => \%dt_params
106 });
107
108 is( $search_results->{ iTotalDisplayRecords }, 1,
109     "John Doe has only one match on $branchcode (Bug 12595)");
110
111 ok( $search_results->{ patrons }[0]->{ cardnumber } eq $john_doe{ cardnumber }
112     && ! $search_results->{ patrons }[1],
113     "John Doe is the only match (Bug 12595)");
114
115 # Search "Jane Doe"
116 $search_results = C4::Utils::DataTables::Members::search({
117     searchmember     => "Jane Doe",
118     searchfieldstype => 'standard',
119     searchtype       => 'contains',
120     branchcode       => $branchcode,
121     dt_params        => \%dt_params
122 });
123
124 is( $search_results->{ iTotalDisplayRecords }, 1,
125     "Jane Doe has only one match on $branchcode (Bug 12595)");
126
127 is( $search_results->{ patrons }[0]->{ cardnumber },
128     $jane_doe{ cardnumber },
129     "Jane Doe is the only match (Bug 12595)");
130
131 # Search "John"
132 $search_results = C4::Utils::DataTables::Members::search({
133     searchmember     => "John",
134     searchfieldstype => 'standard',
135     searchtype       => 'contains',
136     branchcode       => $branchcode,
137     dt_params        => \%dt_params
138 });
139
140 is( $search_results->{ iTotalDisplayRecords }, 2,
141     "There are two John at $branchcode");
142
143 is( $search_results->{ patrons }[0]->{ cardnumber },
144     $john_doe{ cardnumber },
145     "John Doe is the first result");
146
147 is( $search_results->{ patrons }[1]->{ cardnumber },
148     $john_smith{ cardnumber },
149     "John Smith is the second result");
150
151 # Search "Doe"
152 $search_results = C4::Utils::DataTables::Members::search({
153     searchmember     => "Doe",
154     searchfieldstype => 'standard',
155     searchtype       => 'contains',
156     branchcode       => $branchcode,
157     dt_params        => \%dt_params
158 });
159
160 is( $search_results->{ iTotalDisplayRecords }, 2,
161     "There are two Doe at $branchcode");
162
163 is( $search_results->{ patrons }[0]->{ cardnumber },
164     $john_doe{ cardnumber },
165     "John Doe is the first result");
166
167 is( $search_results->{ patrons }[1]->{ cardnumber },
168     $jane_doe{ cardnumber },
169     "Jane Doe is the second result");
170
171 $dbh->rollback;
172
173 1;