3 # Copyright 2020 Koha Development team
5 # This file is part of Koha
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.
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.
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>.
22 use Test::More tests => 11;
24 use Koha::Import::Records;
27 use t::lib::TestBuilder;
29 my $schema = Koha::Database->new->schema;
30 $schema->storage->txn_begin;
32 my $builder = t::lib::TestBuilder->new;
33 my $nb_of_records = Koha::Import::Records->search->count;
35 my $record_1 = $builder->build({ source => 'ImportRecord' });
36 my $record_2 = $builder->build({ source => 'ImportRecord' });
38 is( Koha::Import::Records->search->count, $nb_of_records + 2, 'The 2 records should have been added' );
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' );
43 my $matches = $retrieved_record_1->get_import_record_matches();
44 is( $matches->count, 0, "No matches returned if none set");
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',
53 candidate_match_id => $biblio->biblionumber,
54 import_record_id => $retrieved_record_1->import_record_id
57 my $match_2 = $builder->build_object({ class => 'Koha::Import::Record::Matches',
61 candidate_match_id => $biblio_1->biblionumber,
62 import_record_id => $retrieved_record_1->import_record_id
65 my $match_3 = $builder->build_object({ class => 'Koha::Import::Record::Matches',
69 candidate_match_id => $biblio_2->biblionumber,
70 import_record_id => $retrieved_record_1->import_record_id
74 $matches = $retrieved_record_1->get_import_record_matches();
75 is( $matches->count, 3, 'We get three matches');
77 is_deeply( $matches->next->unblessed, $match_3->unblessed, "Match order is score desc, biblionumber desc, so 3 is first");
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");
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");
86 $retrieved_record_1->delete;
87 is( Koha::Import::Records->search->count, $nb_of_records + 1, 'Delete should have deleted the record' );
89 subtest 'replace' => sub {
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',
98 import_record_id => $import_record->id
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;
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" );
114 my $auth_record = MARC::Record->new;
115 $auth_record->append_fields(
116 MARC::Field->new( '100', '', '', a => 'Author' ),
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',
125 import_record_id => $import_record->id
130 my $field = $auth_record->field('100');
131 $field->update( 'a' => 'Other author' );
132 $import_record->marcxml( $auth_record->as_xml )->store->discard_changes;
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" );
143 $schema->storage->txn_rollback;