Bug 15496: (QA follow-up) Fix new uses of Koha::Biblio::items in list context
[koha-equinox.git] / t / db_dependent / Koha / Biblio.t
old mode 100755 (executable)
new mode 100644 (file)
index b247d8b..18b3bf2
@@ -1,7 +1,5 @@
 #!/usr/bin/perl
-#
-# Copyright 2014 Catalyst IT
-#
+
 # This file is part of Koha.
 #
 # Koha is free software; you can redistribute it and/or modify it
 
 use Modern::Perl;
 
-use C4::Context;
-use C4::Biblio qw( AddBiblio );
+use Test::More tests => 5;
+
+use t::lib::TestBuilder;
+
+use C4::Biblio;
 use Koha::Database;
-use Koha::Libraries;
-use Koha::Patrons;
 
-use Test::More tests => 4;
+BEGIN {
+    use_ok('Koha::Biblio');
+    use_ok('Koha::Biblios');
+}
+
+my $schema  = Koha::Database->new->schema;
+my $builder = t::lib::TestBuilder->new;
+
+subtest 'metadata() tests' => sub {
+
+    plan tests => 4;
+
+    $schema->storage->txn_begin;
+
+    my $title = 'Oranges and Peaches';
+
+    my $record = MARC::Record->new();
+    my $field = MARC::Field->new('245','','','a' => $title);
+    $record->append_fields( $field );
+    my ($biblionumber) = C4::Biblio::AddBiblio($record, '');
+
+    my $biblio = Koha::Biblios->find( $biblionumber );
+    is( ref $biblio, 'Koha::Biblio', 'Found a Koha::Biblio object' );
+
+    my $metadata = $biblio->metadata;
+    is( ref $metadata, 'Koha::Biblio::Metadata', 'Method metadata() returned a Koha::Biblio::Metadata object' );
+
+    my $record2 = $metadata->record;
+    is( ref $record2, 'MARC::Record', 'Method record() returned a MARC::Record object' );
+
+    is( $record2->field('245')->subfield("a"), $title, 'Title in 245$a matches title from original record object' );
+
+    $schema->storage->txn_rollback;
+};
+
+subtest 'hidden_in_opac() tests' => sub {
+
+    plan tests => 3;
+
+    $schema->storage->txn_begin;
+
+    my $biblio = $builder->build_sample_biblio();
+    my $item_1 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
+    my $item_2 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
+
+    $item_1->withdrawn( 1 )->store->discard_changes;
+    $item_2->withdrawn( 1 )->store->discard_changes;
+
+    ok( !$biblio->hidden_in_opac({ rules => { withdrawn => [ 2 ] } }), 'Biblio not hidden' );
+
+    $item_2->withdrawn( 2 )->store->discard_changes;
+    $biblio->discard_changes; # refresh
+
+    ok( !$biblio->hidden_in_opac({ rules => { withdrawn => [ 2 ] } }), 'Biblio not hidden' );
+
+    $item_1->withdrawn( 2 )->store->discard_changes;
+    $biblio->discard_changes; # refresh
+
+    ok( $biblio->hidden_in_opac({ rules => { withdrawn => [ 2 ] } }), 'Biblio hidden' );
 
-use_ok('Koha::Biblio');
-use_ok('Koha::Biblios');
+    $schema->storage->txn_rollback;
+};
 
-my $schema = Koha::Database->new()->schema();
-$schema->storage->txn_begin();
+subtest 'items() tests' => sub {
 
-my $dbh = C4::Context->dbh;
-$dbh->{RaiseError} = 1;
+    plan tests => 3;
 
-my @branches = Koha::Libraries->search();
-my $borrower = Koha::Patrons->search()->next();
+    $schema->storage->txn_begin;
 
-my $biblio = MARC::Record->new();
-$biblio->append_fields(
-    MARC::Field->new( '100', ' ', ' ', a => 'Hall, Kyle' ),
-    MARC::Field->new( '245', ' ', ' ', a => "Test Record", b => "Test Record Subtitle", b => "Another Test Record Subtitle" ),
-);
-my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $biblio, '' );
+    my $biblio = $builder->build_sample_biblio();
+    my $item_1 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
+    my $item_2 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
 
-my $field_mappings = Koha::Database->new()->schema()->resultset('Fieldmapping');
-$field_mappings->delete();
-$field_mappings->create( { field => 'subtitle', fieldcode => '245', subfieldcode => 'b' } );
+    my $items = $biblio->items;
+    is( ref($items), 'Koha::Items', 'Returns a Koha::Items resultset' );
+    is( $items->count, 2, 'Two items in resultset' );
 
-$biblio = Koha::Biblios->find( $biblionumber );
-my @subtitles = $biblio->subtitles();
-is( $subtitles[0], 'Test Record Subtitle', 'Got first subtitle correctly' );
-is( $subtitles[1], 'Another Test Record Subtitle', 'Got second subtitle correctly' );
+    my @items = $biblio->items->as_list;
+    is( scalar @items, 2, 'Same result, but in list context' );
 
-$schema->storage->txn_rollback();
+    $schema->storage->txn_rollback;
 
-1;
+};