From 16f9e9500c76167070d7feda2d81d50cc1ab19f3 Mon Sep 17 00:00:00 2001
From: CJ Lynce <cj.lynce@westlakelibrary.org>
Date: Thu, 3 Oct 2024 14:24:31 +0000
Subject: [PATCH] Bug 38043: Add unit tests for KohaTimes TT filter
This adds units tests for the new KohaTimes TT filter.
This also corrects a minor issue with a wrong comparitor operation in
KohaTimes.pm causing a WARN to be thrown.
To test
1. Apply patch
2. prove t/db_dependent/Template/Plugin/KohaTimes.t
3. Verify 5 Tests PASS
Signed-off-by: Lucas Gass <lucas@bywatersolutions.com>
---
Koha/Template/Plugin/KohaTimes.pm | 2 +-
t/db_dependent/Template/Plugin/KohaTimes.t | 38 ++++++++++++++++++++++
2 files changed, 39 insertions(+), 1 deletion(-)
create mode 100755 t/db_dependent/Template/Plugin/KohaTimes.t
diff --git a/Koha/Template/Plugin/KohaTimes.pm b/Koha/Template/Plugin/KohaTimes.pm
index c65c68eaf64..3e383e327fd 100644
--- a/Koha/Template/Plugin/KohaTimes.pm
+++ b/Koha/Template/Plugin/KohaTimes.pm
@@ -26,7 +26,7 @@ sub filter {
return "" unless $text;
my ( $hours, $minutes, $seconds ) = split( ':', $text );
- if ( C4::Context->preference('TimeFormat') == "12hr" ) {
+ if ( C4::Context->preference('TimeFormat') eq "12hr" ) {
my $ampm = ( $hours >= 12 ) ? 'pm' : 'am';
$hours = ( $hours == 0 ) ? "12" : $hours;
$hours = ( $hours > 12 ) ? ( $hours - 12 ) : $hours;
diff --git a/t/db_dependent/Template/Plugin/KohaTimes.t b/t/db_dependent/Template/Plugin/KohaTimes.t
new file mode 100755
index 00000000000..4a35f018f8f
--- /dev/null
+++ b/t/db_dependent/Template/Plugin/KohaTimes.t
@@ -0,0 +1,38 @@
+#!/usr/bin/perl
+
+use Modern::Perl;
+
+use C4::Context;
+
+use Test::MockModule;
+use Test::More tests => 5;
+use t::lib::Mocks;
+
+BEGIN {
+ use_ok('Koha::Template::Plugin::KohaTimes');
+}
+
+my $module_context = Test::MockModule->new('C4::Context');
+
+my $test_time = "21:45:32";
+my $test_midnight = "00:00:00";
+
+my $context = C4::Context->new();
+
+my $filter = Koha::Template::Plugin::KohaTimes->new();
+ok( $filter, "new()" );
+
+t::lib::Mocks::mock_preference( "TimeFormat", '24hr' );
+$context->clear_syspref_cache();
+
+my $filtered_time = $filter->filter($test_time);
+is( $filtered_time, "21:45", "24-hour conversion" ) or diag("24-hour conversion failed");
+
+t::lib::Mocks::mock_preference( "TimeFormat", '12hr' );
+$context->clear_syspref_cache();
+
+$filtered_time = $filter->filter($test_time);
+is( $filtered_time, "09:45 pm", "12-hour conversion" ) or diag("12-hour conversion failed");
+
+$filtered_time = $filter->filter($test_midnight);
+is( $filtered_time, "12:00 am", "12-hour midnight/AM conversion" ) or diag("12-hour midnight/AM conversion failed");
--
2.39.2