|
Line 0
Link Here
|
| 0 |
- |
1 |
#!/usr/bin/env perl |
|
|
2 |
|
| 3 |
# Copyright 2015 Open Source Freedom Fighters |
| 4 |
# |
| 5 |
# This file is part of Koha. |
| 6 |
# |
| 7 |
# Koha is free software; you can redistribute it and/or modify it |
| 8 |
# under the terms of the GNU General Public License as published by |
| 9 |
# the Free Software Foundation; either version 3 of the License, or |
| 10 |
# (at your option) any later version. |
| 11 |
# |
| 12 |
# Koha is distributed in the hope that it will be useful, but |
| 13 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU General Public License |
| 18 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
| 19 |
$ENV{KOHA_PAGEOBJECT_DEBUG} = 1; |
| 20 |
use Modern::Perl; |
| 21 |
|
| 22 |
use Test::More; |
| 23 |
use Try::Tiny; #Even Selenium::Remote::Driver uses Try::Tiny :) |
| 24 |
|
| 25 |
use Koha::Auth::PermissionManager; |
| 26 |
|
| 27 |
use t::lib::Page::Mainpage; |
| 28 |
use t::lib::Page::Opac::OpacMain; |
| 29 |
use t::lib::Page::Opac::OpacMemberentry; |
| 30 |
use t::lib::Page::Members::Memberentry; |
| 31 |
use t::lib::Page::Members::Moremember; |
| 32 |
use t::lib::Page::Members::Notices; |
| 33 |
|
| 34 |
use t::lib::TestObjects::BorrowerFactory; |
| 35 |
use t::lib::TestObjects::MessageQueueFactory; |
| 36 |
use t::lib::TestObjects::SystemPreferenceFactory; |
| 37 |
|
| 38 |
use C4::Context; |
| 39 |
use C4::Members; |
| 40 |
|
| 41 |
##Setting up the test context |
| 42 |
my $testContext = {}; |
| 43 |
|
| 44 |
my $password = '1234'; |
| 45 |
my $invalidBorrowerNumber = '000'; |
| 46 |
my $borrowerFactory = t::lib::TestObjects::BorrowerFactory->new(); |
| 47 |
my $borrowers = $borrowerFactory->createTestGroup([ |
| 48 |
{firstname => 'Testthree', |
| 49 |
surname => 'Testfour', |
| 50 |
cardnumber => 'superuberadmin', |
| 51 |
branchcode => 'CPL', |
| 52 |
userid => 'god', |
| 53 |
address => 'testi', |
| 54 |
city => 'joensuu', |
| 55 |
zipcode => '80100', |
| 56 |
password => $password, |
| 57 |
}, |
| 58 |
], undef, $testContext); |
| 59 |
my $messages = t::lib::TestObjects::MessageQueueFactory->createTestGroup([ |
| 60 |
{letter => { |
| 61 |
title => "Test title", |
| 62 |
content => "Tessst content", |
| 63 |
}, |
| 64 |
cardnumber => $borrowers->{'superuberadmin'}->cardnumber, |
| 65 |
message_transport_type => 'sms', |
| 66 |
from_address => 'A001@example.com', |
| 67 |
}, |
| 68 |
{letter => { |
| 69 |
title => "Test title", |
| 70 |
content => "Tessst content", |
| 71 |
}, |
| 72 |
cardnumber => $borrowers->{'superuberadmin'}->cardnumber, |
| 73 |
message_transport_type => 'sms', |
| 74 |
from_address => 'A002@example.com', |
| 75 |
}, |
| 76 |
{letter => { |
| 77 |
title => "Test title", |
| 78 |
content => "Tessst content", |
| 79 |
}, |
| 80 |
cardnumber => $borrowers->{'superuberadmin'}->cardnumber, |
| 81 |
message_transport_type => 'email', |
| 82 |
to_address => '', |
| 83 |
from_address => 'B001@example.com', |
| 84 |
}, |
| 85 |
{letter => { |
| 86 |
title => "Test title", |
| 87 |
content => "Tessst content", |
| 88 |
}, |
| 89 |
cardnumber => $borrowers->{'superuberadmin'}->cardnumber, |
| 90 |
message_transport_type => 'email', |
| 91 |
to_address => 'nobody@example.com', |
| 92 |
from_address => 'B002@example.com', |
| 93 |
}, |
| 94 |
{letter => { |
| 95 |
title => "INVALID USER", |
| 96 |
content => "INVALID USER", |
| 97 |
}, |
| 98 |
cardnumber => $borrowers->{'superuberadmin'}->cardnumber, |
| 99 |
message_transport_type => 'email', |
| 100 |
to_address => '', |
| 101 |
from_address => 'B003@example.com', |
| 102 |
}, |
| 103 |
], undef, $testContext); |
| 104 |
my $systempreferences = t::lib::TestObjects::SystemPreferenceFactory->createTestGroup([ |
| 105 |
{preference => 'SMSSendDriver', |
| 106 |
value => 'nonexistentdriver' |
| 107 |
}, |
| 108 |
], undef, $testContext); |
| 109 |
|
| 110 |
my $permissionManager = Koha::Auth::PermissionManager->new(); |
| 111 |
$permissionManager->grantPermissions($borrowers->{'superuberadmin'}, {superlibrarian => 'superlibrarian'}); |
| 112 |
|
| 113 |
eval { |
| 114 |
|
| 115 |
# staff client |
| 116 |
my $notices = t::lib::Page::Members::Notices->new({borrowernumber => $borrowers->{'superuberadmin'}->borrowernumber}); |
| 117 |
|
| 118 |
# let's send the messages |
| 119 |
|
| 120 |
# first, send one email message with invalid borrower number |
| 121 |
$messages->{'B003@example.com'}->{borrowernumber} = $invalidBorrowerNumber; |
| 122 |
C4::Letters::_send_message_by_email($messages->{'B003@example.com'}); |
| 123 |
|
| 124 |
# the rest should produce errors automatically without our modificaitons |
| 125 |
C4::Letters::SendQueuedMessages(); |
| 126 |
|
| 127 |
# then, login to intranet |
| 128 |
$notices->doPasswordLogin($borrowers->{'superuberadmin'}->userid(), $password); |
| 129 |
|
| 130 |
# test delivery notes for SMS messaging |
| 131 |
DoSMSTests($notices); |
| 132 |
# check delivery notes for email messaging |
| 133 |
CheckEmailMessages($notices); |
| 134 |
|
| 135 |
}; |
| 136 |
if ($@) { #Catch all leaking errors and gracefully terminate. |
| 137 |
warn $@; |
| 138 |
tearDown(); |
| 139 |
exit 1; |
| 140 |
} |
| 141 |
|
| 142 |
##All tests done, tear down test context |
| 143 |
tearDown(); |
| 144 |
done_testing; |
| 145 |
|
| 146 |
sub tearDown { |
| 147 |
t::lib::TestObjects::ObjectFactory->tearDownTestContext($testContext); |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
###################################################### |
| 158 |
### STARTING TEST IMPLEMENTATIONS ### |
| 159 |
###################################################### |
| 160 |
|
| 161 |
sub DoSMSTests { |
| 162 |
my ($notices) = @_; |
| 163 |
|
| 164 |
# login and check that our table is displayed correctly |
| 165 |
$notices-> |
| 166 |
hasDeliveryNoteColumnInNoticesTable()-> |
| 167 |
hasTextInTableCell("Missing SMS number"); |
| 168 |
|
| 169 |
# now let's give the user a SMS number so we proceed to next step of failure |
| 170 |
ModMember( borrowernumber => $borrowers->{superuberadmin}->borrowernumber, |
| 171 |
smsalertnumber => 'just_some_number' ); |
| 172 |
|
| 173 |
# set first to sent and second to pending to check "duplicate" (Letters::_is_duplicate()) |
| 174 |
setMessageStatus($messages->{'A001@example.com'}->{message_id}, "sent"); |
| 175 |
setMessageStatus($messages->{'A002@example.com'}->{message_id}, "pending"); |
| 176 |
|
| 177 |
C4::Letters::SendQueuedMessages(); |
| 178 |
# A002 should go into failed: message is duplicate status |
| 179 |
|
| 180 |
# set A001 back to pending to check that we get no reply from driver |
| 181 |
setMessageStatus($messages->{'A001@example.com'}->{message_id}, "pending"); |
| 182 |
|
| 183 |
# let's send the message and check for delivery note, we should get a warning |
| 184 |
# for non-installed driver. |
| 185 |
C4::Letters::SendQueuedMessages(); |
| 186 |
|
| 187 |
$notices->navigateToNotices()-> |
| 188 |
hasTextInTableCell("Message is duplicate")-> |
| 189 |
hasTextInTableCell("No notes from SMS driver"); |
| 190 |
} |
| 191 |
|
| 192 |
sub CheckEmailMessages { |
| 193 |
my ($notices) = @_; |
| 194 |
|
| 195 |
$notices-> |
| 196 |
hasTextInTableCell("Unable to find an email address")-> |
| 197 |
hasTextInTableCell("Invalid borrowernumber")-> |
| 198 |
hasTextInTableCell("Connection refused"); #sendmail should also fail |
| 199 |
} |
| 200 |
sub setMessageStatus { |
| 201 |
my ($message_id, $status) = @_; |
| 202 |
my $dbh = C4::Context->dbh; |
| 203 |
|
| 204 |
my $sth = $dbh->prepare("UPDATE message_queue SET status=?, delivery_note='' WHERE message_id=?"); |
| 205 |
my $result = $sth->execute($status, $message_id); |
| 206 |
|
| 207 |
return $result; |
| 208 |
} |