Skip to content

Commit

Permalink
Convert tests to phpunit (#17639)
Browse files Browse the repository at this point in the history
* Convert tests to phpunit

* Does not seems relevant

* Public/Static providers

* Fix UserTest

* Ensure CFG_GLPI is reset

* Add globals hack added on main to fix some cases

* Exclude temporary failing tests, see #17648

* Fix timezone issue

* Another bunch of phpunit conversions

* New conversions requires more memory to execute

* Fix provider case in atoum tests (does not matter for atoum, but does for phpunit)

* Fix mock

* Try without backing-up static attributeswq

* CFG_GLPI backup is done globally

Co-authored-by: Adrien Clairembault <[email protected]>

* infocom_auto_create does not exist

* Add and use FIXTURE_DIR constant

Fix typo in direcotry name

* CFG_GLPI backup is done globally

Co-authored-by: Adrien Clairembault <[email protected]>

* Fix typo

* Fix software fixtures path

fix cs

* Revert "CFG_GLPI backup is done globally"

This reverts commit dc4c2e8.

# Conflicts:
#	phpunit/functional/Glpi/RichText/RichTextTest.php

---------

Co-authored-by: AdrienClairembault <[email protected]>
Co-authored-by: Adrien Clairembault <[email protected]>
  • Loading branch information
3 people authored Aug 9, 2024
1 parent 1d56b79 commit 020b950
Show file tree
Hide file tree
Showing 72 changed files with 2,990 additions and 2,787 deletions.
3 changes: 1 addition & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="phpunit/bootstrap.php"
backupStaticAttributes="true"
colors="true"
>
<testsuites>
Expand All @@ -12,7 +11,7 @@
</testsuite>
</testsuites>
<php>
<ini name="memory_limit" value="256M" />
<ini name="memory_limit" value="512M" />
</php>
</phpunit>

41 changes: 39 additions & 2 deletions phpunit/GLPITestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class GLPITestCase extends TestCase
private $int;
private $str;
protected $has_failed = false;
private ?array $config_copy = null;

/**
* @var TestHandler
Expand All @@ -59,10 +60,15 @@ class GLPITestCase extends TestCase

public function setUp(): void
{
// By default, no session, not connected
$this->storeGlobals();

global $DB;
$DB->setTimezone('UTC');

// By default, no session, not connected
$this->resetSession();

// Ensure cache is clear
// Ensure cache is clear
global $GLPI_CACHE;
$GLPI_CACHE->clear();

Expand All @@ -79,6 +85,8 @@ public function setUp(): void

public function tearDown(): void
{
$this->resetGlobalsAndStaticValues();

vfsStreamWrapper::unregister();

if (isset($_SESSION['MESSAGE_AFTER_REDIRECT']) && !$this->has_failed) {
Expand Down Expand Up @@ -333,4 +341,33 @@ protected function getTestRootEntity(bool $only_id = false)
{
return getItemByTypeName('Entity', '_test_root_entity', $only_id);
}

/**
* Store Globals
*
* @return void
*/
private function storeGlobals(): void
{
global $CFG_GLPI;

if ($this->config_copy === null) {
$this->config_copy = $CFG_GLPI;
}
}

/**
* Reset globals and static variables
*
* @return void
*/
private function resetGlobalsAndStaticValues(): void
{
// Globals
global $CFG_GLPI;
$CFG_GLPI = $this->config_copy;

// Statics values
Log::$use_queue = false;
}
}
3 changes: 2 additions & 1 deletion phpunit/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,12 @@
define('GLPI_URI', getenv('GLPI_URI') ?: 'http://localhost:8088');
define('GLPI_STRICT_DEPRECATED', true); //enable strict depreciations

define('FIXTURE_DIR', __DIR__ . "/../tests/fixtures");
define(
'PLUGINS_DIRECTORIES',
[
GLPI_ROOT . '/plugins',
GLPI_ROOT . '/tests/fixtures/plugins',
FIXTURE_DIR . '/plugins',
]
);

Expand Down
10 changes: 3 additions & 7 deletions phpunit/functional/ComputerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,14 +345,10 @@ public function testGetFromIter()
$iter = $DB->request(['SELECT' => 'id',
'FROM' => 'glpi_computers'
]);
$prev = false;
foreach (\Computer::getFromIter($iter) as $comp) {
$this->assertInstanceOf(\Computer::class, $comp);
$this->assertArrayHasKey('name', $comp->fields);
$this->assertNotEquals($prev, $comp->fields['name']);
$prev = $comp->fields['name'];
}
$this->assertTrue((bool)$prev); // we are retrieve something
}

public function testGetFromDbByCrit()
Expand Down Expand Up @@ -548,10 +544,10 @@ public function testCloneWithAutoCreateInfocom()
// clone!
$computer = new \Computer(); //$computer->fields contents is already escaped!
$this->assertTrue($computer->getFromDB($id));
$infocom_auto_create_original = $CFG_GLPI["infocom_auto_create"] ?? 0;
$CFG_GLPI["infocom_auto_create"] = 1;
$auto_create_infocoms_original = $CFG_GLPI["auto_create_infocoms"] ?? 0;
$CFG_GLPI["auto_create_infocoms"] = 1;
$added = $computer->clone();
$CFG_GLPI["infocom_auto_create"] = $infocom_auto_create_original;
$CFG_GLPI["auto_create_infocoms"] = $auto_create_infocoms_original;
$this->assertGreaterThan(0, (int)$added);
$this->assertNotEquals($computer->fields['id'], $added);

Expand Down
6 changes: 3 additions & 3 deletions phpunit/functional/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -906,7 +906,7 @@ public function testAutoCreateInfocom()
];
$infocom_types = array_diff($infocom_types, $excluded_types);

$infocom_auto_create_original = $CFG_GLPI["infocom_auto_create"] ?? 0;
$auto_create_infocoms_original = $CFG_GLPI["auto_create_infocoms"] ?? 0;

$infocom = new \Infocom();
foreach ($infocom_types as $asset_type) {
Expand All @@ -919,7 +919,7 @@ public function testAutoCreateInfocom()
'itemtype' => 'Computer', // Random item type for testing Item_DeviceSimcard
'devicesimcards_id' => 1, // Random ID for testing Item_DeviceSimcard
]);
$CFG_GLPI['auto_create_infocoms'] = $infocom_auto_create_original;
$CFG_GLPI['auto_create_infocoms'] = $auto_create_infocoms_original;
// Verify an Infocom object exists for the newly created asset
$infocom_exists = $infocom->getFromDBforDevice($asset_type, $asset_id);
$this->assertTrue($infocom_exists);
Expand All @@ -933,7 +933,7 @@ public function testAutoCreateInfocom()
'itemtype' => 'Computer', // Random item type for testing Item_DeviceSimcard
'devicesimcards_id' => 1, // Random ID for testing Item_DeviceSimcard
]);
$CFG_GLPI['auto_create_infocoms'] = $infocom_auto_create_original;
$CFG_GLPI['auto_create_infocoms'] = $auto_create_infocoms_original;
$infocom_exists = $infocom->getFromDBforDevice($asset_type, $asset_id2);
$this->assertFalse($infocom_exists);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

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

class DatabaseInstance extends DbTestCase
class DatabaseInstanceTest extends DbTestCase
{
public function testDelete()
{
Expand All @@ -51,34 +51,33 @@ public function testDelete()
'size' => 52000
]);

//check DB is created, and load it
$this->integer($instid)->isGreaterThan(0);
$this->boolean($instance->getFromDB($instid))->isTrue();
//check DB is created, and load it
$this->assertGreaterThan(0, $instid);
$this->assertTrue($instance->getFromDB($instid));

//create databases
//create databases
for ($i = 0; $i < 5; ++$i) {
$database = new \Database();
$this->integer(
$this->assertGreaterThan(
0,
$database->add([
'name' => 'Database ' . $i,
'databaseinstances_id' => $instid
])
)->isGreaterThan(0);
);
}
$this->integer(countElementsInTable(\Database::getTable()))->isIdenticalTo(5);
$this->assertSame(5, countElementsInTable(\Database::getTable()));

//test removal
$this->boolean($instance->delete(['id' => $instid], 1))->isTrue();
$this->boolean($instance->getFromDB($instid))->isFalse();
$this->assertTrue($instance->delete(['id' => $instid], 1));
$this->assertFalse($instance->getFromDB($instid));

//ensure databases has been dropped aswell
$this->integer(countElementsInTable(\Database::getTable()))->isIdenticalTo(0);
$this->assertSame(0, countElementsInTable(\Database::getTable()));
}

public function testGetInventoryAgent(): void
{
$root_entity = getItemByTypeName(\Entity::class, '_test_root_entity', true);

$computer = $this->createItem(
\Computer::class,
[
Expand All @@ -96,7 +95,7 @@ public function testGetInventoryAgent(): void
);

$db_agent = $dbinstance->getInventoryAgent();
$this->variable($db_agent)->isNull();
$this->assertNull($db_agent);

$agenttype_id = getItemByTypeName(\AgentType::class, 'Core', true);

Expand All @@ -122,7 +121,7 @@ public function testGetInventoryAgent(): void
]
);

$agent3 = $this->createItem(
$this->createItem(
\Agent::class,
[
'deviceid' => sprintf('device_%08x', rand()),
Expand All @@ -146,23 +145,23 @@ public function testGetInventoryAgent(): void

// most recent agent directly linked
$db_agent = $dbinstance->getInventoryAgent();
$this->object($db_agent)->isInstanceOf(\Agent::class);
$this->array($db_agent->fields)->isEqualTo($agent1->fields);
$this->assertInstanceOf(\Agent::class, $db_agent);
$this->assertEquals($agent1->fields, $db_agent->fields);

$this->boolean($agent1->delete(['id' => $agent1->fields['id']]))->isTrue();
$this->assertTrue($agent1->delete(['id' => $agent1->fields['id']]));

// most recent agent directly linked
$db_agent = $dbinstance->getInventoryAgent();
$this->object($db_agent)->isInstanceOf(\Agent::class);
$this->array($db_agent->fields)->isEqualTo($agent2->fields);
$this->assertInstanceOf(\Agent::class, $db_agent);
$this->assertEquals($agent2->fields, $db_agent->fields);

$this->boolean($agent2->delete(['id' => $agent2->fields['id']]))->isTrue();
$this->assertTrue($agent2->delete(['id' => $agent2->fields['id']]));

// most recent agent found from linked item, as there is no more agent linked directly
$db_agent = $dbinstance->getInventoryAgent();
$this->object($db_agent)->isInstanceOf(\Agent::class);
$this->assertInstanceOf(\Agent::class, $db_agent);
$computer_agent = $computer->getInventoryAgent();
$this->object($computer_agent)->isInstanceOf(\Agent::class);
$this->array($db_agent->fields)->isEqualTo($computer_agent->fields);
$this->assertInstanceOf(\Agent::class, $computer_agent);
$this->assertEquals($computer_agent->fields, $db_agent->fields);
}
}
24 changes: 12 additions & 12 deletions phpunit/functional/DbUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,14 @@ public function testGetTableNameForForeignKeyField($table, $key)
public static function dataTableType()
{
// Pseudo plugin class for test
require_once __DIR__ . '/../../tests/fixtures/another_test.php';
require_once __DIR__ . '/../../tests/fixtures/pluginbarabstractstuff.php';
require_once __DIR__ . '/../../tests/fixtures/pluginbarfoo.php';
require_once __DIR__ . '/../../tests/fixtures/pluginfoobar.php';
require_once __DIR__ . '/../../tests/fixtures/pluginfooservice.php';
require_once __DIR__ . '/../../tests/fixtures/pluginfoo_search_item_filter.php';
require_once __DIR__ . '/../../tests/fixtures/pluginfoo_search_a_b_c_d_e_f_g_bar.php';
require_once __DIR__ . '/../../tests/fixtures/test_a_b.php';
require_once FIXTURE_DIR . '/another_test.php';
require_once FIXTURE_DIR . '/pluginbarabstractstuff.php';
require_once FIXTURE_DIR . '/pluginbarfoo.php';
require_once FIXTURE_DIR . '/pluginfoobar.php';
require_once FIXTURE_DIR . '/pluginfooservice.php';
require_once FIXTURE_DIR . '/pluginfoo_search_item_filter.php';
require_once FIXTURE_DIR . '/pluginfoo_search_a_b_c_d_e_f_g_bar.php';
require_once FIXTURE_DIR . '/test_a_b.php';

return [
['glpi_dbmysqls', 'DBmysql', false], // not a CommonGLPI, should not be valid
Expand Down Expand Up @@ -233,7 +233,7 @@ public function testGetItemForItemtype($itemtype, $is_valid, $expected_class)

public function testGetItemForItemtypeSanitized()
{
require_once __DIR__ . '/../../tests/fixtures/pluginbarfoo.php';
require_once FIXTURE_DIR . '/pluginbarfoo.php';

$instance = new \DbUtils();
$instance->getItemForItemtype(addslashes('Glpi\Event'));
Expand All @@ -245,7 +245,7 @@ public function testGetItemForItemtypeSanitized()

public function testGetItemForItemtypeSanitized2()
{
require_once __DIR__ . '/../../tests/fixtures/pluginbarfoo.php';
require_once FIXTURE_DIR . '/pluginbarfoo.php';

$instance = new \DbUtils();
$instance->getItemForItemtype(addslashes('GlpiPlugin\Bar\Foo'));
Expand All @@ -257,7 +257,7 @@ public function testGetItemForItemtypeSanitized2()

public function testGetItemForItemtypeAbstract()
{
require_once __DIR__ . '/../../tests/fixtures/pluginbarabstractstuff.php';
require_once FIXTURE_DIR . '/pluginbarabstractstuff.php';

$instance = new \DbUtils();
$instance->getItemForItemtype('CommonDevice');
Expand All @@ -269,7 +269,7 @@ public function testGetItemForItemtypeAbstract()

public function testGetItemForItemtypeAbstract2()
{
require_once __DIR__ . '/../../tests/fixtures/pluginbarabstractstuff.php';
require_once FIXTURE_DIR . '/pluginbarabstractstuff.php';

$instance = new \DbUtils();
$instance->getItemForItemtype('GlpiPlugin\Bar\AbstractStuff');
Expand Down
Loading

0 comments on commit 020b950

Please sign in to comment.