From 138aa4f8d5ade706414e937eb1a9758059a69452 Mon Sep 17 00:00:00 2001 From: Lari Taskula Date: Tue, 21 Oct 2025 15:03:16 +0300 Subject: [PATCH] Bug 27734: Add tests --- t/db_dependent/Output.t | 131 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 129 insertions(+), 2 deletions(-) diff --git a/t/db_dependent/Output.t b/t/db_dependent/Output.t index 5ed9b6e6a54..b344497e370 100755 --- a/t/db_dependent/Output.t +++ b/t/db_dependent/Output.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 3; +use Test::More tests => 4; use Test::MockModule; use Test::NoWarnings; use Test::Warn; @@ -30,13 +30,140 @@ use t::lib::TestBuilder; BEGIN { use_ok( 'C4::Output', - qw( redirect_if_opac_suppressed ) + qw( redirect_if_opac_hidden redirect_if_opac_suppressed ) ); } my $builder = t::lib::TestBuilder->new(); my $schema = Koha::Database->new()->schema(); +subtest 'redirect_if_opac_hidden() tests' => sub { + + plan tests => 2; + + $schema->storage->txn_begin; + + local *STDOUT; + my $stdout; + + # variable controlling opac hidden status + my $opac_hidden; + + my $auth_mock = Test::MockModule->new('C4::Auth'); + $auth_mock->mock( 'safe_exit', sub { warn "safe_exit_called" } ); + + my $biblio_mock = Test::MockModule->new('Koha::Biblio'); + $biblio_mock->mock( 'hidden_in_opac', sub { return $opac_hidden; } ); + + my $biblio = $builder->build_sample_biblio(); + + my $query = CGI->new(); + + subtest 'not hidden tests' => sub { + + plan tests => 1; + + open STDOUT, '>', \$stdout; + $opac_hidden = 0; + + redirect_if_opac_hidden( $query, $biblio ); + + is( $stdout, undef, 'No redirection if the biblio is not hidden' ); + + close STDOUT; + }; + + subtest 'hidden tests' => sub { + + plan tests => 9; + + $opac_hidden = 1; + + { + open STDOUT, '>', \$stdout; + t::lib::Mocks::mock_preference( 'OpacHiddenRecordRedirect', 1 ); + + warning_is { + redirect_if_opac_hidden( $query, $biblio ); + } + q{safe_exit_called}, + 'Safe exit called on redirection'; + + like( $stdout, qr{Status: 302 Found} ); + like( $stdout, qr{Location: /cgi-bin/koha/opac-blocked.pl\?hidden=1} ); + + undef $stdout; + + close STDOUT; + } + + { + open STDOUT, '>', \$stdout; + t::lib::Mocks::mock_preference( 'OpacHiddenRecordRedirect', 0 ); + + warning_is { + redirect_if_opac_hidden( $query, $biblio ); + } + q{safe_exit_called}, + 'Safe exit called on redirection'; + + like( $stdout, qr{Status: 302 Found} ); + like( $stdout, qr{Location: /cgi-bin/koha/errors/404.pl} ); + + undef $stdout; + + close STDOUT; + } + + { + open STDOUT, '>', \$stdout; + t::lib::Mocks::mock_preference( 'OpacHiddenRecordRedirect', 1 ); + + my $patron = $builder->build_object( + { + class => 'Koha::Patrons', + } + ); + + my $patron2 = $builder->build_object( + { + class => 'Koha::Patrons', + } + ); + + t::lib::Mocks::mock_preference( 'OpacHiddenItemsExceptions', $patron->categorycode ); + + redirect_if_opac_hidden( $query, $biblio, $patron ); + + is( $stdout, undef, 'No redirection if the patron category is excluded from hidden items' ); + + undef $stdout; + + t::lib::Mocks::mock_preference( 'OpacHiddenItemsExceptions', $patron2->categorycode ); + + warning_is { + redirect_if_opac_hidden( $query, $biblio, $patron ); + } + q{safe_exit_called}, + 'Safe exit called on redirection'; + + t::lib::Mocks::mock_preference( 'OpacHiddenItemsExceptions', '' ); + + warning_is { + redirect_if_opac_hidden( $query, $biblio, $patron ); + } + q{safe_exit_called}, + 'Safe exit called on redirection'; + + undef $stdout; + + close STDOUT; + } + }; + + $schema->storage->txn_rollback; +}; + subtest 'redirect_if_opac_suppressed() tests' => sub { plan tests => 2; -- 2.34.1