From ea2e0b403ba66700a3e9a00cef8069ed7faa6e5a Mon Sep 17 00:00:00 2001
From: Matt Blenkinsop <matt.blenkinsop@ptfs-europe.com>
Date: Mon, 31 Jul 2023 13:37:33 +0000
Subject: [PATCH] Bug 27378: (QA follow-up) Fix tests and character encoding

This patch fixes an encoding issue when using diacritics. It also fixes a failing test, corrects the format of the "Cancel" links in the modal and perltidy has been used on all relevant files

Signed-off-by: Katrin Fischer <katrin.fischer@bsz-bw.de>
---
 Koha/Template/Plugin/JSConsents.pm            |  4 ++-
 .../bug_27378-add_cookie_consents.pl          | 14 ++++++----
 .../prog/en/includes/intranet-bottom.inc      |  2 +-
 .../bootstrap/en/includes/masthead.inc        |  2 +-
 .../Koha/Template/Plugin/JSConsents.t         | 28 ++++++++++++++++---
 5 files changed, 38 insertions(+), 12 deletions(-)

diff --git a/Koha/Template/Plugin/JSConsents.pm b/Koha/Template/Plugin/JSConsents.pm
index 2148cc12c3..174a36a02e 100644
--- a/Koha/Template/Plugin/JSConsents.pm
+++ b/Koha/Template/Plugin/JSConsents.pm
@@ -21,6 +21,7 @@ use Template::Plugin;
 use base qw( Template::Plugin );
 use MIME::Base64 qw{ decode_base64 };
 use JSON qw{ decode_json };
+use Encode qw{ encode_utf8 };
 
 use C4::Context;
 
@@ -30,7 +31,8 @@ sub all {
     my $consents = C4::Context->preference('CookieConsentedJS');
     if ( length $consents > 0 ) {
         my $decoded_consents  = decode_base64($consents);
-        my $consents_array    = decode_json $decoded_consents;
+        my $convert_to_utf8   = encode_utf8($decoded_consents);
+        my $consents_array    = decode_json $convert_to_utf8;
         my @filtered_consents = grep { $_->{$filter} } @{$consents_array};
         return \@filtered_consents;
     } else {
diff --git a/installer/data/mysql/atomicupdate/bug_27378-add_cookie_consents.pl b/installer/data/mysql/atomicupdate/bug_27378-add_cookie_consents.pl
index ca2205f35e..40f2904985 100644
--- a/installer/data/mysql/atomicupdate/bug_27378-add_cookie_consents.pl
+++ b/installer/data/mysql/atomicupdate/bug_27378-add_cookie_consents.pl
@@ -1,16 +1,20 @@
 use Modern::Perl;
 
 return {
-    bug_number => "27378",
+    bug_number  => "27378",
     description => "Adds the sysprefs for cookie consents",
-    up => sub {
+    up          => sub {
         my ($args) = @_;
-        my ($dbh, $out) = @$args{qw(dbh out)};
+        my ( $dbh, $out ) = @$args{qw(dbh out)};
 
-        $dbh->do( q| INSERT IGNORE INTO systempreferences (variable, value, explanation, options, type) VALUES ('CookieConsentedJS', '', 'Add Javascript code that will run if cookie consent is provided (e.g. tracking code).', '', 'Free'); | );
+        $dbh->do(
+            q| INSERT IGNORE INTO systempreferences (variable, value, explanation, options, type) VALUES ('CookieConsentedJS', '', 'Add Javascript code that will run if cookie consent is provided (e.g. tracking code).', '', 'Free'); |
+        );
         say $out "Added new system preference 'CookieConsentedJS'";
 
-        $dbh->do( q| INSERT IGNORE INTO systempreferences (variable, value, explanation, options, type) VALUES ('CookieConsent', '0', 'Require cookie consent to be displayed', '', 'YesNo'); | );
+        $dbh->do(
+            q| INSERT IGNORE INTO systempreferences (variable, value, explanation, options, type) VALUES ('CookieConsent', '0', 'Require cookie consent to be displayed', '', 'YesNo'); |
+        );
         say $out "Added new system preference 'CookieConsent'";
     },
 };
diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/intranet-bottom.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/intranet-bottom.inc
index cd3abdfb5a..f46b55d01e 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/includes/intranet-bottom.inc
+++ b/koha-tmpl/intranet-tmpl/prog/en/includes/intranet-bottom.inc
@@ -137,7 +137,7 @@
                                 <button type="button" class="btn btn-primary consentAcceptEssential">Accept only essential cookies</button>
                                 <button type="button" class="btn btn-success" id="consentAcceptSelected">Accept selected non-essential cookies</button>
                             [% END %]
-                            <button type="button" class="btn btn-secondary consentCloseModal">Cancel</button>
+                            <a type="button" href="#" class="btn btn-secondary consentCloseModal">Cancel</a>
                         </div>
                     </div>
                 </div> <!-- /.modal-content -->
diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/includes/masthead.inc b/koha-tmpl/opac-tmpl/bootstrap/en/includes/masthead.inc
index 8788bf9520..b33ad52477 100644
--- a/koha-tmpl/opac-tmpl/bootstrap/en/includes/masthead.inc
+++ b/koha-tmpl/opac-tmpl/bootstrap/en/includes/masthead.inc
@@ -503,7 +503,7 @@
                                 <button type="button" class="btn btn-primary consentAcceptEssential">Accept only essential cookies</button>
                                 <button type="button" class="btn btn-warning" id="consentAcceptSelected">Accept selected non-essential cookies</button>
                             [% END %]
-                            <button type="button" class="btn btn-secondary consentCloseModal">Cancel</button>
+                            <a type="button" href="#" class="btn btn-secondary consentCloseModal">Cancel</a>
                         </div>
                     </div>
                 </div> <!-- /.modal-content -->
diff --git a/t/db_dependent/Koha/Template/Plugin/JSConsents.t b/t/db_dependent/Koha/Template/Plugin/JSConsents.t
index e48f359651..27428f0bbe 100755
--- a/t/db_dependent/Koha/Template/Plugin/JSConsents.t
+++ b/t/db_dependent/Koha/Template/Plugin/JSConsents.t
@@ -9,7 +9,7 @@ use Test::More tests => 3;
 use t::lib::Mocks;
 
 BEGIN {
-    use_ok('Koha::Template::Plugin::JSConsents', "Can use Koha::Template::Plugin::JSConsents");
+    use_ok( 'Koha::Template::Plugin::JSConsents', "Can use Koha::Template::Plugin::JSConsents" );
 }
 
 ok( my $consents = Koha::Template::Plugin::JSConsents->new(), 'Able to instantiate template plugin' );
@@ -17,9 +17,29 @@ ok( my $consents = Koha::Template::Plugin::JSConsents->new(), 'Able to instantia
 subtest "all" => sub {
     plan tests => 1;
 
-    t::lib::Mocks::mock_preference( 'CookieConsentedJS', 'eyAidGVzdCI6ICJvbmUiIH0=' );
-
-    is_deeply( $consents->all(), { test => 'one' }, 'Returns a Base64 decoded JSON object converted into a data structure');
+    t::lib::Mocks::mock_preference(
+        'CookieConsentedJS',
+        'W3siaWQiOiJfbGFrZGhjOW11IiwibmFtZSI6InRlc3QiLCJkZXNjcmlwdGlvbiI6InRlc3QiLCJtYXRjaFBhdHRlcm4iOiJ0ZXN0MSIsImNvb2tpZURvbWFpbiI6ImxvY2FsaG9zdCIsImNvb2tpZVBhdGgiOiIvIiwib3BhY0NvbnNlbnQiOnRydWUsInN0YWZmQ29uc2VudCI6dHJ1ZSwiY29kZSI6IktHWjFibU4wYVc5dUtDa2dleUFLSUNBZ0lHTnZibk52YkdVdWJHOW5LQ2RJWld4c2J5Qm1jbTl0SUhSbGMzUXhKeWs3SUFvZ0lDQWdaRzlqZFcxbGJuUXVZMjl2YTJsbElEMGdJblJsYzNReFBYUmxjM1JwYm1jN0lHUnZiV0ZwYmoxc2IyTmhiR2h2YzNRN0lIQmhkR2c5THpzZ1UyRnRaVk5wZEdVOVRtOXVaVHNnVTJWamRYSmxJanNnQ24wcEtDazcifV0='
+    );
+
+    is_deeply(
+        $consents->all('opacConsent'),
+        [
+            {
+                'name'       => 'test',
+                'cookiePath' => '/',
+                'code' =>
+                    'KGZ1bmN0aW9uKCkgeyAKICAgIGNvbnNvbGUubG9nKCdIZWxsbyBmcm9tIHRlc3QxJyk7IAogICAgZG9jdW1lbnQuY29va2llID0gInRlc3QxPXRlc3Rpbmc7IGRvbWFpbj1sb2NhbGhvc3Q7IHBhdGg9LzsgU2FtZVNpdGU9Tm9uZTsgU2VjdXJlIjsgCn0pKCk7',
+                'staffConsent' => 1,
+                'matchPattern' => 'test1',
+                'description'  => 'test',
+                'cookieDomain' => 'localhost',
+                'opacConsent'  => 1,
+                'id'           => '_lakdhc9mu'
+            }
+        ],
+        'Returns a Base64 decoded JSON object converted into a data structure'
+    );
 };
 
 1;
-- 
2.30.2