Lines 26-32
use Koha::Library;
Link Here
|
26 |
use t::lib::Mocks; |
26 |
use t::lib::Mocks; |
27 |
use t::lib::TestBuilder; |
27 |
use t::lib::TestBuilder; |
28 |
|
28 |
|
29 |
use Test::More tests => 9; |
29 |
use Test::More tests => 10; |
30 |
|
30 |
|
31 |
use Test::Warn; |
31 |
use Test::Warn; |
32 |
|
32 |
|
Lines 553-565
subtest 'C4::Biblio::EmbedItemsInMarcBiblio' => sub {
Link Here
|
553 |
$schema->storage->txn_rollback; |
553 |
$schema->storage->txn_rollback; |
554 |
}; |
554 |
}; |
555 |
|
555 |
|
|
|
556 |
|
557 |
subtest 'C4::Items::_build_default_values_for_mod_marc' => sub { |
558 |
plan tests => 4; |
559 |
|
560 |
$schema->storage->txn_begin(); |
561 |
|
562 |
# Clear cache |
563 |
$C4::Context::context->{marcfromkohafield} = undef; |
564 |
$C4::Biblio::inverted_field_map = undef; |
565 |
|
566 |
my $builder = t::lib::TestBuilder->new; |
567 |
my $framework = $builder->build({ |
568 |
source => 'BiblioFramework', |
569 |
}); |
570 |
# Link biblio.biblionumber and biblioitems.biblioitemnumber to avoid _koha_marc_update_bib_ids to fail with 'no biblio[item]number tag for framework" |
571 |
$builder->build({ |
572 |
source => 'MarcSubfieldStructure', |
573 |
value => { |
574 |
frameworkcode => $framework->{frameworkcode}, |
575 |
kohafield => 'biblio.biblionumber', |
576 |
tagfield => '999', |
577 |
tagsubfield => 'c', |
578 |
} |
579 |
}); |
580 |
$builder->build({ |
581 |
source => 'MarcSubfieldStructure', |
582 |
value => { |
583 |
frameworkcode => $framework->{frameworkcode}, |
584 |
kohafield => 'biblioitems.biblioitemnumber', |
585 |
tagfield => '999', |
586 |
tagsubfield => 'd', |
587 |
} |
588 |
}); |
589 |
my $mss_itemnumber = $builder->build({ |
590 |
source => 'MarcSubfieldStructure', |
591 |
value => { |
592 |
frameworkcode => $framework->{frameworkcode}, |
593 |
kohafield => 'items.itemnumber', |
594 |
tagfield => '952', |
595 |
tagsubfield => '9', |
596 |
} |
597 |
}); |
598 |
|
599 |
my $mss_barcode = $builder->build({ |
600 |
source => 'MarcSubfieldStructure', |
601 |
value => { |
602 |
frameworkcode => $framework->{frameworkcode}, |
603 |
kohafield => 'items.barcode', |
604 |
tagfield => '952', |
605 |
tagsubfield => 'p', |
606 |
} |
607 |
}); |
608 |
|
609 |
# Create a record with a barcode |
610 |
my ($biblionumber) = get_biblio( $framework->{frameworkcode} ); |
611 |
my $item_record = new MARC::Record; |
612 |
my $a_barcode = 'a barcode'; |
613 |
my $barcode_field = MARC::Field->new( |
614 |
'952', ' ', ' ', |
615 |
p => $a_barcode, |
616 |
); |
617 |
$item_record->append_fields( $barcode_field ); |
618 |
my (undef, undef, $item_itemnumber) = AddItemFromMarc($item_record, $biblionumber); |
619 |
|
620 |
# Make sure everything has been set up |
621 |
my $item = GetItem($item_itemnumber); |
622 |
is( $item->{barcode}, $a_barcode, 'Everything has been set up correctly, the barcode is defined as expected' ); |
623 |
|
624 |
# Delete the barcode field and save the record |
625 |
$item_record->delete_fields( $barcode_field ); |
626 |
ModItemFromMarc($item_record, $biblionumber, $item_itemnumber); |
627 |
$item = GetItem($item_itemnumber); |
628 |
is( $item->{barcode}, undef, 'The default value should have been set to the barcode, the field is mapped to a kohafield' ); |
629 |
|
630 |
# Re-add the barcode field and save the record |
631 |
$item_record->append_fields( $barcode_field ); |
632 |
ModItemFromMarc($item_record, $biblionumber, $item_itemnumber); |
633 |
$item = GetItem($item_itemnumber); |
634 |
is( $item->{barcode}, $a_barcode, 'Everything has been set up correctly, the barcode is defined as expected' ); |
635 |
|
636 |
# Remove the mapping |
637 |
my $dbh = C4::Context->dbh; |
638 |
$dbh->do(q| |
639 |
DELETE FROM marc_subfield_structure |
640 |
WHERE kohafield = 'items.barcode' |
641 |
AND frameworkcode = ? |
642 |
|, undef, $framework->{frameworkcode} ); |
643 |
|
644 |
# And make sure the caches are cleared |
645 |
$C4::Context::context->{marcfromkohafield} = undef; |
646 |
$C4::Biblio::inverted_field_map = undef; |
647 |
my $cache = Koha::Cache->get_instance(); |
648 |
$cache->clear_from_cache("default_value_for_mod_marc-" . $framework->{frameworkcode} ); |
649 |
|
650 |
# Update the MARC field with another value |
651 |
$item_record->delete_fields( $barcode_field ); |
652 |
my $another_barcode = 'another_barcode'; |
653 |
my $another_barcode_field = MARC::Field->new( |
654 |
'952', ' ', ' ', |
655 |
p => $another_barcode, |
656 |
); |
657 |
$item_record->append_fields( $another_barcode_field ); |
658 |
# The DB value should not have been updated |
659 |
ModItemFromMarc($item_record, $biblionumber, $item_itemnumber); |
660 |
$item = GetItem($item_itemnumber); |
661 |
is ( $item->{barcode}, $a_barcode, 'items.barcode is not mapped anymore, so the DB column has not been updated' ); |
662 |
|
663 |
$schema->storage->txn_rollback; |
664 |
}; |
665 |
|
556 |
# Helper method to set up a Biblio. |
666 |
# Helper method to set up a Biblio. |
557 |
sub get_biblio { |
667 |
sub get_biblio { |
|
|
668 |
my ( $frameworkcode ) = @_; |
669 |
$frameworkcode //= ''; |
558 |
my $bib = MARC::Record->new(); |
670 |
my $bib = MARC::Record->new(); |
559 |
$bib->append_fields( |
671 |
$bib->append_fields( |
560 |
MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'), |
672 |
MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'), |
561 |
MARC::Field->new('245', ' ', ' ', a => 'Silence in the library'), |
673 |
MARC::Field->new('245', ' ', ' ', a => 'Silence in the library'), |
562 |
); |
674 |
); |
563 |
my ($bibnum, $bibitemnum) = AddBiblio($bib, ''); |
675 |
my ($bibnum, $bibitemnum) = AddBiblio($bib, $frameworkcode); |
564 |
return ($bibnum, $bibitemnum); |
676 |
return ($bibnum, $bibitemnum); |
565 |
} |
677 |
} |
566 |
- |
|
|