Bug 31051: DBRev 22.12.00.005
[koha-ffzg.git] / t / Calendar.t
index 7fabe3a..d560b68 100755 (executable)
@@ -1,37 +1,46 @@
-#!/usr/bin/env perl
+#!/usr/bin/perl
+
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
 
 use Modern::Perl;
+
+use Test::More;
+use Test::MockModule;
+
 use DateTime;
 use DateTime::Duration;
-use Test::More tests => 35;
-use Test::MockModule;
-use Koha::Cache;
-use Koha::DateUtils;
+use Koha::Caches;
+use Koha::DateUtils qw( dt_from_string );
 
-BEGIN {
-    use_ok('Koha::Calendar');
+use t::lib::Mocks;
 
-    # This was the only test C4 had
-    # Remove when no longer used
-    #use_ok('C4::Calendar'); # not used anymore?
-}
-use Test::DBIx::Class {
-    schema_class => 'Koha::Schema',
-    connect_info => ['dbi:SQLite:dbname=:memory:','',''],
-    connect_opts => { name_sep => '.', quote_char => '`', },
-    fixture_class => '::Populate',
-}, 'Biblio' ;
-
-sub fixtures {
-    my ( $data ) = @_;
-    fixtures_ok [
-        Biblio => [
-            [ qw/ biblionumber datecreated timestamp  / ],
-            @$data,
-        ],
-    ], 'add fixtures';
+use Module::Load::Conditional qw/check_install/;
+
+BEGIN {
+    if ( check_install( module => 'Test::DBIx::Class' ) ) {
+        plan tests => 40;
+    } else {
+        plan skip_all => "Need Test::DBIx::Class"
+    }
 }
 
+use_ok('Koha::Calendar');
+
+use Test::DBIx::Class;
+
 my $db = Test::MockModule->new('Koha::Database');
 $db->mock(
     _new_schema => sub { return Schema(); }
@@ -40,7 +49,7 @@ $db->mock(
 # We need to mock the C4::Context->preference method for
 # simplicity and re-usability of the session definition. Any
 # syspref fits for syspref-agnostic tests.
-my $module_context = new Test::MockModule('C4::Context');
+my $module_context = Test::MockModule->new('C4::Context');
 $module_context->mock(
     'preference',
     sub {
@@ -63,11 +72,15 @@ fixtures_ok [
         [ 'MPL', 11, 11, 2012, '', '', 1 ],    # sunday exception
         [ 'MPL', 1,  6,  2011, '', '', 0 ],
         [ 'MPL', 4,  7,  2012, '', '', 0 ],
+        [ 'CPL', 6,  8,  2012, '', '', 0 ],
+        [ 'MPL', 7,  7,  2012, '', '', 1 ], # holiday exception
+        [ 'MPL', 7,  7,  2012, '', '', 0 ], # holiday
       ],
 ], "add fixtures";
 
-my $cache = Koha::Cache->get_instance();
-$cache->clear_from_cache( 'single_holidays') ;
+my $cache = Koha::Caches->get_instance();
+$cache->clear_from_cache('MPL_holidays');
+$cache->clear_from_cache('CPL_holidays');
 
 # 'MPL' branch is arbitrary, is not used at all but is needed for initialization
 my $cal = Koha::Calendar->new( branchcode => 'MPL' );
@@ -120,7 +133,19 @@ my $day_after_christmas = DateTime->new(
     year    => 2012,
     month   => 12,
     day     => 26
-);  # for testing negative addDate
+);  # for testing negative addDuration
+
+my $holiday_for_another_branch = DateTime->new(
+    year => 2012,
+    month => 8,
+    day => 6, # This is a monday
+);
+
+my $holiday_excepted = DateTime->new(
+    year => 2012,
+    month => 7,
+    day => 7, # Both a holiday and exception
+);
 
 {   # Syspref-agnostic tests
     is ( $saturday->day_of_week, 6, '\'$saturday\' is actually a saturday (6th day of week)');
@@ -133,6 +158,8 @@ my $day_after_christmas = DateTime->new(
     is ( $cal->is_holiday($single_holiday), 1, 'Single holiday closed day test' );
     is ( $cal->is_holiday($notspecial), 0, 'Fixed single date that is not a holiday test' );
     is ( $cal->is_holiday($sunday_exception), 0, 'Exception holiday is not a closed day test' );
+    is ( $cal->is_holiday($holiday_for_another_branch), 0, 'Holiday defined for another branch should not be defined as an holiday' );
+    is ( $cal->is_holiday($holiday_excepted), 0, 'Holiday defined and excepted should not be a holiday' );
 }
 
 {   # Bugzilla #8966 - is_holiday truncates referenced date
@@ -189,33 +216,25 @@ my $day_after_christmas = DateTime->new(
 
 {    ## 'Datedue' tests
 
-    $module_context->unmock('preference');
-    $module_context->mock(
-        'preference',
-        sub {
-            return 'Datedue';
-        }
-    );
-
-    $cal = Koha::Calendar->new( branchcode => 'MPL' );
+    $cal = Koha::Calendar->new( branchcode => 'MPL', days_mode => 'Datedue' );
 
-    is($cal->addDate( $dt, $one_day_dur, 'days' ), # tuesday
+    is($cal->addDuration( $dt, $one_day_dur, 'days' ), # tuesday
         dt_from_string('2012-07-05','iso'),
         'Single day add (Datedue, matches holiday, shift)' );
 
-    is($cal->addDate( $dt, $two_day_dur, 'days' ),
+    is($cal->addDuration( $dt, $two_day_dur, 'days' ),
         dt_from_string('2012-07-05','iso'),
         'Two days add, skips holiday (Datedue)' );
 
-    cmp_ok($cal->addDate( $test_dt, $seven_day_dur, 'days' ), 'eq',
+    cmp_ok($cal->addDuration( $test_dt, $seven_day_dur, 'days' ), 'eq',
         '2012-07-30T11:53:00',
         'Add 7 days (Datedue)' );
 
-    is( $cal->addDate( $saturday, $one_day_dur, 'days' )->day_of_week, 1,
-        'addDate skips closed Sunday (Datedue)' );
+    is( $cal->addDuration( $saturday, $one_day_dur, 'days' )->day_of_week, 1,
+        'addDuration skips closed Sunday (Datedue)' );
 
-    is( $cal->addDate($day_after_christmas, -1, 'days')->ymd(), '2012-12-24',
-        'Negative call to addDate (Datedue)' );
+    is( $cal->addDuration($day_after_christmas, -1, 'days')->ymd(), '2012-12-24',
+        'Negative call to addDuration (Datedue)' );
 
     ## Note that the days_between API says closed days are not considered.
     ## This tests are here as an API test.
@@ -228,31 +247,23 @@ my $day_after_christmas = DateTime->new(
 
 {   ## 'Calendar' tests'
 
-    $module_context->unmock('preference');
-    $module_context->mock(
-        'preference',
-        sub {
-            return 'Calendar';
-        }
-    );
-
-    $cal = Koha::Calendar->new( branchcode => 'MPL' );
+    $cal = Koha::Calendar->new( branchcode => 'MPL', days_mode => 'Calendar' );
 
     $dt = dt_from_string('2012-07-03','iso');
 
-    is($cal->addDate( $dt, $one_day_dur, 'days' ),
+    is($cal->addDuration( $dt, $one_day_dur, 'days' ),
         dt_from_string('2012-07-05','iso'),
         'Single day add (Calendar)' );
 
-    cmp_ok($cal->addDate( $test_dt, $seven_day_dur, 'days' ), 'eq',
+    cmp_ok($cal->addDuration( $test_dt, $seven_day_dur, 'days' ), 'eq',
        '2012-08-01T11:53:00',
        'Add 7 days (Calendar)' );
 
-    is( $cal->addDate( $saturday, $one_day_dur, 'days' )->day_of_week, 1,
-            'addDate skips closed Sunday (Calendar)' );
+    is( $cal->addDuration( $saturday, $one_day_dur, 'days' )->day_of_week, 1,
+            'addDuration skips closed Sunday (Calendar)' );
 
-    is( $cal->addDate($day_after_christmas, -1, 'days')->ymd(), '2012-12-24',
-            'Negative call to addDate (Calendar)' );
+    is( $cal->addDuration($day_after_christmas, -1, 'days')->ymd(), '2012-12-24',
+            'Negative call to addDuration (Calendar)' );
 
     cmp_ok( $cal->days_between( $test_dt, $later_dt )->in_units('days'),
                 '==', 40, 'days_between calculates correctly (Calendar)' );
@@ -263,31 +274,24 @@ my $day_after_christmas = DateTime->new(
 
 
 {   ## 'Days' tests
-    $module_context->unmock('preference');
-    $module_context->mock(
-        'preference',
-        sub {
-            return 'Days';
-        }
-    );
 
-    $cal = Koha::Calendar->new( branchcode => 'MPL' );
+    $cal = Koha::Calendar->new( branchcode => 'MPL', days_mode => 'Days' );
 
     $dt = dt_from_string('2012-07-03','iso');
 
-    is($cal->addDate( $dt, $one_day_dur, 'days' ),
+    is($cal->addDuration( $dt, $one_day_dur, 'days' ),
         dt_from_string('2012-07-04','iso'),
         'Single day add (Days)' );
 
-    cmp_ok($cal->addDate( $test_dt, $seven_day_dur, 'days' ),'eq',
+    cmp_ok($cal->addDuration( $test_dt, $seven_day_dur, 'days' ),'eq',
         '2012-07-30T11:53:00',
         'Add 7 days (Days)' );
 
-    is( $cal->addDate( $saturday, $one_day_dur, 'days' )->day_of_week, 7,
-        'addDate doesn\'t skip closed Sunday (Days)' );
+    is( $cal->addDuration( $saturday, $one_day_dur, 'days' )->day_of_week, 7,
+        'addDuration doesn\'t skip closed Sunday (Days)' );
 
-    is( $cal->addDate($day_after_christmas, -1, 'days')->ymd(), '2012-12-25',
-        'Negative call to addDate (Days)' );
+    is( $cal->addDuration($day_after_christmas, -1, 'days')->ymd(), '2012-12-25',
+        'Negative call to addDuration (Days)' );
 
     ## Note that the days_between API says closed days are not considered.
     ## This tests are here as an API test.
@@ -298,3 +302,23 @@ my $day_after_christmas = DateTime->new(
                 '==', 40, 'Test parameter order not relevant (Days)' );
 
 }
+
+{
+    $cal = Koha::Calendar->new( branchcode => 'CPL' );
+    is ( $cal->is_holiday($single_holiday), 0, 'Single holiday for MPL, not CPL' );
+    is ( $cal->is_holiday($holiday_for_another_branch), 1, 'Holiday defined for CPL should be defined as an holiday' );
+}
+
+subtest 'days_mode parameter' => sub {
+    plan tests => 1;
+
+    t::lib::Mocks::mock_preference('useDaysMode', 'Days');
+
+    $cal = Koha::Calendar->new( branchcode => 'CPL', days_mode => 'Calendar' );
+    is( $cal->{days_mode}, 'Calendar', q|If set, days_mode is correctly set|);
+};
+
+END {
+    $cache->clear_from_cache('MPL_holidays');
+    $cache->clear_from_cache('CPL_holidays');
+};