From 142eb5ed387c2ef65df8a02f9d63bb531067da36 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Tue, 22 Sep 2015 17:34:16 +0100 Subject: [PATCH] Bug 14878: Tests - Create the branchcodes when needed Tests assume that the branchcodes CPL/MPL/etc. already exist in the DB. If they need them, they should create them. Test plan: Execute the differente test files on a DB without any branchcode or at least without CPL/MPL branches. Confirm that the tests pass. Signed-off-by: Tomas Cohen Arazi Signed-off-by: Kyle M Hall --- t/db_dependent/Accounts.t | 12 +- t/db_dependent/Acquisition/OrderUsers.t | 17 +- t/db_dependent/Borrower_Debarments.t | 16 +- t/db_dependent/Borrower_Discharge.t | 24 +-- t/db_dependent/Borrower_Files.t | 16 +- t/db_dependent/Budgets.t | 22 +-- t/db_dependent/Circulation.t | 57 ++++--- t/db_dependent/Circulation/IsItemIssued.t | 22 ++- .../IssuingRules/maxsuspensiondays.t | 14 +- t/db_dependent/Circulation/Returns.t | 23 ++- t/db_dependent/Circulation_transfers.t | 60 ++----- t/db_dependent/CourseReserves.t | 19 ++- t/db_dependent/Creators/Lib.t | 30 ++-- t/db_dependent/Holds/LocalHoldsPriority.t | 27 +++- t/db_dependent/Holds/RevertWaitingStatus.t | 18 ++- t/db_dependent/HoldsQueue.t | 131 ++++++++-------- t/db_dependent/Items.t | 146 +++++++++++------- t/db_dependent/Items_DelItem.t | 16 +- .../Koha_template_plugin_Branches.t | 17 +- t/db_dependent/Letters.t | 40 +++-- t/db_dependent/Members.t | 30 ++-- .../Members/AddEnrolmentFeeIfNeeded.t | 15 +- t/db_dependent/Members_Attributes.t | 14 +- t/db_dependent/Ratings.t | 15 +- t/db_dependent/Reserves/GetReserveFee.t | 9 +- t/db_dependent/Serials_2.t | 20 ++- t/db_dependent/ShelfBrowser.t | 17 +- 27 files changed, 504 insertions(+), 343 deletions(-) diff --git a/t/db_dependent/Accounts.t b/t/db_dependent/Accounts.t index 4fa582c241..991de12014 100644 --- a/t/db_dependent/Accounts.t +++ b/t/db_dependent/Accounts.t @@ -49,17 +49,19 @@ can_ok( 'C4::Accounts', my $schema = Koha::Database->new->schema; $schema->storage->txn_begin; +my $dbh = C4::Context->dbh; my $builder = t::lib::TestBuilder->new(); -my $dbh = C4::Context->dbh; -$dbh->{RaiseError}=1; -$dbh->{AutoCommit}=0; +my $library = $builder->build({ + source => 'Branch', +}); + $dbh->do(q|DELETE FROM accountlines|); $dbh->do(q|DELETE FROM issues|); $dbh->do(q|DELETE FROM borrowers|); -my $branchcode = 'CPL'; +my $branchcode = $library->{branchcode}; my $borrower_number; my $context = new Test::MockModule('C4::Context'); @@ -293,6 +295,4 @@ subtest "makepartialpayment() tests" => sub { } }; -$dbh->rollback; - 1; diff --git a/t/db_dependent/Acquisition/OrderUsers.t b/t/db_dependent/Acquisition/OrderUsers.t index 02bb815634..7d051e8e7a 100644 --- a/t/db_dependent/Acquisition/OrderUsers.t +++ b/t/db_dependent/Acquisition/OrderUsers.t @@ -8,10 +8,15 @@ use C4::Letters; use Koha::Database; use Koha::Acquisition::Order; -my $schema = Koha::Database->new()->schema(); -$schema->storage->txn_begin(); -my $dbh = C4::Context->dbh; -$dbh->{RaiseError} = 1; +use t::lib::TestBuilder; + +my $schema = Koha::Database->schema; +$schema->storage->txn_begin; +my $builder = t::lib::TestBuilder->new; + +my $library = $builder->build({ + source => "Branch", +}); # Creating some orders my $booksellerid = C4::Bookseller::AddBookseller( @@ -65,7 +70,7 @@ my $borrowernumber = C4::Members::AddMember( firstname => 'TESTFN', surname => 'TESTSN', categorycode => 'S', - branchcode => 'CPL', + branchcode => $library->{branchcode}, dateofbirth => '', dateexpiry => '9999-12-31', userid => 'TESTUSERID' @@ -109,5 +114,3 @@ ModReceiveOrder( $messages = C4::Letters::GetQueuedMessages({ borrowernumber => $borrowernumber }); is( scalar( @$messages ), 1, 'The letter has been sent to message queue on receiving the order'); - -$schema->storage->txn_rollback(); diff --git a/t/db_dependent/Borrower_Debarments.t b/t/db_dependent/Borrower_Debarments.t index 1c1d9bfb3e..b05a57ca5a 100755 --- a/t/db_dependent/Borrower_Debarments.t +++ b/t/db_dependent/Borrower_Debarments.t @@ -4,20 +4,28 @@ use Modern::Perl; use C4::Context; use C4::Members; +use Koha::Database; + +use t::lib::TestBuilder; use Test::More tests => 31; use_ok('Koha::Borrower::Debarments'); +my $schema = Koha::Database->schema; +$schema->storage->txn_begin; +my $builder = t::lib::TestBuilder->new; my $dbh = C4::Context->dbh; -$dbh->{AutoCommit} = 0; -$dbh->{RaiseError} = 1; + +my $library = $builder->build({ + source => 'Branch', +}); my $borrowernumber = AddMember( firstname => 'my firstname', surname => 'my surname', categorycode => 'S', - branchcode => 'CPL', + branchcode => $library->{branchcode}, ); my $success = AddDebarment({ @@ -153,5 +161,3 @@ is( IsDebarred( $borrowernumber ), undef, 'A patron without a debarred date is n $dbh->do(q|UPDATE borrowers SET debarred = '9999-12-31'|); # Note: Change this test before the first of January 10000! is( IsDebarred( $borrowernumber ), '9999-12-31', 'A patron with a debarred date in the future is debarred' ); - -$dbh->rollback; diff --git a/t/db_dependent/Borrower_Discharge.t b/t/db_dependent/Borrower_Discharge.t index d05b4a8bbc..99cb58a5eb 100644 --- a/t/db_dependent/Borrower_Discharge.t +++ b/t/db_dependent/Borrower_Discharge.t @@ -25,10 +25,11 @@ use C4::Context; use C4::Items qw( AddItem ); use C4::Members qw( AddMember GetMember ); -use t::lib::TestBuilder; use Koha::Borrower::Discharge; use Koha::Database; +use t::lib::TestBuilder; + my $schema = Koha::Database->new->schema; $schema->storage->txn_begin; @@ -37,33 +38,38 @@ my $builder = t::lib::TestBuilder->new; my $dbh = C4::Context->dbh; $dbh->do(q|DELETE FROM discharges|); +my $library = $builder->build({ + source => 'Branch', +}); +my $another_library = $builder->build({ + source => 'Branch', +}); + C4::Context->_new_userenv('xxx'); -C4::Context->set_userenv(0, 0, 0, 'firstname', 'surname', 'CPL', 'CPL', '', '', '', '', ''); -my $branchcode = 'CPL'; -my $another_branchcode = 'MPL'; +C4::Context->set_userenv(0, 0, 0, 'firstname', 'surname', $library->{branchcode}, $library->{branchcode}, '', '', '', '', ''); my $borrower = $builder->build({ source => 'Borrower', value => { - branchcode => $branchcode, + branchcode => $library->{branchcode}, } }); my $borrower2 = $builder->build({ source => 'Borrower', value => { - branchcode => $branchcode, + branchcode => $library->{branchcode}, } }); my $borrower3 = $builder->build({ source => 'Borrower', value => { - branchcode => $another_branchcode, + branchcode => $another_library->{branchcode}, } }); # Discharge not possible with issues my ( $biblionumber ) = AddBiblio( MARC::Record->new, ''); my $barcode = 'BARCODE42'; -my ( undef, undef, $itemnumber ) = AddItem({ homebranch => 'CPL', holdingbranch => 'CPL', barcode => $barcode }, $biblionumber); +my ( undef, undef, $itemnumber ) = AddItem({ homebranch => $library->{branchcode}, holdingbranch => $library->{branchcode}, barcode => $barcode }, $biblionumber); AddIssue( $borrower, $barcode ); is( Koha::Borrower::Discharge::can_be_discharged({ borrowernumber => $borrower->{borrowernumber} }), 0, 'A patron with issues cannot be discharged' ); @@ -87,7 +93,7 @@ is( Koha::Borrower::Discharge::is_discharged( { borrowernumber => $borrower->{bo is( Koha::Borrower::Debarments::IsDebarred( $borrower->{borrowernumber} ), '9999-12-31', 'The patron has been debarred after discharge' ); is( scalar( @{ Koha::Borrower::Discharge::get_validated() } ), 3, 'There are 3 validated discharges' ); is( scalar( @{ Koha::Borrower::Discharge::get_validated( { borrowernumber => $borrower->{borrowernumber} } ) } ), 1, 'There is 1 validated discharge for a given patron' ); -is( scalar( @{ Koha::Borrower::Discharge::get_validated( { branchcode => 'CPL' } ) } ), 2, 'There is 2 validated discharges for a given branchcode' ); # This is not used in the code yet +is( scalar( @{ Koha::Borrower::Discharge::get_validated( { branchcode => $library->{branchcode} } ) } ), 2, 'There is 2 validated discharges for a given branchcode' ); # This is not used in the code yet Koha::Borrower::Debarments::DelUniqueDebarment( { 'borrowernumber' => $borrower->{borrowernumber}, 'type' => 'DISCHARGE' } ); ok( !Koha::Borrower::Debarments::IsDebarred( $borrower->{borrowernumber} ), 'The debarment has been lifted' ); ok( !Koha::Borrower::Discharge::is_discharged( { borrowernumber => $borrower->{borrowernumber} } ), 'The patron is not discharged after the restriction has been lifted' ); diff --git a/t/db_dependent/Borrower_Files.t b/t/db_dependent/Borrower_Files.t index 281cefde37..83bf893da5 100644 --- a/t/db_dependent/Borrower_Files.t +++ b/t/db_dependent/Borrower_Files.t @@ -22,23 +22,31 @@ use Modern::Perl; use C4::Context; use C4::Members; +use Koha::Database; +use t::lib::TestBuilder; + use Test::More tests => 23; use_ok('Koha::Borrower::Files'); +my $schema = Koha::Database->schema; +$schema->storage->txn_begin; +my $builder = t::lib::TestBuilder->new; my $dbh = C4::Context->dbh; -$dbh->{AutoCommit} = 0; -$dbh->{RaiseError} = 1; $dbh->do(q|DELETE FROM issues|); $dbh->do(q|DELETE FROM borrowers|); $dbh->do(q|DELETE FROM borrower_files|); +my $library = $builder->build({ + source => 'Branch', +}); + my $borrowernumber = AddMember( firstname => 'my firstname', surname => 'my surname', categorycode => 'S', - branchcode => 'CPL', + branchcode => $library->{branchcode}, ); my $bf = Koha::Borrower::Files->new( @@ -118,5 +126,3 @@ $bf->DelFile( ); $files = $bf->GetFilesInfo(); is( @$files, 0, 'DelFile delete a file' ); - -$dbh->rollback; diff --git a/t/db_dependent/Budgets.t b/t/db_dependent/Budgets.t index 51470025d5..522cb1cf07 100755 --- a/t/db_dependent/Budgets.t +++ b/t/db_dependent/Budgets.t @@ -11,23 +11,27 @@ use C4::Acquisition; use C4::Members qw( AddMember ); use Koha::Acquisition::Order; -use Koha::Database; + +use t::lib::TestBuilder; use YAML; -my $dbh = C4::Context->dbh; -my $database = Koha::Database->new(); -my $schema = $database->schema(); -$schema->storage->txn_begin(); -$dbh->{RaiseError} = 1; +my $schema = Koha::Database->new->schema; +$schema->storage->txn_begin; +my $builder = t::lib::TestBuilder->new; +my $dbh = C4::Context->dbh; $dbh->do(q|DELETE FROM aqbudgetperiods|); $dbh->do(q|DELETE FROM aqbudgets|); +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 => 'CPL' }; +$userenv = { flags => 1, id => 'my_userid', branch => $library->{branchcode} }; # # Budget Periods : @@ -530,7 +534,7 @@ for my $new_budget ( @new_budgets ) { # Test SetOwnerToFundHierarchy my $categorycode = 'S'; -my $branchcode = 'CPL'; +my $branchcode = $library->{branchcode}; my $john_doe = C4::Members::AddMember( cardnumber => '123456', firstname => 'John', @@ -581,8 +585,6 @@ is( C4::Budgets::GetBudget($budget_id2)->{budget_owner_id}, is( C4::Budgets::GetBudget($budget_id21)->{budget_owner_id}, undef, "SetOwnerToFundHierarchy should have set John Doe $john_doe for budget 21 ($budget_id21)" ); -$schema->storage->txn_rollback(); - sub _get_dependencies { my ($budget_hierarchy) = @_; my $graph; diff --git a/t/db_dependent/Circulation.t b/t/db_dependent/Circulation.t index 810b8c2e56..5004b417f4 100755 --- a/t/db_dependent/Circulation.t +++ b/t/db_dependent/Circulation.t @@ -27,32 +27,42 @@ use C4::Overdues qw(UpdateFine); use Koha::DateUtils; use Koha::Database; +use t::lib::TestBuilder; + use Test::More tests => 78 ; BEGIN { use_ok('C4::Circulation'); } +my $schema = Koha::Database->schema; +$schema->storage->txn_begin; +my $builder = t::lib::TestBuilder->new; my $dbh = C4::Context->dbh; -my $schema = Koha::Database->new()->schema(); # Start transaction $dbh->{RaiseError} = 1; -$schema->storage->txn_begin(); # Start with a clean slate $dbh->do('DELETE FROM issues'); +my $library = $builder->build({ + source => 'Branch', +}); +my $library2 = $builder->build({ + source => 'Branch', +}); + my $CircControl = C4::Context->preference('CircControl'); my $HomeOrHoldingBranch = C4::Context->preference('HomeOrHoldingBranch'); my $item = { - homebranch => 'MPL', - holdingbranch => 'MPL' + homebranch => $library2->{branchcode}, + holdingbranch => $library2->{branchcode} }; my $borrower = { - branchcode => 'MPL' + branchcode => $library2->{branchcode} }; # No userenv, PickupLibrary @@ -96,8 +106,8 @@ is( # Now, set a userenv C4::Context->_new_userenv('xxx'); -C4::Context->set_userenv(0,0,0,'firstname','surname', 'MPL', 'Midway Public Library', '', '', ''); -is(C4::Context->userenv->{branch}, 'MPL', 'userenv set'); +C4::Context->set_userenv(0,0,0,'firstname','surname', $library2->{branchcode}, 'Midway Public Library', '', '', ''); +is(C4::Context->userenv->{branch}, $library2->{branchcode}, 'userenv set'); # Userenv set, PickupLibrary C4::Context->set_preference('CircControl', 'PickupLibrary'); @@ -108,7 +118,7 @@ is( ); is( C4::Circulation::_GetCircControlBranch($item, $borrower), - 'MPL', + $library2->{branchcode}, '_GetCircControlBranch returned current branch' ); @@ -174,7 +184,7 @@ my $sth = C4::Context->dbh->prepare("SELECT COUNT(*) FROM accountlines WHERE amo $sth->execute(); my ( $original_count ) = $sth->fetchrow_array(); -C4::Context->dbh->do("INSERT INTO borrowers ( cardnumber, surname, firstname, categorycode, branchcode ) VALUES ( '99999999999', 'Hall', 'Kyle', 'S', 'MPL' )"); +C4::Context->dbh->do("INSERT INTO borrowers ( cardnumber, surname, firstname, categorycode, branchcode ) VALUES ( '99999999999', 'Hall', 'Kyle', 'S', ? )", undef, $library2->{branchcode} ); C4::Circulation::ProcessOfflinePayment({ cardnumber => '99999999999', amount => '123.45' }); @@ -200,7 +210,7 @@ C4::Context->dbh->do("DELETE FROM accountlines"); my ($biblionumber, $biblioitemnumber) = AddBiblio($biblio, ''); my $barcode = 'R00000342'; - my $branch = 'MPL'; + my $branch = $library2->{branchcode}; my ( $item_bibnum, $item_bibitemnum, $itemnumber ) = AddItem( { @@ -502,7 +512,7 @@ C4::Context->dbh->do("DELETE FROM accountlines"); LostItem( $itemnumber, 1 ); - my $item = $schema->resultset('Item')->find( $itemnumber ); + my $item = Koha::Database->new()->schema()->resultset('Item')->find($itemnumber); ok( !$item->onloan(), "Lost item marked as returned has false onloan value" ); my $total_due = $dbh->selectrow_array( @@ -522,7 +532,7 @@ C4::Context->dbh->do("DELETE FROM accountlines"); LostItem( $itemnumber2, 0 ); - my $item2 = $schema->resultset('Item')->find( $itemnumber2 ); + my $item2 = Koha::Database->new()->schema()->resultset('Item')->find($itemnumber2); ok( $item2->onloan(), "Lost item *not* marked as returned has true onloan value" ); $total_due = $dbh->selectrow_array( @@ -535,7 +545,7 @@ C4::Context->dbh->do("DELETE FROM accountlines"); my $now = dt_from_string(); my $future = dt_from_string(); $future->add( days => 7 ); - my $units = C4::Overdues::get_chargeable_units('days', $future, $now, 'MPL'); + my $units = C4::Overdues::get_chargeable_units('days', $future, $now, $library2->{branchcode}); 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 @@ -552,7 +562,7 @@ C4::Context->dbh->do("DELETE FROM accountlines"); my $barcode = 'R00000342'; my $barcode2 = 'R00000343'; my $barcode3 = 'R00000344'; - my $branch = 'MPL'; + my $branch = $library2->{branchcode}; #Create another record my $biblio2 = MARC::Record->new(); @@ -637,7 +647,7 @@ C4::Context->dbh->do("DELETE FROM accountlines"); { my $barcode = '1234567890'; - my $branch = 'MPL'; + my $branch = $library2->{branchcode}; my $biblio = MARC::Record->new(); my ($biblionumber, $biblioitemnumber) = AddBiblio($biblio, ''); @@ -692,8 +702,8 @@ C4::Context->dbh->do("DELETE FROM accountlines"); my $barcode1 = '1234'; my ( undef, undef, $itemnumber1 ) = AddItem( { - homebranch => 'MPL', - holdingbranch => 'MPL', + homebranch => $library2->{branchcode}, + holdingbranch => $library2->{branchcode}, barcode => $barcode1, }, $biblionumber @@ -701,8 +711,8 @@ C4::Context->dbh->do("DELETE FROM accountlines"); my $barcode2 = '4321'; my ( undef, undef, $itemnumber2 ) = AddItem( { - homebranch => 'MPL', - holdingbranch => 'MPL', + homebranch => $library2->{branchcode}, + holdingbranch => $library2->{branchcode}, barcode => $barcode2, }, $biblionumber @@ -712,13 +722,13 @@ C4::Context->dbh->do("DELETE FROM accountlines"); firstname => 'Kyle', surname => 'Hall', categorycode => 'S', - branchcode => 'MPL', + branchcode => $library2->{branchcode}, ); my $borrowernumber2 = AddMember( firstname => 'Chelsea', surname => 'Hall', categorycode => 'S', - branchcode => 'MPL', + branchcode => $library2->{branchcode}, ); my $borrower1 = GetMember( borrowernumber => $borrowernumber1 ); @@ -730,7 +740,7 @@ C4::Context->dbh->do("DELETE FROM accountlines"); is( $renewokay, 1, 'Bug 14337 - Verify the borrower can renew with no hold on the record' ); AddReserve( - 'MPL', $borrowernumber2, $biblionumber, + $library2->{branchcode}, $borrowernumber2, $biblionumber, '', 1, undef, undef, '', undef, undef, undef ); @@ -765,7 +775,7 @@ C4::Context->dbh->do("DELETE FROM accountlines"); { # Don't allow renewing onsite checkout my $barcode = 'R00000XXX'; - my $branch = 'CPL'; + my $branch = $library->{branchcode}; #Create another record my $biblio = MARC::Record->new(); @@ -798,5 +808,4 @@ C4::Context->dbh->do("DELETE FROM accountlines"); is( $error, 'onsite_checkout', 'A correct error code should be returned by CanBookBeRenewed for on-site checkout' ); } -$schema->storage->txn_rollback(); 1; diff --git a/t/db_dependent/Circulation/IsItemIssued.t b/t/db_dependent/Circulation/IsItemIssued.t index 18ea9843c0..2b7e67e7d2 100644 --- a/t/db_dependent/Circulation/IsItemIssued.t +++ b/t/db_dependent/Circulation/IsItemIssued.t @@ -5,21 +5,29 @@ use C4::Biblio; use C4::Circulation; use C4::Items; use C4::Members; +use Koha::Database; use Koha::DateUtils; +use t::lib::TestBuilder; + use MARC::Record; *C4::Context::userenv = \&Mock_userenv; +my $schema = Koha::Database->schema; +$schema->storage->txn_begin; +my $builder = t::lib::TestBuilder->new; my $dbh = C4::Context->dbh; -$dbh->{AutoCommit} = 0; -$dbh->{RaiseError} = 1; + +my $library = $builder->build({ + source => 'Branch', +}); my $borrowernumber = AddMember( firstname => 'my firstname', surname => 'my surname', categorycode => 'S', - branchcode => 'CPL', + branchcode => $library->{branchcode}, ); @@ -27,7 +35,7 @@ my $borrower = GetMember( borrowernumber => $borrowernumber ); my $record = MARC::Record->new(); my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $record, '' ); -my ( undef, undef, $itemnumber ) = AddItem( { homebranch => 'CPL', holdingbranch => 'CPL', barcode => 'i_dont_exist' }, $biblionumber ); +my ( undef, undef, $itemnumber ) = AddItem( { homebranch => $library->{branchcode}, holdingbranch => $library->{branchcode}, barcode => 'i_dont_exist' }, $biblionumber ); my $item = GetItem( $itemnumber ); is ( IsItemIssued( $item->{itemnumber} ), 0, "item is not on loan at first" ); @@ -41,7 +49,7 @@ is( 'item that is on loan cannot be deleted', ); -AddReturn('i_dont_exist', 'CPL'); +AddReturn('i_dont_exist', $library->{branchcode}); is ( IsItemIssued( $item->{itemnumber} ), 0, "item has been returned" ); is( @@ -50,9 +58,7 @@ is( 'item that is not on loan can be deleted', ); -$dbh->rollback; - # C4::Context->userenv sub Mock_userenv { - return { branch => 'CPL' }; + return { branch => $library->{branchcode} }; } diff --git a/t/db_dependent/Circulation/IssuingRules/maxsuspensiondays.t b/t/db_dependent/Circulation/IssuingRules/maxsuspensiondays.t index d2cf9c4399..30a336ff45 100644 --- a/t/db_dependent/Circulation/IssuingRules/maxsuspensiondays.t +++ b/t/db_dependent/Circulation/IssuingRules/maxsuspensiondays.t @@ -10,13 +10,23 @@ use C4::Biblio qw( AddBiblio ); use C4::Circulation qw( AddIssue AddReturn ); use C4::Items qw( AddItem ); use C4::Members qw( AddMember GetMember ); +use Koha::Database; use Koha::DateUtils; use Koha::Borrower::Debarments qw( GetDebarments DelDebarment ); + +use t::lib::TestBuilder; + +my $schema = Koha::Database->schema; +$schema->storage->txn_begin; +my $builder = t::lib::TestBuilder->new; my $dbh = C4::Context->dbh; $dbh->{RaiseError} = 1; -$dbh->{AutoCommit} = 0; -my $branchcode = 'CPL'; +my $library = $builder->build({ + source => 'Branch', +}); + +my $branchcode = $library->{branchcode}; local $SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /redefined/ }; my $userenv->{branch} = $branchcode; *C4::Context::userenv = \&Mock_userenv; diff --git a/t/db_dependent/Circulation/Returns.t b/t/db_dependent/Circulation/Returns.t index e8545d15b3..3d1368442a 100644 --- a/t/db_dependent/Circulation/Returns.t +++ b/t/db_dependent/Circulation/Returns.t @@ -5,23 +5,30 @@ use C4::Biblio; use C4::Circulation; use C4::Items; use C4::Members; +use Koha::Database; use Koha::DateUtils; +use t::lib::TestBuilder; + use MARC::Record; *C4::Context::userenv = \&Mock_userenv; -my $dbh = C4::Context->dbh; -$dbh->{AutoCommit} = 0; -$dbh->{RaiseError} = 1; +my $schema = Koha::Database->schema; +$schema->storage->txn_begin; +my $builder = t::lib::TestBuilder->new; + +my $library = $builder->build({ + source => 'Branch', +}); my $record = MARC::Record->new(); my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $record, '' ); my ( undef, undef, $itemnumber ) = AddItem( { - homebranch => 'CPL', - holdingbranch => 'CPL', + homebranch => $library->{branchcode}, + holdingbranch => $library->{branchcode}, barcode => 'i_dont_exist', location => 'PROC', permanent_location => 'TEST' @@ -32,7 +39,7 @@ my ( undef, undef, $itemnumber ) = AddItem( my $item; C4::Context->set_preference( "InProcessingToShelvingCart", 1 ); -AddReturn( 'i_dont_exist', 'CPL' ); +AddReturn( 'i_dont_exist', $library->{branchcode} ); $item = GetItem($itemnumber); is( $item->{location}, 'CART', "InProcessingToShelvingCart functions as intended" ); @@ -40,11 +47,11 @@ $item->{location} = 'PROC'; ModItem( $item, undef, $itemnumber ); C4::Context->set_preference( "InProcessingToShelvingCart", 0 ); -AddReturn( 'i_dont_exist', 'CPL' ); +AddReturn( 'i_dont_exist', $library->{branchcode} ); $item = GetItem($itemnumber); is( $item->{location}, 'TEST', "InProcessingToShelvingCart functions as intended" ); # C4::Context->userenv sub Mock_userenv { - return { branch => 'CPL' }; + return { branch => $library->{branchcode} }; } diff --git a/t/db_dependent/Circulation_transfers.t b/t/db_dependent/Circulation_transfers.t index 0c92222bdb..7baee3cf88 100644 --- a/t/db_dependent/Circulation_transfers.t +++ b/t/db_dependent/Circulation_transfers.t @@ -6,9 +6,12 @@ use C4::Context; use C4::Items; use C4::Branch; use C4::Circulation; +use Koha::Database; use Koha::DateUtils; use DateTime::Duration; +use t::lib::TestBuilder; + use Test::More tests => 22; use Test::Deep; @@ -26,11 +29,11 @@ can_ok( ) ); -#Start transaction -my $dbh = C4::Context->dbh; -$dbh->{RaiseError} = 1; -$dbh->{AutoCommit} = 0; +my $schema = Koha::Database->schema; +$schema->storage->txn_begin; +my $builder = t::lib::TestBuilder->new; +my $dbh = C4::Context->dbh; $dbh->do(q|DELETE FROM issues|); $dbh->do(q|DELETE FROM borrowers|); $dbh->do(q|DELETE FROM items|); @@ -40,46 +43,12 @@ $dbh->do(q|DELETE FROM branchtransfers|); #Add sample datas #Add branches -my $samplebranch1 = { - add => 1, - branchcode => 'SAB1', - branchname => 'Sample Branch', - branchaddress1 => 'sample adr1', - branchaddress2 => 'sample adr2', - branchaddress3 => 'sample adr3', - branchzip => 'sample zip', - branchcity => 'sample city', - branchstate => 'sample state', - branchcountry => 'sample country', - branchphone => 'sample phone', - branchfax => 'sample fax', - branchemail => 'sample email', - branchurl => 'sample url', - branchip => 'sample ip', - branchprinter => undef, - opac_info => 'sample opac', -}; -my $samplebranch2 = { - add => 1, - branchcode => 'SAB2', - branchname => 'Sample Branch2', - branchaddress1 => 'sample adr1_2', - branchaddress2 => 'sample adr2_2', - branchaddress3 => 'sample adr3_2', - branchzip => 'sample zip2', - branchcity => 'sample city2', - branchstate => 'sample state2', - branchcountry => 'sample country2', - branchphone => 'sample phone2', - branchfax => 'sample fax2', - branchemail => 'sample email2', - branchurl => 'sample url2', - branchip => 'sample ip2', - branchprinter => undef, - opac_info => 'sample opac2', -}; -ModBranch($samplebranch1); -ModBranch($samplebranch2); +my $samplebranch1 = $builder->build({ + source => 'Branch', +}); +my $samplebranch2 = $builder->build({ + source => 'Branch', +}); #Add biblio and items my $record = MARC::Record->new(); @@ -208,6 +177,3 @@ cmp_deeply( C4::Circulation::TransferSlip($samplebranch1->{branchcode}, undef, 1, $samplebranch2->{branchcode}), "Barcode and itemnumber for same item both generate same TransferSlip" ); - -#End transaction -$dbh->rollback; diff --git a/t/db_dependent/CourseReserves.t b/t/db_dependent/CourseReserves.t index ddb6ad9a31..33a320ff54 100755 --- a/t/db_dependent/CourseReserves.t +++ b/t/db_dependent/CourseReserves.t @@ -7,6 +7,9 @@ use Modern::Perl; use Test::More tests => 21; +use Koha::Database; +use t::lib::TestBuilder; + BEGIN { use_ok('C4::Biblio'); use_ok('C4::Context'); @@ -16,12 +19,16 @@ BEGIN { use_ok('MARC::Record'); } +my $schema = Koha::Database->schema; +$schema->storage->txn_begin; +my $builder = t::lib::TestBuilder->new; my $dbh = C4::Context->dbh; - -# Start transaction -$dbh->{AutoCommit} = 0; $dbh->{RaiseError} = 1; +my $library = $builder->build({ + source => 'Branch', +}); + my $sth = $dbh->prepare("SELECT * FROM borrowers ORDER BY RAND() LIMIT 10"); $sth->execute(); my @borrowers = @{ $sth->fetchall_arrayref( {} ) }; @@ -29,10 +36,10 @@ my @borrowers = @{ $sth->fetchall_arrayref( {} ) }; # Create the item my $record = MARC::Record->new(); $record->append_fields( - MARC::Field->new( '952', '0', '0', a => 'CPL', b => 'CPL' ) + MARC::Field->new( '952', '0', '0', a => $library->{branchcode}, b => $library->{branchcode} ) ); my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio($record, ''); -my @iteminfo = C4::Items::AddItem( { homebranch => 'CPL', holdingbranch => 'CPL' }, $biblionumber ); +my @iteminfo = C4::Items::AddItem( { homebranch => $library->{branchcode}, holdingbranch => $library->{branchcode} }, $biblionumber ); my $itemnumber = $iteminfo[2]; my $course_id = ModCourse( @@ -90,5 +97,3 @@ ok( !defined( $course_reserve->{'cr_id'} ), "DelCourseReserve functions correctl DelCourse($course_id); $course = GetCourse($course_id); ok( !defined( $course->{'course_id'} ), "DelCourse deleted course successfully" ); - -$dbh->rollback; diff --git a/t/db_dependent/Creators/Lib.t b/t/db_dependent/Creators/Lib.t index e0ac3ba8d9..fe6b4c96ec 100644 --- a/t/db_dependent/Creators/Lib.t +++ b/t/db_dependent/Creators/Lib.t @@ -21,6 +21,8 @@ use Graphics::Magick; use Test::More tests => 644; use Test::MockModule; use t::lib::Mocks; +use t::lib::TestBuilder; +use Koha::Database; use Test::Warn; BEGIN { @@ -50,9 +52,11 @@ can_ok( html_table ) ); +my $schema = Koha::Database->schema; +$schema->storage->txn_begin; +my $builder = t::lib::TestBuilder->new; + my $dbh = C4::Context->dbh; -$dbh->{AutoCommit} = 0; -$dbh->{RaiseError} = 1; $dbh->do('DELETE FROM issues'); $dbh->do('DELETE FROM creator_templates'); $dbh->do('DELETE FROM creator_layouts'); @@ -67,6 +71,16 @@ $dbh->do('DELETE FROM biblioitems'); # Inserted data ########################################################### +my $library1 = $builder->build({ + source => 'Branch', +}); +my $library2 = $builder->build({ + source => 'Branch', +}); +my $library3 = $builder->build({ + source => 'Branch', +}); + # ---------- Some Templates -------------------- my $query = ' INSERT INTO creator_templates @@ -226,11 +240,11 @@ $query = ' timestamp, branch_code, creator) VALUES (?,?,?,?,?,?)'; $insert_sth = $dbh->prepare($query); -$insert_sth->execute( 11, $item_number1, $borrowernumber1, 'now()', 'CPL', 'Labels' ); +$insert_sth->execute( 11, $item_number1, $borrowernumber1, 'now()', $library1->{branchcode}, 'Labels' ); -$insert_sth->execute( 12, $item_number2, $borrowernumber2, 'now()', 'MPL', 'Labels' ); +$insert_sth->execute( 12, $item_number2, $borrowernumber2, 'now()', $library2->{branchcode}, 'Labels' ); -$insert_sth->execute( 12, $item_number3, $borrowernumber3, 'now()', 'PVL', 'Labels' ); +$insert_sth->execute( 12, $item_number3, $borrowernumber3, 'now()', $library3->{branchcode}, 'Labels' ); ########################################################### # Testing Subs @@ -792,7 +806,7 @@ is( $batches->[1]->{batch_id}, 12, 'batch_id is good' ); is( $batches->[1]->{_item_count}, $count, 'item_number is good for this batch_id' ); # Without filter & creator params ----- -$batches = get_batch_summary( filter => 'branch_code=\'MPL\'', creator => 'Labels' ); +$batches = get_batch_summary( filter => "branch_code='$library1->{branchcode}'", creator => 'Labels' ); is( @$batches, 1, 'There is 1 batch matching' ); $query = ' @@ -802,7 +816,7 @@ $query = ' AND branch_code = ? GROUP BY batch_id '; -my ( $id, $nb ) = $dbh->selectrow_array( $query, {}, 'Labels', 'MPL' ); +my ( $id, $nb ) = $dbh->selectrow_array( $query, {}, 'Labels', $library1->{branchcode} ); is( $batches->[0]->{batch_id}, $id, 'batch_id is good' ); is( $batches->[0]->{_item_count}, $nb, 'item_number is good for this batch_id' ); @@ -1443,5 +1457,3 @@ sub mock_preference { } ); } - -$dbh->rollback; diff --git a/t/db_dependent/Holds/LocalHoldsPriority.t b/t/db_dependent/Holds/LocalHoldsPriority.t index 8bbcdb47e7..b58362bd76 100755 --- a/t/db_dependent/Holds/LocalHoldsPriority.t +++ b/t/db_dependent/Holds/LocalHoldsPriority.t @@ -11,6 +11,9 @@ use MARC::Record; use C4::Biblio; use C4::Items; use C4::Members; +use Koha::Database; + +use t::lib::TestBuilder; BEGIN { use FindBin; @@ -18,11 +21,23 @@ BEGIN { use_ok('C4::Reserves'); } -my $dbh = C4::Context->dbh; +my $schema = Koha::Database->schema; +$schema->storage->txn_begin; + +my $builder = t::lib::TestBuilder->new; -# Start transaction -$dbh->{AutoCommit} = 0; -$dbh->{RaiseError} = 1; +my $library1 = $builder->build({ + source => 'Branch', +}); +my $library2 = $builder->build({ + source => 'Branch', +}); +my $library3 = $builder->build({ + source => 'Branch', +}); +my $library4 = $builder->build({ + source => 'Branch', +}); my $borrowers_count = 5; @@ -30,9 +45,9 @@ my $borrowers_count = 5; my ( $bibnum, $title, $bibitemnum ) = create_helper_biblio(); # Create a helper item for the biblio. my ( $item_bibnum, $item_bibitemnum, $itemnumber ) = - AddItem( { homebranch => 'MPL', holdingbranch => 'CPL' }, $bibnum ); + AddItem( { homebranch => $library4->{branchcode}, holdingbranch => $library3->{branchcode} }, $bibnum ); -my @branchcodes = qw{XXX RPL CPL MPL CPL MPL}; +my @branchcodes = ( $library1->{branchcode}, $library2->{branchcode}, $library3->{branchcode}, $library4->{branchcode}, $library3->{branchcode}, $library4->{branchcode} ); # Create some borrowers my @borrowernumbers; diff --git a/t/db_dependent/Holds/RevertWaitingStatus.t b/t/db_dependent/Holds/RevertWaitingStatus.t index a7648fd078..bdb27abd7a 100755 --- a/t/db_dependent/Holds/RevertWaitingStatus.t +++ b/t/db_dependent/Holds/RevertWaitingStatus.t @@ -13,20 +13,26 @@ use C4::Items; use C4::Members; use C4::Reserves; -my $dbh = C4::Context->dbh; +use t::lib::TestBuilder; -# Start transaction -$dbh->{AutoCommit} = 0; +my $schema = Koha::Database->schema; +$schema->storage->txn_begin; +my $builder = t::lib::TestBuilder->new; +my $dbh = C4::Context->dbh; $dbh->{RaiseError} = 1; $dbh->do("DELETE FROM reserves"); $dbh->do("DELETE FROM old_reserves"); +my $library = $builder->build({ + source => 'Branch', +}); + local $SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /redefined/ }; *C4::Context::userenv = \&Mock_userenv; sub Mock_userenv { - my $userenv = { flags => 1, id => '1', branch => 'CPL' }; + my $userenv = { flags => 1, id => '1', branch => $library->{branchcode} }; return $userenv; } @@ -41,7 +47,7 @@ my ( $bibnum, $title, $bibitemnum ) = create_helper_biblio(); diag("Creating item instance for testing."); my $item_barcode = 'my_barcode'; my ( $item_bibnum, $item_bibitemnum, $itemnumber ) = AddItem( - { homebranch => 'CPL', holdingbranch => 'CPL', barcode => $item_barcode }, + { homebranch => $library->{branchcode}, holdingbranch => $library->{branchcode}, barcode => $item_barcode }, $bibnum ); # Create some borrowers @@ -51,7 +57,7 @@ foreach my $i ( 1 .. $borrowers_count ) { firstname => 'my firstname', surname => 'my surname ' . $i, categorycode => 'S', - branchcode => 'CPL', + branchcode => $library->{branchcode}, ); push @borrowernumbers, $borrowernumber; } diff --git a/t/db_dependent/HoldsQueue.t b/t/db_dependent/HoldsQueue.t index 97018a7fdb..1c07fa52c8 100755 --- a/t/db_dependent/HoldsQueue.t +++ b/t/db_dependent/HoldsQueue.t @@ -14,10 +14,12 @@ use Data::Dumper; use Test::More tests => 22; - use C4::Branch; use C4::ItemType; use C4::Members; +use Koha::Database; + +use t::lib::TestBuilder; BEGIN { use FindBin; @@ -26,29 +28,38 @@ BEGIN { use_ok('C4::HoldsQueue'); } -# Start transaction +my $schema = Koha::Database->schema; +$schema->storage->txn_begin; my $dbh = C4::Context->dbh; -$dbh->{AutoCommit} = 0; -$dbh->{RaiseError} = 1; + +my $builder = t::lib::TestBuilder->new; + +my $library1 = $builder->build({ + source => 'Branch', +}); +my $library2 = $builder->build({ + source => 'Branch', +}); +my $library3 = $builder->build({ + source => 'Branch', +}); my $TITLE = "Test Holds Queue XXX"; -my %data = ( - cardnumber => 'CARDNUMBER42', - firstname => 'my firstname', - surname => 'my surname', - categorycode => 'S', - branchcode => 'CPL', -); +my $borrower = $builder->build({ + source => 'Borrower', + value => { + categorycode => 'S', + branchcode => $library1->{branchcode}, + } +}); -my $borrowernumber = AddMember(%data); -my $borrower = GetMember( borrowernumber => $borrowernumber ); +my $borrowernumber = $borrower->{borrowernumber}; # Set special (for this test) branches my $borrower_branchcode = $borrower->{branchcode}; -my $branches = C4::Branch::GetBranches; -my @other_branches = grep { $_ ne $borrower_branchcode } keys %$branches; -my $least_cost_branch_code = pop @other_branches - or BAIL_OUT("No point testing only one branch..."); +my @branchcodes = ( $library1->{branchcode}, $library2->{branchcode}, $library3->{branchcode} ); +my @other_branches = ( $library2->{branchcode}, $library3->{branchcode} ); +my $least_cost_branch_code = pop @other_branches; my @item_types = C4::ItemType->all; my $itemtype = grep { $_->{notforloan} == 1 } @item_types or BAIL_OUT("No adequate itemtype"); @@ -155,8 +166,8 @@ ok( # XXX All this tests are for borrower branch pick-up. # Maybe needs expanding to homebranch or holdingbranch pick-up. -# Cleanup -$dbh->rollback; +$schema->txn_rollback; +$schema->txn_begin; ### Test holds queue builder does not violate holds policy ### @@ -167,39 +178,40 @@ $dbh->do("DELETE FROM default_circ_rules"); C4::Context->set_preference('UseTransportCostMatrix', 0); -my @branchcodes = keys %$branches; ( $itemtype ) = @{ $dbh->selectrow_arrayref("SELECT itemtype FROM itemtypes LIMIT 1") }; -my $borrowernumber1 = AddMember( - ( - cardnumber => 'CARDNUMBER1', - firstname => 'Firstname', - surname => 'Surname', +$library1 = $builder->build({ + source => 'Branch', +}); +$library2 = $builder->build({ + source => 'Branch', +}); +$library3 = $builder->build({ + source => 'Branch', +}); +@branchcodes = ( $library1->{branchcode}, $library2->{branchcode}, $library3->{branchcode} ); + +my $borrower1 = $builder->build({ + source => 'Borrower', + value => { categorycode => 'S', - branchcode => $branchcodes[0], - ) -); -my $borrowernumber2 = AddMember( - ( - cardnumber => 'CARDNUMBER2', - firstname => 'Firstname', - surname => 'Surname', + branchcode => $branchcodes[0], + }, +}); +my $borrower2 = $builder->build({ + source => 'Borrower', + value => { categorycode => 'S', - branchcode => $branchcodes[1], - ) -); -my $borrowernumber3 = AddMember( - ( - cardnumber => 'CARDNUMBER3', - firstname => 'Firstname', - surname => 'Surname', + branchcode => $branchcodes[1], + }, +}); +my $borrower3 = $builder->build({ + source => 'Borrower', + value => { categorycode => 'S', - branchcode => $branchcodes[2], - ) -); -my $borrower1 = GetMember( borrowernumber => $borrowernumber1 ); -my $borrower2 = GetMember( borrowernumber => $borrowernumber2 ); -my $borrower3 = GetMember( borrowernumber => $borrowernumber3 ); + branchcode => $branchcodes[2], + }, +}); $dbh->do(qq{ INSERT INTO biblio ( @@ -277,8 +289,6 @@ my $sth = $dbh->prepare(q{ $sth->execute( $borrower1->{borrowernumber}, $biblionumber, $branchcodes[0], 1 ); $sth->execute( $borrower2->{borrowernumber}, $biblionumber, $branchcodes[0], 2 ); $sth->execute( $borrower3->{borrowernumber}, $biblionumber, $branchcodes[0], 3 ); -#warn "RESERVES" . Data::Dumper::Dumper( $dbh->selectall_arrayref("SELECT * FROM reserves", { Slice => {} }) ); -#warn "ITEMS: " . Data::Dumper::Dumper( $dbh->selectall_arrayref("SELECT * FROM items WHERE biblionumber = $biblionumber", { Slice => {} }) ); my $holds_queue; @@ -286,25 +296,23 @@ $dbh->do("DELETE FROM default_circ_rules"); $dbh->do("INSERT INTO default_circ_rules ( holdallowed ) VALUES ( 1 )"); C4::HoldsQueue::CreateQueue(); $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} }); -ok( @$holds_queue == 2, "Holds queue filling correct number for default holds policy 'from home library'" ); -ok( $holds_queue->[0]->{cardnumber} eq 'CARDNUMBER1', "Holds queue filling 1st correct hold for default holds policy 'from home library'"); -ok( $holds_queue->[1]->{cardnumber} eq 'CARDNUMBER2', "Holds queue filling 2nd correct hold for default holds policy 'from home library'"); +is( @$holds_queue, 2, "Holds queue filling correct number for default holds policy 'from home library'" ); +is( $holds_queue->[0]->{cardnumber}, $borrower1->{cardnumber}, "Holds queue filling 1st correct hold for default holds policy 'from home library'"); +is( $holds_queue->[1]->{cardnumber}, $borrower2->{cardnumber}, "Holds queue filling 2nd correct hold for default holds policy 'from home library'"); $dbh->do("DELETE FROM default_circ_rules"); $dbh->do("INSERT INTO default_circ_rules ( holdallowed ) VALUES ( 2 )"); C4::HoldsQueue::CreateQueue(); $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} }); -ok( @$holds_queue == 3, "Holds queue filling correct number for holds for default holds policy 'from any library'" ); +is( @$holds_queue, 3, "Holds queue filling correct number for holds for default holds policy 'from any library'" ); #warn "HOLDS QUEUE: " . Data::Dumper::Dumper( $holds_queue ); # Bug 14297 -$borrowernumber = AddMember(%data); -$borrower = GetMember( borrowernumber => $borrowernumber ); -$borrower_branchcode = $borrower->{branchcode}; $itemtype = $item_types[0]->{itemtype}; -my $library_A = 'CPL'; -my $library_B = 'FFL'; -my $library_C = 'MPL'; # Same as our borrower's home library +$borrowernumber = $borrower3->{borrowernumber}; +my $library_A = $library1->{branchcode}; +my $library_B = $library2->{branchcode}; +my $library_C = $borrower3->{branchcode}; $dbh->do("DELETE FROM reserves"); $dbh->do("DELETE FROM issues"); $dbh->do("DELETE FROM items"); @@ -353,11 +361,12 @@ $dbh->do( "UPDATE systempreferences SET value = 0 WHERE variable = 'RandomizeHol my $reserve_id = AddReserve ( $library_C, $borrowernumber, $biblionumber, '', 1 ); C4::HoldsQueue::CreateQueue(); $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} }); -is( @$holds_queue, 1, "Bug 14297 - Holds Queue building ignoring holds where pickup & home branch don't match and item is not from least cost branch" ); +is( @$holds_queue, 1, "Bug 14297 - Holds Queue building ignoring holds where pickup & home branch don't match and item is not from le"); # End Bug 14297 + # Cleanup -$dbh->rollback; +$schema->storage->txn_rollback; ### END Test holds queue builder does not violate holds policy ### diff --git a/t/db_dependent/Items.t b/t/db_dependent/Items.t index 32c8ed2761..35755c6aca 100755 --- a/t/db_dependent/Items.t +++ b/t/db_dependent/Items.t @@ -24,6 +24,7 @@ use C4::Branch; use Koha::Database; use t::lib::Mocks; +use t::lib::TestBuilder; use Test::More tests => 9; @@ -34,10 +35,7 @@ BEGIN { use_ok('Koha::Items'); } -my $schema = Koha::Database->new->schema; - -my $branches = GetBranches; -my ($branch1, $branch2) = keys %$branches; +my $schema = Koha::Database->new->schema; my $location = 'My Location'; subtest 'General Add, Get and Del tests' => sub { @@ -46,12 +44,17 @@ subtest 'General Add, Get and Del tests' => sub { $schema->storage->txn_begin; + my $builder = t::lib::TestBuilder->new; + my $library = $builder->build({ + source => 'Branch', + }); + # Create a biblio instance for testing C4::Context->set_preference('marcflavour', 'MARC21'); my ($bibnum, $bibitemnum) = get_biblio(); # Add an item. - my ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch1, holdingbranch => $branch1, location => $location } , $bibnum); + my ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $library->{branchcode}, holdingbranch => $library->{branchcode}, location => $location } , $bibnum); cmp_ok($item_bibnum, '==', $bibnum, "New item is linked to correct biblionumber."); cmp_ok($item_bibitemnum, '==', $bibitemnum, "New item is linked to correct biblioitemnumber."); @@ -72,7 +75,7 @@ subtest 'General Add, Get and Del tests' => sub { my $getdeleted = GetItem($itemnumber); is($getdeleted->{'itemnumber'}, undef, "Item deleted as expected."); - ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch1, holdingbranch => $branch1, location => $location, permanent_location => 'my permanent location' } , $bibnum); + ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $library->{branchcode}, holdingbranch => $library->{branchcode}, location => $location, permanent_location => 'my permanent location' } , $bibnum); $getitem = GetItem($itemnumber); is( $getitem->{location}, $location, "The location should not have been modified" ); is( $getitem->{permanent_location}, 'my permanent location', "The permanent_location should not have modified" ); @@ -98,28 +101,29 @@ subtest 'GetHiddenItemnumbers tests' => sub { $schema->storage->txn_begin; + my $builder = t::lib::TestBuilder->new; + my $library1 = $builder->build({ + source => 'Branch', + }); + + my $library2 = $builder->build({ + source => 'Branch', + }); + # Create a new biblio C4::Context->set_preference('marcflavour', 'MARC21'); my ($biblionumber, $biblioitemnumber) = get_biblio(); - # Add branches if they don't exist - if (not defined GetBranchDetail('CPL')) { - ModBranch({add => 1, branchcode => 'CPL', branchname => 'Centerville'}); - } - if (not defined GetBranchDetail('MPL')) { - ModBranch({add => 1, branchcode => 'MPL', branchname => 'Midway'}); - } - # Add two items my ($item1_bibnum, $item1_bibitemnum, $item1_itemnumber) = AddItem( - { homebranch => $branch1, - holdingbranch => $branch1, + { homebranch => $library1->{branchcode}, + holdingbranch => $library1->{branchcode}, withdrawn => 1 }, $biblionumber ); my ($item2_bibnum, $item2_bibitemnum, $item2_itemnumber) = AddItem( - { homebranch => $branch2, - holdingbranch => $branch2, + { homebranch => $library2->{branchcode}, + holdingbranch => $library2->{branchcode}, withdrawn => 0 }, $biblionumber ); @@ -160,12 +164,12 @@ subtest 'GetHiddenItemnumbers tests' => sub { # Two variables, a value each $opachiddenitems = " withdrawn: [1] - homebranch: [$branch2] + homebranch: [$library2->{branchcode}] "; C4::Context->set_preference( 'OpacHiddenItems', $opachiddenitems ); @hidden = GetHiddenItemnumbers( @items ); ok( scalar @hidden == 2, "Two items hidden"); - is_deeply( \@hidden, \@itemnumbers, "withdrawn=1 and homebranch=MPL hidden"); + is_deeply( \@hidden, \@itemnumbers, "withdrawn=1 and homebranch library2 hidden"); # Valid OpacHiddenItems, empty list @items = (); @@ -181,20 +185,28 @@ subtest 'GetItemsInfo tests' => sub { $schema->storage->txn_begin; + my $builder = t::lib::TestBuilder->new; + my $library1 = $builder->build({ + source => 'Branch', + }); + my $library2 = $builder->build({ + source => 'Branch', + }); + # Add a biblio my ($biblionumber, $biblioitemnumber) = get_biblio(); # Add an item my ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ - homebranch => $branch1, - holdingbranch => $branch2 + homebranch => $library1->{branchcode}, + holdingbranch => $library2->{branchcode}, }, $biblionumber ); - my $branch = GetBranchDetail( $branch1 ); + my $branch = GetBranchDetail( $library1->{branchcode} ); $branch->{ opac_info } = "homebranch OPAC info"; ModBranch($branch); - $branch = GetBranchDetail( $branch2 ); + $branch = GetBranchDetail( $library2->{branchcode} ); $branch->{ opac_info } = "holdingbranch OPAC info"; ModBranch($branch); @@ -259,30 +271,30 @@ subtest 'SearchItems test' => sub { $schema->storage->txn_begin; my $dbh = C4::Context->dbh; + my $builder = t::lib::TestBuilder->new; + + my $library1 = $builder->build({ + source => 'Branch', + }); + my $library2 = $builder->build({ + source => 'Branch', + }); C4::Context->set_preference('marcflavour', 'MARC21'); - my $cpl_items_before = SearchItemsByField( 'homebranch', 'CPL'); + my $cpl_items_before = SearchItemsByField( 'homebranch', $library1->{branchcode}); my ($biblionumber) = get_biblio(); - # Add branches if they don't exist - if (not defined GetBranchDetail('CPL')) { - ModBranch({add => 1, branchcode => 'CPL', branchname => 'Centerville'}); - } - if (not defined GetBranchDetail('MPL')) { - ModBranch({add => 1, branchcode => 'MPL', branchname => 'Midway'}); - } - my (undef, $initial_items_count) = SearchItems(undef, {rows => 1}); # Add two items my (undef, undef, $item1_itemnumber) = AddItem({ - homebranch => 'CPL', - holdingbranch => 'CPL', + homebranch => $library1->{branchcode}, + holdingbranch => $library1->{branchcode}, }, $biblionumber); my (undef, undef, $item2_itemnumber) = AddItem({ - homebranch => 'MPL', - holdingbranch => 'MPL', + homebranch => $library2->{branchcode}, + holdingbranch => $library2->{branchcode}, }, $biblionumber); my ($items, $total_results); @@ -298,14 +310,14 @@ subtest 'SearchItems test' => sub { # Search all items where homebranch = 'CPL' my $filter = { field => 'homebranch', - query => 'CPL', + query => $library1->{branchcode}, operator => '=', }; ($items, $total_results) = SearchItems($filter); ok($total_results > 0, "There is at least one CPL item"); my $all_items_are_CPL = 1; foreach my $item (@$items) { - if ($item->{homebranch} ne 'CPL') { + if ($item->{homebranch} ne $library1->{branchcode}) { $all_items_are_CPL = 0; last; } @@ -315,14 +327,14 @@ subtest 'SearchItems test' => sub { # Search all items where homebranch != 'CPL' $filter = { field => 'homebranch', - query => 'CPL', + query => $library1->{branchcode}, operator => '!=', }; ($items, $total_results) = SearchItems($filter); ok($total_results > 0, "There is at least one non-CPL item"); my $all_items_are_not_CPL = 1; foreach my $item (@$items) { - if ($item->{homebranch} eq 'CPL') { + if ($item->{homebranch} eq $library1->{branchcode}) { $all_items_are_not_CPL = 0; last; } @@ -350,7 +362,7 @@ subtest 'SearchItems test' => sub { }, { field => 'homebranch', - query => 'CPL', + query => $library1->{branchcode}, operator => '=', }, ], @@ -411,7 +423,7 @@ subtest 'SearchItems test' => sub { ($items, $total_results) = SearchItems($filter); ok(scalar @$items == 1, 'found 1 item with itemnotes = "foobar"'); - my $cpl_items_after = SearchItemsByField( 'homebranch', 'CPL'); + my $cpl_items_after = SearchItemsByField( 'homebranch', $library1->{branchcode}); is( ( scalar( @$cpl_items_after ) - scalar ( @$cpl_items_before ) ), 1, 'SearchItemsByField should return something' ); $schema->storage->txn_rollback; @@ -423,10 +435,18 @@ subtest 'Koha::Item(s) tests' => sub { $schema->storage->txn_begin(); + my $builder = t::lib::TestBuilder->new; + my $library1 = $builder->build({ + source => 'Branch', + }); + my $library2 = $builder->build({ + source => 'Branch', + }); + # Create a biblio and item for testing C4::Context->set_preference('marcflavour', 'MARC21'); my ($bibnum, $bibitemnum) = get_biblio(); - my ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch1, holdingbranch => $branch2 } , $bibnum); + my ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $library1->{branchcode}, holdingbranch => $library2->{branchcode} } , $bibnum); # Get item. my $item = Koha::Items->find( $itemnumber ); @@ -434,11 +454,13 @@ subtest 'Koha::Item(s) tests' => sub { my $homebranch = $item->home_branch(); is( ref($homebranch), 'Koha::Branch', "Got Koha::Branch from home_branch method" ); - is( $homebranch->branchcode(), $branch1, "Home branch code matches homebranch" ); + is( $homebranch->branchcode(), $library1->{branchcode}, "Home branch code matches homebranch" ); my $holdingbranch = $item->holding_branch(); is( ref($holdingbranch), 'Koha::Branch', "Got Koha::Branch from holding_branch method" ); - is( $holdingbranch->branchcode(), $branch2, "Home branch code matches holdingbranch" ); + is( $holdingbranch->branchcode(), $library2->{branchcode}, "Home branch code matches holdingbranch" ); + + $schema->storage->txn_rollback; }; subtest 'C4::Biblio::EmbedItemsInMarcBiblio' => sub { @@ -446,20 +468,28 @@ subtest 'C4::Biblio::EmbedItemsInMarcBiblio' => sub { $schema->storage->txn_begin(); + my $builder = t::lib::TestBuilder->new; + my $library1 = $builder->build({ + source => 'Branch', + }); + my $library2 = $builder->build({ + source => 'Branch', + }); + my ( $biblionumber, $biblioitemnumber ) = get_biblio(); my $item_infos = [ - { homebranch => 'CPL', holdingbranch => 'CPL' }, - { homebranch => 'CPL', holdingbranch => 'CPL' }, - { homebranch => 'CPL', holdingbranch => 'CPL' }, - { homebranch => 'MPL', holdingbranch => 'MPL' }, - { homebranch => 'MPL', holdingbranch => 'MPL' }, - { homebranch => 'CPL', holdingbranch => 'MPL' }, - { homebranch => 'CPL', holdingbranch => 'MPL' }, - { homebranch => 'CPL', holdingbranch => 'MPL' }, + { homebranch => $library1->{branchcode}, holdingbranch => $library1->{branchcode} }, + { homebranch => $library1->{branchcode}, holdingbranch => $library1->{branchcode} }, + { homebranch => $library1->{branchcode}, holdingbranch => $library1->{branchcode} }, + { homebranch => $library2->{branchcode}, holdingbranch => $library2->{branchcode} }, + { homebranch => $library2->{branchcode}, holdingbranch => $library2->{branchcode} }, + { homebranch => $library1->{branchcode}, holdingbranch => $library2->{branchcode} }, + { homebranch => $library1->{branchcode}, holdingbranch => $library2->{branchcode} }, + { homebranch => $library1->{branchcode}, holdingbranch => $library2->{branchcode} }, ]; my $number_of_items = scalar @$item_infos; my $number_of_items_with_homebranch_is_CPL = - grep { $_->{homebranch} eq 'CPL' } @$item_infos; + grep { $_->{homebranch} eq $library1->{branchcode} } @$item_infos; my @itemnumbers; for my $item_info (@$item_infos) { @@ -497,7 +527,7 @@ subtest 'C4::Biblio::EmbedItemsInMarcBiblio' => sub { is( scalar @items, $number_of_items, 'Should return all items for opac' ); my $opachiddenitems = " - homebranch: ['CPL']"; + homebranch: ['$library1->{branchcode}']"; t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems ); C4::Biblio::EmbedItemsInMarcBiblio( $record, $biblionumber ); @@ -515,7 +545,7 @@ subtest 'C4::Biblio::EmbedItemsInMarcBiblio' => sub { ); $opachiddenitems = " - homebranch: ['CPL', 'MPL']"; + homebranch: ['$library1->{branchcode}', '$library2->{branchcode}']"; t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems ); C4::Biblio::EmbedItemsInMarcBiblio( $record, $biblionumber, undef, 1 ); @items = $record->field($itemfield); @@ -524,6 +554,8 @@ subtest 'C4::Biblio::EmbedItemsInMarcBiblio' => sub { 0, 'For OPAC, If all items are hidden, no item should have been embeded' ); + + $schema->storage->txn_rollback; }; # Helper method to set up a Biblio. diff --git a/t/db_dependent/Items_DelItem.t b/t/db_dependent/Items_DelItem.t index 35ff6e4383..2880f11b03 100644 --- a/t/db_dependent/Items_DelItem.t +++ b/t/db_dependent/Items_DelItem.t @@ -3,21 +3,27 @@ use Modern::Perl; use MARC::Record; use C4::Biblio; +use t::lib::TestBuilder; + use Test::More tests => 7; BEGIN { use_ok('C4::Items'); } -my $dbh = C4::Context->dbh; -$dbh->{AutoCommit} = 0; -$dbh->{RaiseError} = 1; +my $schema = Koha::Database->schema; +$schema->storage->txn_begin; +my $builder = t::lib::TestBuilder->new; + +my $library = $builder->build({ + source => 'Branch', +}); my ( $biblionumber, $bibitemnum ) = get_biblio(); my ( $item_bibnum, $item_bibitemnum, $itemnumber ); ( $item_bibnum, $item_bibitemnum, $itemnumber ) = - AddItem( { homebranch => 'CPL', holdingbranch => 'CPL' }, $biblionumber ); + AddItem( { homebranch => $library->{branchcode}, holdingbranch => $library->{branchcode} }, $biblionumber ); my $deleted = DelItem( { biblionumber => $biblionumber, itemnumber => $itemnumber } ); is( $deleted, 1, "DelItem should return 1 if the item has been deleted" ); @@ -25,7 +31,7 @@ my $deleted_item = GetItem($itemnumber); is( $deleted_item->{itemnumber}, undef, "DelItem with biblionumber parameter - the item should be deleted." ); ( $item_bibnum, $item_bibitemnum, $itemnumber ) = - AddItem( { homebranch => 'CPL', holdingbranch => 'CPL' }, $biblionumber ); + AddItem( { homebranch => $library->{branchcode}, holdingbranch => $library->{branchcode} }, $biblionumber ); $deleted = DelItem( { biblionumber => $biblionumber, itemnumber => $itemnumber } ); is( $deleted, 1, "DelItem should return 1 if the item has been deleted" ); $deleted_item = GetItem($itemnumber); diff --git a/t/db_dependent/Koha_template_plugin_Branches.t b/t/db_dependent/Koha_template_plugin_Branches.t index 414a3114f6..af055ada1c 100644 --- a/t/db_dependent/Koha_template_plugin_Branches.t +++ b/t/db_dependent/Koha_template_plugin_Branches.t @@ -21,15 +21,26 @@ use Modern::Perl; use Test::More tests => 7; use C4::Context; +use Koha::Database; + +use t::lib::TestBuilder; + BEGIN { use_ok('Koha::Template::Plugin::Branches'); } +my $schema = Koha::Database->schema; +$schema->storage->txn_begin; +my $builder = t::lib::TestBuilder->new; +my $library = $builder->build({ + source => 'Branch', +}); + my $plugin = Koha::Template::Plugin::Branches->new(); ok($plugin, "initialized Branches plugin"); -my $name = $plugin->GetName('CPL'); -is($name, 'Centerville', 'retrieved expected name for CPL'); +my $name = $plugin->GetName($library->{branchcode}); +is($name, $library->{branchname}, 'retrieved expected name for library'); $name = $plugin->GetName('__ANY__'); is($name, '', 'received empty string as name of the "__ANY__" placeholder library code'); @@ -37,7 +48,7 @@ is($name, '', 'received empty string as name of the "__ANY__" placeholder librar $name = $plugin->GetName(undef); is($name, '', 'received empty string as name of NULL/undefined library code'); -my $library = $plugin->GetLoggedInBranchcode(); +$library = $plugin->GetLoggedInBranchcode(); is($library, '', 'no active library if there is no active user session'); C4::Context->_new_userenv('DUMMY_SESSION_ID'); diff --git a/t/db_dependent/Letters.t b/t/db_dependent/Letters.t index abeeb8b16e..2e0d85455f 100644 --- a/t/db_dependent/Letters.t +++ b/t/db_dependent/Letters.t @@ -42,30 +42,31 @@ use_ok('C4::Biblio'); use_ok('C4::Bookseller'); use_ok('C4::Letters'); use t::lib::Mocks; +use t::lib::TestBuilder; +use Koha::Database; use Koha::DateUtils qw( dt_from_string output_pref ); use Koha::Acquisition::Order; use Koha::Acquisition::Bookseller; -use Koha::Database; - -my $dbh = C4::Context->dbh; - -my $database = Koha::Database->new(); -my $schema = $database->schema(); +my $schema = Koha::Database->schema; $schema->storage->txn_begin(); -# Start transaction +my $builder = t::lib::TestBuilder->new; +my $dbh = C4::Context->dbh; $dbh->{RaiseError} = 1; $dbh->do(q|DELETE FROM letter|); $dbh->do(q|DELETE FROM message_queue|); $dbh->do(q|DELETE FROM message_transport_types|); +my $library = $builder->build({ + source => 'Branch', +}); my $date = dt_from_string; my $borrowernumber = AddMember( firstname => 'Jane', surname => 'Smith', categorycode => 'PT', - branchcode => 'CPL', + branchcode => $library->{branchcode}, dateofbirth => $date, ); @@ -157,18 +158,18 @@ Don't forget your date of birth: <>. Look at this wonderful biblio timestamp: <>. }; -$dbh->do( q|INSERT INTO letter(branchcode,module,code,name,is_html,title,content,message_transport_type) VALUES ('CPL','my module','my code','my name',1,?,?,'email')|, undef, $title, $content ); +$dbh->do( q|INSERT INTO letter(branchcode,module,code,name,is_html,title,content,message_transport_type) VALUES (?,'my module','my code','my name',1,?,?,'email')|, undef, $library->{branchcode}, $title, $content ); $letters = C4::Letters::GetLetters(); is( @$letters, 1, 'GetLetters returns the correct number of letters' ); -is( $letters->[0]->{branchcode}, 'CPL', 'GetLetters gets the branch code correctly' ); +is( $letters->[0]->{branchcode}, $library->{branchcode}, 'GetLetters gets the branch code correctly' ); is( $letters->[0]->{module}, 'my module', 'GetLetters gets the module correctly' ); is( $letters->[0]->{code}, 'my code', 'GetLetters gets the code correctly' ); is( $letters->[0]->{name}, 'my name', 'GetLetters gets the name correctly' ); # getletter -my $letter = C4::Letters::getletter('my module', 'my code', 'CPL', 'email'); -is( $letter->{branchcode}, 'CPL', 'GetLetters gets the branch code correctly' ); +my $letter = C4::Letters::getletter('my module', 'my code', $library->{branchcode}, 'email'); +is( $letter->{branchcode}, $library->{branchcode}, 'GetLetters gets the branch code correctly' ); is( $letter->{module}, 'my module', 'GetLetters gets the module correctly' ); is( $letter->{code}, 'my code', 'GetLetters gets the code correctly' ); is( $letter->{name}, 'my name', 'GetLetters gets the name correctly' ); @@ -234,11 +235,11 @@ is( @$alerts, 0, 'delalert removes an alert' ); t::lib::Mocks::mock_preference('OPACBaseURL', 'http://thisisatest.com'); my $sms_content = 'This is a SMS for an <>'; -$dbh->do( q|INSERT INTO letter(branchcode,module,code,name,is_html,title,content,message_transport_type) VALUES ('CPL','my module','my code','my name',1,'my title',?,'sms')|, undef, $sms_content ); +$dbh->do( q|INSERT INTO letter(branchcode,module,code,name,is_html,title,content,message_transport_type) VALUES (?,'my module','my code','my name',1,'my title',?,'sms')|, undef, $library->{branchcode}, $sms_content ); my $tables = { borrowers => $borrowernumber, - branches => 'CPL', + branches => $library->{branchcode}, biblio => $biblionumber, }; my $substitute = { @@ -256,13 +257,13 @@ my $repeat = [ ]; my $prepared_letter = GetPreparedLetter(( module => 'my module', - branchcode => 'CPL', + branchcode => $library->{branchcode}, letter_code => 'my code', tables => $tables, substitute => $substitute, repeat => $repeat, )); -my $branch = GetBranchDetail('CPL'); +my $branch = GetBranchDetail($library->{branchcode}); my $my_title_letter = qq|$branch->{branchname} - $substitute->{status}|; my $my_content_letter = qq|Dear Jane Smith, According to our current records, you have items that are overdue.Your library does not charge late fines, but please return or renew them at the branch below as soon as possible. @@ -285,7 +286,7 @@ is( $prepared_letter->{content}, $my_content_letter, 'GetPreparedLetter returns $prepared_letter = GetPreparedLetter(( module => 'my module', - branchcode => 'CPL', + branchcode => $library->{branchcode}, letter_code => 'my code', tables => $tables, substitute => $substitute, @@ -418,7 +419,7 @@ my $borrowernumber = AddMember( firstname => 'John', surname => 'Smith', categorycode => 'PT', - branchcode => 'CPL', + branchcode => $library->{branchcode}, dateofbirth => $date, email => 'john.smith@test.de', ); @@ -434,6 +435,3 @@ is($err2, "", "Successfully sent serial notification"); is($mail{'To'}, 'john.smith@test.de', "mailto correct in sent serial notification"); is($mail{'Message'}, 'Silence in the library,'.$subscriptionid.',No. 0', 'Serial notification text constructed successfully'); } - - -$schema->storage->txn_rollback(); diff --git a/t/db_dependent/Members.t b/t/db_dependent/Members.t index 1858b7f2c3..4c87e84cb9 100755 --- a/t/db_dependent/Members.t +++ b/t/db_dependent/Members.t @@ -21,22 +21,31 @@ use Test::More tests => 74; use Test::MockModule; use Data::Dumper; use C4::Context; +use Koha::Database; + +use t::lib::TestBuilder; BEGIN { use_ok('C4::Members'); } +my $schema = Koha::Database->schema; +$schema->storage->txn_begin; +my $builder = t::lib::TestBuilder->new; my $dbh = C4::Context->dbh; - -# Start transaction -$dbh->{AutoCommit} = 0; $dbh->{RaiseError} = 1; +my $library1 = $builder->build({ + source => 'Branch', +}); +my $library2 = $builder->build({ + source => 'Branch', +}); my $CARDNUMBER = 'TESTCARD01'; my $FIRSTNAME = 'Marie'; my $SURNAME = 'Mcknight'; my $CATEGORYCODE = 'S'; -my $BRANCHCODE = 'CPL'; +my $BRANCHCODE = $library1->{branchcode}; my $CHANGED_FIRSTNAME = "Marry Ann"; my $EMAIL = "Marie\@email.com"; @@ -46,8 +55,6 @@ my $PHONE = "555-12123"; # XXX should be randomised and checked against the database my $IMPOSSIBLE_CARDNUMBER = "XYZZZ999"; -# XXX make a non-commit transaction and rollback rather than insert/delete - #my ($usernum, $userid, $usercnum, $userfirstname, $usersurname, $userbranch, $branchname, $userflags, $emailaddress, $branchprinter)= @_; my @USERENV = ( 1, @@ -212,7 +219,7 @@ is( $borrower, undef, 'DelMember should remove the patron' ); firstname => "Tomasito", surname => "None", categorycode => "S", - branchcode => "MPL", + branchcode => $library2->{branchcode}, dateofbirth => '', debarred => '', dateexpiry => '', @@ -269,10 +276,10 @@ is( $borrower->{userid}, $data{userid}, 'AddMember should insert the given useri ## Remove all entries with userid='' (should be only 1 max) $dbh->do(q|DELETE FROM borrowers WHERE userid = ''|); ## And create a patron with a userid='' -$borrowernumber = AddMember( categorycode => 'S', branchcode => 'MPL' ); +$borrowernumber = AddMember( categorycode => 'S', branchcode => $library2->{branchcode} ); $dbh->do(q|UPDATE borrowers SET userid = '' WHERE borrowernumber = ?|, undef, $borrowernumber); # Create another patron and verify the userid has been generated -$borrowernumber = AddMember( categorycode => 'S', branchcode => 'MPL' ); +$borrowernumber = AddMember( categorycode => 'S', branchcode => $library2->{branchcode} ); ok( $borrowernumber > 0, 'AddMember should have inserted the patron even if no userid is given' ); $borrower = GetMember( borrowernumber => $borrowernumber ); ok( $borrower->{userid}, 'A userid should have been generated correctly' ); @@ -329,8 +336,6 @@ subtest 'GetMemberAccountBalance' => sub { is( $total, 15 , "Total calculated correctly"); is( $total_minus_charges, 10, "Holds charges are count if HoldsInNoissuesCharge=0"); is( $other_charges, 5, "Holds charges are considered if HoldsInNoissuesCharge=1"); - - $dbh->rollback(); }; subtest 'purgeSelfRegistration' => sub { @@ -350,9 +355,8 @@ subtest 'purgeSelfRegistration' => sub { C4::Context->set_preference('PatronSelfRegistrationDefaultCategory', $c ); C4::Context->set_preference('PatronSelfRegistrationExpireTemporaryAccountsDelay', 360); C4::Members::DeleteExpiredOpacRegistrations(); - $dbh->do("INSERT INTO borrowers (surname, address, city, branchcode, categorycode, dateenrolled) VALUES ('Testaabbcc', 'Street 1', 'CITY', 'CPL', '$c', '2014-01-01 01:02:03')"); + $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'); - $dbh->rollback(); }; sub _find_member { diff --git a/t/db_dependent/Members/AddEnrolmentFeeIfNeeded.t b/t/db_dependent/Members/AddEnrolmentFeeIfNeeded.t index 7405dc96b8..69c9e7ffd6 100644 --- a/t/db_dependent/Members/AddEnrolmentFeeIfNeeded.t +++ b/t/db_dependent/Members/AddEnrolmentFeeIfNeeded.t @@ -3,11 +3,20 @@ use Test::More tests => 4; use C4::Context; use C4::Members; +use Koha::Database; +use t::lib::TestBuilder; + +my $schema = Koha::Database->schema; +$schema->storage->txn_begin; +my $builder = t::lib::TestBuilder->new; my $dbh = C4::Context->dbh; -$dbh->{AutoCommit} = 0; $dbh->{RaiseError} = 1; +my $library = $builder->build({ + source => 'Branch', +}); + my $enrolmentfee_K = 5; my $enrolmentfee_J = 10; my $enrolmentfee_YA = 20; @@ -34,7 +43,7 @@ my %borrower_data = ( firstname => 'my firstname', surname => 'my surname', categorycode => 'K', - branchcode => 'CPL', + branchcode => $library->{branchcode}, ); my $borrowernumber = C4::Members::AddMember( %borrower_data ); @@ -62,5 +71,3 @@ is( $total, $enrolmentfee_K + $enrolmentfee_J, "Kid growing and become a juvenil C4::Members::AddEnrolmentFeeIfNeeded( 'YA', $borrowernumber ); ( $total ) = C4::Members::GetMemberAccountRecords( $borrowernumber ); is( $total, $enrolmentfee_K + $enrolmentfee_J + $enrolmentfee_YA, "Juvenile growing and become an young adult, he should pay " . ( $enrolmentfee_K + $enrolmentfee_J + $enrolmentfee_YA ) ); - -$dbh->rollback; diff --git a/t/db_dependent/Members_Attributes.t b/t/db_dependent/Members_Attributes.t index 2039db07a4..71e839450b 100755 --- a/t/db_dependent/Members_Attributes.t +++ b/t/db_dependent/Members_Attributes.t @@ -22,13 +22,18 @@ use Modern::Perl; use C4::Context; use C4::Members; use C4::Members::AttributeTypes; +use Koha::Database; use Test::More tests => 60; +use t::lib::TestBuilder; + use_ok('C4::Members::Attributes'); +my $schema = Koha::Database->schema; +$schema->storage->txn_begin; +my $builder = t::lib::TestBuilder->new; my $dbh = C4::Context->dbh; -$dbh->{AutoCommit} = 0; $dbh->{RaiseError} = 1; $dbh->do(q|DELETE FROM issues|); @@ -36,11 +41,14 @@ $dbh->do(q|DELETE FROM borrowers|); $dbh->do(q|DELETE FROM borrower_attributes|); $dbh->do(q|DELETE FROM borrower_attribute_types|); +my $library = $builder->build({ + source => 'Branch', +}); my $borrowernumber = AddMember( firstname => 'my firstname', surname => 'my surname', categorycode => 'S', - branchcode => 'CPL', + branchcode => $library->{branchcode}, ); @@ -196,5 +204,3 @@ is( $borrower_attributes->[0]->{password}, $attributes->[1]->{password}, 'Delete C4::Members::Attributes::DeleteBorrowerAttribute($borrowernumber, $attributes->[1]); $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber); is( @$borrower_attributes, 0, 'DeleteBorrowerAttribute deletes a borrower attribute' ); - -$dbh->rollback; diff --git a/t/db_dependent/Ratings.t b/t/db_dependent/Ratings.t index 1538ca44d5..1f5db0e952 100755 --- a/t/db_dependent/Ratings.t +++ b/t/db_dependent/Ratings.t @@ -23,18 +23,27 @@ use C4::Biblio qw/AddBiblio/; use C4::Members; use C4::Context; use C4::Category; +use Koha::Database; + +use t::lib::TestBuilder; use_ok('C4::Ratings'); +my $schema = Koha::Database->schema; +$schema->storage->txn_begin; +my $builder = t::lib::TestBuilder->new; my $dbh = C4::Context->dbh; $dbh->{RaiseError} = 1; -$dbh->{AutoCommit} = 0; + +my $library = $builder->build({ + source => 'Branch', +}); my ($biblionumber) = AddBiblio( MARC::Record->new, '' ); my @categories = C4::Category->all; my $categorycode = $categories[0]->categorycode; -my $branchcode = 'CPL'; +my $branchcode = $library->{branchcode}; my %john_doe = ( cardnumber => '123456', @@ -109,4 +118,4 @@ ok( defined $rating7, 'delete another rating' ); is( GetRating( $biblionumber, $borrowernumber1 ), undef, 'GetRating should return undef if no rating exist' ); -1; \ No newline at end of file +1; diff --git a/t/db_dependent/Reserves/GetReserveFee.t b/t/db_dependent/Reserves/GetReserveFee.t index 35b5bd15a7..2a7a50cf83 100755 --- a/t/db_dependent/Reserves/GetReserveFee.t +++ b/t/db_dependent/Reserves/GetReserveFee.t @@ -32,12 +32,15 @@ use Koha::Database; my $schema = Koha::Database->new->schema; $schema->storage->txn_begin; +my $builder = t::lib::TestBuilder->new(); +my $library = $builder->build({ + source => 'Branch', +}); my $mContext = new Test::MockModule('C4::Context'); $mContext->mock( 'userenv', sub { - return { branch => 'CPL' }; + return { branch => $library->{branchcode} }; }); -my $builder = t::lib::TestBuilder->new(); my $dbh = C4::Context->dbh; # after start transaction of testbuilder # Category with hold fee, two patrons @@ -116,7 +119,7 @@ sub acctlines { #calculate number of accountlines for a patron sub addreserve { return AddReserve( - 'CPL', + $library->{branchcode}, $_[0], $biblio->{biblionumber}, undef, diff --git a/t/db_dependent/Serials_2.t b/t/db_dependent/Serials_2.t index 1f192ed073..a73ef1fdf5 100644 --- a/t/db_dependent/Serials_2.t +++ b/t/db_dependent/Serials_2.t @@ -7,7 +7,9 @@ use MARC::Record; use C4::Biblio qw( AddBiblio ); use C4::Members qw( AddMember ); +use Koha::Database; use t::lib::Mocks; +use t::lib::TestBuilder; use_ok('C4::Serials'); use_ok('C4::Budgets'); @@ -16,18 +18,26 @@ local $SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /redefined/ }; my $userenv; *C4::Context::userenv = \&Mock_userenv; +my $schema = Koha::Database->schema; +$schema->storage->txn_begin; +my $builder = t::lib::TestBuilder->new; +my $library1 = $builder->build({ + source => 'Branch', +}); +my $library2 = $builder->build({ + source => 'Branch', +}); my $dbh = C4::Context->dbh; -$dbh->{AutoCommit} = 0; $dbh->{RaiseError} = 1; my $record = MARC::Record->new(); $record->append_fields( - MARC::Field->new( '952', '0', '0', a => 'CPL', b => 'CPL' ) + MARC::Field->new( '952', '0', '0', a => $library1->{branchcode}, b => $library1->{branchcode} ) ); my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio($record, ''); -my $my_branch = 'CPL'; -my $another_branch = 'MPL'; +my $my_branch = $library1->{branchcode}; +my $another_branch = $library2->{branchcode}; my $budgetid; my $bpid = AddBudgetPeriod({ budget_period_startdate => '2015-01-01', @@ -234,7 +244,7 @@ is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1, "Without IndependentBranches, renew_subscription cannot show a subscription from another branch" ); -$dbh->rollback; +$schema->storage->txn_rollback; # C4::Context->userenv sub Mock_userenv { diff --git a/t/db_dependent/ShelfBrowser.t b/t/db_dependent/ShelfBrowser.t index 0730f4a31a..10bf219de7 100644 --- a/t/db_dependent/ShelfBrowser.t +++ b/t/db_dependent/ShelfBrowser.t @@ -9,17 +9,26 @@ use MARC::Record; use C4::Biblio; use C4::Context; use C4::Items; +use Koha::Database; + +use t::lib::TestBuilder; use_ok('C4::ShelfBrowser'); +my $schema = Koha::Database->schema; +$schema->storage->txn_begin; +my $builder = t::lib::TestBuilder->new; my $dbh = C4::Context->dbh; -$dbh->{AutoCommit} = 0; $dbh->{RaiseError} = 1; $dbh->do(q|DELETE FROM reserves|); $dbh->do(q|DELETE FROM issues|); $dbh->do(q|DELETE FROM items|); +my $library = $builder->build({ + source => 'Branch', +}); + my $cn; # 100.100 150.100 200.100 210.100 300.000 320.000 400.100 410.100 500.100 510.100 520.100 600.000 610.000 700.100 710.100 720.100 730.100 740.100 750.100 @@ -54,8 +63,8 @@ my ( $biblionumber, undef, undef ) = C4::Biblio::AddBiblio($record, ''); for my $callnumber ( shuffle @callnumbers ) { my ( $biblionumber, undef, $itemnumber ) = C4::Items::AddItem({ - homebranch => 'CPL', - holdingbranch => 'CPL', + homebranch => $library->{branchcode}, + holdingbranch => $library->{branchcode}, itemcallnumber => $callnumber, }, $biblionumber); $cn->{$callnumber} = { @@ -208,5 +217,3 @@ is( $nearby->{prev_item}{itemnumber}, $cn->{'600.000'}{itemnumber}, "Test last i is( $nearby->{next_item}, undef, "Test end of the shelf: no next link" ); is( scalar( @{$nearby->{items}} ), 4, "Test last item of the shelf: got 4 items" ); - -$dbh->rollback; -- 2.39.5