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