Bug 30756: Further adjustments to Koha_Authority.t

Remove use of exported_records file in favor of TestBuilder.
Remove conditional tests with SKIP block. Create reservoir record.

Test plan:
Run t/db_dependent/Koha_Authority.t

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>

Signed-off-by: David Nind <david@davidnind.com>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
(cherry picked from commit 24ff8b4a85)

Signed-off-by: Lucas Gass <lucas@bywatersolutions.com>
This commit is contained in:
Marcel de Rooy 2022-05-14 12:07:17 +00:00 committed by Lucas Gass
parent de503196c4
commit f70b564a18
2 changed files with 43 additions and 57 deletions

View file

@ -1,5 +1,6 @@
#!/usr/bin/perl #!/usr/bin/perl
# Copyright 2022 Koha Development Team, Marcel de Rooy
# Copyright 2012 C & P Bibliography Services # Copyright 2012 C & P Bibliography Services
# #
# This file is part of Koha. # This file is part of Koha.
@ -19,15 +20,12 @@
use Modern::Perl; use Modern::Perl;
use Test::More tests => 3; use Test::More tests => 3;
use Data::Dumper qw/Dumper/;
use File::Basename; use MARC::File::XML;
use MARC::Batch;
use MARC::File; use t::lib::TestBuilder;
use IO::File;
use C4::Context;
use C4::Charset qw( MarcToUTF8Record );
use C4::AuthoritiesMarc qw( AddAuthority );
use Koha::Database; use Koha::Database;
use Koha::Authorities; use Koha::Authorities;
@ -37,71 +35,60 @@ BEGIN {
our $schema = Koha::Database->new->schema; our $schema = Koha::Database->new->schema;
$schema->storage->txn_begin; $schema->storage->txn_begin;
our $dbh = C4::Context->dbh; our $builder = t::lib::TestBuilder->new;
subtest 'Part 1' => sub { our $record1 = MARC::Record->new;
# TODO Move this part to a t/lib packages $record1->add_fields(
my $sourcedir = dirname(__FILE__) . "/data"; [ '001', '1234' ],
my $input_marc_file = "$sourcedir/marc21/zebraexport/authority/exported_records"; [ '150', ' ', ' ', a => 'Cooking' ],
[ '450', ' ', ' ', a => 'Cookery' ],
);
our $record2 = MARC::Record->new;
$record2->add_fields(
[ '001', '2345' ],
[ '150', ' ', ' ', a => 'Baking' ],
[ '450', ' ', ' ', a => 'Bakery' ],
);
my $fh = IO::File->new($input_marc_file); subtest 'Test new, authorized_heading, authid, get_from_authid' => sub {
my $batch = MARC::Batch->new( 'USMARC', $fh ); plan tests => 7;
while ( my $record = $batch->next ) {
C4::Charset::MarcToUTF8Record($record, 'MARC21');
AddAuthority($record, '', '');
}
my $record = MARC::Record->new; my $auth1 = $builder->build_object({ class => 'Koha::Authorities',
$record->add_fields( value => { marcxml => $record1->as_xml },
[ '001', '1234' ], });
[ '150', ' ', ' ', a => 'Cooking' ], my $auth2 = $builder->build_object({ class => 'Koha::Authorities',
[ '450', ' ', ' ', a => 'Cookery' ], value => { marcxml => $record2->as_xml },
); });
my $authority = Koha::MetadataRecord::Authority->new($record);
my $authority = Koha::MetadataRecord::Authority->new( $record1 );
is(ref($authority), 'Koha::MetadataRecord::Authority', 'Created valid Koha::MetadataRecord::Authority object'); is(ref($authority), 'Koha::MetadataRecord::Authority', 'Created valid Koha::MetadataRecord::Authority object');
is($authority->authorized_heading(), 'Cooking', 'Authorized heading was correct'); is($authority->authorized_heading(), 'Cooking', 'Authorized heading was correct');
is_deeply($authority->record, $record1, 'Saved record');
is_deeply($authority->record, $record, 'Saved record'); $authority = Koha::MetadataRecord::Authority->get_from_authid( $auth2->id );
my $authid = Koha::Authorities->search->next->authid;
$authority = Koha::MetadataRecord::Authority->get_from_authid($authid);
is(ref($authority), 'Koha::MetadataRecord::Authority', 'Retrieved valid Koha::MetadataRecord::Authority object'); is(ref($authority), 'Koha::MetadataRecord::Authority', 'Retrieved valid Koha::MetadataRecord::Authority object');
is($authority->authid, $auth2->id, 'Object authid is correct');
is($authority->authid, $authid, 'Object authid is correct'); is($authority->record->field('001')->data(), '2345', 'Retrieved original 001'); # Note: not created via AddAuthority
is($authority->record->field('001')->data(), $authid, 'Retrieved correct record');
$authority = Koha::MetadataRecord::Authority->get_from_authid('alphabetsoup'); $authority = Koha::MetadataRecord::Authority->get_from_authid('alphabetsoup');
is($authority, undef, 'No invalid record is retrieved'); is($authority, undef, 'No invalid record is retrieved');
}; };
subtest 'Part2' => sub { subtest 'Test get_from_breeding' => sub {
SKIP: { plan tests => 4;
my $sth = $dbh->prepare("SELECT import_record_id FROM import_records WHERE record_type = 'auth' LIMIT 1;");
$sth->execute();
my $import_record_id; my $import = $builder->build({ source => 'ImportRecord',
for my $row ($sth->fetchrow_hashref) { value => { marcxml => $record1->as_xml, record_type => 'auth' },
$import_record_id = $row->{'import_record_id'}; });
} my $import_record_id = $import->{import_record_id};
skip 'No authorities in reservoir', 3 unless $import_record_id; my $authority = Koha::MetadataRecord::Authority->get_from_breeding($import_record_id);
my $authority = Koha::MetadataRecord::Authority->get_from_breeding($import_record_id); is(ref($authority), 'Koha::MetadataRecord::Authority', 'Retrieved valid Koha::MetadataRecord::Authority object');
is($authority->authid, undef, 'Records in reservoir do not have an authid');
is(ref($authority->record), 'MARC::Record', 'MARC record attached to authority');
is(ref($authority), 'Koha::MetadataRecord::Authority', 'Retrieved valid Koha::MetadataRecord::Authority object'); $authority = Koha::MetadataRecord::Authority->get_from_breeding('alphabetsoup');
is($authority, undef, 'No invalid record is retrieved from reservoir');
is($authority->authid, undef, 'Records in reservoir do not have an authid');
is(ref($authority->record), 'MARC::Record', 'MARC record attached to authority');
$authority = Koha::MetadataRecord::Authority->get_from_breeding('alphabetsoup');
is($authority, undef, 'No invalid record is retrieved from reservoir');
}
done_testing();
}; };
$schema->storage->txn_rollback; $schema->storage->txn_rollback;

File diff suppressed because one or more lines are too long