From dd3fc22e863ec8a516b3b181916a1616f2cb345e Mon Sep 17 00:00:00 2001
From: Paul Derscheid <paul.derscheid@lmscloud.de>
Date: Thu, 21 Nov 2024 10:01:56 +0000
Subject: [PATCH] Bug 38501: Prevent failures of Koha/Booking.t when running
 the Koha_Main_My8 job

---
 t/db_dependent/Koha/Booking.t | 75 ++++++++++++++++++++++++++++++++++-
 1 file changed, 73 insertions(+), 2 deletions(-)

diff --git a/t/db_dependent/Koha/Booking.t b/t/db_dependent/Koha/Booking.t
index 19aa536b12..5d5953351f 100755
--- a/t/db_dependent/Koha/Booking.t
+++ b/t/db_dependent/Koha/Booking.t
@@ -25,6 +25,8 @@ use Test::More tests => 2;
 use Test::Exception;
 
 use Koha::DateUtils qw( dt_from_string );
+use Koha::Notice::Template;
+use Koha::Notice::Templates;
 
 use t::lib::TestBuilder;
 use t::lib::Mocks;
@@ -341,8 +343,8 @@ subtest 'store() tests' => sub {
         # Cancel both bookings so we can check that cancelled bookings are allowed in the auto-assign
         $booking->status('cancelled')->store();
         $second_booking->status('cancelled')->store();
-        is($booking->status, 'cancelled', "Booking is cancelled");
-        is($second_booking->status, 'cancelled', "Second booking is cancelled");
+        is( $booking->status,        'cancelled', "Booking is cancelled" );
+        is( $second_booking->status, 'cancelled', "Second booking is cancelled" );
 
         # Test randomness of selection
         my %seen_items;
@@ -369,6 +371,39 @@ subtest 'store() tests' => sub {
     subtest 'confirmation notice trigger' => sub {
         plan tests => 2;
 
+        # FIXME: This is a bandaid solution to prevent test failures when running
+        # the Koha_Main_My8 job because notices are not added at upgrade time.
+        my $template = Koha::Notice::Templates->search(
+            {
+                module                 => 'bookings',
+                code                   => 'BOOKING_CONFIRMATION',
+                message_transport_type => 'email',
+            }
+        )->single;
+
+        if ( !$template ) {
+            my $default_content = Koha::Notice::Template->new(
+                {
+                    module                 => 'bookings',
+                    code                   => 'BOOKING_CONFIRMATION',
+                    lang                   => 'default',
+                    message_transport_type => 'email',
+                }
+            )->get_default();
+
+            Koha::Notice::Template->new(
+                {
+                    module                 => 'bookings',
+                    code                   => 'BOOKING_CONFIRMATION',
+                    name                   => 'BOOKING_CONFIRMATION Test Notice',
+                    title                  => 'BOOKING_CONFIRMATION Test Notice',
+                    content                => $default_content || 'Dummy content for BOOKING_CONFIRMATION.',
+                    branchcode             => undef,
+                    message_transport_type => 'email',
+                }
+            )->store;
+        }
+
         my $original_notices_count = Koha::Notice::Messages->search(
             {
                 letter_code    => 'BOOKING_CONFIRMATION',
@@ -420,6 +455,41 @@ subtest 'store() tests' => sub {
     subtest 'modification/cancellation notice triggers' => sub {
         plan tests => 5;
 
+        # FIXME: This is a bandaid solution to prevent test failures when running
+        # the Koha_Main_My8 job because notices are not added at upgrade time.
+        for my $notice_type (qw(BOOKING_MODIFICATION BOOKING_CANCELLATION)) {
+            my $template = Koha::Notice::Templates->search(
+                {
+                    module                 => 'bookings',
+                    code                   => $notice_type,
+                    message_transport_type => 'email',
+                }
+            )->single;
+
+            if ( !$template ) {
+                my $default_content = Koha::Notice::Template->new(
+                    {
+                        module                 => 'bookings',
+                        code                   => $notice_type,
+                        lang                   => 'default',
+                        message_transport_type => 'email',
+                    }
+                )->get_default();
+
+                Koha::Notice::Template->new(
+                    {
+                        module                 => 'bookings',
+                        code                   => $notice_type,
+                        name                   => "$notice_type Test Notice",
+                        title                  => "$notice_type Test Notice",
+                        content                => $default_content || "Dummy content for $notice_type.",
+                        branchcode             => undef,
+                        message_transport_type => 'email',
+                    }
+                )->store;
+            }
+        }
+
         my $original_modification_notices_count = Koha::Notice::Messages->search(
             {
                 letter_code    => 'BOOKING_MODIFICATION',
@@ -556,3 +626,4 @@ subtest 'store() tests' => sub {
 
     $schema->storage->txn_rollback;
 };
+
-- 
2.47.0