Bug 26384: Fix executable flags
[koha-ffzg.git] / t / db_dependent / Illrequests.t
old mode 100644 (file)
new mode 100755 (executable)
index 2b24e92..f6d6fa7
@@ -18,6 +18,9 @@
 use Modern::Perl;
 
 use File::Basename qw/basename/;
+
+use C4::Circulation qw(AddIssue AddReturn);
+
 use Koha::Database;
 use Koha::Illrequestattributes;
 use Koha::Illrequest::Config;
@@ -33,8 +36,10 @@ use t::lib::TestBuilder;
 use Test::MockObject;
 use Test::MockModule;
 use Test::Exception;
+use Test::Deep qw/ cmp_deeply ignore /;
+use Test::Warn;
 
-use Test::More tests => 12;
+use Test::More tests => 13;
 
 my $schema = Koha::Database->new->schema;
 my $builder = t::lib::TestBuilder->new;
@@ -120,7 +125,7 @@ subtest 'Basic object tests' => sub {
 
 subtest 'Working with related objects' => sub {
 
-    plan tests => 5;
+    plan tests => 7;
 
     $schema->storage->txn_begin;
 
@@ -161,6 +166,16 @@ subtest 'Working with related objects' => sub {
     is($illrq_obj->illrequestattributes->count + 1, Koha::Illrequestattributes->search->count,
        "Fetching expected number of Illrequestattributes for our request.");
 
+    is($illrq_obj->biblio, undef, "->biblio returns undef if no biblio");
+    my $biblio = $builder->build_object({ class => 'Koha::Biblios' });
+    my $req_bib = $builder->build_object({
+        class => 'Koha::Illrequests',
+        value => {
+            biblio_id      => $biblio->biblionumber
+        }
+    });
+    isa_ok($req_bib->biblio, 'Koha::Biblio', "OK accessing related biblio");
+
     $illrq_obj->delete;
     is(Koha::Illrequestattributes->search->count, 1,
        "Correct number of illrequestattributes after delete.");
@@ -244,8 +259,13 @@ subtest 'Status Graph tests' => sub {
     # Compare the updated graph to the expected graph
     # The structure we compare against here is just a copy of the structure found
     # in Koha::Illrequest::_core_status_graph() + the new node we created above
-    is_deeply( $new_graph,
+    cmp_deeply( $new_graph,
         {
+        TEST => {
+            prev_actions   => [ ],
+            id             => 'TEST',
+            next_actions   => [ ],
+        },
         NEW => {
             prev_actions => [ ],                           # Actions containing buttons
                                                            # leading to this status
@@ -264,7 +284,7 @@ subtest 'Status Graph tests' => sub {
             name           => 'Requested',
             ui_method_name => 'Confirm request',
             method         => 'confirm',
-            next_actions   => [ 'REQREV', 'COMP' ],
+            next_actions   => [ 'REQREV', 'COMP', 'CHK' ],
             ui_method_icon => 'fa-check',
         },
         GENREQ => {
@@ -273,7 +293,7 @@ subtest 'Status Graph tests' => sub {
             name           => 'Requested from partners',
             ui_method_name => 'Place request with partners',
             method         => 'generic_confirm',
-            next_actions   => [ 'COMP' ],
+            next_actions   => [ 'COMP', 'CHK' ],
             ui_method_icon => 'fa-send-o',
         },
         REQREV => {
@@ -285,11 +305,6 @@ subtest 'Status Graph tests' => sub {
             next_actions   => [ 'REQ', 'GENREQ', 'KILL' ],
             ui_method_icon => 'fa-times',
         },
-        TEST => {
-            prev_actions   => [ ],
-            id             => 'TEST',
-            next_actions   => [ ],
-        },
         QUEUED => {
             prev_actions   => [ ],
             id             => 'QUEUED',
@@ -314,7 +329,7 @@ subtest 'Status Graph tests' => sub {
             name           => 'Completed',
             ui_method_name => 'Mark completed',
             method         => 'mark_completed',
-            next_actions   => [ ],
+            next_actions   => [ 'CHK' ],
             ui_method_icon => 'fa-check',
         },
         KILL => {
@@ -326,6 +341,27 @@ subtest 'Status Graph tests' => sub {
             next_actions   => [ ],
             ui_method_icon => 'fa-trash',
         },
+        CHK => {
+            prev_actions   => [ 'REQ', 'GENREQ', 'COMP' ],
+            id             => 'CHK',
+            name           => 'Checked out',
+            ui_method_name => 'Check out',
+            needs_prefs    => [ 'CirculateILL' ],
+            needs_perms    => [ 'user_circulate_circulate_remaining_permissions' ],
+            needs_all      => ignore(),
+            method         => 'check_out',
+            next_actions   => [ ],
+            ui_method_icon => 'fa-upload',
+        },
+        RET => {
+            prev_actions   => [ 'CHK' ],
+            id             => 'RET',
+            name           => 'Returned to library',
+            ui_method_name => 'Check in',
+            method         => 'check_in',
+            next_actions   => [ 'COMP' ],
+            ui_method_icon => 'fa-download',
+        }
     },
         "new node + core_status_graph = bigger status graph"
     ) || diag explain $new_graph;
@@ -778,13 +814,18 @@ subtest 'Censorship' => sub {
 
 subtest 'Checking out' => sub {
 
-    plan tests => 16;
+    plan tests => 17;
 
     $schema->storage->txn_begin;
 
-    my $itemtype = $builder->build_object({ class => 'Koha::ItemTypes' });
+    my $itemtype = $builder->build_object({
+        class => 'Koha::ItemTypes',
+        value => {
+            notforloan => 1
+        }
+    });
     my $library = $builder->build_object({ class => 'Koha::Libraries' });
-    my $biblio = $builder->build_object({ class => 'Koha::Biblios' });
+    my $biblio = $builder->build_sample_biblio();
     my $patron = $builder->build_object({
         class => 'Koha::Patrons',
         value => { category_type => 'x' }
@@ -827,20 +868,8 @@ subtest 'Checking out' => sub {
         inhouse => 1
     });
     # Too many items attached to biblio
-    my $item1 = $builder->build_object({
-        class => 'Koha::Items',
-        value => {
-            biblionumber => $biblio->biblionumber,
-            biblioitemnumber => 1
-        }
-    });
-    my $item2 = $builder->build_object({
-        class => 'Koha::Items',
-        value => {
-            biblionumber => $biblio->biblionumber,
-            biblioitemnumber => 2
-        }
-    });
+    my $item1 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
+    my $item2 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
     my $form_stage_two_items = $request->check_out({
         stage     => 'form',
         item_type => $itemtype->itemtype,
@@ -849,29 +878,30 @@ subtest 'Checking out' => sub {
         itemcount => 1
     });
 
-    # Passed validation
-    #
     # Delete the items we created, so we can test that we can create one
-    Koha::Items->find({ itemnumber => $item1->itemnumber })->delete;
-    Koha::Items->find({ itemnumber => $item2->itemnumber })->delete;
-    # Create a biblioitem
-    my $biblioitem = $builder->build_object({
-        class => 'Koha::Biblioitems',
-        value => {
-            biblionumber => $biblio->biblionumber
-        }
-    });
+    $item1->delete;
+    $item2->delete;
+
+    # We need to mock the user environment for AddIssue
+    t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
+    #
+
     # First we pass bad parameters to the item creation to test we're
     # catching the failure of item creation
-    # Note: This will generate a DBD::mysql error when running this test!
-    my $form_stage_bad_branchcode = $request->check_out({
-        stage     => 'form',
-        item_type => $itemtype->itemtype,
-        branchcode => '---'
-    });
+    my $form_stage_bad_branchcode;
+    warning_like {
+        $form_stage_bad_branchcode = $request->check_out({
+            stage     => 'form',
+            item_type => $itemtype->itemtype,
+            branchcode => '---'
+        });
+    } qr/DBD::mysql::st execute failed: Cannot add or update a child row: a foreign key constraint fails/,
+    "Item creation fails on bad parameters";
+
     is_deeply($form_stage_bad_branchcode->{value}->{errors}, {
         item_creation => 1
-    });
+    },"We get expected failure of item creation");
+
     # Now create a proper item
     my $form_stage_good_branchcode = $request->check_out({
         stage      => 'form',
@@ -887,7 +917,8 @@ subtest 'Checking out' => sub {
                 NOT_FOR_LOAN => 1,
                 itemtype_notforloan => $itemtype->itemtype
             }
-        }
+        },
+        "We get expected error on notforloan of item"
     );
     # Delete the item that was created
     $biblio->items->delete;
@@ -899,7 +930,7 @@ subtest 'Checking out' => sub {
         }
     });
     # We need to mock the user environment for AddIssue
-    t::lib::Mocks::mock_userenv({ branchcode => $library->{branchcode} });
+    t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
     my $form_stage_loanable = $request->check_out({
         stage      => 'form',
         item_type  => $itemtype_loanable->itemtype,
@@ -1132,3 +1163,62 @@ subtest 'Custom statuses' => sub {
 
     $schema->storage->txn_rollback;
 };
+
+subtest 'Checking in hook' => sub {
+
+    plan tests => 2;
+
+    $schema->storage->txn_begin;
+
+    # Build infrastructure
+    my $backend = Test::MockObject->new;
+    $backend->set_isa('Koha::Illbackends::Mock');
+    $backend->set_always('name', 'Mock');
+
+    my $config = Test::MockObject->new;
+    $config->set_always('backend_dir', "/tmp");
+
+    my $item   = $builder->build_sample_item();
+    my $patron = $builder->build_object({ class => 'Koha::Patrons' });
+
+    t::lib::Mocks::mock_userenv(
+        {
+            patron     => $patron,
+            branchcode => $patron->branchcode
+        }
+    );
+
+    my $illrq = $builder->build_object(
+        {
+            class => 'Koha::Illrequests',
+            value => {
+                biblio_id => $item->biblio->biblionumber,
+                status    => 'NEW'
+            }
+        }
+    );
+
+    $illrq->_config($config);
+    $illrq->_backend($backend);
+
+    t::lib::Mocks::mock_preference('CirculateILL', 1);
+
+    # Add an issue
+    AddIssue( $patron->unblessed, $item->barcode );
+    # Make the item withdrawn so checking-in is rejected
+    t::lib::Mocks::mock_preference('BlockReturnOfWithdrawnItems', 1);
+    $item->set({ withdrawn => 1 })->store;
+    AddReturn( $item->barcode, $patron->branchcode );
+    # refresh request
+    $illrq->discard_changes;
+    isnt( $illrq->status, 'RET' );
+
+    # allow the check-in
+    $item->set({ withdrawn => 0 })->store;
+    AddReturn( $item->barcode, $patron->branchcode );
+    # refresh request
+    $illrq->discard_changes;
+    is( $illrq->status, 'RET' );
+
+    $schema->storage->txn_rollback;
+};