Skip to content

Commit

Permalink
User RuleBuilder from tests
Browse files Browse the repository at this point in the history
Add ranking support on RuleBuilder
  • Loading branch information
trasher committed Feb 25, 2025
1 parent 33f59e4 commit 48fadb8
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 103 deletions.
31 changes: 30 additions & 1 deletion phpunit/DbTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,35 @@ protected function getItemsNames(string $itemtype, array $ids): array
}

/**
* Helper method to avoid writting the same boilerplate code for rule creation
* Adds a new rule
*
* @param string $name New rule name
* @param array $criteria Rule criteria
* @param array $action Rule action
*
* @return int
*/
protected function addRule(string $type, string $name, array $criteria, array $action, ?int $ranking = null): int
{
$builder = new \RuleBuilder($name, $type);
if ($ranking !== null) {
$builder->setRanking($ranking);
}

// Add criteria
foreach ($criteria as $crit) {
$builder->addCriteria($crit['criteria'], $crit['condition'], $crit['pattern']);
}

// Add action
$builder->addAction($action['action_type'], $action['field'], $action['value']);

$rule = $this->createRule($builder);
return $rule->getID();
}

/**
* Helper method to avoid writing the same boilerplate code for rule creation
*
* @param RuleBuilder $builder RuleConfiguration
*
Expand All @@ -343,6 +371,7 @@ protected function createRule(RuleBuilder $builder): Rule
'condition' => $builder->getCondition(),
'is_recursive' => $builder->isRecursive(),
'entities_id' => $builder->getEntity(),
'ranking' => $builder->getRanking(),
]);

foreach ($builder->getCriteria() as $criterion) {
Expand Down
25 changes: 25 additions & 0 deletions phpunit/RuleBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ class RuleBuilder
*/
protected string $rule_type;

protected ?int $ranking = null;

/**
* @param string $name Rule name
*/
Expand Down Expand Up @@ -152,6 +154,19 @@ public function setEntity(int $entities_id): self
return $this;
}

/**
* Set rule rank
*
* @param int $ranking
*
* @return self
*/
public function setRanking(int $ranking): self
{
$this->ranking = $ranking;
return $this;
}

/**
* Add criteria
*
Expand Down Expand Up @@ -275,4 +290,14 @@ public function getActions(): array
{
return $this->actions;
}

/**
* Get rule ranking, if defined
*
* @return ?int
*/
public function getRanking(): ?int
{
return $this->ranking;
}
}
69 changes: 4 additions & 65 deletions phpunit/functional/RuleDefineItemtypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,74 +36,9 @@
namespace tests\units;

use DbTestCase;
use PHPUnit\Framework\Attributes\DataProvider;

/* Test for inc/ruleimportcomputer.class.php */

class RuleDefineItemtypeTest extends DbTestCase
{
/**
* Adds a new rule
*
* @param string $name New rule name
* @param array $criteria Rule criteria
* @param array $action Rule action
*
* @return int
*/
protected function addRule(string $name, array $criteria, array $action): int
{
global $DB;

$rule = new \RuleDefineItemtype();
$rulecriteria = new \RuleCriteria();

$input = [
'is_active' => 1,
'name' => $name,
'match' => 'AND',
'sub_type' => 'RuleDefineItemtype',
];

$this->assertTrue(
$DB->update(
'glpi_rules',
[
'ranking' => new \Glpi\DBAL\QueryExpression($DB->quoteName('ranking') . ' + 2')
],
[
'sub_type' => 'RuleDefineItemtype'
]
)
);
$input['ranking'] = 1;
$rules_id = $rule->add($input);
$this->assertGreaterThan(0, $rules_id);

// Add criteria
foreach ($criteria as $crit) {
$input = [
'rules_id' => $rules_id,
'criteria' => $crit['criteria'],
'pattern' => $crit['pattern'],
'condition' => $crit['condition'],
];
$this->assertGreaterThan(0, (int)$rulecriteria->add($input));
}

// Add action
$ruleaction = new \RuleAction();
$input = [
'rules_id' => $rules_id,
'action_type' => $action['action_type'],
'field' => $action['field'],
'value' => $action['value'],
];
$this->assertGreaterThan(0, (int)$ruleaction->add($input));

return $rules_id;
}

public function testNoRule()
{
$input = [
Expand All @@ -125,6 +60,7 @@ public function testChangeItemtype()

$ruleCollection = new \RuleDefineItemtypeCollection();
$rules_id = $this->addRule(
\RuleDefineItemtype::class,
'Change itemtype',
[
[
Expand Down Expand Up @@ -154,6 +90,7 @@ public function testDefineItemtypeFromItemname()
{
$ruleCollection = new \RuleDefineItemtypeCollection();
$rules_id = $this->addRule(
\RuleDefineItemtype::class,
'Change itemtype',
[
[
Expand Down Expand Up @@ -205,6 +142,7 @@ public function testDefineItemtypeFromItemtypeAndMac()

$ruleCollection = new \RuleDefineItemtypeCollection();
$rules_id = $this->addRule(
\RuleDefineItemtype::class,
'Change itemtype',
[
[
Expand Down Expand Up @@ -255,6 +193,7 @@ public function testDefineItemtypeFromMacAndIfnumber()

$ruleCollection = new \RuleDefineItemtypeCollection();
$rules_id = $this->addRule(
\RuleDefineItemtype::class,
'Change itemtype',
[
[
Expand Down
44 changes: 7 additions & 37 deletions phpunit/functional/RuleImportAssetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,11 @@ protected function enableRule($name)
*
* @return void
*/
protected function addRule($name, array $criteria, array $action, $afterRuleName = null)
protected function addAssetRule($name, array $criteria, array $action, $afterRuleName = null): int
{
global $DB;

$rule = new \RuleImportAsset();
$rulecriteria = new \RuleCriteria();

$input = [
'is_active' => 1,
'name' => $name,
'match' => 'AND',
'sub_type' => 'RuleImportAsset',
];

$ruleARN = $rule->find(['name' => $afterRuleName ?? 'Computer constraint (name)'], [], 1);
$this->assertCount(1, $ruleARN);
Expand All @@ -114,34 +106,12 @@ protected function addRule($name, array $criteria, array $action, $afterRuleName
],
[
'ranking' => ['>', $r['ranking']],
'sub_type' => 'RuleImportAsset'
'sub_type' => \RuleImportAsset::class
]
)
);
$input['ranking'] = ($r['ranking'] + 1);
$rules_id = $rule->add($input);
$this->assertGreaterThan(0, $rules_id);

// Add criteria
foreach ($criteria as $crit) {
$input = [
'rules_id' => $rules_id,
'criteria' => $crit['criteria'],
'pattern' => $crit['pattern'],
'condition' => $crit['condition'],
];
$this->assertGreaterThan(0, (int)$rulecriteria->add($input));
}

// Add action
$ruleaction = new \RuleAction();
$input = [
'rules_id' => $rules_id,
'action_type' => $action['action_type'],
'field' => $action['field'],
'value' => $action['value'],
];
$this->assertGreaterThan(0, (int)$ruleaction->add($input));
return parent::addRule(\RuleImportAsset::class, $name, $criteria, $action, $r['ranking'] + 1);
}

public function testCreateComputerName()
Expand Down Expand Up @@ -388,7 +358,7 @@ public function testUpdateComputerMac()
private function addComputerIPRules()
{
// Create rules
$this->addRule(
$this->addAssetRule(
"Computer update (by ip)",
[
[
Expand All @@ -415,7 +385,7 @@ private function addComputerIPRules()
"Computer update (by mac)"
);

$this->addRule(
$this->addAssetRule(
"Computer import (by ip)",
[
[
Expand Down Expand Up @@ -518,7 +488,7 @@ public function testUpdateComputerIP()
private function addComputerIPLinkOnlyRules()
{
// Create rules
$this->addRule(
$this->addAssetRule(
"Computer update (by ip)",
[
[
Expand Down Expand Up @@ -1140,7 +1110,7 @@ public function testCreateComputerSerial_emptyUUID()
private function updateComputerAgentTagRules()
{
// Create rules
$this->addRule(
$this->addAssetRule(
"Computer update (by name and tag)",
[
[
Expand Down

0 comments on commit 48fadb8

Please sign in to comment.