|
Line 0
Link Here
|
|
|
1 |
package SImpls::MessageQueues; |
| 2 |
|
| 3 |
# Copyright 2015 Vaara-kirjastot |
| 4 |
# |
| 5 |
# This file is part of Koha. |
| 6 |
# |
| 7 |
# Koha is free software; you can redistribute it and/or modify it under the |
| 8 |
# terms of the GNU General Public License as published by the Free Software |
| 9 |
# Foundation; either version 2 of the License, or (at your option) any later |
| 10 |
# version. |
| 11 |
# |
| 12 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
| 13 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
| 14 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License along |
| 17 |
# with Koha; if not, write to the Free Software Foundation, Inc., |
| 18 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 |
|
| 20 |
use Modern::Perl; |
| 21 |
use Carp; |
| 22 |
use Test::More; |
| 23 |
|
| 24 |
use Koha::Database; |
| 25 |
use C4::Context; |
| 26 |
|
| 27 |
sub addMessageQueues { |
| 28 |
my $C = shift; |
| 29 |
my $letterCode = shift; |
| 30 |
my $messageTransportType = shift; |
| 31 |
my $S = $C->{stash}->{scenario}; |
| 32 |
my $F = $C->{stash}->{feature}; |
| 33 |
|
| 34 |
$S->{message_queues} = {} unless $S->{message_queues}; |
| 35 |
$F->{message_queues} = {} unless $F->{message_queues}; |
| 36 |
foreach my $cardumber (sort keys %{$S->{borrowers}}) { |
| 37 |
my $borrower = $S->{borrowers}->{$cardumber}; |
| 38 |
|
| 39 |
my @repeat; |
| 40 |
foreach my $i ( sort keys %{$S->{issues}} ) { |
| 41 |
my $issue = $S->{issues}->{$i}; |
| 42 |
if ( $issue->{borrowernumber} eq $borrower->{borrowernumber} ) { |
| 43 |
my $itemForCurrentBorrower = $S->{items}->{ $issue->{itemnumber} }; |
| 44 |
push @repeat, {items => $itemForCurrentBorrower}; |
| 45 |
} |
| 46 |
} |
| 47 |
my $letter = C4::Letters::GetPreparedLetter ( |
| 48 |
module => 'circulation', |
| 49 |
letter_code => $letterCode, |
| 50 |
branchcode => $borrower->{branchcode}, |
| 51 |
tables => {borrowers => $borrower}, |
| 52 |
#substitute => $substitute, |
| 53 |
repeat => { item => \@repeat }, |
| 54 |
message_transport_type => $messageTransportType, |
| 55 |
); |
| 56 |
my $mqHash = { |
| 57 |
letter => $letter, |
| 58 |
borrowernumber => $borrower->{borrowernumber}, |
| 59 |
message_transport_type => $messageTransportType, |
| 60 |
from_address => C4::Context->preference('KohaAdminEmailAddress'), |
| 61 |
to_address => 'dontsend@example.com', |
| 62 |
}; |
| 63 |
my $message_queue_id = C4::Letters::EnqueueLetter( $mqHash ); |
| 64 |
like($message_queue_id, qr/^\d+$/, "Message enqueued."); |
| 65 |
$S->{message_queues}->{$message_queue_id} = $mqHash; |
| 66 |
$F->{message_queues}->{$message_queue_id} = $mqHash; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
sub deleteAllMessageQueues { |
| 71 |
my $C = shift; |
| 72 |
my $F = $C->{stash}->{feature}; |
| 73 |
my $schema = Koha::Database->new()->schema(); |
| 74 |
|
| 75 |
$schema->resultset('MessageQueue')->search({})->delete_all(); |
| 76 |
} |
| 77 |
|
| 78 |
sub verifyMessageQueueItems { |
| 79 |
my $C = shift; |
| 80 |
my $S = $C->{stash}->{scenario}; |
| 81 |
my $F = $C->{stash}->{feature}; |
| 82 |
my $checks = $C->data(); #Get the checks, which might not have manifested itselves to the message_queue_items |
| 83 |
ok(($checks && scalar(@$checks) > 0), "You must give checks as the data"); |
| 84 |
|
| 85 |
#See which checks are supposed to be in the message_queue_item-table, and which are just to clarify intent. |
| 86 |
my @checksInTable; |
| 87 |
foreach my $check (@$checks) { |
| 88 |
my $status = $check->{status}; |
| 89 |
push @checksInTable, $check if ($status ne 'not_odue' && $status ne 'no_rule'); |
| 90 |
} |
| 91 |
|
| 92 |
##Make sure that there are no trailing MessageQueueItems. |
| 93 |
my $schema = Koha::Database->new()->schema(); |
| 94 |
my $allMessageQueueItemsCount = $schema->resultset('MessageQueueItem')->search({})->count(); |
| 95 |
is($allMessageQueueItemsCount, scalar(@checksInTable), "We should have ".scalar(@checksInTable)." checks for $allMessageQueueItemsCount message_queue_items, with no trailing items."); |
| 96 |
|
| 97 |
#Expressing the following cannot be comfortably done with DBIx |
| 98 |
#especially since message_queue_items-table should not have foreign key linkings to items and issues-tables. |
| 99 |
my $dbh = C4::Context->dbh(); |
| 100 |
my $check_statement = $dbh->prepare( |
| 101 |
"SELECT 1 FROM message_queue_items mi ". |
| 102 |
"LEFT JOIN message_queue mq ON mi.message_id = mq.message_id ". |
| 103 |
"LEFT JOIN borrowers b ON mq.borrowernumber = b.borrowernumber ". |
| 104 |
"LEFT JOIN items i ON mi.itemnumber = i.itemnumber ". |
| 105 |
"LEFT JOIN issues iss ON iss.issue_id = mi.issue_id ". |
| 106 |
"WHERE b.cardnumber = ? AND i.barcode = ? AND mi.branch = ? AND mq.letter_code = ? ". |
| 107 |
"AND mi.letternumber = ? AND mq.status = ? AND mq.message_transport_type = ? ". |
| 108 |
""); |
| 109 |
|
| 110 |
##Check that there is a matching MessageQueueItem for each given test. |
| 111 |
foreach my $check (@checksInTable) { |
| 112 |
my @params; |
| 113 |
push @params, $check->{cardnumber}; |
| 114 |
push @params, $check->{barcode}; |
| 115 |
push @params, $check->{branch}; |
| 116 |
push @params, $check->{lettercode}; |
| 117 |
push @params, $check->{letternumber}; |
| 118 |
push @params, $check->{status}; |
| 119 |
push @params, $check->{transport_type}; |
| 120 |
$check_statement->execute( @params ); |
| 121 |
my $ok = $check_statement->fetchrow(); |
| 122 |
last unless ok(($ok && $ok == 1), "Check: For params @params"); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
sub verifyMessageQueues { |
| 127 |
my $C = shift; |
| 128 |
my $S = $C->{stash}->{scenario}; |
| 129 |
my $F = $C->{stash}->{feature}; |
| 130 |
|
| 131 |
my $checks = $C->data(); #Get the checks, which might not have manifested itselves to the message_queues |
| 132 |
ok(($checks && scalar(@$checks) > 0), "You must give checks as the data"); |
| 133 |
|
| 134 |
##Make sure that there are no trailing MessageQueueItems. |
| 135 |
my $schema = Koha::Database->new()->schema(); |
| 136 |
my $allMessageQueuesCount = $schema->resultset('MessageQueue')->search({})->count(); |
| 137 |
is($allMessageQueuesCount, scalar(@$checks), "We should have ".scalar(@$checks)." checks for $allMessageQueuesCount message_queue_items, with no trailing items."); |
| 138 |
|
| 139 |
my @all = $schema->resultset('MessageQueue')->search({}); |
| 140 |
foreach my $a ( @all ) { |
| 141 |
print $a->content()."\n"; |
| 142 |
} |
| 143 |
|
| 144 |
if ($checks->[0]->{contentRegexp}) { |
| 145 |
verifyMessageQueuesFromContentRegexp($checks); |
| 146 |
} |
| 147 |
elsif ($checks->[0]->{containedBarcodes}) { |
| 148 |
verifyMessageQueuesFromBarcodes($checks); |
| 149 |
} |
| 150 |
else { |
| 151 |
die "\nverifyMessageQueues():> The MessageQueues-data table must contain column 'barcodes', or 'contentRegexp'.\n". |
| 152 |
"'barcodes' contains comma separated list of barcodes that must be found inside the message_queue.content.\n". |
| 153 |
"'contentRegexp' is a regexp that must match with the message_queue.content.\n"; |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
sub verifyMessageQueuesFromBarcodes { |
| 158 |
my ($checks) = @_; |
| 159 |
|
| 160 |
my $dbh = C4::Context->dbh(); |
| 161 |
my $check_statement = $dbh->prepare( |
| 162 |
"SELECT 1 FROM message_queue mq ". |
| 163 |
"LEFT JOIN message_queue_items mqi ON mqi.message_id = mq.message_id ". |
| 164 |
"LEFT JOIN borrowers b ON mq.borrowernumber = b.borrowernumber ". |
| 165 |
"LEFT JOIN items i ON mqi.itemnumber = i.itemnumber ". |
| 166 |
"WHERE b.cardnumber = ? ". |
| 167 |
"AND i.barcode = ? ". |
| 168 |
"AND mq.letter_code = ? ". |
| 169 |
"AND mq.status = ? ". |
| 170 |
"AND mq.message_transport_type = ? ". |
| 171 |
""); |
| 172 |
|
| 173 |
##Check that there is a matching MessageQueue for each given test. |
| 174 |
foreach my $check (@$checks) { |
| 175 |
my @barcodes = map {my $a = $_; $a =~ s/\s+//gsm; $a;} split(',',$check->{containedBarcodes}); |
| 176 |
foreach my $bc (@barcodes) { |
| 177 |
my @params; |
| 178 |
push @params, $check->{cardnumber}; |
| 179 |
push @params, $bc; |
| 180 |
push @params, $check->{lettercode}; |
| 181 |
push @params, $check->{status}; |
| 182 |
push @params, $check->{transport_type}; |
| 183 |
$check_statement->execute( @params ); |
| 184 |
my $ok = $check_statement->fetchrow(); |
| 185 |
last unless ok(($ok && $ok == 1), "Check: For params @params"); |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
sub verifyMessageQueuesFromContentRegexp { |
| 190 |
my ($checks) = @_; |
| 191 |
|
| 192 |
my $dbh = C4::Context->dbh(); |
| 193 |
my $check_statement = $dbh->prepare( |
| 194 |
"SELECT 1 FROM message_queue mq ". |
| 195 |
"LEFT JOIN borrowers b ON mq.borrowernumber = b.borrowernumber ". |
| 196 |
"WHERE b.cardnumber = ? ". |
| 197 |
"AND mq.letter_code = ? ". |
| 198 |
"AND mq.status = ? ". |
| 199 |
"AND mq.message_transport_type = ? ". |
| 200 |
"AND mq.content REGEXP ? ". |
| 201 |
""); |
| 202 |
|
| 203 |
##Check that there is a matching MessageQueue for each given test. |
| 204 |
foreach my $check (@$checks) { |
| 205 |
my @params; |
| 206 |
push @params, $check->{cardnumber}; |
| 207 |
push @params, $check->{lettercode}; |
| 208 |
push @params, $check->{status}; |
| 209 |
push @params, $check->{transport_type}; |
| 210 |
push @params, $check->{contentRegexp}; |
| 211 |
$check_statement->execute( @params ); |
| 212 |
my $ok = $check_statement->fetchrow(); |
| 213 |
last unless ok(($ok && $ok == 1), "Check: For params @params"); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
1; |