Line 0
Link Here
|
0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
3 |
use Modern::Perl; |
4 |
|
5 |
use Test::More; |
6 |
use Test::MockModule; |
7 |
use C4::Biblio; |
8 |
use C4::Items; |
9 |
use C4::Members; |
10 |
use C4::Branch; |
11 |
use C4::Category; |
12 |
use C4::Circulation; |
13 |
use MARC::Record; |
14 |
|
15 |
my $branchcode; |
16 |
my $branch_created; |
17 |
my @branches = keys %{ GetBranches() }; |
18 |
if (@branches) { |
19 |
$branchcode = $branches[0]; |
20 |
} else { |
21 |
$branchcode = 'B'; |
22 |
ModBranch({ add => 1, branchcode => $branchcode, branchname => 'Branch' }); |
23 |
$branch_created = 1; |
24 |
} |
25 |
|
26 |
my %item_branch_infos = ( |
27 |
homebranch => $branchcode, |
28 |
holdingbranch => $branchcode, |
29 |
); |
30 |
|
31 |
my ($biblionumber1) = AddBiblio(MARC::Record->new, ''); |
32 |
my $itemnumber1 = AddItem({ barcode => '0101', %item_branch_infos }, $biblionumber1); |
33 |
my $itemnumber2 = AddItem({ barcode => '0102', %item_branch_infos }, $biblionumber1); |
34 |
|
35 |
my ($biblionumber2) = AddBiblio(MARC::Record->new, ''); |
36 |
my $itemnumber3 = AddItem({ barcode => '0203', %item_branch_infos }, $biblionumber2); |
37 |
|
38 |
my $categorycode; |
39 |
my $category_created; |
40 |
my @categories = C4::Category->all; |
41 |
if (@categories) { |
42 |
$categorycode = $categories[0]->{categorycode} |
43 |
} else { |
44 |
$categorycode = 'C'; |
45 |
C4::Context->dbh->do( |
46 |
"INSERT INTO categories(categorycode) VALUES(?)", undef, $categorycode); |
47 |
$category_created = 1; |
48 |
} |
49 |
|
50 |
my $borrowernumber = AddMember(categorycode => $categorycode, branchcode => $branchcode); |
51 |
my $borrower = GetMember(borrowernumber => $borrowernumber); |
52 |
|
53 |
# Need to mock userenv for AddIssue |
54 |
my $module = new Test::MockModule('C4::Context'); |
55 |
$module->mock('userenv', sub { { branch => $branchcode } }); |
56 |
AddIssue($borrower, '0101'); |
57 |
AddIssue($borrower, '0203'); |
58 |
|
59 |
# Begin tests... |
60 |
my $issues; |
61 |
$issues = GetIssues({biblionumber => $biblionumber1}); |
62 |
is(scalar @$issues, 1, "Biblio $biblionumber1 has 1 item issued"); |
63 |
is($issues->[0]->{itemnumber}, $itemnumber1, "First item of biblio $biblionumber1 is issued"); |
64 |
|
65 |
$issues = GetIssues({biblionumber => $biblionumber2}); |
66 |
is(scalar @$issues, 1, "Biblio $biblionumber2 has 1 item issued"); |
67 |
is($issues->[0]->{itemnumber}, $itemnumber3, "First item of biblio $biblionumber2 is issued"); |
68 |
|
69 |
$issues = GetIssues({borrowernumber => $borrowernumber}); |
70 |
is(scalar @$issues, 2, "Borrower $borrowernumber checked out 2 items"); |
71 |
|
72 |
$issues = GetIssues({borrowernumber => $borrowernumber, biblionumber => $biblionumber1}); |
73 |
is(scalar @$issues, 1, "One of those is an item from biblio $biblionumber1"); |
74 |
|
75 |
$issues = GetIssues({borrowernumber => $borrowernumber, biblionumber => $biblionumber2}); |
76 |
is(scalar @$issues, 1, "The other is an item from biblio $biblionumber2"); |
77 |
|
78 |
$issues = GetIssues({itemnumber => $itemnumber2}); |
79 |
is(scalar @$issues, 0, "No one has issued the second item of biblio $biblionumber2"); |
80 |
|
81 |
END { |
82 |
AddReturn('0101', $branchcode); |
83 |
AddReturn('0203', $branchcode); |
84 |
DelMember($borrowernumber); |
85 |
if ($category_created) { |
86 |
C4::Context->dbh->do( |
87 |
"DELETE FROM categories WHERE categorycode = ?", undef, $categorycode); |
88 |
} |
89 |
my $dbh = C4::Context->dbh; |
90 |
C4::Items::DelItem($dbh, $biblionumber1, $itemnumber1); |
91 |
C4::Items::DelItem($dbh, $biblionumber1, $itemnumber2); |
92 |
C4::Items::DelItem($dbh, $biblionumber2, $itemnumber3); |
93 |
C4::Biblio::DelBiblio($biblionumber1); |
94 |
C4::Biblio::DelBiblio($biblionumber2); |
95 |
|
96 |
if ($branch_created) { |
97 |
DelBranch($branchcode); |
98 |
} |
99 |
}; |
100 |
|
101 |
done_testing; |