From 478e9ed4d8be3ed88cd465f87196859a9a23d083 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Sun, 13 Jul 2025 08:34:21 +0100 Subject: [PATCH] Bug 35267: (follow-up) More comprehensive and db agnostic tests This patch updates our unit tests to be more agnostic to prior database state during testing by mocking more of the preferences. We also add more tests to ensure we fully cover the new preferences and their options. --- t/db_dependent/Koha/Notice/Message.t | 150 ++++++++++++++++++++++++++- 1 file changed, 148 insertions(+), 2 deletions(-) diff --git a/t/db_dependent/Koha/Notice/Message.t b/t/db_dependent/Koha/Notice/Message.t index 03027662a9f..e18779ff233 100755 --- a/t/db_dependent/Koha/Notice/Message.t +++ b/t/db_dependent/Koha/Notice/Message.t @@ -20,7 +20,7 @@ use Modern::Perl; use Test::NoWarnings; -use Test::More tests => 3; +use Test::More tests => 4; use C4::Letters qw( GetPreparedLetter EnqueueLetter ); @@ -145,7 +145,14 @@ subtest 'html_content() tests' => sub { } ); - t::lib::Mocks::mock_preference( 'AllNoticeStylesheet', '' ); + # Mock all CSS preferences to ensure clean test state + t::lib::Mocks::mock_preference( 'AllNoticeStylesheet', '' ); + t::lib::Mocks::mock_preference( 'AllNoticeCSS', '' ); + t::lib::Mocks::mock_preference( 'EmailNoticeStylesheet', '' ); + t::lib::Mocks::mock_preference( 'EmailNoticeCSS', '' ); + t::lib::Mocks::mock_preference( 'PrintNoticeStylesheet', '' ); + t::lib::Mocks::mock_preference( 'PrintNoticeCSS', '' ); + my $css_import = ''; my $message = Koha::Notice::Messages->find($message_id); my $wrapped_compare = <<"WRAPPED"; @@ -190,6 +197,15 @@ WRAPPED ); $template->is_html(0)->store; + + # Reset all preferences for plaintext test + t::lib::Mocks::mock_preference( 'AllNoticeStylesheet', '' ); + t::lib::Mocks::mock_preference( 'AllNoticeCSS', '' ); + t::lib::Mocks::mock_preference( 'EmailNoticeStylesheet', '' ); + t::lib::Mocks::mock_preference( 'EmailNoticeCSS', '' ); + t::lib::Mocks::mock_preference( 'PrintNoticeStylesheet', '' ); + t::lib::Mocks::mock_preference( 'PrintNoticeCSS', '' ); + $prepared_letter = GetPreparedLetter( ( module => 'test', @@ -220,4 +236,134 @@ WRAPPED $schema->storage->txn_rollback; }; +subtest 'stylesheets() tests' => sub { + plan tests => 8; + + $schema->storage->txn_begin; + + my $template = $builder->build_object( + { + class => 'Koha::Notice::Templates', + value => { + module => 'test', + code => 'TEST', + message_transport_type => 'email', + is_html => '1', + name => 'test notice template', + title => 'Test Title', + content => 'Test content', + branchcode => "", + lang => 'default', + } + } + ); + + my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); + + my $prepared_letter = GetPreparedLetter( + ( + module => 'test', + letter_code => 'TEST', + tables => { + borrowers => $patron->id, + }, + ) + ); + + # Test with no stylesheets set + t::lib::Mocks::mock_preference( 'AllNoticeStylesheet', '' ); + t::lib::Mocks::mock_preference( 'AllNoticeCSS', '' ); + t::lib::Mocks::mock_preference( 'EmailNoticeStylesheet', '' ); + t::lib::Mocks::mock_preference( 'EmailNoticeCSS', '' ); + t::lib::Mocks::mock_preference( 'PrintNoticeStylesheet', '' ); + t::lib::Mocks::mock_preference( 'PrintNoticeCSS', '' ); + + my $message_id = EnqueueLetter( + { + letter => $prepared_letter, + borrowernumber => $patron->id, + message_transport_type => 'email' + } + ); + my $message = Koha::Notice::Messages->find($message_id); + + is( $message->stylesheets, '', "No stylesheets when all preferences are empty" ); + + # Test AllNoticeStylesheet only + t::lib::Mocks::mock_preference( 'AllNoticeStylesheet', 'https://example.com/all.css' ); + is( + $message->stylesheets, '', + "AllNoticeStylesheet works correctly" + ); + + # Test AllNoticeCSS only + t::lib::Mocks::mock_preference( 'AllNoticeStylesheet', '' ); + t::lib::Mocks::mock_preference( 'AllNoticeCSS', 'body { color: red; }' ); + is( $message->stylesheets, '', "AllNoticeCSS works correctly" ); + + # Test email-specific stylesheet for email transport + t::lib::Mocks::mock_preference( 'AllNoticeCSS', '' ); + t::lib::Mocks::mock_preference( 'EmailNoticeStylesheet', 'https://example.com/email.css' ); + is( + $message->stylesheets, '', + "EmailNoticeStylesheet works for email transport" + ); + + # Test email-specific CSS for email transport + t::lib::Mocks::mock_preference( 'EmailNoticeStylesheet', '' ); + t::lib::Mocks::mock_preference( 'EmailNoticeCSS', '.email { font-weight: bold; }' ); + is( + $message->stylesheets, '', + "EmailNoticeCSS works for email transport" + ); + + # Test combined all + email styles + t::lib::Mocks::mock_preference( 'AllNoticeStylesheet', 'https://example.com/all.css' ); + t::lib::Mocks::mock_preference( 'AllNoticeCSS', 'body { margin: 0; }' ); + t::lib::Mocks::mock_preference( 'EmailNoticeStylesheet', 'https://example.com/email.css' ); + t::lib::Mocks::mock_preference( 'EmailNoticeCSS', '.email { color: blue; }' ); + + my $expected_combined = + '' . "\n" + . '' + . '' . "\n" + . ''; + is( $message->stylesheets, $expected_combined, "Combined all and email styles work correctly" ); + + # Test print transport type + $message_id = EnqueueLetter( + { + letter => $prepared_letter, + borrowernumber => $patron->id, + message_transport_type => 'print' + } + ); + $message = Koha::Notice::Messages->find($message_id); + + # Reset email preferences and set print preferences + t::lib::Mocks::mock_preference( 'EmailNoticeStylesheet', '' ); + t::lib::Mocks::mock_preference( 'EmailNoticeCSS', '' ); + t::lib::Mocks::mock_preference( 'PrintNoticeStylesheet', 'https://example.com/print.css' ); + t::lib::Mocks::mock_preference( 'PrintNoticeCSS', '.print { page-break-after: always; }' ); + + my $expected_print = + '' . "\n" + . '' + . '' . "\n" + . ''; + is( $message->stylesheets, $expected_print, "Print transport type uses correct stylesheets" ); + + # Test that email styles are NOT included for print transport + t::lib::Mocks::mock_preference( 'AllNoticeStylesheet', '' ); + t::lib::Mocks::mock_preference( 'AllNoticeCSS', '' ); + t::lib::Mocks::mock_preference( 'EmailNoticeStylesheet', 'https://example.com/email.css' ); + t::lib::Mocks::mock_preference( 'EmailNoticeCSS', '.email { color: blue; }' ); + t::lib::Mocks::mock_preference( 'PrintNoticeStylesheet', '' ); + t::lib::Mocks::mock_preference( 'PrintNoticeCSS', '' ); + + is( $message->stylesheets, '', "Print transport does not include email-specific styles" ); + + $schema->storage->txn_rollback; +}; + 1; -- 2.50.1