3 # This file is part of Koha.
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.
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.
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>.
26 t::lib::Mocks - A library to mock things for testing
38 t::lib::Mocks::mock_config( $config_entry, $value );
40 Mock the configuration I<$config_entry> with the specified I<$value>.
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).
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};
58 my $method = $koha_config->original('get');
59 return $method->( $self, $key, $section );
65 =head3 mock_preference
67 t::lib::Mocks::mock_preference( $preference, $value );
69 Mock the I<$preference> with the specified I<value>.
74 my ( $pref, $value ) = @_;
76 $preferences{lc($pref)} = $value;
78 my $context = Test::MockModule->new('C4::Context');
79 $context->mock('preference', sub {
80 my ( $self, $pref ) = @_;
82 if ( exists $preferences{$pref} ) {
83 return $preferences{$pref}
85 my $method = $context->original('preference');
86 return $method->($self, $pref);
93 t::lib::Mocks::mock_userenv(
96 borrowernumber => $borrowernumber,
98 cardnumber => $cardnumber,
99 firstname => $firstname,
101 branchcode => $branchcode,
102 branchname => $branchname,
104 emailaddress => $emailaddress,
106 desk_name => $desk_name,
107 register_id => $register_id,
108 register_name => $register_name, ]
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.
115 Also, some sane defaults are set if no parameters are passed.
122 C4::Context->_new_userenv(42);
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;
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};
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