Bug 28787: Send a notice with the TOTP token
[koha-ffzg.git] / t / db_dependent / Circulation.t
index e2a1d5d..3c69d68 100755 (executable)
@@ -18,7 +18,7 @@
 use Modern::Perl;
 use utf8;
 
-use Test::More tests => 61;
+use Test::More tests => 63;
 use Test::Exception;
 use Test::MockModule;
 use Test::Deep qw( cmp_deeply );
@@ -56,9 +56,11 @@ use Koha::ActionLogs;
 use Koha::Notice::Messages;
 use Koha::Cache::Memory::Lite;
 
+my $builder = t::lib::TestBuilder->new;
 sub set_userenv {
     my ( $library ) = @_;
-    t::lib::Mocks::mock_userenv({ branchcode => $library->{branchcode} });
+    my $staff = $builder->build_object({ class => "Koha::Patrons" });
+    t::lib::Mocks::mock_userenv({ patron => $staff, branchcode => $library->{branchcode} });
 }
 
 sub str {
@@ -105,7 +107,6 @@ sub test_debarment_on_checkout {
 
 my $schema = Koha::Database->schema;
 $schema->storage->txn_begin;
-my $builder = t::lib::TestBuilder->new;
 my $dbh = C4::Context->dbh;
 
 # Prevent random failures by mocking ->now
@@ -712,6 +713,9 @@ subtest "CanBookBeRenewed tests" => sub {
     # Make sure fine calculation isn't skipped when adding renewal
     t::lib::Mocks::mock_preference('CalculateFinesOnReturn', 1);
 
+    my $staff = $builder->build_object({ class => "Koha::Patrons" });
+    t::lib::Mocks::mock_userenv({ patron => $staff });
+
     t::lib::Mocks::mock_preference('RenewalLog', 0);
     my $date = output_pref( { dt => dt_from_string(), dateonly => 1, dateformat => 'iso' } );
     my %params_renewal = (
@@ -3940,26 +3944,34 @@ subtest 'ItemsDeniedRenewal preference' => sub {
         }
     });
     my $future = dt_from_string->add( days => 1 );
-    my $deny_issue = $builder->build_object({ class => 'Koha::Checkouts', value => {
-        returndate => undef,
-        renewals => 0,
-        auto_renew => 0,
-        borrowernumber => $idr_borrower->borrowernumber,
-        itemnumber => $deny_book->itemnumber,
-        onsite_checkout => 0,
-        date_due => $future,
+    my $deny_issue = $builder->build_object(
+        {
+            class => 'Koha::Checkouts',
+            value => {
+                returndate      => undef,
+                renewals_count  => 0,
+                auto_renew      => 0,
+                borrowernumber  => $idr_borrower->borrowernumber,
+                itemnumber      => $deny_book->itemnumber,
+                onsite_checkout => 0,
+                date_due        => $future,
+            }
         }
-    });
-    my $allow_issue = $builder->build_object({ class => 'Koha::Checkouts', value => {
-        returndate => undef,
-        renewals => 0,
-        auto_renew => 0,
-        borrowernumber => $idr_borrower->borrowernumber,
-        itemnumber => $allow_book->itemnumber,
-        onsite_checkout => 0,
-        date_due => $future,
+    );
+    my $allow_issue = $builder->build_object(
+        {
+            class => 'Koha::Checkouts',
+            value => {
+                returndate      => undef,
+                renewals_count  => 0,
+                auto_renew      => 0,
+                borrowernumber  => $idr_borrower->borrowernumber,
+                itemnumber      => $allow_book->itemnumber,
+                onsite_checkout => 0,
+                date_due        => $future,
+            }
         }
-    });
+    );
 
     my $idr_rules;
 
@@ -4286,6 +4298,25 @@ subtest 'AddReturn | recalls' => sub {
     $recall1->set_cancelled;
 };
 
+subtest 'AddReturn | bundles' => sub {
+    plan tests => 1;
+
+    my $schema = Koha::Database->schema;
+    $schema->storage->txn_begin;
+
+    my $patron1 = $builder->build_object({ class => 'Koha::Patrons' });
+    my $host_item1 = $builder->build_sample_item;
+    my $bundle_item1 = $builder->build_sample_item;
+    $schema->resultset('ItemBundle')
+      ->create(
+        { host => $host_item1->itemnumber, item => $bundle_item1->itemnumber } );
+
+    my ( $doreturn, $messages, $iteminfo, $borrowerinfo ) = AddReturn( $bundle_item1->barcode, $bundle_item1->homebranch );
+    is($messages->{InBundle}->id, $host_item1->id, 'AddReturn returns InBundle host item when item is part of a bundle');
+
+    $schema->storage->txn_rollback;
+};
+
 subtest 'AddRenewal and AddIssuingCharge tests' => sub {
 
     plan tests => 13;
@@ -4398,6 +4429,34 @@ subtest 'AddRenewal and AddIssuingCharge tests' => sub {
 
 };
 
+subtest 'AddRenewal() adds to renewals' => sub {
+    plan tests => 4;
+
+    my $library  = $builder->build_object({ class => 'Koha::Libraries' });
+    my $patron   = $builder->build_object({
+        class => 'Koha::Patrons',
+        value => { branchcode => $library->id }
+    });
+
+    my $item = $builder->build_sample_item();
+
+    set_userenv( $library->unblessed );
+
+    # Check the item out
+    my $issue = AddIssue( $patron->unblessed, $item->barcode );
+    is(ref($issue), 'Koha::Checkout', 'Issue added');
+
+    # Renew item
+    my $duedate = AddRenewal( $patron->id, $item->id, $library->id );
+
+    ok( $duedate, "Renewal added" );
+
+    my $renewals = Koha::Checkouts::Renewals->search({ checkout_id => $issue->issue_id });
+    is($renewals->count, 1, 'One renewal added');
+    my $THE_renewal = $renewals->next;
+    is( $THE_renewal->renewer_id, C4::Context->userenv->{'number'}, 'Renewer recorded from context' );
+};
+
 subtest 'ProcessOfflinePayment() tests' => sub {
 
     plan tests => 4;
@@ -5345,7 +5404,7 @@ subtest "updateWrongTransfer tests" => sub {
 };
 
 subtest "SendCirculationAlert" => sub {
-    plan tests => 2;
+    plan tests => 3;
 
     # When you would unsuspectingly call this unit test (with perl, not prove), you will be bitten by LOCK.
     # LOCK will commit changes and ruin your data
@@ -5358,7 +5417,7 @@ subtest "SendCirculationAlert" => sub {
     my $patron = $builder->build_object({ class => 'Koha::Patrons' });
     C4::Members::Messaging::SetMessagingPreference({
         borrowernumber => $patron->id,
-        message_transport_types => ['email'],
+        message_transport_types => ['sms'],
         message_attribute_id => 5
     });
     my $item = $builder->build_sample_item();
@@ -5371,7 +5430,7 @@ subtest "SendCirculationAlert" => sub {
             name => 'Test Checkin',
             is_html => 0,
             content => "Checkins:\n----\n[% biblio.title %]-[% old_checkout.issue_id %]\n----Thank you.",
-            message_transport_type => 'email',
+            message_transport_type => 'sms',
             lang => 'default'
         }
     })->store;
@@ -5388,6 +5447,7 @@ subtest "SendCirculationAlert" => sub {
     });
     my $notice = Koha::Notice::Messages->find({ borrowernumber => $patron->id, letter_code => 'CHECKIN' });
     is($notice->content,"Checkins:\n".$item->biblio->title."-".$issue_1->id."\nThank you.", 'Letter generated with expected output on first checkin' );
+    is($notice->to_address, $patron->smsalertnumber, "Letter has the correct to_address set to smsalertnumber for SMS type notices");
 
     # Checkout an item, mark it returned, generate a notice
     my $issue_2 = AddIssue( $patron->unblessed, $item->barcode);