Source
xxxxxxxxxx
* f.e., `templates.templateid`. In case of matching object names (e.g. item inherited from
<?php
/*
** Copyright (C) 2001-2025 Zabbix SIA
**
** This program is free software: you can redistribute it and/or modify it under the terms of
** the GNU Affero General Public License as published by the Free Software Foundation, version 3.
**
** This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
** without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
** See the GNU Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License along with this program.
** If not, see <https://www.gnu.org/licenses/>.
**/
/**
* Class for generating DB objects or API data for tests.
*/
class CTestDataHelper {
private static $objectids = [];
/**
* Create objects using API.
* Usually only the object's "name/key" need to be provided. Some defaults (e.g. item's type, value_type) are mixed
* in and the "parent ID" property (e.g. host's groupid, item's hostid, etc.), if not specified, inferred by using
* the last known such object's ID. Previously defined objects can be linked by their reference ID, f.e.:
* `'master_itemid' => ':item:key.of.master.item'`.
*
* Call CTestDataHelper::cleanUp() to delete objects defined here along with child objects created during the test.
*
* @param array $objects
*/
public static function createObjects(array $objects): void {
$objects += array_fill_keys(['template_groups', 'host_groups', 'templates', 'proxies', 'hosts', 'triggers',
'roles', 'user_groups', 'users', 'scripts', 'drules', 'actions'
], []);
try {
self::createTemplateGroups($objects['template_groups']);
self::createHostGroups($objects['host_groups']);
self::createTemplates($objects['templates']);
self::createProxies($objects['proxies']);
self::createHosts($objects['hosts']);
self::createTriggers($objects['triggers']);
self::createRoles($objects['roles']);
self::createUserGroups($objects['user_groups']);
self::createUsers($objects['users']);
self::createScripts($objects['scripts']);
self::createDrules($objects['drules']);
self::createActions($objects['actions']);
}
catch (Exception $e) {
self::cleanUp();
throw $e;
}
}
/**
* @param array $template_groups
*/
private static function createTemplateGroups(array $template_groups): void {
if (!$template_groups) {
return;
}
$result = CDataHelper::call('templategroup.create', $template_groups);
foreach ($template_groups as $template_group) {
self::$objectids['template_group'][$template_group['name']] = array_shift($result['groupids']);
}
}
/**
* @param array $host_groups
*/
private static function createHostGroups(array $host_groups): void {
if (!$host_groups) {
return;
}
$result = CDataHelper::call('hostgroup.create', $host_groups);
foreach ($host_groups as $host_group) {
self::$objectids['host_group'][$host_group['name']] = array_shift($result['groupids']);
}
}
private static function createProxies(array $proxies): void {
if (!$proxies) {