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;
27 use Koha::Patron::Categories;
28 use Koha::Virtualshelves;
30 use_ok( "C4::Utils::DataTables::VirtualShelves" );
32 my $schema = Koha::Database->new->schema;
33 $schema->storage->txn_begin;
34 my $dbh = C4::Context->dbh;
36 $dbh->do(q|DELETE FROM virtualshelves|);
38 # Pick a categorycode from the DB
39 my @categories = Koha::Patron::Categories->search_limited;
40 my $categorycode = $categories[0]->categorycode;
41 my $branchcode = "ABC";
43 branchcode => $branchcode,
44 branchname => 'my branchname',
46 Koha::Library->new( $branch_data )->store;
49 cardnumber => '123456',
52 categorycode => $categorycode,
53 branchcode => $branchcode,
55 dateexpiry => '9999-12-31',
60 cardnumber => '234567',
63 categorycode => $categorycode,
64 branchcode => $branchcode,
66 dateexpiry => '9999-12-31',
70 cardnumber => '345678',
73 categorycode => $categorycode,
74 branchcode => $branchcode,
76 dateexpiry => '9999-12-31',
77 userid => 'john.smith',
80 $john_doe{borrowernumber} = Koha::Patron->new( \%john_doe )->store->borrowernumber;
81 $jane_doe{borrowernumber} = Koha::Patron->new( \%jane_doe )->store->borrowernumber;
82 $john_smith{borrowernumber} = Koha::Patron->new( \%john_smith )->store->borrowernumber;
84 my $shelf1 = Koha::Virtualshelf->new(
86 shelfname => 'my first private list (empty)',
87 category => 1, # private
88 sortfield => 'author',
89 owner => $john_doe{borrowernumber},
93 my $shelf2 = Koha::Virtualshelf->new(
95 shelfname => 'my second private list',
96 category => 1, # private
98 owner => $john_doe{borrowernumber},
101 my $biblionumber1 = _add_biblio('title 1');
102 my $biblionumber2 = _add_biblio('title 2');
103 my $biblionumber3 = _add_biblio('title 3');
104 my $biblionumber4 = _add_biblio('title 4');
105 my $biblionumber5 = _add_biblio('title 5');
106 $shelf2->add_biblio( $biblionumber1, $john_doe{borrowernumber} );
107 $shelf2->add_biblio( $biblionumber2, $john_doe{borrowernumber} );
108 $shelf2->add_biblio( $biblionumber3, $john_doe{borrowernumber} );
109 $shelf2->add_biblio( $biblionumber4, $john_doe{borrowernumber} );
110 $shelf2->add_biblio( $biblionumber5, $john_doe{borrowernumber} );
112 my $shelf3 = Koha::Virtualshelf->new(
114 shelfname => 'The first public list',
115 category => 2, # public
116 sortfield => 'author',
117 owner => $jane_doe{borrowernumber},
120 my $biblionumber6 = _add_biblio('title 6');
121 my $biblionumber7 = _add_biblio('title 7');
122 my $biblionumber8 = _add_biblio('title 8');
123 $shelf3->add_biblio( $biblionumber6, $jane_doe{borrowernumber} );
124 $shelf3->add_biblio( $biblionumber7, $jane_doe{borrowernumber} );
125 $shelf3->add_biblio( $biblionumber8, $jane_doe{borrowernumber} );
127 my $shelf4 = Koha::Virtualshelf->new(
129 shelfname => 'my second public list',
130 category => 2, # public
131 sortfield => 'title',
132 owner => $jane_doe{borrowernumber},
135 my $biblionumber9 = _add_biblio('title 9');
136 my $biblionumber10 = _add_biblio('title 10');
137 my $biblionumber11 = _add_biblio('title 11');
138 my $biblionumber12 = _add_biblio('title 12');
139 $shelf3->add_biblio( $biblionumber9, $jane_doe{borrowernumber} );
140 $shelf3->add_biblio( $biblionumber10, $jane_doe{borrowernumber} );
141 $shelf3->add_biblio( $biblionumber11, $jane_doe{borrowernumber} );
142 $shelf3->add_biblio( $biblionumber12, $jane_doe{borrowernumber} );
144 my $shelf5 = Koha::Virtualshelf->new(
146 shelfname => 'my third private list',
147 category => 1, # private
148 sortfield => 'title',
149 owner => $jane_doe{borrowernumber},
152 my $biblionumber13 = _add_biblio('title 13');
153 my $biblionumber14 = _add_biblio('title 14');
154 my $biblionumber15 = _add_biblio('title 15');
155 my $biblionumber16 = _add_biblio('title 16');
156 my $biblionumber17 = _add_biblio('title 17');
157 my $biblionumber18 = _add_biblio('title 18');
158 $shelf5->add_biblio( $biblionumber13, $jane_doe{borrowernumber} );
159 $shelf5->add_biblio( $biblionumber14, $jane_doe{borrowernumber} );
160 $shelf5->add_biblio( $biblionumber15, $jane_doe{borrowernumber} );
161 $shelf5->add_biblio( $biblionumber16, $jane_doe{borrowernumber} );
162 $shelf5->add_biblio( $biblionumber17, $jane_doe{borrowernumber} );
163 $shelf5->add_biblio( $biblionumber18, $jane_doe{borrowernumber} );
165 for my $i ( 6 .. 15 ) {
166 Koha::Virtualshelf->new(
168 shelfname => "another public list $i",
170 owner => $john_smith{borrowernumber},
175 # Set common datatables params
177 iDisplayLength => 10,
182 C4::Context->_new_userenv ('DUMMY_SESSION_ID');
183 C4::Context->set_userenv($john_doe{borrowernumber}, $john_doe{userid}, 'usercnum', 'First name', 'Surname', 'MYLIBRARY', 'My Library', 0);
185 # Search private lists by title
186 $search_results = C4::Utils::DataTables::VirtualShelves::search({
188 dt_params => \%dt_params,
192 is( $search_results->{ iTotalRecords }, 2,
193 "There should be 2 private shelves in total" );
195 is( $search_results->{ iTotalDisplayRecords }, 2,
196 "There should be 2 private shelves with title like '%ist%" );
198 is( @{ $search_results->{ shelves } }, 2,
199 "There should be 2 private shelves returned" );
201 # Search by type only
202 $search_results = C4::Utils::DataTables::VirtualShelves::search({
203 dt_params => \%dt_params,
206 is( $search_results->{ iTotalRecords }, 12,
207 "There should be 12 public shelves in total" );
209 is( $search_results->{ iTotalDisplayRecords }, 12,
210 "There should be 12 private shelves" );
212 is( @{ $search_results->{ shelves } }, 10,
213 "There should be 10 public shelves returned" );
216 $search_results = C4::Utils::DataTables::VirtualShelves::search({
218 dt_params => \%dt_params,
221 is( $search_results->{ iTotalRecords }, 12,
222 "There should be 12 public shelves in total" );
224 is( $search_results->{ iTotalDisplayRecords }, 2,
225 "There should be 1 public shelves for jane" );
227 is( @{ $search_results->{ shelves } }, 2,
228 "There should be 1 public shelf returned" );
230 # Search by owner and shelf name
231 $search_results = C4::Utils::DataTables::VirtualShelves::search({
233 shelfname => "public list 1",
234 dt_params => \%dt_params,
237 is( $search_results->{ iTotalRecords }, 12,
238 "There should be 12 public shelves in total" );
240 is( $search_results->{ iTotalDisplayRecords }, 6,
241 "There should be 6 public shelves for john with name like %public list 1%" );
243 is( @{ $search_results->{ shelves } }, 6,
244 "There should be 6 public chalves returned" );
248 my $biblio = MARC::Record->new();
249 $biblio->append_fields(
250 MARC::Field->new('245', ' ', ' ', a => $title),
252 my ($biblionumber, $biblioitemnumber) = AddBiblio($biblio, '');
253 return $biblionumber;