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