Line 0
Link Here
|
|
|
1 |
#!/usr/bin/perl |
2 |
|
3 |
# Copyright 2017 Koha-Suomi Oy |
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 |
|
20 |
use Modern::Perl; |
21 |
|
22 |
use Test::More tests => 2; |
23 |
|
24 |
use t::lib::Mocks; |
25 |
use t::lib::TestBuilder; |
26 |
|
27 |
use Koha::Notice::Templates; |
28 |
use Koha::Patron::Categories; |
29 |
use Koha::Patron::Message::Attributes; |
30 |
use Koha::Patron::Message::Preferences; |
31 |
use Koha::Patron::Message::Transport::Types; |
32 |
use Koha::Patron::Message::Transports; |
33 |
use Koha::Patrons; |
34 |
|
35 |
my $schema = Koha::Database->new->schema; |
36 |
my $builder = t::lib::TestBuilder->new; |
37 |
|
38 |
subtest 'Test class imports' => sub { |
39 |
plan tests => 2; |
40 |
|
41 |
use_ok('Koha::Patron::Message::Transport::Preference'); |
42 |
use_ok('Koha::Patron::Message::Transport::Preferences'); |
43 |
}; |
44 |
|
45 |
subtest 'Test Koha::Patron::Message::Transport::Preferences' => sub { |
46 |
plan tests => 2; |
47 |
|
48 |
$schema->storage->txn_begin; |
49 |
|
50 |
my $attribute = build_a_test_attribute(); |
51 |
my $mtt = build_a_test_transport_type(); |
52 |
my $letter = build_a_test_letter({ |
53 |
mtt => $mtt->message_transport_type |
54 |
}); |
55 |
|
56 |
subtest 'For a patron' => sub { |
57 |
my $patron = build_a_test_patron(); |
58 |
my $preference = Koha::Patron::Message::Preference->new({ |
59 |
borrowernumber => $patron->borrowernumber, |
60 |
message_attribute_id => $attribute->message_attribute_id, |
61 |
wants_digest => 0, |
62 |
days_in_advance => undef, |
63 |
})->store; |
64 |
|
65 |
my $pref_id = $preference->borrower_message_preference_id; |
66 |
my $transport_pref = Koha::Patron::Message::Transport::Preference->new({ |
67 |
borrower_message_preference_id => $pref_id, |
68 |
message_transport_type => $mtt->message_transport_type, |
69 |
})->store; |
70 |
is(ref($transport_pref), 'Koha::Patron::Message::Transport::Preference', |
71 |
'Added a new messaging transport preference for patron.'); |
72 |
|
73 |
$transport_pref->delete; |
74 |
is(Koha::Patron::Message::Transport::Preferences->search({ |
75 |
borrower_message_preference_id => $pref_id, |
76 |
message_transport_type => $mtt->message_transport_type, |
77 |
})->count, 0, 'Deleted the messaging transport preference.'); |
78 |
}; |
79 |
|
80 |
subtest 'For a category' => sub { |
81 |
my $category = build_a_test_category(); |
82 |
my $preference = Koha::Patron::Message::Preference->new({ |
83 |
categorycode => $category->categorycode, |
84 |
message_attribute_id => $attribute->message_attribute_id, |
85 |
wants_digest => 0, |
86 |
days_in_advance => undef, |
87 |
})->store; |
88 |
|
89 |
my $pref_id = $preference->borrower_message_preference_id; |
90 |
my $transport_pref = Koha::Patron::Message::Transport::Preference->new({ |
91 |
borrower_message_preference_id => $pref_id, |
92 |
message_transport_type => $mtt->message_transport_type, |
93 |
})->store; |
94 |
is(ref($transport_pref), 'Koha::Patron::Message::Transport::Preference', |
95 |
'Added a new messaging transport preference for category.'); |
96 |
|
97 |
$transport_pref->delete; |
98 |
is(Koha::Patron::Message::Transport::Preferences->search({ |
99 |
borrower_message_preference_id => $pref_id, |
100 |
message_transport_type => $mtt->message_transport_type, |
101 |
})->count, 0, 'Deleted the messaging transport preference.'); |
102 |
}; |
103 |
|
104 |
$schema->storage->txn_rollback; |
105 |
}; |
106 |
|
107 |
sub build_a_test_attribute { |
108 |
my ($params) = @_; |
109 |
|
110 |
$params->{takes_days} = $params->{takes_days} && $params->{takes_days} > 0 |
111 |
? 1 : 0; |
112 |
|
113 |
my $attribute = $builder->build({ |
114 |
source => 'MessageAttribute', |
115 |
value => $params, |
116 |
}); |
117 |
|
118 |
return Koha::Patron::Message::Attributes->find( |
119 |
$attribute->{message_attribute_id} |
120 |
); |
121 |
} |
122 |
|
123 |
sub build_a_test_category { |
124 |
my $categorycode = $builder->build({ |
125 |
source => 'Category' })->{categorycode}; |
126 |
|
127 |
return Koha::Patron::Categories->find($categorycode); |
128 |
} |
129 |
|
130 |
sub build_a_test_letter { |
131 |
my ($params) = @_; |
132 |
|
133 |
my $mtt = $params->{mtt} ? $params->{mtt} : 'email'; |
134 |
my $branchcode = $builder->build({ |
135 |
source => 'Branch' })->{branchcode}; |
136 |
my $letter = $builder->build({ |
137 |
source => 'Letter', |
138 |
value => { |
139 |
branchcode => '', |
140 |
is_html => 0, |
141 |
message_transport_type => $mtt |
142 |
} |
143 |
}); |
144 |
|
145 |
return Koha::Notice::Templates->find({ |
146 |
module => $letter->{module}, |
147 |
code => $letter->{code}, |
148 |
branchcode => $letter->{branchcode}, |
149 |
}); |
150 |
} |
151 |
|
152 |
sub build_a_test_patron { |
153 |
my $categorycode = $builder->build({ |
154 |
source => 'Category' })->{categorycode}; |
155 |
my $branchcode = $builder->build({ |
156 |
source => 'Branch' })->{branchcode}; |
157 |
my $borrowernumber = $builder->build({ |
158 |
source => 'Borrower' })->{borrowernumber}; |
159 |
|
160 |
return Koha::Patrons->find($borrowernumber); |
161 |
} |
162 |
|
163 |
sub build_a_test_transport_type { |
164 |
my $mtt = $builder->build({ |
165 |
source => 'MessageTransportType' }); |
166 |
|
167 |
return Koha::Patron::Message::Transport::Types->find( |
168 |
$mtt->{message_transport_type} |
169 |
); |
170 |
} |
171 |
|
172 |
1; |