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 C4::VirtualShelves;
29 use Koha::Virtualshelf;
30 use Koha::Virtualshelves;
32 use_ok( "C4::Utils::DataTables::VirtualShelves" );
34 my $dbh = C4::Context->dbh;
37 $dbh->{AutoCommit} = 0;
38 $dbh->{RaiseError} = 1;
40 $dbh->do(q|DELETE FROM virtualshelves|);
42 # Pick a categorycode from the DB
43 my @categories = C4::Category->all;
44 my $categorycode = $categories[0]->categorycode;
45 my $branchcode = "ABC";
48 branchcode => $branchcode,
49 branchname => 'my branchname',
51 ModBranch( $branch_data );
54 cardnumber => '123456',
57 categorycode => $categorycode,
58 branchcode => $branchcode,
60 dateexpiry => '9999-12-31',
65 cardnumber => '234567',
68 categorycode => $categorycode,
69 branchcode => $branchcode,
71 dateexpiry => '9999-12-31',
75 cardnumber => '345678',
78 categorycode => $categorycode,
79 branchcode => $branchcode,
81 dateexpiry => '9999-12-31',
82 userid => 'john.smith',
85 $john_doe{borrowernumber} = AddMember( %john_doe );
86 $jane_doe{borrowernumber} = AddMember( %jane_doe );
87 $john_smith{borrowernumber} = AddMember( %john_smith );
89 my $shelf1 = Koha::Virtualshelf->new(
91 shelfname => 'my first private list (empty)',
92 category => 1, # private
93 sortfield => 'author',
94 owner => $john_doe{borrowernumber},
98 my $shelf2 = Koha::Virtualshelf->new(
100 shelfname => 'my second private list',
101 category => 1, # private
102 sortfield => 'title',
103 owner => $john_doe{borrowernumber},
106 my $biblionumber1 = _add_biblio('title 1');
107 my $biblionumber2 = _add_biblio('title 2');
108 my $biblionumber3 = _add_biblio('title 3');
109 my $biblionumber4 = _add_biblio('title 4');
110 my $biblionumber5 = _add_biblio('title 5');
111 $shelf2->add_biblio( $biblionumber1, $john_doe{borrowernumber} );
112 $shelf2->add_biblio( $biblionumber2, $john_doe{borrowernumber} );
113 $shelf2->add_biblio( $biblionumber3, $john_doe{borrowernumber} );
114 $shelf2->add_biblio( $biblionumber4, $john_doe{borrowernumber} );
115 $shelf2->add_biblio( $biblionumber5, $john_doe{borrowernumber} );
117 my $shelf3 = Koha::Virtualshelf->new(
119 shelfname => 'The first public list',
120 category => 2, # public
121 sortfield => 'author',
122 owner => $jane_doe{borrowernumber},
125 my $biblionumber6 = _add_biblio('title 6');
126 my $biblionumber7 = _add_biblio('title 7');
127 my $biblionumber8 = _add_biblio('title 8');
128 $shelf3->add_biblio( $biblionumber6, $jane_doe{borrowernumber} );
129 $shelf3->add_biblio( $biblionumber7, $jane_doe{borrowernumber} );
130 $shelf3->add_biblio( $biblionumber8, $jane_doe{borrowernumber} );
132 my $shelf4 = Koha::Virtualshelf->new(
134 shelfname => 'my second public list',
135 category => 2, # public
136 sortfield => 'title',
137 owner => $jane_doe{borrowernumber},
140 my $biblionumber9 = _add_biblio('title 9');
141 my $biblionumber10 = _add_biblio('title 10');
142 my $biblionumber11 = _add_biblio('title 11');
143 my $biblionumber12 = _add_biblio('title 12');
144 $shelf3->add_biblio( $biblionumber9, $jane_doe{borrowernumber} );
145 $shelf3->add_biblio( $biblionumber10, $jane_doe{borrowernumber} );
146 $shelf3->add_biblio( $biblionumber11, $jane_doe{borrowernumber} );
147 $shelf3->add_biblio( $biblionumber12, $jane_doe{borrowernumber} );
149 my $shelf5 = Koha::Virtualshelf->new(
151 shelfname => 'my third private list',
152 category => 1, # private
153 sortfield => 'title',
154 owner => $jane_doe{borrowernumber},
157 my $biblionumber13 = _add_biblio('title 13');
158 my $biblionumber14 = _add_biblio('title 14');
159 my $biblionumber15 = _add_biblio('title 15');
160 my $biblionumber16 = _add_biblio('title 16');
161 my $biblionumber17 = _add_biblio('title 17');
162 my $biblionumber18 = _add_biblio('title 18');
163 $shelf5->add_biblio( $biblionumber13, $jane_doe{borrowernumber} );
164 $shelf5->add_biblio( $biblionumber14, $jane_doe{borrowernumber} );
165 $shelf5->add_biblio( $biblionumber15, $jane_doe{borrowernumber} );
166 $shelf5->add_biblio( $biblionumber16, $jane_doe{borrowernumber} );
167 $shelf5->add_biblio( $biblionumber17, $jane_doe{borrowernumber} );
168 $shelf5->add_biblio( $biblionumber18, $jane_doe{borrowernumber} );
170 for my $i ( 6 .. 15 ) {
171 Koha::Virtualshelf->new(
173 shelfname => "another public list $i",
175 owner => $john_smith{borrowernumber},
180 # Set common datatables params
182 iDisplayLength => 10,
187 C4::Context->_new_userenv ('DUMMY_SESSION_ID');
188 C4::Context->set_userenv($john_doe{borrowernumber}, $john_doe{userid}, 'usercnum', 'First name', 'Surname', 'MYLIBRARY', 'My Library', 0);
190 # Search private lists by title
191 $search_results = C4::Utils::DataTables::VirtualShelves::search({
193 dt_params => \%dt_params,
197 is( $search_results->{ iTotalRecords }, 2,
198 "There should be 2 private shelves in total" );
200 is( $search_results->{ iTotalDisplayRecords }, 2,
201 "There should be 2 private shelves with title like '%ist%" );
203 is( @{ $search_results->{ shelves } }, 2,
204 "There should be 2 private shelves returned" );
206 # Search by type only
207 $search_results = C4::Utils::DataTables::VirtualShelves::search({
208 dt_params => \%dt_params,
211 is( $search_results->{ iTotalRecords }, 12,
212 "There should be 12 public shelves in total" );
214 is( $search_results->{ iTotalDisplayRecords }, 12,
215 "There should be 12 private shelves" );
217 is( @{ $search_results->{ shelves } }, 10,
218 "There should be 10 public shelves returned" );
221 $search_results = C4::Utils::DataTables::VirtualShelves::search({
223 dt_params => \%dt_params,
226 is( $search_results->{ iTotalRecords }, 12,
227 "There should be 12 public shelves in total" );
229 is( $search_results->{ iTotalDisplayRecords }, 2,
230 "There should be 1 public shelves for jane" );
232 is( @{ $search_results->{ shelves } }, 2,
233 "There should be 1 public shelf returned" );
235 # Search by owner and shelf name
236 $search_results = C4::Utils::DataTables::VirtualShelves::search({
238 shelfname => "public list 1",
239 dt_params => \%dt_params,
242 is( $search_results->{ iTotalRecords }, 12,
243 "There should be 12 public shelves in total" );
245 is( $search_results->{ iTotalDisplayRecords }, 6,
246 "There should be 6 public shelves for john with name like %public list 1%" );
248 is( @{ $search_results->{ shelves } }, 6,
249 "There should be 6 public chalves returned" );
253 my $biblio = MARC::Record->new();
254 $biblio->append_fields(
255 MARC::Field->new('245', ' ', ' ', a => $title),
257 my ($biblionumber, $biblioitemnumber) = AddBiblio($biblio, '');
258 return $biblionumber;