From 828530308c63472ca6dbc460626520e1c2d93195 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Fri, 14 Jan 2022 14:49:29 -0300 Subject: [PATCH] Bug 29886: Unit tests Signed-off-by: Tomas Cohen Arazi Signed-off-by: David Nind --- t/db_dependent/Koha/Suggestions.t | 74 ++++++++++++++++++++++++++++++- 1 file changed, 73 insertions(+), 1 deletion(-) diff --git a/t/db_dependent/Koha/Suggestions.t b/t/db_dependent/Koha/Suggestions.t index 9cd1bff8c0..8dbb93afd3 100755 --- a/t/db_dependent/Koha/Suggestions.t +++ b/t/db_dependent/Koha/Suggestions.t @@ -19,7 +19,7 @@ use Modern::Perl; -use Test::More tests => 8; +use Test::More tests => 9; use Test::Exception; use Koha::Suggestion; @@ -27,6 +27,7 @@ use Koha::Suggestions; use Koha::Database; use Koha::DateUtils qw( dt_from_string output_pref ); +use t::lib::Mocks; use t::lib::TestBuilder; my $schema = Koha::Database->new->schema; @@ -257,3 +258,74 @@ subtest 'fund' => sub { $schema->storage->txn_rollback; }; + +subtest 'search_limited() tests' => sub { + + plan tests => 4; + + $schema->storage->txn_begin; + + # Two libraries + my $library_1 = $builder->build_object({ class => 'Koha::Libraries' }); + my $library_2 = $builder->build_object({ class => 'Koha::Libraries' }); + + # A patron from $library_1, that is not superlibrarian at all + my $patron = $builder->build_object( + { + class => 'Koha::Patrons', + value => { branchcode => $library_1->id, flags => 0 } + } + ); + + # Add 3 suggestions, to be sorted by author + my $suggestion_1 = $builder->build_object( + { + class => 'Koha::Suggestions', + value => { branchcode => $library_1->id, author => 'A' } + } + ); + my $suggestion_2 = $builder->build_object( + { + class => 'Koha::Suggestions', + value => { branchcode => $library_2->id, author => 'B' } + } + ); + my $suggestion_3 = $builder->build_object( + { + class => 'Koha::Suggestions', + value => { branchcode => $library_2->id, author => 'C' } + } + ); + + my $resultset = Koha::Suggestions->search( + { branchcode => [ $library_1->id, $library_2->id ] }, + { order_by => { -desc => ['author'] } } ); + + is( $resultset->count, 3, 'Only this three suggestions are returned' ); + + # Now the tests + t::lib::Mocks::mock_userenv({ patron => $patron, branchcode => $library_1->id }); + + # Disable IndependentBranches + t::lib::Mocks::mock_preference( 'IndependentBranches', 0 ); + + my $filtered_rs = $resultset->search_limited; + is( $filtered_rs->count, 3, 'No IndependentBranches, all suggestions returned' ); + + # Enable IndependentBranches + t::lib::Mocks::mock_preference( 'IndependentBranches', 1 ); + + $filtered_rs = $resultset->search_limited; + + is( $filtered_rs->count, 1, 'IndependentBranches, only suggestions from own branch returned' ); + + # Make the patron superlibrarian to override IndependentBranches + $patron->flags(1)->store; + # So it reloads C4::Context->userenv->{flags} + t::lib::Mocks::mock_userenv({ patron => $patron, branchcode => $library_1->id }); + + $filtered_rs = $resultset->search_limited; + is( $filtered_rs->count, 3, 'IndependentBranches but patron is superlibrarian, all suggestions returned' ); + + $schema->storage->txn_rollback; +}; -- 2.30.2