Bug 19532: (follow-up) aria-hidden attr on OPAC, and more
[koha.git] / t / db_dependent / Koha / Notices.t
1 #!/usr/bin/perl
2
3 # Copyright 2017 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 => 4;
23
24 use Koha::Notice::Templates;
25 use Koha::Database;
26
27 use t::lib::TestBuilder;
28 use t::lib::Mocks;
29
30 my $schema = Koha::Database->new->schema;
31 $schema->storage->txn_begin;
32
33 my $builder       = t::lib::TestBuilder->new;
34 my $library       = $builder->build( { source => 'Branch' } );
35 my $nb_of_templates = Koha::Notice::Templates->search->count;
36 my ( $module, $mtt ) = ( 'circulation', 'email' );
37 my $new_template = Koha::Notice::Template->new(
38     {
39         module                 => $module,
40         code                   => 'tmpl_code_for_t',
41         branchcode             => $library->{branchcode},
42         name                   => 'my template name for test 1',
43         title                  => 'my template title for test 1',
44         content                => 'This one is almost empty',
45         message_transport_type => $mtt,
46     }
47 )->store;
48
49 is(
50     Koha::Notice::Templates->search->count,
51     $nb_of_templates + 1,
52     'The template should have been added'
53 );
54
55 my $retrieved_template = Koha::Notice::Templates->find(
56     {
57         module                 => $module,
58         code                   => $new_template->code,
59         branchcode             => $library->{branchcode},
60         message_transport_type => $mtt,
61     }
62 );
63 is( $retrieved_template->name, $new_template->name,
64     'Find a notice template by pk should return the correct template' );
65
66 $retrieved_template->delete;
67 is( Koha::Notice::Templates->search->count,
68     $nb_of_templates, 'Delete should have deleted the template' );
69
70 subtest 'find_effective_template' => sub {
71     plan tests => 7;
72
73     my $default_template = $builder->build_object(
74         { class => 'Koha::Notice::Templates', value => { branchcode => '', lang => 'default' } }
75     );
76     my $key = {
77         module                 => $default_template->module,
78         code                   => $default_template->code,
79         message_transport_type => $default_template->message_transport_type,
80     };
81
82     my $library_specific_template = $builder->build_object(
83         { class => 'Koha::Notice::Templates', value => { %$key, lang => 'default' } }
84     );
85
86     my $es_template = $builder->build_object(
87         {
88             class => 'Koha::Notice::Templates',
89             value => { %$key, lang => 'es-ES' },
90         }
91     );
92
93     $key->{branchcode} = $es_template->branchcode;
94
95     t::lib::Mocks::mock_preference( 'TranslateNotices', 0 );
96
97     my $template = Koha::Notice::Templates->find_effective_template($key);
98     is( $template->lang, 'default', 'no lang passed, default is returned' );
99     $template = Koha::Notice::Templates->find_effective_template( { %$key, lang => 'es-ES' } );
100     is( $template->lang, 'default',
101         'TranslateNotices is off, default is returned' );
102
103     t::lib::Mocks::mock_preference( 'TranslateNotices', 1 );
104     $template = Koha::Notice::Templates->find_effective_template($key);
105     is( $template->lang, 'default', 'no lang passed, default is returned' );
106     $template = Koha::Notice::Templates->find_effective_template( { %$key, lang => 'es-ES' } );
107     is( $template->lang, 'es-ES',
108         'TranslateNotices is on and es-ES is requested, es-ES is returned' );
109
110
111     {    # IndependentBranches => 1
112         t::lib::Mocks::mock_userenv( { branchcode => $library_specific_template->branchcode, flag => 0 } );
113         t::lib::Mocks::mock_preference( 'IndependentBranches', 1 );
114         $template = Koha::Notice::Templates->find_effective_template( { %$key, branchcode => $library_specific_template->branchcode } );
115         is( $template->content, $library_specific_template->content,
116             'IndependentBranches is on, logged in patron is not superlibrarian but asks for their specific template, it is returned'
117         );
118
119         my $another_library = $builder->build_object( { class => 'Koha::Libraries' } );
120         t::lib::Mocks::mock_userenv( { branchcode => $another_library->branchcode, flag => 0 } );
121         $template = Koha::Notice::Templates->find_effective_template($key);
122         is( $template->content, $default_template->content,
123 'IndependentBranches is on, logged in patron is not superlibrarian, default is returned'
124         );
125     }
126
127     t::lib::Mocks::mock_preference( 'IndependentBranches', 0 );
128     $es_template->delete;
129
130     $template = Koha::Notice::Templates->find_effective_template( { %$key, lang => 'es-ES' } );
131     is( $template->lang, 'default',
132         'TranslateNotices is on and es-ES is requested but does not exist, default is returned'
133     );
134
135 };
136
137 $schema->storage->txn_rollback;
138