mirror of
https://github.com/Combodo/iTop.git
synced 2026-05-02 15:08:45 +02:00
Merge remote-tracking branch 'origin/support/3.2' into develop
# Conflicts: # tests/php-unit-tests/src/BaseTestCase/ItopDataTestCase.php
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
|
||||
namespace Combodo\iTop\Test\UnitTest\Core;
|
||||
|
||||
use AttributeDate;
|
||||
use AttributeDateTime;
|
||||
use Change;
|
||||
use Combodo\iTop\Test\UnitTest\ItopDataTestCase;
|
||||
use MetaModel;
|
||||
@@ -229,7 +231,7 @@ PHP
|
||||
$this->assertEquals($bComputationExpected, $oAttDef->HasPHPComputation(), "Standard DataModel should be configured with property 'has_php_computation'=$sComputationExpected for $sClass:$sAttCode");
|
||||
}
|
||||
|
||||
public function WithConstraintParameterProvider()
|
||||
public static function WithConstraintParameterProvider()
|
||||
{
|
||||
return [
|
||||
['User', 'profile_list', true, true],
|
||||
@@ -238,4 +240,109 @@ PHP
|
||||
['Ticket', 'functionalcis_list', false, true],
|
||||
];
|
||||
}
|
||||
|
||||
public function testDateTimeEmptyDefaultReturnsNullAsDefaultValue()
|
||||
{
|
||||
// Given
|
||||
$oDateAttribute = new AttributeDateTime('start_date', ['sql' => 'start_date', 'is_null_allowed' => false, 'default_value' => '', 'allowed_values' => null, 'depends_on' => [], 'always_load_in_tables' => false]);
|
||||
$oDateAttribute->SetHostClass('WorkOrder');
|
||||
$oWorkOrder = MetaModel::NewObject('WorkOrder');
|
||||
|
||||
//When
|
||||
$defaultValue = $oDateAttribute->GetDefaultValue();
|
||||
$oField = $oDateAttribute->MakeFormField($oWorkOrder);
|
||||
|
||||
// Then
|
||||
self::assertNull($defaultValue, 'Empty default value for DateTime attribute should give null default value');
|
||||
self::assertEmpty($oField->GetCurrentValue(), 'Empty default value for DateTime attribute should give empty form field');
|
||||
}
|
||||
|
||||
public function testDateEmptyDefaultReturnsNullAsDefaultValue()
|
||||
{
|
||||
// Given
|
||||
$oDateAttribute = new AttributeDate('start_date', ['sql' => 'start_date', 'is_null_allowed' => false, 'default_value' => '', 'allowed_values' => null, 'depends_on' => [], 'always_load_in_tables' => false]);
|
||||
$oDateAttribute->SetHostClass('WorkOrder');
|
||||
$oWorkOrder = MetaModel::NewObject('WorkOrder');
|
||||
|
||||
//When
|
||||
$defaultValue = $oDateAttribute->GetDefaultValue();
|
||||
$oField = $oDateAttribute->MakeFormField($oWorkOrder);
|
||||
|
||||
// Then
|
||||
self::assertNull($defaultValue, 'Empty default value for Date attribute should give null default value');
|
||||
self::assertEmpty($oField->GetCurrentValue(), 'Empty default value for DateTime attribute should give empty form field');
|
||||
}
|
||||
|
||||
|
||||
public function testDateTimeNowAsDefaultGivesCurrentDateAsDefaultValue()
|
||||
{
|
||||
// Given
|
||||
$oDateAttribute = new AttributeDateTime('start_date', ['sql' => 'start_date', 'is_null_allowed' => false, 'default_value' => 'now', 'allowed_values' => null, 'depends_on' => [], 'always_load_in_tables' => false]);
|
||||
$oDateAttribute->SetHostClass('WorkOrder');
|
||||
$oWorkOrder = MetaModel::NewObject('WorkOrder');
|
||||
|
||||
//When
|
||||
$defaultValue = $oDateAttribute->GetDefaultValue();
|
||||
$oField = $oDateAttribute->MakeFormField($oWorkOrder);
|
||||
|
||||
// Then
|
||||
$sNow = date($oDateAttribute->GetInternalFormat());
|
||||
self::assertEquals($sNow, $defaultValue, 'Now as default value for DateTime attribute should give current date as default value');
|
||||
self::assertEquals($sNow, $oField->GetCurrentValue(), 'Now as default value for DateTime attribute should give current date as form field');
|
||||
}
|
||||
|
||||
|
||||
public function testDateNowAsDefaultGivesCurrentDateAsDefaultValue()
|
||||
{
|
||||
// Given
|
||||
$oDateAttribute = new AttributeDate('start_date', ['sql' => 'start_date', 'is_null_allowed' => false, 'default_value' => 'now', 'allowed_values' => null, 'depends_on' => [], 'always_load_in_tables' => false]);
|
||||
$oDateAttribute->SetHostClass('WorkOrder');
|
||||
$oWorkOrder = MetaModel::NewObject('WorkOrder');
|
||||
|
||||
//When
|
||||
$defaultValue = $oDateAttribute->GetDefaultValue();
|
||||
$oField = $oDateAttribute->MakeFormField($oWorkOrder);
|
||||
|
||||
// Then
|
||||
$sNow = date($oDateAttribute->GetInternalFormat());
|
||||
self::assertEquals($sNow, $defaultValue, 'Now as default value for Date attribute should give current date as default value');
|
||||
self::assertEquals($sNow, $oField->GetCurrentValue(), 'Now as default value for Date attribute should give current date as form field');
|
||||
}
|
||||
|
||||
public function testDateTimeIntervalAsDefaultGivesCorrectDateAsDefaultValue()
|
||||
{
|
||||
// Given
|
||||
$oDateAttribute = new AttributeDateTime('start_date', ['sql' => 'start_date', 'is_null_allowed' => false, 'default_value' => '+1day', 'allowed_values' => null, 'depends_on' => [], 'always_load_in_tables' => false]);
|
||||
$oDateAttribute->SetHostClass('WorkOrder');
|
||||
$oWorkOrder = MetaModel::NewObject('WorkOrder');
|
||||
|
||||
//When
|
||||
$defaultValue = $oDateAttribute->GetDefaultValue();
|
||||
$oField = $oDateAttribute->MakeFormField($oWorkOrder);
|
||||
|
||||
// Then
|
||||
$oDate = new \DateTimeImmutable('+1day');
|
||||
$sExpected = $oDate->format($oDateAttribute->GetInternalFormat());
|
||||
self::assertEquals($sExpected, $defaultValue, 'Interval as default value for DateTime attribute should give correct date as default value');
|
||||
self::assertEquals($sExpected, $oField->GetCurrentValue(), 'Interval as default value for DateTime attribute should give correct date as form field');
|
||||
}
|
||||
|
||||
public function testDateIntervalAsDefaultGivesCorrectDateAsDefaultValue()
|
||||
{
|
||||
// Given
|
||||
$oDateAttribute = new AttributeDate('start_date', ['sql' => 'start_date', 'is_null_allowed' => false, 'default_value' => '+1day', 'allowed_values' => null, 'depends_on' => [], 'always_load_in_tables' => false]);
|
||||
$oDateAttribute->SetHostClass('WorkOrder');
|
||||
$oWorkOrder = MetaModel::NewObject('WorkOrder');
|
||||
|
||||
//When
|
||||
$defaultValue = $oDateAttribute->GetDefaultValue();
|
||||
$oField = $oDateAttribute->MakeFormField($oWorkOrder);
|
||||
|
||||
// Then
|
||||
$oDate = new \DateTimeImmutable('+1day');
|
||||
$sExpected = $oDate->format($oDateAttribute->GetInternalFormat());
|
||||
self::assertEquals($sExpected, $defaultValue, 'Interval as default value for Date attribute should give correct date as default value');
|
||||
self::assertEquals($sExpected, $oField->GetCurrentValue(), 'Interval as default value for Date attribute should give correct date as form field');
|
||||
}
|
||||
|
||||
}
|
||||
@@ -62,7 +62,7 @@ class EventIssueTest extends ItopDataTestCase
|
||||
$oEventIssue->DBInsert();
|
||||
}
|
||||
catch (CoreException $e) {
|
||||
$this->fail('we should be able to persist the object though it contains long values in its attributes');
|
||||
$this->fail('we should be able to persist the object though it contains long values in its attributes: '.$e->getMessage());
|
||||
}
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
|
||||
@@ -90,7 +90,7 @@ class ExpressionEvaluateTest extends ItopDataTestCase
|
||||
{
|
||||
$aExpressions = array(
|
||||
// Test case to isolate for troubleshooting purposes
|
||||
array('1+1', 2),
|
||||
array("'a' IN ('a', 'b')", true),
|
||||
);
|
||||
}
|
||||
else
|
||||
@@ -141,6 +141,9 @@ class ExpressionEvaluateTest extends ItopDataTestCase
|
||||
array('"2020-06-12 17:35:13" < "2020-06-12"', 0),
|
||||
array('"2020-06-12 00:00:00" = "2020-06-12"', 0),
|
||||
|
||||
// IN operator
|
||||
array("'a' IN ('a', 'b')", true),
|
||||
|
||||
// Logical operators
|
||||
array('0 AND 0', 0),
|
||||
array('1 AND 0', 0),
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
namespace Combodo\iTop\Test\UnitTest\Core;
|
||||
|
||||
use Combodo\iTop\Test\UnitTest\ItopDataTestCase;
|
||||
use MetaModel;
|
||||
use Person;
|
||||
|
||||
class TriggerOnStateEnterTest extends ItopDataTestCase
|
||||
{
|
||||
const CREATE_TEST_ORG = true;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->RemoveAllObjects(\Trigger::class);
|
||||
$this->RemoveAllObjects(\EventNotificationEmail::class);
|
||||
}
|
||||
|
||||
public function testIsTriggeredOnTransition()
|
||||
{
|
||||
$iTrigger = $this->GivenTriggerWithAction('TriggerOnStateEnter', 'assigned');
|
||||
$oUserRequest = $this->GivenUserRequest('new');
|
||||
|
||||
$oUserRequest->ApplyStimulus('ev_assign');
|
||||
$this->AssertTriggerExecuted($iTrigger, 1, 'The trigger should have been executed');
|
||||
|
||||
$oUserRequest->ApplyStimulus('ev_assign');
|
||||
$this->AssertTriggerExecuted($iTrigger, 1, 'The trigger should not be executed when stimulus not expected in current state');
|
||||
}
|
||||
|
||||
public function testIsTriggeredOnTransitionStayingInSameState()
|
||||
{
|
||||
$iTrigger = $this->GivenTriggerWithAction('TriggerOnStateEnter', 'assigned');
|
||||
$oUserRequest = $this->GivenUserRequest('new');
|
||||
$oUserRequest->ApplyStimulus('ev_assign');
|
||||
|
||||
$bTransitioned = $oUserRequest->ApplyStimulus('ev_reassign');
|
||||
$this->assertTrue($bTransitioned, 'The stimulus should have been accepted');
|
||||
|
||||
$this->AssertTriggerExecuted($iTrigger, 2, 'The trigger should have been executed twice');
|
||||
}
|
||||
public function testIsTriggeredOnNewObject()
|
||||
{
|
||||
$iTrigger = $this->GivenTriggerWithAction('TriggerOnStateEnter', 'new');
|
||||
$oUserRequest = $this->GivenUserRequest('new');
|
||||
$this->AssertTriggerExecuted($iTrigger, 0, 'The trigger TriggerOnStateEnter should not be executed on created object');
|
||||
}
|
||||
|
||||
private function GivenTriggerWithAction(string $sTriggerClass, string $sState)
|
||||
{
|
||||
$iTrigger = $this->GivenObjectInDB($sTriggerClass, [
|
||||
'description' => 'Description',
|
||||
'target_class' => 'UserRequest',
|
||||
'state' => $sState,
|
||||
]);
|
||||
$this->GivenObjectInDB('ActionEmail', [
|
||||
'from' => 'test@combodo.com',
|
||||
'subject' => 'Subject',
|
||||
'body' => 'Body',
|
||||
'description' => 'Description',
|
||||
'test_recipient' => 'test@combodo.com',
|
||||
'name' => 'UserRequest',
|
||||
'asynchronous' => 'yes',
|
||||
'trigger_list' => [
|
||||
"trigger_id:$iTrigger",
|
||||
],
|
||||
]);
|
||||
return $iTrigger;
|
||||
}
|
||||
|
||||
private function AssertTriggerExecuted(int $iTrigger, $iCount, $sMessage = '')
|
||||
{
|
||||
$oSearch = new \DBObjectSearch('EventNotificationEmail');
|
||||
$oSearch->AddCondition('trigger_id', $iTrigger);
|
||||
$oSet = new \DBObjectSet($oSearch);
|
||||
$this->assertEquals($iCount, $oSet->Count(), $sMessage);
|
||||
}
|
||||
|
||||
public function GivenUserRequest(string $sStatus): ?\DBObject
|
||||
{
|
||||
$iUserRequest = $this->GivenObjectInDB('UserRequest', [
|
||||
'title' => 'Title',
|
||||
'description' => 'Description',
|
||||
'status' => $sStatus,
|
||||
]);
|
||||
return MetaModel::GetObject('UserRequest', $iUserRequest);
|
||||
}
|
||||
}
|
||||
@@ -491,52 +491,65 @@ class UserRightsTest extends ItopDataTestCase
|
||||
|
||||
public function testFindUser_ExistingInternalUser()
|
||||
{
|
||||
$sLogin = 'UserRightsFindUser'.uniqid();
|
||||
$iKey = $this->CreateUser($sLogin, self::$aURP_Profiles['Administrator'])->GetKey();
|
||||
$oUser = $this->InvokeNonPublicStaticMethod(UserRights::class, "FindUser", [$sLogin]);
|
||||
$sLogin = 'AnInternalUser'.uniqid();
|
||||
$iKey = $this->GivenObjectInDB(\UserLocal::class, ['login' => $sLogin]);
|
||||
|
||||
$this->assertNotNull($oUser);
|
||||
$this->assertEquals($iKey, $oUser->GetKey());
|
||||
$this->assertEquals(\UserLocal::class, get_class($oUser));
|
||||
$this->assertDBQueryCount(
|
||||
1,
|
||||
fn() => $this->FindUserAndAssertItHasBeenFound($sLogin, $iKey),
|
||||
'A query should be performed the first time FindUser is called'
|
||||
);
|
||||
|
||||
$this->assertDBQueryCount(0, function() use ($sLogin, $iKey){
|
||||
$oUser = $this->InvokeNonPublicStaticMethod(UserRights::class, "FindUser", [$sLogin]);
|
||||
static::assertEquals($iKey, $oUser->GetKey());
|
||||
static::assertEquals(\UserLocal::class, get_class($oUser));
|
||||
});
|
||||
$this->assertDBQueryCount(
|
||||
0,
|
||||
fn() => $this->FindUserAndAssertItHasBeenFound($sLogin, $iKey),
|
||||
'The cache should prevent additional queries on subsequent calls'
|
||||
);
|
||||
}
|
||||
|
||||
public function testFindUser_ExistingExternalUser()
|
||||
{
|
||||
$sLogin = 'UserRightsFindUser'.uniqid();
|
||||
$sLogin = 'AnExternalUser'.uniqid();
|
||||
$iKey = $this->GivenObjectInDB(\UserExternal::class, ['login' => $sLogin]);
|
||||
|
||||
$iKey = $this->GivenObjectInDB(\UserExternal::class, [
|
||||
'login' => $sLogin,
|
||||
'language' => 'EN US',
|
||||
]);
|
||||
$this->assertDBQueryCount(
|
||||
2,
|
||||
fn() => $this->FindUserAndAssertItHasBeenFound($sLogin, $iKey),
|
||||
'Some queries should be performed the first time FindUser is called'
|
||||
);
|
||||
|
||||
$oUser = $this->InvokeNonPublicStaticMethod(UserRights::class, "FindUser", [$sLogin]);
|
||||
|
||||
$this->assertNotNull($oUser);
|
||||
$this->assertEquals($iKey, $oUser->GetKey());
|
||||
$this->assertEquals(\UserExternal::class, get_class($oUser));
|
||||
|
||||
$this->assertDBQueryCount(0, function() use ($sLogin, $iKey){
|
||||
$oUser = $this->InvokeNonPublicStaticMethod(UserRights::class, "FindUser", [$sLogin]);
|
||||
static::assertEquals($iKey, $oUser->GetKey());
|
||||
static::assertEquals(\UserExternal::class, get_class($oUser));
|
||||
});
|
||||
$this->assertDBQueryCount(
|
||||
0,
|
||||
fn() => $this->FindUserAndAssertItHasBeenFound($sLogin, $iKey),
|
||||
'The cache should prevent additional queries on subsequent calls'
|
||||
);
|
||||
}
|
||||
|
||||
public function testFindUser_UnknownLogin_AvoidSameSqlQueryTwice()
|
||||
public function testFindUser_UnknownLogin()
|
||||
{
|
||||
$sLogin = 'UserRightsFindUser'.uniqid();
|
||||
$oUser = $this->InvokeNonPublicStaticMethod(UserRights::class, "FindUser", [$sLogin]);
|
||||
$this->assertNull($oUser);
|
||||
$sLogin = 'NobodyLogin';
|
||||
|
||||
$this->assertDBQueryCount(0, function() use ($sLogin){
|
||||
$oUser = $this->InvokeNonPublicStaticMethod(UserRights::class, "FindUser", [$sLogin]);
|
||||
$this->assertNull($oUser);
|
||||
});
|
||||
$this->assertDBQueryCount(
|
||||
2,
|
||||
fn() => $this->FindUserAndAssertItWasNotFound($sLogin),
|
||||
'Some queries should be performed the first time FindUser is called'
|
||||
);
|
||||
|
||||
$this->assertDBQueryCount(
|
||||
0,
|
||||
fn() => $this->FindUserAndAssertItWasNotFound($sLogin),
|
||||
'The cache should prevent additional queries on subsequent calls'
|
||||
);
|
||||
}
|
||||
|
||||
public function FindUserAndAssertItHasBeenFound($sLogin, $iExpectedKey)
|
||||
{
|
||||
$oUser = $this->InvokeNonPublicStaticMethod(UserRights::class, "FindUser", [$sLogin]);
|
||||
static::assertIsDBObject(\User::class, $iExpectedKey, $oUser, 'FindUser should return the User object corresponding to the login');
|
||||
}
|
||||
public function FindUserAndAssertItWasNotFound($sLogin)
|
||||
{
|
||||
$oUser = $this->InvokeNonPublicStaticMethod(UserRights::class, "FindUser", [$sLogin]);
|
||||
static::assertNull($oUser, 'FindUser should return null when the login is unknown');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user