From 57f23c6bd12df644fe814e6cf2df3f20f9f34969 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Wed, 5 Oct 2022 11:53:46 -0300 Subject: [PATCH] Bug 31333: (QA follow-up) Move and improve tests This patch moves tests to the right file (singular class name). It also refactors them a bit, in order to cover more cases, including the syspref split one. To test: 1. Run: $ kshell k$ prove t/db_dependent/Koha/Patron/Category.t \ t/db_dependent/Koha/Patron/Categories.t => SUCCESS: All good. Signed-off-by: Tomas Cohen Arazi --- t/db_dependent/Koha/Patron/Categories.t | 27 +------------ t/db_dependent/Koha/Patron/Category.t | 52 ++++++++++++++++++++++++- 2 files changed, 52 insertions(+), 27 deletions(-) diff --git a/t/db_dependent/Koha/Patron/Categories.t b/t/db_dependent/Koha/Patron/Categories.t index 6be30bb82a..f6cdba7e24 100755 --- a/t/db_dependent/Koha/Patron/Categories.t +++ b/t/db_dependent/Koha/Patron/Categories.t @@ -19,7 +19,7 @@ use Modern::Perl; -use Test::More tests => 11; +use Test::More tests => 10; use C4::Context; use Koha::Database; @@ -115,30 +115,5 @@ my $new_category_4 = Koha::Patron::Category->new( )->store; is( Koha::Patron::Categories->search->count, $nb_of_categories + 2, 'upperagelimit and dateofbirthrequired should have a default value if empty string is passed' ); -subtest 'can_make_suggestions' => sub { - plan tests => 5; - t::lib::Mocks::mock_preference('suggestion', 0); - my $category = Koha::Patron::Category->new({ - categorycode => 'the_cat', - category_type => 'A', - description => 'thecatdesc', - enrolementperiod => undef - })->store; - is ( $category->can_make_suggestions, undef, 'With suggestion syspref disabled and suggestionPatronCategoryExceptions syspref empty then Koha::Patron::Category->can_make_suggestions() returns undef' ); - - t::lib::Mocks::mock_preference('suggestion', 1); - is( $category->can_make_suggestions, 1, 'With suggestion syspref enabled and suggestionPatronsCategoryExceptions syspref empty then Koha::Patron::Category->can_make_suggestions() returns 1' ); - - t::lib::Mocks::mock_preference('suggestionPatronCategoryExceptions', 'the_cat'); - is( $category->can_make_suggestions, undef, 'With suggestion syspref enabled and suggestionPatronCategoryExceptions syspref = "the_cat" then Koha::Patron::Category->can_make_suggestions() returns undef' ); - - t::lib::Mocks::mock_preference('suggestionPatronCategoryExceptions', 'mycatcodeW'); - is( $category->can_make_suggestions, 1, 'With suggestion syspref enabled and suggestionPatronCategoryExceptions syspref = "mycatcodeW" then Koha::Patron::Category->can_make_suggestions returns 1' ); - - t::lib::Mocks::mock_preference('suggestion', 0); - is( $category->can_make_suggestions, undef, 'With suggestion syspref disabled and suggestionPatronCategoryExceptions syspref = "mycatcodeW" then Koha::Patron::Category->can_make_suggestions returns undef' ); - $category->delete; -}; - $schema->storage->txn_rollback; diff --git a/t/db_dependent/Koha/Patron/Category.t b/t/db_dependent/Koha/Patron/Category.t index be7f32b8de..983c4b2b31 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 => 6; +use Test::More tests => 7; use t::lib::TestBuilder; use t::lib::Mocks; @@ -219,3 +219,53 @@ subtest 'get_password_expiry_date() tests' => sub { is( $category->get_password_expiry_date(), dt_from_string()->add( days => 32 )->ymd, "Date correctly calculated from password_expiry_days when set"); }; + +subtest 'can_make_suggestions' => sub { + + plan tests => 6; + + $schema->storage->txn_begin; + + t::lib::Mocks::mock_preference( 'suggestion', 0 ); + t::lib::Mocks::mock_preference( 'suggestionPatronCategoryExceptions', undef ); + + my $category_1 = $builder->build_object({ class => 'Koha::Patron::Categories' }); + my $category_2 = $builder->build_object({ class => 'Koha::Patron::Categories' }); + + ok( + !$category_1->can_make_suggestions && !$category_2->can_make_suggestions, + 'suggestions globally disabled, categories not in exceptions' + ); + + t::lib::Mocks::mock_preference( 'suggestion', 1 ); + ok( + $category_1->can_make_suggestions && $category_2->can_make_suggestions, + 'suggestions globally enabled' + ); + + t::lib::Mocks::mock_preference( 'suggestionPatronCategoryExceptions', $category_2->categorycode ); + ok( + $category_1->can_make_suggestions && !$category_2->can_make_suggestions, + 'suggestions enabled, suggestionPatronCategoryExceptions set, so present categories not allowed' + ); + + t::lib::Mocks::mock_preference( 'suggestionPatronCategoryExceptions', $category_1->categorycode ); + ok( + !$category_1->can_make_suggestions && $category_2->can_make_suggestions, + 'suggestions enabled, suggestionPatronCategoryExceptions set, so present categories not allowed' + ); + + t::lib::Mocks::mock_preference( 'suggestionPatronCategoryExceptions', join( ',', $category_1->categorycode, $category_2->categorycode) ); + ok( + !$category_1->can_make_suggestions && !$category_2->can_make_suggestions, + 'suggestions enabled, suggestionPatronCategoryExceptions set to both categories, both denied' + ); + + t::lib::Mocks::mock_preference( 'suggestion', 0 ); + ok( + !$category_1->can_make_suggestions && !$category_2->can_make_suggestions, + 'suggestions disabled, no matter what the value of suggestionPatronCategoryExceptions is' + ); + + $schema->storage->txn_rollback; +}; -- 2.37.3