Bug 18292: Remove return 1 statements in tests
[koha.git] / t / db_dependent / Circulation / CheckIfIssuedToPatron.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 Test::More tests => 22;
21 use Test::MockModule;
22 use t::lib::TestBuilder;
23
24 use C4::Biblio;
25 use C4::Items;
26 use C4::Members;
27 use Koha::Library;
28 use Koha::Patrons;
29 use MARC::Record;
30
31 BEGIN {
32     use_ok('C4::Circulation');
33 }
34
35 my $schema = Koha::Database->schema;
36 $schema->storage->txn_begin;
37
38 my $builder = t::lib::TestBuilder->new;
39
40 my $dbh = C4::Context->dbh;
41 $dbh->{AutoCommit} = 0;
42 $dbh->{RaiseError} = 1;
43
44 $dbh->do(q|DELETE FROM issues|);
45 $dbh->do(q|DELETE FROM items|);
46 $dbh->do(q|DELETE FROM borrowers|);
47 $dbh->do(q|DELETE FROM branches|);
48 $dbh->do(q|DELETE FROM biblio|);
49 $dbh->do(q|DELETE FROM items|);
50 $dbh->do(q|DELETE FROM categories|);
51
52
53 ## Create sample data
54 # Add a branch
55 my $branchcode = $builder->build( { source => 'Branch' } )->{branchcode};
56
57 # Add a category
58 my $categorycode = $builder->build( { source => 'Category' } )->{categorycode};
59
60 # Add an itemtype
61 my $itemtype = $builder->build( { source => 'Itemtype' } )->{itemtype};
62
63 my %item_info = (
64     homebranch    => $branchcode,
65     holdingbranch => $branchcode,
66     itype         => $itemtype
67 );
68
69 my ($biblionumber1) = AddBiblio(MARC::Record->new, '');
70 my $barcode1 = '0101';
71 AddItem({ barcode => $barcode1, %item_info }, $biblionumber1);
72 my ($biblionumber2) = AddBiblio(MARC::Record->new, '');
73 my $barcode2 = '0202';
74 AddItem({ barcode => $barcode2, %item_info }, $biblionumber2);
75
76 my $borrowernumber1 = AddMember(categorycode => $categorycode, branchcode => $branchcode);
77 my $borrowernumber2 = AddMember(categorycode => $categorycode, branchcode => $branchcode);
78 my $borrower1 = Koha::Patrons->find( $borrowernumber1 )->unblessed;
79 my $borrower2 = Koha::Patrons->find( $borrowernumber2 )->unblessed;
80
81 my $module = new Test::MockModule('C4::Context');
82 $module->mock('userenv', sub { { branch => $branchcode } });
83
84
85 my $check_if_issued = C4::Circulation::CheckIfIssuedToPatron();
86 is( $check_if_issued, undef, 'CheckIfIssuedToPatron without argument returns undef' );
87 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron(undef, $biblionumber1);
88 is( $check_if_issued, undef, 'CheckIfIssuedToPatron without the borrower number returns undef' );
89 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber1, undef);
90 is( $check_if_issued, undef, 'CheckIfIssuedToPatron without the biblio number returns undef' );
91 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber1, $biblionumber1);
92 is( $check_if_issued, undef, 'CheckIfIssuedToPatron returns unef' );
93 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber1, $biblionumber2);
94 is( $check_if_issued, undef, 'CheckIfIssuedToPatron returns undef' );
95 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber2, $biblionumber1);
96 is( $check_if_issued, undef, 'CheckIfIssuedToPatron returns undef' );
97 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber2, $biblionumber2);
98 is( $check_if_issued, undef, 'CheckIfIssuedToPatron returns undef' );
99
100 AddIssue($borrower1, '0101');
101 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron();
102 is( $check_if_issued, undef, 'CheckIfIssuedToPatron without argument returns undef' );
103 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron(undef, $biblionumber1);
104 is( $check_if_issued, undef, 'CheckIfIssuedToPatron without the borrower number returns undef' );
105 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber1, undef);
106 is( $check_if_issued, undef, 'CheckIfIssuedToPatron without the biblio number returns undef' );
107 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber1, $biblionumber1);
108 is( $check_if_issued, 1, 'CheckIfIssuedToPatron returns true' );
109 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber1, $biblionumber2);
110 is( $check_if_issued, undef, 'CheckIfIssuedToPatron returns undef' );
111 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber2, $biblionumber1);
112 is( $check_if_issued, undef, 'CheckIfIssuedToPatron returns undef' );
113 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber2, $biblionumber2);
114 is( $check_if_issued, undef, 'CheckIfIssuedToPatron returns undef' );
115
116 AddIssue($borrower2, '0202');
117 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron();
118 is( $check_if_issued, undef, 'CheckIfIssuedToPatron without argument returns undef' );
119 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron(undef, $biblionumber1);
120 is( $check_if_issued, undef, 'CheckIfIssuedToPatron without the borrower number returns undef' );
121 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber1, undef);
122 is( $check_if_issued, undef, 'CheckIfIssuedToPatron without the biblio number returns undef' );
123 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber1, $biblionumber1);
124 is( $check_if_issued, 1, 'CheckIfIssuedToPatron returns true' );
125 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber1, $biblionumber2);
126 is( $check_if_issued, undef, 'CheckIfIssuedToPatron returns undef' );
127 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber2, $biblionumber1);
128 is( $check_if_issued, undef, 'CheckIfIssuedToPatron returns undef' );
129 $check_if_issued = C4::Circulation::CheckIfIssuedToPatron($borrowernumber2, $biblionumber2);
130 is( $check_if_issued, 1, 'CheckIfIssuedToPatron returns true' );
131
132 $dbh->rollback();
133