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 |
{subject => "Test title", |
61 |
content => "Tessst content", |
62 |
cardnumber => $borrowers->{'superuberadmin'}->cardnumber, |
63 |
message_transport_type => 'sms', |
64 |
from_address => 'A001@example.com', |
65 |
}, |
66 |
{subject => "Test title", |
67 |
content => "Tessst content", |
68 |
cardnumber => $borrowers->{'superuberadmin'}->cardnumber, |
69 |
message_transport_type => 'sms', |
70 |
from_address => 'A002@example.com', |
71 |
}, |
72 |
{subject => "Test title", |
73 |
content => "Tessst content", |
74 |
cardnumber => $borrowers->{'superuberadmin'}->cardnumber, |
75 |
message_transport_type => 'email', |
76 |
to_address => '', |
77 |
from_address => 'B001@example.com', |
78 |
}, |
79 |
{subject => "Test title", |
80 |
content => "Tessst content", |
81 |
cardnumber => $borrowers->{'superuberadmin'}->cardnumber, |
82 |
message_transport_type => 'email', |
83 |
to_address => 'nobody@example.com', |
84 |
from_address => 'B002@example.com', |
85 |
}, |
86 |
{subject => "Test title", |
87 |
content => "Tessst content", |
88 |
cardnumber => $borrowers->{'superuberadmin'}->cardnumber, |
89 |
message_transport_type => 'email', |
90 |
to_address => '', |
91 |
from_address => 'B003@example.com', |
92 |
}, |
93 |
], undef, $testContext); |
94 |
my $systempreferences = t::lib::TestObjects::SystemPreferenceFactory->createTestGroup([ |
95 |
{preference => 'SMSSendDriver', |
96 |
value => 'nonexistentdriver' |
97 |
}, |
98 |
{ |
99 |
preference => 'EnhancedMessagingPreferences', |
100 |
value => '1' |
101 |
} |
102 |
], undef, $testContext); |
103 |
|
104 |
my $permissionManager = Koha::Auth::PermissionManager->new(); |
105 |
$permissionManager->grantPermissions($borrowers->{'superuberadmin'}, {superlibrarian => 'superlibrarian'}); |
106 |
|
107 |
eval { |
108 |
|
109 |
# staff client |
110 |
my $notices = t::lib::Page::Members::Notices->new({borrowernumber => $borrowers->{'superuberadmin'}->borrowernumber}); |
111 |
|
112 |
# let's send the messages |
113 |
|
114 |
# first, send one email message with invalid borrower number |
115 |
$messages->{'B003@example.com'}->{borrowernumber} = $invalidBorrowerNumber; |
116 |
C4::Letters::_send_message_by_email($messages->{'B003@example.com'}); |
117 |
|
118 |
# the rest should produce errors automatically without our modificaitons |
119 |
C4::Letters::SendQueuedMessages(); |
120 |
|
121 |
# then, login to intranet |
122 |
$notices->doPasswordLogin($borrowers->{'superuberadmin'}->userid(), $password); |
123 |
|
124 |
# test delivery notes for SMS messaging |
125 |
DoSMSTests($notices); |
126 |
# check delivery notes for email messaging |
127 |
CheckEmailMessages($notices); |
128 |
|
129 |
}; |
130 |
if ($@) { #Catch all leaking errors and gracefully terminate. |
131 |
warn $@; |
132 |
tearDown(); |
133 |
exit 1; |
134 |
} |
135 |
|
136 |
##All tests done, tear down test context |
137 |
tearDown(); |
138 |
done_testing; |
139 |
|
140 |
sub tearDown { |
141 |
t::lib::TestObjects::ObjectFactory->tearDownTestContext($testContext); |
142 |
} |
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
###################################################### |
152 |
### STARTING TEST IMPLEMENTATIONS ### |
153 |
###################################################### |
154 |
|
155 |
sub DoSMSTests { |
156 |
my ($notices) = @_; |
157 |
|
158 |
# login and check that our table is displayed correctly |
159 |
$notices-> |
160 |
hasDeliveryNoteColumnInNoticesTable()-> |
161 |
hasTextInTableCell("Missing SMS number"); |
162 |
|
163 |
# now let's give the user a SMS number so we proceed to next step of failure |
164 |
ModMember( borrowernumber => $borrowers->{superuberadmin}->borrowernumber, |
165 |
smsalertnumber => 'just_some_number' ); |
166 |
|
167 |
# set first to sent and second to pending to check "duplicate" (Letters::_is_duplicate()) |
168 |
setMessageStatus($messages->{'A001@example.com'}->{message_id}, "sent"); |
169 |
setMessageStatus($messages->{'A002@example.com'}->{message_id}, "pending"); |
170 |
|
171 |
C4::Letters::SendQueuedMessages(); |
172 |
# A002 should go into failed: message is duplicate status |
173 |
|
174 |
# set A001 back to pending to check that we get no reply from driver |
175 |
setMessageStatus($messages->{'A001@example.com'}->{message_id}, "pending"); |
176 |
|
177 |
# let's send the message and check for delivery note, we should get a warning |
178 |
# for non-installed driver. |
179 |
C4::Letters::SendQueuedMessages(); |
180 |
|
181 |
$notices->navigateToNotices()-> |
182 |
hasTextInTableCell("Message is duplicate")-> |
183 |
hasTextInTableCell("No notes from SMS driver"); |
184 |
} |
185 |
|
186 |
sub CheckEmailMessages { |
187 |
my ($notices) = @_; |
188 |
|
189 |
$notices-> |
190 |
hasTextInTableCell("Unable to find an email address")-> |
191 |
hasTextInTableCell("Invalid borrowernumber")-> |
192 |
hasTextInTableCell("Connection refused"); #sendmail should also fail |
193 |
} |
194 |
sub setMessageStatus { |
195 |
my ($message_id, $status) = @_; |
196 |
my $dbh = C4::Context->dbh; |
197 |
|
198 |
my $sth = $dbh->prepare("UPDATE message_queue SET status=?, delivery_note='' WHERE message_id=?"); |
199 |
my $result = $sth->execute($status, $message_id); |
200 |
|
201 |
return $result; |
202 |
} |