View | Details | Raw Unified | Return to bug 35115
Collapse All | Expand All

(-)a/Koha/ERM/EHoldings/Resource.pm (-39 lines)
Lines 21-27 use MARC::Record; Link Here
21
21
22
use Koha::Database;
22
use Koha::Database;
23
23
24
use C4::Biblio qw( AddBiblio TransformKohaToMarc );
25
use Koha::Acquisition::Booksellers;
24
use Koha::Acquisition::Booksellers;
26
use Koha::Biblios;
25
use Koha::Biblios;
27
use Koha::ERM::EHoldings::Titles;
26
use Koha::ERM::EHoldings::Titles;
Lines 39-82 Koha::ERM::EHoldings::Resource - Koha EHolding resource Object class Link Here
39
38
40
=cut
39
=cut
41
40
42
=head3 store
43
44
=cut
45
46
sub store {
47
    my ($self) = @_;
48
49
    # FIXME This is terrible and ugly, we need to:
50
    # * Provide a mapping for each attribute of title
51
    # * Create a txn
52
    my $title = $self->title;
53
    my $biblio =
54
      $title->biblio_id
55
      ? Koha::Biblios->find( $title->biblio_id )->unblessed
56
      : {};
57
58
    my $marc_record = TransformKohaToMarc(
59
        {
60
            %$biblio,
61
            'biblio.title' => $title->publication_title,
62
63
        }
64
    );
65
66
    my $biblio_id;
67
    if ( %$biblio ) {
68
        $biblio_id = $title->biblio_id;
69
        C4::Biblio::ModBiblio($marc_record, $title->biblio_id, '');
70
    } else {
71
        ( $biblio_id ) = C4::Biblio::AddBiblio($marc_record, '');
72
    }
73
74
    $title->biblio_id($biblio_id)->store;
75
76
    $self = $self->SUPER::store;
77
    return $self;
78
}
79
80
=head3 package
41
=head3 package
81
42
82
Return the package for this resource
43
Return the package for this resource
(-)a/Koha/ERM/EHoldings/Title.pm (+37 lines)
Lines 21-26 use Koha::Database; Link Here
21
21
22
use base qw(Koha::Object);
22
use base qw(Koha::Object);
23
23
24
use C4::Biblio qw( AddBiblio TransformKohaToMarc GetMarcFromKohaField );
25
24
use Koha::ERM::EHoldings::Resources;
26
use Koha::ERM::EHoldings::Resources;
25
27
26
=head1 NAME
28
=head1 NAME
Lines 31-36 Koha::ERM::EHoldings::Title - Koha ERM Title Object class Link Here
31
33
32
=head2 Class Methods
34
=head2 Class Methods
33
35
36
=head3 store
37
38
=cut
39
40
sub store {
41
    my ($self) = @_;
42
43
    # FIXME This is terrible and ugly, we need to:
44
    # * Provide a mapping for each attribute of title
45
    # * Create a txn
46
47
    # If the 'title' is alreay linked to a biblio, then we update the title subfield only
48
    if ( $self->biblio_id ){
49
        my $biblio = Koha::Biblios->find( $self->biblio_id );
50
        my ($title_tag, $title_subfield) = GetMarcFromKohaField( 'biblio.title' );
51
        my $record = $biblio->metadata->record();
52
        my $title_field = $record->field($title_tag);
53
        $title_field->update( $title_subfield => $self->publication_title );
54
        C4::Biblio::ModBiblio( $record, $self->biblio_id, '' );
55
    } else {
56
    # If it's not linked, we create a simple biblio and save the biblio id to the 'title'
57
        my $marc_record = TransformKohaToMarc(
58
            {
59
                'biblio.title' => $self->publication_title,
60
            }
61
        );
62
        my ( $biblio_id ) = C4::Biblio::AddBiblio($marc_record, '');
63
        $self->biblio_id($biblio_id);
64
    }
65
66
    $self = $self->SUPER::store;
67
    return $self;
68
69
}
70
34
=head3 resources
71
=head3 resources
35
72
36
Returns the resources linked to this title
73
Returns the resources linked to this title
(-)a/t/db_dependent/api/v1/erm_eholdings_titles.t (-4 / +39 lines)
Lines 23-28 use Test::Mojo; Link Here
23
use t::lib::TestBuilder;
23
use t::lib::TestBuilder;
24
use t::lib::Mocks;
24
use t::lib::Mocks;
25
25
26
use C4::Biblio qw( GetMarcFromKohaField );
27
26
use Koha::ERM::EHoldings::Titles;
28
use Koha::ERM::EHoldings::Titles;
27
use Koha::ERM::EHoldings::Packages;
29
use Koha::ERM::EHoldings::Packages;
28
use Koha::Virtualshelves;
30
use Koha::Virtualshelves;
Lines 337-343 subtest 'add() tests' => sub { Link Here
337
339
338
subtest 'update() tests' => sub {
340
subtest 'update() tests' => sub {
339
341
340
    plan tests => 15;
342
    plan tests => 16;
341
343
342
    $schema->storage->txn_begin;
344
    $schema->storage->txn_begin;
343
345
Lines 362-369 subtest 'update() tests' => sub { Link Here
362
    my $unauth_userid = $patron->userid;
364
    my $unauth_userid = $patron->userid;
363
365
364
    my $ehtitle_id =
366
    my $ehtitle_id =
365
      $builder->build_object( { class => 'Koha::ERM::EHoldings::Titles' } )
367
        $builder->build_object( { class => 'Koha::ERM::EHoldings::Titles', value => { biblio_id => undef } } )
366
      ->title_id;
368
        ->title_id;
367
369
368
    # Unauthorized attempt to update
370
    # Unauthorized attempt to update
369
    $t->put_ok(
371
    $t->put_ok(
Lines 460-465 subtest 'update() tests' => sub { Link Here
460
        "//$userid:$password@/api/v1/erm/eholdings/local/titles/$ehtitle_id" =>
462
        "//$userid:$password@/api/v1/erm/eholdings/local/titles/$ehtitle_id" =>
461
          json => $ehtitle_with_updated_field )->status_is(404);
463
          json => $ehtitle_with_updated_field )->status_is(404);
462
464
465
    subtest 'update eholdings title linked to biblio tests' => sub {
466
467
        plan tests => 6;
468
469
        my $biblio = $builder->build_sample_biblio();
470
        my $record = $biblio->metadata->record();
471
472
        my $ehtitle_id =
473
            $builder->build_object( { class => 'Koha::ERM::EHoldings::Titles', value => { biblio_id => $biblio->id } } )
474
            ->title_id;
475
        my $ehtitle_updated_title = { publication_title => "The journal of writing unit tests :" };
476
477
        $t->put_ok(
478
            "//$userid:$password@/api/v1/erm/eholdings/local/titles/$ehtitle_id" => json => $ehtitle_updated_title )
479
            ->status_is(200)->json_is( '/publication_title' => 'The journal of writing unit tests :' );
480
481
        $biblio->discard_changes;
482
        warn $biblio->title;
483
        my $record_after = $biblio->metadata->record;
484
485
        my ( $title_tag, $title_subfield ) = GetMarcFromKohaField('biblio.title');
486
        is(
487
            $record_after->subfield( $title_tag, $title_subfield ), "The journal of writing unit tests :",
488
            "Biblio title is correctly update by eholding title update"
489
        );
490
491
        is( $record->fields(), $record_after->fields(), "We have the same number of fields after edit" );
492
        my ($author_tag) = GetMarcFromKohaField('biblio.author');
493
        my $old_author   = $record->field($author_tag);
494
        my $new_author   = $record_after->field($author_tag);
495
        is( $old_author->as_string, $new_author->as_string, "The record is otherwise untouched" );
496
497
    };
498
463
    $schema->storage->txn_rollback;
499
    $schema->storage->txn_rollback;
464
};
500
};
465
501
466
- 

Return to bug 35115