Bug 31441

Summary: Koha::Item::as_marc_field ignores subfields where kohafield is an empty string
Product: Koha Reporter: Julian Maurice <julian.maurice>
Component: Architecture, internals, and plumbingAssignee: Julian Maurice <julian.maurice>
Status: RESOLVED FIXED QA Contact: Marcel de Rooy <m.de.rooy>
Severity: normal    
Priority: P5 - low CC: arthur.suzuki, david, lucas, m.de.rooy, wainuiwitikapark
Version: Main   
Hardware: All   
OS: All   
Change sponsored?: --- Patch complexity: Small patch
Documentation contact: Documentation submission:
Text to go in the release notes:
Version(s) released in:
22.11.00, 22.05.08, 21.11.15
Attachments: Bug 31441: Fix Koha::Item::as_marc_field when kohafield = ''
Bug 31441: Fix Koha::Item::as_marc_field when kohafield = ''
Bug 31441: Fix Koha::Item::as_marc_field when kohafield = ''

Description Julian Maurice 2022-08-23 12:21:47 UTC
marc_subfield_structure.kohafield can be NULL, but it can also be an empty string. But in that case, Koha::Item::as_marc_field ignores them, which means the resulting MARC::Field object has missing data.  This can produce a bug in OPAC ISBD view (and probably other places where this method is used)
Comment 1 Julian Maurice 2022-08-23 12:23:32 UTC
Created attachment 139676 [details] [review]
Bug 31441: Fix Koha::Item::as_marc_field when kohafield = ''

marc_subfield_structure.kohafield can be NULL, but it can also be an
empty string. But in that case, Koha::Item::as_marc_field ignores them,
which means the resulting MARC::Field object has missing data.
This can produce a bug in OPAC ISBD view (and probably other places
where this method is used)

Test plan:
1. Edit the default biblio MARC framework for the item field: find or
   create a subfield that is not linked to a DB column.
   Save even if you made no changes to make sure that
   marc_subfield_structure.kohafield is set to an empty string.
   I'll use 995$Z as an example for the following steps.
2. Add the following to syspref OPACISBD:
    #995|<br>Item:|{995Z}|
3. Create a biblio with an item and put a value into 995$Z
4. Go to the ISBD detail page for this record at OPAC. Confirm that the
   value you entered in 995$Z is not visible
5. Apply patch and restart koha
6. Refresh the ISBD detail page. Confirm that the 995$Z is now visible.
7. Run `prove t/db_dependent/Koha/Item.t`
Comment 2 David Nind 2022-09-22 23:25:04 UTC
Created attachment 140877 [details] [review]
Bug 31441: Fix Koha::Item::as_marc_field when kohafield = ''

marc_subfield_structure.kohafield can be NULL, but it can also be an
empty string. But in that case, Koha::Item::as_marc_field ignores them,
which means the resulting MARC::Field object has missing data.
This can produce a bug in OPAC ISBD view (and probably other places
where this method is used)

Test plan:
1. Edit the default biblio MARC framework for the item field: find or
   create a subfield that is not linked to a DB column.
   Save even if you made no changes to make sure that
   marc_subfield_structure.kohafield is set to an empty string.
   I'll use 995$Z as an example for the following steps.
2. Add the following to syspref OPACISBD:
    #995|<br>Item:|{995Z}|
3. Create a biblio with an item and put a value into 995$Z
4. Go to the ISBD detail page for this record at OPAC. Confirm that the
   value you entered in 995$Z is not visible
5. Apply patch and restart koha
6. Refresh the ISBD detail page. Confirm that the 995$Z is now visible.
7. Run `prove t/db_dependent/Koha/Item.t`

Signed-off-by: David Nind <david@davidnind.com>
Comment 3 David Nind 2022-09-23 00:30:43 UTC
Testing notes using a MARC21 instance (using koha-testing-docker):

1. I edited an existing subfield 995$z for the BKS framework, and made visible in the OPAC, staff interface, and editor

2. I also created a new subfield 995$Z for the BKS framework, and made visible in the OPAC, staff interface, and editor

3. Initially, everything worked as expected as per the test plan: not displayed in OPAC before the patch was applied. Only tested for OPAC ISBD display.

4. Then I retested again after I signed off just to check the staff interface, and it was displayed correctly without applying the patch on master (flush_memcached, restart_all, cleared browser cache - I also shut down KTD and restarted again, still the same behaviour).

5. I also added the display of the field to the staff interface (ISBD system preference).

Has this been fixed some other way, or have I totally screwed up caching/my git clone/testing? Or is it UNIMARC specific?
Comment 4 Marcel de Rooy 2022-09-30 08:17:48 UTC
Created attachment 141111 [details] [review]
Bug 31441: Fix Koha::Item::as_marc_field when kohafield = ''

marc_subfield_structure.kohafield can be NULL, but it can also be an
empty string. But in that case, Koha::Item::as_marc_field ignores them,
which means the resulting MARC::Field object has missing data.
This can produce a bug in OPAC ISBD view (and probably other places
where this method is used)

Test plan:
1. Edit the default biblio MARC framework for the item field: find or
   create a subfield that is not linked to a DB column.
   Save even if you made no changes to make sure that
   marc_subfield_structure.kohafield is set to an empty string.
   I'll use 995$Z as an example for the following steps.
2. Add the following to syspref OPACISBD:
    #995|<br>Item:|{995Z}|
3. Create a biblio with an item and put a value into 995$Z
4. Go to the ISBD detail page for this record at OPAC. Confirm that the
   value you entered in 995$Z is not visible
5. Apply patch and restart koha
6. Refresh the ISBD detail page. Confirm that the 995$Z is now visible.
7. Run `prove t/db_dependent/Koha/Item.t`

Signed-off-by: David Nind <david@davidnind.com>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Comment 5 Marcel de Rooy 2022-09-30 08:20:53 UTC
(In reply to David Nind from comment #3)
> Has this been fixed some other way, or have I totally screwed up caching/my
> git clone/testing? Or is it UNIMARC specific?

Koha/Filter/MARC/EmbedItems.pm:        push @item_fields, $item->as_marc_field( { mss => $mss } );

opac/opac-ISBDdetail.pl:    {   filters => [ 'EmbedItems', 'ViewPolicy' ],


Use of as_marc_field is not very widespread. catalogue/ISBDdetail.pl does not use it.
Comment 6 Tomás Cohen Arazi 2022-10-03 11:50:11 UTC
Pushed to master for 22.11.

Nice work everyone, thanks!
Comment 7 Lucas Gass 2022-12-05 15:40:50 UTC
Backported to 22.05.x for upcomiong 22.05.08 release
Comment 8 Arthur Suzuki 2022-12-14 09:44:38 UTC
applied to 21.11 for 21.11.15
Comment 9 wainuiwitikapark 2023-03-15 00:48:42 UTC
Not backported to 21.05.x