From d510bbbd4b0890bd09560ca669cd3e600837cd08 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Joonas=20Kylm=C3=A4l=C3=A4?= <joonas.kylmala@helsinki.fi>
Date: Wed, 10 Jun 2020 20:21:46 +0300
Subject: [PATCH] Bug 11175: (follow-up) Add tests

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
Signed-off-by: Pasi Kallinen <pasi.kallinen@koha-suomi.fi>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
---
 t/db_dependent/Koha/Biblio.t | 70 +++++++++++++++++++++++++++++++++++-
 1 file changed, 69 insertions(+), 1 deletion(-)

diff --git a/t/db_dependent/Koha/Biblio.t b/t/db_dependent/Koha/Biblio.t
index 83de0634c6..4b749413d4 100755
--- a/t/db_dependent/Koha/Biblio.t
+++ b/t/db_dependent/Koha/Biblio.t
@@ -17,14 +17,18 @@
 
 use Modern::Perl;
 
-use Test::More tests => 14;
+use Test::More tests => 15;
 
 use C4::Biblio qw( AddBiblio ModBiblio );
 use Koha::Database;
 use Koha::Acquisition::Orders;
 
+use MARC::Field;
+use MARC::Record;
+
 use t::lib::TestBuilder;
 use t::lib::Mocks;
+use Test::MockModule;
 
 BEGIN {
     use_ok('Koha::Biblio');
@@ -499,6 +503,41 @@ subtest 'suggestions() tests' => sub {
     $schema->storage->txn_rollback;
 };
 
+subtest 'components() tests' => sub {
+
+    plan tests => 3;
+
+    $schema->storage->txn_begin;
+
+    my ($host_bibnum) = C4::Biblio::AddBiblio(host_record(), '');
+    my $host_biblio = Koha::Biblios->find($host_bibnum);
+    t::lib::Mocks::mock_preference( 'SearchEngine', 'Zebra' );
+    my $search_mod = Test::MockModule->new( 'Koha::SearchEngine::Zebra::Search' );
+    $search_mod->mock( 'simple_search_compat', \&search_component_record2 );
+
+    my @components = $host_biblio->components;
+    is( ref(\@components), 'ARRAY', 'Return type is correct' );
+
+    is_deeply(
+        [@components],
+        [()],
+        '->components returns an empty ARRAY'
+    );
+
+    $search_mod->unmock( 'simple_search_compat');
+    $search_mod->mock( 'simple_search_compat', \&search_component_record1 );
+    my $component_record = component_record1()->as_xml();
+
+    is_deeply(
+        $host_biblio->components,
+        [($component_record)],
+        '->components returns the related component part record'
+    );
+    $search_mod->unmock( 'simple_search_compat');
+
+    $schema->storage->txn_rollback;
+};
+
 subtest 'orders() and active_orders() tests' => sub {
 
     plan tests => 5;
@@ -637,3 +676,32 @@ subtest 'get_marc_notes() UNIMARC tests' => sub {
 
     $schema->storage->txn_rollback;
 };
+
+sub component_record1 {
+    my $marc = MARC::Record->new;
+    $marc->append_fields(
+        MARC::Field->new( '001', '3456' ),
+        MARC::Field->new( '245', '', '', a => 'Some title 1' ),
+        MARC::Field->new( '773', '', '', w => '(FIRST)1234' ),
+    );
+    return $marc;
+}
+sub search_component_record1 {
+    my @results = ( component_record1()->as_xml() );
+    return ( undef, \@results, 1 );
+}
+
+sub search_component_record2 {
+    my @results;
+    return ( undef, \@results, 0 );
+}
+
+sub host_record {
+    my $marc = MARC::Record->new;
+    $marc->append_fields(
+        MARC::Field->new( '001', '1234' ),
+        MARC::Field->new( '003', 'FIRST' ),
+        MARC::Field->new( '245', '', '', a => 'Some title' ),
+    );
+    return $marc;
+}
-- 
2.20.1