From a9576a1adfeb9425e768709ac7f2a4313d7be3c7 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Mon, 12 Nov 2018 18:21:58 -0300 Subject: [PATCH] Bug 21817: Centralize the mock of userenv from tests This patch adds a new method mock_userenv from t::lib::Mocks in order to simplify the mock of the userenv. Test plan: prove all the test files modified by this patch --- t/Budgets/CanUserModifyBudget.t | 18 ++----- t/Budgets/CanUserUseBudget.t | 13 ++--- t/Circulation_barcodedecode.t | 4 +- t/db_dependent/Acquisition/NewOrder.t | 5 +- t/db_dependent/ArticleRequests.t | 16 +----- t/db_dependent/Bookseller.t | 6 +-- t/db_dependent/Budgets.t | 18 +++---- t/db_dependent/Circulation.t | 5 +- t/db_dependent/Circulation/IsItemIssued.t | 10 +--- .../Circulation/IssuingRules/maxsuspensiondays.t | 10 +--- t/db_dependent/Circulation/MarkIssueReturned.t | 3 +- t/db_dependent/Circulation/SwitchOnSiteCheckouts.t | 3 +- t/db_dependent/Circulation/TooMany.t | 4 +- t/db_dependent/Circulation/issue.t | 31 ++++-------- t/db_dependent/DecreaseLoanHighHolds.t | 4 +- .../Holds/DisallowHoldIfItemsAvailable.t | 4 +- t/db_dependent/Holds/RevertWaitingStatus.t | 8 +-- t/db_dependent/Koha/Items.t | 3 +- t/db_dependent/Koha/Object.t | 5 +- t/db_dependent/Koha/Patron/Modifications.t | 15 +----- t/db_dependent/Koha/Patrons.t | 59 +++++++--------------- t/db_dependent/Koha/Reviews.t | 14 +---- t/db_dependent/Labels/t_Label.t | 3 +- t/db_dependent/Letters.t | 18 +++---- t/db_dependent/Letters/TemplateToolkit.t | 4 +- t/db_dependent/MarcModificationTemplates.t | 19 +------ t/db_dependent/Members.t | 19 +------ t/db_dependent/Members/Attributes.t | 6 +-- t/db_dependent/Patron/Borrower_Discharge.t | 18 ++----- t/db_dependent/Patron/Borrower_PrevCheckout.t | 8 +-- t/db_dependent/PatronLists.t | 4 +- t/db_dependent/Reserves.t | 4 +- t/db_dependent/Template/Plugin/Branches.t | 3 +- t/db_dependent/Utils/Datatables_Members.t | 14 +---- t/db_dependent/Utils/Datatables_Virtualshelves.t | 8 +-- t/db_dependent/rollingloans.t | 4 +- t/lib/Mocks.pm | 25 +++++++++ 37 files changed, 130 insertions(+), 285 deletions(-) diff --git a/t/Budgets/CanUserModifyBudget.t b/t/Budgets/CanUserModifyBudget.t index df2a7d7..91f4c77 100644 --- a/t/Budgets/CanUserModifyBudget.t +++ b/t/Budgets/CanUserModifyBudget.t @@ -4,11 +4,11 @@ use Modern::Perl; use Test::More tests => 134; use C4::Budgets; +use t::lib::Mocks; # Avoid "redefined subroutine" warnings local $SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /redefined/ }; *C4::Budgets::GetBudgetUsers = \&Mock_GetBudgetUsers; -*C4::Context::userenv = \&Mock_userenv; my %budgetusers = ( 1 => [], @@ -133,8 +133,6 @@ my $budget16 = { budget_branchcode => 'B1', }; -my $userenv = {}; - ok( !CanUserModifyBudget( 0, undef, {} ), "CanUserModifyBudget evaluates to false if DB user is passed" ); ok (CanUserModifyBudget($borrower1, $budget1, {superlibrarian => 1})); @@ -150,7 +148,7 @@ ok (!CanUserModifyBudget($borrower1, $budget1, {acquisition => 0})); my $flags = {acquisition => {budget_modify => 1}}; -$userenv->{branch} = 'B1'; +t::lib::Mocks::mock_userenv({ branchcode => 'B1' }); # Restriction is 'none' ok (CanUserModifyBudget($borrower1, $budget1, $flags)); @@ -192,8 +190,7 @@ ok (!CanUserModifyBudget($borrower2, $budget14, $flags)); # No owner and user li ok (CanUserModifyBudget($borrower2, $budget15, $flags)); ok (!CanUserModifyBudget($borrower2, $budget16, $flags)); - -$userenv->{branch} = 'B2'; +t::lib::Mocks::mock_userenv({ branchcode => 'B2' }); # Restriction is 'none' ok (CanUserModifyBudget($borrower1, $budget1, $flags)); @@ -240,7 +237,7 @@ ok (!CanUserModifyBudget($borrower2, $budget16, $flags)); # All tests should failed $flags = {acquisition => {order_manage => 1}}; -$userenv->{branch} = 'B1'; +t::lib::Mocks::mock_userenv({ branchcode => 'B1' }); # Restriction is 'none' ok (!CanUserModifyBudget($borrower1, $budget1, $flags)); @@ -283,7 +280,7 @@ ok (!CanUserModifyBudget($borrower2, $budget15, $flags)); ok (!CanUserModifyBudget($borrower2, $budget16, $flags)); -$userenv->{branch} = 'B2'; +t::lib::Mocks::mock_userenv({ branchcode => 'B2' }); # Restriction is 'none' ok (!CanUserModifyBudget($borrower1, $budget1, $flags)); @@ -334,8 +331,3 @@ sub Mock_GetBudgetUsers { return @{ $budgetusers{$budget_id} }; } - -# C4::Context->userenv -sub Mock_userenv { - return $userenv; -} diff --git a/t/Budgets/CanUserUseBudget.t b/t/Budgets/CanUserUseBudget.t index fc905f4..9be23a3 100644 --- a/t/Budgets/CanUserUseBudget.t +++ b/t/Budgets/CanUserUseBudget.t @@ -4,12 +4,12 @@ use Modern::Perl; use Test::More tests => 70; use C4::Budgets; +use t::lib::Mocks; # Avoid "redefined subroutine" warnings local $SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /redefined/ }; *C4::Budgets::GetBudgetUsers = \&Mock_GetBudgetUsers; -*C4::Context::userenv = \&Mock_userenv; my %budgetusers = ( 1 => [], @@ -134,8 +134,6 @@ my $budget16 = { budget_branchcode => 'B1', }; -my $userenv = {}; - ok( !CanUserUseBudget( 0, undef, {} ), "CanUserUseBudget evaluates to false if DB user is passed" ); ok (CanUserUseBudget($borrower1, $budget1, {superlibrarian => 1})); @@ -151,7 +149,7 @@ ok (!CanUserUseBudget($borrower1, $budget1, {acquisition => 0})); my $flags = {acquisition => {order_manage => 1}}; -$userenv->{branch} = 'B1'; +t::lib::Mocks::mock_userenv({ branchcode => 'B1' }); # Restriction is 'none' ok (CanUserUseBudget($borrower1, $budget1, $flags)); @@ -194,7 +192,7 @@ ok (CanUserUseBudget($borrower2, $budget15, $flags)); ok (!CanUserUseBudget($borrower2, $budget16, $flags)); -$userenv->{branch} = 'B2'; +t::lib::Mocks::mock_userenv({ branchcode => 'B2' }); # Restriction is 'none' ok (CanUserUseBudget($borrower1, $budget1, $flags)); @@ -245,8 +243,3 @@ sub Mock_GetBudgetUsers { return @{ $budgetusers{$budget_id} }; } - -# C4::Context->userenv -sub Mock_userenv { - return $userenv; -} diff --git a/t/Circulation_barcodedecode.t b/t/Circulation_barcodedecode.t index 8715661..dc6fe96 100644 --- a/t/Circulation_barcodedecode.t +++ b/t/Circulation_barcodedecode.t @@ -20,11 +20,11 @@ use Modern::Perl; use Test::More tests => 26; use C4::Context; +use t::lib::Mocks; use_ok( 'C4::Circulation' ); -C4::Context->_new_userenv(123456); -C4::Context->set_userenv(1,'kmkale' , 1, 'km', 'kale' , 'IMS', 'IMS Branch DEscription', 0, 'kmkale@anantcorp.com'); +t::lib::Mocks::mock_userenv({ branchcode => 'IMS' }); our %inputs = ( cuecat => ["26002315", '.C3nZC3nZC3nYD3b6ENnZCNnY.fHmc.C3D1Dxr2C3nZE3n7.', ".C3nZC3nZC3nYD3b6ENnZCNnY.fHmc.C3D1Dxr2C3nZE3n7.\r\n", diff --git a/t/db_dependent/Acquisition/NewOrder.t b/t/db_dependent/Acquisition/NewOrder.t index b17dcec..96a2d72 100644 --- a/t/db_dependent/Acquisition/NewOrder.t +++ b/t/db_dependent/Acquisition/NewOrder.t @@ -11,15 +11,14 @@ use Koha::Database; use Koha::DateUtils qw( dt_from_string output_pref ); use Koha::Acquisition::Booksellers; use Koha::Acquisition::Orders; +use t::lib::Mocks; my $schema = Koha::Database->new()->schema(); $schema->storage->txn_begin(); my $dbh = C4::Context->dbh; $dbh->{RaiseError} = 1; -C4::Context->_new_userenv('xxx'); -C4::Context->set_userenv(42, 'ordercreator', '0042', 'Order', 'Creator', 'CPL', - 0, 'ordercreator@example.com'); +t::lib::Mocks::mock_userenv({ branchcode => 'CPL' }); my $bookseller = Koha::Acquisition::Bookseller->new( { diff --git a/t/db_dependent/ArticleRequests.t b/t/db_dependent/ArticleRequests.t index 884c2e2..84ddbde 100755 --- a/t/db_dependent/ArticleRequests.t +++ b/t/db_dependent/ArticleRequests.t @@ -207,16 +207,15 @@ $rule->delete(); subtest 'search_limited' => sub { plan tests => 2; - C4::Context->_new_userenv('xxx'); my $nb_article_requests = Koha::ArticleRequests->count; my $group_1 = Koha::Library::Group->new( { title => 'TEST Group 1' } )->store; my $group_2 = Koha::Library::Group->new( { title => 'TEST Group 2' } )->store; Koha::Library::Group->new({ parent_id => $group_1->id, branchcode => $patron->branchcode })->store(); Koha::Library::Group->new({ parent_id => $group_2->id, branchcode => $patron_2->branchcode })->store(); - set_logged_in_user( $patron ); # Is superlibrarian + t::lib::Mocks::mock_userenv( { patron => $patron } ); # Is superlibrarian is( Koha::ArticleRequests->search_limited->count, $nb_article_requests, 'Koha::ArticleRequests->search_limited should return all article requests for superlibrarian' ); - set_logged_in_user( $patron_2 ); # Is restricted + t::lib::Mocks::mock_userenv( { patron => $patron_2 } ); # Is restricted is( Koha::ArticleRequests->search_limited->count, 0, 'Koha::ArticleRequests->search_limited should not return all article requests for restricted patron' ); }; @@ -244,14 +243,3 @@ subtest 'may_article_request' => sub { }; $schema->storage->txn_rollback(); - -sub set_logged_in_user { - my ($patron) = @_; - C4::Context->set_userenv( - $patron->borrowernumber, $patron->userid, - $patron->cardnumber, 'firstname', - 'surname', $patron->library->branchcode, - 'Midway Public Library', $patron->flags, - '', '' - ); -} diff --git a/t/db_dependent/Bookseller.t b/t/db_dependent/Bookseller.t index 48b2cef..9e5a193 100644 --- a/t/db_dependent/Bookseller.t +++ b/t/db_dependent/Bookseller.t @@ -639,9 +639,7 @@ ok( exists( $suppliers{$id_supplier1} ), "Supplier1 has late orders and $daysago10==$daysago10 " ) ; -C4::Context->_new_userenv('DUMMY SESSION'); -C4::Context->set_userenv(0,0,0,'firstname','surname', 'BRANCH1', 'Library 1', 0, '', ''); -my $userenv = C4::Context->userenv; +t::lib::Mocks::mock_userenv({ flags => 0 }); my $module = Test::MockModule->new('C4::Auth'); $module->mock( @@ -672,7 +670,7 @@ is( ); # don the cape and turn into Superlibrarian! -C4::Context->set_userenv(0,0,0,'firstname','surname', 'BRANCH1', 'Library 1', 1, '', ''); +t::lib::Mocks::mock_userenv({ flags => 1 }); @subscriptions = SearchSubscriptions({expiration_date => '2013-12-31'}); is( scalar(grep { !$_->{cannotdisplay} } @subscriptions ), diff --git a/t/db_dependent/Budgets.t b/t/db_dependent/Budgets.t index 3ca1a9d..6e4ad2c 100755 --- a/t/db_dependent/Budgets.t +++ b/t/db_dependent/Budgets.t @@ -15,6 +15,7 @@ use Koha::Acquisition::Funds; use Koha::Patrons; use t::lib::TestBuilder; +use t::lib::Mocks; use Koha::DateUtils; use YAML; @@ -30,11 +31,13 @@ my $library = $builder->build({ source => 'Branch', }); -# Mock userenv -local $SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /redefined/ }; -my $userenv; -*C4::Context::userenv = \&Mock_userenv; -$userenv = { flags => 1, id => 'my_userid', branch => $library->{branchcode} }; +t::lib::Mocks::mock_userenv( + { + flags => 1, + userid => 'my_userid', + branch => $library->{branchcode}, + } +); # # Budget Periods : @@ -953,8 +956,3 @@ sub _get_budgetname_by_id { @$budgets; return $budget_name; } - -# C4::Context->userenv -sub Mock_userenv { - return $userenv; -} diff --git a/t/db_dependent/Circulation.t b/t/db_dependent/Circulation.t index 3b1da09..fd729ba 100755 --- a/t/db_dependent/Circulation.t +++ b/t/db_dependent/Circulation.t @@ -133,8 +133,7 @@ is( ); # Now, set a userenv -C4::Context->_new_userenv('xxx'); -C4::Context->set_userenv(0,0,0,'firstname','surname', $library2->{branchcode}, 'Midway Public Library', '', '', ''); +t::lib::Mocks::mock_userenv({ branchcode => $library2->{branchcode} }); is(C4::Context->userenv->{branch}, $library2->{branchcode}, 'userenv set'); # Userenv set, PickupLibrary @@ -2750,7 +2749,7 @@ $cache->clear_from_cache('single_holidays'); sub set_userenv { my ( $library ) = @_; - C4::Context->set_userenv(0,0,0,'firstname','surname', $library->{branchcode}, $library->{branchname}, '', '', ''); + t::lib::Mocks::mock_userenv({ branchcode => $library->{branchcode} }); } sub str { diff --git a/t/db_dependent/Circulation/IsItemIssued.t b/t/db_dependent/Circulation/IsItemIssued.t index ab26f0c..c893923 100644 --- a/t/db_dependent/Circulation/IsItemIssued.t +++ b/t/db_dependent/Circulation/IsItemIssued.t @@ -28,6 +28,7 @@ use Koha::DateUtils; use Koha::Patrons; use t::lib::TestBuilder; +use t::lib::Mocks; use MARC::Record; @@ -39,14 +40,7 @@ my $library = $builder->build({ source => 'Branch' }); my $itemtype = $builder->build({ source => 'Itemtype' })->{itemtype}; my $patron_category = $builder->build({ source => 'Category' }); -C4::Context->_new_userenv('DUMMY SESSION'); -C4::Context->set_userenv( - undef, undef, undef, undef, undef, - $library->{branchcode}, - $library->{branchname} -); - - +t::lib::Mocks::mock_userenv({ branchcode => $library->{branchcode} }); my $borrowernumber = Koha::Patron->new({ firstname => 'my firstname', diff --git a/t/db_dependent/Circulation/IssuingRules/maxsuspensiondays.t b/t/db_dependent/Circulation/IssuingRules/maxsuspensiondays.t index 86f64fd..d260226 100644 --- a/t/db_dependent/Circulation/IssuingRules/maxsuspensiondays.t +++ b/t/db_dependent/Circulation/IssuingRules/maxsuspensiondays.t @@ -14,6 +14,7 @@ use Koha::Patron::Debarments qw( GetDebarments DelDebarment ); use Koha::Patrons; use t::lib::TestBuilder; +use t::lib::Mocks; my $schema = Koha::Database->schema; $schema->storage->txn_begin; @@ -25,9 +26,7 @@ my $branchcode = $builder->build({ source => 'Branch' })->{branchcode}; my $itemtype = $builder->build({ source => 'Itemtype' })->{itemtype}; my $patron_category = $builder->build({ source => 'Category' }); -local $SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /redefined/ }; -my $userenv->{branch} = $branchcode; -*C4::Context::userenv = \&Mock_userenv; +t::lib::Mocks::mock_userenv({ branchcode => $branchcode }); # Test without maxsuspensiondays set Koha::IssuingRules->search->delete; @@ -103,8 +102,3 @@ is( DelDebarment( $debarments->[0]->{borrower_debarment_id} ); $schema->storage->txn_rollback; - -# C4::Context->userenv -sub Mock_userenv { - return $userenv; -} diff --git a/t/db_dependent/Circulation/MarkIssueReturned.t b/t/db_dependent/Circulation/MarkIssueReturned.t index 452707a..7f2289f 100644 --- a/t/db_dependent/Circulation/MarkIssueReturned.t +++ b/t/db_dependent/Circulation/MarkIssueReturned.t @@ -36,8 +36,7 @@ my $builder = t::lib::TestBuilder->new; my $library = $builder->build({ source => 'Branch' }); -C4::Context->_new_userenv('xxx'); -C4::Context->set_userenv(0,0,0,'firstname','surname', $library->{branchcode}, $library->{branchname}, '', '', ''); +t::lib::Mocks::mock_userenv({ branchcode => $library->{branchcode} }); my $patron_category = $builder->build({ source => 'Category', value => { category_type => 'P', enrolmentfee => 0 } }); my $patron = $builder->build({ source => 'Borrower', value => { branchcode => $library->{branchcode}, categorycode => $patron_category->{categorycode} } } ); diff --git a/t/db_dependent/Circulation/SwitchOnSiteCheckouts.t b/t/db_dependent/Circulation/SwitchOnSiteCheckouts.t index 6268561..335f1c2 100644 --- a/t/db_dependent/Circulation/SwitchOnSiteCheckouts.t +++ b/t/db_dependent/Circulation/SwitchOnSiteCheckouts.t @@ -100,8 +100,7 @@ my $issuingrule = $builder->build({ }, }); -C4::Context->_new_userenv ('DUMMY_SESSION_ID'); -C4::Context->set_userenv($patron->borrowernumber, $patron->userid, 'usercnum', 'First name', 'Surname', $branch->{branchcode}, 'My Library', 0); +t::lib::Mocks::mock_userenv({ patron => $patron }); t::lib::Mocks::mock_preference('AllowTooManyOverride', 0); diff --git a/t/db_dependent/Circulation/TooMany.t b/t/db_dependent/Circulation/TooMany.t index a7f19db..c88b200 100644 --- a/t/db_dependent/Circulation/TooMany.t +++ b/t/db_dependent/Circulation/TooMany.t @@ -83,8 +83,8 @@ my $item = $builder->build({ }, }); -C4::Context->_new_userenv ('DUMMY_SESSION_ID'); -C4::Context->set_userenv($patron->{borrowernumber}, $patron->{userid}, 'usercnum', 'First name', 'Surname', $branch->{branchcode}, 'My Library', 0); +my $patron_object = Koha::Patrons->find( $patron->{borrowernumber} ); +t::lib::Mocks::mock_userenv( { patron => $patron_object }); # TooMany return ($current_loan_count, $max_loans_allowed) or undef # CO = Checkout diff --git a/t/db_dependent/Circulation/issue.t b/t/db_dependent/Circulation/issue.t index f613f08..63e9488 100644 --- a/t/db_dependent/Circulation/issue.t +++ b/t/db_dependent/Circulation/issue.t @@ -148,31 +148,18 @@ my $borrower_id1 = Koha::Patron->new({ categorycode => $categorycode, branchcode => $branchcode_1 })->store->borrowernumber; -my $borrower_1 = Koha::Patrons->find( $borrower_id1 )->unblessed; +my $patron_1 = Koha::Patrons->find( $borrower_id1 ); +my $borrower_1 = $patron_1->unblessed; my $borrower_id2 = Koha::Patron->new({ firstname => 'firstname2', surname => 'surname2 ', categorycode => $categorycode, branchcode => $branchcode_2, })->store->borrowernumber; -my $borrower_2 = Koha::Patrons->find( $borrower_id2 )->unblessed; - -my @USERENV = ( - $borrower_id1, 'test', 'MASTERTEST', 'firstname', $branchcode_1, - $branchcode_1, 'email@example.org' -); - -my @USERENV_DIFFERENT_LIBRARY = ( - $borrower_id1, 'test', 'MASTERTEST', 'firstname', $branchcode_3, - $branchcode_3, 'email@example.org' -); +my $patron_2 = Koha::Patrons->find( $borrower_id2 ); +my $borrower_2 = $patron_2->unblessed; - -C4::Context->_new_userenv('DUMMY_SESSION_ID'); -C4::Context->set_userenv(@USERENV); - -my $userenv = C4::Context->userenv - or BAIL_OUT("No userenv"); +t::lib::Mocks::mock_userenv({ patron => $patron_1 }); #Begin Tests @@ -222,9 +209,13 @@ my $se = Test::MockModule->new( 'C4::Context' ); $se->mock( 'interface', sub {return 'intranet'}); # Let's renew this one at a different library for statistical purposes to test Bug 17781 -C4::Context->set_userenv(@USERENV_DIFFERENT_LIBRARY); +# Mocking userenv with a different branchcode +t::lib::Mocks::mock_userenv({ patron => $patron_2, branchcode => $branchcode_3 }); + my $datedue3 = AddRenewal( $borrower_id1, $item_id1, $branchcode_1, $datedue1, $daysago10 ); -C4::Context->set_userenv(@USERENV); + +# Restoring the userenv with the original branchcode +t::lib::Mocks::mock_userenv({ patron => $patron_1}); like( $datedue3, diff --git a/t/db_dependent/DecreaseLoanHighHolds.t b/t/db_dependent/DecreaseLoanHighHolds.t index a85048f..0ef46f3 100755 --- a/t/db_dependent/DecreaseLoanHighHolds.t +++ b/t/db_dependent/DecreaseLoanHighHolds.t @@ -51,9 +51,7 @@ my $library = $builder->build( { source => 'Branch' } ); my $category = $builder->build( { source => 'Category' } ); my $itemtype = $builder->build( { source => 'Itemtype' } )->{itemtype}; -# Set userenv -C4::Context->_new_userenv('xxx'); -C4::Context->set_userenv( 0, 0, 0, 'firstname', 'surname', $library->{branchcode}, 'Midway Public Library', '', '', '' ); +t::lib::Mocks::mock_userenv({ branchcode => $library->{branchcode} }); is( C4::Context->userenv->{branch}, $library->{branchcode}, 'userenv set' ); my $patron_category = $builder->build({ source => 'Category', value => { category_type => 'P', enrolmentfee => 0 } }); diff --git a/t/db_dependent/Holds/DisallowHoldIfItemsAvailable.t b/t/db_dependent/Holds/DisallowHoldIfItemsAvailable.t index 645616c..fc2d8fe 100755 --- a/t/db_dependent/Holds/DisallowHoldIfItemsAvailable.t +++ b/t/db_dependent/Holds/DisallowHoldIfItemsAvailable.t @@ -30,9 +30,7 @@ my $library2 = $builder->build({ }); -# Now, set a userenv -C4::Context->_new_userenv('xxx'); -C4::Context->set_userenv(0,0,0,'firstname','surname', $library1->{branchcode}, 'Midway Public Library', '', '', ''); +t::lib::Mocks::mock_userenv({ branchcode => $library1->{branchcode} }); my $bib_title = "Test Title"; diff --git a/t/db_dependent/Holds/RevertWaitingStatus.t b/t/db_dependent/Holds/RevertWaitingStatus.t index 9877e68..783006a 100755 --- a/t/db_dependent/Holds/RevertWaitingStatus.t +++ b/t/db_dependent/Holds/RevertWaitingStatus.t @@ -42,13 +42,7 @@ my $branchcode = $builder->build( { source => 'Branch' } )->{branchcode}; my $itemtype = $builder->build( { source => 'Itemtype', value => { notforloan => undef } } )->{itemtype}; -local $SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /redefined/ }; -*C4::Context::userenv = \&Mock_userenv; - -sub Mock_userenv { - my $userenv = { flags => 1, id => '1', branch => $branchcode }; - return $userenv; -} +t::lib::Mocks::mock_userenv({ flags => 1, userid => '1', branchcode => $branchcode }); my $borrowers_count = 3; diff --git a/t/db_dependent/Koha/Items.t b/t/db_dependent/Koha/Items.t index 87ebfb3..98e51b3 100644 --- a/t/db_dependent/Koha/Items.t +++ b/t/db_dependent/Koha/Items.t @@ -57,8 +57,7 @@ my $new_item_2 = Koha::Item->new( } )->store; -C4::Context->_new_userenv('xxx'); -C4::Context->set_userenv(0,0,0,'firstname','surname', $library->{branchcode}, 'Midway Public Library', '', '', ''); +t::lib::Mocks::mock_userenv({ branchcode => $library->{branchcode} }); like( $new_item_1->itemnumber, qr|^\d+$|, 'Adding a new item should have set the itemnumber' ); is( Koha::Items->search->count, $nb_of_items + 2, 'The 2 items should have been added' ); diff --git a/t/db_dependent/Koha/Object.t b/t/db_dependent/Koha/Object.t index e1812bc..ca0ef5f 100755 --- a/t/db_dependent/Koha/Object.t +++ b/t/db_dependent/Koha/Object.t @@ -36,6 +36,7 @@ use Scalar::Util qw( isvstring ); use Try::Tiny; use t::lib::TestBuilder; +use t::lib::Mocks; BEGIN { use_ok('Koha::Object'); @@ -315,8 +316,8 @@ subtest 'unblessed_all_relateds' => sub { # FIXME It's very painful to create an issue in tests! my $library = $builder->build_object( { class => 'Koha::Libraries' } ); - C4::Context->_new_userenv('xxx'); - C4::Context->set_userenv(0,0,0,'firstname','surname', $library->branchcode, 'Midway Public Library', '', '', ''); + t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode }); + my $patron_category = $builder->build( { source => 'Category', diff --git a/t/db_dependent/Koha/Patron/Modifications.t b/t/db_dependent/Koha/Patron/Modifications.t index 1888088..ed4c9b3 100755 --- a/t/db_dependent/Koha/Patron/Modifications.t +++ b/t/db_dependent/Koha/Patron/Modifications.t @@ -23,6 +23,7 @@ use Test::More tests => 6; use Test::Exception; use t::lib::TestBuilder; +use t::lib::Mocks; use Digest::MD5 qw( md5_base64 md5_hex ); use Try::Tiny; @@ -347,8 +348,7 @@ subtest 'pending_count() and pending() tests' => sub { is( $p2_pm_attribute_2->attribute, 'ciao', 'patron modification has the right attribute change' ); - C4::Context->_new_userenv('xxx'); - set_logged_in_user( $patron_1 ); + t::lib::Mocks::mock_userenv({ patron => $patron_1 }); is( Koha::Patron::Modifications->pending_count($library_1), 1, 'pending_count() correctly returns 1 if filtered by library' ); @@ -372,14 +372,3 @@ subtest 'pending_count() and pending() tests' => sub { $schema->storage->txn_rollback; }; - -sub set_logged_in_user { - my ($patron) = @_; - C4::Context->set_userenv( - $patron->borrowernumber, $patron->userid, - $patron->cardnumber, 'firstname', - 'surname', $patron->library->branchcode, - 'Midway Public Library', $patron->flags, - '', '' - ); -} diff --git a/t/db_dependent/Koha/Patrons.t b/t/db_dependent/Koha/Patrons.t index c552220..948479e 100644 --- a/t/db_dependent/Koha/Patrons.t +++ b/t/db_dependent/Koha/Patrons.t @@ -68,8 +68,7 @@ my $new_patron_2 = Koha::Patron->new( } )->store; -C4::Context->_new_userenv('xxx'); -set_logged_in_user( $new_patron_1 ); +t::lib::Mocks::mock_userenv({ patron => $new_patron_1 }); is( Koha::Patrons->search->count, $nb_of_patrons + 2, 'The 2 patrons should have been added' ); @@ -561,8 +560,7 @@ subtest 'checkouts + pending_checkouts + get_overdues + old_checkouts' => sub { # Not sure how this is useful, but AddIssue pass this variable to different other subroutines $patron = Koha::Patrons->find( $patron->borrowernumber )->unblessed; - my $module = new Test::MockModule('C4::Context'); - $module->mock( 'userenv', sub { { branch => $library->{branchcode} } } ); + t::lib::Mocks::mock_userenv({ branchcode => $library->{branchcode} }); AddIssue( $patron, $item_1->{barcode}, DateTime->now->subtract( days => 1 ) ); AddIssue( $patron, $item_2->{barcode}, DateTime->now->subtract( days => 5 ) ); @@ -595,7 +593,6 @@ subtest 'checkouts + pending_checkouts + get_overdues + old_checkouts' => sub { # Clean stuffs Koha::Checkouts->search( { borrowernumber => $patron->borrowernumber } )->delete; $patron->delete; - $module->unmock('userenv'); }; subtest 'get_routing_lists' => sub { @@ -844,19 +841,12 @@ subtest 'search_patrons_to_anonymise & anonymise_issue_history' => sub { # TODO create a subroutine in t::lib::Mocks my $branch = $builder->build({ source => 'Branch' }); - my $userenv_patron = $builder->build({ - source => 'Borrower', - value => { branchcode => $branch->{branchcode} }, + my $userenv_patron = $builder->build_object({ + class => 'Koha::Patrons', + value => { branchcode => $branch->{branchcode}, flags => 0 }, }); - C4::Context->_new_userenv('DUMMY SESSION'); - C4::Context->set_userenv( - $userenv_patron->{borrowernumber}, - $userenv_patron->{userid}, - 'usercnum', 'First name', 'Surname', - $branch->{branchcode}, - $branch->{branchname}, - 0, - ); + t::lib::Mocks::mock_userenv({ patron => $userenv_patron }); + my $anonymous = $builder->build( { source => 'Borrower', }, ); t::lib::Mocks::mock_preference( 'AnonymousPatron', $anonymous->{borrowernumber} ); @@ -1052,7 +1042,7 @@ subtest 'search_patrons_to_anonymise & anonymise_issue_history' => sub { }; Koha::Patrons->find( $anonymous->{borrowernumber})->delete; - Koha::Patrons->find( $userenv_patron->{borrowernumber})->delete; + $userenv_patron->delete; # Reset IndependentBranches for further tests t::lib::Mocks::mock_preference('IndependentBranches', 0); @@ -1108,27 +1098,27 @@ subtest 'libraries_where_can_see_patrons + can_see_patron_infos + search_limited my @branchcodes; - set_logged_in_user( $patron_11_1 ); + t::lib::Mocks::mock_userenv({ patron => $patron_11_1 }); @branchcodes = $patron_11_1->libraries_where_can_see_patrons; is_deeply( \@branchcodes, [], q|patron_11_1 has view_borrower_infos_from_any_libraries => No restriction| ); - set_logged_in_user( $patron_11_2 ); + t::lib::Mocks::mock_userenv({ patron => $patron_11_2 }); @branchcodes = $patron_11_2->libraries_where_can_see_patrons; 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| ); - set_logged_in_user( $patron_21 ); + t::lib::Mocks::mock_userenv({ patron => $patron_21 }); @branchcodes = $patron_21->libraries_where_can_see_patrons; 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| ); }; subtest 'can_see_patron_infos' => sub { plan tests => 6; - set_logged_in_user( $patron_11_1 ); + t::lib::Mocks::mock_userenv({ patron => $patron_11_1 }); is( $patron_11_1->can_see_patron_infos( $patron_11_2 ), 1, q|patron_11_1 can see patron_11_2, from its library| ); is( $patron_11_1->can_see_patron_infos( $patron_12 ), 1, q|patron_11_1 can see patron_12, from its group| ); is( $patron_11_1->can_see_patron_infos( $patron_21 ), 1, q|patron_11_1 can see patron_11_2, from another group| ); - set_logged_in_user( $patron_11_2 ); + t::lib::Mocks::mock_userenv({ patron => $patron_11_2 }); is( $patron_11_2->can_see_patron_infos( $patron_11_1 ), 1, q|patron_11_2 can see patron_11_1, from its library| ); is( $patron_11_2->can_see_patron_infos( $patron_12 ), 1, q|patron_11_2 can see patron_12, from its group| ); is( $patron_11_2->can_see_patron_infos( $patron_21 ), 0, q|patron_11_2 can NOT see patron_21, from another group| ); @@ -1136,16 +1126,16 @@ subtest 'libraries_where_can_see_patrons + can_see_patron_infos + search_limited subtest 'search_limited' => sub { plan tests => 6; - set_logged_in_user( $patron_11_1 ); + t::lib::Mocks::mock_userenv({ patron => $patron_11_1 }); my $total_number_of_patrons = $nb_of_patrons + 4; #we added four in these tests is( Koha::Patrons->search->count, $total_number_of_patrons, 'Non-limited search should return all patrons' ); is( Koha::Patrons->search_limited->count, $total_number_of_patrons, 'patron_11_1 is allowed to see all patrons' ); - set_logged_in_user( $patron_11_2 ); + t::lib::Mocks::mock_userenv({ patron => $patron_11_2 }); is( Koha::Patrons->search->count, $total_number_of_patrons, 'Non-limited search should return all patrons'); 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' ); - set_logged_in_user( $patron_21 ); + t::lib::Mocks::mock_userenv({ patron => $patron_21 }); is( Koha::Patrons->search->count, $total_number_of_patrons, 'Non-limited search should return all patrons'); is( Koha::Patrons->search_limited->count, 1, 'patron_21 is not allowed to see patrons from other groups, only himself' ); }; @@ -1285,8 +1275,7 @@ subtest 'get_overdues' => sub { } ); - my $module = new Test::MockModule('C4::Context'); - $module->mock( 'userenv', sub { { branch => $library->{branchcode} } } ); + t::lib::Mocks::mock_preference({ branchcode => $library->{branchcode} }); AddIssue( $patron, $item_1->{barcode}, DateTime->now->subtract( days => 1 ) ); AddIssue( $patron, $item_2->{barcode}, DateTime->now->subtract( days => 5 ) ); @@ -1596,17 +1585,3 @@ subtest '->set_password' => sub { $schema->storage->txn_rollback; }; - - - -# TODO Move to t::lib::Mocks and reuse it! -sub set_logged_in_user { - my ($patron) = @_; - C4::Context->set_userenv( - $patron->borrowernumber, $patron->userid, - $patron->cardnumber, 'firstname', - 'surname', $patron->library->branchcode, - 'Midway Public Library', $patron->flags, - '', '' - ); -} diff --git a/t/db_dependent/Koha/Reviews.t b/t/db_dependent/Koha/Reviews.t index 8688789..9a51775 100644 --- a/t/db_dependent/Koha/Reviews.t +++ b/t/db_dependent/Koha/Reviews.t @@ -26,6 +26,7 @@ use Koha::Reviews; use Koha::Database; use t::lib::TestBuilder; +use t::lib::Mocks; my $schema = Koha::Database->new->schema; $schema->storage->txn_begin; @@ -74,7 +75,7 @@ subtest 'search_limited' => sub { my $group_2 = Koha::Library::Group->new( { title => 'TEST Group 2' } )->store; Koha::Library::Group->new({ parent_id => $group_1->id, branchcode => $patron_1->branchcode })->store(); Koha::Library::Group->new({ parent_id => $group_2->id, branchcode => $patron_2->branchcode })->store(); - set_logged_in_user( $patron_1 ); + t::lib::Mocks::mock_userenv( { patron => $patron_1 } ); is( Koha::Reviews->search->count, $nb_of_approved_reviews + 3, 'Koha::Reviews->search should return all reviews' ); is( Koha::Reviews->search_limited->count, $nb_of_approved_reviews + 2, 'Koha::Reviews->search_limited should return reviews depending on patron permissions' ); }; @@ -83,14 +84,3 @@ $retrieved_review_1_1->delete; is( Koha::Reviews->search->count, $nb_of_reviews + 2, 'Delete should have deleted the review' ); $schema->storage->txn_rollback; - -sub set_logged_in_user { - my ($patron) = @_; - C4::Context->set_userenv( - $patron->borrowernumber, $patron->userid, - $patron->cardnumber, 'firstname', - 'surname', $patron->library->branchcode, - 'Midway Public Library', $patron->flags, - '', '' - ); -} diff --git a/t/db_dependent/Labels/t_Label.t b/t/db_dependent/Labels/t_Label.t index 2d44b44..ad84c01 100644 --- a/t/db_dependent/Labels/t_Label.t +++ b/t/db_dependent/Labels/t_Label.t @@ -21,6 +21,7 @@ use Modern::Perl; use Test::More tests => 3; use t::lib::TestBuilder; +use t::lib::Mocks; use MARC::Record; use MARC::Field; @@ -56,7 +57,7 @@ my $itemtype = $builder->build( { source => 'Itemtype', value => { notforloan => undef } } ) ->{itemtype}; -C4::Context->set_userenv( undef, undef, undef, undef, undef, undef, $branch_1 ); +t::lib::Mocks::mock_userenv({ branchcode => $branch_1 }); # Create a helper biblio my $bib = MARC::Record->new(); diff --git a/t/db_dependent/Letters.t b/t/db_dependent/Letters.t index a664918..e656ddd 100644 --- a/t/db_dependent/Letters.t +++ b/t/db_dependent/Letters.t @@ -199,12 +199,7 @@ subtest 'getletter' => sub { is( $letter->{content}, $content, 'GetLetters gets the content correctly' ); is( $letter->{message_transport_type}, 'email', 'GetLetters gets the message type correctly' ); - my $context = Test::MockModule->new('C4::Context'); - $context->mock( 'userenv', sub { - return { - flags => 1, - branch => "anotherlib" } - }); + t::lib::Mocks::mock_userenv({ branchcode => "anotherlib", flags => 1 }); t::lib::Mocks::mock_preference('IndependentBranches', 1); $letter = C4::Letters::getletter('my module', 'my code', $library->{branchcode}, 'email'); @@ -216,8 +211,6 @@ subtest 'getletter' => sub { is( $letter->{title}, $title, 'GetLetters gets the title correctly' ); is( $letter->{content}, $content, 'GetLetters gets the content correctly' ); is( $letter->{message_transport_type}, 'email', 'GetLetters gets the message type correctly' ); - - $context->unmock('userenv'); }; @@ -392,6 +385,9 @@ if (C4::Context->preference('marcflavour') eq 'UNIMARC') { ); } +my $logged_in_user = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $library->{branchcode} }}); +t::lib::Mocks::mock_userenv({ patron => $logged_in_user }); + ($biblionumber, $biblioitemnumber) = AddBiblio($bib, ''); my $order = Koha::Acquisition::Order->new( { @@ -418,8 +414,6 @@ $bookseller->contacts->next->email('testemail@mydomain.com')->store; t::lib::Mocks::mock_preference( 'LetterLog', 'on' ); # SendAlerts needs branchemail or KohaAdminEmailAddress as sender -C4::Context->_new_userenv('DUMMY'); -C4::Context->set_userenv( 0, 0, 0, 'firstname', 'surname', $library->{branchcode}, 'My Library', 0, '', ''); t::lib::Mocks::mock_preference( 'KohaAdminEmailAddress', 'library@domain.com' ); { @@ -550,8 +544,8 @@ subtest 'SendAlerts - claimissue' => sub { t::lib::Mocks::mock_preference( 'LetterLog', 'on' ); # SendAlerts needs branchemail or KohaAdminEmailAddress as sender - C4::Context->_new_userenv('DUMMY'); - C4::Context->set_userenv( 0, 0, 0, 'firstname', 'surname', $library->{branchcode}, 'My Library', 0, '', ''); + t::lib::Mocks::mock_userenv({ branchcode => $library->{branchcode} }); + t::lib::Mocks::mock_preference( 'KohaAdminEmailAddress', 'library@domain.com' ); { diff --git a/t/db_dependent/Letters/TemplateToolkit.t b/t/db_dependent/Letters/TemplateToolkit.t index 2714317..49301eb 100644 --- a/t/db_dependent/Letters/TemplateToolkit.t +++ b/t/db_dependent/Letters/TemplateToolkit.t @@ -26,6 +26,7 @@ use Test::Warn; use MARC::Record; use t::lib::TestBuilder; +use t::lib::Mocks; use C4::Circulation; use C4::Letters; @@ -333,8 +334,7 @@ subtest 'regression tests' => sub { } )->store->unblessed; - C4::Context->_new_userenv('xxx'); - C4::Context->set_userenv(0,0,0,'firstname','surname', $library->{branchcode}, 'Midway Public Library', '', '', ''); + t::lib::Mocks::mock_userenv({ branchcode => $library->{branchcode} }); subtest 'ACQ_NOTIF_ON_RECEIV ' => sub { plan tests => 1; diff --git a/t/db_dependent/MarcModificationTemplates.t b/t/db_dependent/MarcModificationTemplates.t index a820ede9..ccd11bcb 100644 --- a/t/db_dependent/MarcModificationTemplates.t +++ b/t/db_dependent/MarcModificationTemplates.t @@ -2,6 +2,7 @@ use Modern::Perl; use Test::More tests => 115; +use t::lib::Mocks; use Koha::SimpleMARC; use_ok("MARC::Field"); @@ -158,18 +159,7 @@ is( GetModificationTemplateAction( $actions[3]->{mmta_id} ), undef, "fourth acti is( GetModificationTemplateActions( $template_id ), 0, "There is no action for deleted template" ); # ModifyRecordWithTemplate -my @USERENV = ( - 1, - 'test', - 'MASTERTEST', - 'Test', - 'Test', - 't', - 'Test', - 0, -); -C4::Context->_new_userenv ('DUMMY_SESSION_ID'); -C4::Context->set_userenv ( @USERENV ); +t::lib::Mocks::mock_userenv(); $template_id = AddModificationTemplate("new_template_test"); like( $template_id, qr|^\d+$|, "new template returns an id" ); @@ -554,8 +544,3 @@ sub expected_record_2 { $record->append_fields(@fields); return $record; } - -# C4::Context->userenv -sub Mock_userenv { - return { branchcode => 'CPL' }; -} diff --git a/t/db_dependent/Members.t b/t/db_dependent/Members.t index b878afb..3d6a041 100755 --- a/t/db_dependent/Members.t +++ b/t/db_dependent/Members.t @@ -63,24 +63,7 @@ my $PHONE = "555-12123"; # XXX should be randomised and checked against the database my $IMPOSSIBLE_CARDNUMBER = "XYZZZ999"; -#my ($usernum, $userid, $usercnum, $userfirstname, $usersurname, $userbranch, $branchname, $userflags, $emailaddress, $branchprinter)= @_; -my @USERENV = ( - 1, - 'test', - 'MASTERTEST', - 'Test', - 'Test', - 't', - 'Test', - 0, -); -my $BRANCH_IDX = 5; - -C4::Context->_new_userenv ('DUMMY_SESSION_ID'); -C4::Context->set_userenv ( @USERENV ); - -my $userenv = C4::Context->userenv - or BAIL_OUT("No userenv"); +t::lib::Mocks::mock_userenv(); # Make a borrower for testing my %data = ( diff --git a/t/db_dependent/Members/Attributes.t b/t/db_dependent/Members/Attributes.t index cd409ed..b30291d 100644 --- a/t/db_dependent/Members/Attributes.t +++ b/t/db_dependent/Members/Attributes.t @@ -24,6 +24,7 @@ use C4::Members; use C4::Members::AttributeTypes; use Koha::Database; use t::lib::TestBuilder; +use t::lib::Mocks; use Test::More tests => 48; @@ -52,8 +53,7 @@ my $patron = $builder->build( } } ); -C4::Context->_new_userenv('DUMMY SESSION'); -C4::Context->set_userenv(123, 'userid', 'usercnum', 'First name', 'Surname', $library->{branchcode}, 'My Library', 0); +t::lib::Mocks::mock_userenv({ branchcode => $library->{branchcode} }); my $borrowernumber = $patron->{borrowernumber}; my $attribute_type1 = C4::Members::AttributeTypes->new('my code1', 'my description1'); @@ -209,7 +209,7 @@ $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowern is( @$borrower_attributes, 1, 'DeleteBorrowerAttribute deletes a borrower attribute' ); # Regression tests for bug 16504 -C4::Context->set_userenv(123, 'userid', 'usercnum', 'First name', 'Surname', $new_library->{branchcode}, 'My Library', 0); +t::lib::Mocks::mock_userenv({ branchcode => $new_library->{branchcode} }); my $another_patron = $builder->build( { source => 'Borrower', value => { diff --git a/t/db_dependent/Patron/Borrower_Discharge.t b/t/db_dependent/Patron/Borrower_Discharge.t index ffd3370..3a5ddd9 100644 --- a/t/db_dependent/Patron/Borrower_Discharge.t +++ b/t/db_dependent/Patron/Borrower_Discharge.t @@ -29,6 +29,7 @@ use Koha::Patron::Discharge; use Koha::Database; use t::lib::TestBuilder; +use t::lib::Mocks; my $schema = Koha::Database->new->schema; $schema->storage->txn_begin; @@ -51,7 +52,7 @@ my $patron = $builder->build({ } }); my $p = Koha::Patrons->find( $patron->{borrowernumber} ); -set_logged_in_user( $p ); +t::lib::Mocks::mock_userenv({ patron => $p }); my $patron2 = $builder->build({ source => 'Borrower', @@ -138,7 +139,7 @@ subtest 'search_limited' => sub { my $group_2 = Koha::Library::Group->new( { title => 'TEST Group 2' } )->store; # $patron and $patron2 are from the same library, $patron3 from another one # Logged in user is $patron, superlibrarian - set_logged_in_user( $p ); + t::lib::Mocks::mock_userenv({ patron => $p }); Koha::Library::Group->new({ parent_id => $group_1->id, branchcode => $patron->{branchcode} })->store(); Koha::Library::Group->new({ parent_id => $group_2->id, branchcode => $patron3->{branchcode} })->store(); Koha::Patron::Discharge::request({ borrowernumber => $patron->{borrowernumber} }); @@ -148,20 +149,9 @@ subtest 'search_limited' => sub { is( Koha::Patron::Discharge::count({pending => 1}), 3, 'With permission, all discharges are visible' ); # With patron 3 logged in, only discharges from their group are visible - set_logged_in_user( $p3 ); + t::lib::Mocks::mock_userenv({ patron => $p3 }); is( scalar( Koha::Patron::Discharge::get_pendings), 1, 'Without permission, only discharge from our group are visible' ); is( Koha::Patron::Discharge::count({pending => 1}), 1, 'Without permission, only discharge from our group are visible' ); }; $schema->storage->txn_rollback; - -sub set_logged_in_user { - my ($patron) = @_; - C4::Context->set_userenv( - $patron->borrowernumber, $patron->userid, - $patron->cardnumber, 'firstname', - 'surname', $patron->library->branchcode, - 'Midway Public Library', $patron->flags, - '', '' - ); -} diff --git a/t/db_dependent/Patron/Borrower_PrevCheckout.t b/t/db_dependent/Patron/Borrower_PrevCheckout.t index d58a543..dc53e65 100644 --- a/t/db_dependent/Patron/Borrower_PrevCheckout.t +++ b/t/db_dependent/Patron/Borrower_PrevCheckout.t @@ -44,14 +44,8 @@ my $inheritCatCode = $builder->build({ # Create context for some tests late on in the file. my $library = $builder->build({ source => 'Branch' }); my $staff = $builder->build({source => 'Borrower'}); -my @USERENV = ( - $staff->{borrowernumber}, 'test', 'MASTERTEST', 'firstname', $library->{branchcode}, - $library->{branchcode}, 'email@example.org' -); -C4::Context->_new_userenv('DUMMY_SESSION_ID'); -C4::Context->set_userenv(@USERENV); -BAIL_OUT("No userenv") unless C4::Context->userenv; +t::lib::Mocks::mock_userenv({ branchcode => $library->{branchcode} }); # wants_check_for_previous_checkout diff --git a/t/db_dependent/PatronLists.t b/t/db_dependent/PatronLists.t index 205538b..6e4f5f8 100755 --- a/t/db_dependent/PatronLists.t +++ b/t/db_dependent/PatronLists.t @@ -19,6 +19,7 @@ use Modern::Perl; use Test::More tests => 9; use t::lib::TestBuilder; +use t::lib::Mocks; use Koha::Database; use Koha::List::Patron @@ -29,8 +30,7 @@ $schema->storage->txn_begin; my $builder = t::lib::TestBuilder->new; -C4::Context->_new_userenv('DUMMY SESSION'); -C4::Context->set_userenv( 0 ); # Koha::List::Patron only needs a number +t::lib::Mocks::mock_userenv(); # Create 10 sample borrowers my @borrowers = (); diff --git a/t/db_dependent/Reserves.t b/t/db_dependent/Reserves.t index 9fe293d..f6a412c 100755 --- a/t/db_dependent/Reserves.t +++ b/t/db_dependent/Reserves.t @@ -77,9 +77,7 @@ my $category_2 = $builder->build({ source => 'Category' })->{ categorycode }; my $itemtype = $builder->build( { source => 'Itemtype', value => { notforloan => undef } } )->{itemtype}; -C4::Context->set_userenv( - undef, undef, undef, undef, undef, undef, $branch_1 -); +t::lib::Mocks::mock_userenv({ branchcode => $branch_1 }); # Create a helper biblio my $bib = MARC::Record->new(); diff --git a/t/db_dependent/Template/Plugin/Branches.t b/t/db_dependent/Template/Plugin/Branches.t index 860d8b8..97cfcdd 100644 --- a/t/db_dependent/Template/Plugin/Branches.t +++ b/t/db_dependent/Template/Plugin/Branches.t @@ -59,8 +59,7 @@ is($name, '', 'received empty string as name of NULL/undefined library code'); $library = $plugin->GetLoggedInBranchcode(); is($library, '', 'no active library if there is no active user session'); -C4::Context->_new_userenv('DUMMY_SESSION_ID'); -C4::Context->set_userenv(123, 'userid', 'usercnum', 'First name', 'Surname', 'MYLIBRARY', 'My Library', 0); +t::lib::Mocks::mock_userenv({ branchcode => 'MYLIBRARY' }); $library = $plugin->GetLoggedInBranchcode(); is($library, 'MYLIBRARY', 'GetLoggedInBranchcode() returns active library'); diff --git a/t/db_dependent/Utils/Datatables_Members.t b/t/db_dependent/Utils/Datatables_Members.t index 1d162cf..d378f8f 100644 --- a/t/db_dependent/Utils/Datatables_Members.t +++ b/t/db_dependent/Utils/Datatables_Members.t @@ -45,7 +45,7 @@ my $library = $builder->build({ }); my $patron = $builder->build_object({ class => 'Koha::Patrons', value => { flags => 1 } }); -set_logged_in_user( $patron ); +t::lib::Mocks::mock_userenv({ patron => $patron }); my $branchcode=$library->{branchcode}; @@ -463,15 +463,3 @@ subtest 'ExtendedPatronAttributes' => sub { # End $schema->storage->txn_rollback; - -sub set_logged_in_user { - my ($patron) = @_; - C4::Context->_new_userenv('xxx'); - C4::Context->set_userenv( - $patron->borrowernumber, $patron->userid, - $patron->cardnumber, 'firstname', - 'surname', $patron->library->branchcode, - 'Midway Public Library', $patron->flags, - '', '' - ); -} diff --git a/t/db_dependent/Utils/Datatables_Virtualshelves.t b/t/db_dependent/Utils/Datatables_Virtualshelves.t index cdc7225..eaf5ce5 100644 --- a/t/db_dependent/Utils/Datatables_Virtualshelves.t +++ b/t/db_dependent/Utils/Datatables_Virtualshelves.t @@ -27,6 +27,8 @@ use Koha::Patrons; use Koha::Patron::Categories; use Koha::Virtualshelves; +use t::lib::Mocks; + use_ok( "C4::Utils::DataTables::VirtualShelves" ); my $schema = Koha::Database->new->schema; @@ -77,7 +79,8 @@ my %john_smith = ( userid => 'john.smith', ); -$john_doe{borrowernumber} = Koha::Patron->new( \%john_doe )->store->borrowernumber; +my $john_doe_patron = Koha::Patron->new( \%john_doe )->store; +$john_doe{borrowernumber} = $john_doe_patron->borrowernumber; $jane_doe{borrowernumber} = Koha::Patron->new( \%jane_doe )->store->borrowernumber; $john_smith{borrowernumber} = Koha::Patron->new( \%john_smith )->store->borrowernumber; @@ -179,8 +182,7 @@ my %dt_params = ( ); my $search_results; -C4::Context->_new_userenv ('DUMMY_SESSION_ID'); -C4::Context->set_userenv($john_doe{borrowernumber}, $john_doe{userid}, 'usercnum', 'First name', 'Surname', 'MYLIBRARY', 'My Library', 0); +t::lib::Mocks::mock_userenv({ patron => $john_doe_patron }); # Search private lists by title $search_results = C4::Utils::DataTables::VirtualShelves::search({ diff --git a/t/db_dependent/rollingloans.t b/t/db_dependent/rollingloans.t index bd86f5e..973ab98 100644 --- a/t/db_dependent/rollingloans.t +++ b/t/db_dependent/rollingloans.t @@ -20,9 +20,7 @@ my $builder = t::lib::TestBuilder->new; $builder->build({ source => 'Branch', value => { branchcode => 'CPL' } }) unless Koha::Libraries->find('CPL'); -C4::Context->_new_userenv(1234567); -C4::Context->set_userenv(91, 'CLIstaff', '23529001223661', 'CPL', - 'CPL', 'CPL', '', 'cc@cscnet.co.uk'); +t::lib::Mocks::mock_userenv({ branchcode => 'CPL' }); t::lib::Mocks::mock_preference('BlockReturnOfWithdrawnItems',0); my $test_patron = '23529001223651'; diff --git a/t/lib/Mocks.pm b/t/lib/Mocks.pm index 00c3d4f..a28d873 100644 --- a/t/lib/Mocks.pm +++ b/t/lib/Mocks.pm @@ -40,4 +40,29 @@ sub mock_preference { }); } +sub mock_userenv { + my ( $params ) = @_; + + C4::Context->_new_userenv(42); + + my $userenv; + if ( $params and my $patron = $params->{patron} ) { + $userenv = $patron->unblessed; + $userenv->{branchcode} = $params->{branchcode} || $patron->library->branchcode; + $userenv->{branchname} = $params->{branchname} || $patron->library->branchname; + } + my $usernum = $params->{borrowernumber} || $userenv->{borrowernumber} || 51; + my $userid = $params->{userid} || $userenv->{userid} || 'userid4tests'; + my $cardnumber = $params->{cardnumber} || $userenv->{cardnumber}; + my $firstname = $params->{firstname} || $userenv->{firstname} || 'firstname'; + my $surname = $params->{surname} || $userenv->{surname} || 'surname'; + my $branchcode = $params->{branchcode} || $userenv->{branchcode} || 'Branch4T'; + my $branchname = $params->{branchname} || $userenv->{branchname}; + my $flags = $params->{flags} || $userenv->{flags} || 0; + my $emailaddress = $params->{emailaddress} || $userenv->{emailaddress}; + my ( $branchprinter, $shibboleth ); + + C4::Context->set_userenv($usernum, $userid, $cardnumber, $firstname, $surname, $branchcode, $branchname, $flags, $emailaddress, $branchprinter, $shibboleth ); +} + 1; -- 2.1.4