|
Lines 19-25
Link Here
|
| 19 |
|
19 |
|
| 20 |
use Modern::Perl; |
20 |
use Modern::Perl; |
| 21 |
|
21 |
|
| 22 |
use Test::More tests => 3; |
22 |
use Test::More tests => 8; |
| 23 |
|
23 |
|
| 24 |
use Koha::Notice::Templates; |
24 |
use Koha::Notice::Templates; |
| 25 |
use Koha::Database; |
25 |
use Koha::Database; |
|
Lines 66-70
$retrieved_template->delete;
Link Here
|
| 66 |
is( Koha::Notice::Templates->search->count, |
66 |
is( Koha::Notice::Templates->search->count, |
| 67 |
$nb_of_templates, 'Delete should have deleted the template' ); |
67 |
$nb_of_templates, 'Delete should have deleted the template' ); |
| 68 |
|
68 |
|
| 69 |
$schema->storage->txn_rollback; |
69 |
## Tests for Koha::Notice::Messages->get_failed_notices |
|
|
70 |
|
| 71 |
# Remove all existing messages in the message_queue |
| 72 |
Koha::Notice::Messages->delete; |
| 73 |
|
| 74 |
# Make a patron |
| 75 |
my $patron_category = $builder->build({ source => 'Category' })->{categorycode}; |
| 76 |
my $borrowernumber = Koha::Patron->new({ |
| 77 |
firstname => 'Jane', |
| 78 |
surname => 'Smith', |
| 79 |
categorycode => $patron_category, |
| 80 |
branchcode => $library->{branchcode}, |
| 81 |
smsalertnumber => '123', |
| 82 |
})->store->borrowernumber; |
| 83 |
|
| 84 |
# With all notices removed from the message_queue table confirm get_failed_notices() returns 0 |
| 85 |
my @failed_notices = Koha::Notice::Messages->get_failed_notices(); |
| 86 |
is( @failed_notices, 0, 'No failed notices currently exist'); |
| 87 |
|
| 88 |
# Add a failed notice to the message_queue table |
| 89 |
my $message = Koha::Notice::Message->new({ |
| 90 |
borrowernumber => $borrowernumber, |
| 91 |
subject => 'subject', |
| 92 |
content => 'content', |
| 93 |
message_transport_type => 'sms', |
| 94 |
status => 'failed', |
| 95 |
letter_code => 'just_a_code', |
| 96 |
time_queued => \"NOW()", |
| 97 |
})->store; |
| 98 |
|
| 99 |
# With one failed notice in the message_queue table confirm get_failed_notices() returns 1 |
| 100 |
my @failed_notices2 = Koha::Notice::Messages->get_failed_notices(); |
| 101 |
is( @failed_notices2, 1, 'One failed notice currently exists'); |
| 102 |
|
| 103 |
# Change failed notice status to 'pending' |
| 104 |
$message->update({ status => 'pending' }); |
| 105 |
|
| 106 |
# With the 1 failed notice in the message_queue table marked 'pending' confirm get_failed_notices() returns 0 |
| 107 |
my @failed_notices3 = Koha::Notice::Messages->get_failed_notices(); |
| 108 |
is( @failed_notices3, 0, 'No failed notices currently existing, now the notice has been marked pending'); |
| 109 |
|
| 110 |
|
| 111 |
## Tests for Koha::Notice::Message::restrict_patron_when_notice_fails |
| 112 |
|
| 113 |
# Empty the borrower_debarments table |
| 114 |
my $dbh = C4::Context->dbh; |
| 115 |
$dbh->do(q|DELETE FROM borrower_debarments|); |
| 70 |
|
116 |
|
| 71 |
- |
117 |
# Change the status of the notice back to 'failed' |
|
|
118 |
$message->update({ status => 'failed' }); |
| 119 |
|
| 120 |
my @failed_notices4 = Koha::Notice::Messages->get_failed_notices(); |
| 121 |
|
| 122 |
# There should be one failed notice |
| 123 |
if (@failed_notices4) { |
| 124 |
# Restrict the borrower who has the failed notice |
| 125 |
foreach my $failed_notice (@failed_notices4) { |
| 126 |
if ($failed_notice->message_transport_type eq 'sms' || $failed_notice->message_transport_type eq 'email') { |
| 127 |
$failed_notice->restrict_patron_when_notice_fails; |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
# Confirm that the restrict_patron_when_notice_fails() has added a restriction to the patron |
| 133 |
is(@{ Koha::Patron::Debarments::GetDebarments({ borrowernumber => $borrowernumber, comment => 'SMS number invalid' })}, 1, "Patron has a restriction placed on them"); |
| 134 |
|
| 135 |
# Restrict the borrower who has the failed notice |
| 136 |
foreach my $failed_notice (@failed_notices4) { |
| 137 |
if ($failed_notice->message_transport_type eq 'sms' || $failed_notice->message_transport_type eq 'email') { |
| 138 |
|
| 139 |
# If the borrower already has a debarment for failed SMS or email notice then don't apply |
| 140 |
# a new debarment to their account |
| 141 |
if ( @{ Koha::Patron::Debarments::GetDebarments({ borrowernumber => $borrowernumber, comment => 'SMS number invalid' }) } ) { |
| 142 |
next; |
| 143 |
} elsif ( @{ Koha::Patron::Debarments::GetDebarments({ borrowernumber => $borrowernumber, comment => 'Email address invalid' }) }) { |
| 144 |
next; |
| 145 |
} |
| 146 |
|
| 147 |
# Place the debarment if the borrower doesn't already have one for failed SMS or email |
| 148 |
# notice |
| 149 |
$failed_notice->restrict_patron_when_notice_fails; |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
# Confirm that no new debarment is added to the borrower |
| 154 |
is(@{ Koha::Patron::Debarments::GetDebarments({ borrowernumber => $borrowernumber, comment => 'SMS number invalid' })}, 1, "No new restriction has been placed on the patron"); |
| 155 |
|
| 156 |
$schema->storage->txn_rollback; |