]> git.koha-community.org Git - koha.git/blob - t/db_dependent/Koha/Authorities.t
Bug 9988: Add Koha objects for table need_merge_authorities
[koha.git] / t / db_dependent / Koha / Authorities.t
1 #!/usr/bin/perl
2
3 # Copyright 2015 Koha Development team
4 #
5 # This file is part of Koha
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use Test::More tests => 5;
23 use MARC::Field;
24 use MARC::File::XML;
25 use MARC::Record;
26 use Test::Deep;
27
28 use Koha::Authority;
29 use Koha::Authorities;
30 use Koha::Authority::MergeRequest;
31 use Koha::Authority::MergeRequests;
32 use Koha::Authority::Type;
33 use Koha::Authority::Types;
34 use Koha::Database;
35
36 use t::lib::TestBuilder;
37
38 my $schema = Koha::Database->new->schema;
39 $schema->storage->txn_begin;
40
41 my $builder               = t::lib::TestBuilder->new;
42 my $nb_of_authorities     = Koha::Authorities->search->count;
43 my $nb_of_authority_types = Koha::Authority::Types->search->count;
44 my $new_authority_type_1  = Koha::Authority::Type->new(
45     {   authtypecode       => 'my_ac_1',
46         authtypetext       => 'my authority type text 1',
47         auth_tag_to_report => '100',
48         summary            => 'my summary for authority 1',
49     }
50 )->store;
51 my $new_authority_1 = Koha::Authority->new( { authtypecode => $new_authority_type_1->authtypecode, } )->store;
52 my $new_authority_2 = Koha::Authority->new( { authtypecode => $new_authority_type_1->authtypecode, } )->store;
53
54 is( Koha::Authority::Types->search->count, $nb_of_authority_types + 1, 'The authority type should have been added' );
55 is( Koha::Authorities->search->count,      $nb_of_authorities + 2,     'The 2 authorities should have been added' );
56
57 $new_authority_1->delete;
58 is( Koha::Authorities->search->count, $nb_of_authorities + 1, 'Delete should have deleted the authority' );
59
60 subtest 'New merge request, method oldmarc' => sub {
61     plan tests => 4;
62
63     my $marc = MARC::Record->new;
64     $marc->append_fields(
65         MARC::Field->new( '100', '', '', a => 'a', b => 'b_findme' ),
66         MARC::Field->new( '200', '', '', a => 'aa' ),
67     );
68     my $req = Koha::Authority::MergeRequest->new({
69         authid => $new_authority_2->authid,
70         reportxml => 'Should be discarded',
71     });
72     is( $req->reportxml, undef, 'Reportxml is undef without oldrecord' );
73
74     $req = Koha::Authority::MergeRequest->new({
75         authid => $new_authority_2->authid,
76         oldrecord => $marc,
77     });
78     like( $req->reportxml, qr/b_findme/, 'Reportxml initialized' );
79
80     # Check if oldmarc is a MARC::Record and has one field
81     is( ref( $req->oldmarc ), 'MARC::Record', 'Check oldmarc method' );
82     is( scalar $req->oldmarc->fields, 1, 'Contains one field' );
83 };
84
85 subtest 'Testing reporting_tag_xml in MergeRequests' => sub {
86     plan tests => 2;
87
88     my $record = MARC::Record->new;
89     $record->append_fields(
90         MARC::Field->new( '024', '', '', a => 'aaa' ),
91         MARC::Field->new( '100', '', '', a => 'Best author' ),
92         MARC::Field->new( '234', '', '', a => 'Just a field' ),
93     );
94     my $xml = Koha::Authority::MergeRequests->reporting_tag_xml({
95         record => $record, tag => '110',
96     });
97     is( $xml, undef, 'Expected no result for wrong tag' );
98     $xml = Koha::Authority::MergeRequests->reporting_tag_xml({
99         record => $record, tag => '100',
100     });
101     my $newrecord = MARC::Record->new_from_xml(
102         $xml, 'UTF-8',
103         C4::Context->preference('marcflavour') eq 'UNIMARC' ?
104         'UNIMARCAUTH' :
105         'MARC21',
106     ); # MARC format does not actually matter here
107     cmp_deeply( $record->field('100')->subfields,
108         $newrecord->field('100')->subfields,
109         'Compare reporting tag in both records',
110     );
111 };
112
113 $schema->storage->txn_rollback;