Bug 24606: (QA follow-up) Add test for encoding/decoding
[koha.git] / t / db_dependent / Koha / Item / Templates.t
1 #!/usr/bin/perl
2
3 # Copyright 2022 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 use utf8;
22
23 use Koha::Database;
24
25 use t::lib::TestBuilder;
26
27 use Test::More tests => 2;
28
29 my $schema  = Koha::Database->new->schema;
30 my $builder = t::lib::TestBuilder->new;
31
32 use_ok("Koha::Item::Templates");
33
34 subtest 'get_available' => sub {
35     plan tests => 3;
36
37     $schema->storage->txn_begin;
38
39     Koha::Item::Templates->search->delete;
40
41     my $library  = $builder->build( { source => 'Branch' } );
42     my $category = $builder->build( { source => 'Category' } );
43     my $patron_1 = Koha::Patron->new(
44         {
45             branchcode   => $library->{branchcode},
46             categorycode => $category->{categorycode},
47             surname      => 'surname for patron1',
48             firstname    => 'firstname for patron1',
49         }
50     )->store;
51     my $patron_2 = Koha::Patron->new(
52         {
53             branchcode   => $library->{branchcode},
54             categorycode => $category->{categorycode},
55             surname      => 'surname for patron2',
56             firstname    => 'firstname for patron2',
57         }
58     )->store;
59
60     my $owner_template = Koha::Item::Template->new(
61         {
62             patron_id => $patron_1->id,
63             name      => 'My template',
64             contents  => { location => 'test' },
65             is_shared => 0,
66         }
67     )->store();
68
69     my $shared_template = Koha::Item::Template->new(
70         {
71             patron_id => $patron_2->id,
72             name      => 'My template',
73             contents  => { location => 'test' },
74             is_shared => 1,
75         }
76     )->store();
77
78     my $unshared_template = Koha::Item::Template->new(
79         {
80             patron_id => $patron_2->id,
81             name      => 'My template',
82             contents  => { location => 'test🙂' },
83             is_shared => 0,
84         }
85     )->store;
86     $unshared_template->discard_changes; # refresh
87     is( $unshared_template->decoded_contents->{location}, 'test🙂', 'Tested encoding/decoding' );
88
89     my $templates = Koha::Item::Templates->get_available( $patron_1->id );
90     is( $templates->{owned}->count, 1, "Got back one owned template" );
91     is( $templates->{shared}->count, 1, "Got back one shared templated" );
92
93     $schema->storage->txn_rollback;
94 };