3 # This file is part of Koha
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.
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.
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>.
20 use Test::More tests => 7;
25 use Koha::ClassSources;
26 use Koha::DateUtils qw( dt_from_string );
30 # Auth required for cataloguing plugins
31 my $mAuth = Test::MockModule->new('C4::Auth');
32 $mAuth->mock( 'check_cookie_auth', sub { return ('ok') } );
34 my $schema = Koha::Database->new->schema;
35 $schema->storage->txn_begin;
37 use_ok('Koha::UI::Form::Builder::Biblio');
39 subtest 'generate_subfield_form default value' => sub {
40 my $builder = Koha::UI::Form::Builder::Biblio->new();
42 my $subfield = $builder->generate_subfield_form(
47 index_tag => int(rand(1000000)),
51 defaultvalue => 'The date is <<YYYY>>-<<MM>>-<<DD>> and user is <<USER>>',
59 my $today = dt_from_string()->ymd;
60 is($subfield->{marc_value}->{value}, "The date is $today and user is superlibrarian");
63 subtest 'generate_subfield_form branches' => sub {
64 my $builder = Koha::UI::Form::Builder::Biblio->new();
66 my $subfield = $builder->generate_subfield_form(
71 index_tag => int(rand(1000000)),
75 authorised_value => 'branches',
83 my @libraries = Koha::Libraries->search({}, {order_by => 'branchname'})->as_list;
84 my %labels = map { $_->branchcode => $_->branchname } @libraries;
85 my @values = map { $_->branchcode } @libraries;
87 is($subfield->{marc_value}->{type}, 'select');
88 is_deeply($subfield->{marc_value}->{labels}, \%labels);
89 is_deeply($subfield->{marc_value}->{values}, \@values);
92 subtest 'generate_subfield_form itemtypes' => sub {
93 my $builder = Koha::UI::Form::Builder::Biblio->new();
95 my $subfield = $builder->generate_subfield_form(
100 index_tag => int(rand(1000000)),
104 authorised_value => 'itemtypes',
112 my @itemtypes = Koha::ItemTypes->search_with_localization()->as_list;
113 my %labels = map { $_->itemtype => $_->description } @itemtypes;
114 my @values = ('', map { $_->itemtype } @itemtypes);
116 is($subfield->{marc_value}->{type}, 'select');
117 is_deeply($subfield->{marc_value}->{labels}, \%labels);
118 is_deeply($subfield->{marc_value}->{values}, \@values);
121 subtest 'generate_subfield_form class sources' => sub {
122 my $builder = Koha::UI::Form::Builder::Biblio->new();
124 my $subfield = $builder->generate_subfield_form(
129 index_tag => int(rand(1000000)),
133 authorised_value => 'cn_source',
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);
145 is($subfield->{marc_value}->{type}, 'select');
146 is_deeply($subfield->{marc_value}->{labels}, \%labels);
147 is_deeply($subfield->{marc_value}->{values}, \@values);
150 subtest 'generate_subfield_form authorised value' => sub {
151 my $builder = Koha::UI::Form::Builder::Biblio->new();
153 my $subfield = $builder->generate_subfield_form(
158 index_tag => int(rand(1000000)),
162 authorised_value => 'YES_NO',
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);
174 is($subfield->{marc_value}->{type}, 'select');
175 is_deeply($subfield->{marc_value}->{labels}, \%labels);
176 is_deeply($subfield->{marc_value}->{values}, \@values);
179 subtest 'generate_subfield_form framework plugin' => sub {
180 my $builder = Koha::UI::Form::Builder::Biblio->new();
182 my $subfield = $builder->generate_subfield_form(
187 index_tag => int(rand(1000000)),
191 value_builder => 'barcode.pl',
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);
205 $schema->storage->txn_rollback;