Bug 32799: Rename ILLSTATUS authorised value category to ILL_STATUS_ALIAS
[srvgit] / t / db_dependent / Illrequests.t
index 0ed9821..65c1bc6 100755 (executable)
@@ -19,7 +19,7 @@ use Modern::Perl;
 
 use File::Basename qw/basename/;
 
-use C4::Circulation qw(AddIssue AddReturn);
+use C4::Circulation qw( AddIssue AddReturn );
 
 use Koha::Database;
 use Koha::Illrequestattributes;
@@ -41,7 +41,7 @@ use Test::Exception;
 use Test::Deep qw/ cmp_deeply ignore /;
 use Test::Warn;
 
-use Test::More tests => 13;
+use Test::More tests => 15;
 
 my $schema = Koha::Database->new->schema;
 my $builder = t::lib::TestBuilder->new;
@@ -125,6 +125,70 @@ subtest 'Basic object tests' => sub {
     $schema->storage->txn_rollback;
 };
 
+subtest 'store borrowernumber change also updates holds' => sub {
+    plan tests => 5;
+
+    $schema->storage->txn_begin;
+
+    Koha::Illrequests->search->delete;
+
+    my $patron = $builder->build_object({ class => 'Koha::Patrons' });
+    my $other_patron = $builder->build_object({ class => 'Koha::Patrons' });
+    my $biblio = $builder->build_object({ class => 'Koha::Biblios' });
+
+    my $request = $builder->build_object({
+        class => 'Koha::Illrequests',
+        value => {
+            borrowernumber => $patron->borrowernumber,
+            biblio_id => $biblio->biblionumber,
+        }
+    });
+    $builder->build({
+        source => 'Reserve',
+        value => {
+            borrowernumber => $patron->borrowernumber,
+            biblionumber => $request->biblio_id
+        }
+    });
+
+    my $hold = Koha::Holds->find({
+        biblionumber => $request->biblio_id,
+        borrowernumber => $request->borrowernumber,
+    });
+
+    is( $hold->borrowernumber, $request->borrowernumber,
+       'before change, original borrowernumber found' );
+
+    $request->borrowernumber( $other_patron->borrowernumber )->store;
+
+    # reload changes
+    $hold->discard_changes;
+
+    is( $hold->borrowernumber, $other_patron->borrowernumber,
+       'after change, changed borrowernumber found in holds' );
+
+    is( $request->borrowernumber, $other_patron->borrowernumber,
+       'after change, changed borrowernumber found in illrequests' );
+
+    my $new_request = Koha::Illrequest->new({
+        biblio_id => $biblio->biblionumber,
+        branchcode => $patron->branchcode,
+    })->borrowernumber( $patron->borrowernumber )->store;
+
+    is( $new_request->borrowernumber, $patron->borrowernumber,
+       'Koha::Illrequest->new()->store() works as expected');
+
+    my $new_holds_found = Koha::Holds->search({
+        biblionumber => $new_request->biblio_id,
+        borrowernumber => $new_request->borrowernumber,
+    })->count;
+
+    is( $new_holds_found, 0, 'no holds found with new()->store()' );
+
+    $schema->storage->txn_rollback;
+
+};
+
 subtest 'Working with related objects' => sub {
 
     plan tests => 7;
@@ -190,7 +254,7 @@ subtest 'Working with related objects' => sub {
 
 subtest 'Status Graph tests' => sub {
 
-    plan tests => 5;
+    plan tests => 6;
 
     $schema->storage->txn_begin;
 
@@ -368,6 +432,125 @@ subtest 'Status Graph tests' => sub {
         "new node + core_status_graph = bigger status graph"
     ) || diag explain $new_graph;
 
+    # Create a duplicate node
+    my $dupe_node = {
+        REQ => {
+            prev_actions   => [ 'NEW', 'REQREV', 'QUEUED', 'CANCREQ' ],
+            id             => 'REQ',
+            name           => 'Requested',
+            ui_method_name => 'Confirm request dupe',
+            method         => 'confirm',
+            next_actions   => [ 'REQREV', 'COMP', 'CHK' ],
+            ui_method_icon => 'fa-check',
+        }
+    };
+    # Add the dupe node to the core_status_grpah
+    my $dupe_graph = $illrq_obj->_status_graph_union( $illrq_obj->_core_status_graph, $dupe_node);
+    # 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
+    cmp_deeply( $dupe_graph,
+        {
+        NEW => {
+            prev_actions => [ ],                           # Actions containing buttons
+                                                           # leading to this status
+            id             => 'NEW',                       # ID of this status
+            name           => 'New request',               # UI name of this status
+            ui_method_name => 'New request',               # UI name of method leading
+                                                           # to this status
+            method         => 'create',                    # method to this status
+            next_actions   => [ 'REQ', 'GENREQ', 'KILL' ], # buttons to add to all
+                                                           # requests with this status
+            ui_method_icon => 'fa-plus',                   # UI Style class
+        },
+        REQ => {
+            prev_actions   => [ 'NEW', 'REQREV', 'QUEUED', 'CANCREQ' ],
+            id             => 'REQ',
+            name           => 'Requested',
+            ui_method_name => 'Confirm request dupe',
+            method         => 'confirm',
+            next_actions   => [ 'REQREV', 'COMP', 'CHK' ],
+            ui_method_icon => 'fa-check',
+        },
+        GENREQ => {
+            prev_actions   => [ 'NEW', 'REQREV' ],
+            id             => 'GENREQ',
+            name           => 'Requested from partners',
+            ui_method_name => 'Place request with partners',
+            method         => 'generic_confirm',
+            next_actions   => [ 'COMP', 'CHK' ],
+            ui_method_icon => 'fa-send-o',
+        },
+        REQREV => {
+            prev_actions   => [ 'REQ' ],
+            id             => 'REQREV',
+            name           => 'Request reverted',
+            ui_method_name => 'Revert Request',
+            method         => 'cancel',
+            next_actions   => [ 'REQ', 'GENREQ', 'KILL' ],
+            ui_method_icon => 'fa-times',
+        },
+        QUEUED => {
+            prev_actions   => [ ],
+            id             => 'QUEUED',
+            name           => 'Queued request',
+            ui_method_name => 0,
+            method         => 0,
+            next_actions   => [ 'REQ', 'KILL' ],
+            ui_method_icon => 0,
+        },
+        CANCREQ => {
+            prev_actions   => [ 'NEW' ],
+            id             => 'CANCREQ',
+            name           => 'Cancellation requested',
+            ui_method_name => 0,
+            method         => 0,
+            next_actions   => [ 'KILL', 'REQ' ],
+            ui_method_icon => 0,
+        },
+        COMP => {
+            prev_actions   => [ 'REQ' ],
+            id             => 'COMP',
+            name           => 'Completed',
+            ui_method_name => 'Mark completed',
+            method         => 'mark_completed',
+            next_actions   => [ 'CHK' ],
+            ui_method_icon => 'fa-check',
+        },
+        KILL => {
+            prev_actions   => [ 'QUEUED', 'REQREV', 'NEW', 'CANCREQ' ],
+            id             => 'KILL',
+            name           => 0,
+            ui_method_name => 'Delete request',
+            method         => 'delete',
+            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 $dupe_graph;
+
     $schema->storage->txn_rollback;
 };
 
@@ -517,7 +700,7 @@ subtest 'Backend testing (mocks)' => sub {
 
 subtest 'Backend core methods' => sub {
 
-    plan tests => 18;
+    plan tests => 20;
 
     $schema->storage->txn_begin;
 
@@ -655,6 +838,11 @@ subtest 'Backend core methods' => sub {
               },
               "Backend cancel: arbitrary stage.");
 
+    # backend_illview
+    $backend->set_series('illview', { stage => '', method => 'illview' });
+    is_deeply($illrq->backend_illview({test => 1}), 0,
+              "Backend illview optional method.");
+
     # backend_update_status
     $backend->set_series('update_status', { stage => 'bar', method => 'update_status' });
     is_deeply($illrq->backend_update_status({test => 1}),
@@ -675,6 +863,18 @@ subtest 'Backend core methods' => sub {
               },
               "Backend confirm: arbitrary stage.");
 
+    # backend_get_update
+    $backend->mock(
+        'get_supplier_update',
+        sub {
+            my ( $self, $options ) = @_;
+            return $options;
+        }
+    );
+    $backend->mock('capabilities', sub { return sub { return 1; } });
+    is_deeply($illrq->backend_get_update({}), 1,
+              "Backend get_update method.");
+
     $config->set_always('partner_code', "ILLTSTLIB");
     $backend->set_always('metadata', { Test => "Foobar" });
     my $illbrn = $builder->build({
@@ -714,7 +914,7 @@ subtest 'Backend core methods' => sub {
 
 subtest 'Helpers' => sub {
 
-    plan tests => 20;
+    plan tests => 25;
 
     $schema->storage->txn_begin;
 
@@ -758,6 +958,30 @@ subtest 'Helpers' => sub {
     $illrq_obj->_config($config);
     $illrq_obj->_backend($backend);
 
+    #attach_processors
+    my $type = 'test_type_1';
+    my $name = 'test_name_1';
+    my $update = Test::MockObject->new;
+    $update->set_isa('Koha::Illrequest::SupplierUpdate');
+    $update->{source_type} = $type;
+    $update->{source_name} = $name;
+    $update->{processors} = [];
+    $update->mock('attach_processor', sub {
+        my ( $self, $to_attach ) = @_;
+        push @{$self->{processors}}, $to_attach;
+    });
+    my $processor = Test::MockObject->new;
+    $processor->{target_source_type} = $type;
+    $processor->{target_source_name} = $name;
+    $illrq_obj->init_processors();
+    $illrq_obj->push_processor($processor);
+    $illrq_obj->attach_processors($update);
+    is_deeply(
+        scalar @{$update->{processors}},
+        1,
+        'attaching processors as appropriate works'
+    );
+
     # getPrefix
     $config->set_series('getPrefixes',
                         { HDE => "TEST", TSL => "BAR", default => "DEFAULT" },
@@ -809,6 +1033,27 @@ subtest 'Helpers' => sub {
     );
     is($notice, 'ILL_PICKUP_READY' ,"Notice is correctly created");
 
+    # ill update notice, passes additional text parameter
+    my $attr_update = Koha::MessageAttributes->find({ message_name => 'Ill_update' });
+    C4::Members::Messaging::SetMessagingPreference({
+        borrowernumber => $patron->{borrowernumber},
+        message_attribute_id => $attr_update->message_attribute_id,
+        message_transport_types => ['email']
+    });
+    my $return_patron_update = $illrq_obj->send_patron_notice('ILL_REQUEST_UPDATE', 'Some additional text');
+    my $notice_update = $schema->resultset('MessageQueue')->search({
+            letter_code => 'ILL_REQUEST_UPDATE',
+            message_transport_type => 'email',
+            borrowernumber => $illrq_obj->borrowernumber
+        })->next()->letter_code;
+    is_deeply(
+        $return_patron_update,
+        { result => { success => ['email'], fail => [] } },
+        "Correct return when notice created"
+    );
+    is($notice_update, 'ILL_REQUEST_UPDATE' ,"Notice is correctly created");
+
+
     my $return_patron_fail = $illrq_obj->send_patron_notice();
     is_deeply(
         $return_patron_fail,
@@ -882,6 +1127,19 @@ subtest 'Helpers' => sub {
         $not->{title} eq 'Interlibrary loan request cancelled',
         'Correct title return from get_notice'
     );
+    $not->{content} =~ s/\s//g;
+
+    is(
+        $not->{content},"Thepatronforinterlibraryloansrequest" . $illrq_obj->id . ",withthefollowingdetails,hasrequestedcancellationofthisILLrequest:-author:myauthor-title:mytitle",
+        'Correct content returned from get_notice with metadata correctly ordered'
+    );
+
+    $illrq_obj->append_to_note('Some text');
+    like(
+        $illrq_obj->notesstaff,
+        qr/Some text$/,
+        'appending to a note works'
+    );
 
     $schema->storage->txn_rollback;
 };
@@ -935,10 +1193,7 @@ subtest 'Checking out' => sub {
     });
     my $library = $builder->build_object({ class => 'Koha::Libraries' });
     my $biblio = $builder->build_sample_biblio();
-    my $patron = $builder->build_object({
-        class => 'Koha::Patrons',
-        value => { category_type => 'x' }
-    });
+    my $patron = $builder->build_object({ class => 'Koha::Patrons' });
     my $request = $builder->build_object({
         class => 'Koha::Illrequests',
         value => {
@@ -1053,6 +1308,40 @@ subtest 'Checking out' => sub {
     $schema->storage->txn_rollback;
 };
 
+subtest 'Checking out with custom due date' => sub {
+    plan tests => 1;
+    $schema->storage->txn_begin;
+
+    my $library = $builder->build_object({ class => 'Koha::Libraries' });
+    my $patron = $builder->build_object({ class => 'Koha::Patrons' });
+    my $biblio = $builder->build_sample_biblio();
+    my $itemtype_loanable = $builder->build_object({
+        class => 'Koha::ItemTypes',
+        value => {
+            notforloan => 0
+        }
+    });
+    my $request = $builder->build_object({
+        class => 'Koha::Illrequests',
+        value => {
+            borrowernumber => $patron->borrowernumber,
+            biblio_id      => $biblio->biblionumber
+        }
+    });
+
+    t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
+    my $duedate = '2099-05-21 00:00:00';
+    my $form_stage_loanable = $request->check_out({
+        stage      => 'form',
+        item_type  => $itemtype_loanable->itemtype,
+        branchcode => $library->branchcode,
+        duedate    => $duedate
+    });
+    is($patron->checkouts->next->date_due, $duedate, "Custom due date was used");
+
+    $schema->storage->txn_rollback;
+};
+
 subtest 'Checking Limits' => sub {
 
     plan tests => 30;
@@ -1228,7 +1517,7 @@ subtest 'Custom statuses' => sub {
 
     my $cat = Koha::AuthorisedValueCategories->search(
         {
-            category_name => 'ILLSTATUS'
+            category_name => 'ILL_STATUS_ALIAS'
         }
     );
 
@@ -1237,7 +1526,7 @@ subtest 'Custom statuses' => sub {
             {
                 class => 'Koha::AuthorisedValueCategory',
                 value => {
-                    category_name => 'ILLSTATUS'
+                    category_name => 'ILL_STATUS_ALIAS'
                 }
             }
         );
@@ -1247,12 +1536,12 @@ subtest 'Custom statuses' => sub {
         {
             class => 'Koha::AuthorisedValues',
             value => {
-                category => 'ILLSTATUS'
+                category => 'ILL_STATUS_ALIAS'
             }
         }
     );
 
-    is($av->category, 'ILLSTATUS',
+    is($av->category, 'ILL_STATUS_ALIAS',
        "Successfully created authorised value for custom status");
 
     my $ill_req = $builder->build_object(