From a0cf54106ed38999e87bbd88daf48ddafc7b074e Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Fri, 4 Mar 2016 13:14:59 +0000 Subject: [PATCH] Bug 11998: Use t::lib::Mocks::mock_preference in tests Unless in t/db_dependent/Context.t where we want to test the cache behaviors. Signed-off-by: Tomas Cohen Arazi Signed-off-by: Jacek Ablewicz --- C4/Context.pm | 2 + t/db_dependent/Amazon.t | 19 ++++----- t/db_dependent/Auth.t | 7 ++-- t/db_dependent/Auth_with_cas.t | 5 ++- t/db_dependent/Bookseller.t | 5 ++- t/db_dependent/Circulation.t | 45 +++++++++++----------- t/db_dependent/Circulation/GetTopIssues.t | 3 +- t/db_dependent/Circulation/Returns.t | 5 ++- t/db_dependent/Circulation_Branch.t | 3 +- t/db_dependent/Circulation_issue.t | 5 ++- t/db_dependent/Hold.t | 5 ++- t/db_dependent/Holds.t | 12 +++--- t/db_dependent/HoldsQueue.t | 35 ++++++++--------- t/db_dependent/Items.t | 22 +++++------ t/db_dependent/Koha/Patron/Messages.t | 9 +++-- t/db_dependent/Koha_template_plugin_KohaDates.t | 7 ++-- t/db_dependent/Languages.t | 5 ++- t/db_dependent/Letters.t | 2 +- t/db_dependent/Members.t | 19 ++++----- t/db_dependent/Members/AddEnrolmentFeeIfNeeded.t | 5 ++- t/db_dependent/QueryParser.t | 3 +- t/db_dependent/Record.t | 7 ++-- t/db_dependent/Reserves.t | 32 +++++++-------- t/db_dependent/Suggestions.t | 5 ++- t/db_dependent/XISBN.t | 9 +++-- t/db_dependent/default_search_class.pl | 3 +- 26 files changed, 149 insertions(+), 130 deletions(-) diff --git a/C4/Context.pm b/C4/Context.pm index 5b27e28..6811e32 100644 --- a/C4/Context.pm +++ b/C4/Context.pm @@ -606,6 +606,8 @@ sub set_preference { : $syspref ? $syspref->type : undef; + $value = 0 if ( $type && $type eq 'YesNo' && $value eq '' ); + # force explicit protocol on OPACBaseURL if ( $variable eq 'opacbaseurl' && substr( $value, 0, 4 ) !~ /http/ ) { $value = 'http://' . $value; diff --git a/t/db_dependent/Amazon.t b/t/db_dependent/Amazon.t index f8352c8..2304073 100755 --- a/t/db_dependent/Amazon.t +++ b/t/db_dependent/Amazon.t @@ -7,43 +7,44 @@ use strict; use warnings; use Test::More tests => 8; +use t::lib::Mocks; use C4::Context; BEGIN { - use_ok('C4::External::Amazon'); + use_ok('C4::External::Amazon'); } my $context = C4::Context->new(); my $locale = $context->preference('AmazonLocale'); -$context->set_preference('AmazonLocale','CA'); +t::lib::Mocks::mock_preference('AmazonLocale','CA'); $context->clear_syspref_cache(); is(get_amazon_tld,'.ca','Changes locale to CA and tests get_amazon_tld'); -$context->set_preference('AmazonLocale','DE'); +t::lib::Mocks::mock_preference('AmazonLocale','DE'); $context->clear_syspref_cache(); is(get_amazon_tld,'.de','Changes locale to DE and tests get_amazon_tld'); -$context->set_preference('AmazonLocale','FR'); +t::lib::Mocks::mock_preference('AmazonLocale','FR'); $context->clear_syspref_cache(); is(get_amazon_tld,'.fr','Changes locale to FR and tests get_amazon_tld'); -$context->set_preference('AmazonLocale','JP'); +t::lib::Mocks::mock_preference('AmazonLocale','JP'); $context->clear_syspref_cache(); is(get_amazon_tld,'.jp','Changes locale to JP and tests get_amazon_tld'); -$context->set_preference('AmazonLocale','UK'); +t::lib::Mocks::mock_preference('AmazonLocale','UK'); $context->clear_syspref_cache(); is(get_amazon_tld,'.co.uk','Changes locale to UK and tests get_amazon_tld'); -$context->set_preference('AmazonLocale','US'); +t::lib::Mocks::mock_preference('AmazonLocale','US'); $context->clear_syspref_cache(); is(get_amazon_tld,'.com','Changes locale to US and tests get_amazon_tld'); -$context->set_preference('AmazonLocale','NZ'); +t::lib::Mocks::mock_preference('AmazonLocale','NZ'); $context->clear_syspref_cache(); is(get_amazon_tld,'.com','Changes locale to one not in the array and tests get_amazon_tld'); -$context->set_preference('AmazonLocale',$locale); +t::lib::Mocks::mock_preference('AmazonLocale',$locale); $context->clear_syspref_cache(); diff --git a/t/db_dependent/Auth.t b/t/db_dependent/Auth.t index 681476b..68e3ebc 100644 --- a/t/db_dependent/Auth.t +++ b/t/db_dependent/Auth.t @@ -10,6 +10,7 @@ use Test::MockModule; use List::MoreUtils qw/all any none/; use Test::More tests => 13; use Test::Warn; +use t::lib::Mocks; use C4::Members; use Koha::AuthUtils qw/hash_password/; @@ -61,10 +62,10 @@ $dbh->{RaiseError} = 1; $auth->mock( 'checkauth', \&MockedCheckauth ); # Make sure 'EnableOpacSearchHistory' is set - C4::Context->set_preference('EnableOpacSearchHistory',1); + t::lib::Mocks::mock_preference('EnableOpacSearchHistory',1); # Enable es-ES for the OPAC and staff interfaces - C4::Context->set_preference('opaclanguages','en,es-ES'); - C4::Context->set_preference('language','en,es-ES'); + t::lib::Mocks::mock_preference('opaclanguages','en,es-ES'); + t::lib::Mocks::mock_preference('language','en,es-ES'); # we need a session cookie $ENV{"SERVER_PORT"} = 80; diff --git a/t/db_dependent/Auth_with_cas.t b/t/db_dependent/Auth_with_cas.t index adea51b..ab21233 100755 --- a/t/db_dependent/Auth_with_cas.t +++ b/t/db_dependent/Auth_with_cas.t @@ -20,6 +20,7 @@ use Modern::Perl; use Test::More tests => 4; use CGI; +use t::lib::Mocks; use C4::Context; BEGIN { @@ -39,8 +40,8 @@ $dbh->{ AutoCommit } = 0; $dbh->{ RaiseError } = 1; C4::Context->disable_syspref_cache(); -C4::Context->set_preference('OPACBaseURL','http://localhost'); -C4::Context->set_preference('staffClientBaseURL','localhost:8080'); +t::lib::Mocks::mock_preference('OPACBaseURL','http://localhost'); +t::lib::Mocks::mock_preference('staffClientBaseURL','localhost:8080'); my $opac_base_url = C4::Context->preference('OpacBaseURL'); my $staff_base_url = C4::Context->preference('staffClientBaseURL'); diff --git a/t/db_dependent/Bookseller.t b/t/db_dependent/Bookseller.t index 964ba87..3fa5be3 100644 --- a/t/db_dependent/Bookseller.t +++ b/t/db_dependent/Bookseller.t @@ -9,6 +9,7 @@ use Test::Warn; use C4::Context; use Koha::DateUtils; use DateTime::Duration; +use t::lib::Mocks; use C4::Acquisition; use C4::Serials; use C4::Budgets; @@ -656,7 +657,7 @@ $module->mock( } ); -C4::Context->set_preference('IndependentBranches', 0); +t::lib::Mocks::mock_preference('IndependentBranches', 0); @subscriptions = SearchSubscriptions({expiration_date => '2013-12-31'}); is( scalar(grep { !$_->{cannotdisplay} } @subscriptions ), @@ -664,7 +665,7 @@ is( 'ordinary user can see all subscriptions with IndependentBranches off' ); -C4::Context->set_preference('IndependentBranches', 1); +t::lib::Mocks::mock_preference('IndependentBranches', 1); @subscriptions = SearchSubscriptions({expiration_date => '2013-12-31'}); is( scalar(grep { !$_->{cannotdisplay} } @subscriptions ), diff --git a/t/db_dependent/Circulation.t b/t/db_dependent/Circulation.t index 477959c..b1dd6f1 100755 --- a/t/db_dependent/Circulation.t +++ b/t/db_dependent/Circulation.t @@ -18,6 +18,7 @@ use Modern::Perl; use DateTime; +use t::lib::Mocks; use C4::Biblio; use C4::Branch; use C4::Items; @@ -66,7 +67,7 @@ my $borrower = { }; # No userenv, PickupLibrary -C4::Context->set_preference('CircControl', 'PickupLibrary'); +t::lib::Mocks::mock_preference('CircControl', 'PickupLibrary'); is( C4::Context->preference('CircControl'), 'PickupLibrary', @@ -79,7 +80,7 @@ is( ); # No userenv, PatronLibrary -C4::Context->set_preference('CircControl', 'PatronLibrary'); +t::lib::Mocks::mock_preference('CircControl', 'PatronLibrary'); is( C4::Context->preference('CircControl'), 'PatronLibrary', @@ -92,7 +93,7 @@ is( ); # No userenv, ItemHomeLibrary -C4::Context->set_preference('CircControl', 'ItemHomeLibrary'); +t::lib::Mocks::mock_preference('CircControl', 'ItemHomeLibrary'); is( C4::Context->preference('CircControl'), 'ItemHomeLibrary', @@ -110,7 +111,7 @@ C4::Context->set_userenv(0,0,0,'firstname','surname', $library2->{branchcode}, ' is(C4::Context->userenv->{branch}, $library2->{branchcode}, 'userenv set'); # Userenv set, PickupLibrary -C4::Context->set_preference('CircControl', 'PickupLibrary'); +t::lib::Mocks::mock_preference('CircControl', 'PickupLibrary'); is( C4::Context->preference('CircControl'), 'PickupLibrary', @@ -123,7 +124,7 @@ is( ); # Userenv set, PatronLibrary -C4::Context->set_preference('CircControl', 'PatronLibrary'); +t::lib::Mocks::mock_preference('CircControl', 'PatronLibrary'); is( C4::Context->preference('CircControl'), 'PatronLibrary', @@ -136,7 +137,7 @@ is( ); # Userenv set, ItemHomeLibrary -C4::Context->set_preference('CircControl', 'ItemHomeLibrary'); +t::lib::Mocks::mock_preference('CircControl', 'ItemHomeLibrary'); is( C4::Context->preference('CircControl'), 'ItemHomeLibrary', @@ -149,7 +150,7 @@ is( ); # Reset initial configuration -C4::Context->set_preference('CircControl', $CircControl); +t::lib::Mocks::mock_preference('CircControl', $CircControl); is( C4::Context->preference('CircControl'), $CircControl, @@ -318,7 +319,7 @@ C4::Context->dbh->do("DELETE FROM accountlines"); # Testing of feature to allow the renewal of reserved items if other items on the record can fill all needed holds C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 1"); - C4::Context->set_preference('AllowRenewalIfOtherItemsAvailable', 1 ); + t::lib::Mocks::mock_preference('AllowRenewalIfOtherItemsAvailable', 1 ); ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber); is( $renewokay, 1, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds'); ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber2); @@ -354,7 +355,7 @@ C4::Context->dbh->do("DELETE FROM accountlines"); is( $renewokay, 0, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds'); ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber2); is( $renewokay, 0, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds'); - C4::Context->set_preference('AllowRenewalIfOtherItemsAvailable', 0 ); + t::lib::Mocks::mock_preference('AllowRenewalIfOtherItemsAvailable', 0 ); ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber); is( $renewokay, 0, '(Bug 10663) Cannot renew, reserved'); @@ -410,7 +411,7 @@ C4::Context->dbh->do("DELETE FROM accountlines"); my $datedue5 = AddIssue($restricted_borrower, $barcode5); is (defined $datedue5, 1, "Item with date due checked out, due date: $datedue5"); - C4::Context->set_preference('RestrictionBlockRenewing','1'); + t::lib::Mocks::mock_preference('RestrictionBlockRenewing','1'); ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber2); is( $renewokay, 1, '(Bug 8236), Can renew, user is not restricted'); ( $renewokay, $error ) = CanBookBeRenewed($restricted_borrowernumber, $itemnumber5); @@ -445,7 +446,7 @@ C4::Context->dbh->do("DELETE FROM accountlines"); is (defined $passeddatedue1, 1, "Item with passed date due checked out, due date: $passeddatedue1"); - C4::Context->set_preference('OverduesBlockRenewing','blockitem'); + t::lib::Mocks::mock_preference('OverduesBlockRenewing','blockitem'); ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber6); is( $renewokay, 1, '(Bug 8236), Can renew, this item is not overdue'); ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber7); @@ -486,7 +487,7 @@ C4::Context->dbh->do("DELETE FROM accountlines"); # Bug 14395 # Test 'exact time' setting for syspref NoRenewalBeforePrecision - C4::Context->set_preference( 'NoRenewalBeforePrecision', 'exact_time' ); + t::lib::Mocks::mock_preference( 'NoRenewalBeforePrecision', 'exact_time' ); is( GetSoonestRenewDate( $renewing_borrowernumber, $itemnumber ), $datedue->clone->add( days => -7 ), @@ -495,7 +496,7 @@ C4::Context->dbh->do("DELETE FROM accountlines"); # Bug 14395 # Test 'date' setting for syspref NoRenewalBeforePrecision - C4::Context->set_preference( 'NoRenewalBeforePrecision', 'date' ); + t::lib::Mocks::mock_preference( 'NoRenewalBeforePrecision', 'date' ); is( GetSoonestRenewDate( $renewing_borrowernumber, $itemnumber ), $datedue->clone->add( days => -7 )->truncate( to => 'day' ), @@ -541,8 +542,8 @@ C4::Context->dbh->do("DELETE FROM accountlines"); is( $error, 'too_many', 'Cannot renew, 0 renewals allowed (returned code is too_many)'); # Test WhenLostForgiveFine and WhenLostChargeReplacementFee - C4::Context->set_preference('WhenLostForgiveFine','1'); - C4::Context->set_preference('WhenLostChargeReplacementFee','1'); + t::lib::Mocks::mock_preference('WhenLostForgiveFine','1'); + t::lib::Mocks::mock_preference('WhenLostChargeReplacementFee','1'); C4::Overdues::UpdateFine( { @@ -569,8 +570,8 @@ C4::Context->dbh->do("DELETE FROM accountlines"); C4::Context->dbh->do("DELETE FROM accountlines"); - C4::Context->set_preference('WhenLostForgiveFine','0'); - C4::Context->set_preference('WhenLostChargeReplacementFee','0'); + t::lib::Mocks::mock_preference('WhenLostForgiveFine','0'); + t::lib::Mocks::mock_preference('WhenLostChargeReplacementFee','0'); C4::Overdues::UpdateFine( { @@ -602,7 +603,7 @@ C4::Context->dbh->do("DELETE FROM accountlines"); ok( $units == 0, '_get_chargeable_units returns 0 for items not past due date (Bug 12596)' ); # Users cannot renew any item if there is an overdue item - C4::Context->set_preference('OverduesBlockRenewing','block'); + t::lib::Mocks::mock_preference('OverduesBlockRenewing','block'); ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber6); is( $renewokay, 0, '(Bug 8236), Cannot renew, one of the items is overdue'); ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber7); @@ -807,22 +808,22 @@ C4::Context->dbh->do("DELETE FROM accountlines"); ); C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 0"); - C4::Context->set_preference( 'AllowRenewalIfOtherItemsAvailable', 0 ); + t::lib::Mocks::mock_preference( 'AllowRenewalIfOtherItemsAvailable', 0 ); ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 ); is( $renewokay, 0, 'Bug 14337 - Verify the borrower cannot renew with a hold on the record if AllowRenewalIfOtherItemsAvailable and onshelfholds are disabled' ); C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 0"); - C4::Context->set_preference( 'AllowRenewalIfOtherItemsAvailable', 1 ); + t::lib::Mocks::mock_preference( 'AllowRenewalIfOtherItemsAvailable', 1 ); ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 ); is( $renewokay, 0, 'Bug 14337 - Verify the borrower cannot renew with a hold on the record if AllowRenewalIfOtherItemsAvailable is enabled and onshelfholds is disabled' ); C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 1"); - C4::Context->set_preference( 'AllowRenewalIfOtherItemsAvailable', 0 ); + t::lib::Mocks::mock_preference( 'AllowRenewalIfOtherItemsAvailable', 0 ); ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 ); is( $renewokay, 0, 'Bug 14337 - Verify the borrower cannot renew with a hold on the record if AllowRenewalIfOtherItemsAvailable is disabled and onshelfhold is enabled' ); C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 1"); - C4::Context->set_preference( 'AllowRenewalIfOtherItemsAvailable', 1 ); + t::lib::Mocks::mock_preference( 'AllowRenewalIfOtherItemsAvailable', 1 ); ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 ); is( $renewokay, 1, 'Bug 14337 - Verify the borrower can renew with a hold on the record if AllowRenewalIfOtherItemsAvailable and onshelfhold are enabled' ); diff --git a/t/db_dependent/Circulation/GetTopIssues.t b/t/db_dependent/Circulation/GetTopIssues.t index 4663934..a008187 100644 --- a/t/db_dependent/Circulation/GetTopIssues.t +++ b/t/db_dependent/Circulation/GetTopIssues.t @@ -19,6 +19,7 @@ use Modern::Perl; use Test::More tests => 14; use Test::MockModule; +use t::lib::Mocks; use t::lib::TestBuilder; use C4::Context; @@ -46,7 +47,7 @@ my $c4_context = Test::MockModule->new('C4::Context'); $c4_context->mock('userenv', sub { { branch => $branch_1->{ branchcode } } }); -C4::Context->set_preference('item-level_itypes', '0'); +t::lib::Mocks::mock_preference('item-level_itypes', '0'); my $biblionumber = create_biblio('Test 1', $itemtype); AddItem({ diff --git a/t/db_dependent/Circulation/Returns.t b/t/db_dependent/Circulation/Returns.t index 3d13684..bbc96a4 100644 --- a/t/db_dependent/Circulation/Returns.t +++ b/t/db_dependent/Circulation/Returns.t @@ -1,6 +1,7 @@ use Modern::Perl; use Test::More tests => 2; +use t::lib::Mocks; use C4::Biblio; use C4::Circulation; use C4::Items; @@ -38,7 +39,7 @@ my ( undef, undef, $itemnumber ) = AddItem( my $item; -C4::Context->set_preference( "InProcessingToShelvingCart", 1 ); +t::lib::Mocks::mock_preference( "InProcessingToShelvingCart", 1 ); AddReturn( 'i_dont_exist', $library->{branchcode} ); $item = GetItem($itemnumber); is( $item->{location}, 'CART', "InProcessingToShelvingCart functions as intended" ); @@ -46,7 +47,7 @@ is( $item->{location}, 'CART', "InProcessingToShelvingCart functions as intended $item->{location} = 'PROC'; ModItem( $item, undef, $itemnumber ); -C4::Context->set_preference( "InProcessingToShelvingCart", 0 ); +t::lib::Mocks::mock_preference( "InProcessingToShelvingCart", 0 ); AddReturn( 'i_dont_exist', $library->{branchcode} ); $item = GetItem($itemnumber); is( $item->{location}, 'TEST', "InProcessingToShelvingCart functions as intended" ); diff --git a/t/db_dependent/Circulation_Branch.t b/t/db_dependent/Circulation_Branch.t index ae7eadf..05ee584 100644 --- a/t/db_dependent/Circulation_Branch.t +++ b/t/db_dependent/Circulation_Branch.t @@ -1,6 +1,7 @@ #!/usr/bin/perl use Modern::Perl; +use t::lib::Mocks; use C4::Biblio; use C4::Members; use C4::Circulation; @@ -315,7 +316,7 @@ is_deeply( ); # Test return policies -C4::Context->set_preference('AutomaticItemReturn','0'); +t::lib::Mocks::mock_preference('AutomaticItemReturn','0'); # item1 returned at branch2 should trigger transfer to homebranch $query = diff --git a/t/db_dependent/Circulation_issue.t b/t/db_dependent/Circulation_issue.t index 9b1a9c8..a28b1c5 100644 --- a/t/db_dependent/Circulation_issue.t +++ b/t/db_dependent/Circulation_issue.t @@ -19,6 +19,7 @@ use Modern::Perl; use Koha::DateUtils; use DateTime::Duration; +use t::lib::Mocks; use C4::Biblio; use C4::Members; use C4::Circulation; @@ -373,12 +374,12 @@ my $itemnumber; $biblionumber ); -C4::Context->set_preference( 'UpdateNotForLoanStatusOnCheckin', q{} ); +t::lib::Mocks::mock_preference( 'UpdateNotForLoanStatusOnCheckin', q{} ); AddReturn( 'barcode_3', $samplebranch1->{branchcode} ); my $item = GetItem( $itemnumber ); ok( $item->{notforloan} eq 1, 'UpdateNotForLoanStatusOnCheckin does not modify value when not enabled' ); -C4::Context->set_preference( 'UpdateNotForLoanStatusOnCheckin', '1: 9' ); +t::lib::Mocks::mock_preference( 'UpdateNotForLoanStatusOnCheckin', '1: 9' ); AddReturn( 'barcode_3', $samplebranch1->{branchcode} ); $item = GetItem( $itemnumber ); ok( $item->{notforloan} eq 9, q{UpdateNotForLoanStatusOnCheckin updates notforloan value from 1 to 9 with setting "1: 9"} ); diff --git a/t/db_dependent/Hold.t b/t/db_dependent/Hold.t index 1395419..db14f37 100755 --- a/t/db_dependent/Hold.t +++ b/t/db_dependent/Hold.t @@ -17,6 +17,7 @@ use Modern::Perl; +use t::lib::Mocks; use C4::Context; use C4::Biblio qw( AddBiblio ); use Koha::Database; @@ -91,7 +92,7 @@ my $hold_borrower = $hold->borrower(); ok( $hold_borrower, 'Got hold borrower' ); is( $hold_borrower->borrowernumber(), $borrower->borrowernumber(), 'Hold borrower matches correct borrower' ); -C4::Context->set_preference( 'ReservesMaxPickUpDelay', '' ); +t::lib::Mocks::mock_preference( 'ReservesMaxPickUpDelay', '' ); $dt = $hold->waiting_expires_on(); is( $dt, undef, "Koha::Hold->waiting_expires_on returns undef if ReservesMaxPickUpDelay is not set" ); @@ -99,7 +100,7 @@ is( $hold->is_waiting, 1, 'The hold is waiting' ); is( $hold->is_found, 1, 'The hold is found'); ok( !$hold->is_in_transit, 'The hold is not in transit' ); -C4::Context->set_preference( 'ReservesMaxPickUpDelay', '5' ); +t::lib::Mocks::mock_preference( 'ReservesMaxPickUpDelay', '5' ); $dt = $hold->waiting_expires_on(); is( $dt->ymd, "2000-01-06", "Koha::Hold->waiting_expires_on returns DateTime of waitingdate + ReservesMaxPickUpDelay if set" ); diff --git a/t/db_dependent/Holds.t b/t/db_dependent/Holds.t index 29095d0..4ba95df 100755 --- a/t/db_dependent/Holds.t +++ b/t/db_dependent/Holds.t @@ -334,10 +334,10 @@ $reserve3 = GetReserve( $reserveid3 ); is( $reserve3->{priority}, 1, "After ModReserve, the 3rd reserve becomes the first on the waiting list" ); ModItem({ damaged => 1 }, $item_bibnum, $itemnumber); -C4::Context->set_preference( 'AllowHoldsOnDamagedItems', 1 ); +t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 1 ); ok( CanItemBeReserved( $borrowernumbers[0], $itemnumber) eq 'OK', "Patron can reserve damaged item with AllowHoldsOnDamagedItems enabled" ); ok( defined( ( CheckReserves($itemnumber) )[1] ), "Hold can be trapped for damaged item with AllowHoldsOnDamagedItems enabled" ); -C4::Context->set_preference( 'AllowHoldsOnDamagedItems', 0 ); +t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 0 ); ok( CanItemBeReserved( $borrowernumbers[0], $itemnumber) eq 'damaged', "Patron cannot reserve damaged item with AllowHoldsOnDamagedItems disabled" ); ok( !defined( ( CheckReserves($itemnumber) )[1] ), "Hold cannot be trapped for damaged item with AllowHoldsOnDamagedItems disabled" ); @@ -409,8 +409,8 @@ is(CanItemBeReserved($borrowernumbers[0], $itemnumber), 'OK', # Test CancelExpiredReserves -C4::Context->set_preference('ExpireReservesMaxPickUpDelay', 1); -C4::Context->set_preference('ReservesMaxPickUpDelay', 1); +t::lib::Mocks::mock_preference('ExpireReservesMaxPickUpDelay', 1); +t::lib::Mocks::mock_preference('ReservesMaxPickUpDelay', 1); my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = localtime(time); $year += 1900; @@ -427,11 +427,11 @@ $calendar->insert_single_holiday( ); $reserve_id = $reserve->{reserve_id}; $dbh->do("UPDATE reserves SET waitingdate = DATE_SUB( NOW(), INTERVAL 5 DAY ), found = 'W', priority = 0 WHERE reserve_id = ?", undef, $reserve_id ); -C4::Context->set_preference('ExpireReservesOnHolidays', 0); +t::lib::Mocks::mock_preference('ExpireReservesOnHolidays', 0); CancelExpiredReserves(); my $count = $dbh->selectrow_array("SELECT COUNT(*) FROM reserves WHERE reserve_id = ?", undef, $reserve_id ); is( $count, 1, "Waiting reserve beyond max pickup delay *not* canceled on holiday" ); -C4::Context->set_preference('ExpireReservesOnHolidays', 1); +t::lib::Mocks::mock_preference('ExpireReservesOnHolidays', 1); CancelExpiredReserves(); $count = $dbh->selectrow_array("SELECT COUNT(*) FROM reserves WHERE reserve_id = ?", undef, $reserve_id ); is( $count, 0, "Waiting reserve beyond max pickup delay canceled on holiday" ); diff --git a/t/db_dependent/HoldsQueue.t b/t/db_dependent/HoldsQueue.t index f0141fb..6fc4302 100755 --- a/t/db_dependent/HoldsQueue.t +++ b/t/db_dependent/HoldsQueue.t @@ -19,6 +19,7 @@ use Koha::Database; use Koha::DateUtils; use Koha::ItemType; +use t::lib::Mocks; use t::lib::TestBuilder; use Koha::ItemTypes; @@ -67,10 +68,9 @@ $itemtype or BAIL_OUT("No adequate itemtype"); #FIXME Should be $itemtype = $ite #Set up the stage # Sysprefs and cost matrix -C4::Context->set_preference('HoldsQueueSkipClosed', 0); -$dbh->do("UPDATE systempreferences SET value = ? WHERE variable = 'StaticHoldsQueueWeight'", undef, - join( ',', @other_branches, $borrower_branchcode, $least_cost_branch_code)); -$dbh->do("UPDATE systempreferences SET value = '0' WHERE variable = 'RandomizeHoldsQueueWeight'"); +t::lib::Mocks::mock_preference('HoldsQueueSkipClosed', 0); +t::lib::Mocks::mock_preference('StaticHoldsQueueWeight', join( ',', @other_branches, $borrower_branchcode, $least_cost_branch_code)); +t::lib::Mocks::mock_preference('RandomizeHoldsQueueWeight', 0); $dbh->do("DELETE FROM transport_cost"); my $transport_cost_insert_sth = $dbh->prepare("insert into transport_cost (frombranch, tobranch, cost) values (?, ?, ?)"); @@ -116,14 +116,13 @@ AddReserve ( $borrower_branchcode, $borrowernumber, $biblionumber, $bibitems, $ $dbh->do("UPDATE reserves SET reservedate = DATE_SUB( reservedate, INTERVAL 1 DAY )"); # Tests -my $use_cost_matrix_sth = $dbh->prepare("UPDATE systempreferences SET value = ? WHERE variable = 'UseTransportCostMatrix'"); my $test_sth = $dbh->prepare("SELECT * FROM hold_fill_targets JOIN tmp_holdsqueue USING (borrowernumber, biblionumber, itemnumber) JOIN items USING (itemnumber) WHERE borrowernumber = $borrowernumber"); # We have a book available homed in borrower branch, no point fiddling with AutomaticItemReturn -C4::Context->set_preference('AutomaticItemReturn', 0); +t::lib::Mocks::mock_preference('AutomaticItemReturn', 0); test_queue ('take from homebranch', 0, $borrower_branchcode, $borrower_branchcode); test_queue ('take from homebranch', 1, $borrower_branchcode, $borrower_branchcode); @@ -132,14 +131,14 @@ $dbh->do("DELETE FROM hold_fill_targets"); $dbh->do("DELETE FROM issues WHERE itemnumber IN (SELECT itemnumber FROM items WHERE homebranch = '$borrower_branchcode' AND holdingbranch = '$borrower_branchcode')"); $dbh->do("DELETE FROM items WHERE homebranch = '$borrower_branchcode' AND holdingbranch = '$borrower_branchcode'"); # test_queue will flush -C4::Context->set_preference('AutomaticItemReturn', 1); +t::lib::Mocks::mock_preference('AutomaticItemReturn', 1); # Not sure how to make this test more difficult - holding branch does not matter $dbh->do("DELETE FROM tmp_holdsqueue"); $dbh->do("DELETE FROM hold_fill_targets"); $dbh->do("DELETE FROM issues WHERE itemnumber IN (SELECT itemnumber FROM items WHERE homebranch = '$borrower_branchcode')"); $dbh->do("DELETE FROM items WHERE homebranch = '$borrower_branchcode'"); -C4::Context->set_preference('AutomaticItemReturn', 0); +t::lib::Mocks::mock_preference('AutomaticItemReturn', 0); # We have a book available held in borrower branch test_queue ('take from holdingbranch', 0, $borrower_branchcode, $borrower_branchcode); test_queue ('take from holdingbranch', 1, $borrower_branchcode, $borrower_branchcode); @@ -178,7 +177,7 @@ $dbh->do("DELETE FROM default_branch_circ_rules"); $dbh->do("DELETE FROM default_branch_item_rules"); $dbh->do("DELETE FROM default_circ_rules"); -C4::Context->set_preference('UseTransportCostMatrix', 0); +t::lib::Mocks::mock_preference('UseTransportCostMatrix', 0); $itemtype = Koha::ItemTypes->search->next->itemtype; @@ -307,7 +306,7 @@ is( $holds_queue->[1]->{cardnumber}, $borrower2->{cardnumber}, "Holds queue fill # 1 of which is coming from MPL. Let's enable HoldsQueueSkipClosed # and make today a holiday for MPL. When we run it again we should only # have 1 row in the holds queue -C4::Context->set_preference('HoldsQueueSkipClosed', 1); +t::lib::Mocks::mock_preference('HoldsQueueSkipClosed', 1); my $today = dt_from_string(); C4::Calendar->new( branchcode => $branchcodes[0] )->insert_single_holiday( day => $today->day(), @@ -322,7 +321,7 @@ is( Koha::Calendar->new( branchcode => $branchcodes[0] )->is_holiday( $today ), C4::HoldsQueue::CreateQueue(); $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} }); is( scalar( @$holds_queue ), 1, "Holds not filled with items from closed libraries" ); -C4::Context->set_preference('HoldsQueueSkipClosed', 0); +t::lib::Mocks::mock_preference('HoldsQueueSkipClosed', 0); $dbh->do("DELETE FROM default_circ_rules"); $dbh->do("INSERT INTO default_circ_rules ( holdallowed ) VALUES ( 2 )"); @@ -335,11 +334,11 @@ is( @$holds_queue, 3, "Holds queue filling correct number for holds for default # one of which is coming from MPL. Let's enable HoldsQueueSkipClosed # and use our previously created holiday for MPL # When we run it again we should only have 2 rows in the holds queue -C4::Context->set_preference( 'HoldsQueueSkipClosed', 1 ); +t::lib::Mocks::mock_preference( 'HoldsQueueSkipClosed', 1 ); C4::HoldsQueue::CreateQueue(); $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} }); is( scalar( @$holds_queue ), 2, "Holds not filled with items from closed libraries" ); -C4::Context->set_preference( 'HoldsQueueSkipClosed', 0 ); +t::lib::Mocks::mock_preference( 'HoldsQueueSkipClosed', 0 ); # Bug 14297 $itemtype = Koha::ItemTypes->search->next->itemtype; @@ -388,9 +387,8 @@ $dbh->do(" ( '$library_A', '$itemtype', 2, 'homebranch' ), ( '$library_B', '$itemtype', 1, 'homebranch' ); "); -$dbh->do( "UPDATE systempreferences SET value = ? WHERE variable = 'StaticHoldsQueueWeight'", - undef, join( ',', $library_B, $library_A, $library_C ) ); -$dbh->do( "UPDATE systempreferences SET value = 0 WHERE variable = 'RandomizeHoldsQueueWeight'" ); +t::lib::Mocks::mock_preference('StaticHoldsQueueWeight', join( ',', $library_B, $library_A, $library_C ) ); +t::lib::Mocks::mock_preference('RandomizeHoldsQueueWeight', 0 ); my $reserve_id = AddReserve ( $library_C, $borrowernumber, $biblionumber, '', 1 ); C4::HoldsQueue::CreateQueue(); @@ -416,7 +414,7 @@ $dbh->do("DELETE FROM default_branch_item_rules"); $dbh->do("DELETE FROM default_circ_rules"); $dbh->do("DELETE FROM branch_item_rules"); -C4::Context->set_preference("UseTransportCostMatrix",1); +t::lib::Mocks::mock_preference("UseTransportCostMatrix",1); my $tc_rs = $schema->resultset('TransportCost'); $tc_rs->create({ frombranch => $library_A, tobranch => $library_B, cost => 0, disable_transfer => 1 }); @@ -456,8 +454,7 @@ sub test_queue { $test_name = "$test_name (".($use_cost_matrix ? "" : "don't ")."use cost matrix)"; - $use_cost_matrix_sth->execute($use_cost_matrix); - C4::Context->clear_syspref_cache(); + t::lib::Mocks::mock_preference('UseTransportCostMatrix', $use_cost_matrix); C4::HoldsQueue::CreateQueue(); my $results = $dbh->selectall_arrayref($test_sth, { Slice => {} }); # should be only one diff --git a/t/db_dependent/Items.t b/t/db_dependent/Items.t index a8f9ac2..5f8dac3 100755 --- a/t/db_dependent/Items.t +++ b/t/db_dependent/Items.t @@ -50,7 +50,7 @@ subtest 'General Add, Get and Del tests' => sub { }); # Create a biblio instance for testing - C4::Context->set_preference('marcflavour', 'MARC21'); + t::lib::Mocks::mock_preference('marcflavour', 'MARC21'); my ($bibnum, $bibitemnum) = get_biblio(); # Add an item. @@ -111,7 +111,7 @@ subtest 'GetHiddenItemnumbers tests' => sub { }); # Create a new biblio - C4::Context->set_preference('marcflavour', 'MARC21'); + t::lib::Mocks::mock_preference('marcflavour', 'MARC21'); my ($biblionumber, $biblioitemnumber) = get_biblio(); # Add two items @@ -136,19 +136,19 @@ subtest 'GetHiddenItemnumbers tests' => sub { push @items, GetItem( $item2_itemnumber ); # Empty OpacHiddenItems - C4::Context->set_preference('OpacHiddenItems',''); + t::lib::Mocks::mock_preference('OpacHiddenItems',''); ok( !defined( GetHiddenItemnumbers( @items ) ), "Hidden items list undef if OpacHiddenItems empty"); # Blank spaces - C4::Context->set_preference('OpacHiddenItems',' '); + t::lib::Mocks::mock_preference('OpacHiddenItems',' '); ok( scalar GetHiddenItemnumbers( @items ) == 0, "Hidden items list empty if OpacHiddenItems only contains blanks"); # One variable / value $opachiddenitems = " withdrawn: [1]"; - C4::Context->set_preference( 'OpacHiddenItems', $opachiddenitems ); + t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems ); @hidden = GetHiddenItemnumbers( @items ); ok( scalar @hidden == 1, "Only one hidden item"); is( $hidden[0], $item1_itemnumber, "withdrawn=1 is hidden"); @@ -156,7 +156,7 @@ subtest 'GetHiddenItemnumbers tests' => sub { # One variable, two values $opachiddenitems = " withdrawn: [1,0]"; - C4::Context->set_preference( 'OpacHiddenItems', $opachiddenitems ); + t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems ); @hidden = GetHiddenItemnumbers( @items ); ok( scalar @hidden == 2, "Two items hidden"); is_deeply( \@hidden, \@itemnumbers, "withdrawn=1 and withdrawn=0 hidden"); @@ -166,7 +166,7 @@ subtest 'GetHiddenItemnumbers tests' => sub { withdrawn: [1] homebranch: [$library2->{branchcode}] "; - C4::Context->set_preference( 'OpacHiddenItems', $opachiddenitems ); + t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems ); @hidden = GetHiddenItemnumbers( @items ); ok( scalar @hidden == 2, "Two items hidden"); is_deeply( \@hidden, \@itemnumbers, "withdrawn=1 and homebranch library2 hidden"); @@ -244,10 +244,10 @@ subtest q{Test Koha::Database->schema()->resultset('Item')->itemtype()} => sub { my @bi = $biblio->biblioitems(); my ( $item ) = $bi[0]->items(); - C4::Context->set_preference( 'item-level_itypes', 0 ); + t::lib::Mocks::mock_preference( 'item-level_itypes', 0 ); ok( $item->effective_itemtype() eq 'BIB_LEVEL', '$item->itemtype() returns biblioitem.itemtype when item-level_itypes is disabled' ); - C4::Context->set_preference( 'item-level_itypes', 1 ); + t::lib::Mocks::mock_preference( 'item-level_itypes', 1 ); ok( $item->effective_itemtype() eq 'ITEM_LEVEL', '$item->itemtype() returns items.itype when item-level_itypes is enabled' ); # If itemtype is not defined and item-level_level item types are set @@ -280,7 +280,7 @@ subtest 'SearchItems test' => sub { source => 'Branch', }); - C4::Context->set_preference('marcflavour', 'MARC21'); + t::lib::Mocks::mock_preference('marcflavour', 'MARC21'); my $cpl_items_before = SearchItemsByField( 'homebranch', $library1->{branchcode}); my ($biblionumber) = get_biblio(); @@ -444,7 +444,7 @@ subtest 'Koha::Item(s) tests' => sub { }); # Create a biblio and item for testing - C4::Context->set_preference('marcflavour', 'MARC21'); + t::lib::Mocks::mock_preference('marcflavour', 'MARC21'); my ($bibnum, $bibitemnum) = get_biblio(); my ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $library1->{branchcode}, holdingbranch => $library2->{branchcode} } , $bibnum); diff --git a/t/db_dependent/Koha/Patron/Messages.t b/t/db_dependent/Koha/Patron/Messages.t index 22bf694..eb14a53 100644 --- a/t/db_dependent/Koha/Patron/Messages.t +++ b/t/db_dependent/Koha/Patron/Messages.t @@ -27,6 +27,7 @@ use Koha::Patron::Message; use Koha::Patron::Messages; use Koha::Database; +use t::lib::Mocks; use t::lib::TestBuilder; my $schema = Koha::Database->new->schema; @@ -38,7 +39,7 @@ my $patron = $builder->build( { source => 'Borrower', values => { branch my $nb_of_logaction = get_nb_of_logactions(); my $nb_of_messages = Koha::Patron::Messages->search->count; -C4::Context->set_preference('BorrowersLog', 0); +t::lib::Mocks::mock_preference('BorrowersLog', 0); my $new_message_1 = Koha::Patron::Message->new( { borrowernumber => $patron->{borrowernumber}, branchcode => $library->{branchcode}, @@ -48,7 +49,7 @@ my $new_message_1 = Koha::Patron::Message->new( )->store; is( get_nb_of_logactions(), $nb_of_logaction, 'With BorrowersLog off, no new log should have been added' ); -C4::Context->set_preference('BorrowersLog', 1); +t::lib::Mocks::mock_preference('BorrowersLog', 1); my $new_message_2 = Koha::Patron::Message->new( { borrowernumber => $patron->{borrowernumber}, branchcode => $library->{branchcode}, @@ -64,12 +65,12 @@ is( Koha::Patron::Messages->search->count, $nb_of_messages + 2, 'The 2 messages my $retrieved_message_1 = Koha::Patron::Messages->find( $new_message_1->message_id ); is( $retrieved_message_1->message, $new_message_1->message, 'Find a message by id should return the correct message' ); -C4::Context->set_preference('BorrowersLog', 0); +t::lib::Mocks::mock_preference('BorrowersLog', 0); $retrieved_message_1->delete; is( Koha::Patron::Messages->search->count, $nb_of_messages + 1, 'Delete should have deleted the message 1' ); is( get_nb_of_logactions(), $nb_of_logaction + 1, 'With BorrowersLog off, no new log should have been added when deleting a new message' ); -C4::Context->set_preference('BorrowersLog', 1); +t::lib::Mocks::mock_preference('BorrowersLog', 1); $new_message_2->delete; is( Koha::Patron::Messages->search->count, $nb_of_messages, 'Delete should have deleted the message 2' ); is( get_nb_of_logactions(), $nb_of_logaction + 2, 'With BorrowersLog on, 1 new log should have been added when deleting a new message' ); diff --git a/t/db_dependent/Koha_template_plugin_KohaDates.t b/t/db_dependent/Koha_template_plugin_KohaDates.t index d48e3c9..fe18836 100644 --- a/t/db_dependent/Koha_template_plugin_KohaDates.t +++ b/t/db_dependent/Koha_template_plugin_KohaDates.t @@ -5,6 +5,7 @@ use C4::Context; use Koha::DateUtils; use Test::More tests => 7; use Test::MockModule; +use t::lib::Mocks; BEGIN { use_ok('Koha::Template::Plugin::KohaDates'); @@ -18,20 +19,20 @@ my $context = C4::Context->new(); my $filter = Koha::Template::Plugin::KohaDates->new(); ok ($filter, "new()"); -$context->set_preference( "dateformat", 'iso' ); +t::lib::Mocks::mock_preference( "dateformat", 'iso' ); $context->clear_syspref_cache(); my $filtered_date = $filter->filter($date); is ($filtered_date,$date, "iso conversion") or diag ("iso conversion fails"); #$filter = Koha::Template::Plugin::KohaDates->new(); -$context->set_preference( "dateformat", 'us' ); +t::lib::Mocks::mock_preference( "dateformat", 'us' ); $context->clear_syspref_cache(); $filtered_date = $filter->filter($date); is ($filtered_date,'05/21/1973', "us conversion") or diag ("us conversion fails $filtered_date"); -$context->set_preference( "dateformat", 'metric' ); +t::lib::Mocks::mock_preference( "dateformat", 'metric' ); $context->clear_syspref_cache(); $filtered_date = $filter->filter($date); diff --git a/t/db_dependent/Languages.t b/t/db_dependent/Languages.t index d27e823..0ea553a 100755 --- a/t/db_dependent/Languages.t +++ b/t/db_dependent/Languages.t @@ -10,6 +10,7 @@ use Test::More tests => 16; use List::Util qw(first); use Data::Dumper; use Test::Warn; +use t::lib::Mocks; BEGIN { use_ok('C4::Languages'); @@ -27,7 +28,7 @@ is(C4::Languages::accept_language(),undef, 'test that accept_languages returns u ok(C4::Languages::getAllLanguages(), 'test get all languages'); -C4::Context->set_preference('AdvancedSearchLanguages', ''); +t::lib::Mocks::mock_preference('AdvancedSearchLanguages', ''); my $all_languages = C4::Languages::getAllLanguages('eng'); ok(@$all_languages > 10, 'retrieved a bunch of languges'); @@ -37,7 +38,7 @@ is_deeply($languages, $all_languages, 'getLanguages() and getAllLanguages() retu $languages = C4::Languages::getLanguages('eng', 1); is_deeply($languages, $all_languages, 'getLanguages() and getAllLanguages() with filtering selected but AdvancedSearchLanguages blank return the same list'); -C4::Context->set_preference('AdvancedSearchLanguages', 'ita|eng'); +t::lib::Mocks::mock_preference('AdvancedSearchLanguages', 'ita|eng'); $languages = C4::Languages::getLanguages('eng', 1); is(scalar(@$languages), 2, 'getLanguages() filtering using AdvancedSearchLanguages works'); diff --git a/t/db_dependent/Letters.t b/t/db_dependent/Letters.t index abf7d88..ed5c972 100644 --- a/t/db_dependent/Letters.t +++ b/t/db_dependent/Letters.t @@ -404,7 +404,7 @@ C4::Bookseller::ModBookseller($bookseller); $bookseller = Koha::Acquisition::Bookseller->fetch({ id => $booksellerid }); # Ensure that the preference 'LetterLog' is set to logging -C4::Context->set_preference( 'LetterLog', 'on' ); +t::lib::Mocks::mock_preference( 'LetterLog', 'on' ); { warning_is { diff --git a/t/db_dependent/Members.t b/t/db_dependent/Members.t index 97cf9e1..f0b80fe 100755 --- a/t/db_dependent/Members.t +++ b/t/db_dependent/Members.t @@ -23,6 +23,7 @@ use Data::Dumper; use C4::Context; use Koha::Database; +use t::lib::Mocks; use t::lib::TestBuilder; BEGIN { @@ -130,7 +131,7 @@ ok ( $changedmember->{firstname} eq $CHANGED_FIRSTNAME && , "Member Changed") or diag("Mismatching member details: ".Dumper($member, $changedmember)); -C4::Context->set_preference( 'CardnumberLength', '' ); +t::lib::Mocks::mock_preference( 'CardnumberLength', '' ); C4::Context->clear_syspref_cache(); my $checkcardnum=C4::Members::checkcardnumber($CARDNUMBER, ""); @@ -139,7 +140,7 @@ is ($checkcardnum, "1", "Card No. in use"); $checkcardnum=C4::Members::checkcardnumber($IMPOSSIBLE_CARDNUMBER, ""); is ($checkcardnum, "0", "Card No. not used"); -C4::Context->set_preference( 'CardnumberLength', '4' ); +t::lib::Mocks::mock_preference( 'CardnumberLength', '4' ); C4::Context->clear_syspref_cache(); $checkcardnum=C4::Members::checkcardnumber($IMPOSSIBLE_CARDNUMBER, ""); @@ -147,13 +148,13 @@ is ($checkcardnum, "2", "Card number is too long"); -C4::Context->set_preference( 'AutoEmailPrimaryAddress', 'OFF' ); +t::lib::Mocks::mock_preference( 'AutoEmailPrimaryAddress', 'OFF' ); C4::Context->clear_syspref_cache(); my $notice_email = GetNoticeEmailAddress($member->{'borrowernumber'}); is ($notice_email, $EMAIL, "GetNoticeEmailAddress returns correct value when AutoEmailPrimaryAddress is off"); -C4::Context->set_preference( 'AutoEmailPrimaryAddress', 'emailpro' ); +t::lib::Mocks::mock_preference( 'AutoEmailPrimaryAddress', 'emailpro' ); C4::Context->clear_syspref_cache(); $notice_email = GetNoticeEmailAddress($member->{'borrowernumber'}); @@ -278,15 +279,15 @@ subtest 'GetMemberAccountBalance' => sub { 'Expected 15 outstanding for both borrowernumber and cardnumber.'); # do not count holds charges - C4::Context->set_preference( 'HoldsInNoissuesCharge', '1' ); - C4::Context->set_preference( 'ManInvInNoissuesCharge', '0' ); + t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', '1' ); + t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge', '0' ); my ($total, $total_minus_charges, $other_charges) = C4::Members::GetMemberAccountBalance(123); is( $total, 15 , "Total calculated correctly"); is( $total_minus_charges, 15, "Holds charges are not count if HoldsInNoissuesCharge=1"); is( $other_charges, 0, "Holds charges are not considered if HoldsInNoissuesCharge=1"); - C4::Context->set_preference( 'HoldsInNoissuesCharge', '0' ); + t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', '0' ); ($total, $total_minus_charges, $other_charges) = C4::Members::GetMemberAccountBalance(123); is( $total, 15 , "Total calculated correctly"); @@ -308,8 +309,8 @@ subtest 'purgeSelfRegistration' => sub { #purge members in temporary category my $c= 'XYZ'; $dbh->do("INSERT IGNORE INTO categories (categorycode) VALUES ('$c')"); - C4::Context->set_preference('PatronSelfRegistrationDefaultCategory', $c ); - C4::Context->set_preference('PatronSelfRegistrationExpireTemporaryAccountsDelay', 360); + t::lib::Mocks::mock_preference('PatronSelfRegistrationDefaultCategory', $c ); + t::lib::Mocks::mock_preference('PatronSelfRegistrationExpireTemporaryAccountsDelay', 360); C4::Members::DeleteExpiredOpacRegistrations(); $dbh->do("INSERT INTO borrowers (surname, address, city, branchcode, categorycode, dateenrolled) VALUES ('Testaabbcc', 'Street 1', 'CITY', ?, '$c', '2014-01-01 01:02:03')", undef, $library1->{branchcode}); is( C4::Members::DeleteExpiredOpacRegistrations(), 1, 'Test for DeleteExpiredOpacRegistrations'); diff --git a/t/db_dependent/Members/AddEnrolmentFeeIfNeeded.t b/t/db_dependent/Members/AddEnrolmentFeeIfNeeded.t index 69c9e7f..cc0a22c 100644 --- a/t/db_dependent/Members/AddEnrolmentFeeIfNeeded.t +++ b/t/db_dependent/Members/AddEnrolmentFeeIfNeeded.t @@ -5,6 +5,7 @@ use C4::Context; use C4::Members; use Koha::Database; +use t::lib::Mocks; use t::lib::TestBuilder; my $schema = Koha::Database->schema; @@ -52,7 +53,7 @@ $borrower_data{borrowernumber} = $borrowernumber; my ( $total ) = C4::Members::GetMemberAccountRecords( $borrowernumber ); is( $total, $enrolmentfee_K, "New kid pay $enrolmentfee_K" ); -C4::Context->set_preference( 'FeeOnChangePatronCategory', 0 ); +t::lib::Mocks::mock_preference( 'FeeOnChangePatronCategory', 0 ); $borrower_data{categorycode} = 'J'; C4::Members::ModMember( %borrower_data ); ( $total ) = C4::Members::GetMemberAccountRecords( $borrowernumber ); @@ -60,7 +61,7 @@ is( $total, $enrolmentfee_K , "Kid growing and become a juvenile, but shouldn't $borrower_data{categorycode} = 'K'; C4::Members::ModMember( %borrower_data ); -C4::Context->set_preference( 'FeeOnChangePatronCategory', 1 ); +t::lib::Mocks::mock_preference( 'FeeOnChangePatronCategory', 1 ); $borrower_data{categorycode} = 'J'; C4::Members::ModMember( %borrower_data ); diff --git a/t/db_dependent/QueryParser.t b/t/db_dependent/QueryParser.t index 1e44b90..a8d7bdc 100644 --- a/t/db_dependent/QueryParser.t +++ b/t/db_dependent/QueryParser.t @@ -20,9 +20,10 @@ use Modern::Perl; use Test::More tests => 7; use File::Basename; +use t::lib::Mocks; use C4::Context; -C4::Context->set_preference( "UseQueryParser", 1 ); +t::lib::Mocks::mock_preference( "UseQueryParser", 1 ); my $QParser = C4::Context->queryparser(); # Check initialization correctly parsed the config file diff --git a/t/db_dependent/Record.t b/t/db_dependent/Record.t index 8ad07fd..62c1564 100755 --- a/t/db_dependent/Record.t +++ b/t/db_dependent/Record.t @@ -5,6 +5,7 @@ use Modern::Perl; use Test::More tests => 13; use MARC::Record; +use t::lib::Mocks; use C4::Context; BEGIN { @@ -16,7 +17,7 @@ my $dbh = C4::Context->dbh; $dbh->{AutoCommit} = 0; $dbh->{RaiseError} = 1; -C4::Context->set_preference( "BibtexExportAdditionalFields", q{} ); +t::lib::Mocks::mock_preference( "BibtexExportAdditionalFields", q{} ); my @marcarray=marc2marc; is ($marcarray[0],"Feature not yet implemented\n","error works"); @@ -115,7 +116,7 @@ my $test6xml=qq(\@book{testID, is ($bibtex, $test6xml, "testing bibtex"); -C4::Context->set_preference( "BibtexExportAdditionalFields", "'\@': 260\$b\ntest: 260\$b" ); +t::lib::Mocks::mock_preference( "BibtexExportAdditionalFields", "'\@': 260\$b\ntest: 260\$b" ); $bibtex = marc2bibtex( $marc, 'testID' ); my $test7xml = qq(\@Scholastic{testID, \tauthor = {Rowling, J.K.}, @@ -126,7 +127,7 @@ my $test7xml = qq(\@Scholastic{testID, } ); is( $bibtex, $test7xml, "testing bibtex" ); -C4::Context->set_preference( "BibtexExportAdditionalFields", q{} ); +t::lib::Mocks::mock_preference( "BibtexExportAdditionalFields", q{} ); $marc->append_fields(MARC::Field->new( '264', '3', '1', b => 'Reprints', c => '2011' diff --git a/t/db_dependent/Reserves.t b/t/db_dependent/Reserves.t index 8a6ab1c..cfc6eee 100755 --- a/t/db_dependent/Reserves.t +++ b/t/db_dependent/Reserves.t @@ -129,21 +129,21 @@ is($status, "Reserved", "CheckReserves Test 2"); is($status, "Reserved", "CheckReserves Test 3"); my $ReservesControlBranch = C4::Context->preference('ReservesControlBranch'); -C4::Context->set_preference( 'ReservesControlBranch', 'ItemHomeLibrary' ); +t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'ItemHomeLibrary' ); ok( 'ItemHomeLib' eq GetReservesControlBranch( { homebranch => 'ItemHomeLib' }, { branchcode => 'PatronHomeLib' } ), "GetReservesControlBranch returns item home branch when set to ItemHomeLibrary" ); -C4::Context->set_preference( 'ReservesControlBranch', 'PatronLibrary' ); +t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' ); ok( 'PatronHomeLib' eq GetReservesControlBranch( { homebranch => 'ItemHomeLib' }, { branchcode => 'PatronHomeLib' } ), "GetReservesControlBranch returns patron home branch when set to PatronLibrary" ); -C4::Context->set_preference( 'ReservesControlBranch', $ReservesControlBranch ); +t::lib::Mocks::mock_preference( 'ReservesControlBranch', $ReservesControlBranch ); ### ### Regression test for bug 10272 @@ -266,7 +266,7 @@ AddReserve('CPL', $requesters{'CPL'}, $bibnum2, $title, $checkitem, $found); # Ensure that the item's home library controls hold policy lookup -C4::Context->set_preference( 'ReservesControlBranch', 'ItemHomeLibrary' ); +t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'ItemHomeLibrary' ); my $messages; # Return the CPL item at FPL. The hold that should be triggered is @@ -282,7 +282,7 @@ is( $messages->{ResFound}->{borrowernumber}, # and RPL imposes no restrictions on whose holds its items can fill. # Ensure that the preference 'LocalHoldsPriority' is not set (Bug 15244): -C4::Context->set_preference( 'LocalHoldsPriority', '' ); +t::lib::Mocks::mock_preference( 'LocalHoldsPriority', '' ); (undef, $messages, undef, undef) = AddReturn('bug10272_FPL', 'FPL'); is( $messages->{ResFound}->{borrowernumber}, @@ -325,7 +325,7 @@ is( $status, 'Reserved', 'CheckReserves also returns reserve with lookahead'); # Test 9761b: Add a reserve with future date, CheckReserve should not return it $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum)); -C4::Context->set_preference('AllowHoldDateInFuture', 1); +t::lib::Mocks::mock_preference('AllowHoldDateInFuture', 1); $resdate= dt_from_string(); $resdate->add_duration(DateTime::Duration->new(days => 4)); $resdate=output_pref($resdate); @@ -345,13 +345,13 @@ is( $status, 'Reserved', 'CheckReserves returns future reserve with sufficient l # Test 9761d: Check ResFound message of AddReturn for future hold # Note that AddReturn is in Circulation.pm, but this test really pertains to reserves; AddReturn uses the ConfirmFutureHolds pref when calling CheckReserves # In this test we do not need an issued item; it is just a 'checkin' -C4::Context->set_preference('ConfirmFutureHolds', 0); +t::lib::Mocks::mock_preference('ConfirmFutureHolds', 0); (my $doreturn, $messages)= AddReturn('97531','CPL'); is($messages->{ResFound}//'', '', 'AddReturn does not care about future reserve when ConfirmFutureHolds is off'); -C4::Context->set_preference('ConfirmFutureHolds', 3); +t::lib::Mocks::mock_preference('ConfirmFutureHolds', 3); ($doreturn, $messages)= AddReturn('97531','CPL'); is(exists $messages->{ResFound}?1:0, 0, 'AddReturn ignores future reserve beyond ConfirmFutureHolds days'); -C4::Context->set_preference('ConfirmFutureHolds', 7); +t::lib::Mocks::mock_preference('ConfirmFutureHolds', 7); ($doreturn, $messages)= AddReturn('97531','CPL'); is(exists $messages->{ResFound}?1:0, 1, 'AddReturn considers future reserve within ConfirmFutureHolds days'); @@ -382,8 +382,8 @@ ok(defined($letter), 'can successfully generate hold slip (bug 10949)'); # Tests for bug 9788: Does GetReservesFromItemnumber return a future wait? # 9788a: GetReservesFromItemnumber does not return future next available hold $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum)); -C4::Context->set_preference('ConfirmFutureHolds', 2); -C4::Context->set_preference('AllowHoldDateInFuture', 1); +t::lib::Mocks::mock_preference('ConfirmFutureHolds', 2); +t::lib::Mocks::mock_preference('AllowHoldDateInFuture', 1); $resdate= dt_from_string(); $resdate->add_duration(DateTime::Duration->new(days => 2)); $resdate=output_pref($resdate); @@ -435,7 +435,7 @@ AddReserve('CPL', $requesters{'CPL2'}, $bibnum, $p = C4::Reserves::CalculatePriority($bibnum); is($p, 2, 'CalculatePriority should now return priority 2'); #add another future hold -C4::Context->set_preference('AllowHoldDateInFuture', 1); +t::lib::Mocks::mock_preference('AllowHoldDateInFuture', 1); $resdate= dt_from_string(); $resdate->add_duration(DateTime::Duration->new(days => 1)); AddReserve('CPL', $requesters{'CPL3'}, $bibnum, @@ -503,7 +503,7 @@ is($cancancel, 0, 'Reserve in waiting status cant be canceled'); ####### Testing Bug 13113 - Prevent juvenile/children from reserving ageRestricted material >>> #### -C4::Context->set_preference( 'AgeRestrictionMarker', 'FSK|PEGI|Age|K' ); +t::lib::Mocks::mock_preference( 'AgeRestrictionMarker', 'FSK|PEGI|Age|K' ); #Reserving an not-agerestricted Biblio by a Borrower with no dateofbirth is tested previously. @@ -632,8 +632,8 @@ is( int( $bz14464_fines ), 42, 'Bug 14464 - Fine applied after cancelling reserv # tests for MoveReserve in relation to ConfirmFutureHolds (BZ 14526) # hold from A pos 1, today, no fut holds: MoveReserve should fill it $dbh->do('DELETE FROM reserves', undef, ($bibnum)); -C4::Context->set_preference('ConfirmFutureHolds', 0); -C4::Context->set_preference('AllowHoldDateInFuture', 1); +t::lib::Mocks::mock_preference('ConfirmFutureHolds', 0); +t::lib::Mocks::mock_preference('AllowHoldDateInFuture', 1); AddReserve('CPL', $borrowernumber, $item_bibnum, $bibitems, 1, undef, $expdate, $notes, $title, $checkitem, ''); MoveReserve( $itemnumber, $borrowernumber ); @@ -656,7 +656,7 @@ MoveReserve( $itemnumber, $borrowernumber ); is( $status, 'Reserved', 'MoveReserve did not fill future hold'); $dbh->do('DELETE FROM reserves', undef, ($bibnum)); # hold from A pos 1, tomorrow, fut holds=2: MoveReserve should fill it -C4::Context->set_preference('ConfirmFutureHolds', 2); +t::lib::Mocks::mock_preference('ConfirmFutureHolds', 2); AddReserve('CPL', $borrowernumber, $item_bibnum, $bibitems, 1, $resdate, $expdate, $notes, $title, $checkitem, ''); MoveReserve( $itemnumber, $borrowernumber ); diff --git a/t/db_dependent/Suggestions.t b/t/db_dependent/Suggestions.t index 62481d1..a960331 100644 --- a/t/db_dependent/Suggestions.t +++ b/t/db_dependent/Suggestions.t @@ -17,6 +17,7 @@ use Modern::Perl; +use t::lib::Mocks; use C4::Context; use C4::Members; use C4::Letters; @@ -344,11 +345,11 @@ is( @$suggestions, 1, 'DelSuggestion deletes one suggestion' ); is( $suggestions->[0]->{title}, $del_suggestion->{title}, 'DelSuggestion deletes the correct suggestion' ); ## Bug 11466, making sure GetSupportList() returns itemtypes, even if AdvancedSearchTypes has multiple values -C4::Context->set_preference("AdvancedSearchTypes", 'itemtypes|loc|ccode'); +t::lib::Mocks::mock_preference("AdvancedSearchTypes", 'itemtypes|loc|ccode'); my $itemtypes1 = C4::Koha::GetSupportList(); is(@$itemtypes1, 8, "Purchase suggestion itemtypes collected, multiple AdvancedSearchTypes"); -C4::Context->set_preference("AdvancedSearchTypes", 'itemtypes'); +t::lib::Mocks::mock_preference("AdvancedSearchTypes", 'itemtypes'); my $itemtypes2 = C4::Koha::GetSupportList(); is(@$itemtypes2, 8, "Purchase suggestion itemtypes collected, default AdvancedSearchTypes"); diff --git a/t/db_dependent/XISBN.t b/t/db_dependent/XISBN.t index 9486274..2f6d63b 100755 --- a/t/db_dependent/XISBN.t +++ b/t/db_dependent/XISBN.t @@ -11,6 +11,7 @@ use C4::Biblio; use C4::XISBN; use C4::Context; use C4::Search; +use t::lib::Mocks; use Test::MockModule; BEGIN { @@ -54,8 +55,8 @@ is( $trial->{biblionumber}, $biblionumber1, "Gets biblionumber like the previous test." ); ## Test ThingISBN -$context->set_preference( 'ThingISBN', 1 ); -$context->set_preference( 'XISBN', 0 ); +t::lib::Mocks::mock_preference( 'ThingISBN', 1 ); +t::lib::Mocks::mock_preference( 'XISBN', 0 ); my $results_thingisbn; eval { $results_thingisbn = C4::XISBN::get_xisbns($isbn1); }; @@ -68,8 +69,8 @@ SKIP: { } ## Test XISBN -$context->set_preference( 'ThingISBN', 0 ); -$context->set_preference( 'XISBN', 1 ); +t::lib::Mocks::mock_preference( 'ThingISBN', 0 ); +t::lib::Mocks::mock_preference( 'XISBN', 1 ); my $results_xisbn; eval { $results_xisbn = C4::XISBN::get_xisbns($isbn1); }; diff --git a/t/db_dependent/default_search_class.pl b/t/db_dependent/default_search_class.pl index 905ae1d..a95a4b6 100755 --- a/t/db_dependent/default_search_class.pl +++ b/t/db_dependent/default_search_class.pl @@ -24,9 +24,10 @@ the default search class from the QueryParser object =cut +use t::lib::Mocks; use C4::Context; -C4::Context->set_preference("UseQueryParser","1"); +t::lib::Mocks::mock_preference("UseQueryParser","1"); my $QParser = C4::Context->queryparser(); my $default_search_class = $QParser->default_search_class(); -- 1.7.10.4