Bug 33161: Unit tests

To test:
1. Apply this patch
2. Run:
   $ ktd --shell
  k$ prove t/db_dependent/Koha/Item.t
=> SUCCESS: Tests pass!
3. Sign off :-D

Edit: added missing test for unmapped attribute

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
This commit is contained in:
Tomas Cohen Arazi 2023-03-09 19:23:03 -03:00
parent 7bde825a33
commit 35dd17735f
1 changed files with 197 additions and 1 deletions

View File

@ -20,7 +20,7 @@
use Modern::Perl;
use utf8;
use Test::More tests => 27;
use Test::More tests => 28;
use Test::Exception;
use Test::MockModule;
@ -1441,6 +1441,202 @@ subtest 'columns_to_str' => sub {
};
subtest 'strings_map() tests' => sub {
plan tests => 6;
$schema->storage->txn_begin;
my ( $itemtag, $itemsubfield ) = C4::Biblio::GetMarcFromKohaField("items.itemnumber");
my $cache = Koha::Caches->get_instance();
$cache->clear_from_cache("MarcStructure-0-");
$cache->clear_from_cache("MarcStructure-1-");
$cache->clear_from_cache("MarcSubfieldStructure-");
# Recreating subfields just to be sure tests will be ok
# 1 => av (LOST)
# 3 => no link
# a => branches
# y => itemtypes
Koha::MarcSubfieldStructures->search(
{
frameworkcode => '',
tagfield => $itemtag,
tagsubfield => [ '1', '2', '3', 'a', 'y' ],
}
)->delete; # In case it exist already
Koha::MarcSubfieldStructure->new(
{
authorised_value => 'LOST',
defaultvalue => '',
frameworkcode => '',
kohafield => 'items.itemlost',
repeatable => 1,
tab => 10,
tagfield => $itemtag,
tagsubfield => '1',
}
)->store;
Koha::MarcSubfieldStructure->new(
{
authorised_value => 'cn_source',
defaultvalue => '',
frameworkcode => '',
kohafield => 'items.cn_source',
repeatable => 1,
tab => 10,
tagfield => $itemtag,
tagsubfield => '2',
}
)->store;
Koha::MarcSubfieldStructure->new(
{
authorised_value => '',
defaultvalue => '',
frameworkcode => '',
kohafield => 'items.materials',
repeatable => 1,
tab => 10,
tagfield => $itemtag,
tagsubfield => '3',
}
)->store;
Koha::MarcSubfieldStructure->new(
{
authorised_value => 'branches',
defaultvalue => '',
frameworkcode => '',
kohafield => 'items.homebranch',
repeatable => 1,
tab => 10,
tagfield => $itemtag,
tagsubfield => 'a',
}
)->store;
Koha::MarcSubfieldStructure->new(
{
authorised_value => 'itemtypes',
defaultvalue => '',
frameworkcode => '',
kohafield => 'items.itype',
repeatable => 1,
tab => 10,
tagfield => $itemtag,
tagsubfield => 'y',
}
)->store;
my $itype = $builder->build_object( { class => 'Koha::ItemTypes' } );
my $library = $builder->build_object( { class => 'Koha::Libraries' } );
my $biblio = $builder->build_sample_biblio( { frameworkcode => '' } );
my $item = $builder->build_sample_item(
{
biblionumber => $biblio->id,
library => $library->id
}
);
Koha::AuthorisedValues->search( { authorised_value => 3, category => 'LOST' } )->delete;
my $lost_av = $builder->build_object(
{
class => 'Koha::AuthorisedValues',
value => {
authorised_value => 3,
category => 'LOST',
lib => 'internal description',
lib_opac => 'public description',
}
}
);
my $class_sort_rule = $builder->build_object( { class => 'Koha::ClassSortRules', value => { sort_routine => 'Generic' } } );
my $class_split_rule = $builder->build_object( { class => 'Koha::ClassSplitRules' } );
my $class_source = $builder->build_object(
{
class => 'Koha::ClassSources',
value => {
class_sort_rule => $class_sort_rule->class_sort_rule,
class_split_rule => $class_split_rule->class_split_rule,
}
}
);
$item->set(
{
cn_source => $class_source->id,
itemlost => $lost_av->authorised_value,
itype => $itype->itemtype,
materials => 'Suff',
}
)->store->discard_changes;
my $strings = $item->strings_map;
subtest 'unmapped field tests' => sub {
plan tests => 1;
ok( !exists $strings->{materials}, "Unmapped field not present" );
};
subtest 'av handling' => sub {
plan tests => 4;
ok( exists $strings->{itemlost}, "'itemlost' entry exists" );
is( $strings->{itemlost}->{str}, $lost_av->lib, "'str' set to av->lib" );
is( $strings->{itemlost}->{type}, 'av', "'type' is 'av'" );
is( $strings->{itemlost}->{category}, 'LOST', "'category' exists and set to 'LOST'" );
};
subtest 'cn_source handling' => sub {
plan tests => 3;
ok( exists $strings->{cn_source}, "'cn_source' entry exists" );
is( $strings->{cn_source}->{str}, $class_source->description, "'str' set to \$class_source->description" );
is( $strings->{cn_source}->{type}, 'call_number_source', "type is 'library'" );
};
subtest 'branches handling' => sub {
plan tests => 3;
ok( exists $strings->{homebranch}, "'homebranch' entry exists" );
is( $strings->{homebranch}->{str}, $library->branchname, "'str' set to 'branchname'" );
is( $strings->{homebranch}->{type}, 'library', "type is 'library'" );
};
subtest 'itemtype handling' => sub {
plan tests => 3;
ok( exists $strings->{itype}, "'itype' entry exists" );
is( $strings->{itype}->{str}, $itype->description, "'str' correctly set" );
is( $strings->{itype}->{type}, 'item_type', "'type' is 'item_type'" );
};
subtest 'public flag tests' => sub {
plan tests => 4;
$strings = $item->strings_map( { public => 1 } );
ok( exists $strings->{itemlost}, "'itemlost' entry exists" );
is( $strings->{itemlost}->{str}, $lost_av->lib_opac, "'str' set to av->lib" );
is( $strings->{itemlost}->{type}, 'av', "'type' is 'av'" );
is( $strings->{itemlost}->{category}, 'LOST', "'category' exists and set to 'LOST'" );
};
$cache->clear_from_cache("MarcStructure-0-");
$cache->clear_from_cache("MarcStructure-1-");
$cache->clear_from_cache("MarcSubfieldStructure-");
$schema->storage->txn_rollback;
};
subtest 'store() tests' => sub {
plan tests => 3;