View | Details | Raw Unified | Return to bug 14778
Collapse All | Expand All

(-)a/t/Calendar.t (-77 / +39 lines)
Lines 1-12 Link Here
1
#!/usr/bin/env perl
1
#!/usr/bin/env perl
2
2
3
use strict;
3
use Modern::Perl;
4
use warnings;
5
use DateTime;
4
use DateTime;
6
use DateTime::Duration;
5
use DateTime::Duration;
7
use Test::More tests => 34;
6
use Test::More tests => 35;
8
use Test::MockModule;
7
use Test::MockModule;
9
use DBD::Mock;
10
use Koha::Cache;
8
use Koha::Cache;
11
use Koha::DateUtils;
9
use Koha::DateUtils;
12
10
Lines 17-36 BEGIN { Link Here
17
    # Remove when no longer used
15
    # Remove when no longer used
18
    #use_ok('C4::Calendar'); # not used anymore?
16
    #use_ok('C4::Calendar'); # not used anymore?
19
}
17
}
18
use Test::DBIx::Class {
19
    schema_class => 'Koha::Schema',
20
    connect_info => ['dbi:SQLite:dbname=:memory:','',''],
21
    connect_opts => { name_sep => '.', quote_char => '`', },
22
    fixture_class => '::Populate',
23
}, 'Biblio' ;
24
25
sub fixtures {
26
    my ( $data ) = @_;
27
    fixtures_ok [
28
        Biblio => [
29
            [ qw/ biblionumber datecreated timestamp  / ],
30
            @$data,
31
        ],
32
    ], 'add fixtures';
33
}
20
34
21
my $module_context = new Test::MockModule('C4::Context');
35
my $db = Test::MockModule->new('Koha::Database');
22
$module_context->mock(
36
$db->mock(
23
    '_new_dbh',
37
    _new_schema => sub { return Schema(); }
24
    sub {
25
        my $dbh = DBI->connect( 'DBI:Mock:', '', '' )
26
          || die "Cannot create handle: $DBI::errstr\n";
27
        return $dbh;
28
    }
29
);
38
);
30
39
31
# We need to mock the C4::Context->preference method for
40
# We need to mock the C4::Context->preference method for
32
# simplicity and re-usability of the session definition. Any
41
# simplicity and re-usability of the session definition. Any
33
# syspref fits for syspref-agnostic tests.
42
# syspref fits for syspref-agnostic tests.
43
my $module_context = new Test::MockModule('C4::Context');
34
$module_context->mock(
44
$module_context->mock(
35
    'preference',
45
    'preference',
36
    sub {
46
    sub {
Lines 38-107 $module_context->mock( Link Here
38
    }
48
    }
39
);
49
);
40
50
41
SKIP: {
51
fixtures_ok [
42
52
    # weekly holidays
43
skip "DBD::Mock is too old", 33
53
    RepeatableHoliday => [
44
  unless $DBD::Mock::VERSION >= 1.45;
54
        [ qw( branchcode day month weekday title description) ],
45
55
        [ 'MPL', undef, undef, 0, '', '' ], # sundays
46
my $holidays_session = DBD::Mock::Session->new('holidays_session' => (
56
        [ 'MPL', undef, undef, 6, '', '' ],# saturdays
47
    { # weekly holidays
57
        [ 'MPL', 1, 1, undef, '', ''], # new year's day
48
        statement => "SELECT weekday FROM repeatable_holidays WHERE branchcode = ? AND weekday IS NOT NULL",
58
        [ 'MPL', 25, 12, undef, '', ''], # chrismas
49
        results   => [
59
    ],
50
                        ['weekday'],
60
    # exception holidays
51
                        [0],    # sundays
61
    SpecialHoliday => [
52
                        [6]     # saturdays
62
        [qw( branchcode day month year title description isexception )],
53
                     ]
63
        [ 'MPL', 11, 11, 2012, '', '', 1 ],    # sunday exception
54
    },
64
        [ 'MPL', 1,  6,  2011, '', '', 0 ],
55
    { # day and month repeatable holidays
65
        [ 'MPL', 4,  7,  2012, '', '', 0 ],
56
        statement => "SELECT day, month FROM repeatable_holidays WHERE branchcode = ? AND weekday IS NULL",
66
      ],
57
        results   => [
67
], "add fixtures";
58
                        [ 'month', 'day' ],
59
                        [ 1, 1 ],   # new year's day
60
                        [12,25]     # christmas
61
                     ]
62
    },
63
    { # exception holidays
64
        statement => "SELECT day, month, year FROM special_holidays WHERE branchcode = ? AND isexception = 1",
65
        results   => [
66
                        [ 'day', 'month', 'year' ],
67
                        [ 11, 11, 2012 ] # sunday exception
68
                     ]
69
    },
70
71
    { # single holidays1
72
        statement => "SELECT distinct(branchcode) FROM special_holidays",
73
        results   => [
74
                        [ 'branchcode' ],
75
                        [ 'MPL']
76
                     ]
77
    },
78
79
    { # single holidays2
80
        statement => "SELECT day, month, year FROM special_holidays WHERE branchcode = ? AND isexception = 0",
81
        results   => [
82
                        [ 'day', 'month', 'year' ],
83
                        [ 1, 6, 2011 ],  # single holiday
84
                        [ 4, 7, 2012 ]
85
                     ]
86
    },
87
));
88
89
# Initialize the global $dbh variable
90
my $dbh = C4::Context->dbh();
91
# Apply the mock session
92
$dbh->{ mock_session } = $holidays_session;
93
94
68
95
my $cache = Koha::Cache->get_instance();
69
my $cache = Koha::Cache->get_instance();
96
$cache->clear_from_cache( 'single_holidays') ;
70
$cache->clear_from_cache( 'single_holidays') ;
97
71
98
99
# 'MPL' branch is arbitrary, is not used at all but is needed for initialization
72
# 'MPL' branch is arbitrary, is not used at all but is needed for initialization
100
my $cal = Koha::Calendar->new( branchcode => 'MPL' );
73
my $cal = Koha::Calendar->new( branchcode => 'MPL' );
101
74
102
isa_ok( $cal, 'Koha::Calendar', 'Calendar class returned' );
75
isa_ok( $cal, 'Koha::Calendar', 'Calendar class returned' );
103
76
104
105
my $saturday = DateTime->new(
77
my $saturday = DateTime->new(
106
    year      => 2012,
78
    year      => 2012,
107
    month     => 11,
79
    month     => 11,
Lines 224-232 my $day_after_christmas = DateTime->new( Link Here
224
            return 'Datedue';
196
            return 'Datedue';
225
        }
197
        }
226
    );
198
    );
227
    # rewind dbh session
228
    $holidays_session->reset;
229
230
199
231
    $cal = Koha::Calendar->new( branchcode => 'MPL' );
200
    $cal = Koha::Calendar->new( branchcode => 'MPL' );
232
201
Lines 266-273 my $day_after_christmas = DateTime->new( Link Here
266
            return 'Calendar';
235
            return 'Calendar';
267
        }
236
        }
268
    );
237
    );
269
    # rewind dbh session
270
    $holidays_session->reset;
271
238
272
    $cal = Koha::Calendar->new( branchcode => 'MPL' );
239
    $cal = Koha::Calendar->new( branchcode => 'MPL' );
273
240
Lines 303-310 my $day_after_christmas = DateTime->new( Link Here
303
            return 'Days';
270
            return 'Days';
304
        }
271
        }
305
    );
272
    );
306
    # rewind dbh session
307
    $holidays_session->reset;
308
273
309
    $cal = Koha::Calendar->new( branchcode => 'MPL' );
274
    $cal = Koha::Calendar->new( branchcode => 'MPL' );
310
275
Lines 333-337 my $day_after_christmas = DateTime->new( Link Here
333
                '==', 40, 'Test parameter order not relevant (Days)' );
298
                '==', 40, 'Test parameter order not relevant (Days)' );
334
299
335
}
300
}
336
337
} # End SKIP block
338
- 

Return to bug 14778