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

(-)a/t/db_dependent/Koha/Acquisition/Order.t (-2 / +151 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 => 23;
596
597
    $schema->storage->txn_begin;
598
599
    # Scenario:
600
    # * order with one item attached
601
    # * the item is on loan
602
    # * delete_biblio => 1 is passed
603
    # => order is not cancelled
604
    # => item in order is not removed
605
    # => biblio is kept
606
    # => exception is thrown
607
608
    my $item      = $builder->build_sample_item;
609
    my $biblio_id = $item->biblio->id;
610
    my $order     = $builder->build_object(
611
        {
612
            class => 'Koha::Acquisition::Orders',
613
            value => {
614
                orderstatus             => 'new',
615
                biblionumber            => $item->biblio->id,
616
                datecancellationprinted => undef,
617
                cancellationreason      => undef,
618
            }
619
        }
620
    );
621
    $order->add_item( $item->id );
622
623
    my $patron = $builder->build_object({ class => 'Koha::Patrons' });
624
    t::lib::Mocks::mock_userenv(
625
        { patron => $patron, branchcode => $patron->branchcode } );
626
627
    # Add a checkout so cancelling fails because od 'book_on_loan'
628
    C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
629
630
    throws_ok
631
        { $order->cancel({ delete_biblio => 1, reason => 'Some reason' }); }
632
        'Koha::Exceptions::Object::CannotBeDeleted',
633
        'An exception is thrown';
634
635
    like(
636
        "$@",
637
        qr/Cannot delete Koha::Item=HASH\(.*\) object \(id=\d*\). Reason: book_on_loan/,
638
        'The exception is correct, item on loan'
639
    );
640
641
    # refresh the order object
642
    $order->discard_changes;
643
644
    isnt( $order->orderstatus, 'cancelled', 'Order is not marked as cancelled' );
645
    is( $order->datecancellationprinted, undef, 'datecancellationprinted is undef' );
646
    is( $order->cancellationreason, undef, 'cancellationreason is undef' );
647
    is( ref(Koha::Items->find($item->id)), 'Koha::Item', 'The item is present' );
648
    is( ref(Koha::Biblios->find($biblio_id)), 'Koha::Biblio', 'The biblio is present' );
649
650
    # Scenario:
651
    # * order with one item attached
652
    # * the item is no longer on loan
653
    # * delete_biblio => 1 is passed
654
    # => order is cancelled
655
    # => item in order is removed
656
    # => biblio is removed because delete_biblio => 1 is passed
657
658
    C4::Circulation::AddReturn( $item->barcode );
659
660
    $order->cancel({ delete_biblio => 1, reason => 'Some reason' })
661
          ->discard_changes;
662
663
    is( $order->orderstatus, 'cancelled', 'Order is marked as cancelled' );
664
    isnt( $order->datecancellationprinted, undef, 'datecancellationprinted is set' );
665
    is( $order->cancellationreason, 'Some reason', 'cancellationreason is undef' );
666
    is( Koha::Items->find($item->id), undef, 'The item is no longer present' );
667
    is( Koha::Biblios->find($biblio_id), undef, 'The biblio is no longer present' );
668
669
    # Scenario:
670
    # * order with one item attached
671
    # * biblio has one more item
672
    # * delete_biblio => 1 is passed
673
    # => order is cancelled
674
    # => item in order is removed
675
    # => biblio is kept as there's a remaining item
676
    # => the extra item remains untouched
677
678
    my $item_1 = $builder->build_sample_item;
679
    $biblio_id = $item_1->biblio->id;
680
    my $item_2 = $builder->build_sample_item({ biblionumber => $biblio_id });
681
    $order     = $builder->build_object(
682
        {
683
            class => 'Koha::Acquisition::Orders',
684
            value => {
685
                orderstatus             => 'new',
686
                biblionumber            => $biblio_id,
687
                datecancellationprinted => undef,
688
                cancellationreason      => undef,
689
            }
690
        }
691
    );
692
    $order->add_item( $item_1->id );
693
694
    $order->cancel({ delete_biblio => 1, reason => 'Some reason' })
695
          ->discard_changes;
696
697
    is( $order->orderstatus, 'cancelled', 'Order is marked as cancelled' );
698
    isnt( $order->datecancellationprinted, undef, 'datecancellationprinted is set' );
699
    is( $order->cancellationreason, 'Some reason', 'cancellationreason is undef' );
700
    is( Koha::Items->find($item_1->id), undef, 'The item is no longer present' );
701
    is( ref(Koha::Items->find($item_2->id)), 'Koha::Item', 'The item is still present' );
702
    is( ref(Koha::Biblios->find($biblio_id)), 'Koha::Biblio', 'The biblio is still present' );
703
704
    # Scenario:
705
    # * order with one item attached
706
    # * delete_biblio => 0 is passed
707
    # => order is cancelled
708
    # => item in order is removed
709
    # => biblio is kept because delete_biblio => 0 passed
710
711
    $item      = $builder->build_sample_item;
712
    $biblio_id = $item->biblio->id;
713
    $order     = $builder->build_object(
714
        {
715
            class => 'Koha::Acquisition::Orders',
716
            value => {
717
                orderstatus             => 'new',
718
                biblionumber            => $biblio_id,
719
                datecancellationprinted => undef,
720
                cancellationreason      => undef,
721
            }
722
        }
723
    );
724
    $order->add_item( $item->id );
725
726
    $order->cancel({ delete_biblio => 0, reason => 'Some reason' })
727
          ->discard_changes;
728
729
    is( $order->orderstatus, 'cancelled', 'Order is marked as cancelled' );
730
    isnt( $order->datecancellationprinted, undef, 'datecancellationprinted is set' );
731
    is( $order->cancellationreason, 'Some reason', 'cancellationreason is undef' );
732
    is( Koha::Items->find($item->id), undef, 'The item is no longer present' );
733
    is( ref(Koha::Biblios->find($biblio_id)), 'Koha::Biblio', 'The biblio is still present' );
734
585
    $schema->storage->txn_rollback;
735
    $schema->storage->txn_rollback;
586
};
736
};
587
- 

Return to bug 26515