From fb69ade38c4277a714dfe45f5bc7eb5da267fcc3 Mon Sep 17 00:00:00 2001
From: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Date: Wed, 8 Jan 2020 23:47:02 +0100
Subject: [PATCH] Bug 24161: Add tests

Sponsored-by: Cork Institute of Technology
---
 t/db_dependent/Koha/Acquisition/Basket.t | 102 ++++++++++++++++-
 t/db_dependent/Koha/Acquisition/Order.t  | 183 ++++++++++++++++++++++++++++++-
 2 files changed, 283 insertions(+), 2 deletions(-)

diff --git a/t/db_dependent/Koha/Acquisition/Basket.t b/t/db_dependent/Koha/Acquisition/Basket.t
index 2bcf481b88..74f010f13c 100644
--- a/t/db_dependent/Koha/Acquisition/Basket.t
+++ b/t/db_dependent/Koha/Acquisition/Basket.t
@@ -19,7 +19,7 @@
 
 use Modern::Perl;
 
-use Test::More tests => 5;
+use Test::More tests => 8;
 use t::lib::TestBuilder;
 use t::lib::Mocks;
 
@@ -141,3 +141,103 @@ subtest 'to_api() tests' => sub {
 
     $schema->storage->txn_rollback;
 };
+
+subtest 'estimated_delivery_date' => sub {
+    plan tests => 4;
+
+    $schema->storage->txn_begin;
+    my $bookseller = $builder->build_object(
+        {
+            class => 'Koha::Acquisition::Booksellers',
+            value => {
+                deliverytime => undef,   # Does not have a delivery time defined
+            }
+        }
+    );
+
+    my $basket = $builder->build_object(
+        {
+            class => 'Koha::Acquisition::Baskets',
+            value => {
+                booksellerid => $bookseller->id,
+                closedate    => undef,             # Still open
+            }
+        }
+    );
+
+    my $now = dt_from_string;
+    is( $basket->estimated_delivery_date,
+        undef, 'return undef if closedate and deliverytime are not defined' );
+
+    $basket->closedate( $now->clone->subtract( days => 1 ) )
+      ->store;                                     #Closing the basket
+    is( $basket->estimated_delivery_date,
+        undef, 'return undef if deliverytime is not defined' );
+
+    $basket->closedate(undef)->store;              #Reopening
+    $bookseller->deliverytime(2)->store;           # 2 delivery days
+    is( $basket->estimated_delivery_date,
+        undef, 'return undef if closedate is not defined (basket stil open)' );
+
+    $bookseller->deliverytime(2)->store;           # 2 delivery days
+    $basket->closedate( $now->clone->subtract( days => 1 ) )->store; #Closing the basket
+    is(
+        $basket->get_from_storage->estimated_delivery_date,
+        $now->clone->add( days => 1 )->truncate( to => 'day' ),
+        'Estimated delivery date should be tomorrow if basket closed on yesterday and delivery takes 2 days'
+    );
+
+    $schema->storage->txn_rollback;
+};
+
+subtest 'late_since_days' => sub {
+    plan tests => 3;
+
+    $schema->storage->txn_begin;
+    my $basket  = $builder->build_object(
+        {
+            class => 'Koha::Acquisition::Baskets',
+        }
+    );
+
+    my $now = dt_from_string;
+    $basket->closedate(undef)->store; # Basket is open
+    is( $basket->late_since_days, undef, 'return undef if basket is still open');
+
+    $basket->closedate( $now )->store; #Closing the basket today
+    is( $basket->late_since_days, 0, 'return 0 if basket has been closed on today' );
+
+    $basket->closedate( $now->clone->subtract( days => 2 ) )->store;
+    is( $basket->late_since_days, 2, 'return 2 if basket has been closed 2 days ago' );
+
+    $schema->storage->txn_rollback;
+};
+
+subtest 'authorizer' => sub {
+    plan tests => 3;
+
+    $schema->storage->txn_begin;
+    my $basket = $builder->build_object(
+        {
+            class => 'Koha::Acquisition::Baskets',
+            value => { authorisedby => undef },
+        }
+    );
+
+    my $basket_creator = $builder->build_object( { class => 'Koha::Patrons' } );
+
+    is( $basket->authorizer, undef,
+        'authorisedby is null, ->authorized should return undef' );
+
+    $basket->authorisedby( $basket_creator->borrowernumber )->store;
+
+    is( ref( $basket->authorizer ),
+        'Koha::Patron', '->authorized should return a Koha::Patron object' );
+    is(
+        $basket->authorizer->borrowernumber,
+        $basket_creator->borrowernumber,
+        '->authorized should return the correct creator'
+    );
+
+    $schema->storage->txn_rollback;
+};
diff --git a/t/db_dependent/Koha/Acquisition/Order.t b/t/db_dependent/Koha/Acquisition/Order.t
index 5d2a1bd5ca..d18f7d9314 100644
--- a/t/db_dependent/Koha/Acquisition/Order.t
+++ b/t/db_dependent/Koha/Acquisition/Order.t
@@ -19,7 +19,7 @@
 
 use Modern::Perl;
 
-use Test::More tests => 8;
+use Test::More tests => 10;
 
 use t::lib::TestBuilder;
 use t::lib::Mocks;
@@ -341,3 +341,184 @@ subtest 'current_item_level_holds() tests' => sub {
 
     $schema->storage->txn_rollback;
 };
+
+subtest 'claim*' => sub {
+    plan tests => 6;
+
+    $schema->storage->txn_begin;
+    my $order = $builder->build_object(
+        {
+            class => 'Koha::Acquisition::Orders',
+        }
+    );
+
+    my $now = dt_from_string;
+    is( $order->claims->count, 0, 'No claim yet, ->claims should return an empty set');
+    is( $order->claims_count, 0, 'No claim yet, ->claims_count should return 0');
+    is( $order->claimed_date, undef, 'No claim yet, ->claimed_date should return undef');
+
+    my $claim_1 = $order->claim;
+    my $claim_2 = $order->claim;
+
+    $claim_1->claimed_on($now->clone->subtract(days => 1))->store;
+
+    is( $order->claims->count, 2, '->claims should return the correct number of claims');
+    is( $order->claims_count, 2, '->claims_count should return the correct number of claims');
+    is( dt_from_string($order->claimed_date), $now, '->claimed_date should return the date of the last claim');
+
+    $schema->storage->txn_rollback;
+};
+
+subtest 'filter_by_late' => sub {
+    plan tests => 16;
+
+    $schema->storage->txn_begin;
+    my $now        = dt_from_string;
+    my $bookseller = $builder->build_object(
+        {
+            class => 'Koha::Acquisition::Booksellers',
+            value => { deliverytime => 2 }
+        }
+    );
+    my $basket_1 = $builder->build_object(
+        {
+            class => 'Koha::Acquisition::Baskets',
+            value => {
+                booksellerid => $bookseller->id,
+                closedate    => undef,
+            }
+        }
+    );
+    my $order_1 = $builder->build_object(
+        {
+            class => 'Koha::Acquisition::Orders',
+            value => {
+                basketno                => $basket_1->basketno,
+                datereceived            => undef,
+                datecancellationprinted => undef,
+            }
+        }
+    );
+    my $basket_2 = $builder->build_object(    # expected tomorrow
+        {
+            class => 'Koha::Acquisition::Baskets',
+            value => {
+                booksellerid => $bookseller->id,
+                closedate    => $now->clone->subtract( days => 1 ),
+            }
+        }
+    );
+    my $order_2 = $builder->build_object(
+        {
+            class => 'Koha::Acquisition::Orders',
+            value => {
+                basketno                => $basket_2->basketno,
+                datereceived            => undef,
+                datecancellationprinted => undef,
+            }
+        }
+    );
+    my $basket_3 = $builder->build_object(    # expected yesterday (1 day)
+        {
+            class => 'Koha::Acquisition::Baskets',
+            value => {
+                booksellerid => $bookseller->id,
+                closedate    => $now->clone->subtract( days => 3 ),
+            }
+        }
+    );
+    my $order_3 = $builder->build_object(
+        {
+            class => 'Koha::Acquisition::Orders',
+            value => {
+                basketno                => $basket_3->basketno,
+                datereceived            => undef,
+                datecancellationprinted => undef,
+            }
+        }
+    );
+    my $basket_4 = $builder->build_object(    # expected 3 days ago
+        {
+            class => 'Koha::Acquisition::Baskets',
+            value => {
+                booksellerid => $bookseller->id,
+                closedate    => $now->clone->subtract( days => 5 ),
+            }
+        }
+    );
+    my $order_4 = $builder->build_object(
+        {
+            class => 'Koha::Acquisition::Orders',
+            value => {
+                basketno                => $basket_4->basketno,
+                datereceived            => undef,
+                datecancellationprinted => undef,
+            }
+        }
+    );
+
+    my $orders = Koha::Acquisition::Orders->search(
+        {
+            ordernumber => {
+                -in => [
+                    $order_1->ordernumber, $order_2->ordernumber,
+                    $order_3->ordernumber, $order_4->ordernumber,
+                ]
+            }
+        }
+    );
+
+    my $late_orders = $orders->filter_by_lates;
+    is( $late_orders->count, 3 );
+
+    $late_orders = $orders->filter_by_lates( { delay => 0 } );
+    is( $late_orders->count, 3 );
+
+    $late_orders = $orders->filter_by_lates( { delay => 1 } );
+    is( $late_orders->count, 3 );
+
+    $late_orders = $orders->filter_by_lates( { delay => 3 } );
+    is( $late_orders->count, 2 );
+
+    $late_orders = $orders->filter_by_lates( { delay => 4 } );
+    is( $late_orders->count, 1 );
+
+    $late_orders = $orders->filter_by_lates( { delay => 5 } );
+    is( $late_orders->count, 1 );
+
+    $late_orders = $orders->filter_by_lates( { delay => 6 } );
+    is( $late_orders->count, 0 );
+
+    $late_orders = $orders->filter_by_lates(
+        { estimated_from => $now->clone->subtract( days => 6 ) } );
+    is( $late_orders->count,             2 );
+    is( $late_orders->next->ordernumber, $order_3->ordernumber );
+
+    $late_orders = $orders->filter_by_lates(
+        { estimated_from => $now->clone->subtract( days => 5 ) } );
+    is( $late_orders->count,             2 );
+    is( $late_orders->next->ordernumber, $order_3->ordernumber );
+
+    $late_orders = $orders->filter_by_lates(
+        { estimated_from => $now->clone->subtract( days => 4 ) } );
+    is( $late_orders->count,             2 );
+    is( $late_orders->next->ordernumber, $order_3->ordernumber );
+
+    $late_orders = $orders->filter_by_lates(
+        { estimated_from => $now->clone->subtract( days => 3 ) } );
+    is( $late_orders->count, 2 );
+
+    $late_orders = $orders->filter_by_lates(
+        { estimated_from => $now->clone->subtract( days => 1 ) } );
+    is( $late_orders->count, 1 );
+
+    $late_orders = $orders->filter_by_lates(
+        {
+            estimated_from => $now->clone->subtract( days => 4 ),
+            estimated_to   => $now->clone->subtract( days => 3 )
+        }
+    );
+    is( $late_orders->count, 1 );
+
+    $schema->storage->txn_rollback;
+};
-- 
2.11.0