Description | manuals and libraries |
Kernel::System::UnitTest::Helper - unit test helper functions
construct a helper object.
use Kernel::System::ObjectManager;
local $Kernel::OM = Kernel::System::ObjectManager->new(
'Kernel::System::UnitTest::Helper' => {
RestoreDatabase => 1, # runs the test in a transaction,
# and roll it back in the destructor
#
# NOTE: Rollback does not work for
# changes in the database layout. If you
# want to do this in your tests, you cannot
# use this option and must handle the rollback
# yourself.
},
);
my $Helper = $Kernel::OM->Get('Kernel::System::UnitTest::Helper');
creates a random ID that can be used in tests as a unique identifier.
It is guaranteed that within a test this function will never return a duplicate.
Please note that these numbers are not really random and should only be used to create test data.
creates a random Number that can be used in tests as a unique identifier.
It is guaranteed that within a test this function will never return a duplicate.
Please note that these numbers are not really random and should only be used to create test data.
creates a test user that can be used in tests. It will be set to invalid automatically during "DESTROY()". Returns the login name of the new user, the password is the same.
my $TestUserLogin = $Helper->TestUserCreate(
Groups => ['admin', 'users'], # optional, list of groups to add this user to (rw rights)
Language => 'de' # optional, defaults to 'en' if not set
);
creates a test customer user that can be used in tests. It will be set to invalid automatically during "DESTROY()". Returns the login name of the new customer user, the password is the same.
my $TestUserLogin = $Helper->TestCustomerUserCreate(
Language => 'de', # optional, defaults to 'en' if not set
);
$Helper->BeginWork()
Starts a database transaction (in order to isolate the test from the static database).
$Helper->Rollback()
Rolls back the current database transaction.
returns a host name for HTTP based tests, possibly including the port.
makes it possible to override the system time as long as this object lives. You can pass an optional time parameter that should be used, if not, the current system time will be used.
All calls to methods of Kernel::System::Time and Kernel::System::DateTime will use the given time afterwards.
$HelperObject->FixedTimeSet(366475757); # with Timestamp
$HelperObject->FixedTimeSet($DateTimeObject); # with previously created DateTime object
$HelperObject->FixedTimeSet(); # set to current date and time
Returns: Timestamp
restores the regular system time behavior.
adds a number of seconds to the fixed system time which was previously set by FixedTimeSet(). You can pass a negative value to go back in time.
performs various clean-ups.
temporarily change a configuration setting system wide to another value, both in the current ConfigObject and also in the system configuration on disk.
This will be reset when the Helper object is destroyed.
Please note that this will not work correctly in clustered environments.
$Helper->ConfigSettingChange(
Valid => 1, # (optional) enable or disable setting
Key => 'MySetting', # setting name
Value => { ... } , # setting value
);
Temporarily include custom code in the system. For example, you may use this to redefine a subroutine from another class. This change will persist for remainder of the test.
All code will be removed when the Helper object is destroyed.
Please note that this will not work correctly in clustered environments.
$Helper->CustomCodeActivate(
Code => q^
sub Kernel::Config::Files::ZZZZUnitTestIdentifier::Load {} # no-op, avoid warning logs
use Kernel::System::WebUserAgent;
package Kernel::System::WebUserAgent;
use strict;
use warnings;
{
no warnings 'redefine';
sub Request {
my $JSONString = '{"Results":{},"ErrorMessage":"","Success":1}';
return (
Content => \$JSONString,
Status => '200 OK',
);
}
}
1;^,
Identifier => 'News', # (optional) Code identifier to include in file name
);
Remove all custom files from ConfigSettingChange()
and CustomCodeActivate()
.
switch the article storage directory to a temporary one to prevent collisions;
Disable scheduling of asynchronous tasks using AsynchronousExecutor
component of OTRS daemon.
Provide temporary database for the test. Please first define test database settings in Config.pm
, i.e:
$Self->{TestDatabase} = {
DatabaseDSN => 'DBI:mysql:database=otrs_test;host=127.0.0.1;',
DatabaseUser => 'otrs_test',
DatabasePw => 'otrs_test',
};
The method call will override global database configuration for duration of the test, i.e. temporary database will receive all calls sent over system DBObject
.
All database contents will be automatically dropped when the Helper object is destroyed.
$Helper->ProvideTestDatabase(
DatabaseXMLString => $XML, # (optional) OTRS database XML schema to execute
# or
DatabaseXMLFiles => [ # (optional) List of XML files to load and execute
'/opt/otrs/scripts/database/otrs-schema.xml',
'/opt/otrs/scripts/database/otrs-initial_insert.xml',
],
);
This method returns 'undef' in case the test database is not configured. If it is configured, but the supplied XML cannot be read or executed, this method will die()
to interrupt the test with an error.
Clears temporary database used in the test. Always call ProvideTestDatabase()
called first, in order to set it up.
Please note that all database contents will be dropped, USE WITH CARE!
$Helper->TestDatabaseCleanup();
Execute supplied XML against current database. Content of supplied XML or XMLFilename parameter must be valid OTRS database XML schema.
$Helper->DatabaseXMLExecute(
XML => $XML, # OTRS database XML schema to execute
);
Alternatively, it can also load an XML file to execute:
$Helper->DatabaseXMLExecute(
XMLFile => '/path/to/file', # OTRS database XML file to execute
);
This software is part of the OTRS project (https://otrs.org/).
This software comes with ABSOLUTELY NO WARRANTY. For details, see the enclosed file COPYING for license information (GPL). If you did not receive this file, see https://www.gnu.org/licenses/gpl-3.0.txt.