From 9e4fa63e572de16291a9b15ec11df30f8e24d32c Mon Sep 17 00:00:00 2001
From: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Date: Thu, 11 May 2017 10:47:22 +0200
Subject: [PATCH] Bug 18356: Extend GetNextDate.t, add GetFictiveIssueNumber.t
 (unit=year)
Content-Type: text/plain; charset=utf-8

This patch deals with tests for yearly frequencies.

Adjust/extend GetNextDate.t:
[1] Adjust mixup of units/issues in a description.
[2] Add testing +2 years on 29-2 of leap year for freq 1 issue/2 years.
[3] Add tests for freq 9 issues/year.

Add GetFictiveIssueNumber.t:
[1] Two subtests are provided for irregular frequencies (very trivial) and
    for year frequencies (with four specific test cases).

Test plan:
[1] Run t/db_dependent/Serials/GetNextDate.t
[2] Run t/db_dependent/Serials/GetFictiveIssueNumber.t

Note: Without the second patch both tests should fail. This shows the need
of the adjustments in the second patch.

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
---
 t/db_dependent/Serials/GetFictiveIssueNumber.t | 99 ++++++++++++++++++++++++++
 t/db_dependent/Serials/GetNextDate.t           | 47 +++++++++---
 2 files changed, 138 insertions(+), 8 deletions(-)
 create mode 100644 t/db_dependent/Serials/GetFictiveIssueNumber.t

diff --git a/t/db_dependent/Serials/GetFictiveIssueNumber.t b/t/db_dependent/Serials/GetFictiveIssueNumber.t
new file mode 100644
index 0000000..ed79bba
--- /dev/null
+++ b/t/db_dependent/Serials/GetFictiveIssueNumber.t
@@ -0,0 +1,99 @@
+#!/usr/bin/perl
+
+# This test deals with GetFictiveIssueNumber (from C4::Serials)
+
+use Modern::Perl;
+use Test::More tests => 2;
+
+use Koha::Database;
+use C4::Serials;
+use C4::Serials::Frequency;
+
+my $schema  = Koha::Database->new->schema;
+$schema->storage->txn_begin;
+my $dbh = C4::Context->dbh;
+
+subtest 'Tests for irregular frequency' => sub {
+    plan tests => 2;
+
+    # Add a frequency
+    my $freq_irr = AddSubscriptionFrequency({
+        description => "Irregular",
+        unit => undef,
+    });
+
+    # Test it
+    my $subscription = {
+        periodicity => $freq_irr,
+        firstacquidate => '1972-02-07',
+    };
+    is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-12-31'), 0, 'Irregular: should be zero' );
+    is( C4::Serials::GetFictiveIssueNumber($subscription, '1973-12-31'), 0, 'Irregular: still zero' );
+};
+
+subtest 'Tests for yearly frequencies' => sub {
+    plan tests => 10;
+
+    # First add a few frequencies
+    my $freq_1i_1y = AddSubscriptionFrequency({
+        description => "1 issue per year",
+        unit => 'year',
+        issuesperunit => 1,
+        unitsperissue => 1,
+    });
+    my $freq_1i_3y = AddSubscriptionFrequency({
+        description => "1 issue per 3 years",
+        unit => 'year',
+        issuesperunit => 1,
+        unitsperissue => 3,
+    });
+    my $freq_5i_1y = AddSubscriptionFrequency({
+        description => "5 issues per year",
+        unit => 'year',
+        issuesperunit => 5,
+        unitsperissue => 1,
+    });
+    my $freq_366i_1y = AddSubscriptionFrequency({
+        description => "366 issue per year",
+        unit => 'year',
+        issuesperunit => 366,
+        unitsperissue => 1,
+    });
+
+    # TEST CASE - 1 issue per year
+    my $subscription = {
+        periodicity => $freq_1i_1y,
+        firstacquidate => '1972-02-10',
+        countissuesperunit => 1,
+    };
+    is( C4::Serials::GetFictiveIssueNumber($subscription, '1973-02-09'), 1, 'Feb 9 still 1' );
+    is( C4::Serials::GetFictiveIssueNumber($subscription, '1973-02-10'), 2, 'Feb 10 goes to 2' );
+
+    # TEST CASE - 1 issue per 3 years
+    $subscription->{periodicity} = $freq_1i_3y;
+    $subscription->{firstacquidate} = '1972-02-20';
+    is( C4::Serials::GetFictiveIssueNumber($subscription, '1975-02-19'), 1, 'Feb 19, 1975 still 1' );
+    is( C4::Serials::GetFictiveIssueNumber($subscription, '1975-02-20'), 2, 'Feb 20, 1975 goes to 2' );
+
+    # TEST CASE - 5 issues per year
+    $subscription->{periodicity} = $freq_5i_1y;
+    $subscription->{firstacquidate} = '1972-02-29'; #leap year
+    is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-05-11'), 1, 'May 11 still 1' );
+    is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-05-12'), 2, 'May 12 goes to 2' );
+    is( C4::Serials::GetFictiveIssueNumber($subscription, '1973-02-27'), 5, 'Feb 27 should still be 5' );
+    is( C4::Serials::GetFictiveIssueNumber($subscription, '1973-02-28'), 6, 'Feb 28 goes to 6' );
+
+    # TEST CASE - 366 issues per year (hypothetical example)
+    # Testing prevention of divide by zero
+    $subscription->{periodicity} = $freq_366i_1y;
+    $subscription->{firstacquidate} = '1972-02-29'; #leap year
+    is( C4::Serials::GetFictiveIssueNumber($subscription, '1973-02-27'), 366, 'Feb 27 still at 366' );
+    is( C4::Serials::GetFictiveIssueNumber($subscription, '1973-02-28'), 732, 'Feb 28 goes to 732' );
+
+};
+
+# TODO: subtest 'Tests for monthly frequencies' => sub {
+# TODO: subtest 'Tests for weekly frequencies' => sub {
+# TODO: subtest 'Tests for dayly frequencies' => sub {
+
+$schema->storage->txn_rollback;
diff --git a/t/db_dependent/Serials/GetNextDate.t b/t/db_dependent/Serials/GetNextDate.t
index fe6059f..44dd529 100644
--- a/t/db_dependent/Serials/GetNextDate.t
+++ b/t/db_dependent/Serials/GetNextDate.t
@@ -1,15 +1,16 @@
 #!/usr/bin/perl
 
-use C4::Context;
-use Test::More tests => 96;
 use Modern::Perl;
+use Test::More tests => 102;
 
+use Koha::Database;
+use C4::Serials;
+use C4::Serials::Frequency;
+
+my $schema  = Koha::Database->new->schema;
+$schema->storage->txn_begin;
 my $dbh = C4::Context->dbh;
-$dbh->{RaiseError} = 1;
-$dbh->{AutoCommit} = 0;
 
-use C4::Serials::Frequency;
-use C4::Serials;
 
 # TEST CASE - 1 issue per day, no irregularities
 my $frequency = {
@@ -469,10 +470,14 @@ $publisheddate = GetNextDate($subscription, $publisheddate);
 is($publisheddate, '1978-01-01');
 $publisheddate = GetNextDate($subscription, $publisheddate);
 is($publisheddate, '1980-01-01');
+# Move publisheddate to Feb 29 (leap year 1980)
+$publisheddate = '1980-02-29';
+$publisheddate = GetNextDate( $subscription, $publisheddate );
+is( $publisheddate, '1982-02-28', 'Test +2 year from Feb 29' );
 
 # TEST CASE - 2 issues per year, no irregularity
 $id = AddSubscriptionFrequency({
-    description => "1 issue every 2 years",
+    description => "2 issues per year",
     unit => 'year',
     issuesperunit => 2,
     unitsperissue => 1,
@@ -512,6 +517,32 @@ is($publisheddate, '1973-07-02');
 $publisheddate = GetNextDate($subscription, $publisheddate);
 is($publisheddate, '1974-01-01');
 
+# TEST CASE - 9 issues per year, dates spread throughout month
+$id = AddSubscriptionFrequency({
+    description => "9 issues per year",
+    unit => 'year',
+    issuesperunit => 9,
+    unitsperissue => 1,
+});
+$subscription = {
+    periodicity => $id,
+    firstacquidate => '1970-08-10',
+    irregularity => '',
+    countissuesperunit => 1,
+};
+my @dates = ( $subscription->{firstacquidate} );
+foreach(1..27) {
+    push @dates, GetNextDate( $subscription, $dates[-1] );
+}
+is( $dates[9],  '1971-08-10', 'Freq 9/yr, 1 year passed' );
+is( $dates[18], '1972-08-10', 'Freq 9/yr, 2 years passed (leap year)' );
+is( $dates[27], '1973-08-10', 'Freq 9/yr, 3 years passed' );
+# Keep (first) position in cycle, but shift back 9 days
+is( GetNextDate( $subscription, '1973-08-01' ), '1973-09-10', 'Back 9 days, without annual correction' );
+# Set position to last in cycle, and shift back 9 days; annual correction
+$subscription->{countissuesperunit} = 9;
+is( GetNextDate( $subscription, '1973-08-01' ), '1973-09-15', 'Last in cycle, back 9 days, expect annual correction' );
+
 # TEST CASE - Irregular
 $id = AddSubscriptionFrequency({
     description => "Irregular",
@@ -538,4 +569,4 @@ is($publisheddate, undef);
 $publisheddate = GetNextDate(undef, undef);
 is($publisheddate, undef);
 
-$dbh->rollback;
+$schema->storage->txn_rollback;
-- 
2.1.4