From dfe84dd925034f6f2d4ec174e191094f644d99ed Mon Sep 17 00:00:00 2001
From: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Date: Mon, 2 Aug 2021 14:47:09 +0200
Subject: [PATCH] Bug 27526: Add tests for columns_to_str and host_items

Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
---
 Koha/Item.pm                 |   8 +--
 t/db_dependent/Koha/Biblio.t |  42 ++++++++++++++-
 t/db_dependent/Koha/Item.t   | 100 ++++++++++++++++++++++++++++++++++-
 3 files changed, 142 insertions(+), 8 deletions(-)

diff --git a/Koha/Item.pm b/Koha/Item.pm
index 344646a839..4d01f26491 100644
--- a/Koha/Item.pm
+++ b/Koha/Item.pm
@@ -964,12 +964,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 83de0634c6..34103f4294 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,43 @@ subtest 'get_marc_notes() UNIMARC tests' => sub {
 
     $schema->storage->txn_rollback;
 };
+
+subtest 'host_items' => sub {
+    plan tests => 6;
+
+    $schema->storage->txn_begin;
+
+    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 );
+
+    $schema->storage->txn_rollback;
+};
diff --git a/t/db_dependent/Koha/Item.t b/t/db_dependent/Koha/Item.t
index 1ac523c1a0..d76ed46e48 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 <http://www.gnu.org/licenses>.
 
 use Modern::Perl;
+use utf8;
 
-use Test::More tests => 9;
+use Test::More tests => 10;
 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;
@@ -816,3 +818,99 @@ subtest 'get_transfers' => 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{<?xml version="1.0" encoding="UTF-8"?>
+<collection
+  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd"
+  xmlns="http://www.loc.gov/MARC21/slim">
+
+<record>
+  <leader>         a              </leader>
+  <datafield tag="999" ind1=" " ind2=" ">
+    <subfield code="é">value é</subfield>
+    <subfield code="è">$branchcode</subfield>
+  </datafield>
+</record>
+
+</collection>};
+
+    $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.20.1