Bug 24159: Adjust tests
[koha.git] / t / Circulation / AgeRestrictionMarkers.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Copyright 2015 Koha Development Team
6 # Copyright (C) 2015  Mark Tompsett (Time Zone Shifts)
7 #
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21 use Modern::Perl;
22
23 use DateTime;
24 use Koha::DateUtils;
25 use Test::More tests => 8;
26 use Test::Warn;
27
28 use t::lib::Mocks;
29
30 use C4::Circulation;
31
32 t::lib::Mocks::mock_preference( 'AgeRestrictionMarker', 'FSK|PEGI|Age|K' );
33
34 is ( C4::Circulation::GetAgeRestriction('FSK 16'), '16', 'FSK 16 returns 16' );
35 is ( C4::Circulation::GetAgeRestriction('PEGI 16'), '16', 'PEGI 16 returns 16' );
36 is ( C4::Circulation::GetAgeRestriction('PEGI16'), '16', 'PEGI16 returns 16' );
37 is ( C4::Circulation::GetAgeRestriction('Age 16'), '16', 'Age 16 returns 16' );
38 is ( C4::Circulation::GetAgeRestriction('K16'), '16', 'K16 returns 16' );
39
40 subtest 'Patron tests - 15 years old' => sub {
41     plan tests => 5;
42     ##Testing age restriction for a borrower.
43     my $now = dt_from_string();
44     my $borrower = { dateofbirth => $now->add( years => -15 )->strftime("%Y-%m-%d") };
45     TestPatron($borrower,0);
46 };
47
48 subtest 'Patron tests - 15 years old (Time Zone shifts)' => sub {
49     my $CheckTimeFake = eval { require Time::Fake; 1; } || 0;
50     SKIP: {
51         skip "Install Time::Fake to regression test for Bug 14362.", 115 if $CheckTimeFake!=1;
52         # 115 regression tests = 5 tests (see TestPatron) for 23 timezones.
53         plan tests => 115;
54         my $offset = 1;
55         # <24 hours in a day.
56         while ($offset<24) {
57             Time::Fake->offset("+${offset}h");
58
59             ##Testing age restriction for a borrower.
60             my $now = dt_from_string();
61             my $borrower = { dateofbirth => $now->add( years => -15 )->strftime("%Y-%m-%d") };
62             TestPatron($borrower,$offset);
63
64             $offset++;
65         }
66     }
67 };
68
69 subtest 'No age restriction' => sub {
70     plan tests => 1;
71
72     warning_is {
73         C4::Circulation::GetAgeRestriction();
74     }
75     undef, "No warning if GetAgeRestriction is called without restriction";
76
77 };
78
79 # The Patron tests
80 sub TestPatron {
81     my ($borrower,$offset) = @_;
82
83     my ($restriction_age, $daysToAgeRestriction) = C4::Circulation::GetAgeRestriction('FSK 16', $borrower);
84     is ( ($daysToAgeRestriction > 0), 1, "FSK 16 blocked for a 15 year old - $offset hours" );
85     ($restriction_age, $daysToAgeRestriction) = C4::Circulation::GetAgeRestriction('PEGI 15', $borrower);
86     is ( ($daysToAgeRestriction <= 0), 1, "PEGI 15 allowed for a 15 year old - $offset hours" );
87     ($restriction_age, $daysToAgeRestriction) = C4::Circulation::GetAgeRestriction('PEGI14', $borrower);
88     is ( ($daysToAgeRestriction <= 0), 1, "PEGI14 allowed for a 15 year old - $offset hours" );
89     ($restriction_age, $daysToAgeRestriction) = C4::Circulation::GetAgeRestriction('Age 10', $borrower);
90     is ( ($daysToAgeRestriction <= 0), 1, "Age 10 allowed for a 15 year old - $offset hours" );
91     ($restriction_age, $daysToAgeRestriction) = C4::Circulation::GetAgeRestriction('K18', $borrower);
92     is ( ($daysToAgeRestriction > 0), 1, "K18 blocked for a 15 year old - $offset hours" );
93     return;
94 }