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

(-)a/t/db_dependent/Koha/Acquisition/Order.t (-2 / +208 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 => 32;
596
597
    $schema->storage->txn_begin;
598
599
    # Scenario:
600
    # * order with one item attached
601
    # * the item is on loan
602
    # * delete_biblio is passed
603
    # => order is not cancelled
604
    # => item in order is not removed
605
    # => biblio in order is not removed
606
    # => message about not being able to delete
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
    )->reset_messages; # reset them as TestBuilder doesn't call new
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
    my $result = $order->cancel({ reason => 'Some reason' });
631
    # refresh the order object
632
    $order->discard_changes;
633
634
    is( $result, $order, 'self is returned' );
635
    is( $order->orderstatus, 'cancelled', 'Order is not marked as cancelled' );
636
    isnt( $order->datecancellationprinted, undef, 'datecancellationprinted is not undef' );
637
    is( $order->cancellationreason, 'Some reason', 'cancellationreason is set' );
638
    is( ref(Koha::Items->find($item->id)), 'Koha::Item', 'The item is present' );
639
    is( ref(Koha::Biblios->find($biblio_id)), 'Koha::Biblio', 'The biblio is present' );
640
    my @messages = @{ $order->messages };
641
    is( $messages[0]->message, 'error_delitem', 'An error message is attached to the order' );
642
643
    # Scenario:
644
    # * order with one item attached
645
    # * the item is no longer on loan
646
    # * delete_biblio not passed
647
    # => order is cancelled
648
    # => item in order is removed
649
    # => biblio remains untouched
650
651
    C4::Circulation::AddReturn( $item->barcode );
652
653
    $order->reset_messages;
654
    $order->cancel({ reason => 'Some reason' })
655
          ->discard_changes;
656
657
    is( $order->orderstatus, 'cancelled', 'Order is marked as cancelled' );
658
    isnt( $order->datecancellationprinted, undef, 'datecancellationprinted is set' );
659
    is( $order->cancellationreason, 'Some reason', 'cancellationreason is undef' );
660
    is( Koha::Items->find($item->id), undef, 'The item is no longer present' );
661
    is( ref(Koha::Biblios->find($biblio_id)), 'Koha::Biblio', 'The biblio is present' );
662
    @messages = @{ $order->messages };
663
    is( scalar @messages, 0, 'No messages' );
664
665
    # Scenario:
666
    # * order with one item attached
667
    # * biblio has another item
668
    # => order is cancelled
669
    # => item in order is removed
670
    # => the extra item remains untouched
671
    # => biblio remains untouched
672
673
    my $item_1 = $builder->build_sample_item;
674
    $biblio_id = $item_1->biblio->id;
675
    my $item_2 = $builder->build_sample_item({ biblionumber => $biblio_id });
676
    $order     = $builder->build_object(
677
        {
678
            class => 'Koha::Acquisition::Orders',
679
            value => {
680
                orderstatus             => 'new',
681
                biblionumber            => $biblio_id,
682
                datecancellationprinted => undef,
683
                cancellationreason      => undef,
684
            }
685
        }
686
    )->reset_messages;
687
    $order->add_item( $item_1->id );
688
689
    $order->cancel({ reason => 'Some reason', delete_biblio => 1 })
690
          ->discard_changes;
691
692
    is( $order->orderstatus, 'cancelled', 'Order is marked as cancelled' );
693
    isnt( $order->datecancellationprinted, undef, 'datecancellationprinted is set' );
694
    is( $order->cancellationreason, 'Some reason', 'cancellationreason is undef' );
695
    is( Koha::Items->find($item_1->id), undef, 'The item is no longer present' );
696
    is( ref(Koha::Items->find($item_2->id)), 'Koha::Item', 'The item is still present' );
697
    is( ref(Koha::Biblios->find($biblio_id)), 'Koha::Biblio', 'The biblio is still present' );
698
    @messages = @{ $order->messages };
699
    is( $messages[0]->message, 'error_delbiblio', 'Cannot delete biblio and it gets notified' );
700
701
    # Scenario:
702
    # * order with one item attached
703
    # * there's another order pointing to the biblio
704
    # => order is cancelled
705
    # => item in order is removed
706
    # => biblio remains untouched
707
    # => biblio delete error notified
708
709
    $item      = $builder->build_sample_item;
710
    $biblio_id = $item->biblio->id;
711
    $order     = $builder->build_object(
712
        {
713
            class => 'Koha::Acquisition::Orders',
714
            value => {
715
                orderstatus             => 'new',
716
                biblionumber            => $biblio_id,
717
                datecancellationprinted => undef,
718
                cancellationreason      => undef,
719
            }
720
        }
721
    )->reset_messages;
722
    $order->add_item( $item->id );
723
724
    # Add another order
725
    $builder->build_object(
726
        {
727
            class => 'Koha::Acquisition::Orders',
728
            value => {
729
                orderstatus             => 'new',
730
                biblionumber            => $biblio_id,
731
                datecancellationprinted => undef,
732
                cancellationreason      => undef,
733
            }
734
        }
735
    );
736
737
    $order->cancel({ reason => 'Some reason', delete_biblio => 1 })
738
          ->discard_changes;
739
740
    is( $order->orderstatus, 'cancelled', 'Order is marked as cancelled' );
741
    isnt( $order->datecancellationprinted, undef, 'datecancellationprinted is set' );
742
    is( $order->cancellationreason, 'Some reason', 'cancellationreason is undef' );
743
    is( Koha::Items->find($item->id), undef, 'The item is no longer present' );
744
    is( ref(Koha::Biblios->find($biblio_id)), 'Koha::Biblio', 'The biblio is still present' );
745
    @messages = @{ $order->messages };
746
    is( $messages[0]->message, 'error_delbiblio', 'Cannot delete biblio and it gets notified' );
747
748
    # Scenario:
749
    # * order with one item attached
750
    # * there's a subscription on the biblio
751
    # => order is cancelled
752
    # => item in order is removed
753
    # => biblio remains untouched
754
    # => biblio delete error notified
755
756
    $item      = $builder->build_sample_item;
757
    $biblio_id = $item->biblio->id;
758
    $order     = $builder->build_object(
759
        {
760
            class => 'Koha::Acquisition::Orders',
761
            value => {
762
                orderstatus             => 'new',
763
                biblionumber            => $biblio_id,
764
                datecancellationprinted => undef,
765
                cancellationreason      => undef,
766
            }
767
        }
768
    )->reset_messages;
769
    $order->add_item( $item->id );
770
771
    # Add a subscription
772
    $builder->build_object(
773
        {
774
            class => 'Koha::Subscriptions',
775
            value => {
776
                biblionumber => $biblio_id,
777
            }
778
        }
779
    );
780
781
    $order->cancel({ reason => 'Some reason', delete_biblio => 1 })
782
          ->discard_changes;
783
784
    is( $order->orderstatus, 'cancelled', 'Order is marked as cancelled' );
785
    isnt( $order->datecancellationprinted, undef, 'datecancellationprinted is set' );
786
    is( $order->cancellationreason, 'Some reason', 'cancellationreason is undef' );
787
    is( Koha::Items->find($item->id), undef, 'The item is no longer present' );
788
    is( ref(Koha::Biblios->find($biblio_id)), 'Koha::Biblio', 'The biblio is still present' );
789
    @messages = @{ $order->messages };
790
    is( $messages[0]->message, 'error_delbiblio', 'Cannot delete biblio and it gets notified' );
791
585
    $schema->storage->txn_rollback;
792
    $schema->storage->txn_rollback;
586
};
793
};
587
- 

Return to bug 26515