Skip to content

Commit

Permalink
Merge pull request #831 from RotherOSS/issue-#761-fix_test_suite
Browse files Browse the repository at this point in the history
Issue #761 fix test suite
  • Loading branch information
bschmalhofer committed Mar 1, 2021
2 parents d710394 + c6e5e41 commit e6165e5
Show file tree
Hide file tree
Showing 15 changed files with 172 additions and 113 deletions.
14 changes: 9 additions & 5 deletions Kernel/System/UnitTest/RegisterDriver.pm
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@ package Kernel::System::UnitTest::RegisterDriver;

=head1 NAME
Kernel::System::UnitTest::RegisterDriver - another helper for unit tests
Kernel::System::UnitTest::RegisterDriver - setup for test scripts
=head1 SYNOPSIS
# Set up the test driver $Self when we are running as a standalone script.
use if __PACKAGE__ ne 'Kernel::System::UnitTest::Driver', 'Kernel::System::UnitTest::RegisterDriver';
# Set up the test driver $Self and $Kernel::OM in test scripts.
use Kernel::System::UnitTest::RegisterDriver;
=head1 DESCRIPTION
Support for running test scripts as standalone scripts.
This script provides support for running test scripts as standalone scripts.
It sets up the variables C<$main::Self> and C<$Kernel::OM>.
=cut

Expand All @@ -50,12 +51,15 @@ sub import { ## no critic qw(OTOBO::RequireCamelCase)
# This means that we don't have to localize $Kernel::OM.
# This is good, we are in a subroutine that does not eval the test script.
$Kernel::OM = Kernel::System::ObjectManager->new(

# Log to an identifiable logfile.
'Kernel::System::Log' => {
LogPrefix => 'OTOBO-otobo.UnitTest',
},
);

# provide $Self in the test scripts
# Provide $Self in the test script.
# $Self is primarily used for methods like $Self->Is() or $Self->True().
$main::Self = $Kernel::OM->Get('Kernel::System::UnitTest::Driver');

return;
Expand Down
11 changes: 7 additions & 4 deletions Kernel/System/Web/Request.pm
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use namespace::autoclean;
# core modules

# CPAN modules
use CGI;

# OTOBO modules
use Kernel::System::VariableCheck qw(:all);
Expand Down Expand Up @@ -103,9 +104,11 @@ sub new {
$Self->{Query} = $Param{WebRequest};
}

# there is no fallback that would e.g. assume the CGI %ENV variables
# Use an empty CGI object as a fallback.
# This is needed because the ParamObject is sometimes created outside a web context.
# Pass an empty string, in order to avoid that params in %ENV are considered.
else {
die 'Bailing out as neither PSGIEnv nor WebRequest was passed!';
$Self->{Query} = CGI->new('');
}

return $Self;
Expand Down Expand Up @@ -146,7 +149,7 @@ sub GetParam {
my $Value = $Self->{Query}->param( $Param{Param} );

# Fallback to query string for mixed requests.
if ( ! defined $Value ) {
if ( !defined $Value ) {
my $RequestMethod = $Self->{Query}->request_method() // '';
if ( $RequestMethod eq 'POST' ) {
$Value = $Self->{Query}->url_param( $Param{Param} );
Expand Down Expand Up @@ -234,7 +237,7 @@ sub GetArray {
my @Values = $Self->{Query}->multi_param( $Param{Param} );

# Fallback to query string for mixed requests.
if ( ! @Values ) {
if ( !@Values ) {
my $RequestMethod = $Self->{Query}->request_method() // '';
if ( $RequestMethod eq 'POST' ) {
@Values = $Self->{Query}->url_param( $Param{Param} );
Expand Down
47 changes: 14 additions & 33 deletions scripts/test/Console/Command/Maint/SupportBundle/Generate.t
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,21 @@ use strict;
use warnings;
use utf8;

# Set up the test driver $Self when we are running as a standalone script.
use Kernel::System::UnitTest::RegisterDriver;
# core modules

use vars (qw($Self));
# CPAN modules
use CGI;
use Test2::V0;

# Work around a Perl bug that is triggered in Carp
# (Bizarre copy of HASH in list assignment at /usr/share/perl5/vendor_perl/Carp.pm line 229).
#
#   See https://rt.perl.org/Public/Bug/Display.html?id=52610 and
#   http://rt.perl.org/rt3/Public/Bug/Display.html?id=78186
# OTOBO modules
use Kernel::System::ObjectManager;

no warnings 'redefine'; ## no critic qw(TestingAndDebugging::ProhibitNoWarnings)
use Carp;
local *Carp::caller_info = sub { };
use warnings 'redefine';

$Kernel::OM->ObjectParamAdd(
'Kernel::System::UnitTest::Helper' => {
RestoreDatabase => 1,
},
# give the SupportDataCollector an empty HTTP request
local $Kernel::OM = Kernel::System::ObjectManager->new(
'Kernel::System::Web::Request' => { WebRequest => CGI->new() }
);
my $Helper = $Kernel::OM->Get('Kernel::System::UnitTest::Helper');

plan(2);

my $TargetDirectory = $Kernel::OM->Get('Kernel::Config')->Get('Home') . '/var/tmp';

Expand All @@ -54,31 +47,19 @@ foreach my $File (@SupportFiles) {

my $CommandObject = $Kernel::OM->Get('Kernel::System::Console::Command::Maint::SupportBundle::Generate');

# Run the console command and get its exit code as a result.
# Run the console command and get its exit code as a result. 0 indicates success.
my $ExitCode = $CommandObject->Execute( '--target-directory', $TargetDirectory );

$Self->Is(
$ExitCode,
0,
'Maint::SupportBundle::Generate exit code'
);
is( $ExitCode, 0, 'Maint::SupportBundle::Generate exit code' );

@SupportFiles = $Kernel::OM->Get('Kernel::System::Main')->DirectoryRead(
Directory => $TargetDirectory,
Filter => 'SupportBundle_*.tar.gz',
);

$Self->Is(
scalar @SupportFiles,
1,
'Support bundle generated'
);
is( scalar @SupportFiles, 1, 'Support bundle generated' );

# Remove generated support files.
foreach my $File (@SupportFiles) {
unlink $File;
}

# Cleanup cache is done by RestoreDatabase.

$Self->DoneTesting();
21 changes: 15 additions & 6 deletions scripts/test/DynamicField/Driver/WebService/FormDataGet.t
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,18 @@ use strict;
use warnings;
use utf8;

# Set up the test driver $Self when we are running as a standalone script.
use Kernel::System::UnitTest::RegisterDriver;
# core modules
use URI::Escape;

use vars (qw($Self));
# CPAN modules
use CGI;

use URI::Escape;
# OTOBO modules
use Kernel::System::UnitTest::RegisterDriver;

my $Helper = $Kernel::OM->Get('Kernel::System::UnitTest::Helper');
our $Self;

my $Helper = $Kernel::OM->Get('Kernel::System::UnitTest::Helper');
my $ConfigObject = $Kernel::OM->Get('Kernel::Config');

# Disable email address checks
Expand Down Expand Up @@ -108,7 +111,13 @@ for my $Test (@Tests) {
QUERY_STRING => $Test->{Request} // '',
);

CGI->initialize_globals();
# force the ParamObject to use the new request params
CGI::initialize_globals();
$Kernel::OM->ObjectParamAdd(
'Kernel::System::Web::Request' => {
WebRequest => CGI->new(),
}
);

# _FormDataGet() implicitly calls Kernel::System::Web::Request->new();
my $FormData = $DynamicFieldDriverObject->_FormDataGet();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ my $DynamicFieldDeleteSuccess = $DynamicFieldObject->DynamicFieldDelete(
ID => $DynamicFieldID,
UserID => 1,
);
ok( $DynamicFieldDelete, "Database dynamic field ID $DynamicFieldID - deleted" );
ok( $DynamicFieldDeleteSuccess, "Database dynamic field ID $DynamicFieldID - deleted" );

my $WebserviceDeleteSuccess = $WebserviceObject->WebserviceDelete(
ID => $WebserviceID,
Expand All @@ -639,7 +639,7 @@ my $WebserviceDeleteSuccess = $WebserviceObject->WebserviceDelete(
ok( $WebserviceDeleteSuccess, "Web Service ID $WebserviceID - deleted" );

# Make sure the cache is correct.
for my $Cache ( qw(Ticket DynamicField Webservice) ) {
for my $Cache (qw(Ticket DynamicField Webservice)) {
$Kernel::OM->Get('Kernel::System::Cache')->CleanUp(
Type => $Cache,
);
Expand Down
12 changes: 10 additions & 2 deletions scripts/test/DynamicField/ObjectType/Article/ObjectDataGet.t
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ use strict;
use warnings;
use utf8;

use CGI;

# Set up the test driver $Self when we are running as a standalone script.
use Kernel::System::UnitTest::RegisterDriver;

use vars (qw($Self));
our $Self;

# Get helper object
$Kernel::OM->ObjectParamAdd(
Expand Down Expand Up @@ -215,7 +217,13 @@ for my $Test (@Tests) {
QUERY_STRING => $Test->{Request} // '',
);

CGI->initialize_globals();
# force the ParamObject to use the new request params
CGI::initialize_globals();
$Kernel::OM->ObjectParamAdd(
'Kernel::System::Web::Request' => {
WebRequest => CGI->new(),
}
);

# implicitly call Kernel::System::Web::Request->new();
my %ObjectData = $ObjectHandlerObject->ObjectDataGet( %{ $Test->{Config} } );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ use strict;
use warnings;
use utf8;

use CGI;

# Set up the test driver $Self when we are running as a standalone script.
use Kernel::System::UnitTest::RegisterDriver;

use vars (qw($Self));
our $Self;

# Get helper object
$Kernel::OM->ObjectParamAdd(
Expand Down Expand Up @@ -152,12 +154,19 @@ my $ObjectHandlerObject = $Kernel::OM->Get('Kernel::System::DynamicField::Object
TEST:
for my $Test (@Tests) {

# will be picked up in CGI->new()
local %ENV = (
REQUEST_METHOD => 'GET',
QUERY_STRING => $Test->{Request} // '',
);

CGI->initialize_globals();
# force the ParamObject to use the new request params
CGI::initialize_globals();
$Kernel::OM->ObjectParamAdd(
'Kernel::System::Web::Request' => {
WebRequest => CGI->new(),
}
);

# implicitly call Kernel::System::Web::Request->new();
my %ObjectData = $ObjectHandlerObject->ObjectDataGet( %{ $Test->{Config} } );
Expand Down
12 changes: 10 additions & 2 deletions scripts/test/DynamicField/ObjectType/CustomerUser/ObjectDataGet.t
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ use strict;
use warnings;
use utf8;

use CGI;

# Set up the test driver $Self when we are running as a standalone script.
use Kernel::System::UnitTest::RegisterDriver;

use vars (qw($Self));
our $Self;

# Get helper object
$Kernel::OM->ObjectParamAdd(
Expand Down Expand Up @@ -156,7 +158,13 @@ for my $Test (@Tests) {
QUERY_STRING => $Test->{Request} // '',
);

CGI->initialize_globals();
# force the ParamObject to use the new request params
CGI::initialize_globals();
$Kernel::OM->ObjectParamAdd(
'Kernel::System::Web::Request' => {
WebRequest => CGI->new(),
}
);

# implicitly call Kernel::System::Web::Request->new();
my %ObjectData = $ObjectHandlerObject->ObjectDataGet( %{ $Test->{Config} } );
Expand Down
13 changes: 11 additions & 2 deletions scripts/test/DynamicField/ObjectType/Ticket/ObjectDataGet.t
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ use strict;
use warnings;
use utf8;

use CGI;

# Set up the test driver $Self when we are running as a standalone script.
use Kernel::System::UnitTest::RegisterDriver;

use vars (qw($Self));
our $Self;

# Get helper object
$Kernel::OM->ObjectParamAdd(
Expand Down Expand Up @@ -160,7 +162,14 @@ for my $Test (@Tests) {
QUERY_STRING => $Test->{Request} // '',
);

CGI->initialize_globals();
# force the ParamObject to use the new request params
CGI::initialize_globals();
$Kernel::OM->ObjectParamAdd(
'Kernel::System::Web::Request' => {
WebRequest => CGI->new(),
}
);


# implicitly call Kernel::System::Web::Request->new();
my %ObjectData = $ObjectHandlerObject->ObjectDataGet( %{ $Test->{Config} } );
Expand Down
Loading

0 comments on commit e6165e5

Please sign in to comment.