Bug 21798: Fix another test
[koha.git] / t / db_dependent / Koha_Authority.t
index dc57774..300b6a8 100755 (executable)
@@ -4,63 +4,99 @@
 #
 # This file is part of Koha.
 #
-# Koha is free software; you can redistribute it and/or modify it under the
-# terms of the GNU General Public License as published by the Free Software
-# Foundation; either version 2 of the License, or (at your option) any later
-# version.
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
 #
-# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
-# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
 #
-# You should have received a copy of the GNU General Public License along
-# with Koha; if not, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
 
-use strict;
-use warnings;
+use Modern::Perl;
 
 use C4::Context;
+use C4::Charset;
+use C4::AuthoritiesMarc;
+use Koha::Database;
 use Test::More;
+use File::Basename;
+use MARC::Batch;
+use MARC::File;
+use IO::File;
+use Koha::Authorities;
 
 BEGIN {
-        use_ok('Koha::Authority');
+    use_ok('Koha::MetadataRecord::Authority');
 }
 
-my $record = MARC::Record->new;
+my $schema = Koha::Database->new->schema;
+$schema->storage->txn_begin;
+
+# TODO Move this part to a t/lib packages
+my $sourcedir = dirname(__FILE__) . "/data";
+my $input_marc_file = "$sourcedir/marc21/zebraexport/authority/exported_records";
+
+my $fh = IO::File->new($input_marc_file);
+my $batch = MARC::Batch->new( 'USMARC', $fh );
+while ( my $record = $batch->next ) {
+    C4::Charset::MarcToUTF8Record($record, 'MARC21');
+    AddAuthority($record, '', '');
+}
 
+my $record = MARC::Record->new;
 $record->add_fields(
         [ '001', '1234' ],
         [ '150', ' ', ' ', a => 'Cooking' ],
         [ '450', ' ', ' ', a => 'Cookery' ],
         );
-my $authority = Koha::Authority->new($record);
+my $authority = Koha::MetadataRecord::Authority->new($record);
+
+is(ref($authority), 'Koha::MetadataRecord::Authority', 'Created valid Koha::MetadataRecord::Authority object');
 
-is(ref($authority), 'Koha::Authority', 'Created valid Koha::Authority object');
+is($authority->authorized_heading(), 'Cooking', 'Authorized heading was correct');
 
 is_deeply($authority->record, $record, 'Saved record');
 
+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($authority->authid, $authid, 'Object authid is correct');
+
+is($authority->record->field('001')->data(), $authid, 'Retrieved correct record');
+
+$authority = Koha::MetadataRecord::Authority->get_from_authid('alphabetsoup');
+is($authority, undef, 'No invalid record is retrieved');
+
 SKIP:
 {
     my $dbh = C4::Context->dbh;
-    my $sth = $dbh->prepare("SELECT authid FROM auth_header LIMIT 1;");
+    my $sth = $dbh->prepare("SELECT import_record_id FROM import_records WHERE record_type = 'auth' LIMIT 1;");
     $sth->execute();
 
-    my $authid;
+    my $import_record_id;
     for my $row ($sth->fetchrow_hashref) {
-        $authid = $row->{'authid'};
+        $import_record_id = $row->{'import_record_id'};
     }
-    skip 'No authorities', 3 unless $authid;
-    $authority = Koha::Authority->get_from_authid($authid);
 
-    is(ref($authority), 'Koha::Authority', 'Retrieved valid Koha::Authority object');
+    skip 'No authorities in reservoir', 3 unless $import_record_id;
+    $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, $authid, 'Object authid is correct');
+    is($authority->authid, undef, 'Records in reservoir do not have an authid');
 
-    is($authority->record->field('001')->data(), $authid, 'Retrieved correct record');
+    is(ref($authority->record), 'MARC::Record', 'MARC record attached to authority');
 
-    $authority = Koha::Authority->get_from_authid('alphabetsoup');
-    is($authority, undef, 'No invalid record is retrieved');
+    $authority = Koha::MetadataRecord::Authority->get_from_breeding('alphabetsoup');
+    is($authority, undef, 'No invalid record is retrieved from reservoir');
 }
 
 done_testing();