From 498ee5fd8a592de8c4ac0cce2fad56d4b7c59ed0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tadeusz=20=E2=80=9Etadzik=E2=80=9D=20So=C5=9Bnierz?=
 <tadeusz@sosnierz.com>
Date: Thu, 10 Apr 2025 15:34:26 +0200
Subject: [PATCH] Bug 39600: Add tests for patron_list

Sponsored-by: Wiko (https://www.wiko-berlin.de/)
---
 t/db_dependent/api/v1/ill_requests.t | 82 +++++++++++++++++++++++++++-
 1 file changed, 81 insertions(+), 1 deletion(-)

diff --git a/t/db_dependent/api/v1/ill_requests.t b/t/db_dependent/api/v1/ill_requests.t
index be961d8a87..830a2def2b 100755
--- a/t/db_dependent/api/v1/ill_requests.t
+++ b/t/db_dependent/api/v1/ill_requests.t
@@ -18,7 +18,7 @@
 use Modern::Perl;
 
 use Test::NoWarnings;
-use Test::More tests => 3;
+use Test::More tests => 4;
 
 use Test::MockModule;
 use Test::MockObject;
@@ -264,6 +264,86 @@ subtest 'list() tests' => sub {
     $schema->storage->txn_rollback;
 };
 
+subtest 'patron_list() tests' => sub {
+
+    plan tests => 15;
+
+    # Mock ILLBackend (as object)
+    my $backend = Test::MockObject->new;
+    $backend->set_isa('Koha::Illbackends::Mock');
+    $backend->set_always( 'name', 'Mock' );
+    $backend->mock(
+        'status_graph', sub { },
+    );
+
+    # Mock Koha::ILL::Request::load_backend (to load Mocked Backend)
+    my $illreqmodule = Test::MockModule->new('Koha::ILL::Request');
+    $illreqmodule->mock(
+        'load_backend',
+        sub { my $self = shift; $self->{_my_backend} = $backend; return $self }
+    );
+
+    $schema->storage->txn_begin;
+
+    Koha::ILL::Requests->search->delete;
+
+    my $requester = $builder->build_object(
+        {
+            class => 'Koha::Patrons',
+            value => { flags => 0 }
+        }
+    );
+    my $password = 'thePassword123';
+    $requester->set_password( { password => $password, skip_validation => 1 } );
+    my $requester_userid = $requester->userid;
+    my $requester_number = $requester->borrowernumber;
+
+    my $observer = $builder->build_object(
+        {
+            class => 'Koha::Patrons',
+            value => { flags => 0 }
+        }
+    );
+
+    $observer->set_password( { password => $password, skip_validation => 1 } );
+    my $observer_userid = $observer->userid;
+    my $observer_number = $observer->borrowernumber;
+
+    # No requests yet, expect empty for both
+    $t->get_ok("//$requester_userid:$password@/api/v1/patrons/$requester_number/ill/requests")->status_is(200)->json_is( [] );
+    $t->get_ok("//$observer_userid:$password@/api/v1/patrons/$observer_number/ill/requests")->status_is(200)->json_is( [] );
+
+    for (0..25) {
+        $builder->build_object(
+            {
+                class => 'Koha::ILL::Requests',
+                value => {
+                    borrowernumber => $requester_number,
+                    batch_id       => undef,
+                    status         => 'NEW',
+                    backend        => $backend->name,
+                    notesstaff     => 'secret staff notes'
+                }
+            }
+        );
+    }
+
+    # Other user should not see anything
+    $t->get_ok("//$observer_userid:$password@/api/v1/patrons/$observer_number/ill/requests")->status_is(200)->json_is( [] );
+
+    # Staff notes hidden in the public API
+    $t->get_ok("//$requester_userid:$password@/api/v1/patrons/$requester_number/ill/requests")->status_is(200)->json_is(
+        '/0/staff_notes' => undef,
+    );
+
+    # Not all requests get returned, pagination works
+    $t->get_ok("//$requester_userid:$password@/api/v1/patrons/$requester_number/ill/requests")->status_is(200)->json_is(
+        '/21' => undef,
+    );
+
+    $schema->storage->txn_rollback;
+};
+
 subtest 'add() tests' => sub {
 
     plan tests => 2;
-- 
2.47.2