From de18c3c01b57a5941d5541dfbb964b1904216127 Mon Sep 17 00:00:00 2001 From: Baptiste Wojtkowski Date: Fri, 4 Oct 2024 14:22:05 +0200 Subject: [PATCH] Bug 38093: Add unit tests --- t/db_dependent/Koha/Biblio.t | 115 +++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) diff --git a/t/db_dependent/Koha/Biblio.t b/t/db_dependent/Koha/Biblio.t index 6af083b2..e08ec026 100755 --- a/t/db_dependent/Koha/Biblio.t +++ b/t/db_dependent/Koha/Biblio.t @@ -1062,6 +1062,121 @@ subtest 'link_marc_host' => sub { $schema->storage->txn_rollback; }; +subtest 'link_marc_bundled_item' => sub { + plan tests => 4; + $schema->storage->txn_begin; + + my $child = $builder->build_sample_item(); + + my $host = $builder->build_sample_biblio(); + my $host_record = $host->metadata->record; + + is( $host_record->field('774'), undef, "774 field is undefined before link_marc_bundled_item" ); + $host->link_marc_bundled_item( { bundled_item => $child->itemnumber } ); + $host->discard_changes; + $host_record = $host->metadata->record; + is( + ref( $host_record->field('774') ), 'MARC::Field', + '774 field is set after calling link_marc_bundled_item({ bundled_item => $itemnumber })' + ); + + $host = $builder->build_sample_biblio(); + $host_record = $host->metadata->record; + is( $host_record->field('774'), undef, "774 field is undefined before link_marc_bundled_item" ); + $host->link_marc_bundled_item( { bundled_item => $child } ); + $host->discard_changes; + $host_record = $host->metadata->record; + is( + ref( $host_record->field('774') ), 'MARC::Field', + '774 field is set after calling link_marc_bundled_item({ bundled_item => $biblio })' + ); + + $schema->storage->txn_rollback; +}; + +subtest 'unlink_marc_bundled_item' => sub { + plan tests => 8; + $schema->storage->txn_begin; + my $child = $builder->build_sample_item(); + my $wrongchild = $builder->build_sample_item(); + my $host = $builder->build_sample_biblio(); + my $host_record = $host->metadata->record; + + # Prepare the environment + $host->link_marc_bundled_item( { bundled_item => $child->itemnumber } ); + $host->discard_changes; + $host_record = $host->metadata->record; + is( + ref( $host_record->field('774') ), 'MARC::Field', + '774 field is set before calling unlink_marc_bundled_item({ bundled_item => $itemnumber })' + ); + + # Test + $host->unlink_marc_bundled_item( { bundled_item => $child->itemnumber } ); + $host->discard_changes; + $host_record = $host->metadata->record; + is( + $host_record->field('774'), undef, + 'Can remove a link to a bundle using a itemnumber to identify the bundle' + ); + + # Prepare the environment + $host->link_marc_bundled_item( { bundled_item => $child->itemnumber } ); + $host->discard_changes; + $host_record = $host->metadata->record; + is( + ref( $host_record->field('774') ), 'MARC::Field', + '774 field is set before calling unlink_marc_bundled_item({ bundled_item => $itemnumber })' + ); + + # Test + $host->unlink_marc_bundled_item( { bundled_item => $child } ); + $host->discard_changes; + $host_record = $host->metadata->record; + is( + $host_record->field('774'), undef, + 'Can remove a link to a bundle using a record to identify the bundle' + ); + + # Prepare the environment + $host->link_marc_bundled_item( { bundled_item => $child->itemnumber } ); + $host->discard_changes; + $host_record = $host->metadata->record; + is( + ref( $host_record->field('774') ), 'MARC::Field', + '774 field is set before calling unlink_marc_bundled_item({ bundled_item => $itemnumber })' + ); + + # Test + $host->unlink_marc_bundled_item( { bundled_item => $wrongchild->itemnumber } ); + $host->discard_changes; + $host_record = $host->metadata->record; + is( + ref( $host_record->field('774') ), 'MARC::Field', + 'Removing a link from another bundle does not remove the field' + ); + + # Prepare the environment + $host->link_marc_bundled_item( { bundled_item => $child->itemnumber } ); + $host->discard_changes; + $host_record = $host->metadata->record; + is( + ref( $host_record->field('774') ), 'MARC::Field', + '774 field is set before calling unlink_marc_bundled_item({ bundled_item => $itemnumber })' + ); + + # Test + $host->unlink_marc_bundled_item( { bundled_item => $wrongchild } ); + $host->discard_changes; + $host_record = $host->metadata->record; + is( + ref( $host_record->field('774') ), 'MARC::Field', + 'Removing a link from another bundle does not remove the field' + ); + + $schema->storage->txn_rollback; +}; + subtest '->orders, ->uncancelled_orders and ->acq_status tests' => sub { plan tests => 9; -- 2.30.2