Bug 18292: Remove return 1 statements in tests
[koha.git] / t / db_dependent / Utils / Datatables_Virtualshelves.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::Biblio;
23 use C4::Context;
24 use C4::Members;
25
26 use Koha::Library;
27 use Koha::Patron::Categories;
28 use Koha::Virtualshelf;
29 use Koha::Virtualshelves;
30
31 use_ok( "C4::Utils::DataTables::VirtualShelves" );
32
33 my $dbh = C4::Context->dbh;
34
35 # Start transaction
36 $dbh->{AutoCommit} = 0;
37 $dbh->{RaiseError} = 1;
38
39 $dbh->do(q|DELETE FROM virtualshelves|);
40
41 # Pick a categorycode from the DB
42 my @categories   = Koha::Patron::Categories->search_limited;
43 my $categorycode = $categories[0]->categorycode;
44 my $branchcode   = "ABC";
45 my $branch_data = {
46     branchcode     => $branchcode,
47     branchname     => 'my branchname',
48 };
49 Koha::Library->new( $branch_data )->store;
50
51 my %john_doe = (
52     cardnumber   => '123456',
53     firstname    => 'John',
54     surname      => 'Doe',
55     categorycode => $categorycode,
56     branchcode   => $branchcode,
57     dateofbirth  => '',
58     dateexpiry   => '9999-12-31',
59     userid       => 'john.doe',
60 );
61
62 my %jane_doe = (
63     cardnumber   => '234567',
64     firstname    =>  'Jane',
65     surname      => 'Doe',
66     categorycode => $categorycode,
67     branchcode   => $branchcode,
68     dateofbirth  => '',
69     dateexpiry   => '9999-12-31',
70     userid       => 'jane.doe',
71 );
72 my %john_smith = (
73     cardnumber   => '345678',
74     firstname    =>  'John',
75     surname      => 'Smith',
76     categorycode => $categorycode,
77     branchcode   => $branchcode,
78     dateofbirth  => '',
79     dateexpiry   => '9999-12-31',
80     userid       => 'john.smith',
81 );
82
83 $john_doe{borrowernumber} = AddMember( %john_doe );
84 $jane_doe{borrowernumber} = AddMember( %jane_doe );
85 $john_smith{borrowernumber} = AddMember( %john_smith );
86
87 my $shelf1 = Koha::Virtualshelf->new(
88     {
89         shelfname => 'my first private list (empty)',
90         category  => 1, # private
91         sortfield => 'author',
92         owner     => $john_doe{borrowernumber},
93     }
94 )->store;
95
96 my $shelf2 = Koha::Virtualshelf->new(
97     {
98         shelfname => 'my second private list',
99         category  => 1, # private
100         sortfield => 'title',
101         owner     => $john_doe{borrowernumber},
102     }
103 )->store;
104 my $biblionumber1 = _add_biblio('title 1');
105 my $biblionumber2 = _add_biblio('title 2');
106 my $biblionumber3 = _add_biblio('title 3');
107 my $biblionumber4 = _add_biblio('title 4');
108 my $biblionumber5 = _add_biblio('title 5');
109 $shelf2->add_biblio( $biblionumber1, $john_doe{borrowernumber} );
110 $shelf2->add_biblio( $biblionumber2, $john_doe{borrowernumber} );
111 $shelf2->add_biblio( $biblionumber3, $john_doe{borrowernumber} );
112 $shelf2->add_biblio( $biblionumber4, $john_doe{borrowernumber} );
113 $shelf2->add_biblio( $biblionumber5, $john_doe{borrowernumber} );
114
115 my $shelf3 = Koha::Virtualshelf->new(
116     {
117         shelfname => 'The first public list',
118         category  => 2, # public
119         sortfield => 'author',
120         owner     => $jane_doe{borrowernumber},
121     }
122 )->store;
123 my $biblionumber6 = _add_biblio('title 6');
124 my $biblionumber7 = _add_biblio('title 7');
125 my $biblionumber8 = _add_biblio('title 8');
126 $shelf3->add_biblio( $biblionumber6, $jane_doe{borrowernumber} );
127 $shelf3->add_biblio( $biblionumber7, $jane_doe{borrowernumber} );
128 $shelf3->add_biblio( $biblionumber8, $jane_doe{borrowernumber} );
129
130 my $shelf4 = Koha::Virtualshelf->new(
131     {
132         shelfname => 'my second public list',
133         category  => 2, # public
134         sortfield => 'title',
135         owner     => $jane_doe{borrowernumber},
136     }
137 )->store;
138 my $biblionumber9  = _add_biblio('title 9');
139 my $biblionumber10 = _add_biblio('title 10');
140 my $biblionumber11 = _add_biblio('title 11');
141 my $biblionumber12 = _add_biblio('title 12');
142 $shelf3->add_biblio( $biblionumber9, $jane_doe{borrowernumber} );
143 $shelf3->add_biblio( $biblionumber10, $jane_doe{borrowernumber} );
144 $shelf3->add_biblio( $biblionumber11, $jane_doe{borrowernumber} );
145 $shelf3->add_biblio( $biblionumber12, $jane_doe{borrowernumber} );
146
147 my $shelf5 = Koha::Virtualshelf->new(
148     {
149         shelfname => 'my third private list',
150         category  => 1, # private
151         sortfield => 'title',
152         owner     => $jane_doe{borrowernumber},
153     }
154 )->store;
155 my $biblionumber13 = _add_biblio('title 13');
156 my $biblionumber14 = _add_biblio('title 14');
157 my $biblionumber15 = _add_biblio('title 15');
158 my $biblionumber16 = _add_biblio('title 16');
159 my $biblionumber17 = _add_biblio('title 17');
160 my $biblionumber18 = _add_biblio('title 18');
161 $shelf5->add_biblio( $biblionumber13, $jane_doe{borrowernumber} );
162 $shelf5->add_biblio( $biblionumber14, $jane_doe{borrowernumber} );
163 $shelf5->add_biblio( $biblionumber15, $jane_doe{borrowernumber} );
164 $shelf5->add_biblio( $biblionumber16, $jane_doe{borrowernumber} );
165 $shelf5->add_biblio( $biblionumber17, $jane_doe{borrowernumber} );
166 $shelf5->add_biblio( $biblionumber18, $jane_doe{borrowernumber} );
167
168 for my $i ( 6 .. 15 ) {
169     Koha::Virtualshelf->new(
170         {
171             shelfname => "another public list $i",
172             category  => 2,
173             owner     => $john_smith{borrowernumber},
174         }
175     )->store;
176 }
177
178 # Set common datatables params
179 my %dt_params = (
180     iDisplayLength   => 10,
181     iDisplayStart    => 0
182 );
183 my $search_results;
184
185 C4::Context->_new_userenv ('DUMMY_SESSION_ID');
186 C4::Context->set_userenv($john_doe{borrowernumber}, $john_doe{userid}, 'usercnum', 'First name', 'Surname', 'MYLIBRARY', 'My Library', 0);
187
188 # Search private lists by title
189 $search_results = C4::Utils::DataTables::VirtualShelves::search({
190     shelfname => "ist",
191     dt_params => \%dt_params,
192     type => 1,
193 });
194
195 is( $search_results->{ iTotalRecords }, 2,
196     "There should be 2 private shelves in total" );
197
198 is( $search_results->{ iTotalDisplayRecords }, 2,
199     "There should be 2 private shelves with title like '%ist%" );
200
201 is( @{ $search_results->{ shelves } }, 2,
202     "There should be 2 private shelves returned" );
203
204 # Search by type only
205 $search_results = C4::Utils::DataTables::VirtualShelves::search({
206     dt_params => \%dt_params,
207     type => 2,
208 });
209 is( $search_results->{ iTotalRecords }, 12,
210     "There should be 12 public shelves in total" );
211
212 is( $search_results->{ iTotalDisplayRecords }, 12,
213     "There should be 12 private shelves" );
214
215 is( @{ $search_results->{ shelves } }, 10,
216     "There should be 10 public shelves returned" );
217
218 # Search by owner
219 $search_results = C4::Utils::DataTables::VirtualShelves::search({
220     owner => "jane",
221     dt_params => \%dt_params,
222     type => 2,
223 });
224 is( $search_results->{ iTotalRecords }, 12,
225     "There should be 12 public shelves in total" );
226
227 is( $search_results->{ iTotalDisplayRecords }, 2,
228     "There should be 1 public shelves for jane" );
229
230 is( @{ $search_results->{ shelves } }, 2,
231     "There should be 1 public shelf returned" );
232
233 # Search by owner and shelf name
234 $search_results = C4::Utils::DataTables::VirtualShelves::search({
235     owner => "smith",
236     shelfname => "public list 1",
237     dt_params => \%dt_params,
238     type => 2,
239 });
240 is( $search_results->{ iTotalRecords }, 12,
241     "There should be 12 public shelves in total" );
242
243 is( $search_results->{ iTotalDisplayRecords }, 6,
244     "There should be 6 public shelves for john with name like %public list 1%" );
245
246 is( @{ $search_results->{ shelves } }, 6,
247     "There should be 6 public chalves returned" );
248
249 sub _add_biblio {
250     my ( $title ) = @_;
251     my $biblio = MARC::Record->new();
252     $biblio->append_fields(
253         MARC::Field->new('245', ' ', ' ', a => $title),
254     );
255     my ($biblionumber, $biblioitemnumber) = AddBiblio($biblio, '');
256     return $biblionumber;
257 }
258