3 # Copyright 2017 Koha Development team
5 # This file is part of Koha
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.
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.
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>.
22 use Test::More tests => 4;
24 use Koha::Notice::Templates;
27 use t::lib::TestBuilder;
30 my $schema = Koha::Database->new->schema;
31 $schema->storage->txn_begin;
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(
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,
50 Koha::Notice::Templates->search->count,
52 'The template should have been added'
55 my $retrieved_template = Koha::Notice::Templates->find(
58 code => $new_template->code,
59 branchcode => $library->{branchcode},
60 message_transport_type => $mtt,
63 is( $retrieved_template->name, $new_template->name,
64 'Find a notice template by pk should return the correct template' );
66 $retrieved_template->delete;
67 is( Koha::Notice::Templates->search->count,
68 $nb_of_templates, 'Delete should have deleted the template' );
70 subtest 'find_effective_template' => sub {
73 my $default_template = $builder->build_object(
74 { class => 'Koha::Notice::Templates', value => { branchcode => '', lang => 'default' } }
77 module => $default_template->module,
78 code => $default_template->code,
79 message_transport_type => $default_template->message_transport_type,
82 my $library_specific_template = $builder->build_object(
83 { class => 'Koha::Notice::Templates', value => { %$key, lang => 'default' } }
86 my $es_template = $builder->build_object(
88 class => 'Koha::Notice::Templates',
89 value => { %$key, lang => 'es-ES' },
93 $key->{branchcode} = $es_template->branchcode;
95 t::lib::Mocks::mock_preference( 'TranslateNotices', 0 );
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' );
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' );
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'
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'
127 t::lib::Mocks::mock_preference( 'IndependentBranches', 0 );
128 $es_template->delete;
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'
137 $schema->storage->txn_rollback;