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;
22 use C4::Biblio qw( AddBiblio );
27 use Koha::Patron::Categories;
28 use Koha::Virtualshelves;
32 use_ok( "C4::Utils::DataTables::VirtualShelves" );
34 my $schema = Koha::Database->new->schema;
35 $schema->storage->txn_begin;
36 my $dbh = C4::Context->dbh;
38 $dbh->do(q|DELETE FROM virtualshelves|);
40 # Pick a categorycode from the DB
41 my @categories = Koha::Patron::Categories->search_with_library_limits;
42 my $categorycode = $categories[0]->categorycode;
43 my $branchcode = "ABC";
45 branchcode => $branchcode,
46 branchname => 'my branchname',
48 Koha::Library->new( $branch_data )->store;
51 cardnumber => '123456',
54 categorycode => $categorycode,
55 branchcode => $branchcode,
57 dateexpiry => '9999-12-31',
62 cardnumber => '234567',
65 categorycode => $categorycode,
66 branchcode => $branchcode,
68 dateexpiry => '9999-12-31',
72 cardnumber => '345678',
75 categorycode => $categorycode,
76 branchcode => $branchcode,
78 dateexpiry => '9999-12-31',
79 userid => 'john.smith',
82 my $john_doe_patron = Koha::Patron->new( \%john_doe )->store;
83 $john_doe{borrowernumber} = $john_doe_patron->borrowernumber;
84 $jane_doe{borrowernumber} = Koha::Patron->new( \%jane_doe )->store->borrowernumber;
85 $john_smith{borrowernumber} = Koha::Patron->new( \%john_smith )->store->borrowernumber;
87 my $shelf1 = Koha::Virtualshelf->new(
89 shelfname => 'my first private list (empty)',
90 category => 1, # private
91 sortfield => 'author',
92 owner => $john_doe{borrowernumber},
96 my $shelf2 = Koha::Virtualshelf->new(
98 shelfname => 'my second private list',
99 category => 1, # private
100 sortfield => 'title',
101 owner => $john_doe{borrowernumber},
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} );
115 my $shelf3 = Koha::Virtualshelf->new(
117 shelfname => 'The first public list',
118 category => 2, # public
119 sortfield => 'author',
120 owner => $jane_doe{borrowernumber},
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} );
130 my $shelf4 = Koha::Virtualshelf->new(
132 shelfname => 'my second public list',
133 category => 2, # public
134 sortfield => 'title',
135 owner => $jane_doe{borrowernumber},
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} );
147 my $shelf5 = Koha::Virtualshelf->new(
149 shelfname => 'my third private list',
150 category => 1, # private
151 sortfield => 'title',
152 owner => $jane_doe{borrowernumber},
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} );
168 for my $i ( 6 .. 15 ) {
169 Koha::Virtualshelf->new(
171 shelfname => "another public list $i",
173 owner => $john_smith{borrowernumber},
178 # Set common datatables params
180 iDisplayLength => 10,
185 t::lib::Mocks::mock_userenv({ patron => $john_doe_patron });
187 # Search private lists by title
188 $search_results = C4::Utils::DataTables::VirtualShelves::search({
190 dt_params => \%dt_params,
194 is( $search_results->{ iTotalRecords }, 2,
195 "There should be 2 private shelves in total" );
197 is( $search_results->{ iTotalDisplayRecords }, 2,
198 "There should be 2 private shelves with title like '%ist%" );
200 is( @{ $search_results->{ shelves } }, 2,
201 "There should be 2 private shelves returned" );
203 # Search by type only
204 $search_results = C4::Utils::DataTables::VirtualShelves::search({
205 dt_params => \%dt_params,
208 is( $search_results->{ iTotalRecords }, 12,
209 "There should be 12 public shelves in total" );
211 is( $search_results->{ iTotalDisplayRecords }, 12,
212 "There should be 12 private shelves" );
214 is( @{ $search_results->{ shelves } }, 10,
215 "There should be 10 public shelves returned" );
218 $search_results = C4::Utils::DataTables::VirtualShelves::search({
220 dt_params => \%dt_params,
223 is( $search_results->{ iTotalRecords }, 12,
224 "There should be 12 public shelves in total" );
226 is( $search_results->{ iTotalDisplayRecords }, 2,
227 "There should be 1 public shelves for jane" );
229 is( @{ $search_results->{ shelves } }, 2,
230 "There should be 1 public shelf returned" );
232 # Search by owner and shelf name
233 $search_results = C4::Utils::DataTables::VirtualShelves::search({
235 shelfname => "public list 1",
236 dt_params => \%dt_params,
239 is( $search_results->{ iTotalRecords }, 12,
240 "There should be 12 public shelves in total" );
242 is( $search_results->{ iTotalDisplayRecords }, 6,
243 "There should be 6 public shelves for john with name like %public list 1%" );
245 is( @{ $search_results->{ shelves } }, 6,
246 "There should be 6 public chalves returned" );
250 my $biblio = MARC::Record->new();
251 $biblio->append_fields(
252 MARC::Field->new('245', ' ', ' ', a => $title),
254 my ($biblionumber, $biblioitemnumber) = AddBiblio($biblio, '');
255 return $biblionumber;