Bug 25898: Prohibit indirect object notation
[koha.git] / t / lib / Mocks.pm
1 package t::lib::Mocks;
2
3 use Modern::Perl;
4 use C4::Context;
5
6 use Test::MockModule;
7
8 my %configs;
9 sub mock_config {
10     my $context = Test::MockModule->new('C4::Context');
11     my ( $conf, $value ) = @_;
12     $configs{$conf} = $value;
13     $context->mock('config', sub {
14         my ( $self, $conf ) = @_;
15         if ( exists $configs{$conf} ) {
16             return $configs{$conf}
17         } else {
18             my $method = $context->original('config');
19             return $method->($self, $conf);
20         }
21     });
22 }
23
24 my %preferences;
25 sub mock_preference {
26     my ( $pref, $value ) = @_;
27
28     $preferences{lc($pref)} = $value;
29
30     my $context = Test::MockModule->new('C4::Context');
31     $context->mock('preference', sub {
32         my ( $self, $pref ) = @_;
33         $pref = lc($pref);
34         if ( exists $preferences{$pref} ) {
35             return $preferences{$pref}
36         } else {
37             my $method = $context->original('preference');
38             return $method->($self, $pref);
39         }
40     });
41 }
42
43 sub mock_userenv {
44     my ( $params ) = @_;
45
46     C4::Context->_new_userenv(42);
47
48     my $userenv;
49     if ( $params and my $patron = $params->{patron} ) {
50         $userenv = $patron->unblessed;
51         $userenv->{branchcode} = $params->{branchcode} || $patron->library->branchcode;
52         $userenv->{branchname} = $params->{branchname} || $patron->library->branchname;
53     }
54     my $usernum    = $params->{borrowernumber} || $userenv->{borrowernumber} || 51;
55     my $userid     = $params->{userid}         || $userenv->{userid}         || 'userid4tests';
56     my $cardnumber = $params->{cardnumber}     || $userenv->{cardnumber};
57     my $firstname  = $params->{firstname}      || $userenv->{firstname}      || 'firstname';
58     my $surname    = $params->{surname}        || $userenv->{surname}        || 'surname';
59     my $branchcode = $params->{branchcode}     || $userenv->{branchcode}     || 'Branch4T';
60     my $branchname   = $params->{branchname}   || $userenv->{branchname};
61     my $flags        = $params->{flags}        || $userenv->{flags}          || 0;
62     my $emailaddress = $params->{emailaddress} || $userenv->{emailaddress};
63     my ( $shibboleth );
64
65     C4::Context->set_userenv($usernum, $userid, $cardnumber, $firstname, $surname, $branchcode, $branchname, $flags, $emailaddress, $shibboleth );
66 }
67
68 1;