NAME
Kernel::Test::Role::ProvidesTestData::Base – Role which holds the base functionality for the data providers.
PUBLIC INTERFACE
Provides some base functionality for the single data provider roles.
HANDLING STATIC TEST DATA
Every own data provider needs to define the following functions:
– TestDataCreateObjectType
The TestDataCreate-Function handles the creation of new object for the current object type. We should provide default values for the different keys, so that we don't need to provide all needed keys from outside. In the create function for the object we should add the "%Param", so that we can override all default values from outside, if this is needed. For values which have an own data provider we should add some code that this objects will be created inside the data provider (for example a new queue in the ticket data provider). The object types which have special values which needs to be added in a manual step (e.g. user or service preferences) should handle this with a special key in the parameters, for example Preferences
. After the creation we need to fetch the data for the created object and save this in the global TestData
-Attribute.
For example: $Self->TestData()->{Ticket}->{$TicketID} = \%Ticket;
At the end we need to return the complete data.
– _TestDataCleanupObjectType
In the cleanup function all related stuff for the current object type needs to be removed for every value inside the "$Self->TestData()->{ObjectType
}"-Array.
Extending of the 'FinishHook' for the data cleanup. Here is an example code, which can normally be used in every new data provider. Only the correct cleanup function needs to be called:
around 'FinishHook' => sub { my ( $Original, $Self, %Param ) = @_;
# Don't perform clean-up if we are in a transaction.
if ( !$Self->does('Kernel::Test::Role::Environment::RestoreDatabase') ) {
$Self->_TestDataCleanupC<ObjectType>();
}
$Self->$Original(%Param);
};
For special object types it's also possible to create additional helper functions for this object (for example a helper function which set a dynamic field value for an given object id).
has TestData
Attribute that holds all created test data in the following structure:
{
Ticket => {
123 => {
<Result of C<TicketGet()>>,
},
},
Queue => {
456 => {
<Result of C<QueueGet()>>
},
},
Group => {
789 => {
<Result of C<GroupGet()>>
},
},
},
TestDataGet()
Fetches created test data.
my $Ticket = $Self->TestDataGet(
ObjectName => 'Ticket',
ObjectID => 123,
);
Returns undef if no test data is found for the given parameters or the found test data as hash ref.
TestDataAdd()
Adds test data (e.g. for later clean up).
my $Success = $Self->TestDataAdd(
ObjectName => 'Ticket',
ObjectID => 123,
ObjectData => { ... },
);
Returns true.
TestDataDelete()
Delete test data (e.g. because it was deleted via GUI).
my $Success = $Self->TestDataDelete(
ObjectName => 'Ticket',
ObjectID => 123,
);
Returns true if deletion in test data was successful, false otherwise.
TestDataCleanup()
Cleanup the test data for given object name.
my $Success = $Self->TestDataCleanup(
ObjectName => 'Ticket',
);
Returns true if cleanup was successful, false otherwise.