Bug 17600: Standardize our EXPORT_OK
[koha.git] / t / db_dependent / Serials / ReNewSubscription.t
1 #!/usr/bin/perl
2
3 # This script includes tests for ReNewSubscription
4
5 # Copyright 2015 BibLibre, Paul Poulain
6 # Copyright 2018 Catalyst IT, Alex Buckley
7 #
8 # This file is part of Koha.
9 #
10 # Koha is free software; you can redistribute it and/or modify it
11 # under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 3 of the License, or
13 # (at your option) any later version.
14 #
15 # Koha is distributed in the hope that it will be useful, but
16 # WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22
23 use Modern::Perl;
24
25 use Test::More tests => 7;
26 use Test::MockModule;
27 use t::lib::TestBuilder;
28 use t::lib::Mocks;
29
30 use C4::Serials qw( NewSubscription ReNewSubscription GetSubscription GetSubscriptionLength );
31
32 use Koha::Database;
33 use Koha::Subscription::Histories;
34
35 my $schema = Koha::Database->new->schema;
36 $schema->storage->txn_begin;
37
38 my $builder = t::lib::TestBuilder->new();
39
40 # create fake numberpattern & fake periodicity
41 my $frequency = $builder->build({
42     source => 'SubscriptionFrequency',
43     value => {
44         description   => "daily",
45         unit          => "day",
46         unitsperissue => 1,
47     },
48 });
49
50 my $pattern = $builder->build({
51     source => 'SubscriptionNumberpattern',
52     value => {
53         label           => 'mock',
54         description     =>'mock',
55         numberingmethod => 'Issue {X}',
56         add1            => 1,
57         every1          => 1,
58         setto1          => 100,
59     }
60 });
61
62 my $biblio = $builder->build_sample_biblio();
63
64 # Create fake subscription, daily subscription, duration 12 months, issues startint at #100
65 my $subscription = $builder->build({
66     source => 'Subscription',
67     value  => {
68         biblionumber    => $biblio->biblionumber,
69         startdate       => '2015-01-01',
70         enddate         => '2015-12-31',
71         aqbooksellerid  => 1,
72         periodicity     => $frequency->{id},
73         numberpattern   => $pattern->{id},
74         monthlength     => 12,
75     },
76 });
77
78 my $subscriptionhistory = $builder->build({
79     source => 'Subscriptionhistory',
80     value  => {
81         biblionumber   => $biblio->biblionumber,
82         subscriptionid => $subscription->{subscriptionid},
83         histenddate    => undef,
84         opacnote       => 'Testing',
85     }
86 });
87
88 # Actual testing starts here!
89 # Renew the subscription and check that enddate has not been set
90 ReNewSubscription(
91     {
92         subscriptionid => $subscription->{subscriptionid},
93         startdate      => "2016-01-01",
94         monthlength    => 12
95     }
96 );
97
98 # Calculate the subscription length for the renewal for issues, days and months
99 my ($numberlength, $weeklength, $monthlength) = GetSubscriptionLength('issues', 7);
100 is ( $numberlength, 7, "Subscription length is 7 issues");
101
102 ($numberlength, $weeklength, $monthlength) = GetSubscriptionLength('weeks', 7);
103 is ( $weeklength, 7, "Subscription length is 7 weeks");
104
105 ($numberlength, $weeklength, $monthlength) = GetSubscriptionLength('months', 7);
106 is ( $monthlength, 7, "Subscription length is 7 months");
107
108 # Check subscription length when no value is inputted into the numeric sublength field
109 ($numberlength, $weeklength, $monthlength) = GetSubscriptionLength('months', '');
110 is ($monthlength, undef, "Subscription length is undef months, invalid month data was not stored");
111
112 # Check subscription length when a letter is inputted into the numeric sublength field
113 ($numberlength, $weeklength, $monthlength) = GetSubscriptionLength('issues', 'w');
114 is ($monthlength, undef, "Subscription length is undef issues, invalid issue data was not stored");
115
116 # Check subscription length when a special character is inputted into numberic sublength field
117 ($numberlength, $weeklength, $monthlength) = GetSubscriptionLength('weeks', '!');
118 is ($weeklength, undef, "Subscription length is undef weeks, invalid weeks data was not stored");
119
120 # Renew the subscription and check that enddate has not been set
121
122 my $history = Koha::Subscription::Histories->find($subscription->{subscriptionid});
123
124 is ( $history->histenddate(), undef, 'subscription history not empty after renewal');
125
126 # End of tests
127
128 $schema->storage->txn_rollback;
129