Bug 22456: Add Koha::Holds->filter_by_has_cancellation_requests
authorTomas Cohen Arazi <tomascohen@theke.io>
Mon, 23 May 2022 14:44:19 +0000 (11:44 -0300)
committerTomas Cohen Arazi <tomascohen@theke.io>
Fri, 29 Jul 2022 18:28:29 +0000 (15:28 -0300)
This patch adds that method. Which is covered by new tests.

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

Sponsored-by: Montgomery County Public Libraries
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: David Nind <david@davidnind.com>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Koha/Holds.pm
t/db_dependent/Koha/Holds.t

index 7d070f8..7bedab1 100644 (file)
@@ -32,7 +32,7 @@ Koha::Holds - Koha Hold object set class
 
 =head1 API
 
-=head2 Class Methods
+=head2 Class methods
 
 =cut
 
@@ -148,7 +148,24 @@ sub get_items_that_can_fill {
     )->filter_by_for_hold();
 }
 
-=head3 type
+=head3 filter_by_has_cancellation_requests
+
+    my $with_cancellation_reqs = $holds->filter_by_has_cancellation_requests;
+
+Returns a filtered resultset only containing holds that have cancellation requests.
+
+=cut
+
+sub filter_by_has_cancellation_requests {
+    my ($self) = @_;
+
+    return $self->search( { 'hold_cancellation_request_id' => { '!=' => undef } },
+        { join => 'cancellation_requests' } );
+}
+
+=head2 Internal methods
+
+=head3 _type
 
 =cut
 
index 6d47bf2..188ffd5 100755 (executable)
@@ -19,7 +19,7 @@
 
 use Modern::Perl;
 
-use Test::More tests => 7;
+use Test::More tests => 8;
 use Test::Warn;
 
 use C4::Circulation qw( AddIssue );
@@ -644,4 +644,67 @@ subtest 'set_waiting+patron_expiration_date' => sub {
 
 $schema->storage->txn_rollback;
 
-1;
+subtest 'filter_by_has_cancellation_requests() tests' => sub {
+
+    plan tests => 4;
+
+    $schema->storage->txn_begin;
+
+    my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
+
+    my $item_1 = $builder->build_sample_item;
+    my $item_2 = $builder->build_sample_item;
+    my $item_3 = $builder->build_sample_item;
+
+    my $hold_1 = $builder->build_object(
+        {
+            class => 'Koha::Holds',
+            value => {
+                found          => 'W',
+                itemnumber     => $item_1->id,
+                biblionumber   => $item_1->biblionumber,
+                borrowernumber => $patron->id
+            }
+        }
+    );
+    my $hold_2 = $builder->build_object(
+        {
+            class => 'Koha::Holds',
+            value => {
+                found          => 'W',
+                itemnumber     => $item_2->id,
+                biblionumber   => $item_2->biblionumber,
+                borrowernumber => $patron->id
+            }
+        }
+    );
+    my $hold_3 = $builder->build_object(
+        {
+            class => 'Koha::Holds',
+            value => {
+                found          => 'W',
+                itemnumber     => $item_3->id,
+                biblionumber   => $item_3->biblionumber,
+                borrowernumber => $patron->id
+            }
+        }
+    );
+
+    my $rs = Koha::Holds->search(
+        { reserve_id => [ $hold_1->id, $hold_2->id, $hold_3->id ] } );
+
+    is( $rs->count, 3 );
+
+    my $filtered_rs = $rs->filter_by_has_cancellation_requests;
+
+    is( $filtered_rs->count, 0 );
+
+    $hold_2->add_cancellation_request;
+
+    $filtered_rs = $rs->filter_by_has_cancellation_requests;
+
+    is( $filtered_rs->count,    1 );
+    is( $filtered_rs->next->id, $hold_2->id );
+
+    $schema->storage->txn_rollback;
+};