From 28659382b0abd65e499c1d11a5f57f1677492ed3 Mon Sep 17 00:00:00 2001 From: Matt Blenkinsop Date: Thu, 4 Jul 2024 10:14:21 +0000 Subject: [PATCH] Bug 37115: Add unit tests prove t/db_dependent/Koha/Item.t prove t/db_dependent/Koha/BackgroundJobs/BatchDeleteItem.t --- .../Koha/BackgroundJobs/BatchDeleteItem.t | 43 ++++++++++++++- t/db_dependent/Koha/Item.t | 52 ++++++++++++++++++- 2 files changed, 93 insertions(+), 2 deletions(-) diff --git a/t/db_dependent/Koha/BackgroundJobs/BatchDeleteItem.t b/t/db_dependent/Koha/BackgroundJobs/BatchDeleteItem.t index ee40088c8c2..49bc760c7c6 100755 --- a/t/db_dependent/Koha/BackgroundJobs/BatchDeleteItem.t +++ b/t/db_dependent/Koha/BackgroundJobs/BatchDeleteItem.t @@ -24,6 +24,8 @@ use JSON qw( encode_json ); use Koha::Database; use Koha::BackgroundJob::BatchDeleteItem; +use Koha::Serial; +use Koha::Serial::Items; use t::lib::Mocks; use t::lib::TestBuilder; @@ -33,7 +35,7 @@ my $builder = t::lib::TestBuilder->new; subtest "process() tests" => sub { - plan tests => 2; + plan tests => 4; $schema->storage->txn_begin; @@ -99,5 +101,44 @@ subtest "process() tests" => sub { is( $counter, 1, 'Counter untouched with RealTimeHoldsQueue disabled' ); + $biblio = $builder->build_sample_biblio; + my $item_with_serial = $builder->build_sample_item( { biblionumber => $biblio->id } ); + my $serial = + $builder->build_object( { class => 'Koha::Serials', value => { biblionumber => $biblio->biblionumber } } ); + my $serial_item = $builder->build_object( + { + class => 'Koha::Serial::Items', + value => { itemnumber => $item_with_serial->itemnumber, serialid => $serial->serialid } + } + ); + + $job = Koha::BackgroundJob::BatchDeleteItem->new( + { + status => 'new', + size => 2, + borrowernumber => undef, + type => 'batch_item_record_deletion', + data => encode_json { + record_ids => [ $item_with_serial->id ], + delete_biblios => 1, + delete_serial_issues => 1, + } + } + ); + + $job->process( + { + record_ids => [ $item_with_serial->id ], + delete_biblios => 1, + delete_serial_issues => 1, + } + ); + + my $serial_check = Koha::Serials->find( $serial->serialid ); + my $serial_item_check = Koha::Serial::Items->find( $serial_item->itemnumber ); + + is( $serial_check, undef, 'Serial deleted' ); + is( $serial_item_check, undef, 'Serial item deleted' ); + $schema->storage->txn_rollback; }; diff --git a/t/db_dependent/Koha/Item.t b/t/db_dependent/Koha/Item.t index ab6d01c5d71..1d5cbb9780e 100755 --- a/t/db_dependent/Koha/Item.t +++ b/t/db_dependent/Koha/Item.t @@ -955,7 +955,7 @@ subtest 'store check barcodes' => sub { }; subtest 'deletion' => sub { - plan tests => 15; + plan tests => 16; $schema->storage->txn_begin; @@ -1131,6 +1131,56 @@ subtest 'deletion' => sub { $schema->storage->txn_rollback; }; + subtest 'serial issues tests' => sub { + plan tests => 4; + + t::lib::Mocks::mock_preference( 'IndependentBranches', 0 ); + + $schema->storage->txn_begin; + + my $biblio = $builder->build_sample_biblio; + my $item_with_serial = $builder->build_sample_item( { biblionumber => $biblio->id } ); + my $serial = + $builder->build_object( { class => 'Koha::Serials', value => { biblionumber => $biblio->biblionumber } } ); + my $serial_item = $builder->build_object( + { + class => 'Koha::Serial::Items', + value => { itemnumber => $item_with_serial->itemnumber, serialid => $serial->serialid } + } + ); + + # Check serial issue is not deleted unless argument passed + $item_with_serial->safe_delete( { delete_serial_issues => 0 } ); + + my $serial_check = Koha::Serials->find( $serial->serialid ); + my $serial_item_check = Koha::Serial::Items->find( $serial_item->itemnumber ); + + is( $serial_check->serialid, $serial->serialid, 'Serial not deleted as the argument was not passed' ); + is( $serial_item_check, undef, 'Serial item deleted by cascade' ); + + $biblio = $builder->build_sample_biblio; + $item_with_serial = $builder->build_sample_item( { biblionumber => $biblio->id } ); + $serial = + $builder->build_object( { class => 'Koha::Serials', value => { biblionumber => $biblio->biblionumber } } ); + $serial_item = $builder->build_object( + { + class => 'Koha::Serial::Items', + value => { itemnumber => $item_with_serial->itemnumber, serialid => $serial->serialid } + } + ); + + # Check serial issue is deleted when argument passed + $item_with_serial->safe_delete( { delete_serial_issues => 1 } ); + + $serial_check = Koha::Serials->find( $serial->serialid ); + $serial_item_check = Koha::Serial::Items->find( $serial_item->itemnumber ); + + is( $serial_check, undef, 'Serial deleted' ); + is( $serial_item_check, undef, 'Serial item deleted by cascade' ); + + $schema->storage->txn_rollback; + }; + $schema->storage->txn_rollback; }; -- 2.39.3 (Apple Git-146)