X-Git-Url: http://koha-dev.rot13.org:8081/gitweb/?a=blobdiff_plain;f=t%2FCalendar.t;h=151e8b1874f573fabcb7b60e080d2960d85fa932;hb=9d8523ed2b88c8fbf18fc61835967ff555a5dc55;hp=8648968e000ec942eab1ad771eab969a1506e994;hpb=47a8c10d997097f70204bf234340e61521013d39;p=srvgit diff --git a/t/Calendar.t b/t/Calendar.t index 8648968e00..151e8b1874 100755 --- a/t/Calendar.t +++ b/t/Calendar.t @@ -1,35 +1,55 @@ -#!/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 . + +use Modern::Perl; + +use Test::More; +use Test::MockModule; -use strict; -use warnings; use DateTime; use DateTime::Duration; -use Test::More tests => 35; -use Test::MockModule; -use DBD::Mock; +use Koha::Caches; use Koha::DateUtils; -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'); -} +use Module::Load::Conditional qw/check_install/; -my $module_context = new Test::MockModule('C4::Context'); -$module_context->mock( - '_new_dbh', - sub { - my $dbh = DBI->connect( 'DBI:Mock:', '', '' ) - || die "Cannot create handle: $DBI::errstr\n"; - return $dbh; +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(); } ); # 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 = Test::MockModule->new('C4::Context'); $module_context->mock( 'preference', sub { @@ -37,55 +57,36 @@ $module_context->mock( } ); -SKIP: { - -skip "DBD::Mock is too old", 33 - unless $DBD::Mock::VERSION >= 1.45; - -my $holidays_session = DBD::Mock::Session->new('holidays_session' => ( - { # weekly holidays - statement => "SELECT weekday FROM repeatable_holidays WHERE branchcode = ? AND weekday IS NOT NULL", - results => [ - ['weekday'], - [0], # sundays - [6] # saturdays - ] - }, - { # day and month repeatable holidays - statement => "SELECT day, month FROM repeatable_holidays WHERE branchcode = ? AND weekday IS NULL", - results => [ - [ 'month', 'day' ], - [ 1, 1 ], # new year's day - [12,25] # christmas - ] - }, - { # exception holidays - statement => "SELECT day, month, year FROM special_holidays WHERE branchcode = ? AND isexception = 1", - results => [ - [ 'day', 'month', 'year' ], - [ 11, 11, 2012 ] # sunday exception - ] - }, - { # single holidays - statement => "SELECT day, month, year FROM special_holidays WHERE branchcode = ? AND isexception = 0", - results => [ - [ 'day', 'month', 'year' ], - [ 1, 6, 2011 ], # single holiday - [ 4, 7, 2012 ] - ] - } -)); +fixtures_ok [ + # weekly holidays + RepeatableHoliday => [ + [ qw( branchcode day month weekday title description) ], + [ 'MPL', undef, undef, 0, '', '' ], # sundays + [ 'MPL', undef, undef, 6, '', '' ],# saturdays + [ 'MPL', 1, 1, undef, '', ''], # new year's day + [ 'MPL', 25, 12, undef, '', ''], # chrismas + ], + # exception holidays + SpecialHoliday => [ + [qw( branchcode day month year title description isexception )], + [ '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::Caches->get_instance(); +$cache->clear_from_cache('MPL_holidays'); +$cache->clear_from_cache('CPL_holidays'); -# Initialize the global $dbh variable -my $dbh = C4::Context->dbh(); -# Apply the mock session -$dbh->{ mock_session } = $holidays_session; # 'MPL' branch is arbitrary, is not used at all but is needed for initialization my $cal = Koha::Calendar->new( branchcode => 'MPL' ); isa_ok( $cal, 'Koha::Calendar', 'Calendar class returned' ); - my $saturday = DateTime->new( year => 2012, month => 11, @@ -134,6 +135,17 @@ my $day_after_christmas = DateTime->new( day => 26 ); # for testing negative addDate +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)'); @@ -146,9 +158,10 @@ 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 my $later_dt = DateTime->new( # Monday year => 2012, @@ -164,7 +177,6 @@ my $day_after_christmas = DateTime->new( cmp_ok( $later_dt, 'eq', '2012-09-17T17:30:00', 'bz-8966 (2/2) Date should be the same after is_holiday' ); } - { # Bugzilla #8800 - is_holiday should use truncated date for 'contains' call my $single_holiday_time = DateTime->new( year => 2011, @@ -179,12 +191,12 @@ my $day_after_christmas = DateTime->new( 'bz-8800 is_holiday should truncate the date for holiday validation' ); } - my $one_day_dur = DateTime::Duration->new( days => 1 ); my $two_day_dur = DateTime::Duration->new( days => 2 ); my $seven_day_dur = DateTime::Duration->new( days => 7 ); - my $dt = dt_from_string( '2012-07-03','iso' ); + my $dt = dt_from_string( '2012-07-03','iso' ); #tuesday + my $test_dt = DateTime->new( # Monday year => 2012, month => 7, @@ -202,23 +214,11 @@ my $day_after_christmas = DateTime->new( time_zone => 'Europe/London', ); - { ## 'Datedue' tests - $module_context->unmock('preference'); - $module_context->mock( - 'preference', - sub { - return 'Datedue'; - } - ); - # rewind dbh session - $holidays_session->reset; - + $cal = Koha::Calendar->new( branchcode => 'MPL', days_mode => 'Datedue' ); - $cal = Koha::Calendar->new( branchcode => 'MPL' ); - - is($cal->addDate( $dt, $one_day_dur, 'days' ), + is($cal->addDate( $dt, $one_day_dur, 'days' ), # tuesday dt_from_string('2012-07-05','iso'), 'Single day add (Datedue, matches holiday, shift)' ); @@ -243,24 +243,11 @@ my $day_after_christmas = DateTime->new( cmp_ok( $cal->days_between( $later_dt, $test_dt )->in_units('days'), '==', 40, 'Test parameter order not relevant (Days)' ); - - } - { ## 'Calendar' tests' - $module_context->unmock('preference'); - $module_context->mock( - 'preference', - sub { - return 'Calendar'; - } - ); - # rewind dbh session - $holidays_session->reset; - - $cal = Koha::Calendar->new( branchcode => 'MPL' ); + $cal = Koha::Calendar->new( branchcode => 'MPL', days_mode => 'Calendar' ); $dt = dt_from_string('2012-07-03','iso'); @@ -287,17 +274,8 @@ my $day_after_christmas = DateTime->new( { ## 'Days' tests - $module_context->unmock('preference'); - $module_context->mock( - 'preference', - sub { - return 'Days'; - } - ); - # rewind dbh session - $holidays_session->reset; - $cal = Koha::Calendar->new( branchcode => 'MPL' ); + $cal = Koha::Calendar->new( branchcode => 'MPL', days_mode => 'Days' ); $dt = dt_from_string('2012-07-03','iso'); @@ -325,4 +303,22 @@ my $day_after_christmas = DateTime->new( } -} # End SKIP block +{ + $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'); +};