From 900bc4f1f57b96052fc658d89058e1bc4cf9717f Mon Sep 17 00:00:00 2001
From: Baptiste Wojtkowski <baptiste.wojtkowski@biblibre.com>
Date: Thu, 23 Feb 2017 16:21:05 +0000
Subject: [PATCH] BUG 11580 : Added unit test

Added one unit test when the syspref useDaysMode is active.  To perform it I
had to put the function add_holiday in the perl module Koha/Calendar.pm from
the script tools/newHolidays.pl, in order to use the function in the tests.
---
 Koha/Calendar.pm                        | 67 +++++++++++++++++++++++++
 t/db_dependent/Circulation/dateexpiry.t | 26 +++++++++-
 tools/newHolidays.pl                    | 88 +++++----------------------------
 3 files changed, 104 insertions(+), 77 deletions(-)

diff --git a/Koha/Calendar.pm b/Koha/Calendar.pm
index f6cb58c..b36beab 100644
--- a/Koha/Calendar.pm
+++ b/Koha/Calendar.pm
@@ -7,6 +7,7 @@ use DateTime;
 use DateTime::Set;
 use DateTime::Duration;
 use C4::Context;
+use C4::Calendar;
 use Koha::Caches;
 use Carp;
 
@@ -368,6 +369,72 @@ sub clear_weekly_closed_days {
     return;
 }
 
+
+sub add_holiday {
+    my ($newoperation, $branchcode, $weekday, $day, $month, $year, $title, $description, @holiday_list) = @_;  
+    my $calendar = C4::Calendar->new(branchcode => $branchcode);
+
+    if ($newoperation eq 'weekday') {
+        unless ( $weekday && ($weekday ne '') ) { 
+            # was dow calculated by javascript?  original code implies it was supposed to be.
+            # if not, we need it.
+            $weekday = &Date::Calc::Day_of_Week($year, $month, $day) % 7 unless($weekday);
+        }
+        unless($calendar->isHoliday($day, $month, $year)) {
+        $calendar->insert_week_day_holiday(weekday => $weekday,
+                                           title => $title,
+                                           description => $description);
+        }
+    }elsif ($newoperation eq 'repeatable') {
+        unless($calendar->isHoliday($day, $month, $year)) {
+            $calendar->insert_day_month_holiday(day => $day,
+                                                month => $month, 
+                                                title => $title,
+                                                description => $description);
+        }
+    } elsif ($newoperation eq 'holiday') {
+        unless($calendar->isHoliday($day, $month, $year)) {
+            $calendar->insert_single_holiday(
+                day => $day,
+                month => $month,
+                year => $year,
+                title => $title,
+                description => $description);
+        }
+
+    } elsif ( $newoperation eq 'holidayrange' ) {
+        if (@holiday_list){
+            foreach my $date (@holiday_list){
+                unless ( $calendar->isHoliday( $date->{local_c}->{day}, $date->{local_c}->{month}, $date->{local_c}->{year} ) ) {
+                    $calendar->insert_single_holiday(
+                            day         => $date->{local_c}->{day},
+                            month       => $date->{local_c}->{month},
+                            year        => $date->{local_c}->{year},
+                            title       => $title,
+                            description => $description
+                    );
+                 }
+            }
+        }
+    } elsif ( $newoperation eq 'holidayrangerepeat' ) {
+        if (@holiday_list){
+            foreach my $date (@holiday_list){
+                unless ( $calendar->isHoliday( $date->{local_c}->{day}, $date->{local_c}->{month}, $date->{local_c}->{year} ) ) {
+                    $calendar->insert_day_month_holiday(
+                        day         => $date->{local_c}->{day},
+                        month       => $date->{local_c}->{month},
+                        title       => $title,
+                        description => $description
+                    );
+                }
+            }
+        }
+    }
+    # we updated the single_holidays table, so wipe its cache
+    my $cache = Koha::Caches->get_instance();
+    $cache->clear_from_cache( 'single_holidays') ;
+}
+
 1;
 __END__
 
diff --git a/t/db_dependent/Circulation/dateexpiry.t b/t/db_dependent/Circulation/dateexpiry.t
index 57f306e..8e1cc7b 100644
--- a/t/db_dependent/Circulation/dateexpiry.t
+++ b/t/db_dependent/Circulation/dateexpiry.t
@@ -23,6 +23,7 @@ use Test::More tests => 2;
 use C4::Members;
 use Koha::DateUtils;
 use Koha::Database;
+use Koha::Calendar;
 
 use t::lib::TestBuilder;
 use t::lib::Mocks qw( mock_preference );
@@ -39,7 +40,7 @@ subtest 'Tests for CanBookBeIssued related to dateexpiry' => sub {
     can_book_be_issued();
 };
 subtest 'Tests for CalcDateDue related to dateexpiry' => sub {
-    plan tests => 4;
+    plan tests => 5;
     calc_date_due();
 };
 
@@ -82,6 +83,7 @@ sub can_book_be_issued {
 
 sub calc_date_due {
     t::lib::Mocks::mock_preference( 'ReturnBeforeExpiry', 1 );
+    t::lib::Mocks::mock_preference( 'useDaysMode', 'Days' );
 
     # this triggers the compare between expiry and due date
 
@@ -116,6 +118,28 @@ sub calc_date_due {
     $d = C4::Circulation::CalcDateDue( $today, $item->{itype}, $branch->{branchcode}, $patron );
     my $t2 = time;
     is( ref $d eq "DateTime" && $t2 - $t1 < 1, 1, "CalcDateDue with expiry in year 9876 in " . sprintf( "%6.4f", $t2 - $t1 ) . " seconds." );
+     
+    # fifth test takes account of closed days
+    $d = C4::Circulation::CalcDateDue( $today, $item->{itype}, $branch->{branchcode}, $patron );
+    t::lib::Mocks::mock_preference( 'useDaysMode', 'Datedue' );
+    my $calendar = C4::Calendar->new(branchcode => $branch->{branchcode});
+    Koha::Calendar::add_holiday('holiday', 
+                            $branch->{branchcode}, 
+                            $d->day_of_week(),
+                            $d->day(),
+                            $d->month(),
+                            $d->year(),
+                            'holidayTest',
+                            'holidayDesc'
+    );
+    $calendar->delete_holiday(weekday => $d->day_of_week() - 1, day => $d->day()-1, month =>$d->month(), year=>$d->year() );
+    $d2 = C4::Circulation::CalcDateDue( $today, $item->{itype}, $branch->{branchcode}, $patron );
+    $d2->add(days => 1);
+    $d->truncate( to => 'day' );
+    $d2->truncate( to => 'day' );
+    warn Data::Dumper::Dumper($d->datetime());
+    warn Data::Dumper::Dumper($d2->datetime());
+    is ( DateTime->compare( $d, $d2) == 0, 1, "no problem with closed days");
 }
 
 $schema->storage->txn_rollback;
diff --git a/tools/newHolidays.pl b/tools/newHolidays.pl
index eb948c7..7ebca63 100755
--- a/tools/newHolidays.pl
+++ b/tools/newHolidays.pl
@@ -15,20 +15,21 @@ use Koha::Caches;
 use C4::Calendar;
 use DateTime;
 use Koha::DateUtils;
+use Koha::Calendar;
 
 my $input               = new CGI;
 my $dbh                 = C4::Context->dbh();
 
-our $branchcode          = $input->param('newBranchName');
+my $branchcode          = $input->param('newBranchName');
 my $originalbranchcode  = $branchcode;
-our $weekday             = $input->param('newWeekday');
-our $day                 = $input->param('newDay');
-our $month               = $input->param('newMonth');
-our $year                = $input->param('newYear');
+my $weekday             = $input->param('newWeekday');
+my $day                 = $input->param('newDay');
+my $month               = $input->param('newMonth');
+my $year                = $input->param('newYear');
 my $dateofrange         = $input->param('dateofrange');
-our $title               = $input->param('newTitle');
-our $description         = $input->param('newDescription');
-our $newoperation        = $input->param('newOperation');
+my $title               = $input->param('newTitle');
+my $description         = $input->param('newDescription');
+my $newoperation        = $input->param('newOperation');
 my $allbranches         = $input->param('allBranches');
 
 
@@ -46,7 +47,7 @@ if ($description) {
 }
 
 # We make an array with holiday's days
-our @holiday_list;
+my @holiday_list;
 if ($end_dt){
     for (my $dt = $first_dt->clone();
     $dt <= $end_dt;
@@ -59,75 +60,10 @@ if ($end_dt){
 if($allbranches) {
     my $libraries = Koha::Libraries->search;
     while ( my $library = $libraries->next ) {
-        add_holiday($newoperation, $library->branchcode, $weekday, $day, $month, $year, $title, $description);
+        Koha::Calendar::add_holiday($newoperation, $library->branchcode, $weekday, $day, $month, $year, $title, $description, @holiday_list);
     }
 } else {
-    add_holiday($newoperation, $branchcode, $weekday, $day, $month, $year, $title, $description);
+    Koha::Calendar::add_holiday($newoperation, $branchcode, $weekday, $day, $month, $year, $title, $description, @holiday_list);
 }
 
 print $input->redirect("/cgi-bin/koha/tools/holidays.pl?branch=$originalbranchcode&calendardate=$calendardate");
-
-#FIXME: move add_holiday() to a better place
-sub add_holiday {
-	($newoperation, $branchcode, $weekday, $day, $month, $year, $title, $description) = @_;  
-	my $calendar = C4::Calendar->new(branchcode => $branchcode);
-
-	if ($newoperation eq 'weekday') {
-		unless ( $weekday && ($weekday ne '') ) { 
-			# was dow calculated by javascript?  original code implies it was supposed to be.
-			# if not, we need it.
-			$weekday = &Date::Calc::Day_of_Week($year, $month, $day) % 7 unless($weekday);
-		}
-		unless($calendar->isHoliday($day, $month, $year)) {
-			$calendar->insert_week_day_holiday(weekday => $weekday,
-							           title => $title,
-							           description => $description);
-		}
-	} elsif ($newoperation eq 'repeatable') {
-		unless($calendar->isHoliday($day, $month, $year)) {
-			$calendar->insert_day_month_holiday(day => $day,
-	                                    month => $month,
-							            title => $title,
-							            description => $description);
-		}
-	} elsif ($newoperation eq 'holiday') {
-		unless($calendar->isHoliday($day, $month, $year)) {
-			$calendar->insert_single_holiday(day => $day,
-	                                 month => $month,
-						             year => $year,
-						             title => $title,
-						             description => $description);
-		}
-
-	} elsif ( $newoperation eq 'holidayrange' ) {
-        if (@holiday_list){
-            foreach my $date (@holiday_list){
-                unless ( $calendar->isHoliday( $date->{local_c}->{day}, $date->{local_c}->{month}, $date->{local_c}->{year} ) ) {
-                    $calendar->insert_single_holiday(
-                        day         => $date->{local_c}->{day},
-                        month       => $date->{local_c}->{month},
-                        year        => $date->{local_c}->{year},
-                        title       => $title,
-                        description => $description
-                    );
-                }
-            }
-        }
-    } elsif ( $newoperation eq 'holidayrangerepeat' ) {
-        if (@holiday_list){
-            foreach my $date (@holiday_list){
-                unless ( $calendar->isHoliday( $date->{local_c}->{day}, $date->{local_c}->{month}, $date->{local_c}->{year} ) ) {
-                    $calendar->insert_day_month_holiday(
-                        day         => $date->{local_c}->{day},
-                        month       => $date->{local_c}->{month},
-                        title       => $title,
-                        description => $description
-                    );
-                }
-            }
-        }
-    }
-    # we updated the single_holidays table, so wipe its cache
-    my $cache = Koha::Caches->get_instance();
-    $cache->clear_from_cache( 'single_holidays') ;
-}
-- 
2.7.4