|
Line 0
Link Here
|
|
|
1 |
From 5cb18bd6b778730cd86e44f2920e75b167854b3c Mon Sep 17 00:00:00 2001 |
| 2 |
From: Lari Taskula <lari.taskula@jns.fi> |
| 3 |
Date: Fri, 21 Oct 2016 17:26:24 +0300 |
| 4 |
Subject: [PATCH] Bug 17499: Add Koha-objects for messaging preferences |
| 5 |
|
| 6 |
This patch adds Koha-objects for messaging preferences. |
| 7 |
|
| 8 |
Adds simple validation for messaging preferences. |
| 9 |
|
| 10 |
The validation includes |
| 11 |
- throw exception if both borrowernumber or categorycode is given for a new pref |
| 12 |
- throw exception if patron for the given borrowernumber is not found |
| 13 |
- throw exception if category for the given categorycode is not found |
| 14 |
- throw exception if days in advance cannot be configured but is given |
| 15 |
- throw exception if days in advance configuration is invalid (value between 0-30) |
| 16 |
- throw exception if digest is not available but attempted to set on |
| 17 |
- throw exception if digest must be enabled but attempted to set off |
| 18 |
- throw exception on duplicate messaging preference |
| 19 |
|
| 20 |
Adds a method for getting available messaging options. |
| 21 |
|
| 22 |
Adds a method for setting default messaging preferenes. |
| 23 |
$patron->set_default_messaging_preferences (where $patron is a Koha::Patron) |
| 24 |
...or... |
| 25 |
Koha::Patron::Message::Preference->new_from_default({ |
| 26 |
borrowernumber => 123, |
| 27 |
categorycode => "ABC", |
| 28 |
message_attribute_id => 1, |
| 29 |
}); |
| 30 |
|
| 31 |
Since messaging preference is a feature that has multiple related database tables, |
| 32 |
usage via Koha-objects is sometimes frustrating. This patch adds easy access to |
| 33 |
message transport types via |
| 34 |
$preference->message_transport_types (for getting) |
| 35 |
$preference->set({ message_transport_types => ['email', 'sms'] }) (for setting) |
| 36 |
(also supports other calling conventions, see documentation for more) |
| 37 |
|
| 38 |
Adds optional parameter message_name for Koha::Patron::Message::Preferences->find |
| 39 |
and ->search. Simplifies the Koha-object usage by allowing developer to skip joins |
| 40 |
and / or querying the message name via attribute_id from message_attributes table. |
| 41 |
|
| 42 |
Includes test coverage for basic usage. |
| 43 |
|
| 44 |
To test: |
| 45 |
1. prove t/db_dependent/Koha/Patron/Message/* |
| 46 |
|
| 47 |
Following Bug 17499, check also Bug 18595 that replaces C4::Members::Messaging |
| 48 |
with these new Koha-objects. |
| 49 |
|
| 50 |
Signed-off-by: Dominic Pichette <dominic@inlibro.com> |
| 51 |
--- |
| 52 |
Koha/Exceptions.pm | 8 +- |
| 53 |
Koha/Patron.pm | 42 ++ |
| 54 |
Koha/Patron/Message/Attribute.pm | 50 ++ |
| 55 |
Koha/Patron/Message/Attributes.pm | 55 ++ |
| 56 |
Koha/Patron/Message/Preference.pm | 451 +++++++++++++ |
| 57 |
Koha/Patron/Message/Preferences.pm | 146 +++++ |
| 58 |
Koha/Patron/Message/Transport.pm | 50 ++ |
| 59 |
Koha/Patron/Message/Transport/Preference.pm | 51 ++ |
| 60 |
Koha/Patron/Message/Transport/Preferences.pm | 56 ++ |
| 61 |
Koha/Patron/Message/Transport/Type.pm | 51 ++ |
| 62 |
Koha/Patron/Message/Transport/Types.pm | 56 ++ |
| 63 |
Koha/Patron/Message/Transports.pm | 55 ++ |
| 64 |
t/db_dependent/Koha/Patron/Message/Attributes.t | 74 +++ |
| 65 |
t/db_dependent/Koha/Patron/Message/Preferences.t | 719 +++++++++++++++++++++ |
| 66 |
.../Koha/Patron/Message/Transport/Preferences.t | 179 +++++ |
| 67 |
.../Koha/Patron/Message/Transport/Types.t | 54 ++ |
| 68 |
t/db_dependent/Koha/Patron/Message/Transports.t | 119 ++++ |
| 69 |
17 files changed, 2215 insertions(+), 1 deletion(-) |
| 70 |
create mode 100644 Koha/Patron/Message/Attribute.pm |
| 71 |
create mode 100644 Koha/Patron/Message/Attributes.pm |
| 72 |
create mode 100644 Koha/Patron/Message/Preference.pm |
| 73 |
create mode 100644 Koha/Patron/Message/Preferences.pm |
| 74 |
create mode 100644 Koha/Patron/Message/Transport.pm |
| 75 |
create mode 100644 Koha/Patron/Message/Transport/Preference.pm |
| 76 |
create mode 100644 Koha/Patron/Message/Transport/Preferences.pm |
| 77 |
create mode 100644 Koha/Patron/Message/Transport/Type.pm |
| 78 |
create mode 100644 Koha/Patron/Message/Transport/Types.pm |
| 79 |
create mode 100644 Koha/Patron/Message/Transports.pm |
| 80 |
create mode 100644 t/db_dependent/Koha/Patron/Message/Attributes.t |
| 81 |
create mode 100644 t/db_dependent/Koha/Patron/Message/Preferences.t |
| 82 |
create mode 100644 t/db_dependent/Koha/Patron/Message/Transport/Preferences.t |
| 83 |
create mode 100644 t/db_dependent/Koha/Patron/Message/Transport/Types.t |
| 84 |
create mode 100644 t/db_dependent/Koha/Patron/Message/Transports.t |
| 85 |
|
| 86 |
diff --git a/Koha/Exceptions.pm b/Koha/Exceptions.pm |
| 87 |
index 528a151..c212653 100644 |
| 88 |
--- a/Koha/Exceptions.pm |
| 89 |
+++ b/Koha/Exceptions.pm |
| 90 |
@@ -27,7 +27,13 @@ use Exception::Class ( |
| 91 |
}, |
| 92 |
'Koha::Exceptions::MissingParameter' => { |
| 93 |
isa => 'Koha::Exceptions::Exception', |
| 94 |
- description => 'A required parameter is missing' |
| 95 |
+ description => 'A required parameter is missing', |
| 96 |
+ fields => ['parameter'], |
| 97 |
+ }, |
| 98 |
+ 'Koha::Exceptions::TooManyParameters' => { |
| 99 |
+ isa => 'Koha::Exceptions::Exception', |
| 100 |
+ description => 'Too many parameters given', |
| 101 |
+ fields => ['parameter'], |
| 102 |
}, |
| 103 |
'Koha::Exceptions::WrongParameter' => { |
| 104 |
isa => 'Koha::Exceptions::Exception', |
| 105 |
diff --git a/Koha/Patron.pm b/Koha/Patron.pm |
| 106 |
index daa972c..a00f05c 100644 |
| 107 |
--- a/Koha/Patron.pm |
| 108 |
+++ b/Koha/Patron.pm |
| 109 |
@@ -33,6 +33,7 @@ use Koha::Patron::Categories; |
| 110 |
use Koha::Patron::HouseboundProfile; |
| 111 |
use Koha::Patron::HouseboundRole; |
| 112 |
use Koha::Patron::Images; |
| 113 |
+use Koha::Patron::Message::Preferences; |
| 114 |
use Koha::Patrons; |
| 115 |
use Koha::Virtualshelves; |
| 116 |
use Koha::Club::Enrollments; |
| 117 |
@@ -656,6 +657,47 @@ sub account_locked { |
| 118 |
and $self->login_attempts >= $FailedLoginAttempts )? 1 : 0; |
| 119 |
} |
| 120 |
|
| 121 |
+=head3 set_default_messaging_preferences |
| 122 |
+ |
| 123 |
+ $patron->set_default_messaging_preferences |
| 124 |
+ |
| 125 |
+Sets default messaging preferences on patron. |
| 126 |
+ |
| 127 |
+See Koha::Patron::Message::Preference(s) for more documentation, especially on |
| 128 |
+thrown exceptions. |
| 129 |
+ |
| 130 |
+=cut |
| 131 |
+ |
| 132 |
+sub set_default_messaging_preferences { |
| 133 |
+ my ($self, $categorycode) = @_; |
| 134 |
+ |
| 135 |
+ my $options = Koha::Patron::Message::Preferences->get_options; |
| 136 |
+ |
| 137 |
+ foreach my $option (@$options) { |
| 138 |
+ # Check that this option has preference configuration for this category |
| 139 |
+ unless (Koha::Patron::Message::Preferences->search({ |
| 140 |
+ message_attribute_id => $option->{message_attribute_id}, |
| 141 |
+ categorycode => $categorycode || $self->categorycode, |
| 142 |
+ })->count) { |
| 143 |
+ next; |
| 144 |
+ } |
| 145 |
+ |
| 146 |
+ # Delete current setting |
| 147 |
+ Koha::Patron::Message::Preferences->search({ |
| 148 |
+ borrowernumber => $self->borrowernumber, |
| 149 |
+ message_attribute_id => $option->{message_attribute_id}, |
| 150 |
+ })->delete; |
| 151 |
+ |
| 152 |
+ Koha::Patron::Message::Preference->new_from_default({ |
| 153 |
+ borrowernumber => $self->borrowernumber, |
| 154 |
+ categorycode => $categorycode || $self->categorycode, |
| 155 |
+ message_attribute_id => $option->{message_attribute_id}, |
| 156 |
+ }); |
| 157 |
+ } |
| 158 |
+ |
| 159 |
+ return $self; |
| 160 |
+} |
| 161 |
+ |
| 162 |
=head3 type |
| 163 |
|
| 164 |
=cut |
| 165 |
diff --git a/Koha/Patron/Message/Attribute.pm b/Koha/Patron/Message/Attribute.pm |
| 166 |
new file mode 100644 |
| 167 |
index 0000000..9d9004c |
| 168 |
--- /dev/null |
| 169 |
+++ b/Koha/Patron/Message/Attribute.pm |
| 170 |
@@ -0,0 +1,50 @@ |
| 171 |
+package Koha::Patron::Message::Attribute; |
| 172 |
+ |
| 173 |
+# Copyright Koha-Suomi Oy 2016 |
| 174 |
+# |
| 175 |
+# This file is part of Koha. |
| 176 |
+# |
| 177 |
+# Koha is free software; you can redistribute it and/or modify it under the |
| 178 |
+# terms of the GNU General Public License as published by the Free Software |
| 179 |
+# Foundation; either version 3 of the License, or (at your option) any later |
| 180 |
+# version.a |
| 181 |
+# |
| 182 |
+# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
| 183 |
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
| 184 |
+# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 185 |
+# |
| 186 |
+# You should have received a copy of the GNU General Public License along |
| 187 |
+# with Koha; if not, write to the Free Software Foundation, Inc., |
| 188 |
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 189 |
+ |
| 190 |
+use Modern::Perl; |
| 191 |
+ |
| 192 |
+use Koha::Database; |
| 193 |
+ |
| 194 |
+use base qw(Koha::Object); |
| 195 |
+ |
| 196 |
+=head1 NAME |
| 197 |
+ |
| 198 |
+Koha::Patron::Message::Attribute - Koha Patron Message Attribute object class |
| 199 |
+ |
| 200 |
+=head1 API |
| 201 |
+ |
| 202 |
+=head2 Class Methods |
| 203 |
+ |
| 204 |
+=cut |
| 205 |
+ |
| 206 |
+=head3 type |
| 207 |
+ |
| 208 |
+=cut |
| 209 |
+ |
| 210 |
+sub _type { |
| 211 |
+ return 'MessageAttribute'; |
| 212 |
+} |
| 213 |
+ |
| 214 |
+=head1 AUTHOR |
| 215 |
+ |
| 216 |
+Lari Taskula <lari.taskula@jns.fi> |
| 217 |
+ |
| 218 |
+=cut |
| 219 |
+ |
| 220 |
+1; |
| 221 |
diff --git a/Koha/Patron/Message/Attributes.pm b/Koha/Patron/Message/Attributes.pm |
| 222 |
new file mode 100644 |
| 223 |
index 0000000..a2df4e6 |
| 224 |
--- /dev/null |
| 225 |
+++ b/Koha/Patron/Message/Attributes.pm |
| 226 |
@@ -0,0 +1,55 @@ |
| 227 |
+package Koha::Patron::Message::Attributes; |
| 228 |
+ |
| 229 |
+# Copyright Koha-Suomi Oy 2016 |
| 230 |
+# |
| 231 |
+# This file is part of Koha. |
| 232 |
+# |
| 233 |
+# Koha is free software; you can redistribute it and/or modify it under the |
| 234 |
+# terms of the GNU General Public License as published by the Free Software |
| 235 |
+# Foundation; either version 3 of the License, or (at your option) any later |
| 236 |
+# version. |
| 237 |
+# |
| 238 |
+# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
| 239 |
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
| 240 |
+# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 241 |
+# |
| 242 |
+# You should have received a copy of the GNU General Public License along |
| 243 |
+# with Koha; if not, write to the Free Software Foundation, Inc., |
| 244 |
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 245 |
+ |
| 246 |
+use Modern::Perl; |
| 247 |
+ |
| 248 |
+use Koha::Database; |
| 249 |
+use Koha::Patron::Message::Attribute; |
| 250 |
+ |
| 251 |
+use base qw(Koha::Objects); |
| 252 |
+ |
| 253 |
+=head1 NAME |
| 254 |
+ |
| 255 |
+Koha::Patron::Message::Attributes - Koha Patron Message Attributes object class |
| 256 |
+ |
| 257 |
+=head1 API |
| 258 |
+ |
| 259 |
+=head2 Class Methods |
| 260 |
+ |
| 261 |
+=cut |
| 262 |
+ |
| 263 |
+=head3 type |
| 264 |
+ |
| 265 |
+=cut |
| 266 |
+ |
| 267 |
+sub _type { |
| 268 |
+ return 'MessageAttribute'; |
| 269 |
+} |
| 270 |
+ |
| 271 |
+sub object_class { |
| 272 |
+ return 'Koha::Patron::Message::Attribute'; |
| 273 |
+} |
| 274 |
+ |
| 275 |
+=head1 AUTHOR |
| 276 |
+ |
| 277 |
+Lari Taskula <lari.taskula@jns.fi> |
| 278 |
+ |
| 279 |
+=cut |
| 280 |
+ |
| 281 |
+1; |
| 282 |
diff --git a/Koha/Patron/Message/Preference.pm b/Koha/Patron/Message/Preference.pm |
| 283 |
new file mode 100644 |
| 284 |
index 0000000..db96b2a |
| 285 |
--- /dev/null |
| 286 |
+++ b/Koha/Patron/Message/Preference.pm |
| 287 |
@@ -0,0 +1,451 @@ |
| 288 |
+package Koha::Patron::Message::Preference; |
| 289 |
+ |
| 290 |
+# Copyright Koha-Suomi Oy 2016 |
| 291 |
+# |
| 292 |
+# This file is part of Koha. |
| 293 |
+# |
| 294 |
+# Koha is free software; you can redistribute it and/or modify it under the |
| 295 |
+# terms of the GNU General Public License as published by the Free Software |
| 296 |
+# Foundation; either version 3 of the License, or (at your option) any later |
| 297 |
+# version.a |
| 298 |
+# |
| 299 |
+# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
| 300 |
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
| 301 |
+# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 302 |
+# |
| 303 |
+# You should have received a copy of the GNU General Public License along |
| 304 |
+# with Koha; if not, write to the Free Software Foundation, Inc., |
| 305 |
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 306 |
+ |
| 307 |
+use Modern::Perl; |
| 308 |
+ |
| 309 |
+use Koha::Database; |
| 310 |
+use Koha::Exceptions; |
| 311 |
+use Koha::Patron::Categories; |
| 312 |
+use Koha::Patron::Message::Attributes; |
| 313 |
+use Koha::Patron::Message::Preferences; |
| 314 |
+use Koha::Patron::Message::Transport::Preferences; |
| 315 |
+use Koha::Patron::Message::Transport::Types; |
| 316 |
+use Koha::Patron::Message::Transports; |
| 317 |
+use Koha::Patrons; |
| 318 |
+ |
| 319 |
+use base qw(Koha::Object); |
| 320 |
+ |
| 321 |
+=head1 NAME |
| 322 |
+ |
| 323 |
+Koha::Patron::Message::Preference - Koha Patron Message Preference object class |
| 324 |
+ |
| 325 |
+=head1 API |
| 326 |
+ |
| 327 |
+=head2 Class Methods |
| 328 |
+ |
| 329 |
+=cut |
| 330 |
+ |
| 331 |
+=head3 new |
| 332 |
+ |
| 333 |
+my $preference = Koha::Patron::Message::Preference->new({ |
| 334 |
+ borrowernumber => 123, |
| 335 |
+ #categorycode => 'ABC', |
| 336 |
+ message_attribute_id => 4, |
| 337 |
+ message_transport_types => ['email', 'sms'], # see documentation below |
| 338 |
+ wants_digest => 1, |
| 339 |
+ days_in_advance => 7, |
| 340 |
+}); |
| 341 |
+ |
| 342 |
+Takes either borrowernumber or categorycode, but not both. |
| 343 |
+ |
| 344 |
+days_in_advance may not be available. See message_attributes table for takes_days |
| 345 |
+configuration. |
| 346 |
+ |
| 347 |
+wants_digest may not be available. See message_transports table for is_digest |
| 348 |
+configuration. |
| 349 |
+ |
| 350 |
+You can instantiate a new object without custom validation errors, but when |
| 351 |
+storing, validation may throw exceptions. See C<validate()> for more |
| 352 |
+documentation. |
| 353 |
+ |
| 354 |
+C<message_transport_types> is a parameter that is not actually a column in this |
| 355 |
+Koha-object. Given this parameter, the message transport types will be added as |
| 356 |
+related transport types for this object. For get and set, you can access them via |
| 357 |
+subroutine C<message_transport_types()> in this class. |
| 358 |
+ |
| 359 |
+=cut |
| 360 |
+ |
| 361 |
+sub new { |
| 362 |
+ my ($class, $params) = @_; |
| 363 |
+ |
| 364 |
+ my $types = $params->{'message_transport_types'}; |
| 365 |
+ delete $params->{'message_transport_types'}; |
| 366 |
+ |
| 367 |
+ my $self = $class->SUPER::new($params); |
| 368 |
+ |
| 369 |
+ $self->_set_message_transport_types($types); |
| 370 |
+ |
| 371 |
+ return $self; |
| 372 |
+} |
| 373 |
+ |
| 374 |
+=head3 new_from_default |
| 375 |
+ |
| 376 |
+my $preference = Koha::Patron::Message::Preference->new_from_default({ |
| 377 |
+ borrowernumber => 123, |
| 378 |
+ categorycode => 'ABC', # if not given, patron's categorycode will be used |
| 379 |
+ message_attribute_id => 1, |
| 380 |
+}); |
| 381 |
+ |
| 382 |
+NOTE: This subroutine initializes and STORES the object (in order to set |
| 383 |
+message transport types for the preference), so no need to call ->store when |
| 384 |
+preferences are initialized via this method. |
| 385 |
+ |
| 386 |
+Stores default messaging preference for C<categorycode> to patron for given |
| 387 |
+C<message_attribute_id>. |
| 388 |
+ |
| 389 |
+Throws Koha::Exceptions::MissingParameter if any of following is missing: |
| 390 |
+- borrowernumber |
| 391 |
+- message_attribute_id |
| 392 |
+ |
| 393 |
+Throws Koha::Exceptions::ObjectNotFound if default preferences are not found. |
| 394 |
+ |
| 395 |
+=cut |
| 396 |
+ |
| 397 |
+sub new_from_default { |
| 398 |
+ my ($class, $params) = @_; |
| 399 |
+ |
| 400 |
+ my @required = qw(borrowernumber message_attribute_id); |
| 401 |
+ foreach my $p (@required) { |
| 402 |
+ Koha::Exceptions::MissingParameter->throw( |
| 403 |
+ error => 'Missing required parameter.', |
| 404 |
+ parameter => $p, |
| 405 |
+ ) unless exists $params->{$p}; |
| 406 |
+ } |
| 407 |
+ unless ($params->{'categorycode'}) { |
| 408 |
+ my $patron = Koha::Patrons->find($params->{borrowernumber}); |
| 409 |
+ $params->{'categorycode'} = $patron->categorycode; |
| 410 |
+ } |
| 411 |
+ |
| 412 |
+ my $default = Koha::Patron::Message::Preferences->find({ |
| 413 |
+ categorycode => $params->{'categorycode'}, |
| 414 |
+ message_attribute_id => $params->{'message_attribute_id'}, |
| 415 |
+ }); |
| 416 |
+ Koha::Exceptions::ObjectNotFound->throw( |
| 417 |
+ error => 'Default messaging preference for given categorycode and' |
| 418 |
+ .' message_attribute_id cannot be found.', |
| 419 |
+ ) unless $default; |
| 420 |
+ $default = $default->unblessed; |
| 421 |
+ |
| 422 |
+ # Add a new messaging preference for patron |
| 423 |
+ my $self = $class->SUPER::new({ |
| 424 |
+ borrowernumber => $params->{'borrowernumber'}, |
| 425 |
+ message_attribute_id => $default->{'message_attribute_id'}, |
| 426 |
+ days_in_advance => $default->{'days_in_advance'}, |
| 427 |
+ wants_digest => $default->{'wants_digest'}, |
| 428 |
+ })->store; |
| 429 |
+ |
| 430 |
+ # Set default messaging transport types |
| 431 |
+ my $default_transport_types = |
| 432 |
+ Koha::Patron::Message::Transport::Preferences->search({ |
| 433 |
+ borrower_message_preference_id => |
| 434 |
+ $default->{'borrower_message_preference_id'} |
| 435 |
+ }); |
| 436 |
+ while (my $transport = $default_transport_types->next) { |
| 437 |
+ Koha::Patron::Message::Transport::Preference->new({ |
| 438 |
+ borrower_message_preference_id => $self->borrower_message_preference_id, |
| 439 |
+ message_transport_type => $transport->message_transport_type, |
| 440 |
+ })->store; |
| 441 |
+ } |
| 442 |
+ |
| 443 |
+ return $self; |
| 444 |
+} |
| 445 |
+ |
| 446 |
+=head3 message_name |
| 447 |
+ |
| 448 |
+$preference->message_name |
| 449 |
+ |
| 450 |
+Gets message_name for this messaging preference. |
| 451 |
+ |
| 452 |
+Setter not implemented. |
| 453 |
+ |
| 454 |
+=cut |
| 455 |
+ |
| 456 |
+sub message_name { |
| 457 |
+ my ($self) = @_; |
| 458 |
+ |
| 459 |
+ if ($self->{'_message_name'}) { |
| 460 |
+ return $self->{'_message_name'}; |
| 461 |
+ } |
| 462 |
+ $self->{'_message_name'} = Koha::Patron::Message::Attributes->find({ |
| 463 |
+ message_attribute_id => $self->message_attribute_id, |
| 464 |
+ })->message_name; |
| 465 |
+ return $self->{'_message_name'}; |
| 466 |
+} |
| 467 |
+ |
| 468 |
+=head3 message_transport_types |
| 469 |
+ |
| 470 |
+$preference->message_transport_types |
| 471 |
+Returns a HASHREF of message transport types for this messaging preference, e.g. |
| 472 |
+if ($preference->message_transport_types->{'email'}) { |
| 473 |
+ # email is one of the transport preferences |
| 474 |
+} |
| 475 |
+ |
| 476 |
+$preference->message_transport_types('email', 'sms'); |
| 477 |
+Sets the given message transport types for this messaging preference |
| 478 |
+ |
| 479 |
+=cut |
| 480 |
+ |
| 481 |
+sub message_transport_types { |
| 482 |
+ my $self = shift; |
| 483 |
+ |
| 484 |
+ unless (@_) { |
| 485 |
+ if ($self->{'_message_transport_types'}) { |
| 486 |
+ return $self->{'_message_transport_types'}; |
| 487 |
+ } |
| 488 |
+ map { |
| 489 |
+ my $transport = Koha::Patron::Message::Transports->find({ |
| 490 |
+ message_attribute_id => $self->message_attribute_id, |
| 491 |
+ message_transport_type => $_->message_transport_type, |
| 492 |
+ is_digest => $self->wants_digest |
| 493 |
+ }); |
| 494 |
+ unless ($transport) { |
| 495 |
+ my $logger = Koha::Logger->get; |
| 496 |
+ $logger->warn( |
| 497 |
+ $self->message_name . ' has no transport with '. |
| 498 |
+ $_->message_transport_type . ' (digest: '. |
| 499 |
+ ($self->wants_digest ? 'yes':'no').').' |
| 500 |
+ ); |
| 501 |
+ } |
| 502 |
+ $self->{'_message_transport_types'}->{$_->message_transport_type} |
| 503 |
+ = $transport ? $transport->letter_code : ' '; |
| 504 |
+ } |
| 505 |
+ Koha::Patron::Message::Transport::Preferences->search({ |
| 506 |
+ borrower_message_preference_id => $self->borrower_message_preference_id, |
| 507 |
+ })->as_list; |
| 508 |
+ return $self->{'_message_transport_types'} || {}; |
| 509 |
+ } |
| 510 |
+ else { |
| 511 |
+ $self->_set_message_transport_types(@_); |
| 512 |
+ return $self; |
| 513 |
+ } |
| 514 |
+} |
| 515 |
+ |
| 516 |
+=head3 set |
| 517 |
+ |
| 518 |
+$preference->set({ |
| 519 |
+ message_transport_types => ['sms', 'phone'], |
| 520 |
+ wants_digest => 0, |
| 521 |
+})->store; |
| 522 |
+ |
| 523 |
+Sets preference object values and additionally message_transport_types if given. |
| 524 |
+ |
| 525 |
+=cut |
| 526 |
+ |
| 527 |
+sub set { |
| 528 |
+ my ($self, $params) = @_; |
| 529 |
+ |
| 530 |
+ my $mtt = $params->{'message_transport_types'}; |
| 531 |
+ delete $params->{'message_transport_types'}; |
| 532 |
+ |
| 533 |
+ $self->SUPER::set($params) if $params; |
| 534 |
+ if ($mtt) { |
| 535 |
+ $self->message_transport_types($mtt); |
| 536 |
+ } |
| 537 |
+ |
| 538 |
+ return $self; |
| 539 |
+} |
| 540 |
+ |
| 541 |
+=head3 store |
| 542 |
+ |
| 543 |
+Makes a validation before actual Koha::Object->store so that proper exceptions |
| 544 |
+can be thrown. See C<validate()> for documentation about exceptions. |
| 545 |
+ |
| 546 |
+=cut |
| 547 |
+ |
| 548 |
+sub store { |
| 549 |
+ my $self = shift; |
| 550 |
+ |
| 551 |
+ $self->validate->SUPER::store(@_); |
| 552 |
+ |
| 553 |
+ # store message transport types |
| 554 |
+ if (exists $self->{'_message_transport_types'}) { |
| 555 |
+ Koha::Patron::Message::Transport::Preferences->search({ |
| 556 |
+ borrower_message_preference_id => |
| 557 |
+ $self->borrower_message_preference_id, |
| 558 |
+ })->delete; |
| 559 |
+ foreach my $type (keys %{$self->{'_message_transport_types'}}) { |
| 560 |
+ Koha::Patron::Message::Transport::Preference->new({ |
| 561 |
+ borrower_message_preference_id => |
| 562 |
+ $self->borrower_message_preference_id, |
| 563 |
+ message_transport_type => $type, |
| 564 |
+ })->store; |
| 565 |
+ } |
| 566 |
+ } |
| 567 |
+ |
| 568 |
+ return $self; |
| 569 |
+} |
| 570 |
+ |
| 571 |
+=head3 validate |
| 572 |
+ |
| 573 |
+Makes a basic validation for object. |
| 574 |
+ |
| 575 |
+Throws following exceptions regarding parameters. |
| 576 |
+- Koha::Exceptions::MissingParameter |
| 577 |
+- Koha::Exceptions::TooManyParameters |
| 578 |
+- Koha::Exceptions::BadParameter |
| 579 |
+ |
| 580 |
+See $_->parameter to identify the parameter causing the exception. |
| 581 |
+ |
| 582 |
+Throws Koha::Exceptions::DuplicateObject if this preference already exists. |
| 583 |
+ |
| 584 |
+Returns Koha::Patron::Message::Preference object. |
| 585 |
+ |
| 586 |
+=cut |
| 587 |
+ |
| 588 |
+sub validate { |
| 589 |
+ my ($self) = @_; |
| 590 |
+ |
| 591 |
+ if ($self->borrowernumber && $self->categorycode) { |
| 592 |
+ Koha::Exceptions::TooManyParameters->throw( |
| 593 |
+ error => 'Both borrowernumber and category given, only one accepted', |
| 594 |
+ parameter => ['borrowernumber', 'categorycode'], |
| 595 |
+ ); |
| 596 |
+ } |
| 597 |
+ if (!$self->borrowernumber && !$self->categorycode) { |
| 598 |
+ Koha::Exceptions::MissingParameter->throw( |
| 599 |
+ error => 'borrowernumber or category required, none given', |
| 600 |
+ parameter => ['borrowernumber', 'categorycode'], |
| 601 |
+ ); |
| 602 |
+ } |
| 603 |
+ if ($self->borrowernumber) { |
| 604 |
+ Koha::Exceptions::BadParameter->throw( |
| 605 |
+ error => 'Patron not found.', |
| 606 |
+ parameter => 'borrowernumber', |
| 607 |
+ ) unless Koha::Patrons->find($self->borrowernumber); |
| 608 |
+ } |
| 609 |
+ if ($self->categorycode) { |
| 610 |
+ Koha::Exceptions::BadParameter->throw( |
| 611 |
+ error => 'Category not found.', |
| 612 |
+ parameter => 'categorycode', |
| 613 |
+ ) unless Koha::Patron::Categories->find($self->categorycode); |
| 614 |
+ } |
| 615 |
+ |
| 616 |
+ if (!$self->in_storage) { |
| 617 |
+ my $previous = Koha::Patron::Message::Preferences->search({ |
| 618 |
+ borrowernumber => $self->borrowernumber, |
| 619 |
+ categorycode => $self->categorycode, |
| 620 |
+ message_attribute_id => $self->message_attribute_id, |
| 621 |
+ }); |
| 622 |
+ if ($previous->count) { |
| 623 |
+ Koha::Exceptions::DuplicateObject->throw( |
| 624 |
+ error => 'A preference for this borrower/category and' |
| 625 |
+ .' message_attribute_id already exists', |
| 626 |
+ ); |
| 627 |
+ } |
| 628 |
+ } |
| 629 |
+ |
| 630 |
+ my $attr = Koha::Patron::Message::Attributes->find( |
| 631 |
+ $self->message_attribute_id |
| 632 |
+ ); |
| 633 |
+ unless ($attr) { |
| 634 |
+ Koha::Exceptions::BadParameter->throw( |
| 635 |
+ error => 'Message attribute with id '.$self->message_attribute_id |
| 636 |
+ .' not found', |
| 637 |
+ parameter => 'message_attribute_id' |
| 638 |
+ ); |
| 639 |
+ } |
| 640 |
+ if (defined $self->days_in_advance) { |
| 641 |
+ if ($attr && $attr->takes_days == 0) { |
| 642 |
+ Koha::Exceptions::BadParameter->throw( |
| 643 |
+ error => 'days_in_advance cannot be defined for '. |
| 644 |
+ $attr->message_name . '.', |
| 645 |
+ parameter => 'days_in_advance', |
| 646 |
+ ); |
| 647 |
+ } |
| 648 |
+ elsif ($self->days_in_advance < 0 || $self->days_in_advance > 30) { |
| 649 |
+ Koha::Exceptions::BadParameter->throw( |
| 650 |
+ error => 'days_in_advance has to be a value between 0-30 for '. |
| 651 |
+ $attr->message_name . '.', |
| 652 |
+ parameter => 'days_in_advance', |
| 653 |
+ ); |
| 654 |
+ } |
| 655 |
+ } |
| 656 |
+ if (defined $self->wants_digest) { |
| 657 |
+ my $transports = Koha::Patron::Message::Transports->search({ |
| 658 |
+ message_attribute_id => $self->message_attribute_id, |
| 659 |
+ is_digest => $self->wants_digest ? 1 : 0, |
| 660 |
+ }); |
| 661 |
+ Koha::Exceptions::BadParameter->throw( |
| 662 |
+ error => (!$self->wants_digest ? 'Digest must be selected' |
| 663 |
+ : 'Digest cannot be selected') |
| 664 |
+ . ' for '.$attr->message_name.'.', |
| 665 |
+ parameter => 'wants_digest', |
| 666 |
+ ) if $transports->count == 0; |
| 667 |
+ } |
| 668 |
+ |
| 669 |
+ return $self; |
| 670 |
+} |
| 671 |
+ |
| 672 |
+sub _set_message_transport_types { |
| 673 |
+ my $self = shift; |
| 674 |
+ |
| 675 |
+ return unless $_[0]; |
| 676 |
+ |
| 677 |
+ $self->{'_message_transport_types'} = undef; |
| 678 |
+ my $types = ref $_[0] eq "ARRAY" ? $_[0] : [@_]; |
| 679 |
+ return unless $types; |
| 680 |
+ $self->_validate_message_transport_types({ message_transport_types => $types }); |
| 681 |
+ foreach my $type (@$types) { |
| 682 |
+ unless (exists $self->{'_message_transport_types'}->{$type}) { |
| 683 |
+ my $transport = Koha::Patron::Message::Transports->find({ |
| 684 |
+ message_attribute_id => $self->message_attribute_id, |
| 685 |
+ message_transport_type => $type |
| 686 |
+ }); |
| 687 |
+ unless ($transport) { |
| 688 |
+ Koha::Exceptions::BadParameter->throw( |
| 689 |
+ error => 'No transport configured for '.$self->message_name. |
| 690 |
+ " transport type $type.", |
| 691 |
+ parameter => 'message_transport_types' |
| 692 |
+ ); |
| 693 |
+ } |
| 694 |
+ $self->{'_message_transport_types'}->{$type} |
| 695 |
+ = $transport->letter_code; |
| 696 |
+ } |
| 697 |
+ } |
| 698 |
+ return $self; |
| 699 |
+} |
| 700 |
+ |
| 701 |
+sub _validate_message_transport_types { |
| 702 |
+ my ($self, $params) = @_; |
| 703 |
+ |
| 704 |
+ if (ref($params) eq 'HASH' && $params->{'message_transport_types'}) { |
| 705 |
+ if (ref($params->{'message_transport_types'}) ne 'ARRAY') { |
| 706 |
+ $params->{'message_transport_types'} = [$params->{'message_transport_types'}]; |
| 707 |
+ } |
| 708 |
+ my $types = $params->{'message_transport_types'}; |
| 709 |
+ |
| 710 |
+ foreach my $type (@{$types}) { |
| 711 |
+ unless (Koha::Patron::Message::Transport::Types->find({ |
| 712 |
+ message_transport_type => $type |
| 713 |
+ })) { |
| 714 |
+ Koha::Exceptions::BadParameter->throw( |
| 715 |
+ error => "Message transport type '$type' does not exist", |
| 716 |
+ parameter => 'message_transport_types', |
| 717 |
+ ); |
| 718 |
+ } |
| 719 |
+ } |
| 720 |
+ return $types; |
| 721 |
+ } |
| 722 |
+} |
| 723 |
+ |
| 724 |
+=head3 type |
| 725 |
+ |
| 726 |
+=cut |
| 727 |
+ |
| 728 |
+sub _type { |
| 729 |
+ return 'BorrowerMessagePreference'; |
| 730 |
+} |
| 731 |
+ |
| 732 |
+=head1 AUTHOR |
| 733 |
+ |
| 734 |
+Lari Taskula <lari.taskula@jns.fi> |
| 735 |
+ |
| 736 |
+=cut |
| 737 |
+ |
| 738 |
+1; |
| 739 |
diff --git a/Koha/Patron/Message/Preferences.pm b/Koha/Patron/Message/Preferences.pm |
| 740 |
new file mode 100644 |
| 741 |
index 0000000..fa8e974 |
| 742 |
--- /dev/null |
| 743 |
+++ b/Koha/Patron/Message/Preferences.pm |
| 744 |
@@ -0,0 +1,146 @@ |
| 745 |
+package Koha::Patron::Message::Preferences; |
| 746 |
+ |
| 747 |
+# Copyright Koha-Suomi Oy 2016 |
| 748 |
+# |
| 749 |
+# This file is part of Koha. |
| 750 |
+# |
| 751 |
+# Koha is free software; you can redistribute it and/or modify it under the |
| 752 |
+# terms of the GNU General Public License as published by the Free Software |
| 753 |
+# Foundation; either version 3 of the License, or (at your option) any later |
| 754 |
+# version. |
| 755 |
+# |
| 756 |
+# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
| 757 |
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
| 758 |
+# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 759 |
+# |
| 760 |
+# You should have received a copy of the GNU General Public License along |
| 761 |
+# with Koha; if not, write to the Free Software Foundation, Inc., |
| 762 |
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 763 |
+ |
| 764 |
+use Modern::Perl; |
| 765 |
+ |
| 766 |
+use Koha::Database; |
| 767 |
+use Koha::Patron::Message::Attributes; |
| 768 |
+use Koha::Patron::Message::Preference; |
| 769 |
+use Koha::Patron::Message::Transports; |
| 770 |
+ |
| 771 |
+use base qw(Koha::Objects); |
| 772 |
+ |
| 773 |
+=head1 NAME |
| 774 |
+ |
| 775 |
+Koha::Patron::Message::Preferences - Koha Patron Message Preferences object class |
| 776 |
+ |
| 777 |
+=head1 API |
| 778 |
+ |
| 779 |
+=head2 Class Methods |
| 780 |
+ |
| 781 |
+=cut |
| 782 |
+ |
| 783 |
+=head3 find_with_message_name |
| 784 |
+ |
| 785 |
+Koha::Patron::Message::Preferences->find_with_message_name({ |
| 786 |
+ borrowernumber => 123, |
| 787 |
+ message_name => 'Hold_Filled', |
| 788 |
+}); |
| 789 |
+ |
| 790 |
+Converts C<message_name> into C<message_attribute_id> and continues find. |
| 791 |
+ |
| 792 |
+=cut |
| 793 |
+ |
| 794 |
+sub find_with_message_name { |
| 795 |
+ my ($self, $id) = @_; |
| 796 |
+ |
| 797 |
+ if (ref($id) eq "HASH" && $id->{'message_name'}) { |
| 798 |
+ my $attr = Koha::Patron::Message::Attributes->find({ |
| 799 |
+ message_name => $id->{'message_name'}, |
| 800 |
+ }); |
| 801 |
+ $id->{'message_attribute_id'} = ($attr) ? |
| 802 |
+ $attr->message_attribute_id : undef; |
| 803 |
+ delete $id->{'message_name'}; |
| 804 |
+ } |
| 805 |
+ |
| 806 |
+ return $self->SUPER::find($id); |
| 807 |
+} |
| 808 |
+ |
| 809 |
+=head3 get_options |
| 810 |
+ |
| 811 |
+my $messaging_options = Koha::Patron::Message::Preferences->get_options |
| 812 |
+ |
| 813 |
+Returns an ARRAYref of HASHrefs on available messaging options. |
| 814 |
+ |
| 815 |
+=cut |
| 816 |
+ |
| 817 |
+sub get_options { |
| 818 |
+ my ($self) = @_; |
| 819 |
+ |
| 820 |
+ my $transports = Koha::Patron::Message::Transports->search(undef, |
| 821 |
+ { |
| 822 |
+ join => ['message_attribute'], |
| 823 |
+ '+select' => ['message_attribute.message_name', 'message_attribute.takes_days'], |
| 824 |
+ '+as' => ['message_name', 'takes_days'], |
| 825 |
+ }); |
| 826 |
+ |
| 827 |
+ my $choices; |
| 828 |
+ while (my $transport = $transports->next) { |
| 829 |
+ my $name = $transport->get_column('message_name'); |
| 830 |
+ $choices->{$name}->{'message_attribute_id'} = $transport->message_attribute_id; |
| 831 |
+ $choices->{$name}->{'message_name'} = $name; |
| 832 |
+ $choices->{$name}->{'takes_days'} = $transport->get_column('takes_days'); |
| 833 |
+ $choices->{$name}->{'has_digest'} ||= 1 if $transport->is_digest; |
| 834 |
+ $choices->{$name}->{'has_digest_off'} ||= 1 if !$transport->is_digest; |
| 835 |
+ $choices->{$name}->{'transport_'.$transport->get_column('message_transport_type')} = ' '; |
| 836 |
+ } |
| 837 |
+ |
| 838 |
+ my @return = values %$choices; |
| 839 |
+ @return = sort { $a->{message_attribute_id} <=> $b->{message_attribute_id} } @return; |
| 840 |
+ |
| 841 |
+ return \@return; |
| 842 |
+} |
| 843 |
+ |
| 844 |
+=head3 search_with_message_name |
| 845 |
+ |
| 846 |
+Koha::Patron::Message::Preferences->search_with_message_name({ |
| 847 |
+ borrowernumber => 123, |
| 848 |
+ message_name => 'Hold_Filled', |
| 849 |
+}); |
| 850 |
+ |
| 851 |
+Converts C<message_name> into C<message_attribute_id> and continues search. Use |
| 852 |
+Koha::Patron::Message::Preferences->search with a proper join for more complicated |
| 853 |
+searches. |
| 854 |
+ |
| 855 |
+=cut |
| 856 |
+ |
| 857 |
+sub search_with_message_name { |
| 858 |
+ my ($self, $params, $attributes) = @_; |
| 859 |
+ |
| 860 |
+ if (ref($params) eq "HASH" && $params->{'message_name'}) { |
| 861 |
+ my $attr = Koha::Patron::Message::Attributes->find({ |
| 862 |
+ message_name => $params->{'message_name'}, |
| 863 |
+ }); |
| 864 |
+ $params->{'message_attribute_id'} = ($attr) ? |
| 865 |
+ $attr->message_attribute_id : undef; |
| 866 |
+ delete $params->{'message_name'}; |
| 867 |
+ } |
| 868 |
+ |
| 869 |
+ return $self->SUPER::search($params, $attributes); |
| 870 |
+} |
| 871 |
+ |
| 872 |
+=head3 type |
| 873 |
+ |
| 874 |
+=cut |
| 875 |
+ |
| 876 |
+sub _type { |
| 877 |
+ return 'BorrowerMessagePreference'; |
| 878 |
+} |
| 879 |
+ |
| 880 |
+sub object_class { |
| 881 |
+ return 'Koha::Patron::Message::Preference'; |
| 882 |
+} |
| 883 |
+ |
| 884 |
+=head1 AUTHOR |
| 885 |
+ |
| 886 |
+Lari Taskula <lari.taskula@jns.fi> |
| 887 |
+ |
| 888 |
+=cut |
| 889 |
+ |
| 890 |
+1; |
| 891 |
diff --git a/Koha/Patron/Message/Transport.pm b/Koha/Patron/Message/Transport.pm |
| 892 |
new file mode 100644 |
| 893 |
index 0000000..ca0906e |
| 894 |
--- /dev/null |
| 895 |
+++ b/Koha/Patron/Message/Transport.pm |
| 896 |
@@ -0,0 +1,50 @@ |
| 897 |
+package Koha::Patron::Message::Transport; |
| 898 |
+ |
| 899 |
+# Copyright Koha-Suomi Oy 2016 |
| 900 |
+# |
| 901 |
+# This file is part of Koha. |
| 902 |
+# |
| 903 |
+# Koha is free software; you can redistribute it and/or modify it under the |
| 904 |
+# terms of the GNU General Public License as published by the Free Software |
| 905 |
+# Foundation; either version 3 of the License, or (at your option) any later |
| 906 |
+# version.a |
| 907 |
+# |
| 908 |
+# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
| 909 |
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
| 910 |
+# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 911 |
+# |
| 912 |
+# You should have received a copy of the GNU General Public License along |
| 913 |
+# with Koha; if not, write to the Free Software Foundation, Inc., |
| 914 |
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 915 |
+ |
| 916 |
+use Modern::Perl; |
| 917 |
+ |
| 918 |
+use Koha::Database; |
| 919 |
+ |
| 920 |
+use base qw(Koha::Object); |
| 921 |
+ |
| 922 |
+=head1 NAME |
| 923 |
+ |
| 924 |
+Koha::Patron::Message::Transport - Koha Patron Message Transport object class |
| 925 |
+ |
| 926 |
+=head1 API |
| 927 |
+ |
| 928 |
+=head2 Class Methods |
| 929 |
+ |
| 930 |
+=cut |
| 931 |
+ |
| 932 |
+=head3 type |
| 933 |
+ |
| 934 |
+=cut |
| 935 |
+ |
| 936 |
+sub _type { |
| 937 |
+ return 'MessageTransport'; |
| 938 |
+} |
| 939 |
+ |
| 940 |
+=head1 AUTHOR |
| 941 |
+ |
| 942 |
+Lari Taskula <lari.taskula@jns.fi> |
| 943 |
+ |
| 944 |
+=cut |
| 945 |
+ |
| 946 |
+1; |
| 947 |
diff --git a/Koha/Patron/Message/Transport/Preference.pm b/Koha/Patron/Message/Transport/Preference.pm |
| 948 |
new file mode 100644 |
| 949 |
index 0000000..fe0ddeb |
| 950 |
--- /dev/null |
| 951 |
+++ b/Koha/Patron/Message/Transport/Preference.pm |
| 952 |
@@ -0,0 +1,51 @@ |
| 953 |
+package Koha::Patron::Message::Transport::Preference; |
| 954 |
+ |
| 955 |
+# Copyright Koha-Suomi Oy 2016 |
| 956 |
+# |
| 957 |
+# This file is part of Koha. |
| 958 |
+# |
| 959 |
+# Koha is free software; you can redistribute it and/or modify it under the |
| 960 |
+# terms of the GNU General Public License as published by the Free Software |
| 961 |
+# Foundation; either version 3 of the License, or (at your option) any later |
| 962 |
+# version.a |
| 963 |
+# |
| 964 |
+# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
| 965 |
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
| 966 |
+# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 967 |
+# |
| 968 |
+# You should have received a copy of the GNU General Public License along |
| 969 |
+# with Koha; if not, write to the Free Software Foundation, Inc., |
| 970 |
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 971 |
+ |
| 972 |
+use Modern::Perl; |
| 973 |
+ |
| 974 |
+use Koha::Database; |
| 975 |
+ |
| 976 |
+use base qw(Koha::Object); |
| 977 |
+ |
| 978 |
+=head1 NAME |
| 979 |
+ |
| 980 |
+Koha::Patron::Message::Transport::Preference - Koha Patron Message Transport |
| 981 |
+Preference object class |
| 982 |
+ |
| 983 |
+=head1 API |
| 984 |
+ |
| 985 |
+=head2 Class Methods |
| 986 |
+ |
| 987 |
+=cut |
| 988 |
+ |
| 989 |
+=head3 type |
| 990 |
+ |
| 991 |
+=cut |
| 992 |
+ |
| 993 |
+sub _type { |
| 994 |
+ return 'BorrowerMessageTransportPreference'; |
| 995 |
+} |
| 996 |
+ |
| 997 |
+=head1 AUTHOR |
| 998 |
+ |
| 999 |
+Lari Taskula <lari.taskula@jns.fi> |
| 1000 |
+ |
| 1001 |
+=cut |
| 1002 |
+ |
| 1003 |
+1; |
| 1004 |
diff --git a/Koha/Patron/Message/Transport/Preferences.pm b/Koha/Patron/Message/Transport/Preferences.pm |
| 1005 |
new file mode 100644 |
| 1006 |
index 0000000..aabd851 |
| 1007 |
--- /dev/null |
| 1008 |
+++ b/Koha/Patron/Message/Transport/Preferences.pm |
| 1009 |
@@ -0,0 +1,56 @@ |
| 1010 |
+package Koha::Patron::Message::Transport::Preferences; |
| 1011 |
+ |
| 1012 |
+# Copyright Koha-Suomi Oy 2016 |
| 1013 |
+# |
| 1014 |
+# This file is part of Koha. |
| 1015 |
+# |
| 1016 |
+# Koha is free software; you can redistribute it and/or modify it under the |
| 1017 |
+# terms of the GNU General Public License as published by the Free Software |
| 1018 |
+# Foundation; either version 3 of the License, or (at your option) any later |
| 1019 |
+# version. |
| 1020 |
+# |
| 1021 |
+# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
| 1022 |
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
| 1023 |
+# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 1024 |
+# |
| 1025 |
+# You should have received a copy of the GNU General Public License along |
| 1026 |
+# with Koha; if not, write to the Free Software Foundation, Inc., |
| 1027 |
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 1028 |
+ |
| 1029 |
+use Modern::Perl; |
| 1030 |
+ |
| 1031 |
+use Koha::Database; |
| 1032 |
+use Koha::Patron::Message::Transport::Preference; |
| 1033 |
+ |
| 1034 |
+use base qw(Koha::Objects); |
| 1035 |
+ |
| 1036 |
+=head1 NAME |
| 1037 |
+ |
| 1038 |
+Koha::Patron::Message::Transport::Preferences - Koha Patron Message Transport |
| 1039 |
+Preferences object class |
| 1040 |
+ |
| 1041 |
+=head1 API |
| 1042 |
+ |
| 1043 |
+=head2 Class Methods |
| 1044 |
+ |
| 1045 |
+=cut |
| 1046 |
+ |
| 1047 |
+=head3 type |
| 1048 |
+ |
| 1049 |
+=cut |
| 1050 |
+ |
| 1051 |
+sub _type { |
| 1052 |
+ return 'BorrowerMessageTransportPreference'; |
| 1053 |
+} |
| 1054 |
+ |
| 1055 |
+sub object_class { |
| 1056 |
+ return 'Koha::Patron::Message::Transport::Preference'; |
| 1057 |
+} |
| 1058 |
+ |
| 1059 |
+=head1 AUTHOR |
| 1060 |
+ |
| 1061 |
+Lari Taskula <lari.taskula@jns.fi> |
| 1062 |
+ |
| 1063 |
+=cut |
| 1064 |
+ |
| 1065 |
+1; |
| 1066 |
diff --git a/Koha/Patron/Message/Transport/Type.pm b/Koha/Patron/Message/Transport/Type.pm |
| 1067 |
new file mode 100644 |
| 1068 |
index 0000000..4a52a10 |
| 1069 |
--- /dev/null |
| 1070 |
+++ b/Koha/Patron/Message/Transport/Type.pm |
| 1071 |
@@ -0,0 +1,51 @@ |
| 1072 |
+package Koha::Patron::Message::Transport::Type; |
| 1073 |
+ |
| 1074 |
+# Copyright Koha-Suomi Oy 2016 |
| 1075 |
+# |
| 1076 |
+# This file is part of Koha. |
| 1077 |
+# |
| 1078 |
+# Koha is free software; you can redistribute it and/or modify it under the |
| 1079 |
+# terms of the GNU General Public License as published by the Free Software |
| 1080 |
+# Foundation; either version 3 of the License, or (at your option) any later |
| 1081 |
+# version.a |
| 1082 |
+# |
| 1083 |
+# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
| 1084 |
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
| 1085 |
+# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 1086 |
+# |
| 1087 |
+# You should have received a copy of the GNU General Public License along |
| 1088 |
+# with Koha; if not, write to the Free Software Foundation, Inc., |
| 1089 |
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 1090 |
+ |
| 1091 |
+use Modern::Perl; |
| 1092 |
+ |
| 1093 |
+use Koha::Database; |
| 1094 |
+ |
| 1095 |
+use base qw(Koha::Object); |
| 1096 |
+ |
| 1097 |
+=head1 NAME |
| 1098 |
+ |
| 1099 |
+Koha::Patron::Message::Transport::Type - Koha Patron Message Transport Type |
| 1100 |
+object class |
| 1101 |
+ |
| 1102 |
+=head1 API |
| 1103 |
+ |
| 1104 |
+=head2 Class Methods |
| 1105 |
+ |
| 1106 |
+=cut |
| 1107 |
+ |
| 1108 |
+=head3 type |
| 1109 |
+ |
| 1110 |
+=cut |
| 1111 |
+ |
| 1112 |
+sub _type { |
| 1113 |
+ return 'MessageTransportType'; |
| 1114 |
+} |
| 1115 |
+ |
| 1116 |
+=head1 AUTHOR |
| 1117 |
+ |
| 1118 |
+Lari Taskula <lari.taskula@jns.fi> |
| 1119 |
+ |
| 1120 |
+=cut |
| 1121 |
+ |
| 1122 |
+1; |
| 1123 |
diff --git a/Koha/Patron/Message/Transport/Types.pm b/Koha/Patron/Message/Transport/Types.pm |
| 1124 |
new file mode 100644 |
| 1125 |
index 0000000..2633ea3 |
| 1126 |
--- /dev/null |
| 1127 |
+++ b/Koha/Patron/Message/Transport/Types.pm |
| 1128 |
@@ -0,0 +1,56 @@ |
| 1129 |
+package Koha::Patron::Message::Transport::Types; |
| 1130 |
+ |
| 1131 |
+# Copyright Koha-Suomi Oy 2016 |
| 1132 |
+# |
| 1133 |
+# This file is part of Koha. |
| 1134 |
+# |
| 1135 |
+# Koha is free software; you can redistribute it and/or modify it under the |
| 1136 |
+# terms of the GNU General Public License as published by the Free Software |
| 1137 |
+# Foundation; either version 3 of the License, or (at your option) any later |
| 1138 |
+# version. |
| 1139 |
+# |
| 1140 |
+# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
| 1141 |
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
| 1142 |
+# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 1143 |
+# |
| 1144 |
+# You should have received a copy of the GNU General Public License along |
| 1145 |
+# with Koha; if not, write to the Free Software Foundation, Inc., |
| 1146 |
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 1147 |
+ |
| 1148 |
+use Modern::Perl; |
| 1149 |
+ |
| 1150 |
+use Koha::Database; |
| 1151 |
+use Koha::Patron::Message::Transport::Type; |
| 1152 |
+ |
| 1153 |
+use base qw(Koha::Objects); |
| 1154 |
+ |
| 1155 |
+=head1 NAME |
| 1156 |
+ |
| 1157 |
+Koha::Patron::Message::Transport::Types - Koha Patron Message Transport Types |
| 1158 |
+object class |
| 1159 |
+ |
| 1160 |
+=head1 API |
| 1161 |
+ |
| 1162 |
+=head2 Class Methods |
| 1163 |
+ |
| 1164 |
+=cut |
| 1165 |
+ |
| 1166 |
+=head3 type |
| 1167 |
+ |
| 1168 |
+=cut |
| 1169 |
+ |
| 1170 |
+sub _type { |
| 1171 |
+ return 'MessageTransportType'; |
| 1172 |
+} |
| 1173 |
+ |
| 1174 |
+sub object_class { |
| 1175 |
+ return 'Koha::Patron::Message::Transport::Type'; |
| 1176 |
+} |
| 1177 |
+ |
| 1178 |
+=head1 AUTHOR |
| 1179 |
+ |
| 1180 |
+Lari Taskula <lari.taskula@jns.fi> |
| 1181 |
+ |
| 1182 |
+=cut |
| 1183 |
+ |
| 1184 |
+1; |
| 1185 |
diff --git a/Koha/Patron/Message/Transports.pm b/Koha/Patron/Message/Transports.pm |
| 1186 |
new file mode 100644 |
| 1187 |
index 0000000..b6aee32 |
| 1188 |
--- /dev/null |
| 1189 |
+++ b/Koha/Patron/Message/Transports.pm |
| 1190 |
@@ -0,0 +1,55 @@ |
| 1191 |
+package Koha::Patron::Message::Transports; |
| 1192 |
+ |
| 1193 |
+# Copyright Koha-Suomi Oy 2016 |
| 1194 |
+# |
| 1195 |
+# This file is part of Koha. |
| 1196 |
+# |
| 1197 |
+# Koha is free software; you can redistribute it and/or modify it under the |
| 1198 |
+# terms of the GNU General Public License as published by the Free Software |
| 1199 |
+# Foundation; either version 3 of the License, or (at your option) any later |
| 1200 |
+# version. |
| 1201 |
+# |
| 1202 |
+# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
| 1203 |
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
| 1204 |
+# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 1205 |
+# |
| 1206 |
+# You should have received a copy of the GNU General Public License along |
| 1207 |
+# with Koha; if not, write to the Free Software Foundation, Inc., |
| 1208 |
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 1209 |
+ |
| 1210 |
+use Modern::Perl; |
| 1211 |
+ |
| 1212 |
+use Koha::Database; |
| 1213 |
+use Koha::Patron::Message::Transport; |
| 1214 |
+ |
| 1215 |
+use base qw(Koha::Objects); |
| 1216 |
+ |
| 1217 |
+=head1 NAME |
| 1218 |
+ |
| 1219 |
+Koha::Patron::Message::Transports - Koha Patron Message Transports object class |
| 1220 |
+ |
| 1221 |
+=head1 API |
| 1222 |
+ |
| 1223 |
+=head2 Class Methods |
| 1224 |
+ |
| 1225 |
+=cut |
| 1226 |
+ |
| 1227 |
+=head3 type |
| 1228 |
+ |
| 1229 |
+=cut |
| 1230 |
+ |
| 1231 |
+sub _type { |
| 1232 |
+ return 'MessageTransport'; |
| 1233 |
+} |
| 1234 |
+ |
| 1235 |
+sub object_class { |
| 1236 |
+ return 'Koha::Patron::Message::Transport'; |
| 1237 |
+} |
| 1238 |
+ |
| 1239 |
+=head1 AUTHOR |
| 1240 |
+ |
| 1241 |
+Lari Taskula <lari.taskula@jns.fi> |
| 1242 |
+ |
| 1243 |
+=cut |
| 1244 |
+ |
| 1245 |
+1; |
| 1246 |
diff --git a/t/db_dependent/Koha/Patron/Message/Attributes.t b/t/db_dependent/Koha/Patron/Message/Attributes.t |
| 1247 |
new file mode 100644 |
| 1248 |
index 0000000..836b4f0 |
| 1249 |
--- /dev/null |
| 1250 |
+++ b/t/db_dependent/Koha/Patron/Message/Attributes.t |
| 1251 |
@@ -0,0 +1,74 @@ |
| 1252 |
+#!/usr/bin/perl |
| 1253 |
+ |
| 1254 |
+# Copyright 2017 Koha-Suomi Oy |
| 1255 |
+# |
| 1256 |
+# This file is part of Koha |
| 1257 |
+# |
| 1258 |
+# Koha is free software; you can redistribute it and/or modify it |
| 1259 |
+# under the terms of the GNU General Public License as published by |
| 1260 |
+# the Free Software Foundation; either version 3 of the License, or |
| 1261 |
+# (at your option) any later version. |
| 1262 |
+# |
| 1263 |
+# Koha is distributed in the hope that it will be useful, but |
| 1264 |
+# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 1265 |
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 1266 |
+# GNU General Public License for more details. |
| 1267 |
+# |
| 1268 |
+# You should have received a copy of the GNU General Public License |
| 1269 |
+# along with Koha; if not, see <http://www.gnu.org/licenses>. |
| 1270 |
+ |
| 1271 |
+use Modern::Perl; |
| 1272 |
+ |
| 1273 |
+use Test::More tests => 2; |
| 1274 |
+ |
| 1275 |
+use Koha::Database; |
| 1276 |
+ |
| 1277 |
+my $schema = Koha::Database->new->schema; |
| 1278 |
+ |
| 1279 |
+subtest 'Test class imports' => sub { |
| 1280 |
+ plan tests => 2; |
| 1281 |
+ |
| 1282 |
+ use_ok('Koha::Patron::Message::Attribute'); |
| 1283 |
+ use_ok('Koha::Patron::Message::Attributes'); |
| 1284 |
+}; |
| 1285 |
+ |
| 1286 |
+subtest 'Test Koha::Patron::Message::Attributes' => sub { |
| 1287 |
+ plan tests => 6; |
| 1288 |
+ |
| 1289 |
+ $schema->storage->txn_begin; |
| 1290 |
+ |
| 1291 |
+ Koha::Patron::Message::Attribute->new({ |
| 1292 |
+ message_name => 'Test_Attribute' |
| 1293 |
+ })->store; |
| 1294 |
+ Koha::Patron::Message::Attribute->new({ |
| 1295 |
+ message_name => 'Test_Attribute2', |
| 1296 |
+ takes_days => 1 |
| 1297 |
+ })->store; |
| 1298 |
+ |
| 1299 |
+ my $attribute = Koha::Patron::Message::Attributes->find({ |
| 1300 |
+ message_name => 'Test_Attribute' }); |
| 1301 |
+ my $attribute2 = Koha::Patron::Message::Attributes->find({ |
| 1302 |
+ message_name => 'Test_Attribute2' }); |
| 1303 |
+ |
| 1304 |
+ is($attribute->message_name, 'Test_Attribute', |
| 1305 |
+ 'Added a new messaging attribute.'); |
| 1306 |
+ is($attribute->takes_days, 0, |
| 1307 |
+ 'For that messaging attribute, takes_days is disabled by default.'); |
| 1308 |
+ is($attribute2->message_name, 'Test_Attribute2', |
| 1309 |
+ 'Added another messaging attribute.'); |
| 1310 |
+ is($attribute2->takes_days, 1, |
| 1311 |
+ 'takes_days is enabled for that message attribute (as expected).'); |
| 1312 |
+ |
| 1313 |
+ $attribute->delete; |
| 1314 |
+ $attribute2->delete; |
| 1315 |
+ is(Koha::Patron::Message::Attributes->find({ |
| 1316 |
+ message_name => 'Test_Attribute' }), undef, |
| 1317 |
+ 'Deleted the first message attribute.'); |
| 1318 |
+ is(Koha::Patron::Message::Attributes->find({ |
| 1319 |
+ message_name => 'Test_Attribute2' }), undef, |
| 1320 |
+ 'Deleted the second message attribute.'); |
| 1321 |
+ |
| 1322 |
+ $schema->storage->txn_rollback; |
| 1323 |
+}; |
| 1324 |
+ |
| 1325 |
+1; |
| 1326 |
diff --git a/t/db_dependent/Koha/Patron/Message/Preferences.t b/t/db_dependent/Koha/Patron/Message/Preferences.t |
| 1327 |
new file mode 100644 |
| 1328 |
index 0000000..bd88c63 |
| 1329 |
--- /dev/null |
| 1330 |
+++ b/t/db_dependent/Koha/Patron/Message/Preferences.t |
| 1331 |
@@ -0,0 +1,719 @@ |
| 1332 |
+#!/usr/bin/perl |
| 1333 |
+ |
| 1334 |
+# Copyright 2017 Koha-Suomi Oy |
| 1335 |
+# |
| 1336 |
+# This file is part of Koha |
| 1337 |
+# |
| 1338 |
+# Koha is free software; you can redistribute it and/or modify it |
| 1339 |
+# under the terms of the GNU General Public License as published by |
| 1340 |
+# the Free Software Foundation; either version 3 of the License, or |
| 1341 |
+# (at your option) any later version. |
| 1342 |
+# |
| 1343 |
+# Koha is distributed in the hope that it will be useful, but |
| 1344 |
+# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 1345 |
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 1346 |
+# GNU General Public License for more details. |
| 1347 |
+# |
| 1348 |
+# You should have received a copy of the GNU General Public License |
| 1349 |
+# along with Koha; if not, see <http://www.gnu.org/licenses>. |
| 1350 |
+ |
| 1351 |
+use Modern::Perl; |
| 1352 |
+ |
| 1353 |
+use Test::More tests => 7; |
| 1354 |
+ |
| 1355 |
+use t::lib::Mocks; |
| 1356 |
+use t::lib::TestBuilder; |
| 1357 |
+ |
| 1358 |
+use C4::Context; |
| 1359 |
+ |
| 1360 |
+use Koha::Notice::Templates; |
| 1361 |
+use Koha::Patron::Categories; |
| 1362 |
+use Koha::Patron::Message::Attributes; |
| 1363 |
+use Koha::Patron::Message::Transport::Types; |
| 1364 |
+use Koha::Patron::Message::Transports; |
| 1365 |
+use Koha::Patrons; |
| 1366 |
+ |
| 1367 |
+use File::Temp qw/tempfile/; |
| 1368 |
+use Log::Log4perl; |
| 1369 |
+ |
| 1370 |
+my $schema = Koha::Database->new->schema; |
| 1371 |
+my $builder = t::lib::TestBuilder->new; |
| 1372 |
+ |
| 1373 |
+subtest 'Test class imports' => sub { |
| 1374 |
+ plan tests => 2; |
| 1375 |
+ |
| 1376 |
+ use_ok('Koha::Patron::Message::Preference'); |
| 1377 |
+ use_ok('Koha::Patron::Message::Preferences'); |
| 1378 |
+}; |
| 1379 |
+ |
| 1380 |
+subtest 'Test Koha::Patron::Message::Preferences' => sub { |
| 1381 |
+ plan tests => 2; |
| 1382 |
+ |
| 1383 |
+ $schema->storage->txn_begin; |
| 1384 |
+ |
| 1385 |
+ my $attribute = build_a_test_attribute(); |
| 1386 |
+ my $letter = build_a_test_letter(); |
| 1387 |
+ my $mtt = build_a_test_transport_type(); |
| 1388 |
+ Koha::Patron::Message::Transport->new({ |
| 1389 |
+ message_attribute_id => $attribute->message_attribute_id, |
| 1390 |
+ message_transport_type => $mtt->message_transport_type, |
| 1391 |
+ is_digest => 0, |
| 1392 |
+ letter_module => $letter->module, |
| 1393 |
+ letter_code => $letter->code, |
| 1394 |
+ })->store; |
| 1395 |
+ |
| 1396 |
+ subtest 'Test for a patron' => sub { |
| 1397 |
+ plan tests => 3; |
| 1398 |
+ |
| 1399 |
+ my $patron = build_a_test_patron(); |
| 1400 |
+ Koha::Patron::Message::Preference->new({ |
| 1401 |
+ borrowernumber => $patron->borrowernumber, |
| 1402 |
+ message_attribute_id => $attribute->message_attribute_id, |
| 1403 |
+ wants_digest => 0, |
| 1404 |
+ days_in_advance => undef, |
| 1405 |
+ })->store; |
| 1406 |
+ |
| 1407 |
+ my $preference = Koha::Patron::Message::Preferences->find({ |
| 1408 |
+ borrowernumber => $patron->borrowernumber, |
| 1409 |
+ message_attribute_id => $attribute->message_attribute_id |
| 1410 |
+ }); |
| 1411 |
+ ok($preference->borrower_message_preference_id > 0, |
| 1412 |
+ 'Added a new messaging preference for patron.'); |
| 1413 |
+ |
| 1414 |
+ subtest 'Test set not throwing an exception on duplicate object' => sub { |
| 1415 |
+ plan tests => 1; |
| 1416 |
+ |
| 1417 |
+ Koha::Patron::Message::Attributes->find({ |
| 1418 |
+ message_attribute_id => $attribute->message_attribute_id |
| 1419 |
+ })->set({ takes_days => 1 })->store; |
| 1420 |
+ $preference->set({ days_in_advance => 1 })->store; |
| 1421 |
+ is(ref($preference), 'Koha::Patron::Message::Preference', |
| 1422 |
+ 'Updating the preference does not cause duplicate object exception'); |
| 1423 |
+ }; |
| 1424 |
+ |
| 1425 |
+ $preference->delete; |
| 1426 |
+ is(Koha::Patron::Message::Preferences->search({ |
| 1427 |
+ borrowernumber => $patron->borrowernumber, |
| 1428 |
+ message_attribute_id => $attribute->message_attribute_id |
| 1429 |
+ })->count, 0, 'Deleted the messaging preference.'); |
| 1430 |
+ }; |
| 1431 |
+ |
| 1432 |
+ subtest 'Test for a category' => sub { |
| 1433 |
+ my $category = build_a_test_category(); |
| 1434 |
+ Koha::Patron::Message::Preference->new({ |
| 1435 |
+ categorycode => $category->categorycode, |
| 1436 |
+ message_attribute_id => $attribute->message_attribute_id, |
| 1437 |
+ wants_digest => 0, |
| 1438 |
+ days_in_advance => undef, |
| 1439 |
+ })->store; |
| 1440 |
+ |
| 1441 |
+ my $preference = Koha::Patron::Message::Preferences->find({ |
| 1442 |
+ categorycode => $category->categorycode, |
| 1443 |
+ message_attribute_id => $attribute->message_attribute_id |
| 1444 |
+ }); |
| 1445 |
+ ok($preference->borrower_message_preference_id > 0, |
| 1446 |
+ 'Added a new messaging preference for category.'); |
| 1447 |
+ |
| 1448 |
+ $preference->delete; |
| 1449 |
+ is(Koha::Patron::Message::Preferences->search({ |
| 1450 |
+ categorycode => $category->categorycode, |
| 1451 |
+ message_attribute_id => $attribute->message_attribute_id |
| 1452 |
+ })->count, 0, 'Deleted the messaging preference.'); |
| 1453 |
+ }; |
| 1454 |
+ |
| 1455 |
+ $schema->storage->txn_rollback; |
| 1456 |
+}; |
| 1457 |
+ |
| 1458 |
+subtest 'Test Koha::Patron::Message::Preferences->get_options' => sub { |
| 1459 |
+ plan tests => 2; |
| 1460 |
+ |
| 1461 |
+ subtest 'Test method availability and return value' => sub { |
| 1462 |
+ plan tests => 3; |
| 1463 |
+ |
| 1464 |
+ ok(Koha::Patron::Message::Preferences->can('get_options'), |
| 1465 |
+ 'Method get_options is available.'); |
| 1466 |
+ ok(my $options = Koha::Patron::Message::Preferences->get_options, |
| 1467 |
+ 'Called get_options successfully.'); |
| 1468 |
+ is(ref($options), 'ARRAY', 'get_options returns a ARRAYref'); |
| 1469 |
+ }; |
| 1470 |
+ |
| 1471 |
+ subtest 'Make sure options are correct' => sub { |
| 1472 |
+ $schema->storage->txn_begin; |
| 1473 |
+ my $options = Koha::Patron::Message::Preferences->get_options; |
| 1474 |
+ |
| 1475 |
+ foreach my $option (@$options) { |
| 1476 |
+ my $n = $option->{'message_name'}; |
| 1477 |
+ my $attr = Koha::Patron::Message::Attributes->find($option->{'message_attribute_id'}); |
| 1478 |
+ is($option->{'message_attribute_id'}, $attr->message_attribute_id, |
| 1479 |
+ '$n: message_attribute_id is set'); |
| 1480 |
+ is($option->{'message_name'}, $attr->message_name, '$n: message_name is set'); |
| 1481 |
+ is($option->{'takes_days'}, $attr->takes_days, '$n: takes_days is set'); |
| 1482 |
+ my $transports = Koha::Patron::Message::Transports->search({ |
| 1483 |
+ message_attribute_id => $option->{'message_attribute_id'}, |
| 1484 |
+ is_digest => $option->{'has_digest'} || 0, |
| 1485 |
+ }); |
| 1486 |
+ while (my $trnzport = $transports->next) { |
| 1487 |
+ is($option->{'has_digest'} || 0, $trnzport->is_digest, '$n: has_digest is set for '.$trnzport->message_transport_type); |
| 1488 |
+ is($option->{'transport_'.$trnzport->message_transport_type}, ' ', '$n: transport_'.$trnzport->message_transport_type.' is set'); |
| 1489 |
+ } |
| 1490 |
+ } |
| 1491 |
+ |
| 1492 |
+ $schema->storage->txn_rollback; |
| 1493 |
+ }; |
| 1494 |
+}; |
| 1495 |
+ |
| 1496 |
+subtest 'Add preferences from defaults' => sub { |
| 1497 |
+ plan tests => 3; |
| 1498 |
+ |
| 1499 |
+ $schema->storage->txn_begin; |
| 1500 |
+ |
| 1501 |
+ my $patron = build_a_test_patron(); |
| 1502 |
+ my ($default, $mtt1, $mtt2) = build_a_test_category_preference({ |
| 1503 |
+ patron => $patron, |
| 1504 |
+ }); |
| 1505 |
+ ok(Koha::Patron::Message::Preference->new_from_default({ |
| 1506 |
+ borrowernumber => $patron->borrowernumber, |
| 1507 |
+ categorycode => $patron->categorycode, |
| 1508 |
+ message_attribute_id => $default->message_attribute_id, |
| 1509 |
+ })->store, 'Added a default preference to patron.'); |
| 1510 |
+ ok(my $pref = Koha::Patron::Message::Preferences->find({ |
| 1511 |
+ borrowernumber => $patron->borrowernumber, |
| 1512 |
+ message_attribute_id => $default->message_attribute_id, |
| 1513 |
+ }), 'Found the default preference from patron.'); |
| 1514 |
+ is(Koha::Patron::Message::Transport::Preferences->search({ |
| 1515 |
+ borrower_message_preference_id => $pref->borrower_message_preference_id |
| 1516 |
+ })->count, 2, 'Found the two transport types that we set earlier'); |
| 1517 |
+ |
| 1518 |
+ $schema->storage->txn_rollback; |
| 1519 |
+}; |
| 1520 |
+ |
| 1521 |
+subtest 'Test Koha::Patron::Message::Preference->message_transport_types' => sub { |
| 1522 |
+ plan tests => 4; |
| 1523 |
+ |
| 1524 |
+ ok(Koha::Patron::Message::Preference->can('message_transport_types'), |
| 1525 |
+ 'Method message_transport_types available'); |
| 1526 |
+ |
| 1527 |
+ subtest 'get message_transport_types' => sub { |
| 1528 |
+ plan tests => 5; |
| 1529 |
+ |
| 1530 |
+ $schema->storage->txn_begin; |
| 1531 |
+ |
| 1532 |
+ my $patron = build_a_test_patron(); |
| 1533 |
+ my ($preference, $mtt1, $mtt2) = build_a_test_complete_preference({ |
| 1534 |
+ patron => $patron |
| 1535 |
+ }); |
| 1536 |
+ Koha::Patron::Message::Transport::Preferences->search({ |
| 1537 |
+ borrower_message_preference_id => $preference->borrower_message_preference_id, |
| 1538 |
+ })->delete; |
| 1539 |
+ Koha::Patron::Message::Transport::Preference->new({ |
| 1540 |
+ borrower_message_preference_id => $preference->borrower_message_preference_id, |
| 1541 |
+ message_transport_type => $mtt1->message_transport_type, |
| 1542 |
+ })->store; |
| 1543 |
+ Koha::Patron::Message::Transport::Preference->new({ |
| 1544 |
+ borrower_message_preference_id => $preference->borrower_message_preference_id, |
| 1545 |
+ message_transport_type => $mtt2->message_transport_type, |
| 1546 |
+ })->store; |
| 1547 |
+ my $stored_transports = Koha::Patron::Message::Transport::Preferences->search({ |
| 1548 |
+ borrower_message_preference_id => $preference->borrower_message_preference_id, |
| 1549 |
+ }); |
| 1550 |
+ my $transport1 = Koha::Patron::Message::Transports->find({ |
| 1551 |
+ message_attribute_id => $preference->message_attribute_id, |
| 1552 |
+ message_transport_type => $mtt1->message_transport_type, |
| 1553 |
+ }); |
| 1554 |
+ my $transport2 = Koha::Patron::Message::Transports->find({ |
| 1555 |
+ message_attribute_id => $preference->message_attribute_id, |
| 1556 |
+ message_transport_type => $mtt2->message_transport_type, |
| 1557 |
+ }); |
| 1558 |
+ my $transports = $preference->message_transport_types; |
| 1559 |
+ is(keys %{$transports}, $stored_transports->count, |
| 1560 |
+ '->message_transport_types gets correct amount of transport types.'); |
| 1561 |
+ is($transports->{$stored_transports->next->message_transport_type}, |
| 1562 |
+ $transport1->letter_code, 'Found correct message transport type and letter code.'); |
| 1563 |
+ is($transports->{$stored_transports->next->message_transport_type}, |
| 1564 |
+ $transport2->letter_code, 'Found correct message transport type and letter code.'); |
| 1565 |
+ ok(!$preference->message_transport_types->{'nonexistent'}, |
| 1566 |
+ 'Didn\'t find nonexistent transport type.'); |
| 1567 |
+ |
| 1568 |
+ subtest 'test logging of warnings by invalid message transport type' => sub { |
| 1569 |
+ plan tests => 2; |
| 1570 |
+ |
| 1571 |
+ my $log = mytempfile(); |
| 1572 |
+ my $conf = mytempfile( <<"HERE" |
| 1573 |
+log4perl.logger.opac = WARN, OPAC |
| 1574 |
+log4perl.appender.OPAC=Log::Log4perl::Appender::TestBuffer |
| 1575 |
+log4perl.appender.OPAC.filename=$log |
| 1576 |
+log4perl.appender.OPAC.mode=append |
| 1577 |
+log4perl.appender.OPAC.layout=SimpleLayout |
| 1578 |
+log4perl.logger.intranet = WARN, INTRANET |
| 1579 |
+log4perl.appender.INTRANET=Log::Log4perl::Appender::TestBuffer |
| 1580 |
+log4perl.appender.INTRANET.filename=$log |
| 1581 |
+log4perl.appender.INTRANET.mode=append |
| 1582 |
+log4perl.appender.INTRANET.layout=SimpleLayout |
| 1583 |
+HERE |
| 1584 |
+ ); |
| 1585 |
+ t::lib::Mocks::mock_config('log4perl_conf', $conf); |
| 1586 |
+ my $appenders = Log::Log4perl->appenders; |
| 1587 |
+ my $appender = Log::Log4perl->appenders->{OPAC}; |
| 1588 |
+ |
| 1589 |
+ my $pref = Koha::Patron::Message::Preferences->find( |
| 1590 |
+ $preference->borrower_message_preference_id |
| 1591 |
+ ); |
| 1592 |
+ my $transports = $pref->message_transport_types; |
| 1593 |
+ is($appender, undef, 'Nothing in buffer yet'); |
| 1594 |
+ |
| 1595 |
+ my $mtt_new = build_a_test_transport_type(); |
| 1596 |
+ Koha::Patron::Message::Transport::Preference->new({ |
| 1597 |
+ borrower_message_preference_id => |
| 1598 |
+ $pref->borrower_message_preference_id, |
| 1599 |
+ message_transport_type => $mtt_new->message_transport_type, |
| 1600 |
+ })->store; |
| 1601 |
+ $pref = Koha::Patron::Message::Preferences->find( |
| 1602 |
+ $pref->borrower_message_preference_id |
| 1603 |
+ ); |
| 1604 |
+ $transports = $pref->message_transport_types; |
| 1605 |
+ $appender = Log::Log4perl->appenders->{OPAC}; |
| 1606 |
+ my $name = $pref->message_name; |
| 1607 |
+ my $tt = $mtt_new->message_transport_type; |
| 1608 |
+ like($appender->buffer, qr/WARN - $name has no transport with $tt/, |
| 1609 |
+ 'Logged invalid message transport type'); |
| 1610 |
+ }; |
| 1611 |
+ |
| 1612 |
+ $schema->storage->txn_rollback; |
| 1613 |
+ }; |
| 1614 |
+ |
| 1615 |
+ subtest 'set message_transport_types' => sub { |
| 1616 |
+ plan tests => 6; |
| 1617 |
+ |
| 1618 |
+ $schema->storage->txn_begin; |
| 1619 |
+ |
| 1620 |
+ my $patron = build_a_test_patron(); |
| 1621 |
+ my ($preference, $mtt1, $mtt2) = build_a_test_complete_preference({ |
| 1622 |
+ patron => $patron |
| 1623 |
+ }); |
| 1624 |
+ |
| 1625 |
+ my $mtt1_str = $mtt1->message_transport_type; |
| 1626 |
+ my $mtt2_str = $mtt2->message_transport_type; |
| 1627 |
+ # 1/3, use message_transport_types(list) |
| 1628 |
+ Koha::Patron::Message::Transport::Preferences->search({ |
| 1629 |
+ borrower_message_preference_id => $preference->borrower_message_preference_id, |
| 1630 |
+ })->delete; |
| 1631 |
+ ok($preference->message_transport_types($mtt1_str, $mtt2_str)->store, |
| 1632 |
+ '1/3 Set returned true.'); |
| 1633 |
+ my $stored_transports = Koha::Patron::Message::Transport::Preferences->search({ |
| 1634 |
+ borrower_message_preference_id => $preference->borrower_message_preference_id, |
| 1635 |
+ '-or' => [ |
| 1636 |
+ message_transport_type => $mtt1_str, |
| 1637 |
+ message_transport_type => $mtt2_str |
| 1638 |
+ ] |
| 1639 |
+ }); |
| 1640 |
+ is($stored_transports->count, 2, 'Two transports selected'); |
| 1641 |
+ |
| 1642 |
+ # 2/3, use message_transport_types(ARRAYREF) |
| 1643 |
+ Koha::Patron::Message::Transport::Preferences->search({ |
| 1644 |
+ borrower_message_preference_id => $preference->borrower_message_preference_id, |
| 1645 |
+ })->delete; |
| 1646 |
+ ok($preference->message_transport_types([$mtt1_str, $mtt2_str])->store, |
| 1647 |
+ '2/3 Set returned true.'); |
| 1648 |
+ $stored_transports = Koha::Patron::Message::Transport::Preferences->search({ |
| 1649 |
+ borrower_message_preference_id => $preference->borrower_message_preference_id, |
| 1650 |
+ '-or' => [ |
| 1651 |
+ message_transport_type => $mtt1_str, |
| 1652 |
+ message_transport_type => $mtt2_str |
| 1653 |
+ ] |
| 1654 |
+ }); |
| 1655 |
+ is($stored_transports->count, 2, 'Two transports selected'); |
| 1656 |
+ |
| 1657 |
+ # 3/3, use set({ message_transport_types => ARRAYREF }) |
| 1658 |
+ Koha::Patron::Message::Transport::Preferences->search({ |
| 1659 |
+ borrower_message_preference_id => $preference->borrower_message_preference_id, |
| 1660 |
+ })->delete; |
| 1661 |
+ ok($preference->set({ |
| 1662 |
+ message_transport_types => [$mtt1_str, $mtt2_str]})->store, |
| 1663 |
+ '3/3 Set returned true.'); |
| 1664 |
+ $stored_transports = Koha::Patron::Message::Transport::Preferences->search({ |
| 1665 |
+ borrower_message_preference_id => $preference->borrower_message_preference_id, |
| 1666 |
+ '-or' => [ |
| 1667 |
+ message_transport_type => $mtt1_str, |
| 1668 |
+ message_transport_type => $mtt2_str |
| 1669 |
+ ] |
| 1670 |
+ }); |
| 1671 |
+ is($stored_transports->count, 2, 'Two transports selected'); |
| 1672 |
+ |
| 1673 |
+ $schema->storage->txn_rollback; |
| 1674 |
+ }; |
| 1675 |
+ |
| 1676 |
+ subtest 'new message_transport_types' => sub { |
| 1677 |
+ plan tests => 3; |
| 1678 |
+ |
| 1679 |
+ $schema->storage->txn_begin; |
| 1680 |
+ |
| 1681 |
+ my $patron = build_a_test_patron(); |
| 1682 |
+ my $letter = build_a_test_letter(); |
| 1683 |
+ my $attribute = build_a_test_attribute(); |
| 1684 |
+ my $mtt = build_a_test_transport_type(); |
| 1685 |
+ Koha::Patron::Message::Transport->new({ |
| 1686 |
+ message_attribute_id => $attribute->message_attribute_id, |
| 1687 |
+ message_transport_type => $mtt->message_transport_type, |
| 1688 |
+ is_digest => 0, |
| 1689 |
+ letter_module => $letter->module, |
| 1690 |
+ letter_code => $letter->code, |
| 1691 |
+ })->store; |
| 1692 |
+ ok(my $preference = Koha::Patron::Message::Preference->new({ |
| 1693 |
+ borrowernumber => $patron->borrowernumber, |
| 1694 |
+ message_attribute_id => $attribute->message_attribute_id, |
| 1695 |
+ wants_digest => 0, |
| 1696 |
+ days_in_advance => undef, |
| 1697 |
+ message_transport_types => $mtt->message_transport_type, |
| 1698 |
+ })->store, 'Added a new messaging preference and transport types to patron.'); |
| 1699 |
+ ok($preference->message_transport_types->{$mtt->message_transport_type}, |
| 1700 |
+ 'The transport type is stored in the object.'); |
| 1701 |
+ my $stored_transports = Koha::Patron::Message::Transport::Preferences->search({ |
| 1702 |
+ borrower_message_preference_id => $preference->borrower_message_preference_id, |
| 1703 |
+ }); |
| 1704 |
+ is($stored_transports->next->message_transport_type, $mtt->message_transport_type, |
| 1705 |
+ 'The transport type is stored in the database.'); |
| 1706 |
+ |
| 1707 |
+ $schema->storage->txn_rollback; |
| 1708 |
+ }; |
| 1709 |
+}; |
| 1710 |
+ |
| 1711 |
+subtest 'Test Koha::Patron::Message::Preference->message_name' => sub { |
| 1712 |
+ plan tests => 1; |
| 1713 |
+ |
| 1714 |
+ $schema->storage->txn_begin; |
| 1715 |
+ |
| 1716 |
+ my $patron = build_a_test_patron(); |
| 1717 |
+ my $attribute = build_a_test_attribute(); |
| 1718 |
+ my ($preference, $mtt1, $mtt2) = build_a_test_complete_preference({ |
| 1719 |
+ patron => $patron, |
| 1720 |
+ attr => $attribute, |
| 1721 |
+ }); |
| 1722 |
+ my $message_name_pref = Koha::Patron::Message::Preferences->search_with_message_name({ |
| 1723 |
+ borrowernumber => $patron->{'borrowernumber'}, |
| 1724 |
+ message_name => $attribute->message_name, |
| 1725 |
+ })->next; |
| 1726 |
+ is($message_name_pref->message_name, $attribute->message_name, "Found preference with message_name"); |
| 1727 |
+ |
| 1728 |
+ $schema->storage->txn_rollback; |
| 1729 |
+}; |
| 1730 |
+ |
| 1731 |
+subtest 'Test adding a new preference with invalid parameters' => sub { |
| 1732 |
+ plan tests => 4; |
| 1733 |
+ |
| 1734 |
+ subtest 'Missing parameters' => sub { |
| 1735 |
+ plan tests => 1; |
| 1736 |
+ |
| 1737 |
+ eval { Koha::Patron::Message::Preference->new->store }; |
| 1738 |
+ is(ref $@, 'Koha::Exceptions::MissingParameter', |
| 1739 |
+ 'Adding a message preference without parameters' |
| 1740 |
+ .' => Koha::Exceptions::MissingParameter'); |
| 1741 |
+ }; |
| 1742 |
+ |
| 1743 |
+ subtest 'Too many parameters' => sub { |
| 1744 |
+ plan tests => 1; |
| 1745 |
+ |
| 1746 |
+ $schema->storage->txn_begin; |
| 1747 |
+ |
| 1748 |
+ my $patron = build_a_test_patron(); |
| 1749 |
+ eval { Koha::Patron::Message::Preference->new({ |
| 1750 |
+ borrowernumber => $patron->borrowernumber, |
| 1751 |
+ categorycode => $patron->categorycode, |
| 1752 |
+ })->store }; |
| 1753 |
+ is(ref $@, 'Koha::Exceptions::TooManyParameters', |
| 1754 |
+ 'Adding a message preference for both borrowernumber and categorycode' |
| 1755 |
+ .' => Koha::Exceptions::TooManyParameters'); |
| 1756 |
+ |
| 1757 |
+ $schema->storage->txn_rollback; |
| 1758 |
+ }; |
| 1759 |
+ |
| 1760 |
+ subtest 'Bad parameter' => sub { |
| 1761 |
+ plan tests => 22; |
| 1762 |
+ |
| 1763 |
+ $schema->storage->txn_begin; |
| 1764 |
+ |
| 1765 |
+ eval { Koha::Patron::Message::Preference->new({ |
| 1766 |
+ borrowernumber => -999, |
| 1767 |
+ })->store }; |
| 1768 |
+ is(ref $@, 'Koha::Exceptions::BadParameter', |
| 1769 |
+ 'Adding a message preference with invalid borrowernumber' |
| 1770 |
+ .' => Koha::Exceptions::BadParameter'); |
| 1771 |
+ is ($@->parameter, 'borrowernumber', 'The previous exception tells us it' |
| 1772 |
+ .' was the borrowernumber.'); |
| 1773 |
+ |
| 1774 |
+ eval { Koha::Patron::Message::Preference->new({ |
| 1775 |
+ categorycode => 'nonexistent', |
| 1776 |
+ })->store }; |
| 1777 |
+ is(ref $@, 'Koha::Exceptions::BadParameter', |
| 1778 |
+ 'Adding a message preference with invalid categorycode' |
| 1779 |
+ .' => Koha::Exceptions::BadParameter'); |
| 1780 |
+ is($@->parameter, 'categorycode', 'The previous exception tells us it' |
| 1781 |
+ .' was the categorycode.'); |
| 1782 |
+ |
| 1783 |
+ my $attribute = build_a_test_attribute({ takes_days => 0 }); |
| 1784 |
+ my $patron = build_a_test_patron(); |
| 1785 |
+ eval { Koha::Patron::Message::Preference->new({ |
| 1786 |
+ borrowernumber => $patron->borrowernumber, |
| 1787 |
+ message_attribute_id => $attribute->message_attribute_id, |
| 1788 |
+ days_in_advance => 10, |
| 1789 |
+ })->store }; |
| 1790 |
+ is(ref $@, 'Koha::Exceptions::BadParameter', |
| 1791 |
+ 'Adding a message preference with days in advance option when not' |
| 1792 |
+ .' available => Koha::Exceptions::BadParameter'); |
| 1793 |
+ is($@->parameter, 'days_in_advance', 'The previous exception tells us it' |
| 1794 |
+ .' was the days_in_advance.'); |
| 1795 |
+ |
| 1796 |
+ $attribute->set({ takes_days => 1 })->store; |
| 1797 |
+ eval { Koha::Patron::Message::Preference->new({ |
| 1798 |
+ borrowernumber => $patron->borrowernumber, |
| 1799 |
+ message_attribute_id => $attribute->message_attribute_id, |
| 1800 |
+ days_in_advance => 31, |
| 1801 |
+ })->store }; |
| 1802 |
+ is(ref $@, 'Koha::Exceptions::BadParameter', |
| 1803 |
+ 'Adding a message preference with days in advance option too large' |
| 1804 |
+ .' => Koha::Exceptions::BadParameter'); |
| 1805 |
+ is($@->parameter, 'days_in_advance', 'The previous exception tells us it' |
| 1806 |
+ .' was the days_in_advance.'); |
| 1807 |
+ |
| 1808 |
+ eval { Koha::Patron::Message::Preference->new({ |
| 1809 |
+ borrowernumber => $patron->borrowernumber, |
| 1810 |
+ message_transport_types => ['nonexistent'] |
| 1811 |
+ })->store }; |
| 1812 |
+ is (ref $@, 'Koha::Exceptions::BadParameter', |
| 1813 |
+ 'Adding a message preference with invalid message_transport_type' |
| 1814 |
+ .' => Koha::Exceptions::BadParameter'); |
| 1815 |
+ is ($@->parameter, 'message_transport_types', 'The previous exception ' |
| 1816 |
+ .'tells us it was the message_transport_types.'); |
| 1817 |
+ |
| 1818 |
+ my $mtt_new = build_a_test_transport_type(); |
| 1819 |
+ eval { |
| 1820 |
+ Koha::Patron::Message::Preference->new({ |
| 1821 |
+ borrowernumber => $patron->borrowernumber, |
| 1822 |
+ message_attribute_id => $attribute->message_attribute_id, |
| 1823 |
+ message_transport_types => [$mtt_new->message_transport_type], |
| 1824 |
+ wants_digest => 1, |
| 1825 |
+ })->store }; |
| 1826 |
+ is (ref $@, 'Koha::Exceptions::BadParameter', |
| 1827 |
+ 'Adding a message preference with invalid message_transport_type' |
| 1828 |
+ .' => Koha::Exceptions::BadParameter'); |
| 1829 |
+ is ($@->parameter, 'message_transport_types', 'The previous exception ' |
| 1830 |
+ .'tells us it was the message_transport_types.'); |
| 1831 |
+ like ($@->error, qr/^No transport configured/, 'Exception is because of ' |
| 1832 |
+ .'given message_transport_type is not a valid option.'); |
| 1833 |
+ |
| 1834 |
+ eval { |
| 1835 |
+ Koha::Patron::Message::Preference->new({ |
| 1836 |
+ borrowernumber => $patron->borrowernumber, |
| 1837 |
+ message_attribute_id => $attribute->message_attribute_id, |
| 1838 |
+ message_transport_types => [], |
| 1839 |
+ wants_digest => 1, |
| 1840 |
+ })->store }; |
| 1841 |
+ is (ref $@, 'Koha::Exceptions::BadParameter', |
| 1842 |
+ 'Adding a message preference with invalid message_transport_type' |
| 1843 |
+ .' => Koha::Exceptions::BadParameter'); |
| 1844 |
+ is ($@->parameter, 'wants_digest', 'The previous exception tells us it' |
| 1845 |
+ .' was the wants_digest'); |
| 1846 |
+ like ($@->error, qr/^Digest cannot be selected/, 'Exception s because of' |
| 1847 |
+ .' given digest is not available for this transport.'); |
| 1848 |
+ |
| 1849 |
+ eval { |
| 1850 |
+ Koha::Patron::Message::Preference->new({ |
| 1851 |
+ borrowernumber => $patron->borrowernumber, |
| 1852 |
+ message_attribute_id => $attribute->message_attribute_id, |
| 1853 |
+ message_transport_types => [], |
| 1854 |
+ wants_digest => 0, |
| 1855 |
+ })->store }; |
| 1856 |
+ is (ref $@, 'Koha::Exceptions::BadParameter', |
| 1857 |
+ 'Adding a message preference with invalid message_transport_type' |
| 1858 |
+ .' => Koha::Exceptions::BadParameter'); |
| 1859 |
+ is ($@->parameter, 'wants_digest', 'The previous exception tells us it' |
| 1860 |
+ .' was the wants_digest'); |
| 1861 |
+ like ($@->error, qr/^Digest must be selected/, 'Exception s because of' |
| 1862 |
+ .' digest has to be on for this transport.'); |
| 1863 |
+ |
| 1864 |
+ eval { |
| 1865 |
+ Koha::Patron::Message::Preference->new({ |
| 1866 |
+ borrowernumber => $patron->borrowernumber, |
| 1867 |
+ message_attribute_id => -1, |
| 1868 |
+ message_transport_types => [], |
| 1869 |
+ })->store }; |
| 1870 |
+ is (ref $@, 'Koha::Exceptions::BadParameter', |
| 1871 |
+ 'Adding a message preference with invalid message_transport_type' |
| 1872 |
+ .' => Koha::Exceptions::BadParameter'); |
| 1873 |
+ is ($@->parameter, 'message_attribute_id', 'The previous exception tells' |
| 1874 |
+ .' us it was the message_attribute_id'); |
| 1875 |
+ like ($@->error, qr/^Message attribute with id -1 not found/, 'Exception ' |
| 1876 |
+ .' is because of given message attribute id is not found.'); |
| 1877 |
+ |
| 1878 |
+ $schema->storage->txn_rollback; |
| 1879 |
+ }; |
| 1880 |
+ |
| 1881 |
+ subtest 'Duplicate object' => sub { |
| 1882 |
+ plan tests => 2; |
| 1883 |
+ |
| 1884 |
+ $schema->storage->txn_begin; |
| 1885 |
+ |
| 1886 |
+ my $attribute = build_a_test_attribute(); |
| 1887 |
+ my $letter = build_a_test_letter(); |
| 1888 |
+ my $mtt = build_a_test_transport_type(); |
| 1889 |
+ Koha::Patron::Message::Transport->new({ |
| 1890 |
+ message_attribute_id => $attribute->message_attribute_id, |
| 1891 |
+ message_transport_type => $mtt->message_transport_type, |
| 1892 |
+ is_digest => 0, |
| 1893 |
+ letter_module => $letter->module, |
| 1894 |
+ letter_code => $letter->code, |
| 1895 |
+ })->store; |
| 1896 |
+ my $patron = build_a_test_patron(); |
| 1897 |
+ my $preference = Koha::Patron::Message::Preference->new({ |
| 1898 |
+ borrowernumber => $patron->borrowernumber, |
| 1899 |
+ message_attribute_id => $attribute->message_attribute_id, |
| 1900 |
+ wants_digest => 0, |
| 1901 |
+ days_in_advance => undef, |
| 1902 |
+ })->store; |
| 1903 |
+ ok($preference->borrower_message_preference_id, |
| 1904 |
+ 'Added a new messaging preference for patron.'); |
| 1905 |
+ eval { Koha::Patron::Message::Preference->new({ |
| 1906 |
+ borrowernumber => $patron->borrowernumber, |
| 1907 |
+ message_attribute_id => $attribute->message_attribute_id, |
| 1908 |
+ wants_digest => 0, |
| 1909 |
+ days_in_advance => undef, |
| 1910 |
+ })->store }; |
| 1911 |
+ is(ref $@, 'Koha::Exceptions::DuplicateObject', |
| 1912 |
+ 'Adding a duplicate preference' |
| 1913 |
+ .' => Koha::Exceptions::DuplicateObject'); |
| 1914 |
+ |
| 1915 |
+ $schema->storage->txn_rollback; |
| 1916 |
+ }; |
| 1917 |
+}; |
| 1918 |
+ |
| 1919 |
+sub build_a_test_attribute { |
| 1920 |
+ my ($params) = @_; |
| 1921 |
+ |
| 1922 |
+ $params->{takes_days} = $params->{takes_days} && $params->{takes_days} > 0 |
| 1923 |
+ ? 1 : 0; |
| 1924 |
+ |
| 1925 |
+ my $attribute = $builder->build({ |
| 1926 |
+ source => 'MessageAttribute', |
| 1927 |
+ value => $params, |
| 1928 |
+ }); |
| 1929 |
+ |
| 1930 |
+ return Koha::Patron::Message::Attributes->find( |
| 1931 |
+ $attribute->{message_attribute_id} |
| 1932 |
+ ); |
| 1933 |
+} |
| 1934 |
+ |
| 1935 |
+sub build_a_test_category { |
| 1936 |
+ my $categorycode = $builder->build({ |
| 1937 |
+ source => 'Category' })->{categorycode}; |
| 1938 |
+ |
| 1939 |
+ return Koha::Patron::Categories->find($categorycode); |
| 1940 |
+} |
| 1941 |
+ |
| 1942 |
+sub build_a_test_letter { |
| 1943 |
+ my ($params) = @_; |
| 1944 |
+ |
| 1945 |
+ my $mtt = $params->{mtt} ? $params->{mtt} : 'email'; |
| 1946 |
+ my $branchcode = $builder->build({ |
| 1947 |
+ source => 'Branch' })->{branchcode}; |
| 1948 |
+ my $letter = $builder->build({ |
| 1949 |
+ source => 'Letter', |
| 1950 |
+ value => { |
| 1951 |
+ branchcode => '', |
| 1952 |
+ is_html => 0, |
| 1953 |
+ message_transport_type => $mtt |
| 1954 |
+ } |
| 1955 |
+ }); |
| 1956 |
+ |
| 1957 |
+ return Koha::Notice::Templates->find({ |
| 1958 |
+ module => $letter->{module}, |
| 1959 |
+ code => $letter->{code}, |
| 1960 |
+ branchcode => $letter->{branchcode}, |
| 1961 |
+ }); |
| 1962 |
+} |
| 1963 |
+ |
| 1964 |
+sub build_a_test_patron { |
| 1965 |
+ my $categorycode = $builder->build({ |
| 1966 |
+ source => 'Category' })->{categorycode}; |
| 1967 |
+ my $branchcode = $builder->build({ |
| 1968 |
+ source => 'Branch' })->{branchcode}; |
| 1969 |
+ my $borrowernumber = $builder->build({ |
| 1970 |
+ source => 'Borrower' })->{borrowernumber}; |
| 1971 |
+ |
| 1972 |
+ return Koha::Patrons->find($borrowernumber); |
| 1973 |
+} |
| 1974 |
+ |
| 1975 |
+sub build_a_test_transport_type { |
| 1976 |
+ my $mtt = $builder->build({ |
| 1977 |
+ source => 'MessageTransportType' }); |
| 1978 |
+ |
| 1979 |
+ return Koha::Patron::Message::Transport::Types->find( |
| 1980 |
+ $mtt->{message_transport_type} |
| 1981 |
+ ); |
| 1982 |
+} |
| 1983 |
+ |
| 1984 |
+sub build_a_test_category_preference { |
| 1985 |
+ my ($params) = @_; |
| 1986 |
+ |
| 1987 |
+ my $patron = $params->{patron}; |
| 1988 |
+ my $attr = $params->{attr} |
| 1989 |
+ ? $params->{attr} |
| 1990 |
+ : build_a_test_attribute($params->{days_in_advance}); |
| 1991 |
+ |
| 1992 |
+ my $letter = $params->{letter} ? $params->{letter} : build_a_test_letter(); |
| 1993 |
+ my $mtt1 = $params->{mtt1} ? $params->{mtt1} : build_a_test_transport_type(); |
| 1994 |
+ my $mtt2 = $params->{mtt2} ? $params->{mtt2} : build_a_test_transport_type(); |
| 1995 |
+ |
| 1996 |
+ Koha::Patron::Message::Transport->new({ |
| 1997 |
+ message_attribute_id => $attr->message_attribute_id, |
| 1998 |
+ message_transport_type => $mtt1->message_transport_type, |
| 1999 |
+ is_digest => $params->{digest} ? 1 : 0, |
| 2000 |
+ letter_module => $letter->module, |
| 2001 |
+ letter_code => $letter->code, |
| 2002 |
+ })->store; |
| 2003 |
+ |
| 2004 |
+ Koha::Patron::Message::Transport->new({ |
| 2005 |
+ message_attribute_id => $attr->message_attribute_id, |
| 2006 |
+ message_transport_type => $mtt2->message_transport_type, |
| 2007 |
+ is_digest => $params->{digest} ? 1 : 0, |
| 2008 |
+ letter_module => $letter->module, |
| 2009 |
+ letter_code => $letter->code, |
| 2010 |
+ })->store; |
| 2011 |
+ |
| 2012 |
+ my $default = Koha::Patron::Message::Preference->new({ |
| 2013 |
+ categorycode => $patron->categorycode, |
| 2014 |
+ message_attribute_id => $attr->message_attribute_id, |
| 2015 |
+ wants_digest => $params->{digest} ? 1 : 0, |
| 2016 |
+ days_in_advance => $params->{days_in_advance} |
| 2017 |
+ ? $params->{days_in_advance} : undef, |
| 2018 |
+ })->store; |
| 2019 |
+ |
| 2020 |
+ Koha::Patron::Message::Transport::Preference->new({ |
| 2021 |
+ borrower_message_preference_id => $default->borrower_message_preference_id, |
| 2022 |
+ message_transport_type => $mtt1->message_transport_type, |
| 2023 |
+ })->store; |
| 2024 |
+ Koha::Patron::Message::Transport::Preference->new({ |
| 2025 |
+ borrower_message_preference_id => $default->borrower_message_preference_id, |
| 2026 |
+ message_transport_type => $mtt2->message_transport_type, |
| 2027 |
+ })->store; |
| 2028 |
+ |
| 2029 |
+ return ($default, $mtt1, $mtt2); |
| 2030 |
+} |
| 2031 |
+ |
| 2032 |
+sub build_a_test_complete_preference { |
| 2033 |
+ my ($params) = @_; |
| 2034 |
+ |
| 2035 |
+ my ($default, $mtt1, $mtt2) = build_a_test_category_preference($params); |
| 2036 |
+ my $patron = $params->{patron}; |
| 2037 |
+ $patron->set_default_messaging_preferences; |
| 2038 |
+ return (Koha::Patron::Message::Preferences->search({ |
| 2039 |
+ borrowernumber => $patron->borrowernumber |
| 2040 |
+ })->next, $mtt1, $mtt2); |
| 2041 |
+} |
| 2042 |
+ |
| 2043 |
+sub mytempfile { |
| 2044 |
+ my ( $fh, $fn ) = tempfile( SUFFIX => '.logger.test', UNLINK => 1 ); |
| 2045 |
+ print $fh $_[0]//''; |
| 2046 |
+ close $fh; |
| 2047 |
+ return $fn; |
| 2048 |
+} |
| 2049 |
+ |
| 2050 |
+1; |
| 2051 |
diff --git a/t/db_dependent/Koha/Patron/Message/Transport/Preferences.t b/t/db_dependent/Koha/Patron/Message/Transport/Preferences.t |
| 2052 |
new file mode 100644 |
| 2053 |
index 0000000..0cdf809 |
| 2054 |
--- /dev/null |
| 2055 |
+++ b/t/db_dependent/Koha/Patron/Message/Transport/Preferences.t |
| 2056 |
@@ -0,0 +1,179 @@ |
| 2057 |
+#!/usr/bin/perl |
| 2058 |
+ |
| 2059 |
+# Copyright 2017 Koha-Suomi Oy |
| 2060 |
+# |
| 2061 |
+# This file is part of Koha |
| 2062 |
+# |
| 2063 |
+# Koha is free software; you can redistribute it and/or modify it |
| 2064 |
+# under the terms of the GNU General Public License as published by |
| 2065 |
+# the Free Software Foundation; either version 3 of the License, or |
| 2066 |
+# (at your option) any later version. |
| 2067 |
+# |
| 2068 |
+# Koha is distributed in the hope that it will be useful, but |
| 2069 |
+# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 2070 |
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 2071 |
+# GNU General Public License for more details. |
| 2072 |
+# |
| 2073 |
+# You should have received a copy of the GNU General Public License |
| 2074 |
+# along with Koha; if not, see <http://www.gnu.org/licenses>. |
| 2075 |
+ |
| 2076 |
+use Modern::Perl; |
| 2077 |
+ |
| 2078 |
+use Test::More tests => 2; |
| 2079 |
+ |
| 2080 |
+use t::lib::Mocks; |
| 2081 |
+use t::lib::TestBuilder; |
| 2082 |
+ |
| 2083 |
+use Koha::Notice::Templates; |
| 2084 |
+use Koha::Patron::Categories; |
| 2085 |
+use Koha::Patron::Message::Attributes; |
| 2086 |
+use Koha::Patron::Message::Preferences; |
| 2087 |
+use Koha::Patron::Message::Transport::Types; |
| 2088 |
+use Koha::Patron::Message::Transports; |
| 2089 |
+use Koha::Patrons; |
| 2090 |
+ |
| 2091 |
+my $schema = Koha::Database->new->schema; |
| 2092 |
+my $builder = t::lib::TestBuilder->new; |
| 2093 |
+ |
| 2094 |
+subtest 'Test class imports' => sub { |
| 2095 |
+ plan tests => 2; |
| 2096 |
+ |
| 2097 |
+ use_ok('Koha::Patron::Message::Transport::Preference'); |
| 2098 |
+ use_ok('Koha::Patron::Message::Transport::Preferences'); |
| 2099 |
+}; |
| 2100 |
+ |
| 2101 |
+subtest 'Test Koha::Patron::Message::Transport::Preferences' => sub { |
| 2102 |
+ plan tests => 2; |
| 2103 |
+ |
| 2104 |
+ $schema->storage->txn_begin; |
| 2105 |
+ |
| 2106 |
+ my $attribute = build_a_test_attribute(); |
| 2107 |
+ my $mtt = build_a_test_transport_type(); |
| 2108 |
+ my $letter = build_a_test_letter({ |
| 2109 |
+ mtt => $mtt->message_transport_type |
| 2110 |
+ }); |
| 2111 |
+ Koha::Patron::Message::Transport->new({ |
| 2112 |
+ message_attribute_id => $attribute->message_attribute_id, |
| 2113 |
+ message_transport_type => $mtt->message_transport_type, |
| 2114 |
+ is_digest => 0, |
| 2115 |
+ letter_module => $letter->module, |
| 2116 |
+ letter_code => $letter->code, |
| 2117 |
+ })->store; |
| 2118 |
+ |
| 2119 |
+ subtest 'For a patron' => sub { |
| 2120 |
+ my $patron = build_a_test_patron(); |
| 2121 |
+ my $preference = Koha::Patron::Message::Preference->new({ |
| 2122 |
+ borrowernumber => $patron->borrowernumber, |
| 2123 |
+ message_attribute_id => $attribute->message_attribute_id, |
| 2124 |
+ wants_digest => 0, |
| 2125 |
+ days_in_advance => undef, |
| 2126 |
+ })->store; |
| 2127 |
+ |
| 2128 |
+ my $pref_id = $preference->borrower_message_preference_id; |
| 2129 |
+ my $transport_pref = Koha::Patron::Message::Transport::Preference->new({ |
| 2130 |
+ borrower_message_preference_id => $pref_id, |
| 2131 |
+ message_transport_type => $mtt->message_transport_type, |
| 2132 |
+ })->store; |
| 2133 |
+ is(ref($transport_pref), 'Koha::Patron::Message::Transport::Preference', |
| 2134 |
+ 'Added a new messaging transport preference for patron.'); |
| 2135 |
+ |
| 2136 |
+ $transport_pref->delete; |
| 2137 |
+ is(Koha::Patron::Message::Transport::Preferences->search({ |
| 2138 |
+ borrower_message_preference_id => $pref_id, |
| 2139 |
+ message_transport_type => $mtt->message_transport_type, |
| 2140 |
+ })->count, 0, 'Deleted the messaging transport preference.'); |
| 2141 |
+ }; |
| 2142 |
+ |
| 2143 |
+ subtest 'For a category' => sub { |
| 2144 |
+ my $category = build_a_test_category(); |
| 2145 |
+ my $preference = Koha::Patron::Message::Preference->new({ |
| 2146 |
+ categorycode => $category->categorycode, |
| 2147 |
+ message_attribute_id => $attribute->message_attribute_id, |
| 2148 |
+ wants_digest => 0, |
| 2149 |
+ days_in_advance => undef, |
| 2150 |
+ })->store; |
| 2151 |
+ |
| 2152 |
+ my $pref_id = $preference->borrower_message_preference_id; |
| 2153 |
+ my $transport_pref = Koha::Patron::Message::Transport::Preference->new({ |
| 2154 |
+ borrower_message_preference_id => $pref_id, |
| 2155 |
+ message_transport_type => $mtt->message_transport_type, |
| 2156 |
+ })->store; |
| 2157 |
+ is(ref($transport_pref), 'Koha::Patron::Message::Transport::Preference', |
| 2158 |
+ 'Added a new messaging transport preference for category.'); |
| 2159 |
+ |
| 2160 |
+ $transport_pref->delete; |
| 2161 |
+ is(Koha::Patron::Message::Transport::Preferences->search({ |
| 2162 |
+ borrower_message_preference_id => $pref_id, |
| 2163 |
+ message_transport_type => $mtt->message_transport_type, |
| 2164 |
+ })->count, 0, 'Deleted the messaging transport preference.'); |
| 2165 |
+ }; |
| 2166 |
+ |
| 2167 |
+ $schema->storage->txn_rollback; |
| 2168 |
+}; |
| 2169 |
+ |
| 2170 |
+sub build_a_test_attribute { |
| 2171 |
+ my ($params) = @_; |
| 2172 |
+ |
| 2173 |
+ $params->{takes_days} = $params->{takes_days} && $params->{takes_days} > 0 |
| 2174 |
+ ? 1 : 0; |
| 2175 |
+ |
| 2176 |
+ my $attribute = $builder->build({ |
| 2177 |
+ source => 'MessageAttribute', |
| 2178 |
+ value => $params, |
| 2179 |
+ }); |
| 2180 |
+ |
| 2181 |
+ return Koha::Patron::Message::Attributes->find( |
| 2182 |
+ $attribute->{message_attribute_id} |
| 2183 |
+ ); |
| 2184 |
+} |
| 2185 |
+ |
| 2186 |
+sub build_a_test_category { |
| 2187 |
+ my $categorycode = $builder->build({ |
| 2188 |
+ source => 'Category' })->{categorycode}; |
| 2189 |
+ |
| 2190 |
+ return Koha::Patron::Categories->find($categorycode); |
| 2191 |
+} |
| 2192 |
+ |
| 2193 |
+sub build_a_test_letter { |
| 2194 |
+ my ($params) = @_; |
| 2195 |
+ |
| 2196 |
+ my $mtt = $params->{mtt} ? $params->{mtt} : 'email'; |
| 2197 |
+ my $branchcode = $builder->build({ |
| 2198 |
+ source => 'Branch' })->{branchcode}; |
| 2199 |
+ my $letter = $builder->build({ |
| 2200 |
+ source => 'Letter', |
| 2201 |
+ value => { |
| 2202 |
+ branchcode => '', |
| 2203 |
+ is_html => 0, |
| 2204 |
+ message_transport_type => $mtt |
| 2205 |
+ } |
| 2206 |
+ }); |
| 2207 |
+ |
| 2208 |
+ return Koha::Notice::Templates->find({ |
| 2209 |
+ module => $letter->{module}, |
| 2210 |
+ code => $letter->{code}, |
| 2211 |
+ branchcode => $letter->{branchcode}, |
| 2212 |
+ }); |
| 2213 |
+} |
| 2214 |
+ |
| 2215 |
+sub build_a_test_patron { |
| 2216 |
+ my $categorycode = $builder->build({ |
| 2217 |
+ source => 'Category' })->{categorycode}; |
| 2218 |
+ my $branchcode = $builder->build({ |
| 2219 |
+ source => 'Branch' })->{branchcode}; |
| 2220 |
+ my $borrowernumber = $builder->build({ |
| 2221 |
+ source => 'Borrower' })->{borrowernumber}; |
| 2222 |
+ |
| 2223 |
+ return Koha::Patrons->find($borrowernumber); |
| 2224 |
+} |
| 2225 |
+ |
| 2226 |
+sub build_a_test_transport_type { |
| 2227 |
+ my $mtt = $builder->build({ |
| 2228 |
+ source => 'MessageTransportType' }); |
| 2229 |
+ |
| 2230 |
+ return Koha::Patron::Message::Transport::Types->find( |
| 2231 |
+ $mtt->{message_transport_type} |
| 2232 |
+ ); |
| 2233 |
+} |
| 2234 |
+ |
| 2235 |
+1; |
| 2236 |
diff --git a/t/db_dependent/Koha/Patron/Message/Transport/Types.t b/t/db_dependent/Koha/Patron/Message/Transport/Types.t |
| 2237 |
new file mode 100644 |
| 2238 |
index 0000000..cbba32b |
| 2239 |
--- /dev/null |
| 2240 |
+++ b/t/db_dependent/Koha/Patron/Message/Transport/Types.t |
| 2241 |
@@ -0,0 +1,54 @@ |
| 2242 |
+#!/usr/bin/perl |
| 2243 |
+ |
| 2244 |
+# Copyright 2017 Koha-Suomi Oy |
| 2245 |
+# |
| 2246 |
+# This file is part of Koha |
| 2247 |
+# |
| 2248 |
+# Koha is free software; you can redistribute it and/or modify it |
| 2249 |
+# under the terms of the GNU General Public License as published by |
| 2250 |
+# the Free Software Foundation; either version 3 of the License, or |
| 2251 |
+# (at your option) any later version. |
| 2252 |
+# |
| 2253 |
+# Koha is distributed in the hope that it will be useful, but |
| 2254 |
+# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 2255 |
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 2256 |
+# GNU General Public License for more details. |
| 2257 |
+# |
| 2258 |
+# You should have received a copy of the GNU General Public License |
| 2259 |
+# along with Koha; if not, see <http://www.gnu.org/licenses>. |
| 2260 |
+ |
| 2261 |
+use Modern::Perl; |
| 2262 |
+ |
| 2263 |
+use Test::More tests => 2; |
| 2264 |
+ |
| 2265 |
+use Koha::Database; |
| 2266 |
+ |
| 2267 |
+my $schema = Koha::Database->new->schema; |
| 2268 |
+ |
| 2269 |
+subtest 'Test class imports' => sub { |
| 2270 |
+ plan tests => 2; |
| 2271 |
+ |
| 2272 |
+ use_ok('Koha::Patron::Message::Transport::Type'); |
| 2273 |
+ use_ok('Koha::Patron::Message::Transport::Types'); |
| 2274 |
+}; |
| 2275 |
+ |
| 2276 |
+subtest 'Test Koha::Patron::Message::Transport::Types' => sub { |
| 2277 |
+ plan tests => 2; |
| 2278 |
+ |
| 2279 |
+ $schema->storage->txn_begin; |
| 2280 |
+ |
| 2281 |
+ my $transport_type = Koha::Patron::Message::Transport::Type->new({ |
| 2282 |
+ message_transport_type => 'test' |
| 2283 |
+ })->store; |
| 2284 |
+ |
| 2285 |
+ is($transport_type->message_transport_type, 'test', |
| 2286 |
+ 'Added a new message transport type.'); |
| 2287 |
+ |
| 2288 |
+ $transport_type->delete; |
| 2289 |
+ is(Koha::Patron::Message::Transport::Types->find('test'), undef, |
| 2290 |
+ 'Deleted the message transport type.'); |
| 2291 |
+ |
| 2292 |
+ $schema->storage->txn_rollback; |
| 2293 |
+}; |
| 2294 |
+ |
| 2295 |
+1; |
| 2296 |
diff --git a/t/db_dependent/Koha/Patron/Message/Transports.t b/t/db_dependent/Koha/Patron/Message/Transports.t |
| 2297 |
new file mode 100644 |
| 2298 |
index 0000000..b9296fd |
| 2299 |
--- /dev/null |
| 2300 |
+++ b/t/db_dependent/Koha/Patron/Message/Transports.t |
| 2301 |
@@ -0,0 +1,119 @@ |
| 2302 |
+#!/usr/bin/perl |
| 2303 |
+ |
| 2304 |
+# Copyright 2017 Koha-Suomi Oy |
| 2305 |
+# |
| 2306 |
+# This file is part of Koha |
| 2307 |
+# |
| 2308 |
+# Koha is free software; you can redistribute it and/or modify it |
| 2309 |
+# under the terms of the GNU General Public License as published by |
| 2310 |
+# the Free Software Foundation; either version 3 of the License, or |
| 2311 |
+# (at your option) any later version. |
| 2312 |
+# |
| 2313 |
+# Koha is distributed in the hope that it will be useful, but |
| 2314 |
+# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 2315 |
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 2316 |
+# GNU General Public License for more details. |
| 2317 |
+# |
| 2318 |
+# You should have received a copy of the GNU General Public License |
| 2319 |
+# along with Koha; if not, see <http://www.gnu.org/licenses>. |
| 2320 |
+ |
| 2321 |
+use Modern::Perl; |
| 2322 |
+ |
| 2323 |
+use Test::More tests => 2; |
| 2324 |
+ |
| 2325 |
+use t::lib::TestBuilder; |
| 2326 |
+ |
| 2327 |
+use Koha::Notice::Templates; |
| 2328 |
+use Koha::Patron::Message::Attributes; |
| 2329 |
+use Koha::Patron::Message::Transport::Types; |
| 2330 |
+ |
| 2331 |
+my $schema = Koha::Database->new->schema; |
| 2332 |
+my $builder = t::lib::TestBuilder->new; |
| 2333 |
+ |
| 2334 |
+subtest 'Test class imports' => sub { |
| 2335 |
+ plan tests => 2; |
| 2336 |
+ |
| 2337 |
+ use_ok('Koha::Patron::Message::Transport'); |
| 2338 |
+ use_ok('Koha::Patron::Message::Transports'); |
| 2339 |
+}; |
| 2340 |
+ |
| 2341 |
+subtest 'Test Koha::Patron::Message::Transports' => sub { |
| 2342 |
+ plan tests => 2; |
| 2343 |
+ |
| 2344 |
+ $schema->storage->txn_begin; |
| 2345 |
+ |
| 2346 |
+ my $attribute = build_a_test_attribute(); |
| 2347 |
+ my $mtt = build_a_test_transport_type(); |
| 2348 |
+ my $letter = build_a_test_letter({ |
| 2349 |
+ mtt => $mtt->message_transport_type |
| 2350 |
+ }); |
| 2351 |
+ |
| 2352 |
+ my $transport = Koha::Patron::Message::Transport->new({ |
| 2353 |
+ message_attribute_id => $attribute->message_attribute_id, |
| 2354 |
+ message_transport_type => $mtt->message_transport_type, |
| 2355 |
+ is_digest => 0, |
| 2356 |
+ letter_module => $letter->module, |
| 2357 |
+ letter_code => $letter->code, |
| 2358 |
+ })->store; |
| 2359 |
+ |
| 2360 |
+ is($transport->message_attribute_id, $attribute->message_attribute_id, |
| 2361 |
+ 'Added a new messaging transport.'); |
| 2362 |
+ |
| 2363 |
+ $transport->delete; |
| 2364 |
+ is(Koha::Patron::Message::Transports->search({ |
| 2365 |
+ message_attribute_id => $attribute->message_attribute_id, |
| 2366 |
+ message_transport_type => $mtt->message_transport_type, |
| 2367 |
+ is_digest => 0 |
| 2368 |
+ })->count, 0, 'Deleted the messaging transport.'); |
| 2369 |
+ |
| 2370 |
+ $schema->storage->txn_rollback; |
| 2371 |
+}; |
| 2372 |
+ |
| 2373 |
+sub build_a_test_attribute { |
| 2374 |
+ my ($params) = @_; |
| 2375 |
+ |
| 2376 |
+ $params->{takes_days} = $params->{takes_days} && $params->{takes_days} > 0 |
| 2377 |
+ ? 1 : 0; |
| 2378 |
+ |
| 2379 |
+ my $attribute = $builder->build({ |
| 2380 |
+ source => 'MessageAttribute', |
| 2381 |
+ value => $params, |
| 2382 |
+ }); |
| 2383 |
+ |
| 2384 |
+ return Koha::Patron::Message::Attributes->find( |
| 2385 |
+ $attribute->{message_attribute_id} |
| 2386 |
+ ); |
| 2387 |
+} |
| 2388 |
+ |
| 2389 |
+sub build_a_test_letter { |
| 2390 |
+ my ($params) = @_; |
| 2391 |
+ |
| 2392 |
+ my $mtt = $params->{mtt} ? $params->{mtt} : 'email'; |
| 2393 |
+ my $branchcode = $builder->build({ |
| 2394 |
+ source => 'Branch' })->{branchcode}; |
| 2395 |
+ my $letter = $builder->build({ |
| 2396 |
+ source => 'Letter', |
| 2397 |
+ value => { |
| 2398 |
+ branchcode => '', |
| 2399 |
+ is_html => 0, |
| 2400 |
+ message_transport_type => $mtt |
| 2401 |
+ } |
| 2402 |
+ }); |
| 2403 |
+ |
| 2404 |
+ return Koha::Notice::Templates->find({ |
| 2405 |
+ module => $letter->{module}, |
| 2406 |
+ code => $letter->{code}, |
| 2407 |
+ branchcode => $letter->{branchcode}, |
| 2408 |
+ }); |
| 2409 |
+} |
| 2410 |
+ |
| 2411 |
+sub build_a_test_transport_type { |
| 2412 |
+ my $mtt = $builder->build({ |
| 2413 |
+ source => 'MessageTransportType' }); |
| 2414 |
+ |
| 2415 |
+ return Koha::Patron::Message::Transport::Types->find( |
| 2416 |
+ $mtt->{message_transport_type} |
| 2417 |
+ ); |
| 2418 |
+} |
| 2419 |
+ |
| 2420 |
+1; |
| 2421 |
-- |
| 2422 |
2.7.4 |