4 use Test::More tests => 16;
8 use_ok('C4::Overdues');
9 can_ok('C4::Overdues', 'GetOverdueMessageTransportTypes');
10 can_ok('C4::Overdues', 'GetBranchcodesWithOverdueRules');
12 my $dbh = C4::Context->dbh;
13 $dbh->{AutoCommit} = 0;
14 $dbh->{RaiseError} = 1;
16 $dbh->do(q|DELETE FROM letter|);
17 $dbh->do(q|DELETE FROM message_queue|);
18 $dbh->do(q|DELETE FROM message_transport_types|);
19 $dbh->do(q|DELETE FROM overduerules|);
20 $dbh->do(q|DELETE FROM overduerules_transport_types|);
23 INSERT INTO message_transport_types( message_transport_type ) VALUES ('email'), ('phone'), ('print'), ('sms')
27 INSERT INTO overduerules ( overduerules_id, branchcode, categorycode ) VALUES
34 $dbh->do(q|INSERT INTO overduerules_transport_types (overduerules_id, letternumber, message_transport_type) VALUES
50 $mtts = C4::Overdues::GetOverdueMessageTransportTypes('CPL', 'PT');
51 is( $mtts, undef, 'GetOverdueMessageTransportTypes: returns undef if no letternumber given' );
53 $mtts = C4::Overdues::GetOverdueMessageTransportTypes('CPL', undef, 1);
54 is( $mtts, undef, 'GetOverdueMessageTransportTypes: returns undef if no categorycode given' );
56 $mtts = C4::Overdues::GetOverdueMessageTransportTypes('CPL');
57 is( $mtts, undef, 'GetOverdueMessageTransportTypes: returns undef if no letternumber and categorycode given' );
59 $mtts = C4::Overdues::GetOverdueMessageTransportTypes('CPL', 'PT', 1);
60 is_deeply( $mtts, ['email'], 'GetOverdueMessageTransportTypes: first overdue is by email for PT (CPL)' );
62 $mtts = C4::Overdues::GetOverdueMessageTransportTypes('CPL', 'PT', 2);
63 is_deeply( $mtts, ['sms'], 'GetOverdueMessageTransportTypes: second overdue is by sms for PT (CPL)' );
65 $mtts = C4::Overdues::GetOverdueMessageTransportTypes('CPL', 'PT', 3);
66 is_deeply( $mtts, ['email'], 'GetOverdueMessageTransportTypes: third overdue is by email for PT (CPL)' );
68 $mtts = C4::Overdues::GetOverdueMessageTransportTypes('', 'PT', 1);
69 is_deeply( $mtts, ['email'], 'GetOverdueMessageTransportTypes: first overdue is by email for PT (default)' );
71 $mtts = C4::Overdues::GetOverdueMessageTransportTypes('', 'PT', 2);
72 is_deeply( $mtts, ['email', 'sms'], 'GetOverdueMessageTransportTypes: second overdue is by email and sms for PT (default)' );
74 $mtts = C4::Overdues::GetOverdueMessageTransportTypes('', 'PT', 3);
75 is_deeply( $mtts, ['print', 'sms', 'email'], 'GetOverdueMessageTransportTypes: third overdue is by print, sms and email for PT (default). With print in first.' );
77 # Test GetBranchcodesWithOverdueRules
78 $dbh->do(q|DELETE FROM overduerules|);
80 INSERT INTO overduerules
81 ( branchcode,categorycode, delay1,letter1,debarred1, delay2,letter2,debarred2, delay3,letter3,debarred3 )
83 ( '', '', 1, 'LETTER_CODE1', 1, 5, 'LETTER_CODE2', 1, 10, 'LETTER_CODE3', 1 )
86 my @branchcodes = map { $_->branchcode } Koha::Libraries->search;
88 my @overdue_branches = C4::Overdues::GetBranchcodesWithOverdueRules();
89 is_deeply( [ sort @overdue_branches ], [ sort @branchcodes ], 'If a default rule exists, all branches should be returned' );
92 INSERT INTO overduerules
93 ( branchcode,categorycode, delay1,letter1,debarred1, delay2,letter2,debarred2, delay3,letter3,debarred3 )
95 ( 'CPL', '', 1, 'LETTER_CODE1', 1, 5, 'LETTER_CODE2', 1, 10, 'LETTER_CODE3', 1 )
98 @overdue_branches = C4::Overdues::GetBranchcodesWithOverdueRules();
99 is_deeply( [ sort @overdue_branches ], [ sort @branchcodes ], 'If a default rule exists and a specific rule exists, all branches should be returned' );
101 $dbh->do(q|DELETE FROM overduerules|);
103 INSERT INTO overduerules
104 ( branchcode,categorycode, delay1,letter1,debarred1, delay2,letter2,debarred2, delay3,letter3,debarred3 )
106 ( 'CPL', '', 1, 'LETTER_CODE1', 1, 5, 'LETTER_CODE2', 1, 10, 'LETTER_CODE3', 1 )
109 @overdue_branches = C4::Overdues::GetBranchcodesWithOverdueRules();
110 is_deeply( \@overdue_branches, ['CPL'] , 'If only a specific rule exist, only 1 branch should be returned' );
112 $dbh->do(q|DELETE FROM overduerules|);
114 INSERT INTO overduerules
115 ( branchcode,categorycode, delay1,letter1,debarred1, delay2,letter2,debarred2, delay3,letter3,debarred3 )
117 ( 'CPL', '', 1, 'LETTER_CODE1_CPL', 1, 5, 'LETTER_CODE2_CPL', 1, 10, 'LETTER_CODE3_CPL', 1 ),
118 ( 'MPL', '', 1, 'LETTER_CODE1_MPL', 1, 5, 'LETTER_CODE2_MPL', 1, 10, 'LETTER_CODE3_MPL', 1 )
121 @overdue_branches = C4::Overdues::GetBranchcodesWithOverdueRules();
122 is_deeply( \@overdue_branches, ['CPL', 'MPL'] , 'If only 2 specific rules exist, 2 branches should be returned' );