Line 0
Link Here
|
|
|
1 |
package t::lib::TestObjects::MessageQueueFactory; |
2 |
|
3 |
# Copyright Vaara-kirjastot 2015 |
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 3 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 |
|
21 |
use Modern::Perl; |
22 |
use Carp; |
23 |
use Scalar::Util qw(blessed); |
24 |
|
25 |
use C4::Members; |
26 |
use C4::Letters; |
27 |
use Koha::Borrowers; |
28 |
|
29 |
use base qw(t::lib::TestObjects::ObjectFactory); |
30 |
|
31 |
use Koha::Exception::ObjectExists; |
32 |
|
33 |
sub new { |
34 |
my ($class) = @_; |
35 |
|
36 |
my $self = {}; |
37 |
bless($self, $class); |
38 |
return $self; |
39 |
} |
40 |
|
41 |
sub getDefaultHashKey { |
42 |
return 'from_address'; |
43 |
} |
44 |
sub getObjectType { |
45 |
return 'HASH'; |
46 |
} |
47 |
|
48 |
=head createTestGroup( $data [, $hashKey, $testContexts...] ) |
49 |
@OVERLOADED |
50 |
|
51 |
MessageQueueFactory creates a new message into message_queue table with 'pending' |
52 |
status. After testing, all messages created by the MessageQueueFactory will be |
53 |
deleted at tearDown. |
54 |
|
55 |
my $messages = t::lib::TestObjects::MessageQueueFactory->createTestGroup([ |
56 |
subject => "Test title", |
57 |
content => "Tessst content", |
58 |
cardnumber => $borrowers->{'superuberadmin'}->cardnumber, |
59 |
message_transport_type => 'sms', |
60 |
from_address => 'test@unique.com', |
61 |
}, |
62 |
], undef, $testContext1, $testContext2, $testContext3); |
63 |
|
64 |
#Do test stuff... |
65 |
|
66 |
t::lib::TestObjects::ObjectFactory->tearDownTestContext($testContext1); |
67 |
t::lib::TestObjects::ObjectFactory->tearDownTestContext($testContext2); |
68 |
t::lib::TestObjects::ObjectFactory->tearDownTestContext($testContext3); |
69 |
|
70 |
See t::lib::TestObjects::ObjectFactory for more documentation |
71 |
=cut |
72 |
|
73 |
sub handleTestObject { |
74 |
my ($class, $notice, $stashes) = @_; |
75 |
|
76 |
my $borrower = Koha::Borrowers->cast($notice->{cardnumber}); |
77 |
|
78 |
my $letter = { |
79 |
title => $notice->{subject} || '', |
80 |
content => $notice->{content}, |
81 |
content_type => $notice->{content_type}, |
82 |
letter_code => $notice->{letter_code}, |
83 |
}; |
84 |
my $message_id = C4::Letters::EnqueueLetter({ |
85 |
letter => $letter, |
86 |
borrowernumber => $borrower->borrowernumber, |
87 |
message_transport_type => $notice->{message_transport_type}, |
88 |
to_address => $notice->{to_address}, |
89 |
from_address => $notice->{from_address}, |
90 |
}); |
91 |
|
92 |
$notice->{message_id} = $message_id; |
93 |
|
94 |
return $notice; |
95 |
} |
96 |
|
97 |
=head validateAndPopulateDefaultValues |
98 |
@OVERLOAD |
99 |
|
100 |
Validates given Object parameters and makes sure that critical fields are given |
101 |
and populates defaults for missing values. |
102 |
=cut |
103 |
|
104 |
sub validateAndPopulateDefaultValues { |
105 |
my ($class, $object, $hashKey) = @_; |
106 |
$class->SUPER::validateAndPopulateDefaultValues($object, $hashKey); |
107 |
|
108 |
unless ($object->{cardnumber}) { |
109 |
Koha::Exception::BadParameter->throw(error => __PACKAGE__."->createTestGroup():> 'cardnumber' is a mandatory parameter!"); |
110 |
} |
111 |
unless ($object->{from_address}) { |
112 |
Koha::Exception::BadParameter->throw(error => __PACKAGE__."->createTestGroup():> 'from_address' is a mandatory parameter!"); |
113 |
} |
114 |
|
115 |
# Other required fields |
116 |
$object->{message_transport_type} = 'email' unless defined $object->{message_transport_type}; |
117 |
$object->{content} => "Example message content" unless defined $object->{content}; |
118 |
} |
119 |
|
120 |
=head deleteTestGroup |
121 |
@OVERLOADED |
122 |
|
123 |
my $records = createTestGroup(); |
124 |
##Do funky stuff |
125 |
deleteTestGroup($prefs); |
126 |
|
127 |
Removes the given test group from the DB. |
128 |
|
129 |
=cut |
130 |
|
131 |
sub deleteTestGroup { |
132 |
my ($class, $messages) = @_; |
133 |
|
134 |
my $schema = Koha::Database->new_schema(); |
135 |
while( my ($key, $msg) = each %$messages) { |
136 |
$schema->resultset('MessageQueue')->find({"message_id" => $msg->{message_id}})->delete(); |
137 |
} |
138 |
} |
139 |
|
140 |
sub _deleteTestGroupFromIdentifiers { |
141 |
my ($self, $testGroupIdentifiers) = @_; |
142 |
|
143 |
my $schema = Koha::Database->new_schema(); |
144 |
foreach my $key (@$testGroupIdentifiers) { |
145 |
$schema->resultset('MessageQueue')->find({"from_address" => $key})->delete(); |
146 |
} |
147 |
} |
148 |
|
149 |
|
150 |
1; |