Bug 30287: (follow-up) Make unit test more reliable in ktd
[koha.git] / t / db_dependent / Koha / Notice / Message.t
1 #!/usr/bin/perl
2
3 # Copyright 2023 Koha Development team
4 #
5 # This file is part of Koha
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>
19
20 use Modern::Perl;
21
22 use Test::More tests => 1;
23
24 use C4::Letters qw( GetPreparedLetter EnqueueLetter );
25
26 use t::lib::Mocks;
27 use t::lib::TestBuilder;
28
29 my $schema  = Koha::Database->new->schema;
30 my $builder = t::lib::TestBuilder->new;
31
32 subtest 'html_content() tests' => sub {
33     plan tests => 2;
34
35     $schema->storage->txn_begin;
36
37     my $template = $builder->build_object(
38         {
39             class => 'Koha::Notice::Templates',
40             value => {
41                 module                 => 'test',
42                 code                   => 'TEST',
43                 message_transport_type => 'email',
44                 is_html                => '1',
45                 name                   => 'test notice template',
46                 title                  => '[% borrower.firstname %]',
47                 content                => 'This is a test template using borrower [% borrower.id %]',
48                 branchcode             => "",
49                 lang                   => 'default',
50             }
51         }
52     );
53     my $patron         = $builder->build_object( { class => 'Koha::Patrons' } );
54     my $firstname      = $patron->firstname;
55     my $borrowernumber = $patron->id;
56
57     my $prepared_letter = GetPreparedLetter(
58         (
59             module      => 'test',
60             letter_code => 'TEST',
61             tables      => {
62                 borrowers => $patron->id,
63             },
64         )
65     );
66
67     my $message_id = EnqueueLetter(
68         {
69             letter                 => $prepared_letter,
70             borrowernumber         => $patron->id,
71             message_transport_type => 'email'
72         }
73     );
74
75     t::lib::Mocks::mock_preference( 'NoticeCSS', '' );
76     my $css_import      = '';
77     my $message         = Koha::Notice::Messages->find($message_id);
78     my $wrapped_compare = <<"WRAPPED";
79 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
80     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
81 <html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
82   <head>
83     <title>$firstname</title>
84     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
85     $css_import
86   </head>
87   <body>
88   This is a test template using borrower $borrowernumber
89   </body>
90 </html>
91 WRAPPED
92
93     is( $message->html_content, $wrapped_compare, "html_content returned the correct html wrapped letter" );
94
95     my $css_sheet = 'https://localhost/shiny.css';
96     t::lib::Mocks::mock_preference( 'NoticeCSS', $css_sheet );
97     $css_import = qq{<link rel="stylesheet" type="text/css" href="$css_sheet">};
98
99     $wrapped_compare = <<"WRAPPED";
100 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
101     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
102 <html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
103   <head>
104     <title>$firstname</title>
105     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
106     $css_import
107   </head>
108   <body>
109   This is a test template using borrower $borrowernumber
110   </body>
111 </html>
112 WRAPPED
113
114     is(
115         $message->html_content, $wrapped_compare,
116         "html_content returned the correct html wrapped letter including stylesheet"
117     );
118
119     $schema->storage->txn_rollback;
120 };
121
122 1;