Bug 27173: Add tests
[koha.git] / t / lib / Mocks.pm
1 package t::lib::Mocks;
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 use C4::Context;
20
21 use Test::MockModule;
22 use Test::MockObject;
23
24 =head1 NAME
25
26 t::lib::Mocks - A library to mock things for testing
27
28 =head1 API
29
30 =head2 Methods
31
32 =cut
33
34 my %configs;
35
36 =head3 mock_config
37
38     t::lib::Mocks::mock_config( $config_entry, $value );
39
40 Mock the configuration I<$config_entry> with the specified I<$value>.
41
42 =cut
43
44 sub mock_config {
45     my $koha_config = Test::MockModule->new('Koha::Config');
46     my ( $conf, $value ) = @_;
47     $configs{$conf} = $value;
48     $koha_config->mock('get', sub {
49         my ( $self, $conf ) = @_;
50         if ( exists $configs{$conf} ) {
51             return $configs{$conf}
52         } else {
53             my $method = $koha_config->original('get');
54             return $method->($self, $conf);
55         }
56     });
57 }
58
59 my %preferences;
60
61 =head3 mock_preference
62
63     t::lib::Mocks::mock_preference( $preference, $value );
64
65 Mock the I<$preference> with the specified I<value>.
66
67 =cut
68
69 sub mock_preference {
70     my ( $pref, $value ) = @_;
71
72     $preferences{lc($pref)} = $value;
73
74     my $context = Test::MockModule->new('C4::Context');
75     $context->mock('preference', sub {
76         my ( $self, $pref ) = @_;
77         $pref = lc($pref);
78         if ( exists $preferences{$pref} ) {
79             return $preferences{$pref}
80         } else {
81             my $method = $context->original('preference');
82             return $method->($self, $pref);
83         }
84     });
85 }
86
87 =head3 mock_userenv
88
89     t::lib::Mocks::mock_userenv(
90         {
91           [ patron         => $patron,
92             borrowernumber => $borrowernumber,
93             userid         => $userid,
94             cardnumber     => $cardnumber,
95             firstname      => $firstname,
96             surname        => $surname,
97             branchcode     => $branchcode,
98             branchname     => $branchname,
99             flags          => $flags,
100             emailaddress   => $emailaddress,
101             desk_id        => $desk_id,
102             desk_name      => $desk_name,
103             register_id    => $register_id,
104             register_name  => $register_name, ]
105         }
106     );
107
108 Mock userenv in the context of tests. A I<patron> param is usually expected, but
109 some other session attributes might be passed as well, that will override the patron's.
110
111 Also, some sane defaults are set if no parameters are passed.
112
113 =cut
114
115 sub mock_userenv {
116     my ( $params ) = @_;
117
118     C4::Context->_new_userenv(42);
119
120     my $userenv;
121     if ( $params and my $patron = $params->{patron} ) {
122         $userenv = $patron->unblessed;
123         $userenv->{branchcode} = $params->{branchcode} || $patron->library->branchcode;
124         $userenv->{branchname} = $params->{branchname} || $patron->library->branchname;
125     }
126     my $usernum    = $params->{borrowernumber} || $userenv->{borrowernumber} || 51;
127     my $userid     = $params->{userid}         || $userenv->{userid}         || 'userid4tests';
128     my $cardnumber = $params->{cardnumber}     || $userenv->{cardnumber};
129     my $firstname  = $params->{firstname}      || $userenv->{firstname}      || 'firstname';
130     my $surname    = $params->{surname}        || $userenv->{surname}        || 'surname';
131     my $branchcode = $params->{branchcode}     || $userenv->{branchcode}     || 'Branch4T';
132     my $branchname   = $params->{branchname}   || $userenv->{branchname};
133     my $flags        = $params->{flags}        || $userenv->{flags}          || 0;
134     my $emailaddress = $params->{emailaddress} || $userenv->{emailaddress};
135     my $desk_id       = $params->{desk_id}       || $userenv->{desk_id};
136     my $desk_name     = $params->{desk_name}     || $userenv->{desk_name};
137     my $register_id   = $params->{register_id}   || $userenv->{register_id};
138     my $register_name = $params->{register_name} || $userenv->{register_name};
139     my ( $shibboleth );
140
141     C4::Context->set_userenv(
142         $usernum,      $userid,     $cardnumber, $firstname,
143         $surname,      $branchcode, $branchname, $flags,
144         $emailaddress, $shibboleth, $desk_id,    $desk_name,
145         $register_id,  $register_name
146     );
147 }
148
149 1;