From 9bec6eab71aa128a4b166bd4b5c90a235d2e7ee3 Mon Sep 17 00:00:00 2001
From: Tomas Cohen Arazi <tomascohen@theke.io>
Date: Mon, 26 Dec 2022 13:02:25 -0300
Subject: [PATCH] Bug 32529: Add Koha::Holds->filter_by_found

This patch adds a method for filtering Koha::Holds resultsets by the
status belonging to Koha's concept of 'found'.

To test:
1. Apply this patch
2. Run:
   $ kshell
  k$ prove t/db_dependent/Koha/Holds.t
=> SUCCESS: Tests pass!
3. Sign off :-D

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>

Signed-off-by: Katrin Fischer <katrin.fischer.83@web.de>
---
 Koha/Holds.pm               | 13 +++++++++++++
 t/db_dependent/Koha/Holds.t | 32 +++++++++++++++++++++++++++++++-
 2 files changed, 44 insertions(+), 1 deletion(-)

diff --git a/Koha/Holds.pm b/Koha/Holds.pm
index b53b95735a..d4e0b5cdc6 100644
--- a/Koha/Holds.pm
+++ b/Koha/Holds.pm
@@ -34,8 +34,21 @@ Koha::Holds - Koha Hold object set class
 
 =head2 Class methods
 
+=head3 filter_by_found
+
+    my $found_holds = $holds->filter_by_found;
+
+Returns a filtered resultset without holds that are considered I<found>.
+i.e. 'P', 'T' and 'W'.
+
 =cut
 
+sub filter_by_found {
+    my ($self) = @_;
+
+    return $self->search( { found => [ 'P', 'T', 'W' ] } );
+}
+
 =head3 waiting
 
 returns a set of holds that are waiting from an existing set
diff --git a/t/db_dependent/Koha/Holds.t b/t/db_dependent/Koha/Holds.t
index b292672a1c..57f1d96935 100755
--- a/t/db_dependent/Koha/Holds.t
+++ b/t/db_dependent/Koha/Holds.t
@@ -19,7 +19,7 @@
 
 use Modern::Perl;
 
-use Test::More tests => 10;
+use Test::More tests => 11;
 use Test::Warn;
 
 use C4::Circulation qw( AddIssue );
@@ -670,6 +670,36 @@ subtest 'Test Koha::Hold::item_group' => sub {
 
 $schema->storage->txn_rollback;
 
+subtest 'filter_by_found() tests' => sub {
+
+    plan tests => 5;
+
+    $schema->storage->txn_begin;
+
+    my $unfilled   = $builder->build_object( { class => 'Koha::Holds', value => { found => undef } } );
+    my $processing = $builder->build_object( { class => 'Koha::Holds', value => { found => 'P' } } );
+    my $in_transit = $builder->build_object( { class => 'Koha::Holds', value => { found => 'T' } } );
+    my $waiting    = $builder->build_object( { class => 'Koha::Holds', value => { found => 'W' } } );
+
+    my $holds = Koha::Holds->search(
+        { reserve_id => [ $unfilled->id, $processing->id, $in_transit->id, $waiting->id ] },
+        { order_by => ['reserve_id'] }
+    );
+
+    is( $holds->count, 4, 'Resultset count is correct' );
+
+    my $found_holds = $holds->filter_by_found;
+
+    is( $found_holds->count, 3, 'Resultset count is correct' );
+
+    ok( $found_holds->next->is_in_processing, 'Status is correct (P)' );
+    ok( $found_holds->next->is_in_transit, 'Status is correct (T)' );
+    ok( $found_holds->next->is_waiting, 'Status is correct (W)' );
+
+
+    $schema->storage->txn_rollback;
+};
+
 subtest 'filter_by_has_cancellation_requests() and filter_out_has_cancellation_requests() tests' => sub {
 
     plan tests => 7;
-- 
2.30.2