Bug 31196: Remove 'default_value_for_mod_marc-' clear_from_cache calls
[koha-ffzg.git] / t / db_dependent / Koha / UI / Form / Builder / Item.t
index f2366db..caa538b 100755 (executable)
@@ -16,7 +16,8 @@
 # along with Koha; if not, see <http://www.gnu.org/licenses>.
 
 use Modern::Perl;
-use Test::More tests => 7;
+use Test::More tests => 9;
+use Data::Dumper qw( Dumper );
 use utf8;
 
 use List::MoreUtils qw( uniq );
@@ -25,6 +26,7 @@ use Koha::Libraries;
 use Koha::MarcSubfieldStructures;
 use Koha::UI::Form::Builder::Item;
 use t::lib::TestBuilder;
+use t::lib::Mocks;
 
 my $schema = Koha::Database->new->schema;
 $schema->storage->txn_begin;
@@ -34,7 +36,6 @@ my $builder = t::lib::TestBuilder->new;
 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-");
 
 # 952 $x $é are not linked with a kohafield
@@ -44,6 +45,16 @@ $cache->clear_from_cache("MarcSubfieldStructure-");
 # 952$t is linked with items.copynumber and is not repeatable
 setup_mss();
 
+# FIXME Later in this script we are comparing itemtypes, ordered by their description.
+# MySQL and Perl don't sort _ identically.
+# If you have one itemtype BK and another one B_K, MySQL will sort B_K first when Perl will sort it last
+my @itemtypes = Koha::ItemTypes->search->as_list;
+for my $itemtype ( @itemtypes ) {
+    my $d = $itemtype->description;
+    $d =~ s|_||g;
+    $itemtype->description($d)->store;
+}
+
 subtest 'authorised values' => sub {
     #plan tests => 1;
 
@@ -110,28 +121,30 @@ subtest 'authorised values' => sub {
     subtest 'itemtypes' => sub {
         plan tests => 2;
         my ($subfield) = grep { $_->{kohafield} eq 'items.itype' } @$subfields;
-        my $itemtypes = Koha::ItemTypes->search;
-
+        my @itemtypes = Koha::ItemTypes->search->as_list;
+
+        my $expected = [
+            "",
+            map    { $_->itemtype }
+              # We need to sort using uc or perl won't be case insensitive
+              sort { uc($a->translated_description) cmp uc($b->translated_description) }
+              @itemtypes
+        ];
         is_deeply(
             $subfield->{marc_value}->{values},
-            [
-                "",
-                map    { $_->itemtype }
-                  # We need to sort using uc or perl won't be case insensitive
-                  sort { uc($a->translated_description) cmp uc($b->translated_description) }
-                  $itemtypes->as_list
-            ],
-            "Item types should be sorted by description and an empty entries should be shown"
+            $expected,
+            "Item types should be sorted by description and an empty entry should be shown"
         );
+
         is_deeply( $subfield->{marc_value}->{labels},
-            { map { $_->itemtype => $_->description } $itemtypes->as_list },
+            { map { $_->itemtype => $_->description } @itemtypes},
             'Labels should be correctly displayed'
         );
     };
 };
 
 subtest 'prefill_with_default_values' => sub {
-    plan tests => 2;
+    plan tests => 3;
 
     my $biblio = $builder->build_sample_biblio({ value => {frameworkcode => ''}});
     my $subfields =
@@ -150,6 +163,14 @@ subtest 'prefill_with_default_values' => sub {
     ($subfield) = grep { $_->{subfield} eq 'é' } @$subfields;
     is( $subfield->{marc_value}->{value}, 'ééé', 'default value should be set if prefill_with_default_values passed');
 
+    # Do the same for an existing item; we do not expect the defaultvalue to popup
+    my $item = $builder->build_sample_item;
+    $subfields = Koha::UI::Form::Builder::Item->new({
+        biblionumber => $biblio->biblionumber,
+        item => $item->unblessed,
+    })->edit_form({ prefill_with_default_values => 1 });
+    ($subfield) = grep { $_->{subfield} eq 'é' } @$subfields;
+    is( $subfield->{marc_value}->{value}, q{}, 'default value not applied to existing item');
 
 };
 
@@ -277,10 +298,57 @@ subtest 'subfields_to_allow & ignore_not_allowed_subfields' => sub {
     is( $subfield, undef, "subfield that is not in the allow list is not returned" );
 };
 
+subtest 'ignore_invisible_subfields' => sub {
+    plan tests => 2;
+
+    my $biblio =
+      $builder->build_sample_biblio( { value => { frameworkcode => '' } } );
+    my $item = $builder->build_sample_item(
+        {
+            issues => 42,
+        }
+    );
+
+    # items.issues is mapped with 952$l
+    my $subfields = Koha::UI::Form::Builder::Item->new(
+        {
+            biblionumber => $biblio->biblionumber,
+            item         => $item->unblessed,
+        }
+    )->edit_form;
+    ( my $subfield ) = grep { $_->{subfield} eq 'l' } @$subfields;
+    is( $subfield->{marc_value}->{value}, 42, 'items.issues copied' );
+
+    $subfields = Koha::UI::Form::Builder::Item->new(
+        {
+            biblionumber => $biblio->biblionumber,
+            item         => $item->unblessed,
+        }
+    )->edit_form(
+        {
+            ignore_invisible_subfields => 1
+        }
+    );
+    ($subfield) = grep { $_->{subfield} eq 'l' } @$subfields;
+    is( $subfield->{marc_value}->{value},
+        undef, 'items.issues not copied if ignore_invisible_subfields is passed' );
+};
+
+subtest 'Fix subfill_with_default_values - no biblionumber passed' => sub {
+    plan tests => 1;
+
+    t::lib::Mocks::mock_preference('itemcallnumber', '082ab,092ab');
+    my $item = $builder->build_sample_item;
+    my $subfields = Koha::UI::Form::Builder::Item->new(
+        {
+            item         => $item->unblessed,
+        }
+    )->edit_form({ prefill_with_default_values => 1 });
+    pass();
+};
 
 $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-");
 
 sub setup_mss {
@@ -335,6 +403,14 @@ sub setup_mss {
         {
             frameworkcode => '',
             tagfield => $itemtag,
+            tagsubfield => ['l'],
+        }
+    )->update( { hidden => -4 } );
+
+    Koha::MarcSubfieldStructures->search(
+        {
+            frameworkcode => '',
+            tagfield => $itemtag,
             tagsubfield => ['z'],
         }
     )->update( { kohafield => 'items.itemnotes', repeatable => 1 } );