From 27c5642f230daa53943bea111971f7242ea02aa2 Mon Sep 17 00:00:00 2001 From: Sam Lau Date: Mon, 17 Jul 2023 21:30:54 +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 | 41 +++++++++++++++++- t/db_dependent/Koha/Patron/Category.t | 60 ++++++++++++++++++++++++++- 2 files changed, 98 insertions(+), 3 deletions(-) diff --git a/t/db_dependent/Koha/Patron.t b/t/db_dependent/Koha/Patron.t index 5d05a16efd..1c6cb4c3cc 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 => 24; +use Test::More tests => 25; use Test::Exception; use Test::Warn; @@ -30,7 +30,6 @@ use Koha::ArticleRequests; use Koha::Patrons; use Koha::Patron::Relationships; use C4::Circulation qw( AddIssue AddReturn ); - use t::lib::TestBuilder; use t::lib::Mocks; @@ -975,6 +974,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 => { @@ -1061,6 +1061,43 @@ 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 => 14; diff --git a/t/db_dependent/Koha/Patron/Category.t b/t/db_dependent/Koha/Patron/Category.t index eeed784348..043521b9a3 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,64 @@ 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 => 2; -- 2.30.2