Bug 24879: (follow-up) Fix test suite
[koha.git] / t / db_dependent / Koha / UI / Form / Builder / Biblio.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use Test::More tests => 7;
21 use Test::MockModule;
22
23 use C4::ClassSource;
24
25 use Koha::ClassSources;
26 use Koha::DateUtils qw( dt_from_string );
27 use Koha::ItemTypes;
28 use Koha::Libraries;
29
30 # Auth required for cataloguing plugins
31 my $mAuth = Test::MockModule->new('C4::Auth');
32 $mAuth->mock( 'check_cookie_auth', sub { return ('ok') } );
33
34 my $schema = Koha::Database->new->schema;
35 $schema->storage->txn_begin;
36
37 use_ok('Koha::UI::Form::Builder::Biblio');
38
39 subtest 'generate_subfield_form default value' => sub {
40     my $builder = Koha::UI::Form::Builder::Biblio->new();
41
42     my $subfield = $builder->generate_subfield_form(
43         {
44             tag => '999',
45             subfield => '9',
46             value => '',
47             index_tag => int(rand(1000000)),
48             tagslib => {
49                 '999' => {
50                     '9' => {
51                         defaultvalue => 'The date is <<YYYY>>-<<MM>>-<<DD>> and user is <<USER>>',
52                         hidden => 0,
53                     }
54                 }
55             },
56         },
57     );
58
59     my $today = dt_from_string()->ymd;
60     is($subfield->{marc_value}->{value}, "The date is $today and user is superlibrarian");
61 };
62
63 subtest 'generate_subfield_form branches' => sub {
64     my $builder = Koha::UI::Form::Builder::Biblio->new();
65
66     my $subfield = $builder->generate_subfield_form(
67         {
68             tag => '999',
69             subfield => '9',
70             value => '',
71             index_tag => int(rand(1000000)),
72             tagslib => {
73                 '999' => {
74                     '9' => {
75                         authorised_value => 'branches',
76                         hidden => 0,
77                     }
78                 }
79             },
80         },
81     );
82
83     my @libraries = Koha::Libraries->search({}, {order_by => 'branchname'})->as_list;
84     my %labels = map { $_->branchcode => $_->branchname } @libraries;
85     my @values = map { $_->branchcode } @libraries;
86
87     is($subfield->{marc_value}->{type}, 'select');
88     is_deeply($subfield->{marc_value}->{labels}, \%labels);
89     is_deeply($subfield->{marc_value}->{values}, \@values);
90 };
91
92 subtest 'generate_subfield_form itemtypes' => sub {
93     my $builder = Koha::UI::Form::Builder::Biblio->new();
94
95     my $subfield = $builder->generate_subfield_form(
96         {
97             tag => '999',
98             subfield => '9',
99             value => '',
100             index_tag => int(rand(1000000)),
101             tagslib => {
102                 '999' => {
103                     '9' => {
104                         authorised_value => 'itemtypes',
105                         hidden => 0,
106                     }
107                 }
108             },
109         },
110     );
111
112     my @itemtypes = Koha::ItemTypes->search_with_localization()->as_list;
113     my %labels = map { $_->itemtype => $_->description } @itemtypes;
114     my @values = ('', map { $_->itemtype } @itemtypes);
115
116     is($subfield->{marc_value}->{type}, 'select');
117     is_deeply($subfield->{marc_value}->{labels}, \%labels);
118     is_deeply($subfield->{marc_value}->{values}, \@values);
119 };
120
121 subtest 'generate_subfield_form class sources' => sub {
122     my $builder = Koha::UI::Form::Builder::Biblio->new();
123
124     my $subfield = $builder->generate_subfield_form(
125         {
126             tag => '999',
127             subfield => '9',
128             value => '',
129             index_tag => int(rand(1000000)),
130             tagslib => {
131                 '999' => {
132                     '9' => {
133                         authorised_value => 'cn_source',
134                         hidden => 0,
135                     }
136                 }
137             },
138         },
139     );
140
141     my @class_sources = Koha::ClassSources->search({used => 1}, {order_by => 'cn_source'})->as_list;
142     my %labels = map { $_->cn_source => $_->description } @class_sources;
143     my @values = ('', map { $_->cn_source } @class_sources);
144
145     is($subfield->{marc_value}->{type}, 'select');
146     is_deeply($subfield->{marc_value}->{labels}, \%labels);
147     is_deeply($subfield->{marc_value}->{values}, \@values);
148 };
149
150 subtest 'generate_subfield_form authorised value' => sub {
151     my $builder = Koha::UI::Form::Builder::Biblio->new();
152
153     my $subfield = $builder->generate_subfield_form(
154         {
155             tag => '999',
156             subfield => '9',
157             value => '',
158             index_tag => int(rand(1000000)),
159             tagslib => {
160                 '999' => {
161                     '9' => {
162                         authorised_value => 'YES_NO',
163                         hidden => 0,
164                     }
165                 }
166             },
167         },
168     );
169
170     my @authorised_values = Koha::AuthorisedValues->search({category => 'YES_NO'}, {order_by => ['lib', 'lib_opac']})->as_list;
171     my %labels = map { $_->authorised_value => $_->lib } @authorised_values;
172     my @values = ('', map { $_->authorised_value } @authorised_values);
173
174     is($subfield->{marc_value}->{type}, 'select');
175     is_deeply($subfield->{marc_value}->{labels}, \%labels);
176     is_deeply($subfield->{marc_value}->{values}, \@values);
177 };
178
179 subtest 'generate_subfield_form framework plugin' => sub {
180     my $builder = Koha::UI::Form::Builder::Biblio->new();
181
182     my $subfield = $builder->generate_subfield_form(
183         {
184             tag => '999',
185             subfield => '9',
186             value => '',
187             index_tag => int(rand(1000000)),
188             tagslib => {
189                 '999' => {
190                     '9' => {
191                         value_builder => 'barcode.pl',
192                         hidden => 0,
193                     }
194                 }
195             },
196         },
197     );
198
199     is($subfield->{marc_value}->{type}, 'text_complex');
200     is($subfield->{marc_value}->{plugin}, 'barcode.pl');
201     is($subfield->{marc_value}->{noclick}, 1);
202     like($subfield->{marc_value}->{javascript}, qr,<script>.*</script>,s);
203 };
204
205 $schema->storage->txn_rollback;