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

(-)a/t/db_dependent/Illrequests.t (-16 / +173 lines)
Lines 22-27 use Koha::Database; Link Here
22
use Koha::Illrequestattributes;
22
use Koha::Illrequestattributes;
23
use Koha::Illrequest::Config;
23
use Koha::Illrequest::Config;
24
use Koha::Patrons;
24
use Koha::Patrons;
25
use Koha::MessageAttributes;
26
use Koha::MessageAttribute;
27
use Koha::Notice::Templates;
25
use Koha::AuthorisedValueCategories;
28
use Koha::AuthorisedValueCategories;
26
use Koha::AuthorisedValues;
29
use Koha::AuthorisedValues;
27
use t::lib::Mocks;
30
use t::lib::Mocks;
Lines 476-482 subtest 'Backend testing (mocks)' => sub { Link Here
476
479
477
subtest 'Backend core methods' => sub {
480
subtest 'Backend core methods' => sub {
478
481
479
    plan tests => 19;
482
    plan tests => 18;
480
483
481
    $schema->storage->txn_begin;
484
    $schema->storage->txn_begin;
482
485
Lines 667-686 subtest 'Backend core methods' => sub { Link Here
667
    }) }
670
    }) }
668
        "Generic confirm: missing to dies OK.";
671
        "Generic confirm: missing to dies OK.";
669
672
670
    dies_ok { $illrq->generic_confirm({
671
        current_branchcode => $illbrn->{branchcode},
672
        partners => $partner1->{email},
673
        stage => 'draft'
674
    }) }
675
        "Generic confirm: missing from dies OK.";
676
677
    $schema->storage->txn_rollback;
673
    $schema->storage->txn_rollback;
678
};
674
};
679
675
680
676
681
subtest 'Helpers' => sub {
677
subtest 'Helpers' => sub {
682
678
683
    plan tests => 7;
679
    plan tests => 21;
684
680
685
    $schema->storage->txn_begin;
681
    $schema->storage->txn_begin;
686
682
Lines 688-693 subtest 'Helpers' => sub { Link Here
688
    my $backend = Test::MockObject->new;
684
    my $backend = Test::MockObject->new;
689
    $backend->set_isa('Koha::Illbackends::Mock');
685
    $backend->set_isa('Koha::Illbackends::Mock');
690
    $backend->set_always('name', 'Mock');
686
    $backend->set_always('name', 'Mock');
687
    $backend->mock(
688
        'metadata',
689
        sub {
690
            my ( $self, $rq ) = @_;
691
            return {
692
                title => 'mytitle',
693
                author => 'myauthor'
694
            }
695
        }
696
    );
691
697
692
    my $config = Test::MockObject->new;
698
    my $config = Test::MockObject->new;
693
    $config->set_always('backend_dir', "/tmp");
699
    $config->set_always('backend_dir', "/tmp");
Lines 696-704 subtest 'Helpers' => sub { Link Here
696
        source => 'Borrower',
702
        source => 'Borrower',
697
        value => { categorycode => "A" }
703
        value => { categorycode => "A" }
698
    });
704
    });
705
    # Create a mocked branch with no email addressed defined
706
    my $illbrn = $builder->build({
707
        source => 'Branch',
708
        value => {
709
            branchcode => 'HDE',
710
            branchemail => "",
711
            branchillemail => "",
712
            branchreplyto => ""
713
        }
714
    });
699
    my $illrq = $builder->build({
715
    my $illrq = $builder->build({
700
        source => 'Illrequest',
716
        source => 'Illrequest',
701
        value => { branchcode => "CPL", borrowernumber => $patron->{borrowernumber} }
717
        value => { branchcode => "HDE", borrowernumber => $patron->{borrowernumber} }
702
    });
718
    });
703
    my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
719
    my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
704
    $illrq_obj->_config($config);
720
    $illrq_obj->_config($config);
Lines 706-717 subtest 'Helpers' => sub { Link Here
706
722
707
    # getPrefix
723
    # getPrefix
708
    $config->set_series('getPrefixes',
724
    $config->set_series('getPrefixes',
709
                        { CPL => "TEST", TSL => "BAR", default => "DEFAULT" },
725
                        { HDE => "TEST", TSL => "BAR", default => "DEFAULT" },
710
                        { A => "ATEST", C => "CBAR", default => "DEFAULT" });
726
                        { A => "ATEST", C => "CBAR", default => "DEFAULT" });
711
    is($illrq_obj->getPrefix({ brw_cat => "UNKNOWN", branch => "CPL" }), "TEST",
727
    is($illrq_obj->getPrefix({ brw_cat => "UNKNOWN", branch => "HDE" }), "TEST",
712
       "getPrefix: branch");
728
       "getPrefix: branch");
713
    $config->set_series('getPrefixes',
729
    $config->set_series('getPrefixes',
714
                        { CPL => "TEST", TSL => "BAR", default => "DEFAULT" },
730
                        { HDE => "TEST", TSL => "BAR", default => "DEFAULT" },
715
                        { A => "ATEST", C => "CBAR", default => "DEFAULT" });
731
                        { A => "ATEST", C => "CBAR", default => "DEFAULT" });
716
    is($illrq_obj->getPrefix({ branch => "UNKNOWN" }), "",
732
    is($illrq_obj->getPrefix({ branch => "UNKNOWN" }), "",
717
       "getPrefix: default");
733
       "getPrefix: default");
Lines 721-731 subtest 'Helpers' => sub { Link Here
721
737
722
    # id_prefix
738
    # id_prefix
723
    $config->set_series('getPrefixes',
739
    $config->set_series('getPrefixes',
724
                        { CPL => "TEST", TSL => "BAR", default => "DEFAULT" },
740
                        { HDE => "TEST", TSL => "BAR", default => "DEFAULT" },
725
                        { AB => "ATEST", CD => "CBAR", default => "DEFAULT" });
741
                        { AB => "ATEST", CD => "CBAR", default => "DEFAULT" });
726
    is($illrq_obj->id_prefix, "TEST-", "id_prefix: branch");
742
    is($illrq_obj->id_prefix, "TEST-", "id_prefix: branch");
727
    $config->set_series('getPrefixes',
743
    $config->set_series('getPrefixes',
728
                        { CPLT => "TEST", TSLT => "BAR", default => "DEFAULT" },
744
                        { HDET => "TEST", TSLT => "BAR", default => "DEFAULT" },
729
                        { AB => "ATEST", CD => "CBAR", default => "DEFAULT" });
745
                        { AB => "ATEST", CD => "CBAR", default => "DEFAULT" });
730
    is($illrq_obj->id_prefix, "", "id_prefix: default");
746
    is($illrq_obj->id_prefix, "", "id_prefix: default");
731
747
Lines 735-740 subtest 'Helpers' => sub { Link Here
735
    $illrq_obj->status('CANCREQ')->store;
751
    $illrq_obj->status('CANCREQ')->store;
736
    is($illrq_obj->requires_moderation, 'CANCREQ', "requires_moderation: Yes.");
752
    is($illrq_obj->requires_moderation, 'CANCREQ', "requires_moderation: Yes.");
737
753
754
    #send_patron_notice
755
    my $attr = Koha::MessageAttributes->find({ message_name => 'Ill_ready' });
756
    C4::Members::Messaging::SetMessagingPreference({
757
        borrowernumber => $patron->{borrowernumber},
758
        message_attribute_id => $attr->message_attribute_id,
759
        message_transport_types => ['email']
760
    });
761
    my $return_patron = $illrq_obj->send_patron_notice('ILL_PICKUP_READY');
762
    my $notice = $schema->resultset('MessageQueue')->search({
763
            letter_code => 'ILL_PICKUP_READY',
764
            message_transport_type => 'email',
765
            borrowernumber => $illrq_obj->borrowernumber
766
        })->next()->letter_code;
767
    is_deeply(
768
        $return_patron,
769
        { result => { success => ['email'], fail => [] } },
770
        "Correct return when notice created"
771
    );
772
    is($notice, 'ILL_PICKUP_READY' ,"Notice is correctly created");
773
774
    my $return_patron_fail = $illrq_obj->send_patron_notice();
775
    is_deeply(
776
        $return_patron_fail,
777
        { error => 'notice_no_type' },
778
        "Correct error when missing type"
779
    );
780
781
    #get_staff_to_address
782
    # Mock a KohaAdminEmailAddress syspref
783
    t::lib::Mocks::mock_preference(
784
        'KohaAdminEmailAddress',
785
        'kohaadmin@nowhere.com'
786
    );
787
    # No branch addresses defined and no ILLDefaultStaffEmail, so should
788
    # fall back to Koha admin address
789
    my $email_kohaadmin = $illrq_obj->get_staff_to_address;
790
    ok(
791
        $email_kohaadmin eq 'kohaadmin@nowhere.com',
792
        'get_staff_to_address falls back to Koha admin in the absence of other alternatives'
793
    );
794
    # General branch address defined, should fall back to that
795
    $builder->delete({ source => 'Branch', records => $illbrn });
796
    $illbrn = $builder->build({
797
        source => 'Branch',
798
        value => {
799
            branchcode => 'HDE',
800
            branchemail => 'branch@nowhere.com',
801
            branchillemail => "",
802
            branchreplyto => ""
803
        }
804
    });
805
    my $email_gen_branch = $illrq_obj->get_staff_to_address;
806
    ok(
807
        $email_gen_branch eq 'branch@nowhere.com',
808
        'get_staff_to_address falls back to general branch address when defined'
809
    );
810
    # ILL staff syspref address defined, should fall back to that
811
    t::lib::Mocks::mock_preference(
812
        'ILLDefaultStaffEmail',
813
        'illstaff@nowhere.com'
814
    );
815
    my $email_syspref = $illrq_obj->get_staff_to_address;
816
    ok(
817
        $email_syspref eq 'illstaff@nowhere.com',
818
        'get_staff_to_address falls back to ILLDefaultStaffEmail when defined'
819
    );
820
    # Branch ILL address defined, should use that
821
    $builder->delete({ source => 'Branch', records => $illbrn });
822
    $illbrn = $builder->build({
823
        source => 'Branch',
824
        value => {
825
            branchcode => 'HDE',
826
            branchemail => 'branch@nowhere.com',
827
            branchillemail => 'branchill@nowhere.com',
828
            branchreplyto => ""
829
        }
830
    });
831
    my $email_branch = $illrq_obj->get_staff_to_address;
832
    ok(
833
        $email_branch eq 'branchill@nowhere.com',
834
        'get_staff_to_address uses branch ILL address when defined'
835
    );
836
837
    #send_staff_notice
838
    # Specify that no staff notices should be send
839
    t::lib::Mocks::mock_preference('ILLSendStaffNotices', '');
840
    my $return_staff_cancel_fail =
841
        $illrq_obj->send_staff_notice('ILL_REQUEST_CANCEL');
842
    is_deeply(
843
        $return_staff_cancel_fail,
844
        { error => 'notice_not_enabled' },
845
        "Does not send notices that are not enabled"
846
    );
847
    # Specify that the cancel notice can be sent
848
    t::lib::Mocks::mock_preference('ILLSendStaffNotices', 'ILL_REQUEST_CANCEL');
849
    my $return_staff_cancel = $illrq_obj->send_staff_notice(
850
        'ILL_REQUEST_CANCEL'
851
    );
852
    my $cancel = $schema->resultset('MessageQueue')->search({
853
            letter_code => 'ILL_REQUEST_CANCEL',
854
            message_transport_type => 'email',
855
            to_address => 'branchill@nowhere.com'
856
        })->next()->letter_code;
857
    is_deeply(
858
        $return_staff_cancel,
859
        { success => 'notice_queued' },
860
        "Correct return when staff notice created"
861
    );
862
863
    my $return_staff_fail = $illrq_obj->send_staff_notice();
864
    is_deeply(
865
        $return_staff_fail,
866
        { error => 'notice_no_type' },
867
        "Correct error when missing type"
868
    );
869
870
    #get_notice
871
    my $not = $illrq_obj->get_notice({
872
        notice_code => 'ILL_REQUEST_CANCEL',
873
        transport   => 'email'
874
    });
875
876
    # We test the properties of the hashref separately because the random
877
    # hash ordering of the metadata means we can't test the entire thing
878
    # with is_deeply
879
    ok(
880
        $not->{module} eq 'ill',
881
        'Correct module return from get_notice'
882
    );
883
    ok(
884
        $not->{name} eq 'ILL request cancelled',
885
        'Correct name return from get_notice'
886
    );
887
    ok(
888
        $not->{message_transport_type} eq 'email',
889
        'Correct message_transport_type return from get_notice'
890
    );
891
    ok(
892
        $not->{title} eq 'Interlibrary loan request cancelled',
893
        'Correct title return from get_notice'
894
    );
895
738
    $schema->storage->txn_rollback;
896
    $schema->storage->txn_rollback;
739
};
897
};
740
898
741
- 

Return to bug 22818