Bug 12532: Fix test
[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 NOTE: We are only mocking config entries here, so no entries from other
43 sections of koha-conf.xml. Bug 33718 fixed the section parameter of
44 mocked Koha::Config->get calls for other sections (not cached).
45
46 =cut
47
48 sub mock_config {
49     my ( $config_entry, $value ) = @_;
50     my $koha_config = Test::MockModule->new('Koha::Config');
51     $configs{$config_entry} = $value;
52     $koha_config->mock('get', sub {
53         my ( $self, $key, $section ) = @_;
54         $section ||= 'config';
55         if( $section eq 'config' && exists $configs{$key} ) {
56             return $configs{$key};
57         }
58         my $method = $koha_config->original('get');
59         return $method->( $self, $key, $section );
60     });
61 }
62
63 my %preferences;
64
65 =head3 mock_preference
66
67     t::lib::Mocks::mock_preference( $preference, $value );
68
69 Mock the I<$preference> with the specified I<value>.
70
71 =cut
72
73 sub mock_preference {
74     my ( $pref, $value ) = @_;
75
76     $preferences{lc($pref)} = $value;
77
78     my $context = Test::MockModule->new('C4::Context');
79     $context->mock('preference', sub {
80         my ( $self, $pref ) = @_;
81         $pref = lc($pref);
82         if ( exists $preferences{$pref} ) {
83             return $preferences{$pref}
84         } else {
85             my $method = $context->original('preference');
86             return $method->($self, $pref);
87         }
88     });
89 }
90
91 =head3 mock_userenv
92
93     t::lib::Mocks::mock_userenv(
94         {
95           [ patron         => $patron,
96             borrowernumber => $borrowernumber,
97             userid         => $userid,
98             cardnumber     => $cardnumber,
99             firstname      => $firstname,
100             surname        => $surname,
101             branchcode     => $branchcode,
102             branchname     => $branchname,
103             flags          => $flags,
104             emailaddress   => $emailaddress,
105             desk_id        => $desk_id,
106             desk_name      => $desk_name,
107             register_id    => $register_id,
108             register_name  => $register_name, ]
109         }
110     );
111
112 Mock userenv in the context of tests. A I<patron> param is usually expected, but
113 some other session attributes might be passed as well, that will override the patron's.
114
115 Also, some sane defaults are set if no parameters are passed.
116
117 =cut
118
119 sub mock_userenv {
120     my ( $params ) = @_;
121
122     C4::Context->_new_userenv(42);
123
124     my $userenv;
125     if ( $params and my $patron = $params->{patron} ) {
126         $userenv = $patron->unblessed;
127         $userenv->{branchcode} = $params->{branchcode} || $patron->library->branchcode;
128         $userenv->{branchname} = $params->{branchname} || $patron->library->branchname;
129     }
130     my $usernum    = $params->{borrowernumber} || $userenv->{borrowernumber} || 51;
131     my $userid     = $params->{userid}         || $userenv->{userid}         || 'userid4tests';
132     my $cardnumber = $params->{cardnumber}     || $userenv->{cardnumber};
133     my $firstname  = $params->{firstname}      || $userenv->{firstname}      || 'firstname';
134     my $surname    = $params->{surname}        || $userenv->{surname}        || 'surname';
135     my $branchcode = $params->{branchcode}     || $userenv->{branchcode}     || 'Branch4T';
136     my $branchname   = $params->{branchname}   || $userenv->{branchname};
137     my $flags        = $params->{flags}        || $userenv->{flags}          || 0;
138     my $emailaddress = $params->{emailaddress} || $userenv->{email};
139     my $desk_id       = $params->{desk_id}       || $userenv->{desk_id};
140     my $desk_name     = $params->{desk_name}     || $userenv->{desk_name};
141     my $register_id   = $params->{register_id}   || $userenv->{register_id};
142     my $register_name = $params->{register_name} || $userenv->{register_name};
143     my ( $shibboleth );
144
145     C4::Context->set_userenv(
146         $usernum,      $userid,     $cardnumber, $firstname,
147         $surname,      $branchcode, $branchname, $flags,
148         $emailaddress, $shibboleth, $desk_id,    $desk_name,
149         $register_id,  $register_name
150     );
151 }
152
153 1;