View | Details | Raw Unified | Return to bug 13906
Collapse All | Expand All

(-)a/t/lib/TestObjects/MessageQueueFactory.pm (+152 lines)
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
                        {letter => {
57
                         title => "Test title",
58
                         content => "Tessst content",
59
                         },
60
                         cardnumber => $borrowers->{'superuberadmin'}->cardnumber,
61
                         message_transport_type => 'sms',
62
                         from_address => 'test@unique.com',
63
                         },
64
                    ], undef, $testContext1, $testContext2, $testContext3);
65
66
    #Do test stuff...
67
68
    t::lib::TestObjects::ObjectFactory->tearDownTestContext($testContext1);
69
    t::lib::TestObjects::ObjectFactory->tearDownTestContext($testContext2);
70
    t::lib::TestObjects::ObjectFactory->tearDownTestContext($testContext3);
71
72
See t::lib::TestObjects::ObjectFactory for more documentation
73
=cut
74
75
sub handleTestObject {
76
    my ($class, $notice, $stashes) = @_;
77
78
    my $borrower = Koha::Borrowers->cast($notice->{cardnumber});
79
80
    my $message_id = C4::Letters::EnqueueLetter({
81
        letter                 => $notice->{letter},
82
        borrowernumber         => $borrower->borrowernumber,
83
        message_transport_type => $notice->{message_transport_type},
84
        to_address             => $notice->{to_address},
85
        from_address           => $notice->{from_address},
86
    });
87
88
    $notice->{message_id} = $message_id;
89
90
    return $notice;
91
}
92
93
=head validateAndPopulateDefaultValues
94
@OVERLOAD
95
96
Validates given Object parameters and makes sure that critical fields are given
97
and populates defaults for missing values.
98
=cut
99
100
sub validateAndPopulateDefaultValues {
101
    my ($class, $object, $hashKey) = @_;
102
    $class->SUPER::validateAndPopulateDefaultValues($object, $hashKey);
103
104
    unless ($object->{cardnumber}) {
105
        Koha::Exception::BadParameter->throw(error => __PACKAGE__."->createTestGroup():> 'cardnumber' is a mandatory parameter!");
106
    }
107
    unless ($object->{from_address}) {
108
        Koha::Exception::BadParameter->throw(error => __PACKAGE__."->createTestGroup():> 'from_address' is a mandatory parameter!");
109
    }
110
    $object->{to_address} = 'nobody@example.com' unless defined $object->{to_address};
111
    $object->{message_transport_type} = 'email' unless defined $object->{message_transport_type};
112
    $object->{letter} = {
113
        title => "Example message title",
114
        content => "Example message content",
115
        'content-type' => "Example message content type",
116
    } unless defined $object->{letter};
117
    $object->{letter}->{title} = "Example message title" unless defined $object->{letter}->{title};
118
    $object->{letter}->{content} = "Example message content" unless defined $object->{letter}->{content};
119
    $object->{letter}->{'content-type'} = "Example message content type" unless defined $object->{letter}->{'content-type'};
120
}
121
122
=head deleteTestGroup
123
@OVERLOADED
124
125
    my $records = createTestGroup();
126
    ##Do funky stuff
127
    deleteTestGroup($prefs);
128
129
Removes the given test group from the DB.
130
131
=cut
132
133
sub deleteTestGroup {
134
    my ($class, $messages) = @_;
135
136
    my $schema = Koha::Database->new_schema();
137
    while( my ($key, $msg) = each %$messages) {
138
        $schema->resultset('MessageQueue')->find({"message_id" => $msg->{message_id}})->delete();
139
    }
140
}
141
142
sub _deleteTestGroupFromIdentifiers {
143
    my ($self, $testGroupIdentifiers) = @_;
144
145
    my $schema = Koha::Database->new_schema();
146
    foreach my $key (@$testGroupIdentifiers) {
147
        $schema->resultset('MessageQueue')->find({"from_address" => $key})->delete();
148
    }
149
}
150
151
152
1;
(-)a/t/lib/TestObjects/ObjectFactory.pm (+5 lines)
Lines 181-186 sub tearDownTestContext { Link Here
181
        t::lib::TestObjects::SystemPreferenceFactory->deleteTestGroup($stash->{systempreference});
181
        t::lib::TestObjects::SystemPreferenceFactory->deleteTestGroup($stash->{systempreference});
182
        delete $stash->{systempreference};
182
        delete $stash->{systempreference};
183
    }
183
    }
184
    if ($stash->{messageQueue}) {
185
        require t::lib::TestObjects::MessageQueueFactory;
186
        t::lib::TestObjects::MessageQueueFactory->deleteTestGroup($stash->{messageQueue});
187
        delete $stash->{messageQueue};
188
    }
184
}
189
}
185
190
186
=head getHashKey
191
=head getHashKey
(-)a/t/lib/TestObjects/objectFactories.t (-1 / +50 lines)
Lines 50-55 use Koha::Serial::Subscription::Numberpatterns; Link Here
50
use Koha::Serial::Serials;
50
use Koha::Serial::Serials;
51
use t::lib::TestObjects::SystemPreferenceFactory;
51
use t::lib::TestObjects::SystemPreferenceFactory;
52
use C4::Context;
52
use C4::Context;
53
use t::lib::TestObjects::MessageQueueFactory;
54
use C4::Letters;
53
55
54
56
55
my $testContext = {}; #Gather all created Objects here so we can finally remove them all.
57
my $testContext = {}; #Gather all created Objects here so we can finally remove them all.
Lines 510-515 sub testSystemPreferenceFactory { Link Here
510
512
511
513
512
514
515
########## MessageQueueFactory subtests ##########
516
subtest 't::lib::TestObjects::MessageQueueFactory' => sub {
517
    my $subtestContext = {};
518
519
    my $messages = t::lib::TestObjects::MessageQueueFactory->createTestGroup([
520
            {letter => {
521
                title => "The quick brown fox",
522
                content => "Jumps over the lazy dog.",
523
                },
524
             cardnumber => '11A001',
525
             message_transport_type => 'sms',
526
             from_address => '11A001@example.com',
527
            },
528
        ], undef, $subtestContext);
529
530
    # check that the message exists in queue
531
    my $queued_messages = C4::Letters->_get_unsent_messages();
532
533
    my $found_testMessage = 0;
534
    foreach my $message (@$queued_messages){
535
        if ($message->{from_address} eq '11A001@example.com'){
536
            $found_testMessage = 1;
537
            last;
538
        }
539
    }
540
541
    ok($found_testMessage, 'MessageQueue \'11A001@example.com\', message_queue match.');
542
543
    # delete the queued message
544
    t::lib::TestObjects::MessageQueueFactory->deleteTestGroup($messages);
545
546
    # confirm the deletion
547
    $queued_messages = C4::Letters->_get_unsent_messages();
548
549
    $found_testMessage = 0;
550
    foreach my $message (@$queued_messages){
551
        if ($message->{from_address} eq '11A001@example.com'){
552
            $found_testMessage = 1;
553
            last;
554
        }
555
    }
556
557
    is($found_testMessage, 0, 'MessageQueue \'11A001@example.com\', deleted.');
558
559
};
560
561
562
513
########## Global test context subtests ##########
563
########## Global test context subtests ##########
514
subtest 't::lib::TestObjects::ObjectFactory clearing global test context' => \&testGlobalSubtestContext;
564
subtest 't::lib::TestObjects::ObjectFactory clearing global test context' => \&testGlobalSubtestContext;
515
sub testGlobalSubtestContext {
565
sub testGlobalSubtestContext {
516
- 

Return to bug 13906