From 9dfefd082c52395396285a1b187ef0fb3ffedc17 Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Wed, 4 Jul 2018 01:26:02 +0000 Subject: [PATCH] Bug 20664: Unit tests for GetMarcItemFields To test: prove -v t/db_dependent/Items.t --- t/db_dependent/Items.t | 99 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 96 insertions(+), 3 deletions(-) diff --git a/t/db_dependent/Items.t b/t/db_dependent/Items.t index 96dc4e0..08ecbd5 100755 --- a/t/db_dependent/Items.t +++ b/t/db_dependent/Items.t @@ -34,7 +34,7 @@ use C4::Items; use t::lib::Mocks; use t::lib::TestBuilder; -use Test::More tests => 13; +use Test::More tests => 14; use Test::Warn; @@ -862,12 +862,11 @@ subtest 'tests for GetMarcItem' => sub { cn_sort => "", } }); - my ($itemtag, $item_num_subfield )=C4::Biblio::GetMarcFromKohaField("items.itemnumber"); my $get_itemnumber = $item1->itemnumber; my $item1_marc = C4::Items::GetMarcItem( $biblio->{biblionumber}, $item1->itemnumber ); my (undef, undef, $itemnumber2 ) = AddItemFromMarc( $item1_marc, $biblio->{biblionumber} ); my $item2_marc = C4::Items::GetMarcItem( $biblio->{biblionumber}, $itemnumber2 ); - ($itemtag, $item_num_subfield )=C4::Biblio::GetMarcFromKohaField("items.itemnumber"); #get itemnumber tag + my ($itemtag, $item_num_subfield )=C4::Biblio::GetMarcFromKohaField("items.itemnumber"); #get itemnumber tag $item1_marc->field($itemtag)->delete_subfield(code => $item_num_subfield); #and remove it $item2_marc->field($itemtag)->delete_subfield(code => $item_num_subfield); #because it won't match is_deeply( $item1_marc, $item2_marc, "The Marc should match if the items are the same"); @@ -875,6 +874,100 @@ subtest 'tests for GetMarcItem' => sub { }; +subtest 'tests for GetMarcItemFields' => sub { + + plan tests => 5; + + $schema->storage->txn_begin; + #Setup the information we need + my $builder = t::lib::TestBuilder->new; + my $library = $builder->build({ source => 'Branch', }); + my $itemtype = $builder->build({ source => 'Itemtype', }); + my $biblio = $builder->build({ + source => 'Biblio', + value=>{ + frameworkcode => "", + } + }); + my $biblioitem = $builder->build({ + source => 'Biblioitem', + value => { biblionumber => $biblio->{biblionumber} }, + }); + my @items; + my $item1 = $builder->build_object({ + class => 'Koha::Items', + value => { + biblionumber => $biblio->{biblionumber}, + biblioitemnumber => $biblioitem->{biblioitemnumber}, + itype => $itemtype->{itemtype}, + homebranch => $library->{branchcode}, + barcode => undef, + restricted => 1, + itemcallnumber => "", + cn_sort => "", + withdrawn => 0, + } + }); + push @items, GetItem( $item1->itemnumber ); + my $item2 = $builder->build_object({ + class => 'Koha::Items', + value => { + biblionumber => $biblio->{biblionumber}, + biblioitemnumber => $biblioitem->{biblioitemnumber}, + itype => $itemtype->{itemtype}, + homebranch => $library->{branchcode}, + barcode => undef, + restricted => 1, + itemcallnumber => "", + cn_sort => "", + withdrawn => 0, + } + }); + push @items, GetItem( $item2->itemnumber ); + my $item3 = $builder->build_object({ + class => 'Koha::Items', + value => { + biblionumber => $biblio->{biblionumber}, + biblioitemnumber => $biblioitem->{biblioitemnumber}, + itype => $itemtype->{itemtype}, + homebranch => $library->{branchcode}, + barcode => undef, + restricted => 1, + itemcallnumber => "", + cn_sort => "", + withdrawn => 1, + } + }); + push @items, GetItem( $item3->itemnumber ); + + #Get the marc for our items individually for comparison later + my $item1_marc = C4::Items::GetMarcItem( $biblio->{biblionumber}, $item1->itemnumber ); + my $item2_marc = C4::Items::GetMarcItem( $biblio->{biblionumber}, $item2->itemnumber ); + my $item3_marc = C4::Items::GetMarcItem( $biblio->{biblionumber}, $item3->itemnumber ); + my ($itemtag, $item_num_subfield )=C4::Biblio::GetMarcFromKohaField("items.itemnumber"); #get itemnumber tag + $item1_marc->field($itemtag)->delete_subfield(code => $item_num_subfield); #and remove it + $item2_marc->field($itemtag)->delete_subfield(code => $item_num_subfield); #because it won't match + + #Testing with hidden items + my $opachiddenitems = " + withdrawn: [1]"; + my $hidingrules = YAML::Load($opachiddenitems); + my $marc_items = C4::Items::GetMarcItemFields( $biblio->{biblionumber}, [], $hidingrules ); + @$marc_items[0]->delete_subfield(code => $item_num_subfield); + @$marc_items[1]->delete_subfield(code => $item_num_subfield); + is(scalar @$marc_items, 2, "We should not get a hidden item"); + is_deeply( $item1_marc->field($itemtag), @$marc_items[0], "We should get the first item"); + is_deeply( $item2_marc->field($itemtag), @$marc_items[1], "We should also get the second item"); + + #Testing with specified items + $marc_items = C4::Items::GetMarcItemFields( $biblio->{biblionumber}, [ $item3->itemnumber ], undef ); + is(scalar @$marc_items,1,"We should only get the specifically requested item"); + is_deeply( $item3_marc->field($itemtag), @$marc_items[0], "We should get the third item when explicitly requested"); + + $schema->storage->txn_rollback; + +}; + # Helper method to set up a Biblio. sub get_biblio { my ( $frameworkcode ) = @_; -- 2.7.4