From 95dad5ad4528ca5664e00bb9e0d3f88366a2b7f8 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Mon, 2 Aug 2021 14:47:09 +0200 Subject: [PATCH] Bug 27526: Add tests for columns_to_str and host_items --- Koha/Item.pm | 8 +-- t/db_dependent/Koha/Biblio.t | 39 +++++++++++++- t/db_dependent/Koha/Item.t | 100 ++++++++++++++++++++++++++++++++++- 3 files changed, 139 insertions(+), 8 deletions(-) diff --git a/Koha/Item.pm b/Koha/Item.pm index c2582598049..3cce38faa2c 100644 --- a/Koha/Item.pm +++ b/Koha/Item.pm @@ -965,12 +965,8 @@ sub columns_to_str { next if $column eq 'more_subfields_xml'; - my $value; - if ( Koha::Object::_datetime_column_type( $columns_info->{$column}->{data_type} ) ) { - $value = output_pref({ dateformat => 'rfc3339', dt => dt_from_string($value, 'sql')}); - } else { - $value = $self->$column; - } + my $value = $self->$column; + # Maybe we need to deal with datetime columns here, but so far we have damaged_on, itemlost_on and withdrawn_on, and they are not linked with kohafield if ( not defined $value or $value eq "" ) { $values->{$column} = $value; diff --git a/t/db_dependent/Koha/Biblio.t b/t/db_dependent/Koha/Biblio.t index 83de0634c6b..cdb69615a17 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 => 14; +use Test::More tests => 15; use C4::Biblio qw( AddBiblio ModBiblio ); use Koha::Database; @@ -637,3 +637,40 @@ subtest 'get_marc_notes() UNIMARC tests' => sub { $schema->storage->txn_rollback; }; + +subtest 'host_items' => sub { + plan tests => 6; + + my $biblio = $builder->build_sample_biblio( { frameworkcode => '' } ); + + t::lib::Mocks::mock_preference( 'EasyAnalyticalRecords', 1 ); + my $host_items = $biblio->host_items; + is( ref($host_items), 'Koha::Items' ); + is( $host_items->count, 0 ); + + my $item_1 = + $builder->build_sample_item( { biblionumber => $biblio->biblionumber } ); + my $host_item_1 = $builder->build_sample_item; + my $host_item_2 = $builder->build_sample_item; + + my $record = $biblio->metadata->record; + $record->append_fields( + MARC::Field->new( + '773', '', '', + 9 => $host_item_1->itemnumber, + 9 => $host_item_2->itemnumber + ), + ); + C4::Biblio::ModBiblio( $record, $biblio->biblionumber ); + $biblio = $biblio->get_from_storage; + $host_items = $biblio->host_items; + is( $host_items->count, 2 ); + is_deeply( [ $host_items->get_column('itemnumber') ], + [ $host_item_1->itemnumber, $host_item_2->itemnumber ] ); + + t::lib::Mocks::mock_preference( 'EasyAnalyticalRecords', 0 ); + $host_items = $biblio->host_items; + is( ref($host_items), 'Koha::Items' ); + is( $host_items->count, 0 ); + +}; diff --git a/t/db_dependent/Koha/Item.t b/t/db_dependent/Koha/Item.t index e9cc993cd75..fe2289f7682 100755 --- a/t/db_dependent/Koha/Item.t +++ b/t/db_dependent/Koha/Item.t @@ -18,13 +18,15 @@ # along with Koha; if not, see . use Modern::Perl; +use utf8; -use Test::More tests => 12; +use Test::More tests => 13; use Test::Exception; use C4::Biblio qw( GetMarcSubfieldStructure ); use C4::Circulation qw( AddIssue AddReturn ); +use Koha::Caches; use Koha::Items; use Koha::Database; use Koha::DateUtils; @@ -1050,3 +1052,99 @@ subtest 'move_to_biblio() tests' => sub { $schema->storage->txn_rollback; }; + +subtest 'columns_to_str' => sub { + plan tests => 4; + + $schema->storage->txn_begin; + + my ( $itemtag, $itemsubfield ) = C4::Biblio::GetMarcFromKohaField( "items.itemnumber" ); + + my $cache = Koha::Caches->get_instance(); + $cache->clear_from_cache("MarcStructure-0-"); + $cache->clear_from_cache("MarcStructure-1-"); + $cache->clear_from_cache("default_value_for_mod_marc-"); + $cache->clear_from_cache("MarcSubfieldStructure-"); + + # Creating subfields 'é', 'è' that are not linked with a kohafield + Koha::MarcSubfieldStructures->search( + { + frameworkcode => '', + tagfield => $itemtag, + tagsubfield => ['é', 'è'], + } + )->delete; # In case it exist already + + # é is not linked with a AV + # è is linked with AV branches + Koha::MarcSubfieldStructure->new( + { + frameworkcode => '', + tagfield => $itemtag, + tagsubfield => 'é', + kohafield => undef, + repeatable => 1, + defaultvalue => 'ééé', + tab => 10, + } + )->store; + Koha::MarcSubfieldStructure->new( + { + frameworkcode => '', + tagfield => $itemtag, + tagsubfield => 'è', + kohafield => undef, + repeatable => 1, + defaultvalue => 'èèè', + tab => 10, + authorised_value => 'branches', + } + )->store; + + my $biblio = $builder->build_sample_biblio({ frameworkcode => '' }); + my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber }); + my $itemlost = Koha::AuthorisedValues->search({ category => 'LOST' })->next->authorised_value; + my $dateaccessioned = '2020-12-15'; + my $library = Koha::Libraries->search->next; + my $branchcode = $library->branchcode; + + my $some_marc_xml = qq{ + + + + a + + value é + $branchcode + + + +}; + + $item->update( + { + itemlost => $itemlost, + dateaccessioned => $dateaccessioned, + more_subfields_xml => $some_marc_xml, + } + ); + + $item = $item->get_from_storage; + + my $s = $item->columns_to_str; + is( $s->{itemlost}, 'Lost' ); + is( $s->{dateaccessioned}, '2020-12-15'); + is( $s->{é}, 'value é'); + is( $s->{è}, $library->branchname ); + + $cache->clear_from_cache("MarcStructure-0-"); + $cache->clear_from_cache("MarcStructure-1-"); + $cache->clear_from_cache("default_value_for_mod_marc-"); + $cache->clear_from_cache("MarcSubfieldStructure-"); + + $schema->storage->txn_rollback; + +}; -- 2.25.1