14a1f49164cdb0b363158d40a7c666106e9785f6
[koha.git] / t / db_dependent / Patron / Messaging.t
1 #!/usr/bin/perl
2 #
3 # This file is part of Koha.
4 #
5 # Copyright (C) 2018  Andreas Jonsson <andreas.jonsson@kreablo.se>
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use Test::More tests => 1;
23 use t::lib::TestBuilder;
24 use t::lib::Mocks;
25 use File::Spec;
26 use File::Basename;
27
28 use Koha::DateUtils qw( dt_from_string );
29
30 my $schema = Koha::Database->new->schema;
31 my $dbh    = C4::Context->dbh;
32
33 my $library;
34 my $borrower;
35
36 subtest 'Default behaviour tests' => sub {
37
38     plan tests => 3;
39
40     $schema->storage->txn_begin;
41
42     # Set only to avoid exception.
43     t::lib::Mocks::mock_preference( 'dateformat', 'metric' );
44
45     my $builder = t::lib::TestBuilder->new;
46
47     $library = $builder->build(
48         {
49             source => 'Branch',
50         }
51     );
52
53     $borrower = $builder->build(
54         {
55             source => 'Borrower',
56             value  => {
57                 branchcode => $library->{branchcode},
58             }
59         }
60     );
61
62     $dbh->do(<<DELETESQL);
63 DELETE FROM letter
64  WHERE module='circulation'
65    AND code = 'PREDUEDGST'
66    AND message_transport_type='email'
67    AND branchcode=''
68 DELETESQL
69
70     $dbh->do(<<DELETESQL);
71 DELETE FROM message_attributes WHERE message_name = 'Advance_Notice'
72 DELETESQL
73
74     my $message_attribute = $builder->build(
75         {
76             source => 'MessageAttribute',
77             value  => {
78                 message_name => 'Advance_Notice'
79             }
80         }
81     );
82
83     my $letter = $builder->build(
84         {
85             source => 'Letter',
86             value  => {
87                 module                 => 'circulation',
88                 code                   => 'PREDUEDGST',
89                 branchcode             => '',
90                 message_transport_type => 'email',
91                 lang                   => 'default',
92                 is_html                => 0,
93                 content                => '<<count>> <<branches.branchname>>'
94             }
95         }
96     );
97     my $borrower_message_preference = $builder->build(
98         {
99             source => 'BorrowerMessagePreference',
100             value  => {
101                 borrowernumber  => $borrower->{borrowernumber},
102                 wants_digest    => 1,
103                 days_in_advance => 1,
104                 message_attribute_id =>
105                   $message_attribute->{message_attribute_id}
106             }
107         }
108     );
109
110     my $borrower_message_transport_preference = $builder->build(
111         {
112             source => 'BorrowerMessageTransportPreference',
113             value  => {
114                 borrower_message_preference_id => $borrower_message_preference
115                   ->{borrower_message_preference_id},
116                 message_transport_type => 'email'
117             }
118         }
119     );
120
121     my $borrower_message_transport_preference_1 = $builder->build(
122         {
123             source => 'BorrowerMessageTransportPreference',
124             value  => {
125                 borrower_message_preference_id => $borrower_message_preference
126                   ->{borrower_message_preference_id},
127                 message_transport_type => 'phone'
128             }
129         }
130     );
131
132     my $patron = Koha::Patrons->find( $borrower->{borrowernumber} );
133
134     is( $patron->has_messaging_preference({ message_name => 'Advance_Notice', message_transport_type => 'email' }), 1, "Patron has Advance_Notice email preference" );
135     is( $patron->has_messaging_preference({ message_name => 'Advance_Notice', message_transport_type => 'phone' }), 1, "Patron has Advance_Notice phone preference" );
136     is( $patron->has_messaging_preference({ message_name => 'Advance_Notice', message_transport_type => 'sms' }), 0, "Patron has no Advance_Notice sms preference" );
137 };