Skip to content

Commit

Permalink
Migrate more tests to phpunit
Browse files Browse the repository at this point in the history
  • Loading branch information
trasher authored and cedric-anne committed Jan 13, 2025
1 parent c5270ec commit f2f65d0
Show file tree
Hide file tree
Showing 10 changed files with 268 additions and 179 deletions.
50 changes: 44 additions & 6 deletions phpunit/DbTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

use Glpi\Asset\AssetDefinition;
use Glpi\Asset\AssetDefinitionManager;
use Glpi\Dropdown\DropdownDefinition;

class DbTestCase extends \GLPITestCase
{
Expand Down Expand Up @@ -116,8 +117,8 @@ protected function setEntity($entityname, $subtree)
* Generic method to test if an added object is corretly inserted
*
* @param CommonDBTM $object The object to test
* @param int $id The id of added object
* @param array $input the input used for add object (optionnal)
* @param int $id The id of added object
* @param array $input the input used for add object (optionnal)
*
* @return void
*/
Expand Down Expand Up @@ -196,11 +197,12 @@ protected static function getClasses($function = false, array $excludes = [])
/**
* Create an item of the given class
*
* @param string $itemtype
* @template T of CommonDBTM
* @param class-string<T> $itemtype
* @param array $input
* @param array $skip_fields Fields that wont be checked after creation
*
* @return CommonDBTM
* @return T
*/
protected function createItem($itemtype, $input, $skip_fields = []): CommonDBTM
{
Expand Down Expand Up @@ -377,8 +379,9 @@ protected function registerAssetsAutoloader(): void
/**
* Initialize a definition.
*
* @param string $system_name
* @param ?string $system_name
* @param array $capacities
* @param ?array $profiles
*
* @return AssetDefinition
*/
Expand All @@ -404,8 +407,9 @@ protected function initAssetDefinition(
'is_active' => true,
'capacities' => $capacities,
'profiles' => $profiles,
'fields_display' => [],
],
skip_fields: ['capacities', 'profiles'] // JSON encoded fields cannot be automatically checked
skip_fields: ['capacities', 'profiles', 'fields_display'] // JSON encoded fields cannot be automatically checked
);
$this->assertEquals(
$capacities,
Expand All @@ -419,6 +423,40 @@ protected function initAssetDefinition(
return $definition;
}

/**
* Initialize a definition.
*
* @param ?string $system_name
* @param ?array $profiles
*
* @return DropdownDefinition
*/
protected function initDropdownDefinition(
?string $system_name = null,
?array $profiles = null,
): DropdownDefinition {
if ($profiles === null) {
// Initialize with all standard rights for super admin profile
$superadmin_p_id = getItemByTypeName(Profile::class, 'Super-Admin', true);
$profiles = [
$superadmin_p_id => ALLSTANDARDRIGHT,
];
}

$definition = $this->createItem(
DropdownDefinition::class,
[
'system_name' => $system_name ?? $this->getUniqueString(),
'is_active' => true,
'profiles' => $profiles,
],
skip_fields: ['profiles'] // JSON encoded fields cannot be automatically checked
);
$this->assertEquals($profiles, $this->callPrivateMethod($definition, 'getDecodedProfilesField'));

return $definition;
}

/**
* Create a random text document.
* @return \Document
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
use Glpi\Form\Form;
use Glpi\Http\Response;

class CommonAjaxController extends DbTestCase
class CommonAjaxControllerTest extends DbTestCase
{
/**
* Data provider for the testHandleRequest method
Expand Down Expand Up @@ -371,46 +371,41 @@ protected function testHandleRequestProvider(): iterable
/**
* Test the handleRequest method
*
* @dataProvider testHandleRequestProvider
*
* @param array $input Request content
* @param Response $expected_response Expected response object
* @param string $user Logged in user:
* - TU_USER (default)
* - "normal"
*
* @return void
*/
public function testHandleRequest(
array $input,
Response $expected_response,
string $user = TU_USER,
): void {
if ($user === TU_USER) {
$this->login();
} elseif ($user === "normal") {
$this->login("normal", "normal");
}
public function testHandleRequest(): void
{
foreach ($this->testHandleRequestProvider() as $row) {
$input = $row['input'];
$expected_response = $row['expected_response'];
$user = $row['user'] ?? TU_USER;

if ($user === TU_USER) {
$this->login();
} elseif ($user === "normal") {
$this->login("normal", "normal");
}

$controller = new \Glpi\Controller\CommonAjaxController();
$response = $controller->handleRequest($input);
$controller = new \Glpi\Controller\CommonAjaxController();
$response = $controller->handleRequest($input);

// Validate return code
$this
->integer($response->getStatusCode())
->isEqualTo($expected_response->getStatusCode())
;
// Validate return code
$this->assertEquals(
$expected_response->getStatusCode(),
$response->getStatusCode()
);

// Validate body
$this
->array($response->getHeaders())
->isEqualTo($expected_response->getHeaders())
;
// Validate headers
$this->assertEquals(
$expected_response->getHeaders(),
$response->getHeaders()
);

// Validate headers
$this
->string((string) $response->getBody())
->isEqualTo((string) $expected_response->getBody())
;
// Validate body
$this->assertEquals(
(string)$expected_response->getBody(),
(string)$response->getBody()
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
use DbTestCase;
use Glpi\Dropdown\Dropdown;

class DropdownDefinitionManager extends DbTestCase
class DropdownDefinitionManagerTest extends DbTestCase
{
public function testLoadConcreteClass(): void
{
Expand All @@ -50,8 +50,8 @@ public function testLoadConcreteClass(): void
}

foreach ($mapping as $expected_classname => $definition) {
$this->boolean(class_exists($expected_classname))->isTrue();
$this->array($expected_classname::getDefinition()->fields)->isEqualTo($definition->fields);
$this->assertTrue(class_exists($expected_classname));
$this->assertEquals($definition->fields, $expected_classname::getDefinition()->fields);
}
}

Expand All @@ -76,6 +76,6 @@ public function testStandardDropdownRegistration(): void
break;
}
}
$this->boolean($has_dropdown)->isTrue();
$this->assertTrue($has_dropdown);
}
}
Loading

0 comments on commit f2f65d0

Please sign in to comment.