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>.
24 use t::lib::TestBuilder;
27 use Test::More tests => 9;
34 my $builder = t::lib::TestBuilder->new();
35 my $schema = Koha::Database->new->schema;
37 $schema->storage->txn_begin;
39 my $branch = $builder->build(
45 my $module = new Test::MockModule('C4::Context');
46 $module->mock('userenv', sub {
48 branch => $branch->{branchcode}
52 my $branch2 = $builder->build(
58 my $category = $builder->build(
64 my $patron = $builder->build(
68 categorycode => $category->{categorycode},
69 branchcode => $branch->{branchcode},
74 my $biblio = $builder->build(
78 branchcode => $branch->{branchcode},
83 my $item = $builder->build(
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,
98 AddIssue( $patron, $item->{barcode} );
101 ItemSafeToDelete( $biblio->{biblionumber}, $item->{itemnumber} ),
103 'ItemSafeToDelete reports item on loan',
107 DelItemCheck( $biblio->{biblionumber}, $item->{itemnumber} ),
109 'item that is on loan cannot be deleted',
112 AddReturn( $item->{barcode}, $branch->{branchcode} );
114 # book_reserved is tested in t/db_dependent/Reserves.t
117 t::lib::Mocks::mock_preference('IndependentBranches', 1);
118 ModItem( { homebranch => $branch2->{branchcode}, holdingbranch => $branch2->{branchcode} }, $biblio->{biblionumber}, $item->{itemnumber} );
121 ItemSafeToDelete( $biblio->{biblionumber}, $item->{itemnumber} ),
123 'ItemSafeToDelete reports IndependentBranches restriction',
127 DelItemCheck( $biblio->{biblionumber}, $item->{itemnumber} ),
129 'IndependentBranches prevents deletion at another branch',
132 ModItem( { homebranch => $branch->{branchcode}, holdingbranch => $branch->{branchcode} }, $biblio->{biblionumber}, $item->{itemnumber} );
136 { # codeblock to limit scope of $module->mock
138 my $module = Test::MockModule->new('C4::Items');
139 $module->mock( GetAnalyticsCount => sub { return 1 } );
142 ItemSafeToDelete( $biblio->{biblionumber}, $item->{itemnumber} ),
144 'ItemSafeToDelete reports linked analytics',
148 DelItemCheck( $biblio->{biblionumber}, $item->{itemnumber} ),
150 'Linked analytics prevents deletion of item',
156 ItemSafeToDelete( $biblio->{biblionumber}, $item->{itemnumber} ),
158 'ItemSafeToDelete shows item safe to delete'
161 DelItemCheck( $biblio->{biblionumber}, $item->{itemnumber} );
163 my $test_item = Koha::Items->find( $item->{itemnumber} );
165 is( $test_item, undef,
166 "DelItemCheck should delete item if ItemSafeToDelete returns true"
169 $schema->storage->txn_rollback;