Bug 29234: Further clean Z3950 Tests
[koha-ffzg.git] / t / db_dependent / Koha / Holds.t
index 6d47bf2..4b859cf 100755 (executable)
 
 use Modern::Perl;
 
-use Test::More tests => 7;
+use Test::More tests => 11;
 use Test::Warn;
 
 use C4::Circulation qw( AddIssue );
 use C4::Reserves qw( AddReserve ModReserve ModReserveCancelAll );
 use Koha::AuthorisedValueCategory;
+use Koha::Biblio::ItemGroups;
 use Koha::Database;
 use Koha::DateUtils qw( dt_from_string );
 use Koha::Holds;
@@ -528,7 +529,7 @@ subtest 'get_items_that_can_fill' => sub {
         }
     );
     $items = $holds->get_items_that_can_fill;
-    is_deeply( [ sort map { $_->itemnumber } $items->as_list ],
+    is_deeply( [ sort { $a <=> $b } map { $_->itemnumber } $items->as_list ],
         [ $item_1->itemnumber, $item_2->itemnumber, $item_5->itemnumber ], 'Items 1, 2, and 5 are available for filling the holds' );
 
     my $no_holds = Koha::Holds->new->empty();
@@ -641,7 +642,169 @@ subtest 'set_waiting+patron_expiration_date' => sub {
     };
 };
 
+subtest 'Test Koha::Hold::item_group' => sub {
+    plan tests => 1;
+    my $library    = $builder->build_object( { class => 'Koha::Libraries' } );
+    my $patron = $builder->build_object({ class => 'Koha::Patrons' });
+    my $item = $builder->build_sample_item;
+    my $item_group = $builder->build_object(
+        {
+            class => 'Koha::Biblio::ItemGroups',
+        }
+    );
+    my $reserve_id = AddReserve(
+        {
+            branchcode       => $library->branchcode,
+            borrowernumber   => $patron->borrowernumber,
+            biblionumber     => $item->biblionumber,
+            itemnumber       => $item->itemnumber,
+            item_group_id    => $item_group->id,
+        }
+    );
+
+    my $hold = Koha::Holds->find($reserve_id);
+    is( $hold->item_group_id, $item_group->id,
+        'Koha::Hold::item_group returns the correct item_group' );
+};
+
 
 $schema->storage->txn_rollback;
 
-1;
+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;
+
+    $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 );
+
+    my $filtered_out_rs = $rs->filter_out_has_cancellation_requests;
+
+    is( $filtered_out_rs->count, 3 );
+
+    $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 );
+
+    $filtered_out_rs = $rs->filter_out_has_cancellation_requests;
+
+    is( $filtered_out_rs->count,    2 );
+    is( $filtered_out_rs->next->id, $hold_1->id );
+
+    $schema->storage->txn_rollback;
+};
+
+subtest 'processing() tests' => sub {
+
+    plan tests => 3;
+
+    $schema->storage->txn_begin;
+
+    my $hold_1 = $builder->build_object(
+        {
+            class => 'Koha::Holds',
+            value => { found => 'P' }
+        }
+    );
+    my $hold_2 = $builder->build_object(
+        {
+            class => 'Koha::Holds',
+            value => { found => undef }
+        }
+    );
+    my $hold_3 = $builder->build_object(
+        {
+            class => 'Koha::Holds',
+            value => { found => 'T' }
+        }
+    );
+
+    my $holds = Koha::Holds->search({ reserve_id => [ $hold_1->id, $hold_2->id, $hold_3->id ] });
+    is( $holds->count, 3, 'Resultset contains 3 holds' );
+
+    my $processing = $holds->processing;
+    is( $processing->count, 1 );
+    is( $processing->next->id, $hold_1->id, "First hold is the only one in 'processing'" );
+
+    $schema->storage->txn_rollback;
+};