|
Lines 1-171
Link Here
|
| 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 C4::Circulation; |
| 21 |
use Koha::Database; |
| 22 |
use Koha::Items; |
| 23 |
|
| 24 |
use t::lib::TestBuilder; |
| 25 |
use t::lib::Mocks; |
| 26 |
|
| 27 |
use Test::More tests => 9; |
| 28 |
use Test::MockModule; |
| 29 |
|
| 30 |
BEGIN { |
| 31 |
use_ok('C4::Items'); |
| 32 |
} |
| 33 |
|
| 34 |
my $builder = t::lib::TestBuilder->new(); |
| 35 |
my $schema = Koha::Database->new->schema; |
| 36 |
# Begin transaction |
| 37 |
$schema->storage->txn_begin; |
| 38 |
|
| 39 |
my $branch = $builder->build( |
| 40 |
{ |
| 41 |
source => 'Branch', |
| 42 |
} |
| 43 |
); |
| 44 |
|
| 45 |
my $module = new Test::MockModule('C4::Context'); |
| 46 |
$module->mock('userenv', sub { |
| 47 |
{ flags => 0, |
| 48 |
branch => $branch->{branchcode} |
| 49 |
} |
| 50 |
}); |
| 51 |
|
| 52 |
my $branch2 = $builder->build( |
| 53 |
{ |
| 54 |
source => 'Branch', |
| 55 |
} |
| 56 |
); |
| 57 |
|
| 58 |
my $category = $builder->build( |
| 59 |
{ |
| 60 |
source => 'Category', |
| 61 |
} |
| 62 |
); |
| 63 |
|
| 64 |
my $patron = $builder->build( |
| 65 |
{ |
| 66 |
source => 'Borrower', |
| 67 |
value => { |
| 68 |
categorycode => $category->{categorycode}, |
| 69 |
branchcode => $branch->{branchcode}, |
| 70 |
}, |
| 71 |
} |
| 72 |
); |
| 73 |
|
| 74 |
my $biblio = $builder->build( |
| 75 |
{ |
| 76 |
source => 'Biblio', |
| 77 |
value => { |
| 78 |
branchcode => $branch->{branchcode}, |
| 79 |
}, |
| 80 |
} |
| 81 |
); |
| 82 |
|
| 83 |
my $item = $builder->build_object( |
| 84 |
{ |
| 85 |
class => 'Koha::Items', |
| 86 |
value => { |
| 87 |
biblionumber => $biblio->{biblionumber}, |
| 88 |
homebranch => $branch->{branchcode}, |
| 89 |
holdingbranch => $branch->{branchcode}, |
| 90 |
withdrawn => 0, # randomly assigned value may block return. |
| 91 |
withdrawn_on => undef, |
| 92 |
}, |
| 93 |
} |
| 94 |
); |
| 95 |
|
| 96 |
# book_on_loan |
| 97 |
|
| 98 |
AddIssue( $patron, $item->barcode ); |
| 99 |
|
| 100 |
is( |
| 101 |
$item->safe_to_delete, |
| 102 |
'book_on_loan', |
| 103 |
'Koha::Item->safe_to_delete reports item on loan', |
| 104 |
); |
| 105 |
|
| 106 |
is( |
| 107 |
$item->safe_delete, |
| 108 |
'book_on_loan', |
| 109 |
'item that is on loan cannot be deleted', |
| 110 |
); |
| 111 |
|
| 112 |
AddReturn( $item->barcode, $branch->{branchcode} ); |
| 113 |
|
| 114 |
# book_reserved is tested in t/db_dependent/Reserves.t |
| 115 |
|
| 116 |
# not_same_branch |
| 117 |
t::lib::Mocks::mock_preference('IndependentBranches', 1); |
| 118 |
$item->set( { homebranch => $branch2->{branchcode}, holdingbranch => $branch2->{branchcode} })->store; |
| 119 |
|
| 120 |
$item->discard_changes; |
| 121 |
is( |
| 122 |
$item->safe_to_delete, |
| 123 |
'not_same_branch', |
| 124 |
'Koha::Item->safe_to_delete reports IndependentBranches restriction', |
| 125 |
); |
| 126 |
|
| 127 |
is( |
| 128 |
$item->safe_delete, |
| 129 |
'not_same_branch', |
| 130 |
'IndependentBranches prevents deletion at another branch', |
| 131 |
); |
| 132 |
|
| 133 |
$item->set( { homebranch => $branch->{branchcode}, holdingbranch => $branch->{branchcode} })->store; |
| 134 |
|
| 135 |
# linked_analytics |
| 136 |
|
| 137 |
{ # codeblock to limit scope of $module->mock |
| 138 |
|
| 139 |
my $module = Test::MockModule->new('C4::Items'); |
| 140 |
$module->mock( GetAnalyticsCount => sub { return 1 } ); |
| 141 |
|
| 142 |
is( |
| 143 |
$item->safe_to_delete, |
| 144 |
'linked_analytics', |
| 145 |
'Koha::Item->safe_to_delete reports linked analytics', |
| 146 |
); |
| 147 |
|
| 148 |
is( |
| 149 |
$item->safe_delete, |
| 150 |
'linked_analytics', |
| 151 |
'Linked analytics prevents deletion of item', |
| 152 |
); |
| 153 |
|
| 154 |
} |
| 155 |
|
| 156 |
is( |
| 157 |
$item->safe_to_delete, |
| 158 |
1, |
| 159 |
'Koha::Item->safe_to_delete shows item safe to delete' |
| 160 |
); |
| 161 |
|
| 162 |
$item->safe_delete, |
| 163 |
|
| 164 |
my $test_item = Koha::Items->find( $item->itemnumber ); |
| 165 |
|
| 166 |
is( $test_item, undef, |
| 167 |
"Koha::Item->safe_delete should delete item if safe_to_delete returns true" |
| 168 |
); |
| 169 |
|
| 170 |
$schema->storage->txn_rollback; |
| 171 |
|