From 956bf0a0cf00fb32b4197f90c7021a210f002ad5 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Wed, 30 Apr 2025 12:36:58 +0200 Subject: [PATCH] Bug 38010: Add tests for embed contacts,interfaces,aliases (cherry picked from commit cae75569e6e8f2bfe5a2e6d3acc111485f81cc1e) Signed-off-by: Jonathan Druart --- Koha/REST/V1/Acquisitions/Vendors.pm | 12 +-- t/db_dependent/api/v1/acquisitions_vendors.t | 93 +++++++++++++++++++- 2 files changed, 95 insertions(+), 10 deletions(-) diff --git a/Koha/REST/V1/Acquisitions/Vendors.pm b/Koha/REST/V1/Acquisitions/Vendors.pm index 9cd94eb26ac..564a5dbdd95 100644 --- a/Koha/REST/V1/Acquisitions/Vendors.pm +++ b/Koha/REST/V1/Acquisitions/Vendors.pm @@ -126,16 +126,16 @@ sub update { return try { my $vendor_update = $c->req->json; - my $contacts = delete $vendor_update->{contacts}; - my $interfaces = delete $vendor_update->{interfaces}; - my $aliases = delete $vendor_update->{aliases}; + my $contacts = exists $vendor_update->{contacts} ? delete $vendor_update->{contacts} : undef; + my $interfaces = exists $vendor_update->{interfaces} ? delete $vendor_update->{interfaces} : undef; + my $aliases = exists $vendor_update->{aliases} ? delete $vendor_update->{aliases} : undef; $vendor->set_from_api($vendor_update); $vendor->store(); - $vendor->contacts( $contacts || [] ); - $vendor->aliases( $aliases || [] ); - $vendor->interfaces( $interfaces || [] ); + $vendor->contacts( $contacts || [] ) if defined $contacts; + $vendor->aliases( $aliases || [] ) if defined $aliases; + $vendor->interfaces( $interfaces || [] ) if defined $interfaces; return $c->render( status => 200, diff --git a/t/db_dependent/api/v1/acquisitions_vendors.t b/t/db_dependent/api/v1/acquisitions_vendors.t index 0320f7ff999..5e259ce2b90 100755 --- a/t/db_dependent/api/v1/acquisitions_vendors.t +++ b/t/db_dependent/api/v1/acquisitions_vendors.t @@ -151,7 +151,7 @@ subtest 'get() test' => sub { subtest 'add() tests' => sub { - plan tests => 16; + plan tests => 17; $schema->storage->txn_begin; @@ -220,12 +220,47 @@ subtest 'add() tests' => sub { ] ); + subtest 'relationships contracts, interfaces, aliases' => sub { + plan tests => 32; + my $vendor = { name => 'another vendor', contacts => [], interfaces => [], aliases => [] }; + my $vendor_id = $t->post_ok( "//$auth_userid:$password@/api/v1/acquisitions/vendors" => json => $vendor ) + ->status_is( 201, 'REST3 .2.1' )->json_is( '/name' => $vendor->{name} ) + + # FIXME Maybe we expect instead + # ->json_is( '/contacts' => [] )->json_is('/interfaces' => [], '/aliases' => [] ); + ->json_hasnt('/contacts')->json_hasnt('/interfaces')->json_hasnt('/aliases')->tx->res->json->{id}; + $t->get_ok( "//$auth_userid:$password@/api/v1/acquisitions/vendors/" . $vendor_id )->status_is(200) + ->json_hasnt('/contacts')->json_hasnt('/interfaces')->json_hasnt('/aliases'); + + $t->get_ok( "//$auth_userid:$password@/api/v1/acquisitions/vendors/" + . $vendor_id => { 'x-koha-embed' => 'contacts,interfaces,aliases' } )->status_is(200) + ->json_is( '/contacts', [] )->json_is( '/interfaces', [] )->json_is( '/aliases', [] ); + + $vendor = { + name => 'yet another vendor', contacts => [ { name => 'contact name' } ], + interfaces => [ { name => 'interface name' } ], aliases => [ { alias => 'foo' } ] + }; + $vendor_id = $t->post_ok( "//$auth_userid:$password@/api/v1/acquisitions/vendors" => json => $vendor ) + ->status_is( 201, 'REST3 .2.1' )->json_is( '/name' => $vendor->{name} ) + + # FIXME Maybe we expect instead + # ->json_is( '/contacts' => [] )->json_is('/interfaces' => [], '/aliases' => [] ); + ->json_hasnt('/contacts')->json_hasnt('/interfaces')->json_hasnt('/aliases')->tx->res->json->{id}; + + $t->get_ok( "//$auth_userid:$password@/api/v1/acquisitions/vendors/" . $vendor_id )->status_is(200) + ->json_hasnt('/contacts')->json_hasnt('/interfaces')->json_hasnt('/aliases'); + + $t->get_ok( "//$auth_userid:$password@/api/v1/acquisitions/vendors/" + . $vendor_id => { 'x-koha-embed' => 'contacts,interfaces,aliases' } )->status_is(200) + ->json_has('/contacts/0/name')->json_has('/interfaces/0/name')->json_has('/aliases/0/alias'); + }; + $schema->storage->txn_rollback; }; subtest 'update() tests' => sub { - plan tests => 15; + plan tests => 16; $schema->storage->txn_begin; @@ -294,12 +329,62 @@ subtest 'update() tests' => sub { $t->put_ok( "//$auth_userid:$password@/api/v1/acquisitions/vendors/" . $non_existent_id => json => $vendor_with_updated_field )->status_is(404); - $schema->storage->txn_rollback; - # Wrong method (POST) $t->post_ok( "//$auth_userid:$password@/api/v1/acquisitions/vendors/" . $vendor->id => json => $vendor_with_updated_field ) ->status_is(404); + + subtest 'relationships contracts, interfaces, aliases' => sub { + plan tests => 30; + my $vendor = $builder->build_object( { class => 'Koha::Acquisition::Booksellers' } ); + $vendor->contacts( [] ); + $vendor->interfaces( [] ); + $vendor->aliases( [] ); + + $t->get_ok( "//$auth_userid:$password@/api/v1/acquisitions/vendors/" + . $vendor->id => { 'x-koha-embed' => 'contacts,interfaces,aliases' } )->status_is(200) + ->json_is( '/contacts', [] )->json_is( '/interfaces', [] )->json_is( '/aliases', [] ); + + my $api_vendor = $vendor->to_api; + delete $api_vendor->{id}; + + $api_vendor->{contacts} = [ { name => 'contact name' } ]; + $api_vendor->{interfaces} = [ { name => 'interface name' } ]; + $api_vendor->{aliases} = [ { alias => 'foo' } ]; + + $t->put_ok( "//$auth_userid:$password@/api/v1/acquisitions/vendors/" . $vendor->id => json => $api_vendor ) + ->status_is( 200, 'REST3 .2.1' )->json_is( '/name' => $vendor->name )->json_hasnt('/contacts') + ->json_hasnt('/interfaces')->json_hasnt('/aliases'); + + $t->get_ok( "//$auth_userid:$password@/api/v1/acquisitions/vendors/" + . $vendor->id => { 'x-koha-embed' => 'contacts,interfaces,aliases' } )->status_is(200) + ->json_has('/contacts/0/name')->json_has('/interfaces/0/name')->json_has('/aliases/0/alias'); + + delete $api_vendor->{contacts}; + delete $api_vendor->{interfaces}; + delete $api_vendor->{aliases}; + + $t->put_ok( "//$auth_userid:$password@/api/v1/acquisitions/vendors/" . $vendor->id => json => $api_vendor ) + ->status_is( 200, 'REST3 .2.1' ); + + $t->get_ok( "//$auth_userid:$password@/api/v1/acquisitions/vendors/" + . $vendor->id => { 'x-koha-embed' => 'contacts,interfaces,aliases' } )->status_is(200) + ->json_has('/contacts/0/name')->json_has('/interfaces/0/name')->json_has('/aliases/0/alias'); + + $api_vendor->{contacts} = []; + $api_vendor->{interfaces} = []; + $api_vendor->{aliases} = []; + + $t->put_ok( "//$auth_userid:$password@/api/v1/acquisitions/vendors/" . $vendor->id => json => $api_vendor ) + ->status_is( 200, 'REST3 .2.1' ); + + $t->get_ok( "//$auth_userid:$password@/api/v1/acquisitions/vendors/" + . $vendor->id => { 'x-koha-embed' => 'contacts,interfaces,aliases' } )->status_is(200) + ->json_is( '/contacts', [] )->json_is( '/interfaces', [] )->json_is( '/aliases', [] ); + }; + + $schema->storage->txn_rollback; + }; subtest 'delete() tests' => sub { -- 2.34.1