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