Bug 12470: adding unit tests for the routines CheckValidBarCode and CheckIfIssuedToPa...
[koha.git] / t / db_dependent / Circulation / CheckIfIssuedToPatron.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4
5 use Test::More tests => 22;
6 use Test::MockModule;
7
8 use C4::Biblio;
9 use C4::Items;
10 use C4::Members;
11 use C4::Branch;
12 use C4::Category;
13 use MARC::Record;
14
15 BEGIN {
16     use_ok('C4::Circulation');
17 }
18
19
20 my $dbh = C4::Context->dbh;
21 $dbh->{AutoCommit} = 0;
22 $dbh->{RaiseError} = 1;
23
24 $dbh->do(q|DELETE FROM issues|);
25 $dbh->do(q|DELETE FROM items|);
26 $dbh->do(q|DELETE FROM borrowers|);
27 $dbh->do(q|DELETE FROM branches|);
28 $dbh->do(q|DELETE FROM biblio|);
29 $dbh->do(q|DELETE FROM items|);
30 $dbh->do(q|DELETE FROM categories|);
31
32
33 my $branchcode = 'B';
34 ModBranch({ add => 1, branchcode => $branchcode, branchname => 'Branch' });
35
36 my $categorycode = 'C';
37 $dbh->do("INSERT INTO categories(categorycode) VALUES(?)", undef, $categorycode);
38
39 my %item_branch_infos = (
40     homebranch => $branchcode,
41     holdingbranch => $branchcode,
42 );
43
44 my ($biblionumber1) = AddBiblio(MARC::Record->new, '');
45 my $barcode1 = '0101';
46 AddItem({ barcode => $barcode1, %item_branch_infos }, $biblionumber1);
47 my ($biblionumber2) = AddBiblio(MARC::Record->new, '');
48 my $barcode2 = '0202';
49 AddItem({ barcode => $barcode2, %item_branch_infos }, $biblionumber2);
50
51 my $borrowernumber1 = AddMember(categorycode => $categorycode, branchcode => $branchcode);
52 my $borrowernumber2 = AddMember(categorycode => $categorycode, branchcode => $branchcode);
53 my $borrower1 = GetMember(borrowernumber => $borrowernumber1);
54 my $borrower2 = GetMember(borrowernumber => $borrowernumber2);
55
56 my $module = new Test::MockModule('C4::Context');
57 $module->mock('userenv', sub { { branch => $branchcode } });
58
59
60 my $check_if_issued = C4::Circulation::CheckIfIssuedToPatron();
61 is( $check_if_issued, undef, 'CheckIfIssuedToPatron without argument returns undef' );
62 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron(undef, $biblionumber1);
63 is( $check_if_issued, undef, 'CheckIfIssuedToPatron without the borrower number returns undef' );
64 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber1, undef);
65 is( $check_if_issued, undef, 'CheckIfIssuedToPatron without the biblio number returns undef' );
66 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber1, $biblionumber1);
67 is( $check_if_issued, undef, 'CheckIfIssuedToPatron returns unef' );
68 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber1, $biblionumber2);
69 is( $check_if_issued, undef, 'CheckIfIssuedToPatron returns undef' );
70 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber2, $biblionumber1);
71 is( $check_if_issued, undef, 'CheckIfIssuedToPatron returns undef' );
72 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber2, $biblionumber2);
73 is( $check_if_issued, undef, 'CheckIfIssuedToPatron returns undef' );
74
75 AddIssue($borrower1, '0101');
76 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron();
77 is( $check_if_issued, undef, 'CheckIfIssuedToPatron without argument returns undef' );
78 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron(undef, $biblionumber1);
79 is( $check_if_issued, undef, 'CheckIfIssuedToPatron without the borrower number returns undef' );
80 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber1, undef);
81 is( $check_if_issued, undef, 'CheckIfIssuedToPatron without the biblio number returns undef' );
82 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber1, $biblionumber1);
83 is( $check_if_issued, 1, 'CheckIfIssuedToPatron returns true' );
84 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber1, $biblionumber2);
85 is( $check_if_issued, undef, 'CheckIfIssuedToPatron returns undef' );
86 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber2, $biblionumber1);
87 is( $check_if_issued, undef, 'CheckIfIssuedToPatron returns undef' );
88 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber2, $biblionumber2);
89 is( $check_if_issued, undef, 'CheckIfIssuedToPatron returns undef' );
90
91 AddIssue($borrower2, '0202');
92 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron();
93 is( $check_if_issued, undef, 'CheckIfIssuedToPatron without argument returns undef' );
94 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron(undef, $biblionumber1);
95 is( $check_if_issued, undef, 'CheckIfIssuedToPatron without the borrower number returns undef' );
96 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber1, undef);
97 is( $check_if_issued, undef, 'CheckIfIssuedToPatron without the biblio number returns undef' );
98 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber1, $biblionumber1);
99 is( $check_if_issued, 1, 'CheckIfIssuedToPatron returns true' );
100 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber1, $biblionumber2);
101 is( $check_if_issued, undef, 'CheckIfIssuedToPatron returns undef' );
102 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber2, $biblionumber1);
103 is( $check_if_issued, undef, 'CheckIfIssuedToPatron returns undef' );
104 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber2, $biblionumber2);
105 is( $check_if_issued, 1, 'CheckIfIssuedToPatron returns true' );
106
107 $dbh->rollback();