N°931 fix PhpUnit tests for TagSets (default datamodel doesn't have anymore Ticket.tagfield, but FAQ.domains)

This commit is contained in:
Pierre Goiffon
2018-10-02 18:06:23 +02:00
parent 7281bd4a1a
commit c66b0bea41
5 changed files with 436 additions and 376 deletions

View File

@@ -18,6 +18,7 @@
// //
namespace Combodo\iTop\Test\UnitTest; namespace Combodo\iTop\Test\UnitTest;
/** /**
* Created by PhpStorm. * Created by PhpStorm.
* User: Eric * User: Eric
@@ -45,8 +46,9 @@ use VirtualHost;
use VirtualMachine; use VirtualMachine;
define('TAG_CLASS', 'Ticket'); /** @see \Combodo\iTop\Test\UnitTest\ItopDataTestCase::CreateObjectWithTagSet() */
define('TAG_ATTCODE', 'tagfield'); define('TAG_CLASS', 'FAQ');
define('TAG_ATTCODE', 'domains');
/** /**
* @runTestsInSeparateProcesses * @runTestsInSeparateProcesses
@@ -69,24 +71,24 @@ class ItopDataTestCase extends ItopTestCase
parent::setUp(); parent::setUp();
//require_once(APPROOT.'/application/startup.inc.php'); //require_once(APPROOT.'/application/startup.inc.php');
require_once(APPROOT.'/core/cmdbobject.class.inc.php'); require_once(APPROOT.'/core/cmdbobject.class.inc.php');
require_once(APPROOT.'/application/utils.inc.php'); require_once(APPROOT.'/application/utils.inc.php');
require_once(APPROOT.'/core/contexttag.class.inc.php'); require_once(APPROOT.'/core/contexttag.class.inc.php');
$sEnv = 'production'; $sEnv = 'production';
$sConfigFile = APPCONF.$sEnv.'/'.ITOP_CONFIG_FILE; $sConfigFile = APPCONF.$sEnv.'/'.ITOP_CONFIG_FILE;
MetaModel::Startup($sConfigFile, false /* $bModelOnly */, true /* $bAllowCache */, false /* $bTraceSourceFiles */, $sEnv); MetaModel::Startup($sConfigFile, false /* $bModelOnly */, true /* $bAllowCache */, false /* $bTraceSourceFiles */, $sEnv);
if (static::USE_TRANSACTION) if (static::USE_TRANSACTION)
{ {
CMDBSource::Query('START TRANSACTION'); CMDBSource::Query('START TRANSACTION');
} }
$this->CreateTestOrganization(); $this->CreateTestOrganization();
} }
/** /**
* @throws Exception * @throws Exception
*/ */
protected function tearDown() protected function tearDown()
{ {
if (static::USE_TRANSACTION) if (static::USE_TRANSACTION)
{ {
@@ -97,7 +99,7 @@ class ItopDataTestCase extends ItopTestCase
{ {
$this->debug(""); $this->debug("");
$this->aCreatedObjects = array_reverse($this->aCreatedObjects); $this->aCreatedObjects = array_reverse($this->aCreatedObjects);
foreach($this->aCreatedObjects as $oObject) foreach ($this->aCreatedObjects as $oObject)
{ {
/** @var DBObject $oObject */ /** @var DBObject $oObject */
try try
@@ -106,7 +108,8 @@ class ItopDataTestCase extends ItopTestCase
$iKey = $oObject->GetKey(); $iKey = $oObject->GetKey();
$this->debug("Removing $sClass::$iKey"); $this->debug("Removing $sClass::$iKey");
$oObject->DBDelete(); $oObject->DBDelete();
} catch (Exception $e) }
catch (Exception $e)
{ {
$this->debug($e->getMessage()); $this->debug($e->getMessage());
} }
@@ -122,29 +125,31 @@ class ItopDataTestCase extends ItopTestCase
return $this->iTestOrgId; return $this->iTestOrgId;
} }
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
/// Database Utilities /// Database Utilities
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
/** /**
* @param string $sClass * @param string $sClass
* @param array $aParams * @param array $aParams
* @return DBObject *
* @throws Exception * @return DBObject
*/ * @throws Exception
protected function createObject($sClass, $aParams) */
{ protected function createObject($sClass, $aParams)
$oMyObj = MetaModel::NewObject($sClass); {
foreach($aParams as $sAttCode => $oValue) $oMyObj = MetaModel::NewObject($sClass);
{ foreach ($aParams as $sAttCode => $oValue)
$oMyObj->Set($sAttCode, $oValue); {
} $oMyObj->Set($sAttCode, $oValue);
$oMyObj->DBInsert(); }
$iKey = $oMyObj->GetKey(); $oMyObj->DBInsert();
$this->debug("Created $sClass::$iKey"); $iKey = $oMyObj->GetKey();
$this->aCreatedObjects[] = $oMyObj; $this->debug("Created $sClass::$iKey");
return $oMyObj; $this->aCreatedObjects[] = $oMyObj;
}
return $oMyObj;
}
/** /**
* @param string $sClass * @param string $sClass
@@ -159,11 +164,12 @@ class ItopDataTestCase extends ItopTestCase
protected static function updateObject($sClass, $iKey, $aParams) protected static function updateObject($sClass, $iKey, $aParams)
{ {
$oMyObj = MetaModel::GetObject($sClass, $iKey); $oMyObj = MetaModel::GetObject($sClass, $iKey);
foreach($aParams as $sAttCode => $oValue) foreach ($aParams as $sAttCode => $oValue)
{ {
$oMyObj->Set($sAttCode, $oValue); $oMyObj->Set($sAttCode, $oValue);
} }
$oMyObj->DBUpdate(); $oMyObj->DBUpdate();
return $oMyObj; return $oMyObj;
} }
@@ -171,6 +177,7 @@ class ItopDataTestCase extends ItopTestCase
* Create an Organization in database * Create an Organization in database
* *
* @param string $sName * @param string $sName
*
* @return \Organization * @return \Organization
* @throws Exception * @throws Exception
*/ */
@@ -181,34 +188,37 @@ class ItopDataTestCase extends ItopTestCase
'name' => $sName, 'name' => $sName,
)); ));
$this->debug("Created Organization {$oObj->Get('name')}"); $this->debug("Created Organization {$oObj->Get('name')}");
return $oObj; return $oObj;
} }
/** /**
* Create a Ticket in database * Create a Ticket in database
* *
* @param int $iNum * @param int $iNum
* @return Ticket *
* @throws Exception * @return Ticket
*/ * @throws Exception
protected function CreateTicket($iNum) */
{ protected function CreateTicket($iNum)
/** @var Ticket $oTicket */ {
$oTicket = $this->createObject('UserRequest', array( /** @var Ticket $oTicket */
'ref' => 'Ticket_'.$iNum, $oTicket = $this->createObject('UserRequest', array(
'title' => 'TICKET_'.$iNum, 'ref' => 'Ticket_'.$iNum,
//'request_type' => 'incident', 'title' => 'TICKET_'.$iNum,
'description' => 'Created for unit tests.', //'request_type' => 'incident',
'org_id' => $this->getTestOrgId(), 'description' => 'Created for unit tests.',
)); 'org_id' => $this->getTestOrgId(),
$this->debug("Created {$oTicket->Get('title')} ({$oTicket->Get('ref')})"); ));
return $oTicket; $this->debug("Created {$oTicket->Get('title')} ({$oTicket->Get('ref')})");
}
protected function RemoveTicket($iNum) return $oTicket;
{ }
$this->RemoveObjects('UserRequest', "SELECT UserRequest WHERE ref = 'Ticket_$iNum'");
} protected function RemoveTicket($iNum)
{
$this->RemoveObjects('UserRequest', "SELECT UserRequest WHERE ref = 'Ticket_$iNum'");
}
/** /**
* Create a Ticket in database * Create a Ticket in database
@@ -257,7 +267,7 @@ class ItopDataTestCase extends ItopTestCase
{ {
$oFilter = \DBSearch::FromOQL($sOQL); $oFilter = \DBSearch::FromOQL($sOQL);
$aRes = $oFilter->ToDataArray(array('id')); $aRes = $oFilter->ToDataArray(array('id'));
foreach($aRes as $aRow) foreach ($aRes as $aRow)
{ {
$this->debug($aRow); $this->debug($aRow);
$iKey = $aRow['id']; $iKey = $aRow['id'];
@@ -269,13 +279,14 @@ class ItopDataTestCase extends ItopTestCase
} }
} }
/** /**
* Create a UserRequest in database * Create a UserRequest in database
* *
* @param int $iNum * @param int $iNum
* @param int $iTimeSpent * @param int $iTimeSpent
* @param int $iOrgId * @param int $iOrgId
* @param int $iCallerId * @param int $iCallerId
*
* @return \UserRequest * @return \UserRequest
* @throws Exception * @throws Exception
*/ */
@@ -292,6 +303,7 @@ class ItopDataTestCase extends ItopTestCase
'org_id' => ($iOrgId == 0 ? $this->getTestOrgId() : $iOrgId), 'org_id' => ($iOrgId == 0 ? $this->getTestOrgId() : $iOrgId),
)); ));
$this->debug("Created {$oTicket->Get('title')} ({$oTicket->Get('ref')})"); $this->debug("Created {$oTicket->Get('title')} ({$oTicket->Get('ref')})");
return $oTicket; return $oTicket;
} }
@@ -313,14 +325,17 @@ class ItopDataTestCase extends ItopTestCase
'nb_u' => $iRackUnit, 'nb_u' => $iRackUnit,
)); ));
$this->debug("Created {$oServer->GetName()} ({$oServer->GetKey()})"); $this->debug("Created {$oServer->GetName()} ({$oServer->GetKey()})");
return $oServer; return $oServer;
} }
/** /**
* Create a PhysicalInterface in database * Create a PhysicalInterface in database
*
* @param int $iNum * @param int $iNum
* @param int $iSpeed * @param int $iSpeed
* @param int $iConnectableCiId * @param int $iConnectableCiId
*
* @return DBObject * @return DBObject
* @throws Exception * @throws Exception
*/ */
@@ -332,14 +347,17 @@ class ItopDataTestCase extends ItopTestCase
'connectableci_id' => $iConnectableCiId, 'connectableci_id' => $iConnectableCiId,
)); ));
$this->debug("Created {$oObj->GetName()} ({$oObj->GetKey()})"); $this->debug("Created {$oObj->GetName()} ({$oObj->GetKey()})");
return $oObj; return $oObj;
} }
/** /**
* Create a FiberChannelInterface in database * Create a FiberChannelInterface in database
*
* @param int $iNum * @param int $iNum
* @param int $iSpeed * @param int $iSpeed
* @param int $iConnectableCiId * @param int $iConnectableCiId
*
* @return DBObject * @return DBObject
* @throws Exception * @throws Exception
*/ */
@@ -351,261 +369,300 @@ class ItopDataTestCase extends ItopTestCase
'datacenterdevice_id' => $iConnectableCiId, 'datacenterdevice_id' => $iConnectableCiId,
)); ));
$this->debug("Created {$oObj->GetName()} ({$oObj->GetKey()})"); $this->debug("Created {$oObj->GetName()} ({$oObj->GetKey()})");
return $oObj; return $oObj;
} }
/** /**
* Create a Person in database * Create a Person in database
*
* @param int $iNum * @param int $iNum
* @param int $iOrgId * @param int $iOrgId
*
* @return Person * @return Person
* @throws Exception * @throws Exception
*/ */
protected function CreatePerson($iNum, $iOrgId = 0) protected function CreatePerson($iNum, $iOrgId = 0)
{ {
/** @var Person $oPerson */ /** @var Person $oPerson */
$oPerson = $this->createObject('Person', array( $oPerson = $this->createObject('Person', array(
'name' => 'Person_'.$iNum, 'name' => 'Person_'.$iNum,
'first_name' => 'Test', 'first_name' => 'Test',
'org_id' => ($iOrgId == 0 ? $this->getTestOrgId() : $iOrgId), 'org_id' => ($iOrgId == 0 ? $this->getTestOrgId() : $iOrgId),
)); ));
$this->debug("Created {$oPerson->GetName()} ({$oPerson->GetKey()})"); $this->debug("Created {$oPerson->GetName()} ({$oPerson->GetKey()})");
return $oPerson;
} return $oPerson;
}
/** /**
* @param string $sLogin * @param string $sLogin
* @param int $iProfileId * @param int $iProfileId
*
* @return \DBObject * @return \DBObject
* @throws Exception * @throws Exception
*/ */
protected function CreateUser($sLogin, $iProfileId) protected function CreateUser($sLogin, $iProfileId)
{ {
$oUserProfile = new URP_UserProfile(); $oUserProfile = new URP_UserProfile();
$oUserProfile->Set('profileid', $iProfileId); $oUserProfile->Set('profileid', $iProfileId);
$oUserProfile->Set('reason', 'UNIT Tests'); $oUserProfile->Set('reason', 'UNIT Tests');
$oSet = DBObjectSet::FromObject($oUserProfile); $oSet = DBObjectSet::FromObject($oUserProfile);
$oUser = $this->createObject('UserLocal', array( $oUser = $this->createObject('UserLocal', array(
'contactid' => 2, 'contactid' => 2,
'login' => $sLogin, 'login' => $sLogin,
'password' => $sLogin, 'password' => $sLogin,
'language' => 'EN US', 'language' => 'EN US',
'profile_list' => $oSet, 'profile_list' => $oSet,
)); ));
$this->debug("Created {$oUser->GetName()} ({$oUser->GetKey()})"); $this->debug("Created {$oUser->GetName()} ({$oUser->GetKey()})");
return $oUser;
} return $oUser;
}
/** /**
* Create a Hypervisor in database * Create a Hypervisor in database
* @param int $iNum *
* @param Server $oServer * @param int $iNum
* @param Farm $oFarm * @param Server $oServer
* @return Hypervisor * @param Farm $oFarm
* @throws Exception *
*/ * @return Hypervisor
protected function CreateHypervisor($iNum, $oServer, $oFarm = null) * @throws Exception
{ */
/** @var Hypervisor $oHypervisor */ protected function CreateHypervisor($iNum, $oServer, $oFarm = null)
$oHypervisor = $this->createObject('Hypervisor', array( {
'name' => 'Hypervisor_'.$iNum, /** @var Hypervisor $oHypervisor */
'org_id' => $this->getTestOrgId(), $oHypervisor = $this->createObject('Hypervisor', array(
'server_id' => $oServer->GetKey(), 'name' => 'Hypervisor_'.$iNum,
'farm_id' => is_null($oFarm) ? 0 : $oFarm->GetKey(), 'org_id' => $this->getTestOrgId(),
)); 'server_id' => $oServer->GetKey(),
if (is_null($oFarm)) 'farm_id' => is_null($oFarm) ? 0 : $oFarm->GetKey(),
{ ));
$this->debug("Created {$oHypervisor->GetName()} ({$oHypervisor->GetKey()}) on {$oServer->GetName()}"); if (is_null($oFarm))
} {
else $this->debug("Created {$oHypervisor->GetName()} ({$oHypervisor->GetKey()}) on {$oServer->GetName()}");
{ }
$this->debug("Created {$oHypervisor->GetName()} ({$oHypervisor->GetKey()}) on {$oServer->GetName()} part of {$oFarm->GetName()}"); else
} {
return $oHypervisor; $this->debug("Created {$oHypervisor->GetName()} ({$oHypervisor->GetKey()}) on {$oServer->GetName()} part of {$oFarm->GetName()}");
} }
/** return $oHypervisor;
* Create a Farm in database }
* @param int $iNum
* @param string $sRedundancy
* @return Farm
* @throws Exception
*/
protected function CreateFarm($iNum, $sRedundancy = '1')
{
/** @var Farm $oFarm */
$oFarm = $this->createObject('Farm', array(
'name' => 'Farm_'.$iNum,
'org_id' => $this->getTestOrgId(),
'redundancy' => $sRedundancy,
));
$this->debug("Created {$oFarm->GetName()} ({$oFarm->GetKey()}) redundancy $sRedundancy");
return $oFarm;
}
/** /**
* Create a VM in database * Create a Farm in database
* @param int $iNum *
* @param VirtualHost $oVirtualHost * @param int $iNum
* @return VirtualMachine * @param string $sRedundancy
* @throws Exception *
*/ * @return Farm
protected function CreateVirtualMachine($iNum, $oVirtualHost) * @throws Exception
{ */
/** @var VirtualMachine $oVirtualMachine */ protected function CreateFarm($iNum, $sRedundancy = '1')
$oVirtualMachine = $this->createObject('VirtualMachine', array( {
'name' => 'VirtualMachine_'.$iNum, /** @var Farm $oFarm */
'org_id' => $this->getTestOrgId(), $oFarm = $this->createObject('Farm', array(
'virtualhost_id' => $oVirtualHost->GetKey(), 'name' => 'Farm_'.$iNum,
)); 'org_id' => $this->getTestOrgId(),
$this->debug("Created {$oVirtualMachine->GetName()} ({$oVirtualMachine->GetKey()}) on {$oVirtualHost->GetName()}"); 'redundancy' => $sRedundancy,
return $oVirtualMachine; ));
} $this->debug("Created {$oFarm->GetName()} ({$oFarm->GetKey()}) redundancy $sRedundancy");
return $oFarm;
}
/**
* Create a VM in database
*
* @param int $iNum
* @param VirtualHost $oVirtualHost
*
* @return VirtualMachine
* @throws Exception
*/
protected function CreateVirtualMachine($iNum, $oVirtualHost)
{
/** @var VirtualMachine $oVirtualMachine */
$oVirtualMachine = $this->createObject('VirtualMachine', array(
'name' => 'VirtualMachine_'.$iNum,
'org_id' => $this->getTestOrgId(),
'virtualhost_id' => $oVirtualHost->GetKey(),
));
$this->debug("Created {$oVirtualMachine->GetName()} ({$oVirtualMachine->GetKey()}) on {$oVirtualHost->GetName()}");
return $oVirtualMachine;
}
protected function CreateObjectWithTagSet()
{
/** @var FAQ $oFaq */
$oFaq = $this->createObject('FAQ', array(
'category_id' => 1,
'title' => 'FAQ_phpunit',
));
$this->debug("Created {$oFaq->GetName()}");
return $oFaq;
}
/** /**
* Add a link between a contact and a CI. * Add a link between a contact and a CI.
* The database is not updated. * The database is not updated.
* *
* @param Contact $oContact * @param Contact $oContact
* @param FunctionalCI $oCI * @param FunctionalCI $oCI
* @return lnkContactToFunctionalCI *
* @throws Exception * @return lnkContactToFunctionalCI
*/ * @throws Exception
protected function AddContactToCI($oContact, $oCI) */
{ protected function AddContactToCI($oContact, $oCI)
$oNewLink = new lnkContactToFunctionalCI(); {
$oNewLink->Set('contact_id', $oContact->GetKey()); $oNewLink = new lnkContactToFunctionalCI();
$oContacts = $oCI->Get('contacts_list'); $oNewLink->Set('contact_id', $oContact->GetKey());
$oContacts->AddItem($oNewLink); $oContacts = $oCI->Get('contacts_list');
$oCI->Set('contacts_list', $oContacts); $oContacts->AddItem($oNewLink);
$oCI->Set('contacts_list', $oContacts);
$this->debug("Added {$oContact->GetName()} to {$oCI->GetName()}"); $this->debug("Added {$oContact->GetName()} to {$oCI->GetName()}");
return $oNewLink;
}
/** return $oNewLink;
* Remove a link between a contact and a CI. }
* The database is not updated.
*
* @param Contact $oContact
* @param FunctionalCI $oCI
* @throws Exception
*/
protected function RemoveContactFromCI($oContact, $oCI)
{
$oContacts = $oCI->Get('contacts_list');
foreach($oContacts as $oLnk)
{
if ($oLnk->Get('contact_id') == $oContact->GetKey())
{
$oContacts->RemoveItem($oLnk->GetKey());
$oCI->Set('contacts_list', $oContacts);
$this->debug("Removed {$oContact->GetName()} from {$oCI->Get('name')}");
return;
}
}
}
/** /**
* Add a link between a CI and a Ticket. * Remove a link between a contact and a CI.
* The database is not updated. * The database is not updated.
* *
* @param FunctionalCI $oCI * @param Contact $oContact
* @param Ticket $oTicket * @param FunctionalCI $oCI
* @param string $sImpactCode *
* @throws Exception * @throws Exception
*/ */
protected function AddCIToTicket($oCI, $oTicket, $sImpactCode) protected function RemoveContactFromCI($oContact, $oCI)
{ {
$oNewLink = new \lnkFunctionalCIToTicket(); $oContacts = $oCI->Get('contacts_list');
$oNewLink->Set('functionalci_id', $oCI->GetKey()); foreach ($oContacts as $oLnk)
$oNewLink->Set('impact_code', $sImpactCode); {
$oCIs = $oTicket->Get('functionalcis_list'); if ($oLnk->Get('contact_id') == $oContact->GetKey())
$oCIs->AddItem($oNewLink); {
$oTicket->Set('functionalcis_list', $oCIs); $oContacts->RemoveItem($oLnk->GetKey());
$oCI->Set('contacts_list', $oContacts);
$this->debug("Removed {$oContact->GetName()} from {$oCI->Get('name')}");
$this->debug("Added {$oCI->GetName()} to {$oTicket->Get('ref')} with {$sImpactCode}"); return;
return array($oCI->GetKey() => $sImpactCode); }
} }
}
/** /**
* Remove a link between a CI and a Ticket. * Add a link between a CI and a Ticket.
* The database is not updated. * The database is not updated.
* *
* @param FunctionalCI $oCI * @param FunctionalCI $oCI
* @param Ticket $oTicket * @param Ticket $oTicket
* @throws Exception * @param string $sImpactCode
*/ *
protected function RemoveCIFromTicket($oCI, $oTicket) * @throws Exception
{ */
$oCIs = $oTicket->Get('functionalcis_list'); protected function AddCIToTicket($oCI, $oTicket, $sImpactCode)
foreach($oCIs as $oLnk) {
{ $oNewLink = new \lnkFunctionalCIToTicket();
if ($oLnk->Get('functionalci_id') == $oCI->GetKey()) $oNewLink->Set('functionalci_id', $oCI->GetKey());
{ $oNewLink->Set('impact_code', $sImpactCode);
$sImpactCode = $oLnk->Get('impact_code'); $oCIs = $oTicket->Get('functionalcis_list');
$oCIs->RemoveItem($oLnk->GetKey()); $oCIs->AddItem($oNewLink);
$oTicket->Set('functionalcis_list', $oCIs); $oTicket->Set('functionalcis_list', $oCIs);
$this->debug("Removed {$oCI->GetName()} from {$oTicket->Get('ref')} ({$sImpactCode})");
return;
}
}
$this->debug("ERROR: {$oCI->GetName()} not attached to {$oTicket->Get('ref')}");
$this->assertTrue(false);
}
/** $this->debug("Added {$oCI->GetName()} to {$oTicket->Get('ref')} with {$sImpactCode}");
* Add a link between a Contact and a Ticket.
* The database is not updated.
*
* @param Contact $oContact
* @param Ticket $oTicket
* @param string $sRoleCode
* @param array $aParams
* @throws Exception
*/
protected function AddContactToTicket($oContact, $oTicket, $sRoleCode, $aParams = array())
{
$oNewLink = new \lnkContactToTicket();
$oNewLink->Set('contact_id', $oContact->GetKey());
$oNewLink->Set('role_code', $sRoleCode);
foreach($aParams as $sAttCode => $oValue)
{
$oNewLink->Set($sAttCode, $oValue);
}
$oCIs = $oTicket->Get('contacts_list');
$oCIs->AddItem($oNewLink);
$oTicket->Set('contacts_list', $oCIs);
$this->debug("Added {$oContact->GetName()} to {$oTicket->Get('ref')} with {$sRoleCode}"); return array($oCI->GetKey() => $sImpactCode);
return array($oContact->GetKey() => $sRoleCode); }
}
/** /**
* Remove a link between a Contact and a Ticket. * Remove a link between a CI and a Ticket.
* The database is not updated. * The database is not updated.
* *
* @param Contact $oContact * @param FunctionalCI $oCI
* @param Ticket $oTicket * @param Ticket $oTicket
* @throws Exception *
*/ * @throws Exception
protected function RemoveContactFromTicket($oContact, $oTicket) */
{ protected function RemoveCIFromTicket($oCI, $oTicket)
$oContacts = $oTicket->Get('contacts_list'); {
foreach($oContacts as $oLnk) $oCIs = $oTicket->Get('functionalcis_list');
{ foreach ($oCIs as $oLnk)
if ($oLnk->Get('contact_id') == $oContact->GetKey()) {
{ if ($oLnk->Get('functionalci_id') == $oCI->GetKey())
$sRoleCode = $oLnk->Get('role_code'); {
$oContacts->RemoveItem($oLnk->GetKey()); $sImpactCode = $oLnk->Get('impact_code');
$oTicket->Set('contacts_list', $oContacts); $oCIs->RemoveItem($oLnk->GetKey());
$this->debug("Removed {$oContact->GetName()} from {$oTicket->Get('ref')} ({$sRoleCode})"); $oTicket->Set('functionalcis_list', $oCIs);
return; $this->debug("Removed {$oCI->GetName()} from {$oTicket->Get('ref')} ({$sImpactCode})");
}
} return;
} }
}
$this->debug("ERROR: {$oCI->GetName()} not attached to {$oTicket->Get('ref')}");
$this->assertTrue(false);
}
/**
* Add a link between a Contact and a Ticket.
* The database is not updated.
*
* @param Contact $oContact
* @param Ticket $oTicket
* @param string $sRoleCode
* @param array $aParams
*
* @throws Exception
*/
protected function AddContactToTicket($oContact, $oTicket, $sRoleCode, $aParams = array())
{
$oNewLink = new \lnkContactToTicket();
$oNewLink->Set('contact_id', $oContact->GetKey());
$oNewLink->Set('role_code', $sRoleCode);
foreach ($aParams as $sAttCode => $oValue)
{
$oNewLink->Set($sAttCode, $oValue);
}
$oCIs = $oTicket->Get('contacts_list');
$oCIs->AddItem($oNewLink);
$oTicket->Set('contacts_list', $oCIs);
$this->debug("Added {$oContact->GetName()} to {$oTicket->Get('ref')} with {$sRoleCode}");
return array($oContact->GetKey() => $sRoleCode);
}
/**
* Remove a link between a Contact and a Ticket.
* The database is not updated.
*
* @param Contact $oContact
* @param Ticket $oTicket
*
* @throws Exception
*/
protected function RemoveContactFromTicket($oContact, $oTicket)
{
$oContacts = $oTicket->Get('contacts_list');
foreach ($oContacts as $oLnk)
{
if ($oLnk->Get('contact_id') == $oContact->GetKey())
{
$sRoleCode = $oLnk->Get('role_code');
$oContacts->RemoveItem($oLnk->GetKey());
$oTicket->Set('contacts_list', $oContacts);
$this->debug("Removed {$oContact->GetName()} from {$oTicket->Get('ref')} ({$sRoleCode})");
return;
}
}
}
/** /**
* Reload a Ticket from the database. * Reload a Ticket from the database.
@@ -616,62 +673,65 @@ class ItopDataTestCase extends ItopTestCase
* @throws ArchivedObjectException * @throws ArchivedObjectException
* @throws Exception * @throws Exception
*/ */
protected function ReloadObject(&$oObject) protected function ReloadObject(&$oObject)
{ {
$oObject = MetaModel::GetObject(get_class($oObject), $oObject->GetKey()); $oObject = MetaModel::GetObject(get_class($oObject), $oObject->GetKey());
return $oObject;
}
/** return $oObject;
* Check or Display the CI list of a Ticket. }
*
* @param Ticket $oTicket
* @param array $aWaitedCIList { iCIId => sImpactCode }
* @throws Exception
*/
protected function CheckFunctionalCIList($oTicket, $aWaitedCIList = array())
{
$this->debug("\nResulting functionalcis_list {$oTicket->Get('ref')} ({$oTicket->Get('functionalcis_list')->Count()}):");
foreach($oTicket->Get('functionalcis_list') as $oLnk)
{
$this->debug($oLnk->Get('functionalci_name')." => ".$oLnk->Get('impact_code')."");
$iId = $oLnk->Get('functionalci_id');
if (!empty($aWaitedCIList))
{
$this->assertTrue(array_key_exists($iId, $aWaitedCIList));
$this->assertEquals($aWaitedCIList[$iId], $oLnk->Get('impact_code'));
}
}
}
/** /**
* Check or Display the Contact list of a DBObject (having a contacts_list). * Check or Display the CI list of a Ticket.
* Can also control other attributes of the link. *
* * @param Ticket $oTicket
* @param Ticket $oTicket * @param array $aWaitedCIList { iCIId => sImpactCode }
* @param array $aWaitedContactList { iContactId => array(attcode => value) } *
* @throws Exception * @throws Exception
*/ */
protected function CheckContactList($oTicket, $aWaitedContactList = array()) protected function CheckFunctionalCIList($oTicket, $aWaitedCIList = array())
{ {
$this->debug("\nResulting contacts_list {$oTicket->Get('ref')} ({$oTicket->Get('contacts_list')->Count()}):"); $this->debug("\nResulting functionalcis_list {$oTicket->Get('ref')} ({$oTicket->Get('functionalcis_list')->Count()}):");
foreach($oTicket->Get('contacts_list') as $oLnk) foreach ($oTicket->Get('functionalcis_list') as $oLnk)
{ {
$this->debug($oLnk->Get('contact_id_friendlyname')." => ".$oLnk->Get('role_code')); $this->debug($oLnk->Get('functionalci_name')." => ".$oLnk->Get('impact_code')."");
$iId = $oLnk->Get('contact_id'); $iId = $oLnk->Get('functionalci_id');
if (!empty($aWaitedContactList)) if (!empty($aWaitedCIList))
{ {
$this->assertTrue(array_key_exists($iId, $aWaitedContactList)); $this->assertTrue(array_key_exists($iId, $aWaitedCIList));
foreach($aWaitedContactList[$iId] as $sAttCode => $oValue) $this->assertEquals($aWaitedCIList[$iId], $oLnk->Get('impact_code'));
{ }
if (MetaModel::IsValidAttCode(get_class($oTicket), $sAttCode)) }
{ }
$this->assertEquals($oValue, $oLnk->Get($sAttCode));
} /**
} * Check or Display the Contact list of a DBObject (having a contacts_list).
} * Can also control other attributes of the link.
} *
} * @param Ticket $oTicket
* @param array $aWaitedContactList { iContactId => array(attcode => value) }
*
* @throws Exception
*/
protected function CheckContactList($oTicket, $aWaitedContactList = array())
{
$this->debug("\nResulting contacts_list {$oTicket->Get('ref')} ({$oTicket->Get('contacts_list')->Count()}):");
foreach ($oTicket->Get('contacts_list') as $oLnk)
{
$this->debug($oLnk->Get('contact_id_friendlyname')." => ".$oLnk->Get('role_code'));
$iId = $oLnk->Get('contact_id');
if (!empty($aWaitedContactList))
{
$this->assertTrue(array_key_exists($iId, $aWaitedContactList));
foreach ($aWaitedContactList[$iId] as $sAttCode => $oValue)
{
if (MetaModel::IsValidAttCode(get_class($oTicket), $sAttCode))
{
$this->assertEquals($oValue, $oLnk->Get($sAttCode));
}
}
}
}
}
protected function CreateTestOrganization() protected function CreateTestOrganization()
{ {

View File

@@ -459,28 +459,28 @@ class CriterionConversionTest extends ItopDataTestCase
'ExpectedCriterion' => array(array('widget' => 'raw')), 'ExpectedCriterion' => array(array('widget' => 'raw')),
), ),
'TagSet Matches' => array( 'TagSet Matches' => array(
'OQL' => "SELECT UserRequest WHERE tagfield MATCHES 'tag1'", 'OQL' => "SELECT ".TAG_CLASS." WHERE ".TAG_ATTCODE." MATCHES 'tag1'",
'ExpectedOQL' => "SELECT `UserRequest` FROM UserRequest AS `UserRequest` WHERE `UserRequest`.`tagfield` MATCHES 'tag1'", 'ExpectedOQL' => "SELECT `".TAG_CLASS."` FROM ".TAG_CLASS." AS `".TAG_CLASS."` WHERE `".TAG_CLASS."`.`".TAG_ATTCODE."` MATCHES 'tag1'",
'ExpectedCriterion' => array(array('widget' => 'tag_set')), 'ExpectedCriterion' => array(array('widget' => 'tag_set')),
), ),
'TagSet Matches2' => array( 'TagSet Matches2' => array(
'OQL' => "SELECT UserRequest WHERE tagfield MATCHES 'tag1 tag2'", 'OQL' => "SELECT ".TAG_CLASS." WHERE ".TAG_ATTCODE." MATCHES 'tag1 tag2'",
'ExpectedOQL' => "SELECT `UserRequest` FROM UserRequest AS `UserRequest` WHERE `UserRequest`.`tagfield` MATCHES 'tag1 tag2'", 'ExpectedOQL' => "SELECT `".TAG_CLASS."` FROM ".TAG_CLASS." AS `".TAG_CLASS."` WHERE `".TAG_CLASS."`.`".TAG_ATTCODE."` MATCHES 'tag1 tag2'",
'ExpectedCriterion' => array(array('widget' => 'tag_set')), 'ExpectedCriterion' => array(array('widget' => 'tag_set')),
), ),
'TagSet Undefined' => array( 'TagSet Undefined' => array(
'OQL' => "SELECT UserRequest WHERE tagfield = ''", 'OQL' => "SELECT ".TAG_CLASS." WHERE ".TAG_ATTCODE." = ''",
'ExpectedOQL' => "SELECT `UserRequest` FROM UserRequest AS `UserRequest` WHERE (`UserRequest`.`tagfield` = '')", 'ExpectedOQL' => "SELECT `".TAG_CLASS."` FROM ".TAG_CLASS." AS `".TAG_CLASS."` WHERE (`".TAG_CLASS."`.`".TAG_ATTCODE."` = '')",
'ExpectedCriterion' => array(array('widget' => 'tag_set')), 'ExpectedCriterion' => array(array('widget' => 'tag_set')),
), ),
'TagSet Undefined and tag' => array( 'TagSet Undefined and tag' => array(
'OQL' => "SELECT UserRequest WHERE (((tagfield MATCHES 'tag1 tag2') OR (tagfield = '')) AND 1)", 'OQL' => "SELECT ".TAG_CLASS." WHERE (((".TAG_ATTCODE." MATCHES 'tag1 tag2') OR (".TAG_ATTCODE." = '')) AND 1)",
'ExpectedOQL' => "SELECT `UserRequest` FROM UserRequest AS `UserRequest` WHERE ((`UserRequest`.`tagfield` MATCHES 'tag1 tag2' OR (`UserRequest`.`tagfield` = '')) AND 1)", 'ExpectedOQL' => "SELECT `".TAG_CLASS."` FROM ".TAG_CLASS." AS `".TAG_CLASS."` WHERE ((`".TAG_CLASS."`.`".TAG_ATTCODE."` MATCHES 'tag1 tag2' OR (`".TAG_CLASS."`.`".TAG_ATTCODE."` = '')) AND 1)",
'ExpectedCriterion' => array(array('widget' => 'tag_set')), 'ExpectedCriterion' => array(array('widget' => 'tag_set')),
), ),
'TagSet equals' => array( 'TagSet equals' => array(
'OQL' => "SELECT UserRequest WHERE tagfield = 'tag1 tag2'", 'OQL' => "SELECT ".TAG_CLASS." WHERE ".TAG_ATTCODE." = 'tag1 tag2'",
'ExpectedOQL' => "SELECT `UserRequest` FROM UserRequest AS `UserRequest` WHERE (`UserRequest`.`tagfield` MATCHES 'tag1' AND `UserRequest`.`tagfield` MATCHES 'tag2')", 'ExpectedOQL' => "SELECT `".TAG_CLASS."` FROM ".TAG_CLASS." AS `".TAG_CLASS."` WHERE (`".TAG_CLASS."`.`".TAG_ATTCODE."` MATCHES 'tag1' AND `".TAG_CLASS."`.`".TAG_ATTCODE."` MATCHES 'tag2')",
'ExpectedCriterion' => array(array('widget' => 'tag_set')), 'ExpectedCriterion' => array(array('widget' => 'tag_set')),
), ),

View File

@@ -41,18 +41,18 @@ class DBSearchCommitTest extends ItopDataTestCase
$this->CreateTagData(TAG_CLASS, TAG_ATTCODE, 'tag1', 'UNIT First'); $this->CreateTagData(TAG_CLASS, TAG_ATTCODE, 'tag1', 'UNIT First');
$this->CreateTagData(TAG_CLASS, TAG_ATTCODE, 'tag2', 'UNIT Second'); $this->CreateTagData(TAG_CLASS, TAG_ATTCODE, 'tag2', 'UNIT Second');
//Use it //Use it
$oTicket = $this->CreateTicket(1); $oObjWithTagSet = $this->CreateObjectWithTagSet();
$oTicket->Set(TAG_ATTCODE, 'tag1'); $oObjWithTagSet->Set(TAG_ATTCODE, 'tag1');
$oTicket->DBWrite(); $oObjWithTagSet->DBWrite();
$oSearch = DBSearch::FromOQL("SELECT UserRequest"); $oSearch = DBSearch::FromOQL("SELECT ".TAG_CLASS);
$oSearch->AddCondition(TAG_ATTCODE, 'tag1', 'MATCHES'); $oSearch->AddCondition(TAG_ATTCODE, 'tag1', 'MATCHES');
$oSet = new \DBObjectSet($oSearch); $oSet = new \DBObjectSet($oSearch);
static::assertEquals(1, $oSet->Count()); static::assertEquals(1, $oSet->Count());
$oTicket->Set(TAG_ATTCODE, 'tag1 tag2'); $oObjWithTagSet->Set(TAG_ATTCODE, 'tag1 tag2');
$oTicket->DBWrite(); $oObjWithTagSet->DBWrite();
$oSet = new \DBObjectSet($oSearch); $oSet = new \DBObjectSet($oSearch);
static::assertEquals(1, $oSet->Count()); static::assertEquals(1, $oSet->Count());
@@ -73,18 +73,18 @@ class DBSearchCommitTest extends ItopDataTestCase
$this->CreateTagData(TAG_CLASS, TAG_ATTCODE, 'tag1', 'UNIT First'); $this->CreateTagData(TAG_CLASS, TAG_ATTCODE, 'tag1', 'UNIT First');
$this->CreateTagData(TAG_CLASS, TAG_ATTCODE, 'tag2', 'UNIT Second'); $this->CreateTagData(TAG_CLASS, TAG_ATTCODE, 'tag2', 'UNIT Second');
//Use it //Use it
$oTicket = $this->CreateTicket(1); $oObjWithTagSet = $this->CreateObjectWithTagSet();
$oTicket->Set(TAG_ATTCODE, 'tag1'); $oObjWithTagSet->Set(TAG_ATTCODE, 'tag1');
$oTicket->DBWrite(); $oObjWithTagSet->DBWrite();
$oSearch = DBSearch::FromOQL("SELECT UserRequest"); $oSearch = DBSearch::FromOQL("SELECT ".TAG_CLASS);
$oSearch->AddCondition(TAG_ATTCODE, 'tag1'); $oSearch->AddCondition(TAG_ATTCODE, 'tag1');
$oSet = new \DBObjectSet($oSearch); $oSet = new \DBObjectSet($oSearch);
static::assertEquals(1, $oSet->Count()); static::assertEquals(1, $oSet->Count());
$oTicket->Set(TAG_ATTCODE, 'tag1 tag2'); $oObjWithTagSet->Set(TAG_ATTCODE, 'tag1 tag2');
$oTicket->DBWrite(); $oObjWithTagSet->DBWrite();
$oSet = new \DBObjectSet($oSearch); $oSet = new \DBObjectSet($oSearch);
static::assertEquals(0, $oSet->Count()); static::assertEquals(0, $oSet->Count());

View File

@@ -105,9 +105,9 @@ class TagSetFieldDataTest extends ItopDataTestCase
// Create a tag // Create a tag
$oTagData = $this->CreateTagData(TAG_CLASS, TAG_ATTCODE, 'tag1', 'First'); $oTagData = $this->CreateTagData(TAG_CLASS, TAG_ATTCODE, 'tag1', 'First');
//Use it //Use it
$oTicket = $this->CreateTicket(1); $oObjWithTagSet = $this->CreateObjectWithTagSet();
$oTicket->Set(TAG_ATTCODE, 'tag1'); $oObjWithTagSet->Set(TAG_ATTCODE, 'tag1');
$oTicket->DBWrite(); $oObjWithTagSet->DBWrite();
// Try to delete the tag, must complain ! // Try to delete the tag, must complain !
try try
@@ -203,9 +203,9 @@ class TagSetFieldDataTest extends ItopDataTestCase
$oTagData->DBWrite(); $oTagData->DBWrite();
//Use it //Use it
$oTicket = $this->CreateTicket(1); $oObjWithTagSet = $this->CreateObjectWithTagSet();
$oTicket->Set(TAG_ATTCODE, 'tag2'); $oObjWithTagSet->Set(TAG_ATTCODE, 'tag2');
$oTicket->DBWrite(); $oObjWithTagSet->DBWrite();
// Try to change the code of the tag, must complain ! // Try to change the code of the tag, must complain !
try try
@@ -264,7 +264,7 @@ class TagSetFieldDataTest extends ItopDataTestCase
$sTagCode = 'MaxTag'.$i; $sTagCode = 'MaxTag'.$i;
$this->CreateTagData(TAG_CLASS, TAG_ATTCODE, $sTagCode, $sTagCode); $this->CreateTagData(TAG_CLASS, TAG_ATTCODE, $sTagCode, $sTagCode);
} }
$oTicket = $this->CreateTicket(1); $oObjWithTagSet = $this->CreateObjectWithTagSet();
$this->debug("Max number of tags is $iMaxTags"); $this->debug("Max number of tags is $iMaxTags");
$sValue = ''; $sValue = '';
for ($i = 0; $i < ($iMaxTags + 1); $i++) for ($i = 0; $i < ($iMaxTags + 1); $i++)
@@ -273,8 +273,8 @@ class TagSetFieldDataTest extends ItopDataTestCase
{ {
$sTagCode = 'MaxTag'.$i; $sTagCode = 'MaxTag'.$i;
$sValue .= "$sTagCode "; $sValue .= "$sTagCode ";
$oTicket->Set(TAG_ATTCODE, $sValue); $oObjWithTagSet->Set(TAG_ATTCODE, $sValue);
$oTicket->DBWrite(); $oObjWithTagSet->DBWrite();
} catch (\Exception $e) } catch (\Exception $e)
{ {
// Should fail on the last iteration // Should fail on the last iteration

View File

@@ -60,7 +60,7 @@ class ormTagSetTest extends ItopDataTestCase
public function testGetTagDataClass() public function testGetTagDataClass()
{ {
$oTagSet = new ormTagSet(TAG_CLASS, TAG_ATTCODE, MAX_TAGS); $oTagSet = new ormTagSet(TAG_CLASS, TAG_ATTCODE, MAX_TAGS);
static::assertEquals($oTagSet->GetTagDataClass(), 'TagSetFieldDataFor_Ticket__tagfield'); static::assertEquals($oTagSet->GetTagDataClass(), 'TagSetFieldDataFor_'.TAG_CLASS.'__'.TAG_ATTCODE);
} }
public function testGetValue() public function testGetValue()