Bug 20045: Fix Selenium tests
[koha.git] / t / db_dependent / Serials / GetFictiveIssueNumber.t
1 #!/usr/bin/perl
2
3 # This test deals with GetFictiveIssueNumber (from C4::Serials)
4
5 use Modern::Perl;
6 use Test::More tests => 5;
7
8 use Koha::Database;
9 use C4::Serials;
10 use C4::Serials::Frequency;
11
12 my $schema  = Koha::Database->new->schema;
13 $schema->storage->txn_begin;
14 my $dbh = C4::Context->dbh;
15
16 subtest 'Tests for irregular frequency' => sub {
17     plan tests => 2;
18
19     # Add a frequency
20     my $freq_irr = AddSubscriptionFrequency({
21         description => "Irregular",
22         unit => undef,
23     });
24
25     # Test it
26     my $subscription = {
27         periodicity => $freq_irr,
28         firstacquidate => '1972-02-07',
29     };
30     is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-12-31'), undef, 'Irregular: should be undef' );
31     is( C4::Serials::GetFictiveIssueNumber($subscription, '1973-12-31'), undef, 'Irregular: still undef' );
32 };
33
34 subtest 'Tests for yearly frequencies' => sub {
35     plan tests => 10;
36
37     # First add a few frequencies
38     my $freq_1i_1y = AddSubscriptionFrequency({
39         description => "1 issue per year",
40         unit => 'year',
41         issuesperunit => 1,
42         unitsperissue => 1,
43     });
44     my $freq_1i_3y = AddSubscriptionFrequency({
45         description => "1 issue per 3 years",
46         unit => 'year',
47         issuesperunit => 1,
48         unitsperissue => 3,
49     });
50     my $freq_5i_1y = AddSubscriptionFrequency({
51         description => "5 issues per year",
52         unit => 'year',
53         issuesperunit => 5,
54         unitsperissue => 1,
55     });
56     my $freq_366i_1y = AddSubscriptionFrequency({
57         description => "366 issue per year",
58         unit => 'year',
59         issuesperunit => 366,
60         unitsperissue => 1,
61     });
62
63     # TEST CASE - 1 issue per year
64     my $subscription = {
65         periodicity => $freq_1i_1y,
66         firstacquidate => '1972-02-10',
67         countissuesperunit => 1,
68     };
69     is( C4::Serials::GetFictiveIssueNumber($subscription, '1973-02-09'), 1, 'Feb 9 still 1' );
70     is( C4::Serials::GetFictiveIssueNumber($subscription, '1973-02-10'), 2, 'Feb 10 goes to 2' );
71
72     # TEST CASE - 1 issue per 3 years
73     $subscription->{periodicity} = $freq_1i_3y;
74     $subscription->{firstacquidate} = '1972-02-20';
75     is( C4::Serials::GetFictiveIssueNumber($subscription, '1975-02-19'), 1, 'Feb 19, 1975 still 1' );
76     is( C4::Serials::GetFictiveIssueNumber($subscription, '1975-02-20'), 2, 'Feb 20, 1975 goes to 2' );
77
78     # TEST CASE - 5 issues per year
79     $subscription->{periodicity} = $freq_5i_1y;
80     $subscription->{firstacquidate} = '1972-02-29'; #leap year
81     $subscription->{countissuesperunit} = 1;
82     is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-05-11'), 1, 'May 11 still 1' );
83     $subscription->{countissuesperunit} = 2;
84     is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-05-12'), 2, 'May 12 goes to 2' );
85     $subscription->{countissuesperunit} = 5;
86     is( C4::Serials::GetFictiveIssueNumber($subscription, '1973-02-27'), 5, 'Feb 27 should still be 5' );
87     $subscription->{countissuesperunit} = 1;
88     is( C4::Serials::GetFictiveIssueNumber($subscription, '1973-02-28'), 6, 'Feb 28 goes to 6' );
89
90     # TEST CASE - 366 issues per year (hypothetical example)
91     # Testing prevention of divide by zero
92     $subscription->{periodicity} = $freq_366i_1y;
93     $subscription->{firstacquidate} = '1972-02-29'; #leap year
94     $subscription->{countissuesperunit} = 366;
95     is( C4::Serials::GetFictiveIssueNumber($subscription, '1973-02-27'), 366, 'Feb 27 still at 366' );
96     is( C4::Serials::GetFictiveIssueNumber($subscription, '1973-02-28'), 732, 'Feb 28 goes to 732' );
97
98 };
99
100 subtest 'Tests for monthly frequencies' => sub {
101     plan tests => 8;
102
103     # First add a few frequencies
104     my $freq_1i_5m = AddSubscriptionFrequency({
105         description => "1 issue per 5 months",
106         unit => 'month',
107         issuesperunit => 1,
108         unitsperissue => 5,
109     });
110     my $freq_4i_1m = AddSubscriptionFrequency({
111         description => "4 issue per month",
112         unit => 'month',
113         issuesperunit => 4,
114         unitsperissue => 1,
115     });
116
117     # TEST CASE - 1 issue per 5 months
118     my $subscription = {
119         periodicity => $freq_1i_5m,
120         firstacquidate => '1972-02-10',
121         countissuesperunit => 1,
122     };
123     is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-07-09'), 1, 'Jul 9 still 1' );
124     is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-07-10'), 2, 'Jul 10 goes to 2' );
125     is( C4::Serials::GetFictiveIssueNumber($subscription, '1973-05-09'), 3, 'May 9 still 3' );
126     is( C4::Serials::GetFictiveIssueNumber($subscription, '1973-05-10'), 4, 'May 10 goes to 4' );
127
128     # TEST CASE - 4 issue per 1 months
129     $subscription = {
130         periodicity => $freq_4i_1m,
131         firstacquidate => '1972-02-22',
132         countissuesperunit => 1,
133     };
134     is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-02-28'), 1, 'Feb 28 still 1' );
135     $subscription->{countissuesperunit} = 2;
136     is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-02-29'), 2, 'Feb 29 goes to 2' );
137     $subscription->{countissuesperunit} = 4;
138     is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-03-21'), 4, 'Mar 21 still 4' );
139     $subscription->{countissuesperunit} = 1;
140     is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-03-22'), 5, 'Mar 22 goes to 5' );
141
142 };
143
144 subtest 'Tests for weekly frequencies' => sub {
145     plan tests => 4;
146
147     # First add a few frequencies
148     my $freq_1i_7w = AddSubscriptionFrequency({
149         description => "1 issue per 7 weeks",
150         unit => 'week',
151         issuesperunit => 1,
152         unitsperissue => 7,
153     });
154     my $freq_3i_1w = AddSubscriptionFrequency({
155         description => "3 issues per week",
156         unit => 'week',
157         issuesperunit => 3,
158         unitsperissue => 1,
159     });
160
161     # TEST CASE - 1 issue per 7 weeks
162     my $subscription = {
163         periodicity => $freq_1i_7w,
164         firstacquidate => '1972-02-10',
165         countissuesperunit => 1,
166     };
167     is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-03-29'), 1, 'Mar 29 still 1' );
168     is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-03-30'), 2, 'Mar 30 goes to 2' );
169
170     # TEST CASE - 3 issue per 1 week
171     $subscription = {
172         periodicity => $freq_3i_1w,
173         firstacquidate => '1972-02-03',
174         countissuesperunit => 1,
175     };
176     $subscription->{countissuesperunit} = 3;
177     is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-02-09'), 3, 'Feb 9 still 3' );
178     $subscription->{countissuesperunit} = 1;
179     is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-02-10'), 4, 'Feb 10 goes to 4' );
180 };
181
182 subtest 'Tests for dayly frequencies' => sub {
183     plan tests => 4;
184
185     # First add a few frequencies
186     my $freq_1i_12d = AddSubscriptionFrequency({
187         description => "1 issue per 12 days",
188         unit => 'day',
189         issuesperunit => 1,
190         unitsperissue => 12,
191     });
192     my $freq_3i_1d = AddSubscriptionFrequency({
193         description => "3 issues per day",
194         unit => 'day',
195         issuesperunit => 3,
196         unitsperissue => 1,
197     });
198
199     # TEST CASE - 1 issue per 12 days
200     my $subscription = {
201         periodicity => $freq_1i_12d,
202         firstacquidate => '1972-03-16',
203         countissuesperunit => 1,
204     };
205     is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-03-27'), 1, 'Mar 27 still 1' );
206     is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-03-28'), 2, 'Mar 28 goes to 2' );
207
208     # TEST CASE - 3 issue per day
209     $subscription = {
210         periodicity => $freq_3i_1d,
211         firstacquidate => '1972-04-23',
212         countissuesperunit => 1,
213     };
214     $subscription->{countissuesperunit} = 3;
215     is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-05-01'), 27, 'May 1 still 27' );
216     $subscription->{countissuesperunit} = 1;
217     is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-05-02'), 28, 'May 2 goes to 28' );
218 };
219
220 $schema->storage->txn_rollback;