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