From b5510017e360c1713f42764fa2437429b4e04760 Mon Sep 17 00:00:00 2001
From: Slava Shishkin <slavashishkin@gmail.com>
Date: Fri, 29 Apr 2022 12:10:54 +0300
Subject: [PATCH] Bug 23919: Add tests for Items search by ISBN and ISSN with
 variations

(sysprefs SearchWithISBNVariations/SearchWithISSNVariations)
---
 t/db_dependent/Items.t | 49 +++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 48 insertions(+), 1 deletion(-)

diff --git a/t/db_dependent/Items.t b/t/db_dependent/Items.t
index ab0fa8e66e..3c6fd4f7b3 100755
--- a/t/db_dependent/Items.t
+++ b/t/db_dependent/Items.t
@@ -408,7 +408,7 @@ subtest q{Test Koha::Database->schema()->resultset('Item')->itemtype()} => sub {
 };
 
 subtest 'SearchItems test' => sub {
-    plan tests => 19;
+    plan tests => 20;
 
     $schema->storage->txn_begin;
     my $dbh = C4::Context->dbh;
@@ -666,6 +666,53 @@ subtest 'SearchItems test' => sub {
     ($items, $total_results) = SearchItems($filter,$params);
     is($items->[0]->{barcode}, $item1->barcode, 'Items sorted as expected by availability');
 
+    subtest 'Search items by ISSN and ISBN with variations' => sub {
+        plan tests => 4;
+
+        my $marc_record = MARC::Record->new;
+        # Prepare ISBN for biblio:
+        $marc_record->append_fields( MARC::Field->new( '020', '', '', 'a' => '9780136019701' ) );
+        # Prepare ISSN for biblio:
+        $marc_record->append_fields( MARC::Field->new( '022', '', '', 'a' => '2434561X' ) );
+        my ( $isbnissn_biblionumber ) = AddBiblio( $marc_record, '' );
+        my $isbnissn_biblio = Koha::Biblios->find($isbnissn_biblionumber);
+
+        my $item = $builder->build_sample_item(
+            {
+                biblionumber => $isbnissn_biblio->biblionumber,
+            }
+        );
+        my $item_itemnumber = $item->itemnumber;
+
+        my $filter_isbn = {
+            field => 'isbn',
+            query => '978013-6019701',
+            operator => 'like',
+        };
+
+        t::lib::Mocks::mock_preference('SearchWithISBNVariations', 0);
+        ($items, $total_results) = SearchItems($filter_isbn);
+        is($total_results, 0, "Search items finds ISBN, no variations");
+
+        t::lib::Mocks::mock_preference('SearchWithISBNVariations', 1);
+        ($items, $total_results) = SearchItems($filter_isbn);
+        is($total_results, 1, "Search items finds ISBN with variations");
+
+        my $filter_issn = {
+            field => 'issn',
+            query => '2434-561X',
+            operator => 'like',
+        };
+
+        t::lib::Mocks::mock_preference('SearchWithISSNVariations', 0);
+        ($items, $total_results) = SearchItems($filter_issn);
+        is($total_results, 0, "Search items finds ISSN, no variations");
+
+        t::lib::Mocks::mock_preference('SearchWithISSNVariations', 1);
+        ($items, $total_results) = SearchItems($filter_issn);
+        is($total_results, 1, "Search items finds ISSN with variations");
+    };
+
     $schema->storage->txn_rollback;
 };
 
-- 
2.35.1