From 193cb1c4bdd5d5dbe2425ee939ba2f63f8a75b48 Mon Sep 17 00:00:00 2001
From: Sam Lau <samalau@gmail.com>
Date: Fri, 7 Jun 2024 19:01:03 +0000
Subject: [PATCH] Bug 33462: Unit Tests

prove t/db_dependent/Koha/Patron/Category.t
prove t/db_dependent/Koha/Patron.t
---
 t/db_dependent/Koha/Patron.t          | 39 +++++++++++++++++-
 t/db_dependent/Koha/Patron/Category.t | 58 ++++++++++++++++++++++++++-
 2 files changed, 95 insertions(+), 2 deletions(-)

diff --git a/t/db_dependent/Koha/Patron.t b/t/db_dependent/Koha/Patron.t
index bad1cc1bfa..f63fcbfc2d 100755
--- a/t/db_dependent/Koha/Patron.t
+++ b/t/db_dependent/Koha/Patron.t
@@ -19,7 +19,7 @@
 
 use Modern::Perl;
 
-use Test::More tests => 33;
+use Test::More tests => 34;
 use Test::Exception;
 use Test::Warn;
 use Time::Fake;
@@ -1359,6 +1359,7 @@ subtest 'password expiration tests' => sub {
     my $category = $builder->build_object({ class => 'Koha::Patron::Categories', value => {
             password_expiry_days => 10,
             require_strong_password => 0,
+            force_password_reset_when_set_by_staff => 0,
         }
     });
     my $patron = $builder->build_object({ class=> 'Koha::Patrons', value => {
@@ -1445,6 +1446,42 @@ subtest 'password expiration tests' => sub {
 
 };
 
+subtest 'force_password_reset_when_set_by_staff tests' => sub {
+    plan tests => 3;
+
+    $schema->storage->txn_begin;
+
+    my $category = $builder->build_object({
+        class => 'Koha::Patron::Categories',
+        value => {
+            force_password_reset_when_set_by_staff => 1,
+            require_strong_password => 0,
+        }
+    });
+
+    my $patron = $builder->build_object({
+        class => 'Koha::Patrons',
+        value => {
+            categorycode => $category->categorycode,
+            password => 'hats'
+        }
+    });
+
+    $patron->delete()->store()->discard_changes();
+    is($patron->password_expired, 1, "Patron forced into changing password, password expired.");
+
+    $patron->category->force_password_reset_when_set_by_staff(0)->store();
+    $patron->delete()->store()->discard_changes();
+    is($patron->password_expired, 0, "Patron not forced into changing password, password not expired.");
+
+    $patron->category->force_password_reset_when_set_by_staff(1)->store();
+    t::lib::Mocks::mock_preference('PatronSelfRegistrationDefaultCategory', $patron->categorycode);
+    $patron->delete()->store()->discard_changes();
+    is($patron->password_expired, 0, "Patron forced into changing password but patron is self registered, password not expired.");
+
+    $schema->storage->txn_rollback;
+};
+
 subtest 'safe_to_delete() tests' => sub {
 
     plan tests => 17;
diff --git a/t/db_dependent/Koha/Patron/Category.t b/t/db_dependent/Koha/Patron/Category.t
index 0300cb4721..4e3670b78a 100755
--- a/t/db_dependent/Koha/Patron/Category.t
+++ b/t/db_dependent/Koha/Patron/Category.t
@@ -19,7 +19,7 @@
 
 use Modern::Perl;
 
-use Test::More tests => 7;
+use Test::More tests => 8;
 
 use t::lib::TestBuilder;
 use t::lib::Mocks;
@@ -204,6 +204,62 @@ subtest 'effective_require_strong_password' => sub {
   $schema->storage->txn_rollback;
 };
 
+subtest 'effective_force_password_reset_when_set_by_staff() tests' => sub {
+    plan tests => 2;
+
+    subtest 'specific overrides global' => sub {
+        plan tests => 4;
+
+        $schema->storage->txn_begin;
+
+        my $category = $builder->build_object({
+            class => 'Koha::Patron::Categories',
+            value => {
+                force_password_reset_when_set_by_staff => 1
+            }
+        });
+
+        t::lib::Mocks::mock_preference('ForcePasswordResetWhenSetByStaff', 0);
+        ok($category->effective_force_password_reset_when_set_by_staff, 'ForcePasswordResetWhenSetByStaff unset, but category has the flag set to 1');
+
+        t::lib::Mocks::mock_preference('ForcePasswordResetWhenSetByStaff', 1);
+        ok($category->effective_force_password_reset_when_set_by_staff, 'ForcePasswordResetWhenSetByStaff set and category has the flag set to 1');
+
+        # disable
+        $category->force_password_reset_when_set_by_staff(0)->store->discard_changes;
+
+        t::lib::Mocks::mock_preference('ForcePasswordResetWhenSetByStaff', 0);
+        ok(!$category->effective_force_password_reset_when_set_by_staff, 'ForcePasswordResetWhenSetByStaff unset, but category has the flag set to 0');
+
+        t::lib::Mocks::mock_preference('ForcePasswordResetWhenSetByStaff', 1);
+        ok(!$category->effective_force_password_reset_when_set_by_staff, 'ForcePasswordResetWhenSetByStaff set and category has the flag set to 0');
+
+        $schema->storage->txn_rollback;
+    };
+
+    subtest 'no specific rule, global applies' => sub {
+        plan tests => 2;
+
+        $schema->storage->txn_begin;
+
+        my $category = $builder->build_object({
+            class => 'Koha::Patron::Categories',
+            value => {
+                force_password_reset_when_set_by_staff => undef
+            }
+        });
+
+        t::lib::Mocks::mock_preference('ForcePasswordResetWhenSetByStaff', 0);
+        ok(!$category->effective_force_password_reset_when_set_by_staff, 'ForcePasswordResetWhenSetByStaff set to 0 used');
+
+        t::lib::Mocks::mock_preference('ForcePasswordResetWhenSetByStaff', 1);
+        ok($category->effective_force_password_reset_when_set_by_staff, 'ForcePasswordResetWhenSetByStaff set to 1 used');
+
+        $schema->storage->txn_rollback;
+    };
+};
+
+
 subtest 'get_password_expiry_date() tests' => sub {
 
     plan tests => 3;
-- 
2.39.2