From f4aaf636eedbbb49d4269ebff747a36b067f6273 Mon Sep 17 00:00:00 2001
From: Thibaud Guillot <thibaud.guillot@biblibre.com>
Date: Fri, 23 Feb 2024 10:00:01 +0100
Subject: [PATCH] Bug 35906: Add unit test

Signed-off-by: Esther <esther@bywatersolutions.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
---
 t/db_dependent/Koha/Items.t | 19 ++++++++++++++++++-
 t/lib/TestBuilder.pm        |  1 +
 2 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/t/db_dependent/Koha/Items.t b/t/db_dependent/Koha/Items.t
index 58e1c1f113..cf0f9e2edb 100755
--- a/t/db_dependent/Koha/Items.t
+++ b/t/db_dependent/Koha/Items.t
@@ -2112,7 +2112,7 @@ subtest 'filter_by_for_hold' => sub {
 };
 
 subtest 'filter_by_bookable' => sub {
-    plan tests => 2;
+    plan tests => 3;
 
     $schema->storage->txn_begin;
 
@@ -2131,5 +2131,22 @@ subtest 'filter_by_bookable' => sub {
         "the correct item is returned from filter_by_bookable"
     );
 
+    # unset level booking on item (for itemtype)
+    t::lib::Mocks::mock_preference( 'item-level_booking', 0 );
+
+    # test with itemtype directly bookable
+    my $item_type = $builder->build_object( { class => 'Koha::ItemTypes', value => { bookable => 1 } } );
+    my $biblio2   = $builder->build_sample_biblio( { itemtype => $item_type->itemtype } );
+
+    # bookable items
+    my $bookable_item3 = $builder->build_sample_item(
+        { biblionumber => $biblio2->biblionumber, itype => $item_type->itemtype, bookable => 1 } );
+    my $bookable_item4 = $builder->build_sample_item(
+        { biblionumber => $biblio2->biblionumber, itype => $item_type->itemtype, bookable => 0 } );
+
+    # items are bookable even if bookable => 0 on item (due to itemtype bookable => 1)
+    is( $biblio2->items->filter_by_bookable->count, 2, "filter_by_bookable returns the correct number of items" );
+
     $schema->storage->txn_rollback;
+
 };
diff --git a/t/lib/TestBuilder.pm b/t/lib/TestBuilder.pm
index 57506b11d7..6a4587fe82 100644
--- a/t/lib/TestBuilder.pm
+++ b/t/lib/TestBuilder.pm
@@ -638,6 +638,7 @@ sub _gen_default_values {
             defaultreplacecost => 0,
             processfee => 0,
             notforloan => 0,
+            bookable => 0,
         },
         Aqbookseller => {
             tax_rate => 0,
-- 
2.25.1