From cf3f429715fb309ffe2e4a1afaed5badd220c626 Mon Sep 17 00:00:00 2001
From: Axel Amghar <axel.amghar@biblibre.com>
Date: Tue, 7 May 2019 15:36:09 +0200
Subject: [PATCH] Bug 20384: unit test for get_all_biblios_iterator &
 get_all_authorities_iterator

---
 .../Koha/BiblioUtils/get_all_biblios_iterator.t    |  83 ++++++++++++++++
 t/db_dependent/Koha/MetadataRecord/Authority.t     | 107 +++++++++++++++++++++
 2 files changed, 190 insertions(+)
 create mode 100644 t/db_dependent/Koha/BiblioUtils/get_all_biblios_iterator.t
 create mode 100644 t/db_dependent/Koha/MetadataRecord/Authority.t

diff --git a/t/db_dependent/Koha/BiblioUtils/get_all_biblios_iterator.t b/t/db_dependent/Koha/BiblioUtils/get_all_biblios_iterator.t
new file mode 100644
index 0000000..3793f09
--- /dev/null
+++ b/t/db_dependent/Koha/BiblioUtils/get_all_biblios_iterator.t
@@ -0,0 +1,83 @@
+# 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 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.
+#
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
+
+use Modern::Perl;
+use Koha::BiblioUtils;
+use Koha::SearchEngine::Elasticsearch;
+use t::lib::TestBuilder;
+use C4::Biblio;
+use C4::AuthoritiesMarc;
+
+use Test::More tests => 4;
+
+my $schema = Koha::Database->new()->schema();
+$schema->storage->txn_begin();
+
+my $dbh = C4::Context->dbh;
+
+$dbh->do("DELETE FROM reserves");
+$dbh->do("DELETE FROM issues");
+$dbh->do("DELETE FROM biblio");
+$dbh->do("DELETE FROM biblioitems");
+
+my $offset = 1;
+my $length = 2;
+my %iterator_options;
+
+#test without items in biblioitems
+
+my $records = Koha::BiblioUtils->get_all_biblios_iterator(\%iterator_options,$offset, $length);
+
+is(ref($records),'Koha::MetadataIterator',"the package name isn't Koha::MetadataIterator");
+
+my $count = 0;
+
+while ($records->next()) {
+    $count++;
+}
+
+is($count,0,"There is not the right number of notices");
+
+my $builder = t::lib::TestBuilder->new();
+
+# Add three biblio records
+
+my $biblio1 = MARC::Record->new;
+$biblio1->append_fields( MARC::Field->new( '200', '0', '0', 'a' => 'Titre1' ));
+my $biblionumber1 = AddBiblio($biblio1, '');
+
+my $biblio2 = MARC::Record->new;
+$biblio2->append_fields( MARC::Field->new( '201', '0', '0', 'a' => 'Titre2' ));
+my $biblionumber2 = AddBiblio($biblio2, '');
+
+my $biblio3 = MARC::Record->new;
+$biblio3->append_fields( MARC::Field->new( '202', '0', '0', 'a' => 'Titre2' ));
+my $biblionumber3 = AddBiblio($biblio3, '');
+
+#test with items in biblioitems
+
+$records = Koha::BiblioUtils->get_all_biblios_iterator(\%iterator_options,$offset, $length);
+
+is(ref($records),'Koha::MetadataIterator',"the package name is Koha::MetadataIterator");
+
+$count = 0;
+
+while ($records->next()) {
+    $count++;
+}
+
+is($count,2,"There is the right number of notices");
+
+$schema->storage->txn_rollback;
\ No newline at end of file
diff --git a/t/db_dependent/Koha/MetadataRecord/Authority.t b/t/db_dependent/Koha/MetadataRecord/Authority.t
new file mode 100644
index 0000000..75c6ce3
--- /dev/null
+++ b/t/db_dependent/Koha/MetadataRecord/Authority.t
@@ -0,0 +1,107 @@
+# 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 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.
+#
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
+
+use Modern::Perl;
+use Koha::MetadataIterator;
+use Koha::MetadataRecord::Authority;
+use Koha::SearchEngine::Elasticsearch;
+use t::lib::TestBuilder;
+use C4::Biblio;
+use C4::AuthoritiesMarc;
+
+use Test::More tests => 1;
+
+my $schema = Koha::Database->new()->schema();
+$schema->storage->txn_begin();
+
+subtest 'Test Koha::MetadataRecord::Authority' => sub {
+    plan tests => 4;
+
+    my $dbh = C4::Context->dbh;
+
+    $dbh->do("DELETE FROM auth_header");
+
+    my $offset = 1;
+    my $length = 2;
+    my %iterator_options;
+
+    #test without items in auth_header
+
+    my $records = Koha::MetadataRecord::Authority->get_all_authorities_iterator(\%iterator_options, $offset, $length);
+
+    is(ref($records),'Koha::MetadataIterator',"the package name isn't Koha::MetadataIterator");
+
+    my $count = 0;
+
+    while ($records->next()) {
+        $count++;
+    }
+
+    is($count,0,"There is not the right number of notices");
+
+    my $builder = t::lib::TestBuilder->new();
+
+    # create three auth types
+
+    my $authtype1 = $builder->build({
+        source => 'AuthType',
+        value  => {
+            auth_tag_to_report => '1',
+        },
+    });
+    my $authtype2 = $builder->build({
+        source => 'AuthType',
+        value  => {
+            auth_tag_to_report => '2',
+        },
+    });
+    my $authtype3 = $builder->build({
+        source => 'AuthType',
+        value  => {
+            auth_tag_to_report => '3',
+        },
+    });
+
+    # Add three authority records
+
+    my $auth1 = MARC::Record->new;
+    $auth1->append_fields( MARC::Field->new( '109', '0', '0', 'a' => 'Auth1' ));
+    my $authid1 = AddAuthority( $auth1, undef, $authtype1->{'authtypecode'} );
+
+    my $auth2 = MARC::Record->new;
+    $auth2->append_fields( MARC::Field->new( '110', '0', '0', 'a' => 'Auth2' ));
+    my $authid2 = AddAuthority( $auth2, undef, $authtype2->{'authtypecode'} );
+
+    my $auth3 = MARC::Record->new;
+    $auth3->append_fields( MARC::Field->new( '111', '0', '0', 'a' => 'Auth3' ));
+    my $authid3 = AddAuthority( $auth3, undef, $authtype3->{'authtypecode'} );
+
+    #test with items in auth_header
+
+    $records = Koha::MetadataRecord::Authority->get_all_authorities_iterator(\%iterator_options, $offset, $length);
+
+    is(ref($records),'Koha::MetadataIterator',"the package name is Koha::MetadataIterator");
+
+    $count = 0;
+
+    while ($records->next()) {
+        $count++;
+    }
+
+    is($count,2,"There is the right number of notices");
+
+};
+
+$schema->storage->txn_rollback;
\ No newline at end of file
-- 
2.7.4