Line 0
Link Here
|
0 |
- |
1 |
use Modern::Perl; |
|
|
2 |
|
3 |
use MARC::Record; |
4 |
use C4::Biblio; |
5 |
use C4::Circulation; |
6 |
use C4::Members; |
7 |
use t::lib::Mocks; |
8 |
|
9 |
|
10 |
use Test::More tests => 10; |
11 |
|
12 |
*C4::Context::userenv = \&Mock_userenv; |
13 |
|
14 |
BEGIN { |
15 |
use_ok('C4::Items'); |
16 |
} |
17 |
|
18 |
my $dbh = C4::Context->dbh; |
19 |
$dbh->{AutoCommit} = 0; |
20 |
$dbh->{RaiseError} = 1; |
21 |
|
22 |
my ( $biblionumber, $bibitemnum ) = get_biblio(); |
23 |
|
24 |
# book_on_loan |
25 |
|
26 |
my ( $borrowernumber, $borrower ) = get_borrower(); |
27 |
my ( $itemnumber, $item ) = get_item( $biblionumber ); |
28 |
AddIssue( $borrower, 'i_dont_exist' ); |
29 |
|
30 |
is( |
31 |
ItemSafeToDelete($dbh, $biblionumber, $itemnumber), |
32 |
'book_on_loan', |
33 |
'ItemSafeToDelete reports item on loan', |
34 |
); |
35 |
|
36 |
is( |
37 |
DelItemCheck($dbh, $biblionumber, $itemnumber), |
38 |
'book_on_loan', |
39 |
'item that is on loan cannot be deleted', |
40 |
); |
41 |
|
42 |
AddReturn('i_dont_exist', 'CPL'); |
43 |
|
44 |
# book_reserved is tested in t/db_dependent/Reserves.t |
45 |
|
46 |
# not_same_branch |
47 |
t::lib::Mocks::mock_preference('IndependentBranches', 1); |
48 |
ModItem( { homebranch => 'FPL', holdingbranch => 'FPL' }, $biblionumber, $itemnumber ); |
49 |
|
50 |
is( |
51 |
ItemSafeToDelete($dbh, $biblionumber, $itemnumber), |
52 |
'not_same_branch', |
53 |
'ItemSafeToDelete reports IndependentBranches restriction', |
54 |
); |
55 |
|
56 |
is( |
57 |
DelItemCheck($dbh, $biblionumber, $itemnumber), |
58 |
'not_same_branch', |
59 |
'IndependentBranches prevents deletion at another branch', |
60 |
); |
61 |
|
62 |
ModItem( { homebranch => 'CPL', holdingbranch => 'CPL' }, $biblionumber, $itemnumber ); |
63 |
|
64 |
# linked_analytics |
65 |
|
66 |
{ # codeblock to limit scope of $module->mock |
67 |
|
68 |
my $module = Test::MockModule->new('C4::Items'); |
69 |
$module->mock( GetAnalyticsCount => sub { return 1 } ); |
70 |
|
71 |
is( |
72 |
ItemSafeToDelete($dbh, $biblionumber, $itemnumber), |
73 |
'linked_analytics', |
74 |
'ItemSafeToDelete reports linked analytics', |
75 |
); |
76 |
|
77 |
is( |
78 |
DelItemCheck($dbh, $biblionumber, $itemnumber), |
79 |
'linked_analytics', |
80 |
'Linked analytics prevents deletion of item', |
81 |
); |
82 |
|
83 |
} |
84 |
|
85 |
is( |
86 |
ItemSafeToDelete($dbh, $biblionumber, $itemnumber), |
87 |
1, |
88 |
'ItemSafeToDelete shows item safe to delete' |
89 |
); |
90 |
|
91 |
DelItemCheck($dbh, $biblionumber, $itemnumber, { do_not_commit => 1 } ); |
92 |
|
93 |
my $testitem = GetItem( $itemnumber ); |
94 |
|
95 |
is( $testitem->{itemnumber} , $itemnumber, |
96 |
"DelItemCheck should not delete item if 'do_not_commit' is set" |
97 |
); |
98 |
|
99 |
DelItemCheck( $dbh, $biblionumber, $itemnumber ); |
100 |
|
101 |
$testitem = GetItem( $itemnumber ); |
102 |
|
103 |
is( $testitem->{itemnumber}, undef, |
104 |
"DelItemCheck should delete item if 'do_not_commit' not set" |
105 |
); |
106 |
|
107 |
# End of testing |
108 |
|
109 |
# Helper methods to set up Biblio, Item, and Borrower. |
110 |
sub get_biblio { |
111 |
my $bib = MARC::Record->new(); |
112 |
$bib->append_fields( |
113 |
MARC::Field->new( '100', ' ', ' ', a => 'Moffat, Steven' ), |
114 |
MARC::Field->new( '245', ' ', ' ', a => 'Silence in the library' ), |
115 |
); |
116 |
my ( $bibnum, $bibitemnum ) = AddBiblio( $bib, '' ); |
117 |
return ( $bibnum, $bibitemnum ); |
118 |
} |
119 |
|
120 |
sub get_item { |
121 |
my $biblionumber = shift; |
122 |
my ( $item_bibnum, $item_bibitemnum, $itemnumber ) = |
123 |
AddItem( { homebranch => 'CPL', holdingbranch => 'CPL', barcode => 'i_dont_exist' }, $biblionumber ); |
124 |
my $item = GetItem( $itemnumber ); |
125 |
return ( $itemnumber, $item ); |
126 |
} |
127 |
|
128 |
sub get_borrower { |
129 |
my $borrowernumber = AddMember( |
130 |
firstname => 'my firstname', |
131 |
surname => 'my surname', |
132 |
categorycode => 'S', |
133 |
branchcode => 'CPL', |
134 |
); |
135 |
|
136 |
my $borrower = GetMember( borrowernumber => $borrowernumber ); |
137 |
return ( $borrowernumber, $borrower ); |
138 |
} |
139 |
|
140 |
# C4::Context->userenv |
141 |
sub Mock_userenv { |
142 |
return { flags => 0, branch => 'CPL' }; |
143 |
} |
144 |
|
145 |
$dbh->rollback; |