Skip to content

Commit

Permalink
Conversion to phpunit
Browse files Browse the repository at this point in the history
  • Loading branch information
trasher committed Aug 30, 2024
1 parent e0b9650 commit 7432c86
Show file tree
Hide file tree
Showing 61 changed files with 5,032 additions and 4,701 deletions.
5 changes: 2 additions & 3 deletions phpunit/DbTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,8 @@ protected function login(
*/
protected function logOut()
{
$ctime = $_SESSION['glpi_currenttime'];
\Session::destroy();
$_SESSION['glpi_currenttime'] = $ctime;
\Session::start();
}

/**
Expand Down Expand Up @@ -119,7 +118,7 @@ protected function checkInput(CommonDBTM $object, $id = 0, $input = [])
{
$input = Sanitizer::dbUnescapeRecursive($input); // slashes in input should not be stored in DB

$this->assertGreaterThan(0, (int)$id);
$this->assertGreaterThan($object instanceof Entity ? -1 : 0, (int)$id);
$this->assertTrue($object->getFromDB($id));
$this->assertEquals($id, $object->getField('id'));

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ abstract class AbstractPlanningEvent extends \DbTestCase
protected $exdate1 = "";
protected $exdate2 = "";

public function beforeTestMethod($method)
public function setUp(): void
{
parent::beforeTestMethod($method);
parent::setUp();

$this->now = time();
$this->duration = 2 * \HOUR_TIMESTAMP;
Expand Down Expand Up @@ -82,18 +82,20 @@ public function testAdd()
$event = new $this->myclass();
$id = $event->add($this->input);

$this->integer((int) $id)->isGreaterThan(0);
$this->boolean($event->getFromDB($id))->isTrue();
$this->assertGreaterThan(0, (int)$id);
$this->assertTrue($event->getFromDB($id));

// check end date
if (isset($event->fields['end'])) {
$this->string($event->fields['end'])->isEqualTo($this->end);
$this->assertEquals($this->end, $event->fields['end']);
}

// check rrule encoding
// check rrule encoding
$exp_exdates = '"exceptions":["' . $this->exdate1 . '","' . $this->exdate2 . '"]';
$this->string($event->fields['rrule'])
->isEqualTo('{"freq":"daily","interval":1,"byweekday":"MO","bymonth":1,' . $exp_exdates . '}');
$this->assertEquals(
'{"freq":"daily","interval":1,"byweekday":"MO","bymonth":1,' . $exp_exdates . '}',
$event->fields['rrule']
);

return $event;
}
Expand Down Expand Up @@ -126,21 +128,21 @@ public function testUpdate()
'state' => \Planning::INFO,
'background' => 0,
]);
$this->boolean($event->update($update))->isTrue();
$this->assertTrue($event->update($update));

// check dates (we added duration to both dates on update)
// check dates (we added duration to both dates on update)
if (isset($event->fields['begin'])) {
$this->string($event->fields['begin'])
->isEqualTo($new_begin);
$this->assertEquals($new_begin, $event->fields['begin']);
}
if (isset($event->fields['end'])) {
$this->string($event->fields['end'])
->isEqualTo($new_end);
$this->assertEquals($new_end, $event->fields['end']);
}

// check rrule encoding
$this->string($event->fields['rrule'])
->isEqualTo('{"freq":"monthly","interval":2,"byweekday":"TU","bymonth":2}');
// check rrule encoding
$this->assertEquals(
'{"freq":"monthly","interval":2,"byweekday":"TU","bymonth":2}',
$event->fields['rrule']
);
}


Expand All @@ -150,11 +152,9 @@ public function testDelete()

$event = new $this->myclass();
$id = $event->add($this->input);
$this->integer((int)$id)->isGreaterThan(0);
$this->assertGreaterThan(0, (int)$id);

$this->boolean($event->delete([
'id' => $id,
]))->isTrue();
$this->boolean($event->getFromDB($id))->isFalse();
$this->assertTrue($event->delete(['id' => $id]));
$this->assertFalse($event->getFromDB($id));
}
}
2 changes: 1 addition & 1 deletion phpunit/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
//include_once __DIR__ . '/FrontBaseClass.php';
include_once __DIR__ . '/RuleBuilder.php';
include_once __DIR__ . '/InventoryTestCase.php';
include_once __DIR__ . '/functional/CommonITILRecurrentTest.php';
include_once __DIR__ . '/abstracts/CommonITILRecurrentTest.php';
//include_once __DIR__ . '/functional/Glpi/ContentTemplates/Parameters/AbstractParameters.php';
include_once __DIR__ . '/AbstractRightsDropdown.php';

Expand Down
8 changes: 4 additions & 4 deletions phpunit/functional/Calendar_HolidayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,17 +139,17 @@ public function testHolidaysCache()
/*
$calendar = new Calendar();
$calendar_id = $calendar->add(['name' => 'Test']);
$this->integer($calendar_id)->isGreaterThan(0);
$this->assertGreaterThan(0, $calendar_id);
$this->validateHolidayCacheMatchesMethodResult($calendar_id);
$holiday_id = getItemByTypeName('Holiday', 'Winter holidays', true);
$this->boolean($calendar_holiday->getFromDBByCrit(['holidays_id' => $holiday_id]))->isTrue();
$this->assertTrue($calendar_holiday->getFromDBByCrit(['holidays_id' => $holiday_id]));
$this->boolean(
$calendar_holiday->update(['id' => $calendar_holiday->fields['id'], 'calendars_id' => $calendar_id])
)->isTrue();
$this->boolean($GLPI_CACHE->has($cache_key))->isFalse(); // Previously associated calendar cache is invalidated
$this->boolean($GLPI_CACHE->has(sprintf('calendar-%s-holidays', $calendar_id)))->isFalse();
$this->assertFalse($GLPI_CACHE->has($cache_key)); // Previously associated calendar cache is invalidated
$this->assertFalse($GLPI_CACHE->has(sprintf('calendar-%s-holidays', $calendar_id)));
*/
}

Expand Down
79 changes: 39 additions & 40 deletions tests/functional/Change.php → phpunit/functional/ChangeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@

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

class Change extends DbTestCase
class ChangeTest extends DbTestCase
{
public function testAddFromItem()
{
// add change from a computer
// add change from a computer
$computer = getItemByTypeName('Computer', '_test_pc01');
$change = new \Change();
$changes_id = $change->add([
Expand All @@ -53,12 +53,12 @@ public function testAddFromItem()
'_from_itemtype' => 'Computer',
'_from_items_id' => $computer->getID(),
]);
$this->integer($changes_id)->isGreaterThan(0);
$this->boolean($change->getFromDB($changes_id))->isTrue();
$this->assertGreaterThan(0, $changes_id);
$this->assertTrue($change->getFromDB($changes_id));

// check relation
// check relation
$change_item = new \Change_Item();
$this->boolean($change_item->getFromDBForItems($change, $computer))->isTrue();
$this->assertTrue($change_item->getFromDBForItems($change, $computer));
}

public function testAssignFromCategory()
Expand All @@ -72,14 +72,14 @@ public function testAssignFromCategory()
'auto_assign_mode' => \Entity::CONFIG_NEVER,
]);

$this->boolean($entity->isNewID($entityId))->isFalse();
$this->assertFalse($entity->isNewID($entityId));
$entity->getFromDB($entityId);
$this->integer((int) $entity->fields['auto_assign_mode'])->isEqualTo(\Entity::CONFIG_NEVER);
$this->assertEquals(\Entity::CONFIG_NEVER, (int)$entity->fields['auto_assign_mode']);

// Login again to acess the new entity
// Login again to acess the new entity
$this->login('glpi', 'glpi');
$success = \Session::changeActiveEntities($entity->getID(), true);
$this->boolean($success)->isTrue();
$this->assertTrue($success);

$group = new \Group();
$group->add([
Expand All @@ -88,7 +88,7 @@ public function testAssignFromCategory()
'is_recursive' => '1',
'level' => 0,
]);
$this->boolean($group->isNewItem())->isFalse();
$this->assertFalse($group->isNewItem());

$itilCategory = new \ITILCategory();
$itilCategory->add([
Expand All @@ -97,29 +97,29 @@ public function testAssignFromCategory()
'users_id' => 4, // Tech
'groups_id' => $group->getID(),
]);
$this->boolean($itilCategory->isNewItem())->isFalse();
$this->assertFalse($itilCategory->isNewItem());

$change = new \Change();
$change->add([
'name' => 'A change to check if it is not automatically assigned user and group',
'content' => 'foo',
'itilcategories_id' => $itilCategory->getID(),
]);
$this->boolean($change->isNewItem())->isFalse();
$this->assertFalse($change->isNewItem());
$change->getFromDB($change->getID());
$changeUser = new \Change_User();
$changeGroup = new \Change_Group();
$rows = $changeUser->find([
'changes_id' => $change->getID(),
'type' => \CommonITILActor::ASSIGN,
]);
$this->integer(count($rows))->isEqualTo(0);
$this->assertCount(0, $rows);
$rows = $changeGroup->find([
'changes_id' => $change->getID(),
'type' => \CommonITILActor::ASSIGN,
]);

// check Entity::AUTO_ASSIGN_HARDWARE_CATEGORY assignment
// check Entity::AUTO_ASSIGN_HARDWARE_CATEGORY assignment
$entity->update([
'id' => $entity->getID(),
'auto_assign_mode' => \Entity::AUTO_ASSIGN_HARDWARE_CATEGORY,
Expand All @@ -131,7 +131,7 @@ public function testAssignFromCategory()
'content' => 'foo',
'itilcategories_id' => $itilCategory->getID(),
]);
$this->boolean($change->isNewItem())->isFalse();
$this->assertFalse($change->isNewItem());
$change->getFromDB($change->getID());
$changeUser = new \Change_User();
$changeGroup = new \Change_Group();
Expand All @@ -140,14 +140,14 @@ public function testAssignFromCategory()
'users_id' => 4, // Tech
'type' => \CommonITILActor::ASSIGN,
]);
$this->integer(count($rows))->isEqualTo(0);
$this->assertCount(0, $rows);
$rows = $changeGroup->find([
'changes_id' => $change->getID(),
'groups_id' => $group->getID(),
'type' => \CommonITILActor::ASSIGN,
]);

// check Entity::AUTO_ASSIGN_CATEGORY_HARDWARE assignment
// check Entity::AUTO_ASSIGN_CATEGORY_HARDWARE assignment
$entity->update([
'id' => $entity->getID(),
'auto_assign_mode' => \Entity::AUTO_ASSIGN_CATEGORY_HARDWARE,
Expand All @@ -159,7 +159,7 @@ public function testAssignFromCategory()
'content' => 'foo',
'itilcategories_id' => $itilCategory->getID(),
]);
$this->boolean($change->isNewItem())->isFalse();
$this->assertFalse($change->isNewItem());
$change->getFromDB($change->getID());
$changeUser = new \Change_User();
$changeGroup = new \Change_Group();
Expand All @@ -168,29 +168,28 @@ public function testAssignFromCategory()
'users_id' => 4, // Tech
'type' => \CommonITILActor::ASSIGN,
]);
$this->integer(count($rows))->isEqualTo(0);
$this->assertCount(0, $rows);
$rows = $changeGroup->find([
'changes_id' => $change->getID(),
'groups_id' => $group->getID(),
'type' => \CommonITILActor::ASSIGN,
]);
$this->assertCount(0, $rows);
}

public function testGetTeamRoles(): void
{
$roles = \Change::getTeamRoles();
$this->array($roles)->containsValues([
\CommonITILActor::ASSIGN,
\CommonITILActor::OBSERVER,
\CommonITILActor::REQUESTER,
]);
$this->assertContains(\CommonITILActor::ASSIGN, $roles);
$this->assertContains(\CommonITILActor::OBSERVER, $roles);
$this->assertContains(\CommonITILActor::REQUESTER, $roles);
}

public function testGetTeamRoleName(): void
{
$roles = \Change::getTeamRoles();
foreach ($roles as $role) {
$this->string(\Change::getTeamRoleName($role))->isNotEmpty();
$this->assertNotEmpty(\Change::getTeamRoleName($role));
}
}

Expand All @@ -206,7 +205,7 @@ public function testAutomaticStatusChange()
]);

// Initial status is new (incoming)
$this->integer($change->fields['status'])->isIdenticalTo(\CommonITILObject::INCOMING);
$this->assertSame(\CommonITILObject::INCOMING, $change->fields['status']);

$change->update([
'id' => $changes_id,
Expand All @@ -216,12 +215,12 @@ public function testAutomaticStatusChange()
],
]);
$test_users_id = getItemByTypeName('User', TU_USER, true);
$this->integer($test_users_id)->isGreaterThan(0);
$this->assertGreaterThan(0, $test_users_id);

// Verify user was assigned and status doesn't change
$change->loadActors();
$this->integer($change->countUsers(\CommonITILActor::ASSIGN))->isIdenticalTo(1);
$this->integer($change->fields['status'])->isIdenticalTo(\CommonITILObject::INCOMING);
$this->assertSame(1, $change->countUsers(\CommonITILActor::ASSIGN));
$this->assertSame(\CommonITILObject::INCOMING, $change->fields['status']);

// Change status to accepted
$change->update([
Expand All @@ -236,8 +235,8 @@ public function testAutomaticStatusChange()
'users_id' => getItemByTypeName('User', TU_USER, true),
]);
$change->getFromDB($changes_id);
$this->integer($change->countUsers(\CommonITILActor::ASSIGN))->isIdenticalTo(0);
$this->integer($change->fields['status'])->isIdenticalTo(\CommonITILObject::ACCEPTED);
$this->assertSame(0, $change->countUsers(\CommonITILActor::ASSIGN));
$this->assertSame(\CommonITILObject::ACCEPTED, $change->fields['status']);
}

public function testAddAdditionalActorsDuplicated()
Expand All @@ -248,7 +247,7 @@ public function testAddAdditionalActorsDuplicated()
'name' => "test add additional actors duplicated",
'content' => "test add additional actors duplicated",
]);
$this->integer($changes_id)->isGreaterThan(0);
$this->assertGreaterThan(0, $changes_id);

$users_id = getItemByTypeName('User', TU_USER, true);

Expand All @@ -261,7 +260,7 @@ public function testAddAdditionalActorsDuplicated()
]
]
]);
$this->boolean($result)->isTrue();
$this->assertTrue($result);

$result = $change->update([
'id' => $changes_id,
Expand All @@ -272,7 +271,7 @@ public function testAddAdditionalActorsDuplicated()
]
]
]);
$this->boolean($result)->isTrue();
$this->assertTrue($result);
}

public function testInitialStatus()
Expand All @@ -285,9 +284,9 @@ public function testInitialStatus()
'entities_id' => getItemByTypeName('Entity', '_test_root_entity', true),
'_users_id_assign' => getItemByTypeName('User', TU_USER, true),
]);
$this->integer($changes_id)->isGreaterThan(0);
$this->assertGreaterThan(0, $changes_id);
// Even when automatically assigning a user, the initial status should be set to New
$this->integer($change->fields['status'])->isIdenticalTo(\CommonITILObject::INCOMING);
$this->assertSame(\CommonITILObject::INCOMING, $change->fields['status']);
}

public function testStatusWhenSolutionIsRefused()
Expand All @@ -301,7 +300,7 @@ public function testStatusWhenSolutionIsRefused()
'_users_id_assign' => getItemByTypeName('User', TU_USER, true),
'status' => \CommonITILObject::SOLVED,
]);
$this->integer($changes_id)->isGreaterThan(0);
$this->assertGreaterThan(0, $changes_id);

$followup = new \ITILFollowup();
$followup_id = $followup->add([
Expand All @@ -314,9 +313,9 @@ public function testStatusWhenSolutionIsRefused()
'timeline_position' => \CommonITILObject::TIMELINE_LEFT,
'add_reopen' => ''
]);
$this->integer($followup_id)->isGreaterThan(0);
$this->assertGreaterThan(0, $followup_id);

$item = $change->getById($changes_id);
$this->integer($item->fields['status'])->isIdenticalTo(\CommonITILObject::INCOMING);
$this->assertSame(\CommonITILObject::INCOMING, $item->fields['status']);
}
}
Loading

0 comments on commit 7432c86

Please sign in to comment.