Bug 33360: Add Koha::Notice::Util for mail domain limits
[koha.git] / t / db_dependent / Koha / Import / Records.t
1 #!/usr/bin/perl
2
3 # Copyright 2020 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 => 11;
23
24 use Koha::Import::Records;
25 use Koha::Database;
26
27 use t::lib::TestBuilder;
28
29 my $schema = Koha::Database->new->schema;
30 $schema->storage->txn_begin;
31
32 my $builder = t::lib::TestBuilder->new;
33 my $nb_of_records = Koha::Import::Records->search->count;
34
35 my $record_1 = $builder->build({ source => 'ImportRecord' });
36 my $record_2 = $builder->build({ source => 'ImportRecord' });
37
38 is( Koha::Import::Records->search->count, $nb_of_records + 2, 'The 2 records should have been added' );
39
40 my $retrieved_record_1 = Koha::Import::Records->search({ import_record_id => $record_1->{import_record_id}})->next;
41 is_deeply( $retrieved_record_1->unblessed, $record_1, 'Find a record by import record id should return the correct record' );
42
43 my $matches = $retrieved_record_1->get_import_record_matches();
44 is( $matches->count, 0, "No matches returned if none set");
45
46 my $biblio = $builder->build_sample_biblio;
47 my $biblio_1 = $builder->build_sample_biblio;
48 my $biblio_2 = $builder->build_sample_biblio;
49 my $match_1 = $builder->build_object({ class => 'Koha::Import::Record::Matches',
50                   value => {
51                       score => 100,
52                       chosen => 0,
53                       candidate_match_id => $biblio->biblionumber,
54                       import_record_id => $retrieved_record_1->import_record_id
55                   }
56               });
57 my $match_2 = $builder->build_object({ class => 'Koha::Import::Record::Matches',
58                   value => {
59                       score => 50,
60                       chosen => 1,
61                       candidate_match_id => $biblio_1->biblionumber,
62                       import_record_id => $retrieved_record_1->import_record_id
63                   }
64               });
65 my $match_3 = $builder->build_object({ class => 'Koha::Import::Record::Matches',
66                   value => {
67                       score => 100,
68                       chosen => 0,
69                       candidate_match_id => $biblio_2->biblionumber,
70                       import_record_id => $retrieved_record_1->import_record_id
71                   }
72               });
73
74 $matches = $retrieved_record_1->get_import_record_matches();
75 is( $matches->count, 3, 'We get three matches');
76
77 is_deeply( $matches->next->unblessed, $match_3->unblessed, "Match order is score desc, biblionumber desc, so 3 is first");
78
79 is_deeply( $matches->next->unblessed, $match_1->unblessed, "Match order is score desc, biblionumber desc, so 1 is second");
80 is_deeply( $matches->next->unblessed, $match_2->unblessed, "Match order is score desc, biblionumber desc, so 2 is third");
81
82 $matches = $retrieved_record_1->get_import_record_matches({ chosen => 1 });
83 is( $matches->count, 1, 'We get only the chosen match when requesting chosen');
84 is_deeply( $matches->next->unblessed, $match_2->unblessed, "Match 2 is the chosen match");
85
86 $retrieved_record_1->delete;
87 is( Koha::Import::Records->search->count, $nb_of_records + 1, 'Delete should have deleted the record' );
88
89 subtest 'replace' => sub {
90     plan tests => 4;
91
92
93     # Replace biblio
94     my $import_record = $builder->build_object({ class => 'Koha::Import::Records' });
95     my $import_record_biblio = $builder->build_object({
96         class => 'Koha::Import::Record::Biblios',
97         value => {
98             import_record_id => $import_record->id
99         }
100     });
101
102     my $koha_biblio = $builder->build_sample_biblio({ title => "The before" });
103     my $koha_xml = $koha_biblio->metadata->record->as_xml;
104     my $import_biblio = $builder->build_sample_biblio({ title => "The after" });
105     $import_record->marcxml( $import_biblio->metadata->record->as_xml )->store->discard_changes;
106
107     $import_record->replace({ biblio => $koha_biblio });
108     $koha_biblio->discard_changes;
109     $import_record->discard_changes;
110     is( $koha_biblio->title, "The after", "The Koha biblio is successfully updated" );
111     is( $import_record->marcxml_old, $koha_xml, "The old marcxml in import records is correctly updated" );
112
113     # Replace authority
114     my $auth_record = MARC::Record->new;
115     $auth_record->append_fields(
116         MARC::Field->new( '100', '', '', a => 'Author' ),
117     );
118     my $auth_id = C4::AuthoritiesMarc::AddAuthority($auth_record, undef, 'PERSO_NAME');
119     my $koha_auth = Koha::Authorities->find( $auth_id );
120     $koha_xml = $koha_auth->marcxml;
121     $import_record = $builder->build_object({ class => 'Koha::Import::Records' });
122     my $import_record_auth = $builder->build_object({
123         class => 'Koha::Import::Record::Auths',
124         value => {
125             import_record_id => $import_record->id
126         }
127     });
128
129
130     my $field = $auth_record->field('100');
131     $field->update( 'a' => 'Other author' );
132     $import_record->marcxml( $auth_record->as_xml )->store->discard_changes;
133
134     $import_record->replace({ authority => $koha_auth });
135     $koha_auth->discard_changes;
136     $import_record->discard_changes;
137     my $updated_record = MARC::Record->new_from_xml( $koha_auth->marcxml, 'UTF-8');
138     is( $updated_record->field('100')->as_string, $auth_record->field('100')->as_string, "The Koha auhtority record is correctly updated" );
139     is( $import_record->marcxml_old, $koha_xml, "The old marcxml in import record is correctly updated" );
140
141 };
142
143 $schema->storage->txn_rollback;