Bug 11844: Take into account itemtypes, branches, and cn_source

MARC subfields can be linked to "authorised values" that are not real
authorised values: itemtypes, branches, and cn_source.
Those were not taken into account. This patch fixes that

Test plan:
1. Create additional fields for order lines with a MARC subfield that is
   linked to one of those "fake" authorised values list
2. Follow the same steps as in the main test plan

Signed-off-by: Katrin Fischer <katrin.fischer@bsz-bw.de>
Signed-off-by: Michaela Sieber <michaela.sieber@kit.edu>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
This commit is contained in:
Julian Maurice 2023-01-20 17:12:21 +01:00 committed by Jonathan Druart
parent b997250026
commit 7be92fb26e
3 changed files with 165 additions and 5 deletions

View file

@ -0,0 +1,41 @@
package Koha::Template::Plugin::ClassSources;
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Koha is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Koha; if not, see <http://www.gnu.org/licenses>.
use Modern::Perl;
use base qw( Template::Plugin );
use C4::Context;
use Koha::ClassSources;
sub all {
my ($self, $params) = @_;
my $selected = $params->{selected};
my $default_source = C4::Context->preference("DefaultClassificationSource");
my @class_sources = grep {
$_->used
or ( $selected and $_->cn_source eq $selected )
or ( $default_source and $_->cn_source eq $default_source )
} Koha::ClassSources->search->as_list;
return @class_sources;
}
1;

View file

@ -1,4 +1,7 @@
[% USE AuthorisedValues %] [% USE AuthorisedValues %]
[% USE Branches %]
[% USE ClassSources %]
[% USE ItemTypes %]
[% IF wrap_fieldset != 0 %] [% IF wrap_fieldset != 0 %]
<fieldset class="rows"> <fieldset class="rows">
<legend>Additional fields</legend> <legend>Additional fields</legend>
@ -15,11 +18,37 @@
<select name="additional_field_[% field.id | html %]" id="additional_field_[% field.id | html %]"> <select name="additional_field_[% field.id | html %]" id="additional_field_[% field.id | html %]">
[% END %] [% END %]
<option value=""></option> <option value=""></option>
[% FOREACH av IN AuthorisedValues.GetAuthValueDropbox( authorised_value_category ) %] [% IF authorised_value_category == 'branches' %]
[% IF av.authorised_value == values.${field.id} %] [% FOREACH branch IN Branches.all() %]
<option value="[% av.authorised_value | html %]" selected="selected">[% av.lib | html %]</option> [% IF branch.branchcode == values.${field.id} %]
[% ELSE %] <option value="[% branch.branchcode | html %]" selected="selected">[% branch.branchname | html %]</option>
<option value="[% av.authorised_value | html %]">[% av.lib | html %]</option> [% ELSE %]
<option value="[% branch.branchcode | html %]">[% branch.branchname | html %]</option>
[% END %]
[% END %]
[% ELSIF authorised_value_category == 'cn_source' %]
[% FOREACH class_source IN ClassSources.all({ selected => values.${field.id} }) %]
[% IF class_source.cn_source == values.${field.id} %]
<option value="[% class_source.cn_source | html %]" selected="selected">[% class_source.description | html %]</option>
[% ELSE %]
<option value="[% class_source.cn_source | html %]">[% class_source.description | html %]</option>
[% END %]
[% END %]
[% ELSIF authorised_value_category == 'itemtypes' %]
[% FOREACH itemtype IN ItemTypes.Get() %]
[% IF itemtype.itemtype == values.${field.id} %]
<option value="[% itemtype.itemtype | html %]" selected="selected">[% itemtype.description | html %]</option>
[% ELSE %]
<option value="[% itemtype.itemtype | html %]">[% itemtype.description | html %]</option>
[% END %]
[% END %]
[% ELSE %]
[% FOREACH av IN AuthorisedValues.GetAuthValueDropbox( authorised_value_category ) %]
[% IF av.authorised_value == values.${field.id} %]
<option value="[% av.authorised_value | html %]" selected="selected">[% av.lib | html %]</option>
[% ELSE %]
<option value="[% av.authorised_value | html %]">[% av.lib | html %]</option>
[% END %]
[% END %] [% END %]
[% END %] [% END %]
</select> <span>(Authorised values for [% authorised_value_category | html %])</span> </select> <span>(Authorised values for [% authorised_value_category | html %])</span>

View file

@ -0,0 +1,90 @@
#!/usr/bin/perl
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation; either version 3 of the License, or (at your option) any later
# version.
#
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with Koha; if not, see <http://www.gnu.org/licenses>.
use Modern::Perl;
use Test::More tests => 2;
use t::lib::Mocks;
BEGIN {
use_ok('Koha::Template::Plugin::ClassSources');
}
my $schema = Koha::Database->schema;
subtest 'all' => sub {
plan tests => 4;
$schema->storage->txn_begin;
$schema->resultset('ClassSource')->delete();
$schema->resultset('ClassSource')->create({
cn_source => 'anscr',
description => 'ANSCR (Sound Recordings)',
used => 0,
class_sort_rule => 'generic',
class_split_rule => 'generic',
});
$schema->resultset('ClassSource')->create({
cn_source => 'ddc',
description => 'Dewey Decimal Classification',
used => 1,
class_sort_rule => 'dewey',
class_split_rule => 'dewey',
});
$schema->resultset('ClassSource')->create({
cn_source => 'z',
description => 'Other/Generic Classification Scheme',
used => 0,
class_sort_rule => 'generic',
class_split_rule => 'generic',
});
t::lib::Mocks::mock_preference('DefaultClassificationSource', '');
my $plugin = Koha::Template::Plugin::ClassSources->new();
subtest 'when given no parameters' => sub {
plan tests => 2;
my @class_sources = $plugin->all();
is(scalar @class_sources, 1, 'it returns only "used" class sources');
is($class_sources[0]->used, 1, 'it returns only "used" class sources');
};
subtest 'when given parameter "selected"' => sub {
plan tests => 1;
my @class_sources = $plugin->all({ selected => 'anscr' });
ok(scalar @class_sources == 2, 'it returns "used" class sources and the selected one');
};
subtest 'when DefaultClassificationSource is set to a not used class source' => sub {
plan tests => 1;
t::lib::Mocks::mock_preference('DefaultClassificationSource', 'anscr');
my @class_sources = $plugin->all();
ok(scalar @class_sources == 2, 'it returns "used" class sources and the default one');
};
subtest 'when DefaultClassificationSource is set and "selected" parameter is given' => sub {
plan tests => 1;
t::lib::Mocks::mock_preference('DefaultClassificationSource', 'anscr');
my @class_sources = $plugin->all({ selected => 'z' });
ok(scalar @class_sources == 3, 'it returns "used" class sources, the default one and the selected one');
};
$schema->storage->txn_rollback;
};