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

(-)a/t/lib/TestObjects/MessageQueueFactory.pm (+149 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
45
=head createTestGroup( $data [, $hashKey, $testContexts...] )
46
@OVERLOADED
47
48
    MessageQueueFactory creates a new message into message_queue table with 'pending'
49
    status. After testing, all messages created by the MessageQueueFactory will be
50
    deleted at tearDown.
51
52
    my $messages = t::lib::TestObjects::MessageQueueFactory->createTestGroup([
53
                        {letter => {
54
                         title => "Test title",
55
                         content => "Tessst content",
56
                         },
57
                         cardnumber => $borrowers->{'superuberadmin'}->cardnumber,
58
                         message_transport_type => 'sms',
59
                         from_address => 'test@unique.com',
60
                         },
61
                    ], undef, $testContext1, $testContext2, $testContext3);
62
63
    #Do test stuff...
64
65
    t::lib::TestObjects::ObjectFactory->tearDownTestContext($testContext1);
66
    t::lib::TestObjects::ObjectFactory->tearDownTestContext($testContext2);
67
    t::lib::TestObjects::ObjectFactory->tearDownTestContext($testContext3);
68
69
See t::lib::TestObjects::ObjectFactory for more documentation
70
=cut
71
72
sub handleTestObject {
73
    my ($class, $notice, $stashes) = @_;
74
75
    my $borrower = Koha::Borrowers->cast($notice->{cardnumber});
76
77
    my $message_id = C4::Letters::EnqueueLetter({
78
        letter                 => $notice->{letter},
79
        borrowernumber         => $borrower->borrowernumber,
80
        message_transport_type => $notice->{message_transport_type},
81
        to_address             => $notice->{to_address},
82
        from_address           => $notice->{from_address},
83
    });
84
85
    $notice->{message_id} = $message_id;
86
87
    return $notice;
88
}
89
90
=head validateAndPopulateDefaultValues
91
@OVERLOAD
92
93
Validates given Object parameters and makes sure that critical fields are given
94
and populates defaults for missing values.
95
=cut
96
97
sub validateAndPopulateDefaultValues {
98
    my ($class, $object, $hashKey) = @_;
99
    $class->SUPER::validateAndPopulateDefaultValues($object, $hashKey);
100
101
    unless ($object->{cardnumber}) {
102
        Koha::Exception::BadParameter->throw(error => __PACKAGE__."->createTestGroup():> 'cardnumber' is a mandatory parameter!");
103
    }
104
    unless ($object->{from_address}) {
105
        Koha::Exception::BadParameter->throw(error => __PACKAGE__."->createTestGroup():> 'from_address' is a mandatory parameter!");
106
    }
107
    $object->{to_address} = 'nobody@example.com' unless $object->{to_address};
108
    $object->{message_transport_type} = 'email' unless $object->{message_transport_type};
109
    $object->{letter} = {
110
        title => "Example message title",
111
        content => "Example message content",
112
        'content-type' => "Example message content type",
113
    } unless $object->{letter};
114
    $object->{letter}->{title} = "Example message title" unless  $object->{letter}->{title};
115
    $object->{letter}->{content} = "Example message content" unless $object->{letter}->{content};
116
    $object->{letter}->{'content-type'} = "Example message content type" unless $object->{letter}->{'content-type'};
117
}
118
119
=head deleteTestGroup
120
@OVERLOADED
121
122
    my $records = createTestGroup();
123
    ##Do funky stuff
124
    deleteTestGroup($prefs);
125
126
Removes the given test group from the DB.
127
128
=cut
129
130
sub deleteTestGroup {
131
    my ($class, $messages) = @_;
132
133
    my $schema = Koha::Database->new_schema();
134
    while( my ($key, $msg) = each %$messages) {
135
        $schema->resultset('MessageQueue')->find({"message_id" => $msg->{message_id}})->delete();
136
    }
137
}
138
139
sub _deleteTestGroupFromIdentifiers {
140
    my ($self, $testGroupIdentifiers) = @_;
141
142
    my $schema = Koha::Database->new_schema();
143
    foreach my $key (@$testGroupIdentifiers) {
144
        $schema->resultset('MessageQueue')->find({"from_address" => $key})->delete();
145
    }
146
}
147
148
149
1;
(-)a/t/lib/TestObjects/ObjectFactory.pm (+5 lines)
Lines 175-180 sub tearDownTestContext { Link Here
175
        t::lib::TestObjects::SystemPreferenceFactory->deleteTestGroup($stash->{systempreference});
175
        t::lib::TestObjects::SystemPreferenceFactory->deleteTestGroup($stash->{systempreference});
176
        delete $stash->{systempreference};
176
        delete $stash->{systempreference};
177
    }
177
    }
178
    if ($stash->{messageQueue}) {
179
        require t::lib::TestObjects::MessageQueueFactory;
180
        t::lib::TestObjects::MessageQueueFactory->deleteTestGroup($stash->{messageQueue});
181
        delete $stash->{messageQueue};
182
    }
178
}
183
}
179
184
180
=head getHashKey
185
=head getHashKey
(-)a/t/lib/TestObjects/objectFactories.t (-1 / +50 lines)
Lines 47-52 use Koha::Serial::Subscription::Numberpatterns; Link Here
47
use Koha::Serial::Serials;
47
use Koha::Serial::Serials;
48
use t::lib::TestObjects::SystemPreferenceFactory;
48
use t::lib::TestObjects::SystemPreferenceFactory;
49
use C4::Context;
49
use C4::Context;
50
use t::lib::TestObjects::MessageQueueFactory;
51
use C4::Letters;
50
52
51
53
52
my $testContext = {}; #Gather all created Objects here so we can finally remove them all.
54
my $testContext = {}; #Gather all created Objects here so we can finally remove them all.
Lines 442-447 subtest 't::lib::TestObjects::SystemPreferenceFactory' => sub { Link Here
442
444
443
445
444
446
447
########## MessageQueueFactory subtests ##########
448
subtest 't::lib::TestObjects::MessageQueueFactory' => sub {
449
    my $subtestContext = {};
450
451
    my $messages = t::lib::TestObjects::MessageQueueFactory->createTestGroup([
452
            {letter => {
453
                title => "The quick brown fox",
454
                content => "Jumps over the lazy dog.",
455
                },
456
             cardnumber => '11A001',
457
             message_transport_type => 'sms',
458
             from_address => '11A001@example.com',
459
            },
460
        ], undef, $subtestContext);
461
    
462
    # check that the message exists in queue
463
    my $queued_messages = C4::Letters->_get_unsent_messages();
464
    
465
    my $found_testMessage = 0;
466
    foreach my $message (@$queued_messages){
467
        if ($message->{from_address} eq '11A001@example.com'){
468
            $found_testMessage = 1;
469
            last;
470
        }
471
    }
472
    
473
    ok($found_testMessage, 'MessageQueue \'11A001@example.com\', message_queue match.');
474
    
475
    # delete the queued message
476
    t::lib::TestObjects::MessageQueueFactory->deleteTestGroup($messages);
477
    
478
    # confirm the deletion
479
    $queued_messages = C4::Letters->_get_unsent_messages();
480
    
481
    $found_testMessage = 0;
482
    foreach my $message (@$queued_messages){
483
        if ($message->{from_address} eq '11A001@example.com'){
484
            $found_testMessage = 1;
485
            last;
486
        }
487
    }
488
    
489
    is($found_testMessage, 0, 'MessageQueue \'11A001@example.com\', deleted.');
490
    
491
};
492
493
494
445
########## Global test context subtests ##########
495
########## Global test context subtests ##########
446
subtest 't::lib::TestObjects::ObjectFactory clearing global test context' => sub {
496
subtest 't::lib::TestObjects::ObjectFactory clearing global test context' => sub {
447
    my $object11A001 = Koha::Borrowers->find({cardnumber => '11A001'});
497
    my $object11A001 = Koha::Borrowers->find({cardnumber => '11A001'});
448
- 

Return to bug 13906