3 # This test deals with GetFictiveIssueNumber (from C4::Serials)
6 use Test::More tests => 5;
9 use C4::Serials qw( GetFictiveIssueNumber GetSubscription );
10 use C4::Serials::Frequency;
12 my $schema = Koha::Database->new->schema;
13 $schema->storage->txn_begin;
14 my $dbh = C4::Context->dbh;
16 subtest 'Tests for irregular frequency' => sub {
20 my $freq_irr = AddSubscriptionFrequency({
21 description => "Irregular",
27 periodicity => $freq_irr,
28 firstacquidate => '1972-02-07',
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' );
34 subtest 'Tests for yearly frequencies' => sub {
37 # First add a few frequencies
38 my $freq_1i_1y = AddSubscriptionFrequency({
39 description => "1 issue per year",
44 my $freq_1i_3y = AddSubscriptionFrequency({
45 description => "1 issue per 3 years",
50 my $freq_5i_1y = AddSubscriptionFrequency({
51 description => "5 issues per year",
56 my $freq_366i_1y = AddSubscriptionFrequency({
57 description => "366 issue per year",
63 # TEST CASE - 1 issue per year
65 periodicity => $freq_1i_1y,
66 firstacquidate => '1972-02-10',
67 countissuesperunit => 1,
70 my $frequency = GetSubscriptionFrequency($freq_1i_1y);
72 is( C4::Serials::GetFictiveIssueNumber($subscription, '1973-02-09', $frequency), 1, 'Feb 9 still 1' );
73 is( C4::Serials::GetFictiveIssueNumber($subscription, '1973-02-10', $frequency), 2, 'Feb 10 goes to 2' );
75 # TEST CASE - 1 issue per 3 years
76 $subscription->{periodicity} = $freq_1i_3y;
77 $subscription->{firstacquidate} = '1972-02-20';
78 $frequency = GetSubscriptionFrequency($freq_1i_3y);
79 is( C4::Serials::GetFictiveIssueNumber($subscription, '1975-02-19', $frequency), 1, 'Feb 19, 1975 still 1' );
80 is( C4::Serials::GetFictiveIssueNumber($subscription, '1975-02-20', $frequency), 2, 'Feb 20, 1975 goes to 2' );
82 # TEST CASE - 5 issues per year
83 $subscription->{periodicity} = $freq_5i_1y;
84 $subscription->{firstacquidate} = '1972-02-29'; #leap year
85 $subscription->{countissuesperunit} = 1;
86 $frequency = GetSubscriptionFrequency($freq_5i_1y);
87 is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-05-11', $frequency), 1, 'May 11 still 1' );
88 $subscription->{countissuesperunit} = 2;
89 is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-05-12', $frequency), 2, 'May 12 goes to 2' );
90 $subscription->{countissuesperunit} = 5;
91 is( C4::Serials::GetFictiveIssueNumber($subscription, '1973-02-27', $frequency), 5, 'Feb 27 should still be 5' );
92 $subscription->{countissuesperunit} = 1;
93 is( C4::Serials::GetFictiveIssueNumber($subscription, '1973-02-28', $frequency), 6, 'Feb 28 goes to 6' );
95 # TEST CASE - 366 issues per year (hypothetical example)
96 # Testing prevention of divide by zero
97 $subscription->{periodicity} = $freq_366i_1y;
98 $subscription->{firstacquidate} = '1972-02-29'; #leap year
99 $subscription->{countissuesperunit} = 366;
100 $frequency = GetSubscriptionFrequency($freq_366i_1y);
101 is( C4::Serials::GetFictiveIssueNumber($subscription, '1973-02-27', $frequency), 366, 'Feb 27 still at 366' );
102 is( C4::Serials::GetFictiveIssueNumber($subscription, '1973-02-28', $frequency), 732, 'Feb 28 goes to 732' );
106 subtest 'Tests for monthly frequencies' => sub {
109 # First add a few frequencies
110 my $freq_1i_5m = AddSubscriptionFrequency({
111 description => "1 issue per 5 months",
116 my $freq_4i_1m = AddSubscriptionFrequency({
117 description => "4 issue per month",
123 # TEST CASE - 1 issue per 5 months
125 periodicity => $freq_1i_5m,
126 firstacquidate => '1972-02-10',
127 countissuesperunit => 1,
129 my $frequency = GetSubscriptionFrequency($freq_1i_5m);
130 is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-07-09', $frequency), 1, 'Jul 9 still 1' );
131 is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-07-10', $frequency), 2, 'Jul 10 goes to 2' );
132 is( C4::Serials::GetFictiveIssueNumber($subscription, '1973-05-09', $frequency), 3, 'May 9 still 3' );
133 is( C4::Serials::GetFictiveIssueNumber($subscription, '1973-05-10', $frequency), 4, 'May 10 goes to 4' );
135 # TEST CASE - 4 issue per 1 months
137 periodicity => $freq_4i_1m,
138 firstacquidate => '1972-02-22',
139 countissuesperunit => 1,
141 $frequency = GetSubscriptionFrequency($freq_4i_1m);
142 is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-02-28', $frequency), 1, 'Feb 28 still 1' );
143 $subscription->{countissuesperunit} = 2;
144 is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-02-29', $frequency), 2, 'Feb 29 goes to 2' );
145 $subscription->{countissuesperunit} = 4;
146 is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-03-21', $frequency), 4, 'Mar 21 still 4' );
147 $subscription->{countissuesperunit} = 1;
148 is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-03-22', $frequency), 5, 'Mar 22 goes to 5' );
152 subtest 'Tests for weekly frequencies' => sub {
155 # First add a few frequencies
156 my $freq_1i_7w = AddSubscriptionFrequency({
157 description => "1 issue per 7 weeks",
162 my $freq_3i_1w = AddSubscriptionFrequency({
163 description => "3 issues per week",
169 # TEST CASE - 1 issue per 7 weeks
171 periodicity => $freq_1i_7w,
172 firstacquidate => '1972-02-10',
173 countissuesperunit => 1,
175 my $frequency = GetSubscriptionFrequency($freq_1i_7w);
176 is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-03-29', $frequency), 1, 'Mar 29 still 1' );
177 is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-03-30', $frequency), 2, 'Mar 30 goes to 2' );
179 # TEST CASE - 3 issue per 1 week
181 periodicity => $freq_3i_1w,
182 firstacquidate => '1972-02-03',
183 countissuesperunit => 1,
185 $subscription->{countissuesperunit} = 3;
186 $frequency = GetSubscriptionFrequency($freq_3i_1w);
187 is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-02-09', $frequency), 3, 'Feb 9 still 3' );
188 $subscription->{countissuesperunit} = 1;
189 is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-02-10', $frequency), 4, 'Feb 10 goes to 4' );
192 subtest 'Tests for dayly frequencies' => sub {
195 # First add a few frequencies
196 my $freq_1i_12d = AddSubscriptionFrequency({
197 description => "1 issue per 12 days",
202 my $freq_3i_1d = AddSubscriptionFrequency({
203 description => "3 issues per day",
209 # TEST CASE - 1 issue per 12 days
211 periodicity => $freq_1i_12d,
212 firstacquidate => '1972-03-16',
213 countissuesperunit => 1,
215 my $frequency = GetSubscriptionFrequency($freq_1i_12d);
216 is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-03-27', $frequency), 1, 'Mar 27 still 1' );
217 is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-03-28', $frequency), 2, 'Mar 28 goes to 2' );
219 # TEST CASE - 3 issue per day
221 periodicity => $freq_3i_1d,
222 firstacquidate => '1972-04-23',
223 countissuesperunit => 1,
225 $subscription->{countissuesperunit} = 3;
226 $frequency = GetSubscriptionFrequency($freq_3i_1d);
227 is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-05-01', $frequency), 27, 'May 1 still 27' );
228 $subscription->{countissuesperunit} = 1;
229 is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-05-02', $frequency), 28, 'May 2 goes to 28' );
232 $schema->storage->txn_rollback;