From c2025d599710ca860a37cb7145e2854b9328faa0 Mon Sep 17 00:00:00 2001
From: Tomas Cohen Arazi <tomascohen@theke.io>
Date: Fri, 27 Mar 2020 16:36:25 -0300
Subject: [PATCH] Bug 25006: Regression tests

This patch introduces regression tests for as_marc_field. It makes sure
there are empty subfields and then uses List::MoreUtils' all to verify
all generated MARC subfields contain something (i.e. the undef ones are
skipped).
---
 t/db_dependent/Koha/Item.t | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/t/db_dependent/Koha/Item.t b/t/db_dependent/Koha/Item.t
index 9ee2f77647..55709643b7 100644
--- a/t/db_dependent/Koha/Item.t
+++ b/t/db_dependent/Koha/Item.t
@@ -28,6 +28,8 @@ use Koha::Items;
 use Koha::Database;
 use Koha::Old::Items;
 
+use List::MoreUtils qw(all);
+
 use t::lib::TestBuilder;
 use t::lib::Mocks;
 
@@ -91,11 +93,13 @@ subtest "as_marc_field() tests" => sub {
     my @schema_columns = $schema->resultset('Item')->result_source->columns;
     my @mapped_columns = grep { exists $mss->{'items.'.$_} } @schema_columns;
 
-    plan tests => 2 * (scalar @mapped_columns + 1) + 1;
+    plan tests => 2 * (scalar @mapped_columns + 1) + 2;
 
     $schema->storage->txn_begin;
 
     my $item = $builder->build_sample_item;
+    # Make sure it has at least one undefined attribute
+    $item->set({ replacementprice => undef })->store->discard_changes;
 
     # Tests with the mss parameter
     my $marc_field = $item->as_marc_field({ mss => $mss });
@@ -141,6 +145,11 @@ subtest "as_marc_field() tests" => sub {
     $item->more_subfields_xml( C4::Items::_get_unlinked_subfields_xml( \@unlinked_subfields ) )->store;
 
     $marc_field = $item->as_marc_field;
+
+    my @subfields = $marc_field->subfields;
+    my $result = all { defined $_->[1] } @subfields;
+    ok( $result, 'There are no undef subfields' );
+
     is( scalar $marc_field->subfield('X'), 'Something weird', 'more_subfield_xml is considered' );
 
     $schema->storage->txn_rollback;
-- 
2.26.0