From 394263fe9430f904443776b4f0a521e0e95cda86 Mon Sep 17 00:00:00 2001
From: Pedro Amorim <pedro.amorim@ptfs-europe.com>
Date: Thu, 29 Feb 2024 13:44:12 +0000
Subject: [PATCH] Bug 34920: Add API endpoint tests

prove koha/t/db_dependent/api/v1/authorised_values.t

Signed-off-by: Laura Escamilla <laura.escamilla@bywatersolutions.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
---
 t/db_dependent/api/v1/authorised_values.t | 36 ++++++++++++++++++++++-
 1 file changed, 35 insertions(+), 1 deletion(-)

diff --git a/t/db_dependent/api/v1/authorised_values.t b/t/db_dependent/api/v1/authorised_values.t
index 187087d4408..067701e1be4 100755
--- a/t/db_dependent/api/v1/authorised_values.t
+++ b/t/db_dependent/api/v1/authorised_values.t
@@ -26,6 +26,8 @@ use t::lib::Mocks;
 use Koha::AuthorisedValues;
 use Koha::Database;
 
+use JSON qw( encode_json );
+
 my $schema  = Koha::Database->new->schema;
 my $builder = t::lib::TestBuilder->new;
 
@@ -34,7 +36,7 @@ t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
 
 subtest 'list_av_from_category() tests' => sub {
 
-    plan tests => 11;
+    plan tests => 22;
 
     $schema->storage->txn_begin;
 
@@ -83,5 +85,37 @@ subtest 'list_av_from_category() tests' => sub {
     $t->get_ok("//$unauth_userid:$password@/api/v1/authorised_value_categories/$av_cat/authorised_values")
       ->status_is(403);
 
+    # Test the query webservice endpoint for multiple av_cats
+    my $av_cat_2 =
+        $builder->build_object( { class => 'Koha::AuthorisedValueCategories', value => { category_name => 'cat_a' } } );
+    my $av_cat_3 =
+        $builder->build_object( { class => 'Koha::AuthorisedValueCategories', value => { category_name => 'cat_b' } } );
+    my $query = { "me.category_name" => [ $av_cat_2->category_name, $av_cat_3->category_name ] };
+    $t->get_ok( "//$userid:$password@/api/v1/authorised_value_categories?q=" . encode_json($query) )->status_is(200)
+        ->json_is( [ $av_cat_2->to_api, $av_cat_3->to_api ] );
+
+    # Test the above but with x-koha-embed: authorised_values
+    my $embedded_query = { "me.category_name" => [ $av_cat_2->category_name, $av_cat_3->category_name ] };
+    $t->get_ok( "//$userid:$password@/api/v1/authorised_value_categories?q="
+            . encode_json($embedded_query) => { 'x-koha-embed' => 'authorised_values' } )->status_is(200)
+        ->json_has( '/0/authorised_values', 'authorised_values object correctly embedded' )
+        ->json_has( '/1/authorised_values', 'authorised_values object correctly embedded' )
+        ->json_hasnt( '/2/', 'authorised_values object correctly embedded' );
+
+    # Test the query webservice endpoint for multiple av_cats with authorised_values embedded
+    my $av_2 = $builder->build_object(
+        { class => 'Koha::AuthorisedValues', value => { category => $av_cat_2->category_name, lib => undef } } );
+    my $av_3 = $builder->build_object(
+        {
+            class => 'Koha::AuthorisedValues',
+            value => { category => $av_cat_2->category_name, lib => 'description_value' }
+        }
+    );
+
+    my $embedded_av_query = { "me.category_name" => [ $av_cat_2->category_name ] };
+    $t->get_ok( "//$userid:$password@/api/v1/authorised_value_categories?q="
+            . encode_json($embedded_av_query) => { 'x-koha-embed' => 'authorised_values' } )->status_is(200)
+        ->json_is( [ { %{ $av_cat_2->to_api }, authorised_values => [ $av_2->to_api, $av_3->to_api ] } ] );
+
     $schema->storage->txn_rollback;
 };
-- 
2.45.2