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

(-)a/t/db_dependent/Authorities/Merge.t (-1 / +99 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# Tests for C4::AuthoritiesMarc::merge
4
5
use Modern::Perl;
6
7
use Test::More tests => 2;
8
9
use MARC::Record;
10
use Test::MockModule;
11
use Test::MockObject;
12
13
use C4::Biblio;
14
use Koha::Database;
15
16
BEGIN {
17
        use_ok('C4::AuthoritiesMarc');
18
}
19
20
my $schema  = Koha::Database->new->schema;
21
$schema->storage->txn_begin;
22
my $dbh = C4::Context->dbh;
23
24
# Some advanced mocking :)
25
my ( @zebrarecords, $index );
26
my $auth_mod = Test::MockModule->new( 'C4::AuthoritiesMarc' );
27
my $context_mod = Test::MockModule->new( 'C4::Context' );
28
my $search_mod = Test::MockModule->new( 'C4::Search' );
29
my $zoom_mod = Test::MockModule->new( 'ZOOM::Query::CCL2RPN', no_auto => 1 );
30
my $conn_obj = Test::MockObject->new;
31
my $zoom_obj = Test::MockObject->new;
32
my $zoom_record_obj = Test::MockObject->new;
33
set_mocks();
34
35
subtest 'Test merge A1 to A2 (withing same authtype)' => sub {
36
# Tests originate from bug 11700
37
    plan tests => 5;
38
39
    # Create authority type TEST_PERSO
40
    $dbh->do("INSERT INTO auth_types(authtypecode, authtypetext, auth_tag_to_report, summary) VALUES('TEST_PERSO', 'Personal Name', '109', 'Personal Names');");
41
    $dbh->do("INSERT INTO auth_tag_structure (authtypecode, tagfield, liblibrarian, libopac, repeatable, mandatory, authorised_value) VALUES('TEST_PERSO', '109', 'HEADING--PERSONAL NAME', 'HEADING--PERSONAL NAME', 0, 0, NULL)");
42
    $dbh->do("INSERT INTO auth_subfield_structure (authtypecode, tagfield, tagsubfield, liblibrarian, libopac, repeatable, mandatory, tab, authorised_value, value_builder, seealso, isurl, hidden, linkid, kohafield, frameworkcode) VALUES ('TEST_PERSO', '109', 'a', 'Personal name', 'Personal name', 0, 0, 1, NULL, NULL, '', 0, 0, '', '', '')");
43
44
    my $auth1 = new MARC::Record;
45
    $auth1->append_fields(new MARC::Field('109', '0', '0', 'a' => 'George Orwell'));
46
    my $authid1 = AddAuthority($auth1, undef, 'TEST_PERSO');
47
    my $auth2 = new MARC::Record;
48
    $auth2->append_fields(new MARC::Field('109', '0', '0', 'a' => 'G. Orwell'));
49
    my $authid2 = AddAuthority($auth2, undef, 'TEST_PERSO');
50
51
    $dbh->do("INSERT IGNORE INTO marc_subfield_structure(tagfield, tagsubfield, liblibrarian, libopac, repeatable, mandatory, kohafield, tab, authorised_value, authtypecode, value_builder, isurl, hidden, frameworkcode, seealso, link, defaultvalue) VALUES('609', 'a', 'Personal name', 'Personal name', 0, 0, '', 6, '', 'TEST_PERSO', '', NULL, 0, '', '', '', NULL)");
52
    $dbh->do("UPDATE marc_subfield_structure SET authtypecode = 'TEST_PERSO' WHERE tagfield='609' AND tagsubfield='a' AND frameworkcode='';");
53
    my $tagfields = $dbh->selectcol_arrayref("select distinct tagfield from marc_subfield_structure where authtypecode='TEST_PERSO'");
54
    my $biblio1 = new MARC::Record;
55
    $biblio1->append_fields(
56
        new MARC::Field('609', '0', '0', '9' => $authid1, 'a' => 'George Orwell')
57
    );
58
    my ( $biblionumber1 ) = AddBiblio($biblio1, '');
59
    my $biblio2 = new MARC::Record;
60
    $biblio2->append_fields(
61
        new MARC::Field('609', '0', '0', '9' => $authid2, 'a' => 'G. Orwell')
62
    );
63
    my ( $biblionumber2 ) = AddBiblio($biblio2, '');
64
65
    @zebrarecords = ( $biblio1, $biblio2 );
66
    $index = 0;
67
    my $rv = C4::AuthoritiesMarc::merge( $authid2, $auth2, $authid1, $auth1 );
68
    is( $rv, 1, 'We expect one biblio record (out of two) to be updated' );
69
70
    $biblio1 = GetMarcBiblio($biblionumber1);
71
    is($biblio1->subfield('609', '9'), $authid1, 'Check biblio1 609$9' );
72
    is($biblio1->subfield('609', 'a'), 'George Orwell',
73
        'Check biblio1 609$a' );
74
    $biblio2 = GetMarcBiblio($biblionumber2);
75
    is($biblio2->subfield('609', '9'), $authid1, 'Check biblio2 609$9' );
76
    is($biblio2->subfield('609', 'a'), 'George Orwell',
77
        'Check biblio2 609$a' );
78
};
79
80
sub set_mocks {
81
    # Mock ZOOM objects: They do nothing actually
82
    # Get new_record_from_zebra to return the records
83
84
    $context_mod->mock( 'Zconn', sub { $conn_obj; } );
85
    $search_mod->mock( 'new_record_from_zebra', sub {
86
         return if $index >= @zebrarecords;
87
         return $zebrarecords[ $index++ ];
88
    });
89
    $zoom_mod->mock( 'new', sub {} );
90
91
    $conn_obj->mock( 'search', sub { $zoom_obj; } );
92
    $zoom_obj->mock( 'destroy', sub {} );
93
    $zoom_obj->mock( 'record', sub { $zoom_record_obj; } );
94
    $zoom_obj->mock( 'search', sub {} );
95
    $zoom_obj->mock( 'size', sub { @zebrarecords } );
96
    $zoom_record_obj->mock( 'raw', sub {} );
97
}
98
99
$schema->storage->txn_rollback;

Return to bug 17909