From ba515499cf678679dac41632adc90f5370765f07 Mon Sep 17 00:00:00 2001 From: Baptiste Wojtkowski Date: Tue, 24 Sep 2024 11:51:20 +0200 Subject: [PATCH] Bug 37710: Add unit tests --- t/db_dependent/Koha/Biblio.t | 104 ++++++++++++++++++++++++++++++++++- 1 file changed, 103 insertions(+), 1 deletion(-) diff --git a/t/db_dependent/Koha/Biblio.t b/t/db_dependent/Koha/Biblio.t index 6b0da1f9..06ea4fe5 100755 --- a/t/db_dependent/Koha/Biblio.t +++ b/t/db_dependent/Koha/Biblio.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 39; +use Test::More tests => 40; use Test::Exception; use Test::Warn; @@ -1071,6 +1071,108 @@ subtest 'link_marc_host' => sub { $schema->storage->txn_rollback; }; +subtest 'unlink_marc_host' => sub { + plan tests => 9; + $schema->storage->txn_begin; + my $host = $builder->build_sample_biblio(); + my $wronghost = $builder->build_sample_biblio(); + my $child = $builder->build_sample_biblio(); + my $child_record = $child->metadata->record; + + # Prepare the environment + $child->link_marc_host( { host => $host->biblionumber } ); + $child->discard_changes; + $child_record = $child->metadata->record; + is( + ref( $child_record->field('773') ), 'MARC::Field', + '773 field is set before calling unlink_marc_host({ host => $biblionumber })' + ); + + # Test + $child->unlink_marc_host( { host => $host->biblionumber } ); + $child->discard_changes; + $child_record = $child->metadata->record; + is( + $child_record->field('773'), undef, + 'Can remove a link to a bundle using a biblionumber to identify the bundle' + ); + + # Prepare the environment + $child->link_marc_host( { host => $host->biblionumber } ); + $child->discard_changes; + $child_record = $child->metadata->record; + is( + ref( $child_record->field('773') ), 'MARC::Field', + '773 field is set before calling unlink_marc_host({ host => $biblionumber })' + ); + + # Test + $child->unlink_marc_host( { host => $host } ); + $child->discard_changes; + $child_record = $child->metadata->record; + is( + $child_record->field('773'), undef, + 'Can remove a link to a bundle using a record to identify the bundle' + ); + + # Prepare the environment + $child->link_marc_host( { host => $host->biblionumber } ); + $child->discard_changes; + $child_record = $child->metadata->record; + is( + ref( $child_record->field('773') ), 'MARC::Field', + '773 field is set before calling unlink_marc_host({ host => $biblionumber })' + ); + + # Test + $child->unlink_marc_host( { host => $wronghost->biblionumber } ); + $child->discard_changes; + $child_record = $child->metadata->record; + is( + ref( $child_record->field('773') ), 'MARC::Field', + 'Removing a link from another bundle does not remove the field' + ); + + # Prepare the environment + $child->link_marc_host( { host => $host->biblionumber } ); + $child->discard_changes; + $child_record = $child->metadata->record; + is( + ref( $child_record->field('773') ), 'MARC::Field', + '773 field is set before calling unlink_marc_host({ host => $biblionumber })' + ); + + # Test + $child->unlink_marc_host( { host => $wronghost } ); + $child->discard_changes; + $child_record = $child->metadata->record; + is( + ref( $child_record->field('773') ), 'MARC::Field', + 'Removing a link from another bundle does not remove the field' + ); + + # Prepare the environment + $child->link_marc_host( { host => $host->biblionumber } ); + $child->link_marc_host( { host => $host->biblionumber } ); + $child->discard_changes; + $child_record = $child->metadata->record; + my @fields = $child_record->field('773'); + my $fields_before = scalar @fields; + + # Test + $child->unlink_marc_host( { host => $host } ); + $child->discard_changes; + $child_record = $child->metadata->record; + @fields = $child_record->field('773'); + my $fields_after = scalar @fields; + is( + $fields_after, $fields_before - 1, + 'Removing a link only removes one link' + ); + + $schema->storage->txn_rollback; +}; + subtest '->orders, ->uncancelled_orders and ->acq_status tests' => sub { plan tests => 9; -- 2.30.2