Bug 32121: Show an alert when adding a checked out item to an item bundle
[srvgit] / t / db_dependent / Koha / Item.t
index d9252ef..6f9d7ce 100755 (executable)
@@ -213,12 +213,17 @@ subtest 'bundle_host tests' => sub {
 };
 
 subtest 'add_to_bundle tests' => sub {
-    plan tests => 6;
+    plan tests => 7;
 
     $schema->storage->txn_begin;
 
     t::lib::Mocks::mock_preference( 'BundleNotLoanValue', 1 );
 
+    my $library = $builder->build_object({ class => 'Koha::Libraries' });
+    t::lib::Mocks::mock_userenv({
+        branchcode => $library->branchcode
+    });
+
     my $host_item = $builder->build_sample_item();
     my $bundle_item1 = $builder->build_sample_item();
     my $bundle_item2 = $builder->build_sample_item();
@@ -242,6 +247,26 @@ subtest 'add_to_bundle tests' => sub {
     'Koha::Exceptions::Item::Bundle::IsBundle',
       'Exception thrown if you try to add a bundle host to a bundle item';
 
+    my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
+    C4::Circulation::AddIssue( $patron->unblessed, $bundle_item2->barcode );
+    throws_ok { $host_item->add_to_bundle($bundle_item2) }
+    'Koha::Exceptions::Item::Bundle::ItemIsCheckedOut',
+      'Exception thrown if you try to add a checked out item';
+
+    $bundle_item2->withdrawn(1)->store;
+    t::lib::Mocks::mock_preference( 'BlockReturnOfWithdrawnItems', 1 );
+    throws_ok { $host_item->add_to_bundle( $bundle_item2, { force_checkin => 1 } ) }
+    'Koha::Exceptions::Checkin::FailedCheckin',
+      'Exception thrown if you try to add a checked out item using
+      "force_checkin" and the return is not possible';
+
+    $bundle_item2->withdrawn(0)->store;
+    lives_ok { $host_item->add_to_bundle( $bundle_item2, { force_checkin => 1 } ) }
+    'No exception if you try to add a checked out item using "force_checkin" and the return is possible';
+
+    $bundle_item2->discard_changes;
+    ok( !$bundle_item2->checkout, 'Item is not checked out after being added to a bundle' );
+
     $schema->storage->txn_rollback;
 };