View | Details | Raw Unified | Return to bug 26515
Collapse All | Expand All

(-)a/t/db_dependent/Koha/Acquisition/Order.t (-2 / +110 lines)
Lines 19-31 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 11;
22
use Test::More tests => 12;
23
use Test::Exception;
23
24
24
use t::lib::TestBuilder;
25
use t::lib::TestBuilder;
25
use t::lib::Mocks;
26
use t::lib::Mocks;
26
27
28
use C4::Circulation;
29
30
use Koha::Biblios;
27
use Koha::Database;
31
use Koha::Database;
28
use Koha::DateUtils qw(dt_from_string);
32
use Koha::DateUtils qw(dt_from_string);
33
use Koha::Items;
29
34
30
my $schema  = Koha::Database->schema;
35
my $schema  = Koha::Database->schema;
31
my $builder = t::lib::TestBuilder->new;
36
my $builder = t::lib::TestBuilder->new;
Lines 582-586 subtest 'filter_by_current & filter_by_cancelled' => sub { Link Here
582
    is( $orders->filter_by_cancelled->count, 1);
587
    is( $orders->filter_by_cancelled->count, 1);
583
588
584
589
590
    $schema->storage->txn_rollback;
591
};
592
593
subtest 'cancel() tests' => sub {
594
595
    plan tests => 15;
596
597
    $schema->storage->txn_begin;
598
599
    # Scenario:
600
    # * order with one item attached
601
    # * the item is on loan
602
    # => order is not cancelled
603
    # => item in order is not removed
604
    # => exception is thrown
605
606
    my $item      = $builder->build_sample_item;
607
    my $biblio_id = $item->biblio->id;
608
    my $order     = $builder->build_object(
609
        {
610
            class => 'Koha::Acquisition::Orders',
611
            value => {
612
                orderstatus             => 'new',
613
                biblionumber            => $item->biblio->id,
614
                datecancellationprinted => undef,
615
                cancellationreason      => undef,
616
            }
617
        }
618
    );
619
    $order->add_item( $item->id );
620
621
    my $patron = $builder->build_object({ class => 'Koha::Patrons' });
622
    t::lib::Mocks::mock_userenv(
623
        { patron => $patron, branchcode => $patron->branchcode } );
624
625
    # Add a checkout so cancelling fails because od 'book_on_loan'
626
    C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
627
628
    throws_ok
629
        { $order->cancel({ reason => 'Some reason' }); }
630
        'Koha::Exceptions::Object::CannotBeDeleted',
631
        'An exception is thrown';
632
633
    like(
634
        "$@",
635
        qr/Cannot delete Koha::Item=HASH\(.*\) object \(id=\d*\). Reason: book_on_loan/,
636
        'The exception is correct, item on loan'
637
    );
638
639
    # refresh the order object
640
    $order->discard_changes;
641
642
    isnt( $order->orderstatus, 'cancelled', 'Order is not marked as cancelled' );
643
    is( $order->datecancellationprinted, undef, 'datecancellationprinted is undef' );
644
    is( $order->cancellationreason, undef, 'cancellationreason is undef' );
645
    is( ref(Koha::Items->find($item->id)), 'Koha::Item', 'The item is present' );
646
647
    # Scenario:
648
    # * order with one item attached
649
    # * the item is no longer on loan
650
    # => order is cancelled
651
    # => item in order is removed
652
653
    C4::Circulation::AddReturn( $item->barcode );
654
655
    $order->cancel({ reason => 'Some reason' })
656
          ->discard_changes;
657
658
    is( $order->orderstatus, 'cancelled', 'Order is marked as cancelled' );
659
    isnt( $order->datecancellationprinted, undef, 'datecancellationprinted is set' );
660
    is( $order->cancellationreason, 'Some reason', 'cancellationreason is undef' );
661
    is( Koha::Items->find($item->id), undef, 'The item is no longer present' );
662
663
    # Scenario:
664
    # * order with one item attached
665
    # => order is cancelled
666
    # => item in order is removed
667
    # => the extra item remains untouched
668
669
    my $item_1 = $builder->build_sample_item;
670
    $biblio_id = $item_1->biblio->id;
671
    my $item_2 = $builder->build_sample_item({ biblionumber => $biblio_id });
672
    $order     = $builder->build_object(
673
        {
674
            class => 'Koha::Acquisition::Orders',
675
            value => {
676
                orderstatus             => 'new',
677
                biblionumber            => $biblio_id,
678
                datecancellationprinted => undef,
679
                cancellationreason      => undef,
680
            }
681
        }
682
    );
683
    $order->add_item( $item_1->id );
684
685
    $order->cancel({ reason => 'Some reason' })
686
          ->discard_changes;
687
688
    is( $order->orderstatus, 'cancelled', 'Order is marked as cancelled' );
689
    isnt( $order->datecancellationprinted, undef, 'datecancellationprinted is set' );
690
    is( $order->cancellationreason, 'Some reason', 'cancellationreason is undef' );
691
    is( Koha::Items->find($item_1->id), undef, 'The item is no longer present' );
692
    is( ref(Koha::Items->find($item_2->id)), 'Koha::Item', 'The item is still present' );
693
585
    $schema->storage->txn_rollback;
694
    $schema->storage->txn_rollback;
586
};
695
};
587
- 

Return to bug 26515