From 21211daeb824c334a0e898705e539320f7be1487 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Mon, 14 Oct 2019 08:32:58 -0300 Subject: [PATCH] Bug 23807: Unit tests This patch introduces tests for the Koha::Item->as_marc_field method. It tries to cover all the use cases. To test: 1. Apply this patchset 2. Run: $ kshell k$ prove t/db_dependent/Koha/Item.t => SUCCESS; Tests pass! 3. Sign off :-D Signed-off-by: Jan Kolator Signed-off-by: Josef Moravec --- t/db_dependent/Koha/Item.t | 68 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/t/db_dependent/Koha/Item.t b/t/db_dependent/Koha/Item.t index a4bb012756..b9ff871097 100644 --- a/t/db_dependent/Koha/Item.t +++ b/t/db_dependent/Koha/Item.t @@ -19,7 +19,9 @@ use Modern::Perl; -use Test::More tests => 2; +use Test::More tests => 3; + +use C4::Biblio; use Koha::Items; use Koha::Database; @@ -81,4 +83,68 @@ subtest 'has_pending_hold() tests' => sub { t::lib::Mocks::mock_preference( 'AllowItemsOnHoldCheckout', 0 ); $dbh->do("DELETE FROM tmp_holdsqueue WHERE itemnumber=$itemnumber"); ok( !$item->has_pending_hold, "We don't have a pending hold if nothing in the tmp_holdsqueue"); + + $schema->storage->txn_rollback; +}; + +subtest "as_marc_field() tests" => sub { + + my $mss = C4::Biblio::GetMarcSubfieldStructure( '', { unsafe => 1 } ); + + my @schema_columns = $schema->resultset('Item')->result_source->columns; + my @mapped_columns = grep { exists $mss->{'items.'.$_} } @schema_columns; + + plan tests => 2 * (scalar @mapped_columns + 1) + 1; + + $schema->storage->txn_begin; + + my $item = $builder->build_sample_item; + + # Tests with the mss parameter + my $marc_field = $item->as_marc_field({ mss => $mss }); + + is( + $marc_field->tag, + $mss->{'items.itemnumber'}[0]->{tagfield}, + 'Generated field set the right tag number' + ); + + foreach my $column ( @mapped_columns ) { + my $tagsubfield = $mss->{ 'items.' . $column }[0]->{tagsubfield}; + is( $marc_field->subfield($tagsubfield), + $item->$column, "Value is mapped correctly for column $column" ); + } + + # Tests without the mss parameter + $marc_field = $item->as_marc_field(); + + is( + $marc_field->tag, + $mss->{'items.itemnumber'}[0]->{tagfield}, + 'Generated field set the right tag number' + ); + + foreach my $column (@mapped_columns) { + my $tagsubfield = $mss->{ 'items.' . $column }[0]->{tagsubfield}; + is( $marc_field->subfield($tagsubfield), + $item->$column, "Value is mapped correctly for column $column" ); + } + + my $unmapped_subfield = Koha::MarcSubfieldStructure->new( + { + frameworkcode => '', + tagfield => $mss->{'items.itemnumber'}[0]->{tagfield}, + tagsubfield => 'X', + } + )->store; + + $mss = C4::Biblio::GetMarcSubfieldStructure( '', { unsafe => 0 } ); + my @unlinked_subfields; + push @unlinked_subfields, X => 'Something weird'; + $item->more_subfields_xml( C4::Items::_get_unlinked_subfields_xml( \@unlinked_subfields ) )->store; + + $marc_field = $item->as_marc_field; + is( scalar $marc_field->subfield('X'), 'Something weird', 'more_subfield_xml is considered' ); + + $schema->storage->txn_rollback; }; -- 2.11.0