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>.
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}
53 my $method = $koha_config->original('get');
54 return $method->($self, $conf);
61 =head3 mock_preference
63 t::lib::Mocks::mock_preference( $preference, $value );
65 Mock the I<$preference> with the specified I<value>.
70 my ( $pref, $value ) = @_;
72 $preferences{lc($pref)} = $value;
74 my $context = Test::MockModule->new('C4::Context');
75 $context->mock('preference', sub {
76 my ( $self, $pref ) = @_;
78 if ( exists $preferences{$pref} ) {
79 return $preferences{$pref}
81 my $method = $context->original('preference');
82 return $method->($self, $pref);
89 t::lib::Mocks::mock_userenv(
92 borrowernumber => $borrowernumber,
94 cardnumber => $cardnumber,
95 firstname => $firstname,
97 branchcode => $branchcode,
98 branchname => $branchname,
100 emailaddress => $emailaddress,
102 desk_name => $desk_name,
103 register_id => $register_id,
104 register_name => $register_name, ]
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.
111 Also, some sane defaults are set if no parameters are passed.
118 C4::Context->_new_userenv(42);
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;
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};
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