From fb7a84dff99bab5b386ef2e841725dfe2978521c Mon Sep 17 00:00:00 2001
From: Nick Clemens <nick@bywatersolutions.com>
Date: Wed, 8 Nov 2023 18:36:29 +0000
Subject: [PATCH] Bug 35115: Move store routine from Resource to Title and
 update code

These patches move the store routine from Koha::ERM::EHoldings::Resource to Koha::ERM::EHoldings::Title as the code deals exclusively with title fields.

It updates the code to ensure that records are created when a title is not attched to a biblio, and that only the biblio title field is updated when updating an eholdings title.

To test:
 1 - Enable ERMModule sys pref
 2 - Create a new public list, visit:
     /cgi-bin/koha/virtualshelves/shelves.pl
 3 - Click "New list" enter name, set public -> public
 4 - Click "Add items", enter 112\n113\n114 (new line for each), in "Biblio numbers"
 5 - Notice that all added biblios have quite a few MARC fields
 6 - Go to packages, visit:
     /cgi-bin/koha/erm/eholdings/local/packages
 7 - Create a new package, add a name and hit 'Submit'
 8 - Go to titles, visit:
     /cgi-bin/koha/erm/eholdings/local/titles
 9 - Click "import from list"
10 - Pick the package created in 7)
11 - On the row of the list created in 2), click "Import"
12 - Go back to the list, visit:
     /cgi-bin/koha/virtualshelves/shelves.pl?op=view&shelfnumber=1
13 - Notice all the biblios have been stripped of their MARC data, and only title remains.
14 - Go to details page for one of the bibs
15 - Edit -> Replace record via Z39.50
16 - You can search for anything, just make sure to import a mostly full record
17 - Go back to ERM - eHoldings - Local - Titles
18 - Edit the title for the record that was replaced
19 - Save
20 - Go to record details - note the record is gone again
21 - Apply patches
22 - Search the catalog
23 - Check some titles
24 - Add to a new list
25 - repeat 8 - 11 with the new list
26 - View and confirm records are intact
27 - Edit the eholdings title for one of the records, changing the title field
28 - Save and view record details
29 - Confirm record is intact and title updated
---
 Koha/ERM/EHoldings/Resource.pm               | 39 --------------------
 Koha/ERM/EHoldings/Title.pm                  | 37 +++++++++++++++++++
 t/db_dependent/api/v1/erm_eholdings_titles.t | 33 ++++++++++++++++-
 3 files changed, 69 insertions(+), 40 deletions(-)

diff --git a/Koha/ERM/EHoldings/Resource.pm b/Koha/ERM/EHoldings/Resource.pm
index 2457b2d6443..b9e3bb4a6f0 100644
--- a/Koha/ERM/EHoldings/Resource.pm
+++ b/Koha/ERM/EHoldings/Resource.pm
@@ -21,7 +21,6 @@ use MARC::Record;
 
 use Koha::Database;
 
-use C4::Biblio qw( AddBiblio TransformKohaToMarc );
 use Koha::Acquisition::Booksellers;
 use Koha::Biblios;
 use Koha::ERM::EHoldings::Titles;
@@ -39,44 +38,6 @@ Koha::ERM::EHoldings::Resource - Koha EHolding resource Object class
 
 =cut
 
-=head3 store
-
-=cut
-
-sub store {
-    my ($self) = @_;
-
-    # FIXME This is terrible and ugly, we need to:
-    # * Provide a mapping for each attribute of title
-    # * Create a txn
-    my $title = $self->title;
-    my $biblio =
-      $title->biblio_id
-      ? Koha::Biblios->find( $title->biblio_id )->unblessed
-      : {};
-
-    my $marc_record = TransformKohaToMarc(
-        {
-            %$biblio,
-            'biblio.title' => $title->publication_title,
-
-        }
-    );
-
-    my $biblio_id;
-    if ( %$biblio ) {
-        $biblio_id = $title->biblio_id;
-        C4::Biblio::ModBiblio($marc_record, $title->biblio_id, '');
-    } else {
-        ( $biblio_id ) = C4::Biblio::AddBiblio($marc_record, '');
-    }
-
-    $title->biblio_id($biblio_id)->store;
-
-    $self = $self->SUPER::store;
-    return $self;
-}
-
 =head3 package
 
 Return the package for this resource
diff --git a/Koha/ERM/EHoldings/Title.pm b/Koha/ERM/EHoldings/Title.pm
index 7802581f37b..4a5c4e5ca17 100644
--- a/Koha/ERM/EHoldings/Title.pm
+++ b/Koha/ERM/EHoldings/Title.pm
@@ -21,6 +21,8 @@ use Koha::Database;
 
 use base qw(Koha::Object);
 
+use C4::Biblio qw( AddBiblio TransformKohaToMarc GetMarcFromKohaField );
+
 use Koha::ERM::EHoldings::Resources;
 
 =head1 NAME
@@ -31,6 +33,41 @@ Koha::ERM::EHoldings::Title - Koha ERM Title Object class
 
 =head2 Class Methods
 
+=head3 store
+
+=cut
+
+sub store {
+    my ($self) = @_;
+
+    # FIXME This is terrible and ugly, we need to:
+    # * Provide a mapping for each attribute of title
+    # * Create a txn
+
+    # If the 'title' is alreay linked to a biblio, then we update the title subfield only
+    if ( $self->biblio_id ){
+        my $biblio = Koha::Biblios->find( $self->biblio_id );
+        my ($title_tag, $title_subfield) = GetMarcFromKohaField( 'biblio.title' );
+        my $record = $biblio->metadata->record();
+        my $title_field = $record->field($title_tag);
+        $title_field->update( $title_subfield => $self->publication_title );
+        C4::Biblio::ModBiblio( $record, $self->biblio_id, '' );
+    } else {
+    # If it's not linked, we create a simple biblio and save the biblio id to the 'title'
+        my $marc_record = TransformKohaToMarc(
+            {
+                'biblio.title' => $self->publication_title,
+            }
+        );
+        my ( $biblio_id ) = C4::Biblio::AddBiblio($marc_record, '');
+        $self->biblio_id($biblio_id);
+    }
+
+    $self = $self->SUPER::store;
+    return $self;
+
+}
+
 =head3 resources
 
 Returns the resources linked to this title
diff --git a/t/db_dependent/api/v1/erm_eholdings_titles.t b/t/db_dependent/api/v1/erm_eholdings_titles.t
index 17b5a022f0b..cbf79d31602 100755
--- a/t/db_dependent/api/v1/erm_eholdings_titles.t
+++ b/t/db_dependent/api/v1/erm_eholdings_titles.t
@@ -23,6 +23,8 @@ use Test::Mojo;
 use t::lib::TestBuilder;
 use t::lib::Mocks;
 
+use C4::Biblio qw( GetMarcFromKohaField );
+
 use Koha::ERM::EHoldings::Titles;
 use Koha::ERM::EHoldings::Packages;
 use Koha::Virtualshelves;
@@ -337,7 +339,7 @@ subtest 'add() tests' => sub {
 
 subtest 'update() tests' => sub {
 
-    plan tests => 15;
+    plan tests => 16;
 
     $schema->storage->txn_begin;
 
@@ -460,6 +462,35 @@ subtest 'update() tests' => sub {
         "//$userid:$password@/api/v1/erm/eholdings/local/titles/$ehtitle_id" =>
           json => $ehtitle_with_updated_field )->status_is(404);
 
+    subtest 'update eholdings title linked to biblio tests' => sub {
+
+        plan tests => 4;
+
+        my $biblio = $builder->build_sample_biblio();
+        my $record = $biblio->metadata->record();
+        warn $record->as_formatted;
+
+        my $ehtitle_id =
+            $builder->build_object( { class => 'Koha::ERM::EHoldings::Titles', value => { biblio_id => $biblio->id } } )
+                ->title_id;
+        my $ehtitle_updated_title = { publication_title => "The journal of writing unit tests :" };
+
+        $t->put_ok(
+            "//$userid:$password@/api/v1/erm/eholdings/local/titles/$ehtitle_id" =>
+              json => $ehtitle_updated_title )->status_is(200)
+          ->json_is( '/publication_title' => 'The journal of writing unit tests :' );
+
+        $biblio->discard_changes;
+        warn $biblio->title;
+        my $record_after = $biblio->metadata->record;
+
+        my ( $title_tag, $title_subfield ) = GetMarcFromKohaField( 'biblio.title' );
+        is( $record_after->subfield( $title_tag, $title_subfield), "The journal of writing unit tests :", "Biblio title is correctly update by eholding title update");
+
+        is( $record->delete_field($title_tag)->as_formatted, $record_after->delete_field($title_tag)->as_formatted, "The record is other untouched" );
+
+    };
+
     $schema->storage->txn_rollback;
 };
 
-- 
2.30.2