Bug 21817: Centralize the mock of userenv from tests
[koha.git] / t / db_dependent / Circulation / IssuingRules / maxsuspensiondays.t
1 use Modern::Perl;
2 use Test::More tests => 2;
3
4 use MARC::Record;
5 use MARC::Field;
6 use C4::Context;
7
8 use C4::Circulation qw( AddIssue AddReturn );
9 use C4::Items qw( AddItem );
10 use C4::Biblio qw( AddBiblio );
11 use Koha::Database;
12 use Koha::DateUtils;
13 use Koha::Patron::Debarments qw( GetDebarments DelDebarment );
14 use Koha::Patrons;
15
16 use t::lib::TestBuilder;
17 use t::lib::Mocks;
18
19 my $schema = Koha::Database->schema;
20 $schema->storage->txn_begin;
21 my $builder = t::lib::TestBuilder->new;
22 my $dbh = C4::Context->dbh;
23 $dbh->{RaiseError} = 1;
24
25 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
26 my $itemtype   = $builder->build({ source => 'Itemtype' })->{itemtype};
27 my $patron_category = $builder->build({ source => 'Category' });
28
29 t::lib::Mocks::mock_userenv({ branchcode => $branchcode });
30
31 # Test without maxsuspensiondays set
32 Koha::IssuingRules->search->delete;
33 $builder->build(
34     {
35         source => 'Issuingrule',
36         value  => {
37             categorycode => '*',
38             itemtype     => '*',
39             branchcode   => '*',
40             firstremind  => 0,
41             finedays     => 2,
42             lengthunit   => 'days',
43             suspension_chargeperiod => 1,
44         }
45     }
46 );
47
48 my $borrowernumber = Koha::Patron->new({
49     firstname =>  'my firstname',
50     surname => 'my surname',
51     categorycode => $patron_category->{categorycode},
52     branchcode => $branchcode,
53 })->store->borrowernumber;
54 my $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
55
56 my $record = MARC::Record->new();
57 $record->append_fields(
58     MARC::Field->new('100', ' ', ' ', a => 'My author'),
59     MARC::Field->new('245', ' ', ' ', a => 'My title'),
60 );
61
62 my $barcode = 'bc_maxsuspensiondays';
63 my ($biblionumber, $biblioitemnumber) = AddBiblio($record, '');
64 my (undef, undef, $itemnumber) = AddItem({
65         homebranch => $branchcode,
66         holdingbranch => $branchcode,
67         barcode => $barcode,
68         itype => $itemtype
69     } , $biblionumber);
70
71 # clear any holidays to avoid throwing off the suspension day
72 # calculations
73 $dbh->do('DELETE FROM special_holidays');
74 $dbh->do('DELETE FROM repeatable_holidays');
75
76 my $daysago20 = dt_from_string->add_duration(DateTime::Duration->new(days => -20));
77 my $daysafter40 = dt_from_string->add_duration(DateTime::Duration->new(days => 40));
78
79 AddIssue( $borrower, $barcode, $daysago20 );
80 AddReturn( $barcode, $branchcode );
81 my $debarments = GetDebarments({borrowernumber => $borrower->{borrowernumber}});
82 is(
83     $debarments->[0]->{expiration},
84     output_pref({ dt => $daysafter40, dateformat => 'iso', dateonly => 1 }),
85     'calculate suspension with no maximum set'
86 );
87 DelDebarment( $debarments->[0]->{borrower_debarment_id} );
88
89 # Test with maxsuspensiondays = 10 days
90 my $issuing_rule = Koha::IssuingRules->search->next;
91 $issuing_rule->maxsuspensiondays( 10 )->store;
92
93 my $daysafter10 = dt_from_string->add_duration(DateTime::Duration->new(days => 10));
94 AddIssue( $borrower, $barcode, $daysago20 );
95 AddReturn( $barcode, $branchcode );
96 $debarments = GetDebarments({borrowernumber => $borrower->{borrowernumber}});
97 is(
98     $debarments->[0]->{expiration},
99     output_pref({ dt => $daysafter10, dateformat => 'iso', dateonly => 1 }),
100     'calculate suspension with a maximum set'
101 );
102 DelDebarment( $debarments->[0]->{borrower_debarment_id} );
103
104 $schema->storage->txn_rollback;