Skip to content

Commit

Permalink
Feature/phpunit 4 (#17700)
Browse files Browse the repository at this point in the history
* Migrate Search tests

* Fix test :/

* phpunit conversions

* Fix tests

* Use same instructions everywhere

* Fix SessionTest

* Revert session tests conversion.

* Fix static SO issue

* Fix CronTask tests
  • Loading branch information
trasher authored Aug 23, 2024
1 parent 137fa6f commit 546c20d
Show file tree
Hide file tree
Showing 35 changed files with 2,607 additions and 2,003 deletions.
258 changes: 258 additions & 0 deletions phpunit/RuleBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,258 @@
<?php

/**
* ---------------------------------------------------------------------
*
* GLPI - Gestionnaire Libre de Parc Informatique
*
* http://glpi-project.org
*
* @copyright 2015-2024 Teclib' and contributors.
* @copyright 2003-2014 by the INDEPNET Development Team.
* @licence https://www.gnu.org/licenses/gpl-3.0.html
*
* ---------------------------------------------------------------------
*
* LICENSE
*
* This file is part of GLPI.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* ---------------------------------------------------------------------
*/

/**
* Helper class to configurate rule creation in DbTestCase::createRule()
*/
class RuleBuilder
{
/**
* @property string $name Rule name
*/
protected string $name;

/**
* @property string $operator 'AND' or 'OR'
*/
protected string $operator;

/**
* @property int $condition RuleTicket::ONADD and/or RuleTicket::ONUPDATE (bitmask)
*/
protected int $condition;

/**
* @property bool $is_recursive
*/
protected bool $is_recursive;

/**
* @property int $entities_id
*/
protected int $entities_id;

/**
* @property array $criteria
*/
protected array $criteria;

/**
* @property array $actions
*/
protected array $actions;

/**
* @param string $name Rule name
*/
public function __construct(string $name)
{
$this->name = $name;

// Default values
$this->operator = "AND";
$this->condition = RuleTicket::ONADD | RuleTicket::ONUPDATE;
$this->is_recursive = true;
$this->entities_id = getItemByTypeName(Entity::class, '_test_root_entity', true);
$this->criteria = [];
$this->actions = [];
}

/**
* Set condition
*
* @param string $operator RuleTicket::ONADD and/or RuleTicket::ONUPDATE
*
* @return self
*/
public function setCondtion(int $condition): self
{
$this->condition = $condition;
return $this;
}

/**
* Set operator
*
* @param string $operator 'AND' or 'OR'
*
* @return self
*/
public function setOperator(string $operator): self
{
$this->operator = $operator;
return $this;
}

/**
* Set entity configuration
*
* @param bool $is_recursive
*
* @return self
*/
public function setIsRecursive(int $is_recursive): self
{
$this->is_recursive = $is_recursive;
return $this;
}

/**
* Set entity configuration
*
* @param int $entities_id
*
* @return self
*/
public function setEntity(int $entities_id): self
{
$this->entities_id = $entities_id;
return $this;
}

/**
* Add criteria
*
* @param string $criteria
* @param int $condition Rule::PATTERN_IS, ...
* @param mixed $pattern
*
* @return self
*/
public function addCriteria(
string $criteria,
int $condition,
$pattern
): self {
$this->criteria[] = [
'criteria' => $criteria,
'condition' => $condition,
'pattern' => $pattern,
];
return $this;
}

/**
* Add action
*
* @param string $action_type
* @param string $field
* @param mixed $value
*
* @return self
*/
public function addAction(
string $action_type,
string $field,
$value
): self {
$this->actions[] = [
'action_type' => $action_type,
'field' => $field,
'value' => $value,
];
return $this;
}


/**
* Get rule name
*
* @return string Rule name
*/
public function getName(): string
{
return $this->name;
}

/**
* Get rule operator
*
* @return string 'AND' or 'OR'
*/
public function getOperator(): string
{
return $this->operator;
}

/**
* Get rule condition
*
* @return int RuleTicket::ONADD and/or RuleTicket::ONUPDATE (bitmask)
*/
public function getCondition(): int
{
return $this->condition;
}

/**
* Get rule entity configuration
*
* @return bool
*/
public function isRecursive(): bool
{
return $this->is_recursive;
}

/**
* Get rule entity configuration
*
* @return int
*/
public function getEntity(): int
{
return $this->entities_id;
}

/**
* Get rule criteria
*
* @return array
*/
public function getCriteria(): array
{
return $this->criteria;
}

/**
* Get rule actions
*
* @return array
*/
public function getActions(): array
{
return $this->actions;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@

abstract class AbstractITILChildTemplate extends \DbTestCase
{
abstract protected function getInstance(): \AbstractITILChildTemplate;

public function testGetRenderedContent()
{
global $CFG_GLPI;

$this->login();

$template = $this->newTestedInstance;
$template = $this->getInstance();
$template->fields['content'] = <<<TPL
Itemtype: {{ itemtype }}
{%if itemtype == 'Ticket' %}{{ ticket.link|raw }}{% endif %}
Expand All @@ -56,39 +58,42 @@ public function testGetRenderedContent()
'content' => '<p>test content</p>',
'entities_id' => getItemByTypeName('Entity', '_test_child_2', true),
]);
$this->string($template->getRenderedContent($change))
->isEqualTo(<<<HTML
$this->assertEquals(
<<<HTML
Itemtype: Change
<a href="{$CFG_GLPI['root_doc']}/front/change.form.php?id={$change->fields['id']}" title="test change">test change</a>
HTML
HTML,
$template->getRenderedContent($change)
);

$problem = $this->createItem('Problem', [
'name' => 'test problem',
'content' => '<p>test content</p>',
'entities_id' => getItemByTypeName('Entity', '_test_child_2', true),
]);
$this->string($template->getRenderedContent($problem))
->isEqualTo(<<<HTML
$this->assertEquals(
<<<HTML
Itemtype: Problem
<a href="{$CFG_GLPI['root_doc']}/front/problem.form.php?id={$problem->fields['id']}" title="test problem">test problem</a>
HTML
HTML,
$template->getRenderedContent($problem)
);

$ticket = $this->createItem('Ticket', [
'name' => 'test ticket',
'content' => '<p>test content</p>',
'entities_id' => getItemByTypeName('Entity', '_test_child_2', true),
]);
$this->string($template->getRenderedContent($ticket))
->isEqualTo(<<<HTML
$this->assertEquals(
<<<HTML
Itemtype: Ticket
<a href="{$CFG_GLPI['root_doc']}/front/ticket.form.php?id={$ticket->fields['id']}" title="test ticket">test ticket</a>
HTML
HTML,
$template->getRenderedContent($ticket)
);
}

protected function prepareInputProvider(): iterable
public static function prepareInputProvider(): iterable
{
yield [
'content' => '{{ itemtype }}',
Expand All @@ -115,13 +120,13 @@ public function testPrepareInputForAdd(string $content, bool $is_valid, ?string
{
$this->login();

$template = $this->newTestedInstance;
$template = $this->getInstance();

$result = $template->add(['content' => $content]);
if ($is_valid) {
$this->integer($result)->isGreaterThan(0);
$this->assertGreaterThan(0, $result);
} else {
$this->boolean($result)->isFalse();
$this->assertFalse($result);
$this->hasSessionMessages(ERROR, [$error]);
}
}
Expand All @@ -133,13 +138,13 @@ public function testPrepareInputForUpdate(string $content, bool $is_valid, ?stri
{
$this->login();

$template = $this->newTestedInstance;
$template = $this->getInstance();

$template_id = $template->add(['content' => 'test']);
$this->integer($template_id)->isGreaterThan(0);
$this->assertGreaterThan(0, $template_id);

$result = $template->update(['id' => $template_id, 'content' => $content]);
$this->boolean($result)->isEqualTo($is_valid);
$this->assertEquals($is_valid, $result);
if (!$is_valid) {
$this->hasSessionMessages(ERROR, [$error]);
}
Expand Down
3 changes: 2 additions & 1 deletion phpunit/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@
//include_once __DIR__ . '/CsvTestCase.php';
//include_once __DIR__ . '/APIBaseClass.php';
//include_once __DIR__ . '/FrontBaseClass.php';
include_once __DIR__ . '/RuleBuilder.php';
include_once __DIR__ . '/InventoryTestCase.php';
//include_once __DIR__ . '/functional/CommonITILRecurrent.php';
include_once __DIR__ . '/functional/CommonITILRecurrentTest.php';
//include_once __DIR__ . '/functional/Glpi/ContentTemplates/Parameters/AbstractParameters.php';
include_once __DIR__ . '/AbstractRightsDropdown.php';

Expand Down
Loading

0 comments on commit 546c20d

Please sign in to comment.