From a40dbf5d00f1e1025c5fa7eac2a6f932ec0241e6 Mon Sep 17 00:00:00 2001
From: Tomas Cohen Arazi <tomascohen@theke.io>
Date: Thu, 9 Mar 2023 19:23:03 -0300
Subject: [PATCH] Bug 33161: Unit tests

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
---
 t/db_dependent/Koha/Item.t | 191 ++++++++++++++++++++++++++++++++++++-
 1 file changed, 190 insertions(+), 1 deletion(-)

diff --git a/t/db_dependent/Koha/Item.t b/t/db_dependent/Koha/Item.t
index 98c9844d6e2..e28ca4ad4a3 100755
--- a/t/db_dependent/Koha/Item.t
+++ b/t/db_dependent/Koha/Item.t
@@ -20,7 +20,7 @@
 use Modern::Perl;
 use utf8;
 
-use Test::More tests => 27;
+use Test::More tests => 28;
 use Test::Exception;
 use Test::MockModule;
 
@@ -1441,6 +1441,195 @@ subtest 'columns_to_str' => sub {
 
 };
 
+subtest 'strings_map() tests' => sub {
+
+    plan tests => 5;
+
+    $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("MarcSubfieldStructure-");
+
+    # Recreating subfields just to be sure tests will be ok
+    # 1 => av (LOST)
+    # 3 => no link
+    # a => branches
+    # y => itemtypes
+    Koha::MarcSubfieldStructures->search(
+        {
+            frameworkcode => '',
+            tagfield      => $itemtag,
+            tagsubfield   => [ '1', '2', '3', 'a', 'y' ],
+        }
+    )->delete;    # In case it exist already
+
+    Koha::MarcSubfieldStructure->new(
+        {
+            authorised_value => 'LOST',
+            defaultvalue     => '',
+            frameworkcode    => '',
+            kohafield        => 'items.itemlost',
+            repeatable       => 1,
+            tab              => 10,
+            tagfield         => $itemtag,
+            tagsubfield      => '1',
+        }
+    )->store;
+    Koha::MarcSubfieldStructure->new(
+        {
+            authorised_value => 'cn_source',
+            defaultvalue     => '',
+            frameworkcode    => '',
+            kohafield        => 'items.cn_source',
+            repeatable       => 1,
+            tab              => 10,
+            tagfield         => $itemtag,
+            tagsubfield      => '2',
+        }
+    )->store;
+    Koha::MarcSubfieldStructure->new(
+        {
+            authorised_value => '',
+            defaultvalue     => '',
+            frameworkcode    => '',
+            kohafield        => 'items.materials',
+            repeatable       => 1,
+            tab              => 10,
+            tagfield         => $itemtag,
+            tagsubfield      => '3',
+        }
+    )->store;
+    Koha::MarcSubfieldStructure->new(
+        {
+            authorised_value => 'branches',
+            defaultvalue     => '',
+            frameworkcode    => '',
+            kohafield        => 'items.homebranch',
+            repeatable       => 1,
+            tab              => 10,
+            tagfield         => $itemtag,
+            tagsubfield      => 'a',
+        }
+    )->store;
+    Koha::MarcSubfieldStructure->new(
+        {
+            authorised_value => 'itemtypes',
+            defaultvalue     => '',
+            frameworkcode    => '',
+            kohafield        => 'items.itype',
+            repeatable       => 1,
+            tab              => 10,
+            tagfield         => $itemtag,
+            tagsubfield      => 'y',
+        }
+    )->store;
+
+    my $itype   = $builder->build_object( { class => 'Koha::ItemTypes' } );
+    my $library = $builder->build_object( { class => 'Koha::Libraries' } );
+    my $biblio  = $builder->build_sample_biblio( { frameworkcode => '' } );
+    my $item    = $builder->build_sample_item(
+        {
+            biblionumber => $biblio->id,
+            library      => $library->id
+        }
+    );
+
+    Koha::AuthorisedValues->search( { authorised_value => 3, category => 'LOST' } )->delete;
+    my $lost_av = $builder->build_object(
+        {
+            class => 'Koha::AuthorisedValues',
+            value => {
+                authorised_value => 3,
+                category         => 'LOST',
+                lib              => 'internal description',
+                lib_opac         => 'public description',
+            }
+        }
+    );
+
+    my $class_sort_rule  = $builder->build_object( { class => 'Koha::ClassSortRules', value => { sort_routine => 'Generic' } } );
+    my $class_split_rule = $builder->build_object( { class => 'Koha::ClassSplitRules' } );
+    my $class_source     = $builder->build_object(
+        {
+            class => 'Koha::ClassSources',
+            value => {
+                class_sort_rule  => $class_sort_rule->class_sort_rule,
+                class_split_rule => $class_split_rule->class_split_rule,
+            }
+        }
+    );
+
+    $item->set(
+        {
+            cn_source => $class_source->id,
+            itemlost  => $lost_av->authorised_value,
+            itype     => $itype->itemtype,
+            materials => 'Suff',
+        }
+    )->store->discard_changes;
+
+    my $strings = $item->strings_map;
+
+    subtest 'av handling' => sub {
+
+        plan tests => 4;
+
+        ok( exists $strings->{itemlost}, "'itemlost' entry exists" );
+        is( $strings->{itemlost}->{str},      $lost_av->lib, "'str' set to av->lib" );
+        is( $strings->{itemlost}->{type},     'av',          "'type' is 'av'" );
+        is( $strings->{itemlost}->{category}, 'LOST',        "'category' exists and set to 'LOST'" );
+    };
+
+    subtest 'cn_source handling' => sub {
+
+        plan tests => 3;
+
+        ok( exists $strings->{cn_source}, "'cn_source' entry exists" );
+        is( $strings->{cn_source}->{str},  $class_source->description,    "'str' set to \$class_source->description" );
+        is( $strings->{cn_source}->{type}, 'call_number_source', "type is 'library'" );
+    };
+
+    subtest 'branches handling' => sub {
+
+        plan tests => 3;
+
+        ok( exists $strings->{homebranch}, "'homebranch' entry exists" );
+        is( $strings->{homebranch}->{str},  $library->branchname, "'str' set to 'branchname'" );
+        is( $strings->{homebranch}->{type}, 'library',            "type is 'library'" );
+    };
+
+    subtest 'itemtype handling' => sub {
+
+        plan tests => 3;
+
+        ok( exists $strings->{itype}, "'itype' entry exists" );
+        is( $strings->{itype}->{str},  $itype->description, "'str' correctly set" );
+        is( $strings->{itype}->{type}, 'item_type',         "'type' is 'item_type'" );
+    };
+
+    subtest 'public flag tests' => sub {
+
+        plan tests => 4;
+
+        $strings = $item->strings_map( { public => 1 } );
+
+        ok( exists $strings->{itemlost}, "'itemlost' entry exists" );
+        is( $strings->{itemlost}->{str},      $lost_av->lib_opac, "'str' set to av->lib" );
+        is( $strings->{itemlost}->{type},     'av',               "'type' is 'av'" );
+        is( $strings->{itemlost}->{category}, 'LOST',             "'category' exists and set to 'LOST'" );
+    };
+
+    $cache->clear_from_cache("MarcStructure-0-");
+    $cache->clear_from_cache("MarcStructure-1-");
+    $cache->clear_from_cache("MarcSubfieldStructure-");
+
+    $schema->storage->txn_rollback;
+};
+
 subtest 'store() tests' => sub {
 
     plan tests => 3;
-- 
2.34.1