Bug 17600: Standardize our EXPORT_OK
[srvgit] / t / db_dependent / Koha / Acquisition / Basket.t
old mode 100644 (file)
new mode 100755 (executable)
index dfa7b6f..971752a
 
 use Modern::Perl;
 
-use Test::More tests => 6;
+use Test::More tests => 12;
+use Test::Exception;
+
 use t::lib::TestBuilder;
 use t::lib::Mocks;
 
-use C4::Acquisition;
+use C4::Acquisition qw( NewBasket ModBasket ModBasketHeader );
 use Koha::Database;
 use Koha::DateUtils qw(dt_from_string);
 
@@ -171,3 +173,211 @@ 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;
+};
+
+subtest 'orders' => sub {
+
+    plan tests => 4;
+
+    $schema->storage->txn_begin;
+
+    my $basket = $builder->build_object(
+        {
+            class => 'Koha::Acquisition::Baskets'
+        }
+    );
+
+    my $orders = $basket->orders;
+    is( ref($orders), 'Koha::Acquisition::Orders', 'Type is correct with no attached orders' );
+    is( $orders->count, 0, 'No orders attached, count 0' );
+
+    my @actual_orders;
+
+    for ( 1..3 ) {
+        push @actual_orders, $builder->build_object({ class => 'Koha::Acquisition::Orders', value => { basketno => $basket->id } });
+    }
+
+    $orders = $basket->orders;
+    is( ref($orders), 'Koha::Acquisition::Orders', 'Type is correct with no attached orders' );
+    is( $orders->count, 3, '3 orders attached, count 3' );
+
+    $schema->storage->txn_rollback;
+};
+
+subtest 'is_closed() tests' => sub {
+
+    plan tests => 2;
+
+    $schema->storage->txn_begin;
+
+    my $open_basket = $builder->build_object(
+        {
+            class => 'Koha::Acquisition::Baskets',
+            value => {
+                closedate => undef
+            }
+        }
+    );
+
+    my $closed_basket = $builder->build_object(
+        {
+            class => 'Koha::Acquisition::Baskets',
+            value => {
+                closedate => \'NOW()'
+            }
+        }
+    );
+
+    ok( $closed_basket->is_closed, 'Closed basket is tested as closed' );
+    ok( !$open_basket->is_closed, 'Open basket is tested as open' );
+
+    $schema->storage->txn_rollback;
+};
+
+subtest 'close() tests' => sub {
+
+    plan tests => 4;
+
+    # Turn on acquisitions logging and ensure the logs are empty
+    t::lib::Mocks::mock_preference('AcquisitionLog', 1);
+    Koha::ActionLogs->delete;
+
+    $schema->storage->txn_begin;
+
+    # Create an open basket
+    my $basket = $builder->build_object(
+        {
+            class => 'Koha::Acquisition::Baskets',
+            value => {
+                closedate => undef
+            }
+        }
+    );
+
+    for my $status ( qw( new ordered partial complete cancelled ) ) {
+        $builder->build_object(
+            {
+                class => 'Koha::Acquisition::Orders',
+                value => {
+                    basketno    => $basket->id,
+                    orderstatus => $status
+                }
+            }
+        );
+    }
+
+    $basket->close;
+
+    ok( $basket->is_closed, 'Basket is closed' );
+    my $ordered_orders = $basket->orders->search({ orderstatus => 'ordered' });
+    is( $ordered_orders->count, 3, 'Only open orders have been marked as ordered' );
+
+    throws_ok
+        { $basket->close; }
+        'Koha::Exceptions::Acquisition::Basket::AlreadyClosed',
+        'Trying to close an already closed basket throws an exception';
+
+    my @close_logs = Koha::ActionLogs->search({ module =>'ACQUISITIONS', action => 'CLOSE_BASKET', object => $basket->id });
+    is (scalar @close_logs, 1, 'Basket closure is logged');
+
+    $schema->storage->txn_rollback;
+};