Bug 18356: Extend GetNextDate.t, add GetFictiveIssueNumber.t (unit=year)
[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 => 2;
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'), 0, 'Irregular: should be zero' );
31     is( C4::Serials::GetFictiveIssueNumber($subscription, '1973-12-31'), 0, 'Irregular: still zero' );
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 # TODO: subtest 'Tests for monthly frequencies' => sub {
101 # TODO: subtest 'Tests for weekly frequencies' => sub {
102 # TODO: subtest 'Tests for dayly frequencies' => sub {
103
104 $schema->storage->txn_rollback;