Bug 28572: Fix Search.t
[srvgit] / t / db_dependent / Calendar.t
old mode 100644 (file)
new mode 100755 (executable)
index 22f481c..0900b57
 
 use Modern::Perl;
 
-use Test::More tests => 4;
+use Test::More tests => 6;
 use Time::Fake;
+
+use t::lib::Mocks;
 use t::lib::TestBuilder;
 
 use DateTime;
@@ -52,7 +54,7 @@ my $holiday = $builder->build(
     }
 );
 
-my $calendar = Koha::Calendar->new( branchcode => $library->branchcode );
+my $calendar = Koha::Calendar->new( branchcode => $library->branchcode, days_mode => 'Calendar' );
 
 subtest 'days_forward' => sub {
 
@@ -125,27 +127,27 @@ subtest 'hours_between | days_between' => sub {
 
             # Between 5th and 6th
             my $diff_hours = $calendar->hours_between( $now, $nov_6 )->hours;
-            is( $diff_hours, 1 * 24, '' );
+            is( $diff_hours, 1 * 24, 'hours: 1 day, no holiday' );
             my $diff_days = $calendar->days_between( $now, $nov_6 )->delta_days;
-            is( $diff_days, 1, '' );
+            is( $diff_days, 1, 'days: 1 day, no holiday' );
 
             # Between 5th and 7th
             $diff_hours = $calendar->hours_between( $now, $nov_7 )->hours;
-            is( $diff_hours, 2 * 24, '' );
+            is( $diff_hours, 2 * 24, 'hours: 2 days, no holiday' );
             $diff_days = $calendar->days_between( $now, $nov_7 )->delta_days;
-            is( $diff_days, 2, '' );
+            is( $diff_days, 2, 'days: 2 days, no holiday' );
 
             # Between 5th and 12th
             $diff_hours = $calendar->hours_between( $now, $nov_12 )->hours;
-            is( $diff_hours, 7 * 24, '' );
+            is( $diff_hours, 7 * 24, 'hours: 7 days, no holiday' );
             $diff_days = $calendar->days_between( $now, $nov_12 )->delta_days;
-            is( $diff_days, 7, '' );
+            is( $diff_days, 7, 'days: 7 days, no holiday' );
 
             # Between 5th and 15th
             $diff_hours = $calendar->hours_between( $now, $nov_15 )->hours;
-            is( $diff_hours, 10 * 24, '' );
+            is( $diff_hours, 10 * 24, 'hours: 10 days, no holiday' );
             $diff_days = $calendar->days_between( $now, $nov_15 )->delta_days;
-            is( $diff_days, 10, '' );
+            is( $diff_days, 10, 'days: 10 days, no holiday' );
         };
 
         subtest 'Different hours' => sub {
@@ -154,33 +156,33 @@ subtest 'hours_between | days_between' => sub {
 
             # Between 5th and 5th (Same day short hours loan)
             my $diff_hours = $calendar->hours_between( $now, $now->clone->add(hours => 3) )->hours;
-            is( $diff_hours, 3, '' );
+            is( $diff_hours, 3, 'hours: 3 hours, no holidays' );
             my $diff_days = $calendar->days_between( $now, $now->clone->add(hours => 3) )->delta_days;
-            is( $diff_days, 0, '' );
+            is( $diff_days, 0, 'days: 3 hours, no holidays' );
 
             # Between 5th and 6th
             $diff_hours = $calendar->hours_between( $now, $nov_6->clone->subtract(hours => 3) )->hours;
-            is( $diff_hours, 1 * 24 - 3, '' );
+            is( $diff_hours, 1 * 24 - 3, 'hours: 21 hours, no holidays' );
             $diff_days = $calendar->days_between( $now, $nov_6->clone->subtract(hours => 3) )->delta_days;
-            is( $diff_days, 1, '' );
+            is( $diff_days, 1, 'days: 21 hours, no holidays' );
 
             # Between 5th and 7th
             $diff_hours = $calendar->hours_between( $now, $nov_7->clone->subtract(hours => 3) )->hours;
-            is( $diff_hours, 2 * 24 - 3, '' );
+            is( $diff_hours, 2 * 24 - 3, 'hours: 45 hours, no holidays' );
             $diff_days = $calendar->days_between( $now, $nov_7->clone->subtract(hours => 3) )->delta_days;
-            is( $diff_days, 2, '' );
+            is( $diff_days, 2, 'days: 45 hours, no holidays' );
 
             # Between 5th and 12th
             $diff_hours = $calendar->hours_between( $now, $nov_12->clone->subtract(hours => 3) )->hours;
-            is( $diff_hours, 7 * 24 - 3, '' );
+            is( $diff_hours, 7 * 24 - 3, 'hours: 165 hours, no holidays' );
             $diff_days = $calendar->days_between( $now, $nov_12->clone->subtract(hours => 3) )->delta_days;
-            is( $diff_days, 7, '' );
+            is( $diff_days, 7, 'days: 165 hours, no holidays' );
 
             # Between 5th and 15th
             $diff_hours = $calendar->hours_between( $now, $nov_15->clone->subtract(hours => 3) )->hours;
-            is( $diff_hours, 10 * 24 - 3, '' );
+            is( $diff_hours, 10 * 24 - 3, 'hours: 237 hours, no holidays' );
             $diff_days = $calendar->days_between( $now, $nov_15->clone->subtract(hours => 3) )->delta_days;
-            is( $diff_days, 10, '' );
+            is( $diff_days, 10, 'days: 237 hours, no holidays' );
         };
     };
 
@@ -199,83 +201,193 @@ subtest 'hours_between | days_between' => sub {
         my $calendar = Koha::Calendar->new( branchcode => $library->branchcode );
 
         subtest 'Same hours' => sub {
-            plan tests => 10;
+            plan tests => 12;
 
             my ( $diff_hours, $diff_days );
 
-            # Between 5th and 6th
+            # Between 5th and 6th (This case should never happen in real code, one cannot return on a closed day)
             $diff_hours = $calendar->hours_between( $now, $nov_6 )->hours;
-            is( $diff_hours, 0 * 24, '' ); # FIXME Is this really should be 0?
+            is( $diff_hours, 0 * 24, 'hours: 1 day, end_dt = holiday' ); # FIXME Is this really should be 0?
             $diff_days = $calendar->days_between( $now, $nov_6)->delta_days;
-            is( $diff_days, 0, '' ); # FIXME Is this really should be 0?
+            is( $diff_days, 0, 'days: 1 day, end_dt = holiday' ); # FIXME Is this really should be 0?
+
+            # Between 6th and 7th (This case should never happen in real code, one cannot issue on a closed day)
+            $diff_hours = $calendar->hours_between( $nov_6, $nov_7 )->hours;
+            is( $diff_hours, 0 * 24, 'hours: 1 day, start_dt = holiday' ); # FIXME Is this really should be 0?
+            $diff_days = $calendar->days_between( $nov_6, $nov_7 )->delta_days;
+            is( $diff_days, 0, 'days: 1 day, start_dt = holiday' ); # FIXME Is this really should be 0?
 
             # Between 5th and 7th
             $diff_hours = $calendar->hours_between( $now, $nov_7 )->hours;
-            is( $diff_hours, 2 * 24 - 1 * 24, '' );
-            $diff_days = $calendar->days_between( $now, $nov_7)->delta_days;
-            is( $diff_days, 2 - 1, '' );
+            is( $diff_hours, 2 * 24 - 1 * 24, 'hours: 2 days, 1 holiday' );
+            $diff_days = $calendar->days_between( $now, $nov_7 )->delta_days;
+            is( $diff_days, 2 - 1, 'days: 2 days, 1 holiday' );
 
             # Between 5th and 12th
             $diff_hours = $calendar->hours_between( $now, $nov_12 )->hours;
-            is( $diff_hours, 7 * 24 - 1 * 24, '' );
+            is( $diff_hours, 7 * 24 - 1 * 24, 'hours: 7 days, 1 holiday' );
             $diff_days = $calendar->days_between( $now, $nov_12)->delta_days;
-            is( $diff_days, 7 - 1, '' );
+            is( $diff_days, 7 - 1, 'day: 7 days, 1 holiday' );
 
             # Between 5th and 13th
             $diff_hours = $calendar->hours_between( $now, $nov_13 )->hours;
-            is( $diff_hours, 8 * 24 - 2 * 24, '' );
+            is( $diff_hours, 8 * 24 - 2 * 24, 'hours: 8 days, 2 holidays' );
             $diff_days = $calendar->days_between( $now, $nov_13)->delta_days;
-            is( $diff_days, 8 - 2, '' );
+            is( $diff_days, 8 - 2, 'days: 8 days, 2 holidays' );
 
             # Between 5th and 15th
             $diff_hours = $calendar->hours_between( $now, $nov_15 )->hours;
-            is( $diff_hours, 10 * 24 - 2 * 24, '' );
+            is( $diff_hours, 10 * 24 - 2 * 24, 'hours: 10 days, 2 holidays' );
             $diff_days = $calendar->days_between( $now, $nov_15)->delta_days;
-            is( $diff_days, 10 - 2, '' );
+            is( $diff_days, 10 - 2, 'days: 10 days, 2 holidays' );
         };
 
         subtest 'Different hours' => sub {
-            plan tests => 11;
+            plan tests => 14;
 
             my ( $diff_hours, $diff_days );
 
             # Between 5th and 5th (Same day short hours loan)
-            # No test - one cannot issue on a holiday and we do not yet record opening hours to take those into account
+            # No test - Tested above as 5th is an open day
 
-            # Between 5th and 6th
+            # Between 5th and 6th (This case should never happen in real code, one cannot return on a closed day)
             my $duration = $calendar->hours_between( $now, $nov_6->clone->subtract(hours => 3) );
-            is( $duration->hours, abs(0 * 24 - 3), '' ); # FIXME $duration->hours always return a abs
-            is( $duration->is_negative, 1, ); # FIXME Do really test for that case in our calls to hours_between?
+            is( $duration->hours, abs(0 * 24 - 3), 'hours: 21 hours, end_dt = holiday' ); # FIXME $duration->hours always return a abs
+            is( $duration->is_negative, 1, '? is negative ?' ); # FIXME Do really test for that case in our calls to hours_between?
             $duration = $calendar->days_between( $now, $nov_6->clone->subtract(hours => 3) );
-            is( $duration->hours, abs(0), '' ); # FIXME Is this correct?
+            is( $duration->hours, abs(0), 'days: 21 hours, end_dt = holiday' ); # FIXME Is this correct?
+
+            # Between 6th and 7th (This case should never happen in real code, one cannot issue on a closed day)
+            $duration = $calendar->hours_between( $nov_6, $nov_7->clone->subtract(hours => 3) );
+            is( $duration->hours, abs(0 * 24 - 3), 'hours: 21 hours, start_dt = holiday' ); # FIXME $duration->hours always return a abs
+            is( $duration->is_negative, 1, '? is negative ?' ); # FIXME Do really test for that case in our calls to hours_between?
+            $duration = $calendar->days_between( $nov_6, $nov_7->clone->subtract(hours => 3) );
+            is( $duration->hours, abs(0), 'days: 21 hours, start_dt = holiday' ); # FIXME Is this correct?
 
             # Between 5th and 7th
             $diff_hours = $calendar->hours_between( $now, $nov_7->clone->subtract(hours => 3) )->hours;
-            is( $diff_hours, 2 * 24 - 1 * 24 - 3, '' );
+            is( $diff_hours, 2 * 24 - 1 * 24 - 3, 'hours: 45 hours, 1 holiday' );
             $diff_days = $calendar->days_between( $now, $nov_7->clone->subtract(hours => 3) )->delta_days;
-            is( $diff_days, 2 - 1, '' );
+            is( $diff_days, 2 - 1, 'days: 45 hours, 1 holiday' );
 
             # Between 5th and 12th
             $diff_hours = $calendar->hours_between( $now, $nov_12->clone->subtract(hours => 3) )->hours;
-            is( $diff_hours, 7 * 24 - 1 * 24 - 3, '' );
+            is( $diff_hours, 7 * 24 - 1 * 24 - 3, 'hours: 165 hours, 1 holiday' );
             $diff_days = $calendar->days_between( $now, $nov_12->clone->subtract(hours => 3) )->delta_days;
-            is( $diff_days, 7 - 1, '' );
+            is( $diff_days, 7 - 1, 'days: 165 hours, 1 holiday' );
 
             # Between 5th and 13th
             $diff_hours = $calendar->hours_between( $now, $nov_13->clone->subtract(hours => 3) )->hours;
-            is( $diff_hours, 8 * 24 - 2 * 24 - 3, '' );
+            is( $diff_hours, 8 * 24 - 2 * 24 - 3, 'hours: 289 hours, 2 holidays ' );
             $diff_days = $calendar->days_between( $now, $nov_13->clone->subtract(hours => 3) )->delta_days;
-            is( $diff_days, 8 - 1, '' );
+            is( $diff_days, 8 - 1, 'days: 289 hours, 2 holidays' );
 
             # Between 5th and 15th
             $diff_hours = $calendar->hours_between( $now, $nov_15->clone->subtract(hours => 3) )->hours;
-            is( $diff_hours, 10 * 24 - 2 * 24 - 3, '' );
+            is( $diff_hours, 10 * 24 - 2 * 24 - 3, 'hours: 237 hours, 2 holidays' );
             $diff_days = $calendar->days_between( $now, $nov_15->clone->subtract(hours => 3) )->delta_days;
-            is( $diff_days, 10 - 2, '' );
+            is( $diff_days, 10 - 2, 'days: 237 hours, 2 holidays' );
         };
 
     };
 
+    Time::Fake->reset;
+};
+
+subtest 'is_holiday' => sub {
+    plan tests => 1;
+
+    subtest 'weekday holidays' => sub {
+        plan tests => 7;
+
+        my $library = $builder->build_object( { class => 'Koha::Libraries' } );
+
+        my $day = dt_from_string();
+        my $dow = scalar $day->day_of_week;
+        $dow = 0 if $dow == 7;
+
+        # Closed this day of the week
+        my $dbh = C4::Context->dbh;
+        $dbh->do(
+            q|
+            INSERT INTO repeatable_holidays (branchcode,weekday,day,month,title,description)
+            VALUES ( ?, ?, NULL, NULL, ?, '' )
+        |, undef, $library->branchcode, $dow, "TEST"
+        );
+
+        # Iterate 7 days
+        my $sth = $dbh->prepare(
+"UPDATE repeatable_holidays SET weekday = ? WHERE branchcode = ? AND title = 'TEST'"
+        );
+        for my $i ( 0 .. 6 ) {
+            my $calendar =
+              Koha::Calendar->new( branchcode => $library->branchcode );
+
+            is( $calendar->is_holiday($day), 1, $day->day_name() ." works as a repeatable holiday");
+
+            # Increment the date and holiday day
+            $day->add( days => 1 );
+            $dow++;
+            $dow = 0 if $dow == 7;
+            $sth->execute($dow, $library->branchcode);
+        }
+    };
+};
+
+subtest 'get_push_amt' => sub {
+    plan tests => 1;
+
+    t::lib::Mocks::mock_preference('useDaysMode', 'Dayweek');
+
+    subtest 'weekday holidays' => sub {
+        plan tests => 7;
+
+        my $library = $builder->build_object( { class => 'Koha::Libraries' } );
+
+        my $day = dt_from_string();
+        my $dow = scalar $day->day_of_week;
+        $dow = 0 if $dow == 7;
+
+        # Closed this day of the week
+        my $dbh = C4::Context->dbh;
+        $dbh->do(
+            q|
+            INSERT INTO repeatable_holidays (branchcode,weekday,day,month,title,description)
+            VALUES ( ?, ?, NULL, NULL, ?, '' )
+        |, undef, $library->branchcode, $dow, "TEST"
+        );
+
+        # Iterate 7 days
+        my $sth = $dbh->prepare(
+"UPDATE repeatable_holidays SET weekday = ? WHERE branchcode = ? AND title = 'TEST'"
+        );
+        for my $i ( 0 .. 6 ) {
+            my $calendar =
+              Koha::Calendar->new( branchcode => $library->branchcode, days_mode => 'Dayweek' );
+
+            my $npa;
+            eval {
+                local $SIG{ALRM} = sub { die "alarm\n" };    # NB: \n required
+                alarm 2;
+                $npa = $calendar->next_open_days( $day, 0 );
+                alarm 0;
+            };
+            if ($@) {
+                die unless $@ eq "alarm\n";    # propagate unexpected errors
+                # timed out
+                ok(0, "next_push_amt succeeded for ".$day->day_name()." weekday holiday");
+            }
+            else {
+                ok($npa, "next_push_amt succeeded for ".$day->day_name()." weekday holiday");
+            }
+
+            # Increment the date and holiday day
+            $day->add( days => 1 );
+            $dow++;
+            $dow = 0 if $dow == 7;
+            $sth->execute( $dow, $library->branchcode );
+        }
+    };
 };
 
 $schema->storage->txn_rollback();