View | Details | Raw Unified | Return to bug 21817
Collapse All | Expand All

(-)a/t/Budgets/CanUserModifyBudget.t (-13 / +5 lines)
Lines 4-14 use Modern::Perl; Link Here
4
use Test::More tests => 134;
4
use Test::More tests => 134;
5
5
6
use C4::Budgets;
6
use C4::Budgets;
7
use t::lib::Mocks;
7
8
8
# Avoid "redefined subroutine" warnings
9
# Avoid "redefined subroutine" warnings
9
local $SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /redefined/ };
10
local $SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /redefined/ };
10
*C4::Budgets::GetBudgetUsers = \&Mock_GetBudgetUsers;
11
*C4::Budgets::GetBudgetUsers = \&Mock_GetBudgetUsers;
11
*C4::Context::userenv = \&Mock_userenv;
12
12
13
my %budgetusers = (
13
my %budgetusers = (
14
    1 => [],
14
    1 => [],
Lines 133-140 my $budget16 = { Link Here
133
    budget_branchcode => 'B1',
133
    budget_branchcode => 'B1',
134
};
134
};
135
135
136
my $userenv = {};
137
138
ok( !CanUserModifyBudget( 0, undef, {} ), "CanUserModifyBudget evaluates to false if DB user is passed" );
136
ok( !CanUserModifyBudget( 0, undef, {} ), "CanUserModifyBudget evaluates to false if DB user is passed" );
139
137
140
ok (CanUserModifyBudget($borrower1, $budget1, {superlibrarian => 1}));
138
ok (CanUserModifyBudget($borrower1, $budget1, {superlibrarian => 1}));
Lines 150-156 ok (!CanUserModifyBudget($borrower1, $budget1, {acquisition => 0})); Link Here
150
148
151
my $flags = {acquisition => {budget_modify => 1}};
149
my $flags = {acquisition => {budget_modify => 1}};
152
150
153
$userenv->{branch} = 'B1';
151
t::lib::Mocks::mock_userenv({ branchcode => 'B1' });
154
152
155
# Restriction is 'none'
153
# Restriction is 'none'
156
ok (CanUserModifyBudget($borrower1, $budget1, $flags));
154
ok (CanUserModifyBudget($borrower1, $budget1, $flags));
Lines 192-199 ok (!CanUserModifyBudget($borrower2, $budget14, $flags)); # No owner and user li Link Here
192
ok (CanUserModifyBudget($borrower2, $budget15, $flags));
190
ok (CanUserModifyBudget($borrower2, $budget15, $flags));
193
ok (!CanUserModifyBudget($borrower2, $budget16, $flags));
191
ok (!CanUserModifyBudget($borrower2, $budget16, $flags));
194
192
195
193
t::lib::Mocks::mock_userenv({ branchcode => 'B2' });
196
$userenv->{branch} = 'B2';
197
194
198
# Restriction is 'none'
195
# Restriction is 'none'
199
ok (CanUserModifyBudget($borrower1, $budget1, $flags));
196
ok (CanUserModifyBudget($borrower1, $budget1, $flags));
Lines 240-246 ok (!CanUserModifyBudget($borrower2, $budget16, $flags)); Link Here
240
# All tests should failed
237
# All tests should failed
241
$flags = {acquisition => {order_manage => 1}};
238
$flags = {acquisition => {order_manage => 1}};
242
239
243
$userenv->{branch} = 'B1';
240
t::lib::Mocks::mock_userenv({ branchcode => 'B1' });
244
241
245
# Restriction is 'none'
242
# Restriction is 'none'
246
ok (!CanUserModifyBudget($borrower1, $budget1, $flags));
243
ok (!CanUserModifyBudget($borrower1, $budget1, $flags));
Lines 283-289 ok (!CanUserModifyBudget($borrower2, $budget15, $flags)); Link Here
283
ok (!CanUserModifyBudget($borrower2, $budget16, $flags));
280
ok (!CanUserModifyBudget($borrower2, $budget16, $flags));
284
281
285
282
286
$userenv->{branch} = 'B2';
283
t::lib::Mocks::mock_userenv({ branchcode => 'B2' });
287
284
288
# Restriction is 'none'
285
# Restriction is 'none'
289
ok (!CanUserModifyBudget($borrower1, $budget1, $flags));
286
ok (!CanUserModifyBudget($borrower1, $budget1, $flags));
Lines 334-341 sub Mock_GetBudgetUsers { Link Here
334
331
335
    return @{ $budgetusers{$budget_id} };
332
    return @{ $budgetusers{$budget_id} };
336
}
333
}
337
338
# C4::Context->userenv
339
sub Mock_userenv {
340
    return $userenv;
341
}
(-)a/t/Budgets/CanUserUseBudget.t (-10 / +3 lines)
Lines 4-15 use Modern::Perl; Link Here
4
use Test::More tests => 70;
4
use Test::More tests => 70;
5
5
6
use C4::Budgets;
6
use C4::Budgets;
7
use t::lib::Mocks;
7
8
8
# Avoid "redefined subroutine" warnings
9
# Avoid "redefined subroutine" warnings
9
local $SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /redefined/ };
10
local $SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /redefined/ };
10
11
11
*C4::Budgets::GetBudgetUsers = \&Mock_GetBudgetUsers;
12
*C4::Budgets::GetBudgetUsers = \&Mock_GetBudgetUsers;
12
*C4::Context::userenv = \&Mock_userenv;
13
13
14
my %budgetusers = (
14
my %budgetusers = (
15
    1 => [],
15
    1 => [],
Lines 134-141 my $budget16 = { Link Here
134
    budget_branchcode => 'B1',
134
    budget_branchcode => 'B1',
135
};
135
};
136
136
137
my $userenv = {};
138
139
ok( !CanUserUseBudget( 0, undef, {} ), "CanUserUseBudget evaluates to false if DB user is passed" );
137
ok( !CanUserUseBudget( 0, undef, {} ), "CanUserUseBudget evaluates to false if DB user is passed" );
140
138
141
ok (CanUserUseBudget($borrower1, $budget1, {superlibrarian => 1}));
139
ok (CanUserUseBudget($borrower1, $budget1, {superlibrarian => 1}));
Lines 151-157 ok (!CanUserUseBudget($borrower1, $budget1, {acquisition => 0})); Link Here
151
149
152
my $flags = {acquisition => {order_manage => 1}};
150
my $flags = {acquisition => {order_manage => 1}};
153
151
154
$userenv->{branch} = 'B1';
152
t::lib::Mocks::mock_userenv({ branchcode => 'B1' });
155
153
156
# Restriction is 'none'
154
# Restriction is 'none'
157
ok (CanUserUseBudget($borrower1, $budget1, $flags));
155
ok (CanUserUseBudget($borrower1, $budget1, $flags));
Lines 194-200 ok (CanUserUseBudget($borrower2, $budget15, $flags)); Link Here
194
ok (!CanUserUseBudget($borrower2, $budget16, $flags));
192
ok (!CanUserUseBudget($borrower2, $budget16, $flags));
195
193
196
194
197
$userenv->{branch} = 'B2';
195
t::lib::Mocks::mock_userenv({ branchcode => 'B2' });
198
196
199
# Restriction is 'none'
197
# Restriction is 'none'
200
ok (CanUserUseBudget($borrower1, $budget1, $flags));
198
ok (CanUserUseBudget($borrower1, $budget1, $flags));
Lines 245-252 sub Mock_GetBudgetUsers { Link Here
245
243
246
    return @{ $budgetusers{$budget_id} };
244
    return @{ $budgetusers{$budget_id} };
247
}
245
}
248
249
# C4::Context->userenv
250
sub Mock_userenv {
251
    return $userenv;
252
}
(-)a/t/Circulation_barcodedecode.t (-2 / +2 lines)
Lines 20-30 use Modern::Perl; Link Here
20
use Test::More tests => 26;
20
use Test::More tests => 26;
21
21
22
use C4::Context;
22
use C4::Context;
23
use t::lib::Mocks;
23
24
24
use_ok( 'C4::Circulation' );
25
use_ok( 'C4::Circulation' );
25
26
26
C4::Context->_new_userenv(123456);
27
t::lib::Mocks::mock_userenv({ branchcode => 'IMS' });
27
C4::Context->set_userenv(1,'kmkale' , 1, 'km', 'kale' , 'IMS', 'IMS Branch DEscription', 0, 'kmkale@anantcorp.com');
28
28
29
our %inputs = (
29
our %inputs = (
30
    cuecat     => ["26002315", '.C3nZC3nZC3nYD3b6ENnZCNnY.fHmc.C3D1Dxr2C3nZE3n7.', ".C3nZC3nZC3nYD3b6ENnZCNnY.fHmc.C3D1Dxr2C3nZE3n7.\r\n",
30
    cuecat     => ["26002315", '.C3nZC3nZC3nYD3b6ENnZCNnY.fHmc.C3D1Dxr2C3nZE3n7.', ".C3nZC3nZC3nYD3b6ENnZCNnY.fHmc.C3D1Dxr2C3nZE3n7.\r\n",
(-)a/t/db_dependent/Acquisition/NewOrder.t (-3 / +2 lines)
Lines 11-25 use Koha::Database; Link Here
11
use Koha::DateUtils qw( dt_from_string output_pref );
11
use Koha::DateUtils qw( dt_from_string output_pref );
12
use Koha::Acquisition::Booksellers;
12
use Koha::Acquisition::Booksellers;
13
use Koha::Acquisition::Orders;
13
use Koha::Acquisition::Orders;
14
use t::lib::Mocks;
14
15
15
my $schema = Koha::Database->new()->schema();
16
my $schema = Koha::Database->new()->schema();
16
$schema->storage->txn_begin();
17
$schema->storage->txn_begin();
17
my $dbh = C4::Context->dbh;
18
my $dbh = C4::Context->dbh;
18
$dbh->{RaiseError} = 1;
19
$dbh->{RaiseError} = 1;
19
20
20
C4::Context->_new_userenv('xxx');
21
t::lib::Mocks::mock_userenv({ branchcode => 'CPL' });
21
C4::Context->set_userenv(42, 'ordercreator', '0042', 'Order', 'Creator', 'CPL',
22
    0, 'ordercreator@example.com');
23
22
24
my $bookseller = Koha::Acquisition::Bookseller->new(
23
my $bookseller = Koha::Acquisition::Bookseller->new(
25
    {
24
    {
(-)a/t/db_dependent/ArticleRequests.t (-14 / +2 lines)
Lines 207-222 $rule->delete(); Link Here
207
207
208
subtest 'search_limited' => sub {
208
subtest 'search_limited' => sub {
209
    plan tests => 2;
209
    plan tests => 2;
210
    C4::Context->_new_userenv('xxx');
211
    my $nb_article_requests = Koha::ArticleRequests->count;
210
    my $nb_article_requests = Koha::ArticleRequests->count;
212
211
213
    my $group_1 = Koha::Library::Group->new( { title => 'TEST Group 1' } )->store;
212
    my $group_1 = Koha::Library::Group->new( { title => 'TEST Group 1' } )->store;
214
    my $group_2 = Koha::Library::Group->new( { title => 'TEST Group 2' } )->store;
213
    my $group_2 = Koha::Library::Group->new( { title => 'TEST Group 2' } )->store;
215
    Koha::Library::Group->new({ parent_id => $group_1->id,  branchcode => $patron->branchcode })->store();
214
    Koha::Library::Group->new({ parent_id => $group_1->id,  branchcode => $patron->branchcode })->store();
216
    Koha::Library::Group->new({ parent_id => $group_2->id,  branchcode => $patron_2->branchcode })->store();
215
    Koha::Library::Group->new({ parent_id => $group_2->id,  branchcode => $patron_2->branchcode })->store();
217
    set_logged_in_user( $patron ); # Is superlibrarian
216
    t::lib::Mocks::mock_userenv( { patron => $patron } ); # Is superlibrarian
218
    is( Koha::ArticleRequests->search_limited->count, $nb_article_requests, 'Koha::ArticleRequests->search_limited should return all article requests for superlibrarian' );
217
    is( Koha::ArticleRequests->search_limited->count, $nb_article_requests, 'Koha::ArticleRequests->search_limited should return all article requests for superlibrarian' );
219
    set_logged_in_user( $patron_2 ); # Is restricted
218
    t::lib::Mocks::mock_userenv( { patron => $patron_2 } ); # Is restricted
220
    is( Koha::ArticleRequests->search_limited->count, 0, 'Koha::ArticleRequests->search_limited should not return all article requests for restricted patron' );
219
    is( Koha::ArticleRequests->search_limited->count, 0, 'Koha::ArticleRequests->search_limited should not return all article requests for restricted patron' );
221
};
220
};
222
221
Lines 244-257 subtest 'may_article_request' => sub { Link Here
244
};
243
};
245
244
246
$schema->storage->txn_rollback();
245
$schema->storage->txn_rollback();
247
248
sub set_logged_in_user {
249
    my ($patron) = @_;
250
    C4::Context->set_userenv(
251
        $patron->borrowernumber, $patron->userid,
252
        $patron->cardnumber,     'firstname',
253
        'surname',               $patron->library->branchcode,
254
        'Midway Public Library', $patron->flags,
255
        '',                      ''
256
    );
257
}
(-)a/t/db_dependent/Bookseller.t (-4 / +2 lines)
Lines 639-647 ok( exists( $suppliers{$id_supplier1} ), Link Here
639
    "Supplier1 has late orders and $daysago10==$daysago10 " )
639
    "Supplier1 has late orders and $daysago10==$daysago10 " )
640
  ;
640
  ;
641
641
642
C4::Context->_new_userenv('DUMMY SESSION');
642
t::lib::Mocks::mock_userenv({ flags => 0 });
643
C4::Context->set_userenv(0,0,0,'firstname','surname', 'BRANCH1', 'Library 1', 0, '', '');
644
my $userenv = C4::Context->userenv;
645
643
646
my $module = Test::MockModule->new('C4::Auth');
644
my $module = Test::MockModule->new('C4::Auth');
647
$module->mock(
645
$module->mock(
Lines 672-678 is( Link Here
672
);
670
);
673
671
674
# don the cape and turn into Superlibrarian!
672
# don the cape and turn into Superlibrarian!
675
C4::Context->set_userenv(0,0,0,'firstname','surname', 'BRANCH1', 'Library 1', 1, '', '');
673
t::lib::Mocks::mock_userenv({ flags => 1 });
676
@subscriptions = SearchSubscriptions({expiration_date => '2013-12-31'});
674
@subscriptions = SearchSubscriptions({expiration_date => '2013-12-31'});
677
is(
675
is(
678
    scalar(grep { !$_->{cannotdisplay} } @subscriptions ),
676
    scalar(grep { !$_->{cannotdisplay} } @subscriptions ),
(-)a/t/db_dependent/Budgets.t (-10 / +8 lines)
Lines 15-20 use Koha::Acquisition::Funds; Link Here
15
use Koha::Patrons;
15
use Koha::Patrons;
16
16
17
use t::lib::TestBuilder;
17
use t::lib::TestBuilder;
18
use t::lib::Mocks;
18
use Koha::DateUtils;
19
use Koha::DateUtils;
19
20
20
use YAML;
21
use YAML;
Lines 30-40 my $library = $builder->build({ Link Here
30
    source => 'Branch',
31
    source => 'Branch',
31
});
32
});
32
33
33
# Mock userenv
34
t::lib::Mocks::mock_userenv(
34
local $SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /redefined/ };
35
    {
35
my $userenv;
36
        flags => 1,
36
*C4::Context::userenv = \&Mock_userenv;
37
        userid => 'my_userid',
37
$userenv = { flags => 1, id => 'my_userid', branch => $library->{branchcode} };
38
        branch => $library->{branchcode},
39
    }
40
);
38
41
39
#
42
#
40
# Budget Periods :
43
# Budget Periods :
Lines 953-960 sub _get_budgetname_by_id { Link Here
953
      @$budgets;
956
      @$budgets;
954
    return $budget_name;
957
    return $budget_name;
955
}
958
}
956
957
# C4::Context->userenv
958
sub Mock_userenv {
959
    return $userenv;
960
}
(-)a/t/db_dependent/Circulation.t (-3 / +2 lines)
Lines 133-140 is( Link Here
133
);
133
);
134
134
135
# Now, set a userenv
135
# Now, set a userenv
136
C4::Context->_new_userenv('xxx');
136
t::lib::Mocks::mock_userenv({ branchcode => $library2->{branchcode} });
137
C4::Context->set_userenv(0,0,0,'firstname','surname', $library2->{branchcode}, 'Midway Public Library', '', '', '');
138
is(C4::Context->userenv->{branch}, $library2->{branchcode}, 'userenv set');
137
is(C4::Context->userenv->{branch}, $library2->{branchcode}, 'userenv set');
139
138
140
# Userenv set, PickupLibrary
139
# Userenv set, PickupLibrary
Lines 2750-2756 $cache->clear_from_cache('single_holidays'); Link Here
2750
2749
2751
sub set_userenv {
2750
sub set_userenv {
2752
    my ( $library ) = @_;
2751
    my ( $library ) = @_;
2753
    C4::Context->set_userenv(0,0,0,'firstname','surname', $library->{branchcode}, $library->{branchname}, '', '', '');
2752
    t::lib::Mocks::mock_userenv({ branchcode => $library->{branchcode} });
2754
}
2753
}
2755
2754
2756
sub str {
2755
sub str {
(-)a/t/db_dependent/Circulation/IsItemIssued.t (-8 / +2 lines)
Lines 28-33 use Koha::DateUtils; Link Here
28
use Koha::Patrons;
28
use Koha::Patrons;
29
29
30
use t::lib::TestBuilder;
30
use t::lib::TestBuilder;
31
use t::lib::Mocks;
31
32
32
use MARC::Record;
33
use MARC::Record;
33
34
Lines 39-52 my $library = $builder->build({ source => 'Branch' }); Link Here
39
my $itemtype = $builder->build({ source => 'Itemtype' })->{itemtype};
40
my $itemtype = $builder->build({ source => 'Itemtype' })->{itemtype};
40
my $patron_category = $builder->build({ source => 'Category' });
41
my $patron_category = $builder->build({ source => 'Category' });
41
42
42
C4::Context->_new_userenv('DUMMY SESSION');
43
t::lib::Mocks::mock_userenv({ branchcode => $library->{branchcode} });
43
C4::Context->set_userenv(
44
    undef, undef, undef, undef, undef,
45
    $library->{branchcode},
46
    $library->{branchname}
47
);
48
49
50
44
51
my $borrowernumber = Koha::Patron->new({
45
my $borrowernumber = Koha::Patron->new({
52
    firstname =>  'my firstname',
46
    firstname =>  'my firstname',
(-)a/t/db_dependent/Circulation/IssuingRules/maxsuspensiondays.t (-8 / +2 lines)
Lines 14-19 use Koha::Patron::Debarments qw( GetDebarments DelDebarment ); Link Here
14
use Koha::Patrons;
14
use Koha::Patrons;
15
15
16
use t::lib::TestBuilder;
16
use t::lib::TestBuilder;
17
use t::lib::Mocks;
17
18
18
my $schema = Koha::Database->schema;
19
my $schema = Koha::Database->schema;
19
$schema->storage->txn_begin;
20
$schema->storage->txn_begin;
Lines 25-33 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode}; Link Here
25
my $itemtype   = $builder->build({ source => 'Itemtype' })->{itemtype};
26
my $itemtype   = $builder->build({ source => 'Itemtype' })->{itemtype};
26
my $patron_category = $builder->build({ source => 'Category' });
27
my $patron_category = $builder->build({ source => 'Category' });
27
28
28
local $SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /redefined/ };
29
t::lib::Mocks::mock_userenv({ branchcode => $branchcode });
29
my $userenv->{branch} = $branchcode;
30
*C4::Context::userenv = \&Mock_userenv;
31
30
32
# Test without maxsuspensiondays set
31
# Test without maxsuspensiondays set
33
Koha::IssuingRules->search->delete;
32
Koha::IssuingRules->search->delete;
Lines 103-110 is( Link Here
103
DelDebarment( $debarments->[0]->{borrower_debarment_id} );
102
DelDebarment( $debarments->[0]->{borrower_debarment_id} );
104
103
105
$schema->storage->txn_rollback;
104
$schema->storage->txn_rollback;
106
107
# C4::Context->userenv
108
sub Mock_userenv {
109
    return $userenv;
110
}
(-)a/t/db_dependent/Circulation/MarkIssueReturned.t (-2 / +1 lines)
Lines 36-43 my $builder = t::lib::TestBuilder->new; Link Here
36
36
37
my $library = $builder->build({ source => 'Branch' });
37
my $library = $builder->build({ source => 'Branch' });
38
38
39
C4::Context->_new_userenv('xxx');
39
t::lib::Mocks::mock_userenv({ branchcode => $library->{branchcode} });
40
C4::Context->set_userenv(0,0,0,'firstname','surname', $library->{branchcode}, $library->{branchname}, '', '', '');
41
40
42
my $patron_category = $builder->build({ source => 'Category', value => { category_type => 'P', enrolmentfee => 0 } });
41
my $patron_category = $builder->build({ source => 'Category', value => { category_type => 'P', enrolmentfee => 0 } });
43
my $patron = $builder->build({ source => 'Borrower', value => { branchcode => $library->{branchcode}, categorycode => $patron_category->{categorycode} } } );
42
my $patron = $builder->build({ source => 'Borrower', value => { branchcode => $library->{branchcode}, categorycode => $patron_category->{categorycode} } } );
(-)a/t/db_dependent/Circulation/SwitchOnSiteCheckouts.t (-2 / +1 lines)
Lines 100-107 my $issuingrule = $builder->build({ Link Here
100
    },
100
    },
101
});
101
});
102
102
103
C4::Context->_new_userenv ('DUMMY_SESSION_ID');
103
t::lib::Mocks::mock_userenv({ patron => $patron });
104
C4::Context->set_userenv($patron->borrowernumber, $patron->userid, 'usercnum', 'First name', 'Surname', $branch->{branchcode}, 'My Library', 0);
105
104
106
t::lib::Mocks::mock_preference('AllowTooManyOverride', 0);
105
t::lib::Mocks::mock_preference('AllowTooManyOverride', 0);
107
106
(-)a/t/db_dependent/Circulation/TooMany.t (-2 / +2 lines)
Lines 83-90 my $item = $builder->build({ Link Here
83
    },
83
    },
84
});
84
});
85
85
86
C4::Context->_new_userenv ('DUMMY_SESSION_ID');
86
my $patron_object = Koha::Patrons->find( $patron->{borrowernumber} );
87
C4::Context->set_userenv($patron->{borrowernumber}, $patron->{userid}, 'usercnum', 'First name', 'Surname', $branch->{branchcode}, 'My Library', 0);
87
t::lib::Mocks::mock_userenv( { patron => $patron_object });
88
88
89
# TooMany return ($current_loan_count, $max_loans_allowed) or undef
89
# TooMany return ($current_loan_count, $max_loans_allowed) or undef
90
# CO = Checkout
90
# CO = Checkout
(-)a/t/db_dependent/Circulation/issue.t (-20 / +11 lines)
Lines 148-178 my $borrower_id1 = Koha::Patron->new({ Link Here
148
    categorycode => $categorycode,
148
    categorycode => $categorycode,
149
    branchcode   => $branchcode_1
149
    branchcode   => $branchcode_1
150
})->store->borrowernumber;
150
})->store->borrowernumber;
151
my $borrower_1 = Koha::Patrons->find( $borrower_id1 )->unblessed;
151
my $patron_1 = Koha::Patrons->find( $borrower_id1 );
152
my $borrower_1 = $patron_1->unblessed;
152
my $borrower_id2 = Koha::Patron->new({
153
my $borrower_id2 = Koha::Patron->new({
153
    firstname    => 'firstname2',
154
    firstname    => 'firstname2',
154
    surname      => 'surname2 ',
155
    surname      => 'surname2 ',
155
    categorycode => $categorycode,
156
    categorycode => $categorycode,
156
    branchcode   => $branchcode_2,
157
    branchcode   => $branchcode_2,
157
})->store->borrowernumber;
158
})->store->borrowernumber;
158
my $borrower_2 = Koha::Patrons->find( $borrower_id2 )->unblessed;
159
my $patron_2 = Koha::Patrons->find( $borrower_id2 );
159
160
my $borrower_2 = $patron_2->unblessed;
160
my @USERENV = (
161
    $borrower_id1, 'test', 'MASTERTEST', 'firstname', $branchcode_1,
162
    $branchcode_1, 'email@example.org'
163
);
164
165
my @USERENV_DIFFERENT_LIBRARY = (
166
    $borrower_id1, 'test', 'MASTERTEST', 'firstname', $branchcode_3,
167
    $branchcode_3, 'email@example.org'
168
);
169
161
170
162
t::lib::Mocks::mock_userenv({ patron => $patron_1 });
171
C4::Context->_new_userenv('DUMMY_SESSION_ID');
172
C4::Context->set_userenv(@USERENV);
173
174
my $userenv = C4::Context->userenv
175
  or BAIL_OUT("No userenv");
176
163
177
#Begin Tests
164
#Begin Tests
178
165
Lines 222-230 my $se = Test::MockModule->new( 'C4::Context' ); Link Here
222
$se->mock( 'interface', sub {return 'intranet'});
209
$se->mock( 'interface', sub {return 'intranet'});
223
210
224
# Let's renew this one at a different library for statistical purposes to test Bug 17781
211
# Let's renew this one at a different library for statistical purposes to test Bug 17781
225
C4::Context->set_userenv(@USERENV_DIFFERENT_LIBRARY);
212
# Mocking userenv with a different branchcode
213
t::lib::Mocks::mock_userenv({ patron => $patron_2, branchcode => $branchcode_3 });
214
226
my $datedue3 = AddRenewal( $borrower_id1, $item_id1, $branchcode_1, $datedue1, $daysago10 );
215
my $datedue3 = AddRenewal( $borrower_id1, $item_id1, $branchcode_1, $datedue1, $daysago10 );
227
C4::Context->set_userenv(@USERENV);
216
217
# Restoring the userenv with the original branchcode
218
t::lib::Mocks::mock_userenv({ patron => $patron_1});
228
219
229
like(
220
like(
230
    $datedue3,
221
    $datedue3,
(-)a/t/db_dependent/DecreaseLoanHighHolds.t (-3 / +1 lines)
Lines 51-59 my $library = $builder->build( { source => 'Branch' } ); Link Here
51
my $category = $builder->build( { source => 'Category' } );
51
my $category = $builder->build( { source => 'Category' } );
52
my $itemtype = $builder->build( { source => 'Itemtype' } )->{itemtype};
52
my $itemtype = $builder->build( { source => 'Itemtype' } )->{itemtype};
53
53
54
# Set userenv
54
t::lib::Mocks::mock_userenv({ branchcode => $library->{branchcode} });
55
C4::Context->_new_userenv('xxx');
56
C4::Context->set_userenv( 0, 0, 0, 'firstname', 'surname', $library->{branchcode}, 'Midway Public Library', '', '', '' );
57
is( C4::Context->userenv->{branch}, $library->{branchcode}, 'userenv set' );
55
is( C4::Context->userenv->{branch}, $library->{branchcode}, 'userenv set' );
58
56
59
my $patron_category = $builder->build({ source => 'Category', value => { category_type => 'P', enrolmentfee => 0 } });
57
my $patron_category = $builder->build({ source => 'Category', value => { category_type => 'P', enrolmentfee => 0 } });
(-)a/t/db_dependent/Holds/DisallowHoldIfItemsAvailable.t (-3 / +1 lines)
Lines 30-38 my $library2 = $builder->build({ Link Here
30
});
30
});
31
31
32
32
33
# Now, set a userenv
33
t::lib::Mocks::mock_userenv({ branchcode => $library1->{branchcode} });
34
C4::Context->_new_userenv('xxx');
35
C4::Context->set_userenv(0,0,0,'firstname','surname', $library1->{branchcode}, 'Midway Public Library', '', '', '');
36
34
37
my $bib_title = "Test Title";
35
my $bib_title = "Test Title";
38
36
(-)a/t/db_dependent/Holds/RevertWaitingStatus.t (-7 / +1 lines)
Lines 42-54 my $branchcode = $builder->build( { source => 'Branch' } )->{branchcode}; Link Here
42
my $itemtype = $builder->build(
42
my $itemtype = $builder->build(
43
    { source => 'Itemtype', value => { notforloan => undef } } )->{itemtype};
43
    { source => 'Itemtype', value => { notforloan => undef } } )->{itemtype};
44
44
45
local $SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /redefined/ };
45
t::lib::Mocks::mock_userenv({ flags => 1, userid => '1', branchcode => $branchcode });
46
*C4::Context::userenv = \&Mock_userenv;
47
48
sub Mock_userenv {
49
    my $userenv = { flags => 1, id => '1', branch => $branchcode };
50
    return $userenv;
51
}
52
46
53
my $borrowers_count = 3;
47
my $borrowers_count = 3;
54
48
(-)a/t/db_dependent/Koha/Items.t (-2 / +1 lines)
Lines 57-64 my $new_item_2 = Koha::Item->new( Link Here
57
    }
57
    }
58
)->store;
58
)->store;
59
59
60
C4::Context->_new_userenv('xxx');
60
t::lib::Mocks::mock_userenv({ branchcode => $library->{branchcode} });
61
C4::Context->set_userenv(0,0,0,'firstname','surname', $library->{branchcode}, 'Midway Public Library', '', '', '');
62
61
63
like( $new_item_1->itemnumber, qr|^\d+$|, 'Adding a new item should have set the itemnumber' );
62
like( $new_item_1->itemnumber, qr|^\d+$|, 'Adding a new item should have set the itemnumber' );
64
is( Koha::Items->search->count, $nb_of_items + 2, 'The 2 items should have been added' );
63
is( Koha::Items->search->count, $nb_of_items + 2, 'The 2 items should have been added' );
(-)a/t/db_dependent/Koha/Object.t (-2 / +3 lines)
Lines 36-41 use Scalar::Util qw( isvstring ); Link Here
36
use Try::Tiny;
36
use Try::Tiny;
37
37
38
use t::lib::TestBuilder;
38
use t::lib::TestBuilder;
39
use t::lib::Mocks;
39
40
40
BEGIN {
41
BEGIN {
41
    use_ok('Koha::Object');
42
    use_ok('Koha::Object');
Lines 315-322 subtest 'unblessed_all_relateds' => sub { Link Here
315
316
316
    # FIXME It's very painful to create an issue in tests!
317
    # FIXME It's very painful to create an issue in tests!
317
    my $library = $builder->build_object( { class => 'Koha::Libraries' } );
318
    my $library = $builder->build_object( { class => 'Koha::Libraries' } );
318
    C4::Context->_new_userenv('xxx');
319
    t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
319
    C4::Context->set_userenv(0,0,0,'firstname','surname', $library->branchcode, 'Midway Public Library', '', '', '');
320
320
    my $patron_category = $builder->build(
321
    my $patron_category = $builder->build(
321
        {
322
        {
322
            source => 'Category',
323
            source => 'Category',
(-)a/t/db_dependent/Koha/Patron/Modifications.t (-13 / +2 lines)
Lines 23-28 use Test::More tests => 6; Link Here
23
use Test::Exception;
23
use Test::Exception;
24
24
25
use t::lib::TestBuilder;
25
use t::lib::TestBuilder;
26
use t::lib::Mocks;
26
27
27
use Digest::MD5 qw( md5_base64 md5_hex );
28
use Digest::MD5 qw( md5_base64 md5_hex );
28
use Try::Tiny;
29
use Try::Tiny;
Lines 347-354 subtest 'pending_count() and pending() tests' => sub { Link Here
347
    is( $p2_pm_attribute_2->attribute, 'ciao', 'patron modification has the right attribute change' );
348
    is( $p2_pm_attribute_2->attribute, 'ciao', 'patron modification has the right attribute change' );
348
349
349
350
350
    C4::Context->_new_userenv('xxx');
351
    t::lib::Mocks::mock_userenv({ patron => $patron_1 });
351
    set_logged_in_user( $patron_1 );
352
    is( Koha::Patron::Modifications->pending_count($library_1),
352
    is( Koha::Patron::Modifications->pending_count($library_1),
353
        1, 'pending_count() correctly returns 1 if filtered by library' );
353
        1, 'pending_count() correctly returns 1 if filtered by library' );
354
354
Lines 372-385 subtest 'pending_count() and pending() tests' => sub { Link Here
372
372
373
    $schema->storage->txn_rollback;
373
    $schema->storage->txn_rollback;
374
};
374
};
375
376
sub set_logged_in_user {
377
    my ($patron) = @_;
378
    C4::Context->set_userenv(
379
        $patron->borrowernumber, $patron->userid,
380
        $patron->cardnumber,     'firstname',
381
        'surname',               $patron->library->branchcode,
382
        'Midway Public Library', $patron->flags,
383
        '',                      ''
384
    );
385
}
(-)a/t/db_dependent/Koha/Patrons.t (-42 / +17 lines)
Lines 68-75 my $new_patron_2 = Koha::Patron->new( Link Here
68
    }
68
    }
69
)->store;
69
)->store;
70
70
71
C4::Context->_new_userenv('xxx');
71
t::lib::Mocks::mock_userenv({ patron => $new_patron_1 });
72
set_logged_in_user( $new_patron_1 );
73
72
74
is( Koha::Patrons->search->count, $nb_of_patrons + 2, 'The 2 patrons should have been added' );
73
is( Koha::Patrons->search->count, $nb_of_patrons + 2, 'The 2 patrons should have been added' );
75
74
Lines 561-568 subtest 'checkouts + pending_checkouts + get_overdues + old_checkouts' => sub { Link Here
561
    # Not sure how this is useful, but AddIssue pass this variable to different other subroutines
560
    # Not sure how this is useful, but AddIssue pass this variable to different other subroutines
562
    $patron = Koha::Patrons->find( $patron->borrowernumber )->unblessed;
561
    $patron = Koha::Patrons->find( $patron->borrowernumber )->unblessed;
563
562
564
    my $module = new Test::MockModule('C4::Context');
563
    t::lib::Mocks::mock_userenv({ branchcode => $library->{branchcode} });
565
    $module->mock( 'userenv', sub { { branch => $library->{branchcode} } } );
566
564
567
    AddIssue( $patron, $item_1->{barcode}, DateTime->now->subtract( days => 1 ) );
565
    AddIssue( $patron, $item_1->{barcode}, DateTime->now->subtract( days => 1 ) );
568
    AddIssue( $patron, $item_2->{barcode}, DateTime->now->subtract( days => 5 ) );
566
    AddIssue( $patron, $item_2->{barcode}, DateTime->now->subtract( days => 5 ) );
Lines 595-601 subtest 'checkouts + pending_checkouts + get_overdues + old_checkouts' => sub { Link Here
595
    # Clean stuffs
593
    # Clean stuffs
596
    Koha::Checkouts->search( { borrowernumber => $patron->borrowernumber } )->delete;
594
    Koha::Checkouts->search( { borrowernumber => $patron->borrowernumber } )->delete;
597
    $patron->delete;
595
    $patron->delete;
598
    $module->unmock('userenv');
599
};
596
};
600
597
601
subtest 'get_routing_lists' => sub {
598
subtest 'get_routing_lists' => sub {
Lines 844-862 subtest 'search_patrons_to_anonymise & anonymise_issue_history' => sub { Link Here
844
841
845
    # TODO create a subroutine in t::lib::Mocks
842
    # TODO create a subroutine in t::lib::Mocks
846
    my $branch = $builder->build({ source => 'Branch' });
843
    my $branch = $builder->build({ source => 'Branch' });
847
    my $userenv_patron = $builder->build({
844
    my $userenv_patron = $builder->build_object({
848
        source => 'Borrower',
845
        class  => 'Koha::Patrons',
849
        value  => { branchcode => $branch->{branchcode} },
846
        value  => { branchcode => $branch->{branchcode}, flags => 0 },
850
    });
847
    });
851
    C4::Context->_new_userenv('DUMMY SESSION');
848
    t::lib::Mocks::mock_userenv({ patron => $userenv_patron });
852
    C4::Context->set_userenv(
849
853
        $userenv_patron->{borrowernumber},
854
        $userenv_patron->{userid},
855
        'usercnum', 'First name', 'Surname',
856
        $branch->{branchcode},
857
        $branch->{branchname},
858
        0,
859
    );
860
    my $anonymous = $builder->build( { source => 'Borrower', }, );
850
    my $anonymous = $builder->build( { source => 'Borrower', }, );
861
851
862
    t::lib::Mocks::mock_preference( 'AnonymousPatron', $anonymous->{borrowernumber} );
852
    t::lib::Mocks::mock_preference( 'AnonymousPatron', $anonymous->{borrowernumber} );
Lines 1052-1058 subtest 'search_patrons_to_anonymise & anonymise_issue_history' => sub { Link Here
1052
    };
1042
    };
1053
1043
1054
    Koha::Patrons->find( $anonymous->{borrowernumber})->delete;
1044
    Koha::Patrons->find( $anonymous->{borrowernumber})->delete;
1055
    Koha::Patrons->find( $userenv_patron->{borrowernumber})->delete;
1045
    $userenv_patron->delete;
1056
1046
1057
    # Reset IndependentBranches for further tests
1047
    # Reset IndependentBranches for further tests
1058
    t::lib::Mocks::mock_preference('IndependentBranches', 0);
1048
    t::lib::Mocks::mock_preference('IndependentBranches', 0);
Lines 1108-1134 subtest 'libraries_where_can_see_patrons + can_see_patron_infos + search_limited Link Here
1108
1098
1109
        my @branchcodes;
1099
        my @branchcodes;
1110
1100
1111
        set_logged_in_user( $patron_11_1 );
1101
        t::lib::Mocks::mock_userenv({ patron => $patron_11_1 });
1112
        @branchcodes = $patron_11_1->libraries_where_can_see_patrons;
1102
        @branchcodes = $patron_11_1->libraries_where_can_see_patrons;
1113
        is_deeply( \@branchcodes, [], q|patron_11_1 has view_borrower_infos_from_any_libraries => No restriction| );
1103
        is_deeply( \@branchcodes, [], q|patron_11_1 has view_borrower_infos_from_any_libraries => No restriction| );
1114
1104
1115
        set_logged_in_user( $patron_11_2 );
1105
        t::lib::Mocks::mock_userenv({ patron => $patron_11_2 });
1116
        @branchcodes = $patron_11_2->libraries_where_can_see_patrons;
1106
        @branchcodes = $patron_11_2->libraries_where_can_see_patrons;
1117
        is_deeply( \@branchcodes, [ sort ( $library_11->branchcode, $library_12->branchcode ) ], q|patron_11_2 has not view_borrower_infos_from_any_libraries => Can only see patron's from its group| );
1107
        is_deeply( \@branchcodes, [ sort ( $library_11->branchcode, $library_12->branchcode ) ], q|patron_11_2 has not view_borrower_infos_from_any_libraries => Can only see patron's from its group| );
1118
1108
1119
        set_logged_in_user( $patron_21 );
1109
        t::lib::Mocks::mock_userenv({ patron => $patron_21 });
1120
        @branchcodes = $patron_21->libraries_where_can_see_patrons;
1110
        @branchcodes = $patron_21->libraries_where_can_see_patrons;
1121
        is_deeply( \@branchcodes, [$library_21->branchcode], q|patron_21 has not view_borrower_infos_from_any_libraries => Can only see patron's from its group| );
1111
        is_deeply( \@branchcodes, [$library_21->branchcode], q|patron_21 has not view_borrower_infos_from_any_libraries => Can only see patron's from its group| );
1122
    };
1112
    };
1123
    subtest 'can_see_patron_infos' => sub {
1113
    subtest 'can_see_patron_infos' => sub {
1124
        plan tests => 6;
1114
        plan tests => 6;
1125
1115
1126
        set_logged_in_user( $patron_11_1 );
1116
        t::lib::Mocks::mock_userenv({ patron => $patron_11_1 });
1127
        is( $patron_11_1->can_see_patron_infos( $patron_11_2 ), 1, q|patron_11_1 can see patron_11_2, from its library| );
1117
        is( $patron_11_1->can_see_patron_infos( $patron_11_2 ), 1, q|patron_11_1 can see patron_11_2, from its library| );
1128
        is( $patron_11_1->can_see_patron_infos( $patron_12 ),   1, q|patron_11_1 can see patron_12, from its group| );
1118
        is( $patron_11_1->can_see_patron_infos( $patron_12 ),   1, q|patron_11_1 can see patron_12, from its group| );
1129
        is( $patron_11_1->can_see_patron_infos( $patron_21 ),   1, q|patron_11_1 can see patron_11_2, from another group| );
1119
        is( $patron_11_1->can_see_patron_infos( $patron_21 ),   1, q|patron_11_1 can see patron_11_2, from another group| );
1130
1120
1131
        set_logged_in_user( $patron_11_2 );
1121
        t::lib::Mocks::mock_userenv({ patron => $patron_11_2 });
1132
        is( $patron_11_2->can_see_patron_infos( $patron_11_1 ), 1, q|patron_11_2 can see patron_11_1, from its library| );
1122
        is( $patron_11_2->can_see_patron_infos( $patron_11_1 ), 1, q|patron_11_2 can see patron_11_1, from its library| );
1133
        is( $patron_11_2->can_see_patron_infos( $patron_12 ),   1, q|patron_11_2 can see patron_12, from its group| );
1123
        is( $patron_11_2->can_see_patron_infos( $patron_12 ),   1, q|patron_11_2 can see patron_12, from its group| );
1134
        is( $patron_11_2->can_see_patron_infos( $patron_21 ),   0, q|patron_11_2 can NOT see patron_21, from another group| );
1124
        is( $patron_11_2->can_see_patron_infos( $patron_21 ),   0, q|patron_11_2 can NOT see patron_21, from another group| );
Lines 1136-1151 subtest 'libraries_where_can_see_patrons + can_see_patron_infos + search_limited Link Here
1136
    subtest 'search_limited' => sub {
1126
    subtest 'search_limited' => sub {
1137
        plan tests => 6;
1127
        plan tests => 6;
1138
1128
1139
        set_logged_in_user( $patron_11_1 );
1129
        t::lib::Mocks::mock_userenv({ patron => $patron_11_1 });
1140
        my $total_number_of_patrons = $nb_of_patrons + 4; #we added four in these tests
1130
        my $total_number_of_patrons = $nb_of_patrons + 4; #we added four in these tests
1141
        is( Koha::Patrons->search->count, $total_number_of_patrons, 'Non-limited search should return all patrons' );
1131
        is( Koha::Patrons->search->count, $total_number_of_patrons, 'Non-limited search should return all patrons' );
1142
        is( Koha::Patrons->search_limited->count, $total_number_of_patrons, 'patron_11_1 is allowed to see all patrons' );
1132
        is( Koha::Patrons->search_limited->count, $total_number_of_patrons, 'patron_11_1 is allowed to see all patrons' );
1143
1133
1144
        set_logged_in_user( $patron_11_2 );
1134
        t::lib::Mocks::mock_userenv({ patron => $patron_11_2 });
1145
        is( Koha::Patrons->search->count, $total_number_of_patrons, 'Non-limited search should return all patrons');
1135
        is( Koha::Patrons->search->count, $total_number_of_patrons, 'Non-limited search should return all patrons');
1146
        is( Koha::Patrons->search_limited->count, 3, 'patron_12_1 is not allowed to see patrons from other groups, only patron_11_1, patron_11_2 and patron_12' );
1136
        is( Koha::Patrons->search_limited->count, 3, 'patron_12_1 is not allowed to see patrons from other groups, only patron_11_1, patron_11_2 and patron_12' );
1147
1137
1148
        set_logged_in_user( $patron_21 );
1138
        t::lib::Mocks::mock_userenv({ patron => $patron_21 });
1149
        is( Koha::Patrons->search->count, $total_number_of_patrons, 'Non-limited search should return all patrons');
1139
        is( Koha::Patrons->search->count, $total_number_of_patrons, 'Non-limited search should return all patrons');
1150
        is( Koha::Patrons->search_limited->count, 1, 'patron_21 is not allowed to see patrons from other groups, only himself' );
1140
        is( Koha::Patrons->search_limited->count, 1, 'patron_21 is not allowed to see patrons from other groups, only himself' );
1151
    };
1141
    };
Lines 1285-1292 subtest 'get_overdues' => sub { Link Here
1285
        }
1275
        }
1286
    );
1276
    );
1287
1277
1288
    my $module = new Test::MockModule('C4::Context');
1278
    t::lib::Mocks::mock_preference({ branchcode => $library->{branchcode} });
1289
    $module->mock( 'userenv', sub { { branch => $library->{branchcode} } } );
1290
1279
1291
    AddIssue( $patron, $item_1->{barcode}, DateTime->now->subtract( days => 1 ) );
1280
    AddIssue( $patron, $item_1->{barcode}, DateTime->now->subtract( days => 1 ) );
1292
    AddIssue( $patron, $item_2->{barcode}, DateTime->now->subtract( days => 5 ) );
1281
    AddIssue( $patron, $item_2->{barcode}, DateTime->now->subtract( days => 5 ) );
Lines 1596-1612 subtest '->set_password' => sub { Link Here
1596
1585
1597
    $schema->storage->txn_rollback;
1586
    $schema->storage->txn_rollback;
1598
};
1587
};
1599
1600
1601
1602
# TODO Move to t::lib::Mocks and reuse it!
1603
sub set_logged_in_user {
1604
    my ($patron) = @_;
1605
    C4::Context->set_userenv(
1606
        $patron->borrowernumber, $patron->userid,
1607
        $patron->cardnumber,     'firstname',
1608
        'surname',               $patron->library->branchcode,
1609
        'Midway Public Library', $patron->flags,
1610
        '',                      ''
1611
    );
1612
}
(-)a/t/db_dependent/Koha/Reviews.t (-12 / +2 lines)
Lines 26-31 use Koha::Reviews; Link Here
26
use Koha::Database;
26
use Koha::Database;
27
27
28
use t::lib::TestBuilder;
28
use t::lib::TestBuilder;
29
use t::lib::Mocks;
29
30
30
my $schema = Koha::Database->new->schema;
31
my $schema = Koha::Database->new->schema;
31
$schema->storage->txn_begin;
32
$schema->storage->txn_begin;
Lines 74-80 subtest 'search_limited' => sub { Link Here
74
    my $group_2 = Koha::Library::Group->new( { title => 'TEST Group 2' } )->store;
75
    my $group_2 = Koha::Library::Group->new( { title => 'TEST Group 2' } )->store;
75
    Koha::Library::Group->new({ parent_id => $group_1->id,  branchcode => $patron_1->branchcode })->store();
76
    Koha::Library::Group->new({ parent_id => $group_1->id,  branchcode => $patron_1->branchcode })->store();
76
    Koha::Library::Group->new({ parent_id => $group_2->id,  branchcode => $patron_2->branchcode })->store();
77
    Koha::Library::Group->new({ parent_id => $group_2->id,  branchcode => $patron_2->branchcode })->store();
77
    set_logged_in_user( $patron_1 );
78
    t::lib::Mocks::mock_userenv( { patron => $patron_1 } );
78
    is( Koha::Reviews->search->count, $nb_of_approved_reviews + 3, 'Koha::Reviews->search should return all reviews' );
79
    is( Koha::Reviews->search->count, $nb_of_approved_reviews + 3, 'Koha::Reviews->search should return all reviews' );
79
    is( Koha::Reviews->search_limited->count, $nb_of_approved_reviews + 2, 'Koha::Reviews->search_limited should return reviews depending on patron permissions' );
80
    is( Koha::Reviews->search_limited->count, $nb_of_approved_reviews + 2, 'Koha::Reviews->search_limited should return reviews depending on patron permissions' );
80
};
81
};
Lines 83-96 $retrieved_review_1_1->delete; Link Here
83
is( Koha::Reviews->search->count, $nb_of_reviews + 2, 'Delete should have deleted the review' );
84
is( Koha::Reviews->search->count, $nb_of_reviews + 2, 'Delete should have deleted the review' );
84
85
85
$schema->storage->txn_rollback;
86
$schema->storage->txn_rollback;
86
87
sub set_logged_in_user {
88
    my ($patron) = @_;
89
    C4::Context->set_userenv(
90
        $patron->borrowernumber, $patron->userid,
91
        $patron->cardnumber,     'firstname',
92
        'surname',               $patron->library->branchcode,
93
        'Midway Public Library', $patron->flags,
94
        '',                      ''
95
    );
96
}
(-)a/t/db_dependent/Labels/t_Label.t (-1 / +2 lines)
Lines 21-26 use Modern::Perl; Link Here
21
21
22
use Test::More tests => 3;
22
use Test::More tests => 3;
23
use t::lib::TestBuilder;
23
use t::lib::TestBuilder;
24
use t::lib::Mocks;
24
25
25
use MARC::Record;
26
use MARC::Record;
26
use MARC::Field;
27
use MARC::Field;
Lines 56-62 my $itemtype = Link Here
56
  $builder->build( { source => 'Itemtype', value => { notforloan => undef } } )
57
  $builder->build( { source => 'Itemtype', value => { notforloan => undef } } )
57
  ->{itemtype};
58
  ->{itemtype};
58
59
59
C4::Context->set_userenv( undef, undef, undef, undef, undef, undef, $branch_1 );
60
t::lib::Mocks::mock_userenv({ branchcode => $branch_1 });
60
61
61
# Create a helper biblio
62
# Create a helper biblio
62
my $bib   = MARC::Record->new();
63
my $bib   = MARC::Record->new();
(-)a/t/db_dependent/Letters.t (-12 / +6 lines)
Lines 199-210 subtest 'getletter' => sub { Link Here
199
    is( $letter->{content}, $content, 'GetLetters gets the content correctly' );
199
    is( $letter->{content}, $content, 'GetLetters gets the content correctly' );
200
    is( $letter->{message_transport_type}, 'email', 'GetLetters gets the message type correctly' );
200
    is( $letter->{message_transport_type}, 'email', 'GetLetters gets the message type correctly' );
201
201
202
    my $context = Test::MockModule->new('C4::Context');
202
    t::lib::Mocks::mock_userenv({ branchcode => "anotherlib", flags => 1 });
203
    $context->mock( 'userenv', sub {
204
        return {
205
            flags  => 1,
206
            branch => "anotherlib" }
207
    });
208
203
209
    t::lib::Mocks::mock_preference('IndependentBranches', 1);
204
    t::lib::Mocks::mock_preference('IndependentBranches', 1);
210
    $letter = C4::Letters::getletter('my module', 'my code', $library->{branchcode}, 'email');
205
    $letter = C4::Letters::getletter('my module', 'my code', $library->{branchcode}, 'email');
Lines 216-223 subtest 'getletter' => sub { Link Here
216
    is( $letter->{title}, $title, 'GetLetters gets the title correctly' );
211
    is( $letter->{title}, $title, 'GetLetters gets the title correctly' );
217
    is( $letter->{content}, $content, 'GetLetters gets the content correctly' );
212
    is( $letter->{content}, $content, 'GetLetters gets the content correctly' );
218
    is( $letter->{message_transport_type}, 'email', 'GetLetters gets the message type correctly' );
213
    is( $letter->{message_transport_type}, 'email', 'GetLetters gets the message type correctly' );
219
220
    $context->unmock('userenv');
221
};
214
};
222
215
223
216
Lines 392-397 if (C4::Context->preference('marcflavour') eq 'UNIMARC') { Link Here
392
    );
385
    );
393
}
386
}
394
387
388
my $logged_in_user = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $library->{branchcode} }});
389
t::lib::Mocks::mock_userenv({ patron => $logged_in_user });
390
395
($biblionumber, $biblioitemnumber) = AddBiblio($bib, '');
391
($biblionumber, $biblioitemnumber) = AddBiblio($bib, '');
396
my $order = Koha::Acquisition::Order->new(
392
my $order = Koha::Acquisition::Order->new(
397
    {
393
    {
Lines 418-425 $bookseller->contacts->next->email('testemail@mydomain.com')->store; Link Here
418
t::lib::Mocks::mock_preference( 'LetterLog', 'on' );
414
t::lib::Mocks::mock_preference( 'LetterLog', 'on' );
419
415
420
# SendAlerts needs branchemail or KohaAdminEmailAddress as sender
416
# SendAlerts needs branchemail or KohaAdminEmailAddress as sender
421
C4::Context->_new_userenv('DUMMY');
422
C4::Context->set_userenv( 0, 0, 0, 'firstname', 'surname', $library->{branchcode}, 'My Library', 0, '', '');
423
t::lib::Mocks::mock_preference( 'KohaAdminEmailAddress', 'library@domain.com' );
417
t::lib::Mocks::mock_preference( 'KohaAdminEmailAddress', 'library@domain.com' );
424
418
425
{
419
{
Lines 550-557 subtest 'SendAlerts - claimissue' => sub { Link Here
550
    t::lib::Mocks::mock_preference( 'LetterLog', 'on' );
544
    t::lib::Mocks::mock_preference( 'LetterLog', 'on' );
551
545
552
    # SendAlerts needs branchemail or KohaAdminEmailAddress as sender
546
    # SendAlerts needs branchemail or KohaAdminEmailAddress as sender
553
    C4::Context->_new_userenv('DUMMY');
547
    t::lib::Mocks::mock_userenv({ branchcode => $library->{branchcode} });
554
    C4::Context->set_userenv( 0, 0, 0, 'firstname', 'surname', $library->{branchcode}, 'My Library', 0, '', '');
548
555
    t::lib::Mocks::mock_preference( 'KohaAdminEmailAddress', 'library@domain.com' );
549
    t::lib::Mocks::mock_preference( 'KohaAdminEmailAddress', 'library@domain.com' );
556
550
557
    {
551
    {
(-)a/t/db_dependent/Letters/TemplateToolkit.t (-2 / +2 lines)
Lines 26-31 use Test::Warn; Link Here
26
use MARC::Record;
26
use MARC::Record;
27
27
28
use t::lib::TestBuilder;
28
use t::lib::TestBuilder;
29
use t::lib::Mocks;
29
30
30
use C4::Circulation;
31
use C4::Circulation;
31
use C4::Letters;
32
use C4::Letters;
Lines 333-340 subtest 'regression tests' => sub { Link Here
333
        }
334
        }
334
    )->store->unblessed;
335
    )->store->unblessed;
335
336
336
    C4::Context->_new_userenv('xxx');
337
    t::lib::Mocks::mock_userenv({ branchcode => $library->{branchcode} });
337
    C4::Context->set_userenv(0,0,0,'firstname','surname', $library->{branchcode}, 'Midway Public Library', '', '', '');
338
338
339
    subtest 'ACQ_NOTIF_ON_RECEIV ' => sub {
339
    subtest 'ACQ_NOTIF_ON_RECEIV ' => sub {
340
        plan tests => 1;
340
        plan tests => 1;
(-)a/t/db_dependent/MarcModificationTemplates.t (-17 / +2 lines)
Lines 2-7 use Modern::Perl; Link Here
2
2
3
use Test::More tests => 115;
3
use Test::More tests => 115;
4
4
5
use t::lib::Mocks;
5
use Koha::SimpleMARC;
6
use Koha::SimpleMARC;
6
7
7
use_ok("MARC::Field");
8
use_ok("MARC::Field");
Lines 158-175 is( GetModificationTemplateAction( $actions[3]->{mmta_id} ), undef, "fourth acti Link Here
158
is( GetModificationTemplateActions( $template_id ), 0, "There is no action for deleted template" );
159
is( GetModificationTemplateActions( $template_id ), 0, "There is no action for deleted template" );
159
160
160
# ModifyRecordWithTemplate
161
# ModifyRecordWithTemplate
161
my @USERENV = (
162
t::lib::Mocks::mock_userenv();
162
    1,
163
    'test',
164
    'MASTERTEST',
165
    'Test',
166
    'Test',
167
    't',
168
    'Test',
169
    0,
170
);
171
C4::Context->_new_userenv ('DUMMY_SESSION_ID');
172
C4::Context->set_userenv ( @USERENV );
173
163
174
$template_id = AddModificationTemplate("new_template_test");
164
$template_id = AddModificationTemplate("new_template_test");
175
like( $template_id, qr|^\d+$|, "new template returns an id" );
165
like( $template_id, qr|^\d+$|, "new template returns an id" );
Lines 554-561 sub expected_record_2 { Link Here
554
    $record->append_fields(@fields);
544
    $record->append_fields(@fields);
555
    return $record;
545
    return $record;
556
}
546
}
557
558
# C4::Context->userenv
559
sub Mock_userenv {
560
    return { branchcode => 'CPL' };
561
}
(-)a/t/db_dependent/Members.t (-18 / +1 lines)
Lines 63-86 my $PHONE = "555-12123"; Link Here
63
# XXX should be randomised and checked against the database
63
# XXX should be randomised and checked against the database
64
my $IMPOSSIBLE_CARDNUMBER = "XYZZZ999";
64
my $IMPOSSIBLE_CARDNUMBER = "XYZZZ999";
65
65
66
#my ($usernum, $userid, $usercnum, $userfirstname, $usersurname, $userbranch, $branchname, $userflags, $emailaddress, $branchprinter)= @_;
66
t::lib::Mocks::mock_userenv();
67
my @USERENV = (
68
    1,
69
    'test',
70
    'MASTERTEST',
71
    'Test',
72
    'Test',
73
    't',
74
    'Test',
75
    0,
76
);
77
my $BRANCH_IDX = 5;
78
79
C4::Context->_new_userenv ('DUMMY_SESSION_ID');
80
C4::Context->set_userenv ( @USERENV );
81
82
my $userenv = C4::Context->userenv
83
  or BAIL_OUT("No userenv");
84
67
85
# Make a borrower for testing
68
# Make a borrower for testing
86
my %data = (
69
my %data = (
(-)a/t/db_dependent/Members/Attributes.t (-3 / +3 lines)
Lines 24-29 use C4::Members; Link Here
24
use C4::Members::AttributeTypes;
24
use C4::Members::AttributeTypes;
25
use Koha::Database;
25
use Koha::Database;
26
use t::lib::TestBuilder;
26
use t::lib::TestBuilder;
27
use t::lib::Mocks;
27
28
28
use Test::More tests => 48;
29
use Test::More tests => 48;
29
30
Lines 52-59 my $patron = $builder->build( Link Here
52
        }
53
        }
53
    }
54
    }
54
);
55
);
55
C4::Context->_new_userenv('DUMMY SESSION');
56
t::lib::Mocks::mock_userenv({ branchcode => $library->{branchcode} });
56
C4::Context->set_userenv(123, 'userid', 'usercnum', 'First name', 'Surname', $library->{branchcode}, 'My Library', 0);
57
my $borrowernumber = $patron->{borrowernumber};
57
my $borrowernumber = $patron->{borrowernumber};
58
58
59
my $attribute_type1 = C4::Members::AttributeTypes->new('my code1', 'my description1');
59
my $attribute_type1 = C4::Members::AttributeTypes->new('my code1', 'my description1');
Lines 209-215 $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowern Link Here
209
is( @$borrower_attributes, 1, 'DeleteBorrowerAttribute deletes a borrower attribute' );
209
is( @$borrower_attributes, 1, 'DeleteBorrowerAttribute deletes a borrower attribute' );
210
210
211
# Regression tests for bug 16504
211
# Regression tests for bug 16504
212
C4::Context->set_userenv(123, 'userid', 'usercnum', 'First name', 'Surname', $new_library->{branchcode}, 'My Library', 0);
212
t::lib::Mocks::mock_userenv({ branchcode => $new_library->{branchcode} });
213
my $another_patron = $builder->build(
213
my $another_patron = $builder->build(
214
    {   source => 'Borrower',
214
    {   source => 'Borrower',
215
        value  => {
215
        value  => {
(-)a/t/db_dependent/Patron/Borrower_Discharge.t (-14 / +4 lines)
Lines 29-34 use Koha::Patron::Discharge; Link Here
29
use Koha::Database;
29
use Koha::Database;
30
30
31
use t::lib::TestBuilder;
31
use t::lib::TestBuilder;
32
use t::lib::Mocks;
32
33
33
my $schema  = Koha::Database->new->schema;
34
my $schema  = Koha::Database->new->schema;
34
$schema->storage->txn_begin;
35
$schema->storage->txn_begin;
Lines 51-57 my $patron = $builder->build({ Link Here
51
    }
52
    }
52
});
53
});
53
my $p = Koha::Patrons->find( $patron->{borrowernumber} );
54
my $p = Koha::Patrons->find( $patron->{borrowernumber} );
54
set_logged_in_user( $p );
55
t::lib::Mocks::mock_userenv({ patron => $p });
55
56
56
my $patron2 = $builder->build({
57
my $patron2 = $builder->build({
57
    source => 'Borrower',
58
    source => 'Borrower',
Lines 138-144 subtest 'search_limited' => sub { Link Here
138
    my $group_2 = Koha::Library::Group->new( { title => 'TEST Group 2' } )->store;
139
    my $group_2 = Koha::Library::Group->new( { title => 'TEST Group 2' } )->store;
139
    # $patron and $patron2 are from the same library, $patron3 from another one
140
    # $patron and $patron2 are from the same library, $patron3 from another one
140
    # Logged in user is $patron, superlibrarian
141
    # Logged in user is $patron, superlibrarian
141
    set_logged_in_user( $p );
142
    t::lib::Mocks::mock_userenv({ patron => $p });
142
    Koha::Library::Group->new({ parent_id => $group_1->id,  branchcode => $patron->{branchcode} })->store();
143
    Koha::Library::Group->new({ parent_id => $group_1->id,  branchcode => $patron->{branchcode} })->store();
143
    Koha::Library::Group->new({ parent_id => $group_2->id,  branchcode => $patron3->{branchcode} })->store();
144
    Koha::Library::Group->new({ parent_id => $group_2->id,  branchcode => $patron3->{branchcode} })->store();
144
    Koha::Patron::Discharge::request({ borrowernumber => $patron->{borrowernumber} });
145
    Koha::Patron::Discharge::request({ borrowernumber => $patron->{borrowernumber} });
Lines 148-167 subtest 'search_limited' => sub { Link Here
148
    is( Koha::Patron::Discharge::count({pending => 1}), 3, 'With permission, all discharges are visible' );
149
    is( Koha::Patron::Discharge::count({pending => 1}), 3, 'With permission, all discharges are visible' );
149
150
150
    # With patron 3 logged in, only discharges from their group are visible
151
    # With patron 3 logged in, only discharges from their group are visible
151
    set_logged_in_user( $p3 );
152
    t::lib::Mocks::mock_userenv({ patron => $p3 });
152
    is( scalar( Koha::Patron::Discharge::get_pendings), 1, 'Without permission, only discharge from our group are visible' );
153
    is( scalar( Koha::Patron::Discharge::get_pendings), 1, 'Without permission, only discharge from our group are visible' );
153
    is( Koha::Patron::Discharge::count({pending => 1}), 1, 'Without permission, only discharge from our group are visible' );
154
    is( Koha::Patron::Discharge::count({pending => 1}), 1, 'Without permission, only discharge from our group are visible' );
154
};
155
};
155
156
156
$schema->storage->txn_rollback;
157
$schema->storage->txn_rollback;
157
158
sub set_logged_in_user {
159
    my ($patron) = @_;
160
    C4::Context->set_userenv(
161
        $patron->borrowernumber, $patron->userid,
162
        $patron->cardnumber,     'firstname',
163
        'surname',               $patron->library->branchcode,
164
        'Midway Public Library', $patron->flags,
165
        '',                      ''
166
    );
167
}
(-)a/t/db_dependent/Patron/Borrower_PrevCheckout.t (-7 / +1 lines)
Lines 44-57 my $inheritCatCode = $builder->build({ Link Here
44
# Create context for some tests late on in the file.
44
# Create context for some tests late on in the file.
45
my $library = $builder->build({ source => 'Branch' });
45
my $library = $builder->build({ source => 'Branch' });
46
my $staff = $builder->build({source => 'Borrower'});
46
my $staff = $builder->build({source => 'Borrower'});
47
my @USERENV = (
48
    $staff->{borrowernumber}, 'test', 'MASTERTEST', 'firstname', $library->{branchcode},
49
    $library->{branchcode}, 'email@example.org'
50
);
51
C4::Context->_new_userenv('DUMMY_SESSION_ID');
52
C4::Context->set_userenv(@USERENV);
53
BAIL_OUT("No userenv") unless C4::Context->userenv;
54
47
48
t::lib::Mocks::mock_userenv({ branchcode => $library->{branchcode} });
55
49
56
# wants_check_for_previous_checkout
50
# wants_check_for_previous_checkout
57
51
(-)a/t/db_dependent/PatronLists.t (-2 / +2 lines)
Lines 19-24 use Modern::Perl; Link Here
19
19
20
use Test::More tests => 9;
20
use Test::More tests => 9;
21
use t::lib::TestBuilder;
21
use t::lib::TestBuilder;
22
use t::lib::Mocks;
22
23
23
use Koha::Database;
24
use Koha::Database;
24
use Koha::List::Patron
25
use Koha::List::Patron
Lines 29-36 $schema->storage->txn_begin; Link Here
29
30
30
my $builder = t::lib::TestBuilder->new;
31
my $builder = t::lib::TestBuilder->new;
31
32
32
C4::Context->_new_userenv('DUMMY SESSION');
33
t::lib::Mocks::mock_userenv();
33
C4::Context->set_userenv( 0 ); # Koha::List::Patron only needs a number
34
34
35
# Create 10 sample borrowers
35
# Create 10 sample borrowers
36
my @borrowers = ();
36
my @borrowers = ();
(-)a/t/db_dependent/Reserves.t (-3 / +1 lines)
Lines 77-85 my $category_2 = $builder->build({ source => 'Category' })->{ categorycode }; Link Here
77
my $itemtype = $builder->build(
77
my $itemtype = $builder->build(
78
    { source => 'Itemtype', value => { notforloan => undef } } )->{itemtype};
78
    { source => 'Itemtype', value => { notforloan => undef } } )->{itemtype};
79
79
80
C4::Context->set_userenv(
80
t::lib::Mocks::mock_userenv({ branchcode => $branch_1 });
81
    undef, undef, undef, undef, undef, undef, $branch_1
82
);
83
81
84
# Create a helper biblio
82
# Create a helper biblio
85
my $bib = MARC::Record->new();
83
my $bib = MARC::Record->new();
(-)a/t/db_dependent/Template/Plugin/Branches.t (-2 / +1 lines)
Lines 59-66 is($name, '', 'received empty string as name of NULL/undefined library code'); Link Here
59
$library = $plugin->GetLoggedInBranchcode();
59
$library = $plugin->GetLoggedInBranchcode();
60
is($library, '', 'no active library if there is no active user session');
60
is($library, '', 'no active library if there is no active user session');
61
61
62
C4::Context->_new_userenv('DUMMY_SESSION_ID');
62
t::lib::Mocks::mock_userenv({ branchcode => 'MYLIBRARY' });
63
C4::Context->set_userenv(123, 'userid', 'usercnum', 'First name', 'Surname', 'MYLIBRARY', 'My Library', 0);
64
$library = $plugin->GetLoggedInBranchcode();
63
$library = $plugin->GetLoggedInBranchcode();
65
is($library, 'MYLIBRARY', 'GetLoggedInBranchcode() returns active library');
64
is($library, 'MYLIBRARY', 'GetLoggedInBranchcode() returns active library');
66
65
(-)a/t/db_dependent/Utils/Datatables_Members.t (-13 / +1 lines)
Lines 45-51 my $library = $builder->build({ Link Here
45
});
45
});
46
46
47
my $patron = $builder->build_object({ class => 'Koha::Patrons', value => { flags => 1 } });
47
my $patron = $builder->build_object({ class => 'Koha::Patrons', value => { flags => 1 } });
48
set_logged_in_user( $patron );
48
t::lib::Mocks::mock_userenv({ patron => $patron });
49
49
50
my $branchcode=$library->{branchcode};
50
my $branchcode=$library->{branchcode};
51
51
Lines 463-477 subtest 'ExtendedPatronAttributes' => sub { Link Here
463
463
464
# End
464
# End
465
$schema->storage->txn_rollback;
465
$schema->storage->txn_rollback;
466
467
sub set_logged_in_user {
468
    my ($patron) = @_;
469
    C4::Context->_new_userenv('xxx');
470
    C4::Context->set_userenv(
471
        $patron->borrowernumber, $patron->userid,
472
        $patron->cardnumber,     'firstname',
473
        'surname',               $patron->library->branchcode,
474
        'Midway Public Library', $patron->flags,
475
        '',                      ''
476
    );
477
}
(-)a/t/db_dependent/Utils/Datatables_Virtualshelves.t (-3 / +5 lines)
Lines 27-32 use Koha::Patrons; Link Here
27
use Koha::Patron::Categories;
27
use Koha::Patron::Categories;
28
use Koha::Virtualshelves;
28
use Koha::Virtualshelves;
29
29
30
use t::lib::Mocks;
31
30
use_ok( "C4::Utils::DataTables::VirtualShelves" );
32
use_ok( "C4::Utils::DataTables::VirtualShelves" );
31
33
32
my $schema = Koha::Database->new->schema;
34
my $schema = Koha::Database->new->schema;
Lines 77-83 my %john_smith = ( Link Here
77
    userid       => 'john.smith',
79
    userid       => 'john.smith',
78
);
80
);
79
81
80
$john_doe{borrowernumber} = Koha::Patron->new( \%john_doe )->store->borrowernumber;
82
my $john_doe_patron = Koha::Patron->new( \%john_doe )->store;
83
$john_doe{borrowernumber} = $john_doe_patron->borrowernumber;
81
$jane_doe{borrowernumber} = Koha::Patron->new( \%jane_doe )->store->borrowernumber;
84
$jane_doe{borrowernumber} = Koha::Patron->new( \%jane_doe )->store->borrowernumber;
82
$john_smith{borrowernumber} = Koha::Patron->new( \%john_smith )->store->borrowernumber;
85
$john_smith{borrowernumber} = Koha::Patron->new( \%john_smith )->store->borrowernumber;
83
86
Lines 179-186 my %dt_params = ( Link Here
179
);
182
);
180
my $search_results;
183
my $search_results;
181
184
182
C4::Context->_new_userenv ('DUMMY_SESSION_ID');
185
t::lib::Mocks::mock_userenv({ patron => $john_doe_patron });
183
C4::Context->set_userenv($john_doe{borrowernumber}, $john_doe{userid}, 'usercnum', 'First name', 'Surname', 'MYLIBRARY', 'My Library', 0);
184
186
185
# Search private lists by title
187
# Search private lists by title
186
$search_results = C4::Utils::DataTables::VirtualShelves::search({
188
$search_results = C4::Utils::DataTables::VirtualShelves::search({
(-)a/t/db_dependent/rollingloans.t (-3 / +1 lines)
Lines 20-28 my $builder = t::lib::TestBuilder->new; Link Here
20
$builder->build({ source => 'Branch', value => { branchcode => 'CPL' } })
20
$builder->build({ source => 'Branch', value => { branchcode => 'CPL' } })
21
    unless Koha::Libraries->find('CPL');
21
    unless Koha::Libraries->find('CPL');
22
22
23
C4::Context->_new_userenv(1234567);
23
t::lib::Mocks::mock_userenv({ branchcode => 'CPL' });
24
C4::Context->set_userenv(91, 'CLIstaff', '23529001223661', 'CPL',
25
                         'CPL', 'CPL', '', 'cc@cscnet.co.uk');
26
24
27
t::lib::Mocks::mock_preference('BlockReturnOfWithdrawnItems',0);
25
t::lib::Mocks::mock_preference('BlockReturnOfWithdrawnItems',0);
28
my $test_patron = '23529001223651';
26
my $test_patron = '23529001223651';
(-)a/t/lib/Mocks.pm (-1 / +25 lines)
Lines 40-43 sub mock_preference { Link Here
40
    });
40
    });
41
}
41
}
42
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 ( $branchprinter, $shibboleth );
64
65
    C4::Context->set_userenv($usernum, $userid, $cardnumber, $firstname, $surname, $branchcode, $branchname, $flags, $emailaddress, $branchprinter, $shibboleth );
66
}
67
43
1;
68
1;
44
- 

Return to bug 21817