From 4c134df5297465ba535e2552c60ac28ae3b0f694 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Tue, 13 Nov 2018 11:20:45 -0300 Subject: [PATCH] Bug 21798: Unify the creation of bibliographic record in tests Using the newly created subroutine Signed-off-by: Marcel de Rooy Signed-off-by: Nick Clemens (cherry picked from commit 9ba6125f72fc4dc44421550561a815cb272b98b7) Signed-off-by: Martin Renvoize --- t/db_dependent/Circulation.t | 138 ++++++++------------ t/db_dependent/Holds.t | 113 +++++++--------- t/db_dependent/Holds/LocalHoldsPriority.t | 23 +--- t/db_dependent/Holds/RevertWaitingStatus.t | 24 +--- t/db_dependent/Items.t | 99 ++++++-------- t/db_dependent/Items/DelItem.t | 23 +--- t/db_dependent/Items/GetItemsForInventory.t | 20 +-- t/db_dependent/Koha/BiblioUtils/Iterator.t | 19 +-- t/db_dependent/Koha/Biblios.t | 19 +-- t/db_dependent/OAI/Sets.t | 47 ++----- t/db_dependent/Serials.t | 13 +- t/lib/TestBuilder.pm | 27 +++- 12 files changed, 201 insertions(+), 364 deletions(-) diff --git a/t/db_dependent/Circulation.t b/t/db_dependent/Circulation.t index aaf8840ba1..613cd74e92 100755 --- a/t/db_dependent/Circulation.t +++ b/t/db_dependent/Circulation.t @@ -229,8 +229,7 @@ C4::Context->dbh->do("DELETE FROM accountlines"); # CanBookBeRenewed tests C4::Context->set_preference('ItemsDeniedRenewal',''); # Generate test biblio - my $title = 'Silence in the library'; - my ($biblionumber, $biblioitemnumber) = add_biblio($title, 'Moffat, Steven'); + my $biblio = $builder->gimme_a_biblio(); my $barcode = 'R00000342'; my $branch = $library2->{branchcode}; @@ -243,7 +242,7 @@ C4::Context->dbh->do("DELETE FROM accountlines"); replacementprice => 12.00, itype => $itemtype }, - $biblionumber + $biblio->biblionumber ); my $barcode2 = 'R00000343'; @@ -255,7 +254,7 @@ C4::Context->dbh->do("DELETE FROM accountlines"); replacementprice => 23.00, itype => $itemtype }, - $biblionumber + $biblio->biblionumber ); my $barcode3 = 'R00000346'; @@ -267,7 +266,7 @@ C4::Context->dbh->do("DELETE FROM accountlines"); replacementprice => 23.00, itype => $itemtype }, - $biblionumber + $biblio->biblionumber ); # Create borrowers @@ -344,9 +343,9 @@ C4::Context->dbh->do("DELETE FROM accountlines"); # Biblio-level hold, renewal test AddReserve( - $branch, $reserving_borrowernumber, $biblionumber, + $branch, $reserving_borrowernumber, $biblio->biblionumber, $bibitems, $priority, $resdate, $expdate, $notes, - $title, $checkitem, $found + 'a title', $checkitem, $found ); # Testing of feature to allow the renewal of reserved items if other items on the record can fill all needed holds @@ -361,7 +360,7 @@ C4::Context->dbh->do("DELETE FROM accountlines"); my $hold = Koha::Database->new()->schema()->resultset('Reserve')->create( { borrowernumber => $hold_waiting_borrowernumber, - biblionumber => $biblionumber, + biblionumber => $biblio->biblionumber, itemnumber => $itemnumber, branchcode => $branch, priority => 3, @@ -376,7 +375,7 @@ C4::Context->dbh->do("DELETE FROM accountlines"); $hold = Koha::Database->new()->schema()->resultset('Reserve')->create( { borrowernumber => $hold_waiting_borrowernumber, - biblionumber => $biblionumber, + biblionumber => $biblio->biblionumber, itemnumber => $itemnumber3, branchcode => $branch, priority => 0, @@ -397,7 +396,7 @@ C4::Context->dbh->do("DELETE FROM accountlines"); is( $renewokay, 0, '(Bug 10663) Cannot renew, reserved'); is( $error, 'on_reserve', '(Bug 10663) Cannot renew, reserved (returned error is on_reserve)'); - my $reserveid = Koha::Holds->search({ biblionumber => $biblionumber, borrowernumber => $reserving_borrowernumber })->next->reserve_id; + my $reserveid = Koha::Holds->search({ biblionumber => $biblio->biblionumber, borrowernumber => $reserving_borrowernumber })->next->reserve_id; my $reserving_borrower = Koha::Patrons->find( $reserving_borrowernumber )->unblessed; AddIssue($reserving_borrower, $barcode3); my $reserve = $dbh->selectrow_hashref( @@ -409,9 +408,9 @@ C4::Context->dbh->do("DELETE FROM accountlines"); # Item-level hold, renewal test AddReserve( - $branch, $reserving_borrowernumber, $biblionumber, + $branch, $reserving_borrowernumber, $biblio->biblionumber, $bibitems, $priority, $resdate, $expdate, $notes, - $title, $itemnumber, $found + 'a title', $itemnumber, $found ); ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber, 1); @@ -422,10 +421,10 @@ C4::Context->dbh->do("DELETE FROM accountlines"); is( $renewokay, 1, 'Can renew item 2, item-level hold is on item 1'); # Items can't fill hold for reasons - ModItem({ notforloan => 1 }, $biblionumber, $itemnumber); + ModItem({ notforloan => 1 }, $biblio->biblionumber, $itemnumber); ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber, 1); is( $renewokay, 1, 'Can renew, item is marked not for loan, hold does not block'); - ModItem({ notforloan => 0, itype => $itemtype }, $biblionumber, $itemnumber); + ModItem({ notforloan => 0, itype => $itemtype }, $biblio->biblionumber, $itemnumber); # FIXME: Add more for itemtype not for loan etc. @@ -439,7 +438,7 @@ C4::Context->dbh->do("DELETE FROM accountlines"); replacementprice => 23.00, itype => $itemtype }, - $biblionumber + $biblio->biblionumber ); my $datedue5 = AddIssue($restricted_borrower, $barcode5); is (defined $datedue5, 1, "Item with date due checked out, due date: $datedue5"); @@ -460,7 +459,7 @@ C4::Context->dbh->do("DELETE FROM accountlines"); replacementprice => 23.00, itype => $itemtype }, - $biblionumber + $biblio->biblionumber ); my $barcode7 = 'R00000349'; @@ -472,7 +471,7 @@ C4::Context->dbh->do("DELETE FROM accountlines"); replacementprice => 23.00, itype => $itemtype }, - $biblionumber + $biblio->biblionumber ); my $datedue6 = AddIssue( $renewing_borrower, $barcode6); is (defined $datedue6, 1, "Item 2 checked out, due date: ".$datedue6->date_due); @@ -536,7 +535,7 @@ C4::Context->dbh->do("DELETE FROM accountlines"); is( $renewokay, 0, '(Bug 8236), Cannot renew, this item is overdue'); - $hold = Koha::Holds->search({ biblionumber => $biblionumber, borrowernumber => $reserving_borrowernumber })->next; + $hold = Koha::Holds->search({ biblionumber => $biblio->biblionumber, borrowernumber => $reserving_borrowernumber })->next; $hold->cancel; # Bug 14101 @@ -551,7 +550,7 @@ C4::Context->dbh->do("DELETE FROM accountlines"); replacementprice => 16.00, itype => $itemtype }, - $biblionumber + $biblio->biblionumber ); $issue = AddIssue( $renewing_borrower, $barcode4, undef, undef, undef, undef, { auto_renew => 1 } ); @@ -621,7 +620,7 @@ C4::Context->dbh->do("DELETE FROM accountlines"); my $item_to_auto_renew = $builder->build( { source => 'Item', value => { - biblionumber => $biblionumber, + biblionumber => $biblio->biblionumber, homebranch => $branch, holdingbranch => $branch, } @@ -680,7 +679,7 @@ C4::Context->dbh->do("DELETE FROM accountlines"); my $item_to_auto_renew = $builder->build({ source => 'Item', value => { - biblionumber => $biblionumber, + biblionumber => $biblio->biblionumber, homebranch => $branch, holdingbranch => $branch, } @@ -720,7 +719,7 @@ C4::Context->dbh->do("DELETE FROM accountlines"); my $item_to_auto_renew = $builder->build({ source => 'Item', value => { - biblionumber => $biblionumber, + biblionumber => $biblio->biblionumber, homebranch => $branch, holdingbranch => $branch, } @@ -772,7 +771,7 @@ C4::Context->dbh->do("DELETE FROM accountlines"); my $item_to_auto_renew = $builder->build( { source => 'Item', value => { - biblionumber => $biblionumber, + biblionumber => $biblio->biblionumber, homebranch => $branch, holdingbranch => $branch, } @@ -924,8 +923,7 @@ C4::Context->dbh->do("DELETE FROM accountlines"); my $branch = $library2->{branchcode}; #Create another record - my $title2 = 'Something is worng here'; - my ($biblionumber2, $biblioitemnumber2) = add_biblio($title2, 'Anonymous'); + my $biblio2 = $builder->gimme_a_biblio(); #Create third item AddItem( @@ -935,7 +933,7 @@ C4::Context->dbh->do("DELETE FROM accountlines"); barcode => $barcode3, itype => $itemtype }, - $biblionumber2 + $biblio2->biblionumber ); # Create a borrower @@ -1004,7 +1002,7 @@ C4::Context->dbh->do("DELETE FROM accountlines"); my $barcode = '1234567890'; my $branch = $library2->{branchcode}; - my ($biblionumber, $biblioitemnumber) = add_biblio(); + my $biblio = $builder->gimme_a_biblio(); #Create third item my ( undef, undef, $itemnumber ) = AddItem( @@ -1014,7 +1012,7 @@ C4::Context->dbh->do("DELETE FROM accountlines"); barcode => $barcode, itype => $itemtype }, - $biblionumber + $biblio->biblionumber ); # Create a borrower @@ -1061,7 +1059,7 @@ C4::Context->dbh->do("DELETE FROM accountlines"); undef, 0, .10, 1 ); - my ( $biblionumber, $biblioitemnumber ) = add_biblio(); + my $biblio = $builder->gimme_a_biblio(); my $barcode1 = '1234'; my ( undef, undef, $itemnumber1 ) = AddItem( @@ -1071,7 +1069,7 @@ C4::Context->dbh->do("DELETE FROM accountlines"); barcode => $barcode1, itype => $itemtype }, - $biblionumber + $biblio->biblionumber ); my $barcode2 = '4321'; my ( undef, undef, $itemnumber2 ) = AddItem( @@ -1081,7 +1079,7 @@ C4::Context->dbh->do("DELETE FROM accountlines"); barcode => $barcode2, itype => $itemtype }, - $biblionumber + $biblio->biblionumber ); my $borrowernumber1 = Koha::Patron->new({ @@ -1106,7 +1104,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( - $library2->{branchcode}, $borrowernumber2, $biblionumber, + $library2->{branchcode}, $borrowernumber2, $biblio->biblionumber, '', 1, undef, undef, '', undef, undef, undef ); @@ -1132,7 +1130,7 @@ C4::Context->dbh->do("DELETE FROM accountlines"); is( $renewokay, 1, 'Bug 14337 - Verify the borrower can renew with a hold on the record if AllowRenewalIfOtherItemsAvailable and onshelfhold are enabled' ); # Setting item not checked out to be not for loan but holdable - ModItem({ notforloan => -1 }, $biblionumber, $itemnumber2); + ModItem({ notforloan => -1 }, $biblio->biblionumber, $itemnumber2); ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 ); is( $renewokay, 0, 'Bug 14337 - Verify the borrower can not renew with a hold on the record if AllowRenewalIfOtherItemsAvailable is enabled but the only available item is notforloan' ); @@ -1144,7 +1142,7 @@ C4::Context->dbh->do("DELETE FROM accountlines"); my $branch = $library->{branchcode}; #Create another record - my ($biblionumber, $biblioitemnumber) = add_biblio('A title', 'Anonymous'); + my $biblio = $builder->gimme_a_biblio(); my (undef, undef, $itemnumber) = AddItem( { @@ -1153,7 +1151,7 @@ C4::Context->dbh->do("DELETE FROM accountlines"); barcode => $barcode, itype => $itemtype }, - $biblionumber + $biblio->biblionumber ); my $borrowernumber = Koha::Patron->new({ @@ -1174,7 +1172,7 @@ C4::Context->dbh->do("DELETE FROM accountlines"); { my $library = $builder->build({ source => 'Branch' }); - my ($biblionumber, $biblioitemnumber) = add_biblio(); + my $biblio = $builder->gimme_a_biblio(); my $barcode = 'just a barcode'; my ( undef, undef, $itemnumber ) = AddItem( @@ -1184,7 +1182,7 @@ C4::Context->dbh->do("DELETE FROM accountlines"); barcode => $barcode, itype => $itemtype }, - $biblionumber, + $biblio->biblionumber, ); my $patron = $builder->build({ source => 'Borrower', value => { branchcode => $library->{branchcode}, categorycode => $patron_category->{categorycode} } } ); @@ -1474,8 +1472,7 @@ subtest 'CanBookBeIssued + Statistic patrons "X"' => sub { subtest 'MultipleReserves' => sub { plan tests => 3; - my $title = 'Silence in the library'; - my ($biblionumber, $biblioitemnumber) = add_biblio($title, 'Moffat, Steven'); + my $biblio = $builder->gimme_a_biblio(); my $branch = $library2->{branchcode}; @@ -1488,7 +1485,7 @@ subtest 'MultipleReserves' => sub { replacementprice => 12.00, itype => $itemtype }, - $biblionumber + $biblio->biblionumber ); my $barcode2 = 'R00110002'; @@ -1500,7 +1497,7 @@ subtest 'MultipleReserves' => sub { replacementprice => 12.00, itype => $itemtype }, - $biblionumber + $biblio->biblionumber ); my $bibitems = ''; @@ -1532,9 +1529,9 @@ subtest 'MultipleReserves' => sub { ); my $reserving_borrowernumber1 = Koha::Patron->new(\%reserving_borrower_data1)->store->borrowernumber; AddReserve( - $branch, $reserving_borrowernumber1, $biblionumber, + $branch, $reserving_borrowernumber1, $biblio->biblionumber, $bibitems, $priority, $resdate, $expdate, $notes, - $title, $checkitem, $found + 'a title', $checkitem, $found ); my %reserving_borrower_data2 = ( @@ -1545,9 +1542,9 @@ subtest 'MultipleReserves' => sub { ); my $reserving_borrowernumber2 = Koha::Patron->new(\%reserving_borrower_data2)->store->borrowernumber; AddReserve( - $branch, $reserving_borrowernumber2, $biblionumber, + $branch, $reserving_borrowernumber2, $biblio->biblionumber, $bibitems, $priority, $resdate, $expdate, $notes, - $title, $checkitem, $found + 'a title', $checkitem, $found ); { @@ -1564,7 +1561,7 @@ subtest 'MultipleReserves' => sub { replacementprice => 12.00, itype => $itemtype }, - $biblionumber + $biblio->biblionumber ); { @@ -2012,9 +2009,7 @@ subtest '_FixAccountForLostAndReturned' => sub { ); my $library = $builder->build_object( { class => 'Koha::Libraries' } ); - # Generate test biblio - my $title = 'Koha for Dummies'; - my ( $biblionumber, $biblioitemnumber ) = add_biblio( $title, 'Hall, Daria' ); + my $biblio = $builder->gimme_a_biblio({ author => 'Hall, Daria' }); subtest 'Full write-off tests' => sub { @@ -2030,13 +2025,13 @@ subtest '_FixAccountForLostAndReturned' => sub { replacementprice => $replacement_amount, itype => $item_type->itemtype }, - $biblionumber + $biblio->biblionumber ); AddIssue( $patron->unblessed, $barcode ); # Simulate item marked as lost - ModItem( { itemlost => 3 }, $biblionumber, $item_id ); + ModItem( { itemlost => 3 }, $biblio->biblionumber, $item_id ); LostItem( $item_id, 1 ); my $processing_fee_lines = Koha::Account::Lines->search( @@ -2092,13 +2087,13 @@ subtest '_FixAccountForLostAndReturned' => sub { replacementprice => $replacement_amount, itype => $item_type->itemtype }, - $biblionumber + $biblio->biblionumber ); AddIssue( $patron->unblessed, $barcode ); # Simulate item marked as lost - ModItem( { itemlost => 1 }, $biblionumber, $item_id ); + ModItem( { itemlost => 1 }, $biblio->biblionumber, $item_id ); LostItem( $item_id, 1 ); my $processing_fee_lines = Koha::Account::Lines->search( @@ -2161,13 +2156,13 @@ subtest '_FixAccountForLostAndReturned' => sub { replacementprice => $replacement_amount, itype => $item_type->itemtype }, - $biblionumber + $biblio->biblionumber ); AddIssue( $patron->unblessed, $barcode ); # Simulate item marked as lost - ModItem( { itemlost => 3 }, $biblionumber, $item_id ); + ModItem( { itemlost => 3 }, $biblio->biblionumber, $item_id ); LostItem( $item_id, 1 ); my $processing_fee_lines = Koha::Account::Lines->search( @@ -2215,13 +2210,13 @@ subtest '_FixAccountForLostAndReturned' => sub { replacementprice => $replacement_amount, itype => $item_type->itemtype }, - $biblionumber + $biblio->biblionumber ); AddIssue( $patron->unblessed, $barcode ); # Simulate item marked as lost - ModItem( { itemlost => 1 }, $biblionumber, $item_id ); + ModItem( { itemlost => 1 }, $biblio->biblionumber, $item_id ); LostItem( $item_id, 1 ); my $processing_fee_lines = Koha::Account::Lines->search( @@ -2379,9 +2374,7 @@ subtest '_FixAccountForLostAndReturned' => sub { subtest '_FixOverduesOnReturn' => sub { plan tests => 10; - # Generate test biblio - my $title = 'Koha for Dummies'; - my ( $biblionumber, $biblioitemnumber ) = add_biblio($title, 'Hall, Kylie'); + my $biblio = $builder->gimme_a_biblio({ author => 'Hall, Kylie' }); my $barcode = 'KD987654321'; my $branchcode = $library2->{branchcode}; @@ -2394,7 +2387,7 @@ subtest '_FixOverduesOnReturn' => sub { replacementprice => 99.00, itype => $itemtype }, - $biblionumber + $biblio->biblionumber ); my $patron = $builder->build( { source => 'Borrower' } ); @@ -2863,29 +2856,6 @@ sub str { return $s; } -sub add_biblio { - my ($title, $author) = @_; - - my $marcflavour = C4::Context->preference('marcflavour'); - - my $biblio = MARC::Record->new(); - if ($title) { - my $tag = $marcflavour eq 'UNIMARC' ? '200' : '245'; - $biblio->append_fields( - MARC::Field->new($tag, ' ', ' ', a => $title), - ); - } - - if ($author) { - my ($tag, $code) = $marcflavour eq 'UNIMARC' ? (200, 'f') : (100, 'a'); - $biblio->append_fields( - MARC::Field->new($tag, ' ', ' ', $code => $author), - ); - } - - return AddBiblio($biblio, ''); -} - sub test_debarment_on_checkout { my ($params) = @_; my $item = $params->{item}; diff --git a/t/db_dependent/Holds.t b/t/db_dependent/Holds.t index 1e76ede04d..8091fd5319 100755 --- a/t/db_dependent/Holds.t +++ b/t/db_dependent/Holds.t @@ -54,12 +54,11 @@ $insert_sth->execute('DUMMY'); $insert_sth->execute('ONLY1'); # Setup Test------------------------ -# Create a biblio instance for testing -my ($bibnum, $title, $bibitemnum) = create_helper_biblio('DUMMY'); +my $biblio = $builder->gimme_a_biblio({ itemtype => 'DUMMY' }); # Create item instance for testing. my ($item_bibnum, $item_bibitemnum, $itemnumber) - = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum); + = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $biblio->biblionumber); # Create some borrowers my @borrowernumbers; @@ -73,26 +72,23 @@ foreach (1..$borrowers_count) { push @borrowernumbers, $borrowernumber; } -my $biblionumber = $bibnum; - # Create five item level holds foreach my $borrowernumber ( @borrowernumbers ) { AddReserve( $branch_1, $borrowernumber, - $biblionumber, + $biblio->biblionumber, my $bibitems = q{}, - my $priority = C4::Reserves::CalculatePriority( $biblionumber ), + my $priority = C4::Reserves::CalculatePriority( $biblio->biblionumber ), my $resdate, my $expdate, my $notes = q{}, - $title, + 'a title', my $checkitem = $itemnumber, my $found, ); } -my $biblio = Koha::Biblios->find( $biblionumber ); my $holds = $biblio->holds; is( $holds->count, $borrowers_count, 'Test GetReserves()' ); is( $holds->next->priority, 1, "Reserve 1 has a priority of 1" ); @@ -176,7 +172,7 @@ ok( ! $hold->suspend, "Test AutoUnsuspendReserves()" ); SuspendAll( borrowernumber => $borrowernumber, - biblionumber => $biblionumber, + biblionumber => $biblio->biblionumber, suspend => 1, suspend_until => '2012-01-01', ); @@ -186,7 +182,7 @@ is( $hold->suspend_until, '2012-01-01 00:00:00', "Test SuspendAll(), with date" SuspendAll( borrowernumber => $borrowernumber, - biblionumber => $biblionumber, + biblionumber => $biblio->biblionumber, suspend => 0, ); $hold = Koha::Holds->find( $reserve_id ); @@ -197,19 +193,19 @@ is( $hold->suspend_until, undef, "Test resuming with SuspendAll(), should have n AddReserve( $branch_1, $borrowernumbers[0], - $biblionumber, + $biblio->biblionumber, my $bibitems = q{}, my $priority, my $resdate, my $expdate, my $notes = q{}, - $title, + 'a title', my $checkitem, my $found, ); $patron = Koha::Patrons->find( $borrowernumber ); $holds = $patron->holds; -my $reserveid = Koha::Holds->search({ biblionumber => $bibnum, borrowernumber => $borrowernumbers[0] })->next->reserve_id; +my $reserveid = Koha::Holds->search({ biblionumber => $biblio->biblionumber, borrowernumber => $borrowernumbers[0] })->next->reserve_id; ModReserveMinusPriority( $itemnumber, $reserveid ); $holds = $patron->holds; is( $holds->next->itemnumber, $itemnumber, "Test ModReserveMinusPriority()" ); @@ -243,9 +239,9 @@ is( $hold->priority, '6', "Test AlterPriority(), move to bottom" ); # Note that canreservefromotherbranches has no effect if # IndependentBranches is OFF. -my ($foreign_bibnum, $foreign_title, $foreign_bibitemnum) = create_helper_biblio('DUMMY'); +my $foreign_biblio = $builder->gimme_a_biblio({ itemtype => 'DUMMY' }); my ($foreign_item_bibnum, $foreign_item_bibitemnum, $foreign_itemnumber) - = AddItem({ homebranch => $branch_2, holdingbranch => $branch_2 } , $foreign_bibnum); + = AddItem({ homebranch => $branch_2, holdingbranch => $branch_2 } , $foreign_biblio->biblionumber); $dbh->do('DELETE FROM issuingrules'); $dbh->do( q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record) @@ -288,14 +284,14 @@ ok( { # Regression test for bug 11336 # Test if ModReserve correctly recalculate the priorities - ($bibnum, $title, $bibitemnum) = create_helper_biblio('DUMMY'); - ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum); - my $reserveid1 = AddReserve($branch_1, $borrowernumbers[0], $bibnum, '', 1); - ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum); - my $reserveid2 = AddReserve($branch_1, $borrowernumbers[1], $bibnum, '', 2); - ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum); - my $reserveid3 = AddReserve($branch_1, $borrowernumbers[2], $bibnum, '', 3); - my $hhh = Koha::Holds->search({ biblionumber => $bibnum }); + $biblio = $builder->gimme_a_biblio({ itemtype => 'DUMMY' }); + ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $biblio->biblionumber); + my $reserveid1 = AddReserve($branch_1, $borrowernumbers[0], $biblio->biblionumber, '', 1); + ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $biblio->biblionumber); + my $reserveid2 = AddReserve($branch_1, $borrowernumbers[1], $biblio->biblionumber, '', 2); + ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $biblio->biblionumber); + my $reserveid3 = AddReserve($branch_1, $borrowernumbers[2], $biblio->biblionumber, '', 3); + my $hhh = Koha::Holds->search({ biblionumber => $biblio->biblionumber }); my $hold3 = Koha::Holds->find( $reserveid3 ); is( $hold3->priority, 3, "The 3rd hold should have a priority set to 3" ); ModReserve({ reserve_id => $reserveid1, rank => 'del' }); @@ -325,12 +321,12 @@ ok( CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status} eq 'damaged', ok( !defined( ( CheckReserves($itemnumber) )[1] ), "Hold cannot be trapped for damaged item with AllowHoldsOnDamagedItems disabled" ); # Regression test for bug 9532 -($bibnum, $title, $bibitemnum) = create_helper_biblio('CANNOT'); -($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CANNOT' } , $bibnum); +$biblio = $builder->gimme_a_biblio({ itemtype => 'CANNOT' }); +($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CANNOT' } , $biblio->biblionumber); AddReserve( $branch_1, $borrowernumbers[0], - $bibnum, + $biblio->biblionumber, '', 1, ); @@ -373,20 +369,20 @@ $dbh->do(q{ INSERT INTO branch_item_rules (branchcode, itemtype, holdallowed, returnbranch) VALUES (?, ?, ?, ?) }, {}, $branch_1, 'CAN', 1, 'homebranch'); -($bibnum, $title, $bibitemnum) = create_helper_biblio('CANNOT'); +$biblio = $builder->gimme_a_biblio({ itemtype => 'CANNOT' }); ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem( - { homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CANNOT' } , $bibnum); + { homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CANNOT' } , $biblio->biblionumber); is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status}, 'notReservable', "CanItemBeReserved should return 'notReservable'"); ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem( - { homebranch => $branch_2, holdingbranch => $branch_1, itype => 'CAN' } , $bibnum); + { homebranch => $branch_2, holdingbranch => $branch_1, itype => 'CAN' } , $biblio->biblionumber); is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status}, 'cannotReserveFromOtherBranches', "CanItemBeReserved should return 'cannotReserveFromOtherBranches'"); ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem( - { homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CAN' } , $bibnum); + { homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CAN' } , $biblio->biblionumber); is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status}, 'OK', "CanItemBeReserved should return 'OK'"); @@ -399,9 +395,9 @@ $dbh->do('DELETE FROM issues'); $dbh->do('DELETE FROM items'); $dbh->do('DELETE FROM biblio'); -( $bibnum, $title, $bibitemnum ) = create_helper_biblio('ONLY1'); +$biblio = $builder->gimme_a_biblio({ itemtype => 'ONLY1' }); ( $item_bibnum, $item_bibitemnum, $itemnumber ) - = AddItem( { homebranch => $branch_1, holdingbranch => $branch_1 }, $bibnum ); + = AddItem( { homebranch => $branch_1, holdingbranch => $branch_1 }, $biblio->biblionumber ); $dbh->do( q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record) @@ -412,7 +408,7 @@ $dbh->do( is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status}, 'OK', 'Patron can reserve item with hold limit of 1, no holds placed' ); -my $res_id = AddReserve( $branch_1, $borrowernumbers[0], $bibnum, '', 1, ); +my $res_id = AddReserve( $branch_1, $borrowernumbers[0], $biblio->biblionumber, '', 1, ); is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status}, 'tooManyReserves', 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed' ); @@ -424,10 +420,10 @@ subtest 'Test max_holds per library/patron category' => sub { $dbh->do('DELETE FROM issuingrules'); $dbh->do('DELETE FROM circulation_rules'); - ( $bibnum, $title, $bibitemnum ) = create_helper_biblio('TEST'); + $biblio = $builder->gimme_a_biblio({ itemtype => 'TEST' }); ( $item_bibnum, $item_bibitemnum, $itemnumber ) = AddItem( { homebranch => $branch_1, holdingbranch => $branch_1 }, - $bibnum ); + $biblio->biblionumber ); $dbh->do( q{ INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record) @@ -436,9 +432,9 @@ subtest 'Test max_holds per library/patron category' => sub { {}, '*', '*', 'TEST', 99, 99 ); - AddReserve( $branch_1, $borrowernumbers[0], $bibnum, '', 1, ); - AddReserve( $branch_1, $borrowernumbers[0], $bibnum, '', 1, ); - AddReserve( $branch_1, $borrowernumbers[0], $bibnum, '', 1, ); + AddReserve( $branch_1, $borrowernumbers[0], $biblio->biblionumber, '', 1, ); + AddReserve( $branch_1, $borrowernumbers[0], $biblio->biblionumber, '', 1, ); + AddReserve( $branch_1, $borrowernumbers[0], $biblio->biblionumber, '', 1, ); my $count = Koha::Holds->search( { borrowernumber => $borrowernumbers[0] } )->count(); @@ -494,9 +490,9 @@ subtest 'Test max_holds per library/patron category' => sub { subtest 'Pickup location availability tests' => sub { plan tests => 4; - my ( $bibnum, $title, $bibitemnum ) = create_helper_biblio('ONLY1'); + $biblio = $builder->gimme_a_biblio({ itemtype => 'ONLY1' }); my ( $item_bibnum, $item_bibitemnum, $itemnumber ) - = AddItem( { homebranch => $branch_1, holdingbranch => $branch_1 }, $bibnum ); + = AddItem( { homebranch => $branch_1, holdingbranch => $branch_1 }, $biblio->biblionumber ); #Add a default rule to allow some holds $dbh->do( q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record) @@ -551,26 +547,26 @@ subtest 'CanItemBeReserved / holds_per_day tests' => sub { my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); # Create 3 biblios with items - my ($bibnum_1) = create_helper_biblio( $itemtype->itemtype ); + my $biblio_1 = $builder->gimme_a_biblio({ itemtype => $itemtype->itemtype }); my ( undef, undef, $itemnumber_1 ) = AddItem( { homebranch => $library->branchcode, holdingbranch => $library->branchcode }, - $bibnum + $biblio_1->biblionumber ); - my ($bibnum_2) = create_helper_biblio( $itemtype->itemtype ); + my $biblio_2 = $builder->gimme_a_biblio({ itemtype => $itemtype->itemtype }); my ( undef, undef, $itemnumber_2 ) = AddItem( { homebranch => $library->branchcode, holdingbranch => $library->branchcode }, - $bibnum_2 + $biblio_2->biblionumber ); - my ($bibnum_3) = create_helper_biblio( $itemtype->itemtype ); + my $biblio_3 = $builder->gimme_a_biblio({ itemtype => $itemtype->itemtype }); my ( undef, undef, $itemnumber_3 ) = AddItem( { homebranch => $library->branchcode, holdingbranch => $library->branchcode }, - $bibnum_3 + $biblio_3->biblionumber ); Koha::IssuingRules->search->delete; @@ -590,7 +586,7 @@ subtest 'CanItemBeReserved / holds_per_day tests' => sub { 'Patron can reserve item with hold limit of 1, no holds placed' ); - AddReserve( $library->branchcode, $patron->borrowernumber, $bibnum_1, '', 1, ); + AddReserve( $library->branchcode, $patron->borrowernumber, $biblio_1->biblionumber, '', 1, ); is_deeply( CanItemBeReserved( $patron->borrowernumber, $itemnumber_1 ), @@ -608,7 +604,7 @@ subtest 'CanItemBeReserved / holds_per_day tests' => sub { ); # Add a second reserve - my $res_id = AddReserve( $library->branchcode, $patron->borrowernumber, $bibnum_2, '', 1, ); + my $res_id = AddReserve( $library->branchcode, $patron->borrowernumber, $biblio_2->biblionumber, '', 1, ); is_deeply( CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ), { status => 'tooManyReservesToday', limit => 2 }, @@ -644,14 +640,14 @@ subtest 'CanItemBeReserved / holds_per_day tests' => sub { { status => 'OK' }, 'Patron can reserve if holds_per_day is undef (i.e. undef is unlimited daily cap)' ); - AddReserve( $library->branchcode, $patron->borrowernumber, $bibnum_1, '', 1, ); - AddReserve( $library->branchcode, $patron->borrowernumber, $bibnum_2, '', 1, ); + AddReserve( $library->branchcode, $patron->borrowernumber, $biblio_1->biblionumber, '', 1, ); + AddReserve( $library->branchcode, $patron->borrowernumber, $biblio_2->biblionumber, '', 1, ); is_deeply( CanItemBeReserved( $patron->borrowernumber, $itemnumber_3 ), { status => 'OK' }, 'Patron can reserve if holds_per_day is undef (i.e. undef is unlimited daily cap)' ); - AddReserve( $library->branchcode, $patron->borrowernumber, $bibnum_3, '', 1, ); + AddReserve( $library->branchcode, $patron->borrowernumber, $biblio_3->biblionumber, '', 1, ); is_deeply( CanItemBeReserved( $patron->borrowernumber, $itemnumber_3 ), { status => 'tooManyReserves', limit => 3 }, @@ -660,16 +656,3 @@ subtest 'CanItemBeReserved / holds_per_day tests' => sub { $schema->storage->txn_rollback; }; - -# Helper method to set up a Biblio. -sub create_helper_biblio { - my $itemtype = shift; - my $bib = MARC::Record->new(); - my $title = 'Silence in the library'; - $bib->append_fields( - MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'), - MARC::Field->new('245', ' ', ' ', a => $title), - MARC::Field->new('942', ' ', ' ', c => $itemtype), - ); - return ($bibnum, $title, $bibitemnum) = AddBiblio($bib, ''); -} diff --git a/t/db_dependent/Holds/LocalHoldsPriority.t b/t/db_dependent/Holds/LocalHoldsPriority.t index af914d6895..5e087161b5 100755 --- a/t/db_dependent/Holds/LocalHoldsPriority.t +++ b/t/db_dependent/Holds/LocalHoldsPriority.t @@ -40,15 +40,13 @@ my $itemtype = $builder->build( my $borrowers_count = 5; -# Create a helper biblio -my ( $bibnum, $title, $bibitemnum ) = create_helper_biblio(); -# Create a helper item for the biblio. +my $biblio = $builder->gimme_a_biblio(); my ( $item_bibnum, $item_bibitemnum, $itemnumber ) = AddItem( { homebranch => $library4->{branchcode}, holdingbranch => $library3->{branchcode}, itype => $itemtype }, - $bibnum + $biblio->biblionumber, ); @@ -67,21 +65,19 @@ foreach ( 1 .. $borrowers_count ) { push @borrowernumbers, $borrowernumber; } -my $biblionumber = $bibnum; - # Create five item level holds my $i = 1; foreach my $borrowernumber (@borrowernumbers) { AddReserve( $branchcodes[$i], $borrowernumber, - $biblionumber, + $biblio->biblionumber, my $bibitems = q{}, my $priority = $i, my $resdate, my $expdate, my $notes = q{}, - $title, + 'a title', my $checkitem, my $found, ); @@ -116,14 +112,3 @@ t::lib::Mocks::mock_preference( 'LocalHoldsPriorityPatronControl', 'HomeLibrary' t::lib::Mocks::mock_preference( 'LocalHoldsPriorityItemControl', 'homebranch' ); ($status, $reserve, $all_reserves) = CheckReserves($itemnumber); ok( $reserve->{borrowernumber} eq $borrowernumbers[2], "Received expected results with HomeLibrary/homebranch" ); - -# Helper method to set up a Biblio. -sub create_helper_biblio { - my $bib = MARC::Record->new(); - my $title = 'Silence in the library'; - $bib->append_fields( - MARC::Field->new( '100', ' ', ' ', a => 'Moffat, Steven' ), - MARC::Field->new( '245', ' ', ' ', a => $title ), - ); - return ( $bibnum, $title, $bibitemnum ) = AddBiblio( $bib, '' ); -} diff --git a/t/db_dependent/Holds/RevertWaitingStatus.t b/t/db_dependent/Holds/RevertWaitingStatus.t index 783006a28e..24bba184c5 100755 --- a/t/db_dependent/Holds/RevertWaitingStatus.t +++ b/t/db_dependent/Holds/RevertWaitingStatus.t @@ -46,10 +46,7 @@ t::lib::Mocks::mock_userenv({ flags => 1, userid => '1', branchcode => $branchco my $borrowers_count = 3; -# Create a biblio instance -my ( $bibnum, $title, $bibitemnum ) = create_helper_biblio(); - -# Create an item +my $biblio = $builder->gimme_a_biblio(); my $item_barcode = 'my_barcode'; my ( $item_bibnum, $item_bibitemnum, $itemnumber ) = AddItem( { homebranch => $branchcode, @@ -57,7 +54,7 @@ my ( $item_bibnum, $item_bibitemnum, $itemnumber ) = AddItem( barcode => $item_barcode, itype => $itemtype }, - $bibnum + $biblio->biblionumber, ); # Create some borrowers @@ -73,20 +70,18 @@ foreach my $i ( 1 .. $borrowers_count ) { push @borrowernumbers, $borrowernumber; } -my $biblionumber = $bibnum; - # Create five item level holds foreach my $borrowernumber (@borrowernumbers) { AddReserve( $branchcode, $borrowernumber, - $biblionumber, + $biblio->biblionumber, my $bibitems = q{}, my $priority, my $resdate, my $expdate, my $notes = q{}, - $title, + 'a title', my $checkitem, my $found, ); @@ -104,14 +99,3 @@ ok( $priorities->[0]->[0] == 1, 'First hold has a priority of 1' ); ok( $priorities->[1]->[0] == 2, 'Second hold has a priority of 2' ); $schema->storage->txn_rollback; - -# Helper method to set up a Biblio. -sub create_helper_biblio { - my $bib = MARC::Record->new(); - my $title = 'Silence in the library'; - $bib->append_fields( - MARC::Field->new( '100', ' ', ' ', a => 'Moffat, Steven' ), - MARC::Field->new( '245', ' ', ' ', a => $title ), - ); - return ( $bibnum, $title, $bibitemnum ) = AddBiblio( $bib, '' ); -} diff --git a/t/db_dependent/Items.t b/t/db_dependent/Items.t index 623184dbfb..719c56492c 100755 --- a/t/db_dependent/Items.t +++ b/t/db_dependent/Items.t @@ -57,12 +57,12 @@ subtest 'General Add, Get and Del tests' => sub { # Create a biblio instance for testing t::lib::Mocks::mock_preference('marcflavour', 'MARC21'); - my ($bibnum, $bibitemnum) = get_biblio(); + my $biblio = $builder->gimme_a_biblio(); # Add an item. - my ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $library->{branchcode}, holdingbranch => $library->{branchcode}, location => $location, itype => $itemtype->{itemtype} } , $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."); + my ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $library->{branchcode}, holdingbranch => $library->{branchcode}, location => $location, itype => $itemtype->{itemtype} } , $biblio->biblionumber); + cmp_ok($item_bibnum, '==', $biblio->biblionumber, "New item is linked to correct biblionumber."); + cmp_ok($item_bibitemnum, '==', $biblio->biblioitem->biblioitemnumber, "New item is linked to correct biblioitemnumber."); # Get item. my $getitem = GetItem($itemnumber); @@ -75,29 +75,29 @@ subtest 'General Add, Get and Del tests' => sub { # Do not modify anything, and do not explode! my $dbh = C4::Context->dbh; local $dbh->{RaiseError} = 1; - ModItem({}, $bibnum, $itemnumber); + ModItem({}, $biblio->biblionumber, $itemnumber); # Modify item; setting barcode. - ModItem({ barcode => '987654321' }, $bibnum, $itemnumber); + ModItem({ barcode => '987654321' }, $biblio->biblionumber, $itemnumber); my $moditem = GetItem($itemnumber); cmp_ok($moditem->{'barcode'}, '==', '987654321', 'Modified item barcode successfully to: '.$moditem->{'barcode'} . '.'); # Delete item. - DelItem({ biblionumber => $bibnum, itemnumber => $itemnumber }); + DelItem({ biblionumber => $biblio->biblionumber, itemnumber => $itemnumber }); my $getdeleted = GetItem($itemnumber); is($getdeleted->{'itemnumber'}, undef, "Item deleted as expected."); - ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $library->{branchcode}, holdingbranch => $library->{branchcode}, location => $location, permanent_location => 'my permanent location', itype => $itemtype->{itemtype} } , $bibnum); + ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $library->{branchcode}, holdingbranch => $library->{branchcode}, location => $location, permanent_location => 'my permanent location', itype => $itemtype->{itemtype} } , $biblio->biblionumber); $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" ); - ModItem({ location => $location }, $bibnum, $itemnumber); + ModItem({ location => $location }, $biblio->biblionumber, $itemnumber); $getitem = GetItem($itemnumber); is( $getitem->{location}, $location, "The location should have been set to correct location" ); is( $getitem->{permanent_location}, $location, "The permanent_location should have been set to location" ); - ModItem({ location => 'CART' }, $bibnum, $itemnumber); + ModItem({ location => 'CART' }, $biblio->biblionumber, $itemnumber); $getitem = GetItem($itemnumber); is( $getitem->{location}, 'CART', "The location should have been set to CART" ); is( $getitem->{permanent_location}, $location, "The permanent_location should not have been set to CART" ); @@ -169,7 +169,7 @@ subtest 'GetHiddenItemnumbers tests' => sub { # Create a new biblio t::lib::Mocks::mock_preference('marcflavour', 'MARC21'); - my ($biblionumber, $biblioitemnumber) = get_biblio(); + my $biblio = $builder->gimme_a_biblio(); # Add two items my ( $item1_bibnum, $item1_bibitemnum, $item1_itemnumber ) = AddItem( @@ -179,7 +179,7 @@ subtest 'GetHiddenItemnumbers tests' => sub { withdrawn => 1, itype => $itemtype->{itemtype}, }, - $biblionumber + $biblio->biblionumber ); my ( $item2_bibnum, $item2_bibitemnum, $item2_itemnumber ) = AddItem( { @@ -188,7 +188,7 @@ subtest 'GetHiddenItemnumbers tests' => sub { withdrawn => 0, itype => $itemtype->{itemtype}, }, - $biblionumber + $biblio->biblionumber ); my $opachiddenitems; @@ -277,7 +277,7 @@ subtest 'GetItemsInfo tests' => sub { )->store(); # Add a biblio - my ($biblionumber, $biblioitemnumber) = get_biblio(); + my $biblio = $builder->gimme_a_biblio(); # Add an item my ( $item_bibnum, $item_bibitemnum, $itemnumber ) = AddItem( { @@ -286,7 +286,7 @@ subtest 'GetItemsInfo tests' => sub { itype => $itemtype->{itemtype}, restricted => 1, }, - $biblionumber + $biblio->biblionumber ); my $library = Koha::Libraries->find( $library1->{branchcode} ); @@ -297,7 +297,7 @@ subtest 'GetItemsInfo tests' => sub { $library->opac_info("holdingbranch OPAC info"); $library->store; - my @results = GetItemsInfo( $biblionumber ); + my @results = GetItemsInfo( $biblio->biblionumber ); ok( @results, 'GetItemsInfo returns results'); is( $results[0]->{ home_branch_opac_info }, "homebranch OPAC info", @@ -376,7 +376,7 @@ subtest 'SearchItems test' => sub { t::lib::Mocks::mock_preference('marcflavour', 'MARC21'); my $cpl_items_before = SearchItemsByField( 'homebranch', $library1->{branchcode}); - my ($biblionumber) = get_biblio(); + my $biblio = $builder->gimme_a_biblio(); my (undef, $initial_items_count) = SearchItems(undef, {rows => 1}); @@ -385,12 +385,12 @@ subtest 'SearchItems test' => sub { homebranch => $library1->{branchcode}, holdingbranch => $library1->{branchcode}, itype => $itemtype->{itemtype}, - }, $biblionumber); + }, $biblio->biblionumber); my (undef, undef, $item2_itemnumber) = AddItem({ homebranch => $library2->{branchcode}, holdingbranch => $library2->{branchcode}, itype => $itemtype->{itemtype}, - }, $biblionumber); + }, $biblio->biblionumber); my ($items, $total_results); @@ -495,7 +495,7 @@ subtest 'SearchItems test' => sub { ) ); my (undef, undef, $item3_itemnumber) = AddItemFromMarc($item3_record, - $biblionumber); + $biblio->biblionumber); # Search item where item subfield z is "foobar" $filter = { @@ -517,7 +517,7 @@ subtest 'SearchItems test' => sub { $cache->clear_from_cache("default_value_for_mod_marc-"); $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode"); - ModItemFromMarc($item3_record, $biblionumber, $item3_itemnumber); + ModItemFromMarc($item3_record, $biblio->biblionumber, $item3_itemnumber); # Make sure the link is used my $item3 = GetItem($item3_itemnumber); @@ -553,14 +553,14 @@ subtest 'Koha::Item(s) tests' => sub { # Create a biblio and item for testing t::lib::Mocks::mock_preference('marcflavour', 'MARC21'); - my ($bibnum, $bibitemnum) = get_biblio(); + my $biblio = $builder->gimme_a_biblio(); my ( $item_bibnum, $item_bibitemnum, $itemnumber ) = AddItem( { homebranch => $library1->{branchcode}, holdingbranch => $library2->{branchcode}, itype => $itemtype->{itemtype}, }, - $bibnum + $biblio->biblionumber ); # Get item. @@ -594,7 +594,7 @@ subtest 'C4::Biblio::EmbedItemsInMarcBiblio' => sub { source => 'Itemtype', }); - my ( $biblionumber, $biblioitemnumber ) = get_biblio(); + my $biblio = $builder->gimme_a_biblio(); my $item_infos = [ { homebranch => $library1->{branchcode}, holdingbranch => $library1->{branchcode} }, { homebranch => $library1->{branchcode}, holdingbranch => $library1->{branchcode} }, @@ -617,7 +617,7 @@ subtest 'C4::Biblio::EmbedItemsInMarcBiblio' => sub { holdingbranch => $item_info->{holdingbanch}, itype => $itemtype->{itemtype}, }, - $biblionumber + $biblio->biblionumber ); push @itemnumbers, $itemnumber; } @@ -627,32 +627,32 @@ subtest 'C4::Biblio::EmbedItemsInMarcBiblio' => sub { my ($itemfield) = C4::Biblio::GetMarcFromKohaField( 'items.itemnumber', '' ); - my $record = C4::Biblio::GetMarcBiblio({ biblionumber => $biblionumber }); + my $record = C4::Biblio::GetMarcBiblio({ biblionumber => $biblio->biblionumber }); warning_is { C4::Biblio::EmbedItemsInMarcBiblio() } { carped => 'EmbedItemsInMarcBiblio: No MARC record passed' }, 'Should carp is no record passed.'; C4::Biblio::EmbedItemsInMarcBiblio({ marc_record => $record, - biblionumber => $biblionumber }); + biblionumber => $biblio->biblionumber }); my @items = $record->field($itemfield); is( scalar @items, $number_of_items, 'Should return all items' ); my $marc_with_items = C4::Biblio::GetMarcBiblio({ - biblionumber => $biblionumber, + biblionumber => $biblio->biblionumber, embed_items => 1 }); is_deeply( $record, $marc_with_items, 'A direct call to GetMarcBiblio with items matches'); C4::Biblio::EmbedItemsInMarcBiblio({ marc_record => $record, - biblionumber => $biblionumber, + biblionumber => $biblio->biblionumber, item_numbers => [ $itemnumbers[1], $itemnumbers[3] ] }); @items = $record->field($itemfield); is( scalar @items, 2, 'Should return all items present in the list' ); C4::Biblio::EmbedItemsInMarcBiblio({ marc_record => $record, - biblionumber => $biblionumber, + biblionumber => $biblio->biblionumber, opac => 1 }); @items = $record->field($itemfield); is( scalar @items, $number_of_items, 'Should return all items for opac' ); @@ -663,7 +663,7 @@ subtest 'C4::Biblio::EmbedItemsInMarcBiblio' => sub { C4::Biblio::EmbedItemsInMarcBiblio({ marc_record => $record, - biblionumber => $biblionumber }); + biblionumber => $biblio->biblionumber }); @items = $record->field($itemfield); is( scalar @items, $number_of_items, @@ -671,7 +671,7 @@ subtest 'C4::Biblio::EmbedItemsInMarcBiblio' => sub { C4::Biblio::EmbedItemsInMarcBiblio({ marc_record => $record, - biblionumber => $biblionumber, + biblionumber => $biblio->biblionumber, opac => 1 }); @items = $record->field($itemfield); is( @@ -685,7 +685,7 @@ subtest 'C4::Biblio::EmbedItemsInMarcBiblio' => sub { t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems ); C4::Biblio::EmbedItemsInMarcBiblio({ marc_record => $record, - biblionumber => $biblionumber, + biblionumber => $biblio->biblionumber, opac => 1 }); @items = $record->field($itemfield); is( @@ -721,7 +721,7 @@ subtest 'C4::Items::_build_default_values_for_mod_marc' => sub { my $itemtype = $builder->build({ source => 'Itemtype' })->{itemtype}; # Create a record with a barcode - my ($biblionumber) = get_biblio( $framework->{frameworkcode} ); + my $biblio = $builder->gimme_a_biblio({ frameworkcode => $framework->{frameworkcode} }); my $item_record = new MARC::Record; my $a_barcode = 'a barcode'; my $barcode_field = MARC::Field->new( @@ -734,7 +734,7 @@ subtest 'C4::Items::_build_default_values_for_mod_marc' => sub { y => $itemtype ); $item_record->append_fields( $barcode_field ); - my (undef, undef, $item_itemnumber) = AddItemFromMarc($item_record, $biblionumber); + my (undef, undef, $item_itemnumber) = AddItemFromMarc($item_record, $biblio->biblionumber); # Make sure everything has been set up my $item = GetItem($item_itemnumber); @@ -743,13 +743,13 @@ subtest 'C4::Items::_build_default_values_for_mod_marc' => sub { # Delete the barcode field and save the record $item_record->delete_fields( $barcode_field ); $item_record->append_fields( $itemtype_field ); # itemtype is mandatory - ModItemFromMarc($item_record, $biblionumber, $item_itemnumber); + ModItemFromMarc($item_record, $biblio->biblionumber, $item_itemnumber); $item = GetItem($item_itemnumber); is( $item->{barcode}, undef, 'The default value should have been set to the barcode, the field is mapped to a kohafield' ); # Re-add the barcode field and save the record $item_record->append_fields( $barcode_field ); - ModItemFromMarc($item_record, $biblionumber, $item_itemnumber); + ModItemFromMarc($item_record, $biblio->biblionumber, $item_itemnumber); $item = GetItem($item_itemnumber); is( $item->{barcode}, $a_barcode, 'Everything has been set up correctly, the barcode is defined as expected' ); @@ -770,7 +770,7 @@ subtest 'C4::Items::_build_default_values_for_mod_marc' => sub { ); $item_record->append_fields( $another_barcode_field ); # The DB value should not have been updated - ModItemFromMarc($item_record, $biblionumber, $item_itemnumber); + ModItemFromMarc($item_record, $biblio->biblionumber, $item_itemnumber); $item = GetItem($item_itemnumber); is ( $item->{barcode}, $a_barcode, 'items.barcode is not mapped anymore, so the DB column has not been updated' ); @@ -887,24 +887,24 @@ subtest 'Test logging for ModItem' => sub { # Create a biblio instance for testing t::lib::Mocks::mock_preference('marcflavour', 'MARC21'); - my ($bibnum, $bibitemnum) = get_biblio(); + my $biblio = $builder->gimme_a_biblio(); # Add an item. - my ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $library->{branchcode}, holdingbranch => $library->{branchcode}, location => $location, itype => $itemtype->{itemtype} } , $bibnum); + my ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $library->{branchcode}, holdingbranch => $library->{branchcode}, location => $location, itype => $itemtype->{itemtype} } , $biblio->biblionumber); # False means no logging $schema->resultset('ActionLog')->search()->delete(); - ModItem({ location => $location }, $bibnum, $itemnumber, { log_action => 0 }); + ModItem({ location => $location }, $biblio->biblionumber, $itemnumber, { log_action => 0 }); is( $schema->resultset('ActionLog')->count(), 0, 'False value does not trigger logging' ); # True means logging $schema->resultset('ActionLog')->search()->delete(); - ModItem({ location => $location }, $bibnum, $itemnumber, { log_action => 1 }); + ModItem({ location => $location }, $biblio->biblionumber, $itemnumber, { log_action => 1 }); is( $schema->resultset('ActionLog')->count(), 1, 'True value does trigger logging' ); # Undefined defaults to true $schema->resultset('ActionLog')->search()->delete(); - ModItem({ location => $location }, $bibnum, $itemnumber); + ModItem({ location => $location }, $biblio->biblionumber, $itemnumber); is( $schema->resultset('ActionLog')->count(), 1, 'Undefined value defaults to true, triggers logging' ); $schema->storage->txn_rollback; @@ -994,16 +994,3 @@ subtest 'Split subfields in Item2Marc (Bug 21774)' => sub { $schema->storage->txn_rollback; }; - -# Helper method to set up a Biblio. -sub get_biblio { - my ( $frameworkcode ) = @_; - $frameworkcode //= ''; - my $bib = MARC::Record->new(); - $bib->append_fields( - MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'), - MARC::Field->new('245', ' ', ' ', a => 'Silence in the library'), - ); - my ($bibnum, $bibitemnum) = AddBiblio($bib, $frameworkcode); - return ($bibnum, $bibitemnum); -} diff --git a/t/db_dependent/Items/DelItem.t b/t/db_dependent/Items/DelItem.t index 03f1f5840e..4d719a4556 100644 --- a/t/db_dependent/Items/DelItem.t +++ b/t/db_dependent/Items/DelItem.t @@ -16,20 +16,20 @@ my $library = $builder->build({ source => 'Branch', }); -my ( $biblionumber, $bibitemnum ) = get_biblio(); +my $biblio = $builder->gimme_a_biblio(); my ( $item_bibnum, $item_bibitemnum, $itemnumber ); ( $item_bibnum, $item_bibitemnum, $itemnumber ) = - AddItem( { homebranch => $library->{branchcode}, holdingbranch => $library->{branchcode} }, $biblionumber ); + AddItem( { homebranch => $library->{branchcode}, holdingbranch => $library->{branchcode} }, $biblio->biblionumber ); -my $deleted = DelItem( { biblionumber => $biblionumber, itemnumber => $itemnumber } ); +my $deleted = DelItem( { biblionumber => $biblio->biblionumber, itemnumber => $itemnumber } ); is( $deleted, 1, "DelItem should return 1 if the item has been deleted" ); 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 => $library->{branchcode}, holdingbranch => $library->{branchcode} }, $biblionumber ); -$deleted = DelItem( { biblionumber => $biblionumber, itemnumber => $itemnumber } ); + AddItem( { homebranch => $library->{branchcode}, holdingbranch => $library->{branchcode} }, $biblio->biblionumber ); +$deleted = DelItem( { biblionumber => $biblio->biblionumber, itemnumber => $itemnumber } ); is( $deleted, 1, "DelItem should return 1 if the item has been deleted" ); $deleted_item = GetItem($itemnumber); is( $deleted_item->{itemnumber}, undef, "DelItem without biblionumber parameter - the item should be deleted." ); @@ -37,16 +37,5 @@ is( $deleted_item->{itemnumber}, undef, "DelItem without biblionumber parameter $deleted = DelItem( { itemnumber => $itemnumber + 1} ); is ( $deleted, 0, "DelItem should return 0 if no item has been deleted" ); -$deleted = DelItem( { itemnumber => $itemnumber + 1, biblionumber => $biblionumber } ); +$deleted = DelItem( { itemnumber => $itemnumber + 1, biblionumber => $biblio->biblionumber } ); is ( $deleted, 0, "DelItem should return 0 if no item has been deleted" ); - -# Helper method to set up a Biblio. -sub get_biblio { - my $bib = MARC::Record->new(); - $bib->append_fields( - MARC::Field->new( '100', ' ', ' ', a => 'Moffat, Steven' ), - MARC::Field->new( '245', ' ', ' ', a => 'Silence in the library' ), - ); - my ( $bibnum, $bibitemnum ) = AddBiblio( $bib, '' ); - return ( $bibnum, $bibitemnum ); -} diff --git a/t/db_dependent/Items/GetItemsForInventory.t b/t/db_dependent/Items/GetItemsForInventory.t index 47a5d82332..b54d521848 100755 --- a/t/db_dependent/Items/GetItemsForInventory.t +++ b/t/db_dependent/Items/GetItemsForInventory.t @@ -75,8 +75,8 @@ subtest 'Skip items with waiting holds' => sub { my $title_1 = 'Title 1, '; my $title_2 = 'Title 2, bizzarre one so doesn\'t already exist'; - my $biblio_1 = create_helper_biblio( $itemtype->itemtype, $title_1 ); - my $biblio_2 = create_helper_biblio( $itemtype->itemtype, $title_2 ); + my $biblio_1 = $builder->gimme_a_biblio({ itemtype => $itemtype->itemtype, title => $title_1 }); + my $biblio_2 = $builder->gimme_a_biblio({ itemtype => $itemtype->itemtype, title => $title_2 }); my ( $items_1, $first_items_count ) = GetItemsForInventory(); is( scalar @{$items_1}, $first_items_count, 'Results and count match' ); @@ -297,19 +297,3 @@ sub OldWay { # FIXME Do we really still need so much code to check results ?? return (\@results, $iTotalRecords); } - -# Helper method to set up a Biblio. -sub create_helper_biblio { - my $itemtype = shift; - my $title = shift; - my $record = MARC::Record->new(); - - $record->append_fields( - MARC::Field->new( '245', ' ', ' ', a => $title ), - MARC::Field->new( '942', ' ', ' ', c => $itemtype ), - ); - - my ($biblio_id) = AddBiblio( $record, '' ); - - return Koha::Biblios->find($biblio_id); -} diff --git a/t/db_dependent/Koha/BiblioUtils/Iterator.t b/t/db_dependent/Koha/BiblioUtils/Iterator.t index 9009b007e3..26a3c49483 100644 --- a/t/db_dependent/Koha/BiblioUtils/Iterator.t +++ b/t/db_dependent/Koha/BiblioUtils/Iterator.t @@ -47,30 +47,17 @@ my $itemtype = $builder->build({ # Create a biblio instance for testing t::lib::Mocks::mock_preference('marcflavour', 'MARC21'); -my ($bibnum, $bibitemnum) = get_biblio(); +my $biblio = $builder->gimme_a_biblio(); # Add an item. -my ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $library->{branchcode}, holdingbranch => $library->{branchcode}, location => $location, itype => $itemtype->{itemtype} } , $bibnum); +my ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $library->{branchcode}, holdingbranch => $library->{branchcode}, location => $location, itype => $itemtype->{itemtype} } , $biblio->biblionumber); my $rs = $schema->resultset('Biblioitem')->search({}); my $iterator = Koha::BiblioUtils::Iterator->new($rs, items => 1 ); my $record = $iterator->next(); -my $expected_tags = [ 100, 245, 952, 999 ]; +my $expected_tags = [ 100, 245, 942, 952, 999 ]; my @result_tags = map { $_->tag() } $record->field('...'); my @sorted_tags = sort @result_tags; is_deeply(\@sorted_tags,$expected_tags, "Got the same tags as expected"); $schema->storage->txn_rollback(); - -# Helper method to set up a Biblio. -sub get_biblio { - my ( $frameworkcode ) = @_; - $frameworkcode //= ''; - my $bib = MARC::Record->new(); - $bib->append_fields( - MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'), - MARC::Field->new('245', ' ', ' ', a => 'Silence in the library'), - ); - my ($bibnum, $bibitemnum) = AddBiblio($bib, $frameworkcode); - return ($bibnum, $bibitemnum); -} diff --git a/t/db_dependent/Koha/Biblios.t b/t/db_dependent/Koha/Biblios.t index e92ae415b1..60c5157f25 100644 --- a/t/db_dependent/Koha/Biblios.t +++ b/t/db_dependent/Koha/Biblios.t @@ -143,11 +143,10 @@ subtest 'can_be_transferred' => sub { my $library1 = $builder->build_object( { class => 'Koha::Libraries' } ); my $library2 = $builder->build_object( { class => 'Koha::Libraries' } ); my $library3 = $builder->build_object( { class => 'Koha::Libraries' } ); - my ($bibnum, $title, $bibitemnum) = create_helper_biblio('ONLY1'); + my $biblio = $builder->gimme_a_biblio({ itemtype => 'ONLY1' }); my ($item_bibnum, $item_bibitemnum, $itemnumber) - = AddItem({ homebranch => $library1->branchcode, holdingbranch => $library1->branchcode }, $bibnum); + = AddItem({ homebranch => $library1->branchcode, holdingbranch => $library1->branchcode }, $biblio->biblionumber); my $item = Koha::Items->find($itemnumber); - my $biblio = Koha::Biblios->find($bibnum); is(Koha::Item::Transfer::Limits->search({ fromBranch => $library1->branchcode, @@ -188,17 +187,3 @@ subtest 'can_be_transferred' => sub { }; $schema->storage->txn_rollback; - -# Helper method to set up a Biblio. -sub create_helper_biblio { - my $itemtype = shift; - my ($bibnum, $title, $bibitemnum); - my $bib = MARC::Record->new(); - $title = 'Silence in the library'; - $bib->append_fields( - MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'), - MARC::Field->new('245', ' ', ' ', a => $title), - MARC::Field->new('942', ' ', ' ', c => $itemtype), - ); - return ($bibnum, $title, $bibitemnum) = AddBiblio($bib, ''); -} diff --git a/t/db_dependent/OAI/Sets.t b/t/db_dependent/OAI/Sets.t index 88f14bf8fa..ede51dac4c 100644 --- a/t/db_dependent/OAI/Sets.t +++ b/t/db_dependent/OAI/Sets.t @@ -27,6 +27,8 @@ use Koha::Database; use C4::Biblio; use C4::OAI::Sets; +use t::lib::TestBuilder; + my $schema = Koha::Database->new->schema; $schema->storage->txn_begin; my $dbh = C4::Context->dbh; @@ -36,7 +38,7 @@ $dbh->do('DELETE FROM oai_sets_descriptions'); $dbh->do('DELETE FROM oai_sets_mappings'); $dbh->do('DELETE FROM oai_sets_biblios'); - +my $builder = t::lib::TestBuilder->new; # ---------- Testing AddOAISet ------------------ ok (!defined(AddOAISet), 'AddOAISet without argument is undef'); @@ -353,9 +355,11 @@ ok (!defined(AddOAISetsBiblios(my $arg=[])), 'AddOAISetsBiblios with a no HASH a ok (defined(AddOAISetsBiblios($arg={})), 'AddOAISetsBiblios with a HASH argument is def'); # Create a biblio instance for testing -my $biblionumber1 = create_helper_biblio('Moffat, Steven'); +my $biblio_1 = $builder->gimme_a_biblio({ author => 'Moffat, Steven' }); +my $biblionumber1 = $biblio_1->biblionumber; isa_ok(\$biblionumber1, 'SCALAR', '$biblionumber1 is a SCALAR'); -my $biblionumber2 = create_helper_biblio('Moffat, Steven'); +my $biblio_2 = $builder->gimme_a_biblio({ author => 'Moffat, Steven' }); +my $biblionumber2 = $biblio_2->biblionumber; isa_ok(\$biblionumber2, 'SCALAR', '$biblionumber2 is a SCALAR'); my $oai_sets_biblios = { @@ -517,7 +521,8 @@ ModOAISetMappings($setVH_id, $mappingsVH); #Create a biblio notice corresponding at one of mappings -my $biblionumberVH = create_helper_biblio('Victor Hugo'); +my $biblio_VH = $builder->gimme_a_biblio({ author => 'Victor Hugo' }); +my $biblionumberVH = $biblio_VH->biblionumber; #Update my $record = GetMarcBiblio({ biblionumber => $biblionumberVH }); @@ -572,7 +577,8 @@ ModOAISetMappings($setNotVH_id, $mappingsNotVH); #Create a biblio notice corresponding at one of mappings -my $biblionumberNotVH = create_helper_biblio('Sponge, Bob'); +my $biblio_NotVH = $builder->gimme_a_biblio({ author => 'Sponge, Bob' }); +my $biblionumberNotVH = $biblio_NotVH->biblionumber; #Update $record = GetMarcBiblio({ biblionumber => $biblionumberNotVH }); @@ -581,35 +587,4 @@ UpdateOAISetsBiblio($biblionumberNotVH, $record); my @setsNotEq = CalcOAISetsBiblio($record); is_deeply(@setsNotEq, $setNotVH_id, 'The $record only belongs to $setNotVH'); - - -# ---------- Subs -------------------------------- - - -# Helper method to set up a Biblio. -sub create_helper_biblio { - my $author = shift; - - return unless (defined($author)); - - my $marcflavour = C4::Context->preference('marcflavour'); - my $bib = MARC::Record->new(); - my $title = 'Silence in the library'; - - if ($marcflavour eq 'UNIMARC' ){ - $bib->append_fields( - MARC::Field->new('200', ' ', ' ', f => $author), - MARC::Field->new('200', ' ', ' ', a => $title), - ); - } - else{ - $bib->append_fields( - MARC::Field->new('100', ' ', ' ', a => $author), - MARC::Field->new('245', ' ', ' ', a => $title), - ); - } - my ($biblionumber)= AddBiblio($bib, ''); - return $biblionumber; -} - $schema->storage->txn_rollback; diff --git a/t/db_dependent/Serials.t b/t/db_dependent/Serials.t index 323e821f6e..125a6e5237 100755 --- a/t/db_dependent/Serials.t +++ b/t/db_dependent/Serials.t @@ -143,7 +143,8 @@ subtest 'Values should not be erased on editing' => sub { plan tests => 1; - ( $biblionumber, $biblioitemnumber ) = get_biblio(); + my $biblio = $builder->gimme_a_biblio(); + my $biblionumber = $biblio->biblionumber; my ( $icn_tag, $icn_sf ) = GetMarcFromKohaField( 'items.itemcallnumber', '' ); my ( $it_tag, $it_sf ) = GetMarcFromKohaField( 'items.itype', '' ); @@ -345,13 +346,3 @@ subtest "Do not generate an expected if one already exists" => sub { @serialsByStatus = C4::Serials::findSerialsByStatus( 1, $subscriptionid ); is( @serialsByStatus, 1, "ModSerialStatus delete corectly serial expected and not create another if exists" ); }; - -sub get_biblio { - my $bib = MARC::Record->new(); - $bib->append_fields( - MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'), - MARC::Field->new('245', ' ', ' ', a => 'Silence in the library'), - ); - my ($bibnum, $bibitemnum) = AddBiblio($bib, ''); - return ($bibnum, $bibitemnum); -} diff --git a/t/lib/TestBuilder.pm b/t/lib/TestBuilder.pm index 452d21ef92..d97243765f 100644 --- a/t/lib/TestBuilder.pm +++ b/t/lib/TestBuilder.pm @@ -3,6 +3,8 @@ package t::lib::TestBuilder; use Modern::Perl; use Koha::Database; +use C4::Biblio; +use Koha::Biblios; use Bytes::Random::Secure; use Carp; @@ -128,16 +130,31 @@ sub build { sub gimme_a_biblio { my ( $self, $args ) = @_; - my $itemtype = $args->{itemtype} || $self->build_object({ class => 'Koha::ItemTypes' })->itemtype; - my $title = $args->{title} || 'Some boring read'; + my $title = $args->{title} || 'Some boring read'; + my $author = $args->{author} || 'Some boring author'; + my $frameworkcode = $args->{frameworkcode} || ''; + my $itemtype = $args->{itemtype} + || $self->build_object( { class => 'Koha::ItemTypes' } )->itemtype; + + my $marcflavour = C4::Context->preference('marcflavour'); my $record = MARC::Record->new(); + my ( $tag, $subfield ) = $marcflavour eq 'UNIMARC' ? ( 200, 'a' ) : ( 245, 'a' ); + $record->append_fields( + MARC::Field->new( $tag, ' ', ' ', $subfield => $title ), + ); + + ( $tag, $subfield ) = $marcflavour eq 'UNIMARC' ? ( 200, 'f' ) : ( 100, 'a' ); + $record->append_fields( + MARC::Field->new( $tag, ' ', ' ', $subfield => $author ), + ); + + ( $tag, $subfield ) = $marcflavour eq 'UNIMARC' ? ( 995, 'r' ) : ( 942, 'c' ); $record->append_fields( - MARC::Field->new( '245', ' ', ' ', a => $title ), - MARC::Field->new( '942', ' ', ' ', c => $itemtype ) + MARC::Field->new( $tag, ' ', ' ', $subfield => $itemtype ) ); - my ($biblio_id) = AddBiblio( $record, '' ); + my ($biblio_id) = C4::Biblio::AddBiblio( $record, $frameworkcode ); return Koha::Biblios->find($biblio_id); } -- 2.39.5