|
Lines 20-27
use Modern::Perl;
Link Here
|
| 20 |
|
20 |
|
| 21 |
use MARC::Record; |
21 |
use MARC::Record; |
| 22 |
use C4::Biblio; |
22 |
use C4::Biblio; |
|
|
23 |
use C4::Branch; |
| 23 |
|
24 |
|
| 24 |
use Test::More tests => 3; |
25 |
use Test::More tests => 4; |
| 25 |
|
26 |
|
| 26 |
BEGIN { |
27 |
BEGIN { |
| 27 |
use_ok('C4::Items'); |
28 |
use_ok('C4::Items'); |
|
Lines 37-44
subtest 'General Add, Get and Del tests' => sub {
Link Here
|
| 37 |
$dbh->{AutoCommit} = 0; |
38 |
$dbh->{AutoCommit} = 0; |
| 38 |
$dbh->{RaiseError} = 1; |
39 |
$dbh->{RaiseError} = 1; |
| 39 |
|
40 |
|
|
|
41 |
|
| 40 |
# Helper biblio. |
42 |
# Helper biblio. |
| 41 |
diag("Creating biblio instance for testing."); |
43 |
diag("Creating biblio instance for testing."); |
|
|
44 |
C4::Context->set_preference('marcflavour', 'MARC21'); |
| 42 |
my ($bibnum, $bibitemnum) = get_biblio(); |
45 |
my ($bibnum, $bibitemnum) = get_biblio(); |
| 43 |
|
46 |
|
| 44 |
# Add an item. |
47 |
# Add an item. |
|
Lines 75-82
subtest 'GetHiddenItemnumbers tests' => sub {
Link Here
|
| 75 |
$dbh->{RaiseError} = 1; |
78 |
$dbh->{RaiseError} = 1; |
| 76 |
|
79 |
|
| 77 |
# Create a new biblio |
80 |
# Create a new biblio |
|
|
81 |
C4::Context->set_preference('marcflavour', 'MARC21'); |
| 78 |
my ($biblionumber, $biblioitemnumber) = get_biblio(); |
82 |
my ($biblionumber, $biblioitemnumber) = get_biblio(); |
| 79 |
|
83 |
|
|
|
84 |
# Add branches if they don't exist |
| 85 |
if (not defined GetBranchDetail('CPL')) { |
| 86 |
ModBranch({add => 1, branchcode => 'CPL', branchname => 'Centerville'}); |
| 87 |
} |
| 88 |
if (not defined GetBranchDetail('MPL')) { |
| 89 |
ModBranch({add => 1, branchcode => 'MPL', branchname => 'Midway'}); |
| 90 |
} |
| 91 |
|
| 80 |
# Add two items |
92 |
# Add two items |
| 81 |
my ($item1_bibnum, $item1_bibitemnum, $item1_itemnumber) = AddItem( |
93 |
my ($item1_bibnum, $item1_bibitemnum, $item1_itemnumber) = AddItem( |
| 82 |
{ homebranch => 'CPL', |
94 |
{ homebranch => 'CPL', |
|
Lines 142-147
subtest 'GetHiddenItemnumbers tests' => sub {
Link Here
|
| 142 |
$dbh->rollback; |
154 |
$dbh->rollback; |
| 143 |
}; |
155 |
}; |
| 144 |
|
156 |
|
|
|
157 |
subtest 'SearchItems test' => sub { |
| 158 |
plan tests => 10; |
| 159 |
|
| 160 |
# Start transaction |
| 161 |
$dbh->{AutoCommit} = 0; |
| 162 |
$dbh->{RaiseError} = 1; |
| 163 |
|
| 164 |
C4::Context->set_preference('marcflavour', 'MARC21'); |
| 165 |
my ($biblionumber) = get_biblio(); |
| 166 |
|
| 167 |
# Add branches if they don't exist |
| 168 |
if (not defined GetBranchDetail('CPL')) { |
| 169 |
ModBranch({add => 1, branchcode => 'CPL', branchname => 'Centerville'}); |
| 170 |
} |
| 171 |
if (not defined GetBranchDetail('MPL')) { |
| 172 |
ModBranch({add => 1, branchcode => 'MPL', branchname => 'Midway'}); |
| 173 |
} |
| 174 |
|
| 175 |
my (undef, $initial_items_count) = SearchItems(undef, {rows => 1}); |
| 176 |
|
| 177 |
# Add two items |
| 178 |
my (undef, undef, $item1_itemnumber) = AddItem({ |
| 179 |
homebranch => 'CPL', |
| 180 |
holdingbranch => 'CPL', |
| 181 |
}, $biblionumber); |
| 182 |
my (undef, undef, $item2_itemnumber) = AddItem({ |
| 183 |
homebranch => 'MPL', |
| 184 |
holdingbranch => 'MPL', |
| 185 |
}, $biblionumber); |
| 186 |
|
| 187 |
my ($items, $total_results); |
| 188 |
|
| 189 |
($items, $total_results) = SearchItems(); |
| 190 |
is($total_results, $initial_items_count + 2, "Created 2 new items"); |
| 191 |
is(scalar @$items, $total_results, "SearchItems() returns all items"); |
| 192 |
|
| 193 |
($items, $total_results) = SearchItems(undef, {rows => 1}); |
| 194 |
is($total_results, $initial_items_count + 2); |
| 195 |
is(scalar @$items, 1, "SearchItems(undef, {rows => 1}) returns only 1 item"); |
| 196 |
|
| 197 |
# Search all items where homebranch = 'CPL' |
| 198 |
my $filter = { |
| 199 |
field => 'homebranch', |
| 200 |
query => 'CPL', |
| 201 |
operator => '=', |
| 202 |
}; |
| 203 |
($items, $total_results) = SearchItems($filter); |
| 204 |
ok($total_results > 0, "There is at least one CPL item"); |
| 205 |
my $all_items_are_CPL = 1; |
| 206 |
foreach my $item (@$items) { |
| 207 |
if ($item->{homebranch} ne 'CPL') { |
| 208 |
$all_items_are_CPL = 0; |
| 209 |
last; |
| 210 |
} |
| 211 |
} |
| 212 |
ok($all_items_are_CPL, "All items returned by SearchItems are from CPL"); |
| 213 |
|
| 214 |
# Search all items where homebranch != 'CPL' |
| 215 |
$filter = { |
| 216 |
field => 'homebranch', |
| 217 |
query => 'CPL', |
| 218 |
operator => '!=', |
| 219 |
}; |
| 220 |
($items, $total_results) = SearchItems($filter); |
| 221 |
ok($total_results > 0, "There is at least one non-CPL item"); |
| 222 |
my $all_items_are_not_CPL = 1; |
| 223 |
foreach my $item (@$items) { |
| 224 |
if ($item->{homebranch} eq 'CPL') { |
| 225 |
$all_items_are_not_CPL = 0; |
| 226 |
last; |
| 227 |
} |
| 228 |
} |
| 229 |
ok($all_items_are_not_CPL, "All items returned by SearchItems are not from CPL"); |
| 230 |
|
| 231 |
# Search all items where biblio title (245$a) is like 'Silence in the %' |
| 232 |
$filter = { |
| 233 |
field => 'marc:245$a', |
| 234 |
query => 'Silence in the %', |
| 235 |
operator => 'like', |
| 236 |
}; |
| 237 |
($items, $total_results) = SearchItems($filter); |
| 238 |
ok($total_results >= 2, "There is at least 2 items with a biblio title like 'Silence in the %'"); |
| 239 |
|
| 240 |
# Search all items where biblio title is 'Silence in the library' |
| 241 |
# and homebranch is 'CPL' |
| 242 |
$filter = { |
| 243 |
conjunction => 'AND', |
| 244 |
filters => [ |
| 245 |
{ |
| 246 |
field => 'marc:245$a', |
| 247 |
query => 'Silence in the %', |
| 248 |
operator => 'like', |
| 249 |
}, |
| 250 |
{ |
| 251 |
field => 'homebranch', |
| 252 |
query => 'CPL', |
| 253 |
operator => '=', |
| 254 |
}, |
| 255 |
], |
| 256 |
}; |
| 257 |
($items, $total_results) = SearchItems($filter); |
| 258 |
my $found = 0; |
| 259 |
foreach my $item (@$items) { |
| 260 |
if ($item->{itemnumber} == $item1_itemnumber) { |
| 261 |
$found = 1; |
| 262 |
last; |
| 263 |
} |
| 264 |
} |
| 265 |
ok($found, "item1 found"); |
| 266 |
|
| 267 |
$dbh->rollback; |
| 268 |
}; |
| 269 |
|
| 145 |
# Helper method to set up a Biblio. |
270 |
# Helper method to set up a Biblio. |
| 146 |
sub get_biblio { |
271 |
sub get_biblio { |
| 147 |
my $bib = MARC::Record->new(); |
272 |
my $bib = MARC::Record->new(); |