View | Details | Raw Unified | Return to bug 39545
Collapse All | Expand All

(-)a/Koha/Biblio.pm (-92 / +107 lines)
Lines 1921-2090 sub generate_marc_host_field { Link Here
1921
1921
1922
    my $marcflavour = C4::Context->preference('marcflavour');
1922
    my $marcflavour = C4::Context->preference('marcflavour');
1923
    my $marc_host   = $self->metadata->record;
1923
    my $marc_host   = $self->metadata->record;
1924
1924
    my @sfd;
1925
    my @sfd;
1925
    my $host_field;
1926
    my $host_field;
1926
    my $link_field;
1927
    my $link_field;
1927
1928
1928
    if ( $marcflavour eq 'MARC21' ) {
1929
    if ( $marcflavour eq 'MARC21' ) {
1929
1930
1930
        # Author
1931
        # Attempt to clone the main entry (100, 110, or 111)
1931
        if ( $host_field = $marc_host->field('100') || $marc_host->field('110') || $marc_host->field('111') ) {
1932
        my $main_entry = $marc_host->field('100') || $marc_host->field('110') || $marc_host->field('111');
1932
            my $s = $host_field->as_string('ab');
1933
        $main_entry = $main_entry ? $main_entry->clone : undef;
1933
            if ($s) {
1934
1934
                push @sfd, ( a => $s );
1935
        # Clean unwanted subfields based on tag type
1936
        if ($main_entry) {
1937
            if ( $main_entry->tag eq '111' ) {
1938
                $main_entry->delete_subfield( code => qr/[94j]/ );
1939
            } else {
1940
                $main_entry->delete_subfield( code => qr/[94e]/ );
1941
            }
1942
        }
1943
1944
        # Construct subfield 7 from leader and main entry tag
1945
        my $s7 = "nn" . substr( $marc_host->leader, 6, 2 );
1946
        if ($main_entry) {
1947
            my $c1 = 'n';
1948
            if ( $main_entry->tag =~ /^1[01]/ ) {
1949
                $c1 = $main_entry->indicator('1');
1950
                $c1 = $main_entry->tag eq '100' ? 1 : 2 unless $c1 =~ /\d/;
1935
            }
1951
            }
1952
            my $c0 =
1953
                  ( $main_entry->tag eq '100' ) ? 'p'
1954
                : ( $main_entry->tag eq '110' ) ? 'c'
1955
                : ( $main_entry->tag eq '111' ) ? 'm'
1956
                :                                 'u';
1957
            substr( $s7, 0, 2, $c0 . $c1 );
1958
        }
1959
        push @sfd, ( '7' => $s7 );
1960
1961
        # Subfield a - cleaned main entry string
1962
        if ($main_entry) {
1963
            my $a = $main_entry->as_string;
1964
            $a =~ s/\.$// unless $a =~ /\b[a-z]{1,2}\.$/i;
1965
            push @sfd, ( 'a' => $a );
1966
        }
1967
1968
        # Subfield t - title from 245, cleaned
1969
        if ( my $f245 = $marc_host->field('245') ) {
1970
            my $f245c = $f245->clone;
1971
            $f245c->delete_subfield( code => 'c' );
1972
            my $t = $f245c->as_string;
1973
            $t =~ s/[\s\/\\.]+$//;
1974
            my $nonfiling = $f245c->indicator('2') // 0;
1975
            $t = ucfirst substr( $t, $nonfiling );
1976
            push @sfd, ( 't' => $t );
1936
        }
1977
        }
1937
1978
1938
        # Title
1979
        # Subfield b - edition from 250
1939
        if ( $host_field = $marc_host->field('245') ) {
1980
        if ( my $f250 = $marc_host->field('250') ) {
1940
            my $s = $host_field->as_string('abnp');
1981
            my $b = $f250->as_string;
1941
            if ($s) {
1982
            $b =~ s/\.$//;
1942
                push @sfd, ( t => $s );
1983
            if ($b) {
1984
                push @sfd, ( 'b' => $b );
1943
            }
1985
            }
1944
        }
1986
        }
1945
1987
1946
        # Publication
1988
        # Subfield d - publication info from 264/260
1947
        my $p;
1989
        my $d;
1948
        my @publication_fields = $marc_host->field('264');
1990
        my @publication_fields = $marc_host->field('264');
1949
        @publication_fields = $marc_host->field('260') unless (@publication_fields);
1991
        @publication_fields = $marc_host->field('260') unless (@publication_fields);
1950
        my $index = 0;
1992
        my $index = 0;
1951
        for my $host_field (@publication_fields) {
1993
        for my $publication_field (@publication_fields) {
1952
1994
1953
            # Use first entry unless we find a preferred indicator1 = 3
1995
            # Use first entry unless we find a preferred indicator1 = 3
1954
            if ( $index == 0 ) {
1996
            if ( $index == 0 ) {
1955
                my $s = $host_field->as_string('abc');
1997
                my $s = $publication_field->as_string('abc');
1998
                $s =~ s/\.$//;
1956
                if ($s) {
1999
                if ($s) {
1957
                    $p = $s;
2000
                    $d = $s;
1958
                }
2001
                }
1959
                $index++;
2002
                $index++;
1960
            }
2003
            }
1961
            if ( $host_field->indicator(1) && ( $host_field->indicator(1) eq '3' ) ) {
2004
            if ( $publication_field->indicator(1) && ( $publication_field->indicator(1) eq '3' ) ) {
1962
                my $s = $host_field->as_string('abc');
2005
                my $s = $publication_field->as_string('abc');
2006
                $s =~ s/\.$//;
1963
                if ($s) {
2007
                if ($s) {
1964
                    $p = $s;
2008
                    $d = $s;
1965
                }
2009
                }
1966
                last;
2010
                last;
1967
            }
2011
            }
1968
        }
2012
        }
1969
        push @sfd, ( d => $p ) if $p;
2013
        push @sfd, ( d => $d ) if $d;
1970
2014
1971
        # Uniform title
2015
        # Subfield k - host info from 800-830 fields
1972
        if ( $host_field = $marc_host->field('240') ) {
2016
        for my $f ( $marc_host->field('8[013][01]') ) {
1973
            my $s = $host_field->as_string('a');
2017
            my $k = $f->as_string('abcdnjltnp');
1974
            if ($s) {
2018
            $k .= ', ISSN ' . $f->subfield('x') if $f->subfield('x');
1975
                push @sfd, ( s => $s );
2019
            $k .= ' ; ' . $f->subfield('v')     if $f->subfield('v');
1976
            }
2020
            push @sfd, ( 'k' => $k );
1977
        }
2021
        }
1978
2022
1979
        # Edition
2023
        # Subfield x - ISSN from 022
1980
        if ( $host_field = $marc_host->field('250') ) {
2024
        for my $f ( $marc_host->field('022') ) {
1981
            my $s = $host_field->as_string('ab');
2025
            push @sfd, ( 'x' => $f->subfield('a') ) if $f->subfield('a');
1982
            if ($s) {
1983
                push @sfd, ( b => $s);
1984
            }
1985
        }
2026
        }
1986
2027
1987
        # ISSN
2028
        # Subfield z - ISBN from 020
1988
        if ( $host_field = $marc_host->field('022') ) {
2029
        for my $f ( $marc_host->field('020') ) {
1989
            my $s = $host_field->as_string('a');
2030
            push @sfd, ( 'z' => $f->subfield('a') ) if $f->subfield('a');
1990
            if ($s) {
1991
                push @sfd, ( x => $s );
1992
            }
1993
        }
2031
        }
1994
2032
1995
        # ISBN
2033
        # Subfield w - control number (001 and optionally 003)
1996
        if ( $host_field = $marc_host->field('020') ) {
1997
            my $s = $host_field->as_string('a');
1998
            if ($s) {
1999
                push @sfd, ( z => $s );
2000
            }
2001
        }
2002
        if ( C4::Context->preference('UseControlNumber') ) {
2034
        if ( C4::Context->preference('UseControlNumber') ) {
2003
2035
            if ( my $f001 = $marc_host->field('001') ) {
2004
            my $w;
2036
                my $w = $f001->data;
2005
2037
                if ( my $f003 = $marc_host->field('003') ) {
2006
            # Control number
2038
                    $w = '(' . $f003->data . ')' . $w;
2007
            if ( $host_field = $marc_host->field('001') ) {
2039
                }
2008
                $w = $host_field->data();
2040
                push @sfd, ( 'w' => $w );
2009
            }
2010
2011
            # Control number identifier
2012
            if ( $host_field = $marc_host->field('003') ) {
2013
                $w = '(' . $host_field->data() . ')' . $w;
2014
            }
2041
            }
2015
2016
            push @sfd, ( w => $w );
2017
        }
2042
        }
2043
2044
        # Construct 773 link field
2018
        $link_field = MARC::Field->new( 773, '0', ' ', @sfd );
2045
        $link_field = MARC::Field->new( 773, '0', ' ', @sfd );
2046
2019
    } elsif ( $marcflavour eq 'UNIMARC' ) {
2047
    } elsif ( $marcflavour eq 'UNIMARC' ) {
2020
2048
2021
        # Author
2049
        # Author (700/710/720)
2022
        if ( $host_field = $marc_host->field('700') || $marc_host->field('710') || $marc_host->field('720') ) {
2050
        if ( $host_field = $marc_host->field('700') || $marc_host->field('710') || $marc_host->field('720') ) {
2023
            my $s = $host_field->as_string('ab');
2051
            my $s = $host_field->as_string('ab');
2024
            if ($s) {
2052
            push @sfd, ( 'a' => $s ) if $s;
2025
                push @sfd, ( a => $s );
2026
            }
2027
        }
2053
        }
2028
2054
2029
        # Title
2055
        # Title (200)
2030
        if ( $host_field = $marc_host->field('200') ) {
2056
        if ( $host_field = $marc_host->field('200') ) {
2031
            my $s = $host_field->as_string('a');
2057
            my $s = $host_field->as_string('a');
2032
            if ($s) {
2058
            push @sfd, ( 't' => $s ) if $s;
2033
                push @sfd, ( t => $s );
2034
            }
2035
        }
2059
        }
2036
2060
2037
        # Place of publication
2061
        # Place of publication (210$a)
2038
        if ( $host_field = $marc_host->field('210') ) {
2062
        if ( $host_field = $marc_host->field('210') ) {
2039
            my $s = $host_field->as_string('a');
2063
            my $s = $host_field->as_string('a');
2040
            if ($s) {
2064
            push @sfd, ( 'c' => $s ) if $s;
2041
                push @sfd, ( c => $s );
2042
            }
2043
        }
2065
        }
2044
2066
2045
        # Date of publication
2067
        # Date of publication (210$d)
2046
        if ( $host_field = $marc_host->field('210') ) {
2068
        if ( $host_field = $marc_host->field('210') ) {
2047
            my $s = $host_field->as_string('d');
2069
            my $s = $host_field->as_string('d');
2048
            if ($s) {
2070
            push @sfd, ( 'd' => $s ) if $s;
2049
                push @sfd, ( d => $s );
2050
            }
2051
        }
2071
        }
2052
2072
2053
        # Edition statement
2073
        # Edition statement (205)
2054
        if ( $host_field = $marc_host->field('205') ) {
2074
        if ( $host_field = $marc_host->field('205') ) {
2055
            my $s = $host_field->as_string();
2075
            my $s = $host_field->as_string;
2056
            if ($s) {
2076
            push @sfd, ( 'e' => $s ) if $s;
2057
                push @sfd, ( e => $s );
2058
            }
2059
        }
2077
        }
2060
2078
2061
        #URL
2079
        # URL (856$u)
2062
        if ( $host_field = $marc_host->field('856') ) {
2080
        if ( $host_field = $marc_host->field('856') ) {
2063
            my $s = $host_field->as_string('u');
2081
            my $s = $host_field->as_string('u');
2064
            if ($s) {
2082
            push @sfd, ( u => $s ) if ($s);
2065
                push @sfd, ( u => $s );
2066
            }
2067
        }
2083
        }
2068
2084
2069
        # ISSN
2085
        # ISSN (011$a)
2070
        if ( $host_field = $marc_host->field('011') ) {
2086
        if ( $host_field = $marc_host->field('011') ) {
2071
            my $s = $host_field->as_string('a');
2087
            my $s = $host_field->as_string('a');
2072
            if ($s) {
2088
            push @sfd, ( x => $s ) if ($s);
2073
                push @sfd, ( x => $s );
2074
            }
2075
        }
2089
        }
2076
2090
2077
        # ISBN
2091
        # ISBN (010$a)
2078
        if ( $host_field = $marc_host->field('010') ) {
2092
        if ( $host_field = $marc_host->field('010') ) {
2079
            my $s = $host_field->as_string('a');
2093
            my $s = $host_field->as_string('a');
2080
            if ($s) {
2094
            push @sfd, ( y => $s ) if ($s);
2081
                push @sfd, ( y => $s );
2082
            }
2083
        }
2095
        }
2084
        if ( $host_field = $marc_host->field('001') ) {
2085
2096
2097
        # Control number (001)
2098
        if ( $host_field = $marc_host->field('001') ) {
2086
            push @sfd, ( 0 => $host_field->data() );
2099
            push @sfd, ( 0 => $host_field->data() );
2087
        }
2100
        }
2101
2102
        # Construct 461 link field
2088
        $link_field = MARC::Field->new( 461, '0', ' ', @sfd );
2103
        $link_field = MARC::Field->new( 461, '0', ' ', @sfd );
2089
    }
2104
    }
2090
2105
(-)a/t/db_dependent/Koha/Biblio.t (-49 / +154 lines)
Lines 999-1044 subtest 'get_volumes_query' => sub { Link Here
999
};
999
};
1000
1000
1001
subtest 'generate_marc_host_field' => sub {
1001
subtest 'generate_marc_host_field' => sub {
1002
    plan tests => 24;
1002
    plan tests => 35;
1003
1003
1004
    $schema->storage->txn_begin;
1004
    $schema->storage->txn_begin;
1005
1005
1006
    # Set up MARC21 tests
1006
    t::lib::Mocks::mock_preference( 'marcflavour', 'MARC21' );
1007
    t::lib::Mocks::mock_preference( 'marcflavour', 'MARC21' );
1007
1008
1008
    my $biblio = $builder->build_sample_biblio();
1009
    # 1. Complete MARC21 record test
1009
    my $record = $biblio->metadata->record;
1010
    my $record = MARC::Record->new();
1011
    $record->leader('00000nam a22000007a 4500');
1010
    $record->append_fields(
1012
    $record->append_fields(
1011
        MARC::Field->new( '001', '1234' ),
1013
        MARC::Field->new( '001', '12345' ),
1012
        MARC::Field->new( '003', 'FIRST' ),
1014
        MARC::Field->new( '003', 'NB' ),
1013
        MARC::Field->new( '240', '', '', a => 'A uniform title' ),
1015
        MARC::Field->new( '020', '',  '',  'a' => '978-3-16-148410-0' ),
1014
        MARC::Field->new( '260', '', '', a => 'Publication 260' ),
1016
        MARC::Field->new( '022', '',  '',  'a' => '1234-5678' ),
1015
        MARC::Field->new( '250', '', '', a => 'Edition a', b => 'Edition b' ),
1017
        MARC::Field->new( '100', '1', '',  'a' => 'Smith, John',  'e' => 'author',   '9' => 'xyz', '4' => 'aut' ),
1016
        MARC::Field->new( '022', '', '', a => '0317-8471' ),
1018
        MARC::Field->new( '245', '1', '0', 'a' => 'The Title',    'b' => 'Subtitle', 'c' => 'John Smith' ),
1019
        MARC::Field->new( '250', '',  '',  'a' => '2nd edition',  'b' => 'revised' ),
1020
        MARC::Field->new( '260', '',  '',  'a' => 'New York',     'b' => 'Publisher', 'c' => '2023' ),
1021
        MARC::Field->new( '830', '',  '',  'a' => 'Series Title', 'v' => 'vol. 2',    'x' => '2345-6789' )
1017
    );
1022
    );
1018
    C4::Biblio::ModBiblio( $record, $biblio->biblionumber );
1023
    my ($biblio_id) = AddBiblio( $record, qw{} );
1019
    $biblio = Koha::Biblios->find( $biblio->biblionumber );
1024
    my $biblio = Koha::Biblios->find($biblio_id);
1020
1025
1021
    t::lib::Mocks::mock_preference( 'UseControlNumber', '0' );
1026
    # Test MARC21 with UseControlNumber off
1027
    t::lib::Mocks::mock_preference( 'UseControlNumber', 0 );
1022
    my $link = $biblio->generate_marc_host_field();
1028
    my $link = $biblio->generate_marc_host_field();
1023
1029
1024
    is( ref($link),           'MARC::Field',         "->generate_marc_host_field returns a MARC::Field object" );
1030
    # Test standard MARC21 field
1025
    is( $link->tag,           '773',                 "MARC::Field->tag returns '773' when marcflavour is 'MARC21" );
1031
    is( ref($link),          'MARC::Field', 'Returns a MARC::Field object' );
1026
    is( $link->subfield('a'), 'Some boring author',  'MARC::Field->subfield(a) returns content from 100ab' );
1032
    is( $link->tag(),        '773',         'Field tag is 773 for MARC21' );
1027
    is( $link->subfield('b'), 'Edition a Edition b', 'MARC::Field->subfield(b) returns content from 250ab' );
1033
    is( $link->indicator(1), '0',           'First indicator is 0' );
1028
    is( $link->subfield('d'), 'Publication 260',     'MARC::Field->subfield(c) returns content from 260abc' );
1034
    is( $link->indicator(2), ' ',           'Second indicator is blank' );
1029
    is( $link->subfield('s'), 'A uniform title',     'MARC::Field->subfield(s) returns content from 240a' );
1035
1030
    is( $link->subfield('t'), 'Some boring read',    'MARC::Field->subfield(s) returns content from 245ab' );
1036
    # Check all subfields
1031
    is( $link->subfield('x'), '0317-8471',           'MARC::Field->subfield(s) returns content from 022a' );
1037
    is( $link->subfield('7'), 'p1am',        'Subfield 7 correctly formed' );
1032
    is( $link->subfield('z'), undef,                 'MARC::Field->subfield(s) returns undef when 020a is empty' );
1038
    is( $link->subfield('a'), 'Smith, John', 'Subfield a contains author from 100a' );
1033
    is( $link->subfield('w'), undef, 'MARC::Field->subfield(w) returns undef when "UseControlNumber" is disabled' );
1039
    is(
1040
        $link->subfield('t'), 'The Title Subtitle',
1041
        'Subfield t contains title without trailing punctuation from 245ab'
1042
    );
1043
    is( $link->subfield('b'), '2nd edition revised',     'Subfield b contains edition info from 250ab' );
1044
    is( $link->subfield('d'), 'New York Publisher 2023', 'Subfield d contains publication info from 260abc' );
1045
    is( $link->subfield('k'), 'Series Title, ISSN 2345-6789 ; vol. 2', 'Subfield k contains series info from 830' );
1046
    is( $link->subfield('x'), '1234-5678',                             'Subfield x contains ISSN from 022a' );
1047
    is( $link->subfield('z'), '978-3-16-148410-0',                     'Subfield z contains ISBN from 020a' );
1048
    is( $link->subfield('w'), undef, 'Subfield w is undefined when UseControlNumber is disabled' );
1034
1049
1050
    # Test with UseControlNumber enabled
1035
    t::lib::Mocks::mock_preference( 'UseControlNumber', '1' );
1051
    t::lib::Mocks::mock_preference( 'UseControlNumber', '1' );
1036
    $link = $biblio->generate_marc_host_field();
1052
    $link = $biblio->generate_marc_host_field();
1037
    is(
1053
    is(
1038
        $link->subfield('w'), '(FIRST)1234',
1054
        $link->subfield('w'), '(NB)12345',
1039
        'MARC::Field->subfield(w) returns content from 003 and 001 when "UseControlNumber" is enabled'
1055
        'Subfield w contains control number with source when UseControlNumber is enabled'
1040
    );
1056
    );
1041
1057
1058
    # 245 punctuation handling tests
1059
    # Trailing slash
1060
    $record->field('245')->update( a => 'A title /', b => '', c => '', 'ind2' => '0' );
1061
    ($biblio_id) = AddBiblio( $record, qw{} );
1062
    $biblio = Koha::Biblios->find($biblio_id);
1063
    $link   = $biblio->generate_marc_host_field();
1064
    is( $link->subfield('t'), 'A title', "Trailing slash is removed from 245a" );
1065
1066
    # Trailing period
1067
    $record->field('245')->update( a => 'Another title.', 'ind2' => '0' );
1068
    ($biblio_id) = AddBiblio( $record, qw{} );
1069
    $biblio = Koha::Biblios->find($biblio_id);
1070
    $link   = $biblio->generate_marc_host_field();
1071
    is( $link->subfield('t'), 'Another title', "Trailing period is removed from 245a" );
1072
1073
    # Offset from indicator 2 = 4
1074
    $record->field('245')->update( a => 'The offset title', 'ind2' => '4' );
1075
    ($biblio_id) = AddBiblio( $record, qw{} );
1076
    $biblio = Koha::Biblios->find($biblio_id);
1077
    $link   = $biblio->generate_marc_host_field();
1078
    is( $link->subfield('t'), 'Offset title', "Title offset applied from indicator 2" );
1079
1080
    # Capitalization after offset
1081
    $record->field('245')->update( a => 'the capital test', 'ind2' => '0' );
1082
    ($biblio_id) = AddBiblio( $record, qw{} );
1083
    $biblio = Koha::Biblios->find($biblio_id);
1084
    $link   = $biblio->generate_marc_host_field();
1085
    is( $link->subfield('t'), 'The capital test', "Title is capitalized after indicator offset" );
1086
1087
    # 260/264 handling tests
1042
    $record->append_fields(
1088
    $record->append_fields(
1043
        MARC::Field->new( '264', '', '', a => 'Publication 264' ),
1089
        MARC::Field->new( '264', '', '', a => 'Publication 264' ),
1044
    );
1090
    );
Lines 1063-1098 subtest 'generate_marc_host_field' => sub { Link Here
1063
        'MARC::Field->subfield(d) returns content from 264 with indicator 1 = 3 in prefernce to 264 without'
1109
        'MARC::Field->subfield(d) returns content from 264 with indicator 1 = 3 in prefernce to 264 without'
1064
    );
1110
    );
1065
1111
1066
    # UNIMARC tests
1112
    # 2. Test MARC21 with corporate author (110)
1067
    t::lib::Mocks::mock_preference( 'marcflavour', 'UNIMARC' );
1113
    my $record_corporate = MARC::Record->new();
1114
    $record_corporate->leader('00000nam a22000007a 4500');
1115
    $record_corporate->append_fields(
1116
        MARC::Field->new( '110', '2', '',  'a' => 'Corporate Author', 'e' => 'sponsor', '9' => 'xyz', '4' => 'spn' ),
1117
        MARC::Field->new( '245', '1', '0', 'a' => 'The Title' )
1118
    );
1119
    ($biblio_id) = AddBiblio( $record_corporate, qw{} );
1120
    $biblio = Koha::Biblios->find($biblio_id);
1068
1121
1069
    $biblio = $builder->build_sample_biblio();
1122
    $link = $biblio->generate_marc_host_field();
1070
    $record = $biblio->metadata->record;
1123
    is( $link->subfield('7'), 'c2am',             'Subfield 7 correctly formed for corporate author' );
1071
    $record->append_fields(
1124
    is( $link->subfield('a'), 'Corporate Author', 'Subfield a contains corporate author' );
1072
        MARC::Field->new( '001', '1234' ),
1125
1073
        MARC::Field->new( '700', '', '', a => 'A nice author' ),
1126
    # 3. Test MARC21 with meeting name (111)
1074
        MARC::Field->new( '210', '', '', a => 'A publication', d => 'A date' ),
1127
    my $record_meeting = MARC::Record->new();
1075
        MARC::Field->new( '205', '', '', a => "Fun things" ),
1128
    $record_meeting->leader('00000nam a22000007a 4500');
1076
        MARC::Field->new( '856', '', '', u => 'http://myurl.com/' ),
1129
    $record_meeting->append_fields(
1077
        MARC::Field->new( '011', '', '', a => '0317-8471' ),
1130
        MARC::Field->new( '111', '2', '',  'a' => 'Conference Name', 'j' => 'relator', '9' => 'xyz', '4' => 'spn' ),
1078
        MARC::Field->new( '545', '', '', a => 'Invisible on OPAC' ),
1131
        MARC::Field->new( '245', '1', '0', 'a' => 'The Title' )
1079
    );
1132
    );
1080
    C4::Biblio::ModBiblio( $record, $biblio->biblionumber );
1133
    ($biblio_id) = AddBiblio( $record_meeting, qw{} );
1134
    $biblio = Koha::Biblios->find($biblio_id);
1135
1136
    $link = $biblio->generate_marc_host_field();
1137
    is( $link->subfield('7'), 'm2am', 'Subfield 7 correctly formed for meeting name' );
1138
1139
    # 4. Test MARC21 with minimal record
1140
    my $record_minimal = MARC::Record->new();
1141
    $record_minimal->leader('00000nam a22000007a 4500');
1142
    $record_minimal->append_fields( MARC::Field->new( '245', '0', '0', 'a' => 'Title Only' ) );
1143
    ($biblio_id) = AddBiblio( $record_minimal, qw{} );
1144
    $biblio = Koha::Biblios->find($biblio_id);
1145
1146
    $link = $biblio->generate_marc_host_field();
1147
    is( $link->subfield('7'), 'nnam', 'Subfield 7 correctly formed with no main entry' );
1148
1149
    # 5. Test UNIMARC
1150
    t::lib::Mocks::mock_preference( 'marcflavour', 'UNIMARC' );
1151
    $biblio = $builder->build_sample_biblio();
1152
    my $record_unimarc = MARC::Record->new();
1153
    $record_unimarc->append_fields(
1154
        MARC::Field->new( '001', '54321' ),
1155
        MARC::Field->new( '010', '', '', 'a' => '978-0-12-345678-9' ),
1156
        MARC::Field->new( '011', '', '', 'a' => '2345-6789' ),
1157
        MARC::Field->new( '200', '', '', 'a' => 'UNIMARC Title' ),
1158
        MARC::Field->new( '205', '', '', 'a' => 'Third edition' ),
1159
        MARC::Field->new( '210', '', '', 'a' => 'Paris', 'd' => '2023' ),
1160
        MARC::Field->new( '700', '', '', 'a' => 'Doe',   'b' => 'Jane' ),
1161
        MARC::Field->new( '856', '', '', 'u' => 'http://example.com' )
1162
    );
1163
    ($biblio_id) = AddBiblio( $record_unimarc, qw{} );
1164
    $biblio = Koha::Biblios->find($biblio_id);
1165
1166
    $link = $biblio->generate_marc_host_field();
1167
1168
    is( ref($link),          'MARC::Field', 'Returns a MARC::Field object for UNIMARC' );
1169
    is( $link->tag(),        '461',         'Field tag is 461 for UNIMARC' );
1170
    is( $link->indicator(1), '0',           'First indicator is 0 for UNIMARC' );
1171
    is( $link->indicator(2), ' ',           'Second indicator is blank for UNIMARC' );
1172
1173
    # Check UNIMARC subfields
1174
    is( $link->subfield('a'), 'Doe Jane',      'Subfield a contains author for UNIMARC' );
1175
    is( $link->subfield('t'), 'UNIMARC Title', 'Subfield t contains title for UNIMARC' );
1176
    is( $link->subfield('c'), 'Paris',         'Subfield c contains place of publication for UNIMARC' );
1177
    is( $link->subfield('d'), '2023',          'Subfield d contains date of publication for UNIMARC' );
1178
    is( $link->subfield('0'), '54321',         'Subfield 0 contains control number for UNIMARC' );
1179
1180
    # 6. Test UNIMARC with different author types
1181
    my $record_unimarc_corporate = MARC::Record->new();
1182
    $record_unimarc_corporate->append_fields(
1183
        MARC::Field->new( '710', '', '', 'a' => 'Corporate', 'b' => 'Department' ),
1184
        MARC::Field->new( '200', '', '', 'a' => 'Title' )
1185
    );
1186
    C4::Biblio::ModBiblio( $record_unimarc_corporate, $biblio->biblionumber );
1081
    $biblio = Koha::Biblios->find( $biblio->biblionumber );
1187
    $biblio = Koha::Biblios->find( $biblio->biblionumber );
1082
1188
1083
    $link = $biblio->generate_marc_host_field();
1189
    $link = $biblio->generate_marc_host_field();
1190
    is( $link->subfield('a'), 'Corporate Department', 'Subfield a contains corporate author for UNIMARC' );
1191
1192
    my $record_unimarc_family = MARC::Record->new();
1193
    $record_unimarc_family->append_fields(
1194
        MARC::Field->new( '720', '', '', 'a' => 'Family', 'b' => 'Name' ),
1195
        MARC::Field->new( '200', '', '', 'a' => 'Title' )
1196
    );
1197
    C4::Biblio::ModBiblio( $record_unimarc_family, $biblio->biblionumber );
1198
    $biblio = Koha::Biblios->find( $biblio->biblionumber );
1084
1199
1085
    is( ref($link),           'MARC::Field',       "->generate_marc_host_field returns a MARC::Field object" );
1200
    $link = $biblio->generate_marc_host_field();
1086
    is( $link->tag,           '461',               "MARC::Field->tag returns '461' when marcflavour is 'UNIMARC" );
1201
    is( $link->subfield('a'), 'Family Name', 'Subfield a contains family name for UNIMARC' );
1087
    is( $link->subfield('a'), 'A nice author',     'MARC::Field->subfield(a) returns content from 700ab' );
1088
    is( $link->subfield('c'), 'A publication',     'MARC::Field->subfield(b) returns content from 210a' );
1089
    is( $link->subfield('d'), 'A date',            'MARC::Field->subfield(c) returns content from 210d' );
1090
    is( $link->subfield('e'), 'Fun things',        'MARC::Field->subfield(s) returns content from 205' );
1091
    is( $link->subfield('t'), 'Some boring read',  'MARC::Field->subfield(s) returns content from 200a' );
1092
    is( $link->subfield('u'), 'http://myurl.com/', 'MARC::Field->subfield(s) returns content from 856u' );
1093
    is( $link->subfield('x'), '0317-8471',         'MARC::Field->subfield(s) returns content from 011a' );
1094
    is( $link->subfield('y'), undef,               'MARC::Field->subfield(w) returns undef if 010a is empty' );
1095
    is( $link->subfield('0'), '1234',              'MARC::Field->subfield(0) returns content from 001' );
1096
1202
1097
    $schema->storage->txn_rollback;
1203
    $schema->storage->txn_rollback;
1098
    t::lib::Mocks::mock_preference( 'marcflavour', 'MARC21' );
1204
    t::lib::Mocks::mock_preference( 'marcflavour', 'MARC21' );
1099
- 

Return to bug 39545