Bug 31378: (follow-up) Fix t/Koha/Auth/Permissions.t
[koha.git] / t / Koha_Template_Plugin_Koha.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use Test::More tests => 4;
21 use Test::MockModule;
22 use t::lib::Mocks;
23
24 use String::Random;
25
26 # Test the plugin is usable
27 use_ok( 'Koha::Template::Plugin::Koha' );
28 ok( my $cache = Koha::Template::Plugin::Koha->new() );
29
30 subtest "Koha::Template::Plugin::Koha::Version tests" => sub {
31
32     plan tests => 2;
33
34     # Variables for mocking Koha::version()
35     my $major;
36     my $minor;
37     my $maintenance;
38     my $development;
39
40     # Mock Koha::version()
41     my $koha = Test::MockModule->new('Koha');
42     $koha->mock( 'version', sub {
43         return "$major.$minor.$maintenance.$development";
44     });
45
46     my $rnd = String::Random->new;
47     # development version test
48     $major       = $rnd->randregex('\d');
49     $minor       = $rnd->randregex('\d\d');
50     $maintenance = $rnd->randregex('\d\d');
51     $development = $rnd->randregex('\d\d\d');
52     my $version = "$major.$minor.$maintenance.$development";
53     my $res = Koha::Template::Plugin::Koha::Version( $version );
54     is_deeply( $res, {
55         major       => $major,
56         minor       => $minor,
57         release     => $major . "." . $minor,
58         maintenance => $major . "." . $minor . "." . $maintenance,
59         development => $development
60     }, "Correct development version");
61
62
63     # maintenance release test
64     $major       = $rnd->randregex('\d');
65     $minor       = $rnd->randregex('\d\d');
66     $maintenance = $rnd->randregex('\d\d');
67     $development = "000";
68     $version = "$major.$minor.$maintenance.$development";
69     $res = Koha::Template::Plugin::Koha::Version( $version );
70     is_deeply( $res, {
71         major       => $major,
72         minor       => $minor,
73         release     => $major . "." . $minor,
74         maintenance => $major . "." . $minor . "." . $maintenance,
75         development => undef
76     }, "Correct maintenance version");
77
78 };
79
80 subtest "Koha::Template::Plugin::Koha::CSVDelimiter tests" => sub {
81
82     plan tests => 8;
83
84     my $plugin = Koha::Template::Plugin::Koha->new();
85
86     t::lib::Mocks::mock_preference('CSVDelimiter', '');
87     is($plugin->CSVDelimiter(), ',', "CSVDelimiter() returns comma when preference is empty string");
88
89     t::lib::Mocks::mock_preference('CSVDelimiter', undef);
90     is($plugin->CSVDelimiter(), ',', "CSVDelimiter() returns comma when preference is undefined");
91
92     t::lib::Mocks::mock_preference('CSVDelimiter', ';');
93     is($plugin->CSVDelimiter(), ';', "CSVDelimiter() returns preference value when preference is not tabulation");
94
95     t::lib::Mocks::mock_preference('CSVDelimiter', 'tabulation');
96     is($plugin->CSVDelimiter(), "\t", "CSVDelimiter() returns \\t when preference is tabulation");
97
98     t::lib::Mocks::mock_preference('CSVDelimiter', '#');
99     is($plugin->CSVDelimiter(undef), '#', "CSVDelimiter(arg) returns preference value when arg is undefined");
100     is($plugin->CSVDelimiter(''), '#', "CSVDelimiter(arg) returns preference value when arg is empty string");
101     is($plugin->CSVDelimiter(','), ',', "CSVDelimiter(arg) returns arg value when arg is not tabulation");
102     is($plugin->CSVDelimiter('tabulation'), "\t", "CSVDelimiter(arg) returns \\t value when arg is tabulation");
103 };