From 3e35dafefb917d51c1a4cae40d244da7f8d180da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eric=20Espi=C3=A9?= Date: Fri, 22 Dec 2017 13:37:26 +0000 Subject: [PATCH] Unit tests based on PHPUnit These tests run with the sample datamodel and they don't commit any data (only some indexes are incremented). To launch the test suite, run the following command from the test repository: php.exe /phpunit.phar --configuration /test/PHPunit.xml SVN:trunk[5164] --- test/ItopDataTestCase.php | 453 +++++++++++++ test/ItopTestCase.php | 50 ++ test/PHPunit.xml | 61 ++ test/core/DBObjectTest.php | 83 +++ test/core/apcEmulationTest.php | 195 ++++++ test/core/dictTest.php | 58 ++ test/core/mockApcEmulation.php | 52 ++ test/core/mockDict.php | 26 + test/core/ormLinkSetTest.php | 245 +++++++ test/itop-tickets/itopTicketTest.php | 929 +++++++++++++++++++++++++++ test/unittestautoload.php | 5 + 11 files changed, 2157 insertions(+) create mode 100644 test/ItopDataTestCase.php create mode 100644 test/ItopTestCase.php create mode 100644 test/PHPunit.xml create mode 100644 test/core/DBObjectTest.php create mode 100644 test/core/apcEmulationTest.php create mode 100644 test/core/dictTest.php create mode 100644 test/core/mockApcEmulation.php create mode 100644 test/core/mockDict.php create mode 100644 test/core/ormLinkSetTest.php create mode 100644 test/itop-tickets/itopTicketTest.php create mode 100644 test/unittestautoload.php diff --git a/test/ItopDataTestCase.php b/test/ItopDataTestCase.php new file mode 100644 index 000000000..b581c7419 --- /dev/null +++ b/test/ItopDataTestCase.php @@ -0,0 +1,453 @@ + +// + +namespace Combodo\iTop\Test\UnitTest; +/** + * Created by PhpStorm. + * User: Eric + * Date: 20/11/2017 + * Time: 11:21 + */ + +use ArchivedObjectException; +use CMDBSource; +use Contact; +use DBObject; +use Exception; +use Farm; +use FunctionalCI; +use Hypervisor; +use lnkContactToFunctionalCI; +use MetaModel; +use Person; +use PHPUnit\Framework\TestCase; +use Server; +use Ticket; +use VirtualHost; +use VirtualMachine; + + +/** + * @runTestsInSeparateProcesses + * @preserveGlobalState disabled + * @backupGlobals disabled + */ +class ItopDataTestCase extends ItopTestCase +{ + protected $testOrgId; + + /** + * @throws Exception + */ + protected function setUp() + { + parent::setUp(); + //require_once(APPROOT.'/application/startup.inc.php'); + + require_once(APPROOT.'/core/cmdbobject.class.inc.php'); + require_once(APPROOT.'/application/utils.inc.php'); + require_once(APPROOT.'/core/contexttag.class.inc.php'); + $sEnv = 'production'; + $sConfigFile = APPCONF.$sEnv.'/'.ITOP_CONFIG_FILE; + MetaModel::Startup($sConfigFile, false /* $bModelOnly */, true /* $bAllowCache */, false /* $bTraceSourceFiles */, $sEnv); + + CMDBSource::Query('START TRANSACTION'); + + // Create a specific organization for the tests + $oOrg = MetaModel::NewObject('Organization', array('name' => 'UnitTestOrganization')); + $this->testOrgId = $oOrg->DBInsert(); + } + + /** + * @throws Exception + */ + protected function tearDown() + { + CMDBSource::Query('ROLLBACK'); + } + + /** + * @return mixed + */ + public function getTestOrgId() + { + return $this->testOrgId; + } + + /** + * PHPUnit complains that there is no test... so here is a dummy one. + */ + public function testDummy() + { + $this->assertTrue(true); + } + + ///////////////////////////////////////////////////////////////////////////// + /// Database Utilities + ///////////////////////////////////////////////////////////////////////////// + + /** + * @param string $sClass + * @param array $aParams + * @return DBObject + * @throws Exception + */ + protected static function createObject($sClass, $aParams) + { + $oMyObj = MetaModel::NewObject($sClass); + foreach($aParams as $sAttCode => $oValue) + { + $oMyObj->Set($sAttCode, $oValue); + } + $oMyObj->DBInsert(); + return $oMyObj; + } + + /** + * Create a Ticket in database + * + * @param int $iNum + * @return Ticket + * @throws Exception + */ + protected function CreateTicket($iNum) + { + /** @var Ticket $oTicket */ + $oTicket = self::createObject('UserRequest', array( + 'ref' => 'Ticket_'.$iNum, + 'title' => 'BUG 789_'.$iNum, + 'request_type' => 'incident', + 'description' => 'method UpdateImpactedItems() reconstruit le lnkContactToTicket donc impossible de rajouter des champs dans cette classe', + 'org_id' => $this->getTestOrgId(), + )); + $this->debug("\nCreated {$oTicket->Get('title')} ({$oTicket->Get('ref')})\n"); + return $oTicket; + } + + /** + * Create a Server in database + * @param int $iNum + * @return Server + * @throws Exception + */ + protected function CreateServer($iNum) + { + /** @var Server $oServer */ + $oServer = self::createObject('Server', array( + 'name' => 'Server_'.$iNum, + 'org_id' => $this->getTestOrgId(), + )); + $this->debug("Created {$oServer->GetName()} ({$oServer->GetKey()})\n"); + return $oServer; + } + + /** + * Create a Person in database + * @param int $iNum + * @return Person + * @throws Exception + */ + protected function CreatePerson($iNum) + { + /** @var Person $oPerson */ + $oPerson = self::createObject('Person', array( + 'name' => 'Person_'.$iNum, + 'first_name' => 'Test', + 'org_id' => $this->getTestOrgId(), + )); + $this->debug("Created {$oPerson->GetName()} ({$oPerson->GetKey()})\n"); + return $oPerson; + } + + /** + * Create a Hypervisor in database + * @param int $iNum + * @param Server $oServer + * @param Farm $oFarm + * @return Hypervisor + * @throws Exception + */ + protected function CreateHypervisor($iNum, $oServer, $oFarm = null) + { + /** @var Hypervisor $oHypervisor */ + $oHypervisor = self::createObject('Hypervisor', array( + 'name' => 'Hypervisor_'.$iNum, + 'org_id' => $this->getTestOrgId(), + 'server_id' => $oServer->GetKey(), + 'farm_id' => is_null($oFarm) ? 0 : $oFarm->GetKey(), + )); + if (is_null($oFarm)) + { + $this->debug("Created {$oHypervisor->GetName()} ({$oHypervisor->GetKey()}) on {$oServer->GetName()}\n"); + } + else + { + $this->debug("Created {$oHypervisor->GetName()} ({$oHypervisor->GetKey()}) on {$oServer->GetName()} part of {$oFarm->GetName()}\n"); + } + 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 = self::createObject('Farm', array( + 'name' => 'Farm_'.$iNum, + 'org_id' => $this->getTestOrgId(), + 'redundancy' => $sRedundancy, + )); + $this->debug("Created {$oFarm->GetName()} ({$oFarm->GetKey()}) redundancy $sRedundancy\n"); + 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 = self::createObject('VirtualMachine', array( + 'name' => 'VirtualMachine_'.$iNum, + 'org_id' => $this->getTestOrgId(), + 'virtualhost_id' => $oVirtualHost->GetKey(), + )); + $this->debug("Created {$oVirtualMachine->GetName()} ({$oVirtualMachine->GetKey()}) on {$oVirtualHost->GetName()}\n"); + return $oVirtualMachine; + } + + + /** + * Add a link between a contact and a CI. + * The database is not updated. + * + * @param Contact $oContact + * @param FunctionalCI $oCI + * @return lnkContactToFunctionalCI + * @throws Exception + */ + protected function AddContactToCI($oContact, $oCI) + { + $oNewLink = new lnkContactToFunctionalCI(); + $oNewLink->Set('contact_id', $oContact->GetKey()); + $oContacts = $oCI->Get('contacts_list'); + $oContacts->AddItem($oNewLink); + $oCI->Set('contacts_list', $oContacts); + + $this->debug("Added {$oContact->GetName()} to {$oCI->GetName()}\n"); + 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')}\n"); + return; + } + } + } + + /** + * Add a link between a CI and a Ticket. + * The database is not updated. + * + * @param FunctionalCI $oCI + * @param Ticket $oTicket + * @param string $sImpactCode + * @throws Exception + */ + protected function AddCIToTicket($oCI, $oTicket, $sImpactCode) + { + $oNewLink = new \lnkFunctionalCIToTicket(); + $oNewLink->Set('functionalci_id', $oCI->GetKey()); + $oNewLink->Set('impact_code', $sImpactCode); + $oCIs = $oTicket->Get('functionalcis_list'); + $oCIs->AddItem($oNewLink); + $oTicket->Set('functionalcis_list', $oCIs); + + $this->debug("Added {$oCI->GetName()} to {$oTicket->Get('ref')} with {$sImpactCode}\n"); + return array($oCI->GetKey() => $sImpactCode); + } + + /** + * Remove a link between a CI and a Ticket. + * The database is not updated. + * + * @param FunctionalCI $oCI + * @param Ticket $oTicket + * @throws Exception + */ + protected function RemoveCIFromTicket($oCI, $oTicket) + { + $oCIs = $oTicket->Get('functionalcis_list'); + foreach($oCIs as $oLnk) + { + if ($oLnk->Get('functionalci_id') == $oCI->GetKey()) + { + $sImpactCode = $oLnk->Get('impact_code'); + $oCIs->RemoveItem($oLnk->GetKey()); + $oTicket->Set('functionalcis_list', $oCIs); + $this->debug("Removed {$oCI->GetName()} from {$oTicket->Get('ref')} ({$sImpactCode})\n"); + return; + } + } + $this->debug("ERROR: {$oCI->GetName()} not attached to {$oTicket->Get('ref')}\n"); + $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}\n"); + 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})\n"); + return; + } + } + } + + /** + * Reload a Ticket from the database. + * + * @param DBObject $oObject + * @throws ArchivedObjectException + * @throws Exception + */ + protected function ReloadObject(&$oObject) + { + $oObject = MetaModel::GetObject(get_class($oObject), $oObject->GetKey()); + 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()}):\n"); + foreach($oTicket->Get('functionalcis_list') as $oLnk) + { + $this->debug($oLnk->Get('functionalci_name')." => ".$oLnk->Get('impact_code')."\n"); + $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). + * 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()}):\n"); + foreach($oTicket->Get('contacts_list') as $oLnk) + { + $this->debug($oLnk->Get('contact_id_friendlyname')." => ".$oLnk->Get('role_code')."\n"); + $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)); + } + } + } + } + } + + +} \ No newline at end of file diff --git a/test/ItopTestCase.php b/test/ItopTestCase.php new file mode 100644 index 000000000..f349076bd --- /dev/null +++ b/test/ItopTestCase.php @@ -0,0 +1,50 @@ + +// + +namespace Combodo\iTop\Test\UnitTest; +/** + * Created by PhpStorm. + * User: Eric + * Date: 20/11/2017 + * Time: 11:21 + */ + +use PHPUnit\Framework\TestCase; + +define('DEBUG_UNIT_TEST', false); + +class ItopTestCase extends TestCase +{ + protected function setUp() + { + @include_once '../approot.inc.php'; + @include_once '../../approot.inc.php'; + @include_once '../../../approot.inc.php'; + + $this->debug("\n----------\n---------- ".$this->getName()."\n----------\n\n"); + } + + protected function debug($sMsg) + { + if (DEBUG_UNIT_TEST) + { + echo $sMsg; + } + } +} \ No newline at end of file diff --git a/test/PHPunit.xml b/test/PHPunit.xml new file mode 100644 index 000000000..7dce7bb0e --- /dev/null +++ b/test/PHPunit.xml @@ -0,0 +1,61 @@ + + + + + + + + + + + + + + + + + + + + + + + core + + + itop-tickets + + + + + + + ../core/apc-emulation.php + ../core/ormlinkset.class.inc.php + ../datamodels/2.x/itop-tickets/main.itop-tickets.php + + + + diff --git a/test/core/DBObjectTest.php b/test/core/DBObjectTest.php new file mode 100644 index 000000000..24afa0904 --- /dev/null +++ b/test/core/DBObjectTest.php @@ -0,0 +1,83 @@ + +// + +/** + * Created by PhpStorm. + * User: Eric + * Date: 02/10/2017 + * Time: 13:58 + */ + +namespace Combodo\iTop\Test\UnitTest\Core; + +use Combodo\iTop\Test\UnitTest\ItopTestCase; +use DBObject; +use PHPUnit\Framework\TestCase; + + +/** + * @runTestsInSeparateProcesses + * @preserveGlobalState disabled + * @backupGlobals disabled + */ +class DBObjectTest extends ItopTestCase +{ + protected function setUp() + { + parent::setUp(); + require_once(APPROOT.'core/coreexception.class.inc.php'); + require_once(APPROOT.'core/dbobject.class.php'); + } + + /** + * Test default page name + */ + public function testGetUIPage() + { + $this->assertEquals('UI.php', DBObject::GetUIPage()); + } + + /** + * Test PKey validation + * @dataProvider keyProviderOK + * @param $key + * @param $res + */ + public function testIsValidPKeyOK($key, $res) + { + $this->assertEquals(DBObject::IsValidPKey($key), $res); + } + + public function keyProviderOK() + { + return array( + array(1, true), + array('255', true), + array(-24576, true), + array(0123, true), + array(0xCAFE, true), + array(PHP_INT_MIN, true), + array(PHP_INT_MAX, true), + array('test', false), + array('', false), + array('a255', false), + array('PHP_INT_MIN', false)); + } + +} diff --git a/test/core/apcEmulationTest.php b/test/core/apcEmulationTest.php new file mode 100644 index 000000000..25923f9dd --- /dev/null +++ b/test/core/apcEmulationTest.php @@ -0,0 +1,195 @@ + +// + +/** + * Created by PhpStorm. + * User: Eric + * Date: 31/10/2017 + * Time: 14:10 + */ + +namespace Combodo\iTop\Test\UnitTest\Core; + +use Combodo\iTop\Test\UnitTest\ItopTestCase; +use PHPUnit\Framework\TestCase; + +define('UNIT_MAX_CACHE_FILES', 10); + + +/** + * @runTestsInSeparateProcesses + * @preserveGlobalState disabled + * @backupGlobals disabled + */ +class apcEmulationTest extends ItopTestCase +{ + + protected function setUp() + { + parent::setUp(); + require_once(APPROOT.'core/apc-emulation.php'); + require_once 'mockApcEmulation.php'; + apc_clear_cache(); + } + + public function tearDown() + { + apc_clear_cache(); + } + + public function testBasic() + { + $this->assertTrue(apc_store('test-ttl', 'This is a test with TTL', 100)); + $this->assertTrue(apc_store('test-nottl', 'This is a test without TTL')); + + $this->assertEquals('This is a test with TTL', apc_fetch('test-ttl')); + $this->assertEquals('This is a test without TTL', apc_fetch('test-nottl')); + } + + public function testMultiple() + { + for($i = 0; $i < UNIT_MAX_CACHE_FILES; $i++) + { + $this->assertTrue(apc_store('testMultiple'.$i, 'This is a test', 100)); + } + $aInfo = apc_cache_info(); + $this->assertEquals(UNIT_MAX_CACHE_FILES, count($aInfo['cache_list'])); + } + + public function testNumberOfFilesTTL() + { + for($i = 0; $i < 2 * UNIT_MAX_CACHE_FILES; $i++) + { + $this->assertTrue(apc_store('testNumberOfFilesTTL'.$i, 'This is a test', 100)); + } + $aInfo = apc_cache_info(); + $this->assertEquals(UNIT_MAX_CACHE_FILES, count($aInfo['cache_list'])); + + $this->assertFalse(apc_fetch('testNumberOfFilesTTL0')); + } + + public function testNumberOfFilesNoTTL() + { + for($i = 0; $i < 2 * UNIT_MAX_CACHE_FILES; $i++) + { + $this->assertTrue(apc_store('testNumberOfFilesNoTTL'.$i, 'This is a test')); + } + $aInfo = apc_cache_info(); + $this->assertEquals(2 * UNIT_MAX_CACHE_FILES, count($aInfo['cache_list'])); + + $this->assertTrue(apc_fetch('testNumberOfFilesNoTTL0') !== false); + } + + public function testArray() + { + $aStoredEntries = array(); + $aFetchedEntries = array(); + for($i = 0; $i < UNIT_MAX_CACHE_FILES; $i++) + { + $sKey = 'testArray'.$i; + $aStoredEntries[$sKey] = 'This is a test ARRAY'.rand(); + $aFetchedEntries[] = $sKey; + } + $aResStore = apc_store($aStoredEntries); + $this->assertEquals(UNIT_MAX_CACHE_FILES, count($aResStore)); + foreach($aResStore as $bValue) + { + $this->assertTrue($bValue); + } + + $aInfo = apc_cache_info(); + $this->assertEquals(UNIT_MAX_CACHE_FILES, count($aInfo['cache_list'])); + + $aResFetch = apc_fetch($aFetchedEntries); + $this->assertEquals(UNIT_MAX_CACHE_FILES, count($aResFetch)); + + foreach($aResFetch as $sKey => $sValue) + { + $this->assertEquals($aStoredEntries[$sKey], $sValue); + } + } + + + public function testSanity() + { + $this->assertTrue(apc_store('testSanity', null, 100)); + $this->assertTrue(is_null(apc_fetch('testSanity'))); + + $this->assertFalse(apc_store(null, 'testSanity', 100)); + $this->assertFalse(apc_fetch(null)); + + $this->assertTrue(apc_store('testSanity2', null)); + $this->assertFalse(apc_store(null, 'testSanity2')); + $this->assertFalse(apc_store('', 'testSanity2')); + + $this->assertFalse(apc_delete(null)); + $this->assertFalse(apc_delete('')); + } + + public function testDelete() + { + $this->assertTrue(apc_store('test-ttl', 'This is a test with TTL', 100)); + $this->assertTrue(apc_store('test-nottl', 'This is a test without TTL')); + + $this->assertTrue(apc_delete('test-ttl')); + $this->assertFalse(apc_delete('test-ttl')); + $this->assertTrue(apc_delete('test-nottl')); + + $this->assertFalse(apc_fetch('test-ttl')); + $this->assertFalse(apc_fetch('test-nottl')); + } + + public function testReuseSameKey() + { + // first use of the key + $this->assertTrue(apc_store('testReuseSameKey', 'This is a test with TTL', 100)); + $this->assertEquals('This is a test with TTL', apc_fetch('testReuseSameKey')); + // same key but no ttl + $this->assertTrue(apc_store('testReuseSameKey', 'This is a test without TTL')); + $this->assertEquals('This is a test without TTL', apc_fetch('testReuseSameKey')); + // same key with ttl + $this->assertTrue(apc_store('testReuseSameKey', 'This is a test with TTL', 100)); + $this->assertEquals('This is a test with TTL', apc_fetch('testReuseSameKey')); + // same key with ttl but other content + $this->assertTrue(apc_store('testReuseSameKey', 'This is a test', 100)); + $this->assertEquals('This is a test', apc_fetch('testReuseSameKey')); + // remove entry + $this->assertTrue(apc_delete('testReuseSameKey')); + // check entry is removed + $this->assertFalse(apc_fetch('testReuseSameKey')); + } + + public function testHuge() + { + $ilen = 20000000; + $sContent = str_pad(' TEST ', $ilen, "-=", STR_PAD_BOTH); + for($i = 0; $i < UNIT_MAX_CACHE_FILES; $i++) + { + $this->assertTrue(apc_store('testHuge'.$i, $sContent, 100)); + } + $aInfo = apc_cache_info(); + $this->assertEquals(UNIT_MAX_CACHE_FILES, count($aInfo['cache_list'])); + + for($i = 0; $i < UNIT_MAX_CACHE_FILES; $i++) + { + $this->assertEquals($ilen, strlen(apc_fetch('testHuge'.$i))); + } + } + +} \ No newline at end of file diff --git a/test/core/dictTest.php b/test/core/dictTest.php new file mode 100644 index 000000000..02d39e9af --- /dev/null +++ b/test/core/dictTest.php @@ -0,0 +1,58 @@ + +// + +/** + * Created by PhpStorm. + * User: Eric + * Date: 30/10/2017 + * Time: 13:43 + */ + +namespace Combodo\iTop\Test\UnitTest\Core; + +use Combodo\iTop\Test\UnitTest\ItopTestCase; +use Dict; +use Exception; +use PHPUnit\Framework\TestCase; + + +/** + * @runTestsInSeparateProcesses + * @preserveGlobalState disabled + * @backupGlobals disabled + */ +class dictTest extends ItopTestCase +{ + protected function setUp() + { + parent::setUp(); + require_once (APPROOT.'core/coreexception.class.inc.php'); + require_once (APPROOT.'core/dict.class.inc.php'); + require_once 'mockDict.php'; + } + + /** + * @throws Exception + */ + public function testType() + { + $this->assertInternalType('string', Dict::S('Core:AttributeURL')); + $this->assertInternalType('string', Dict::Format('Change:AttName_SetTo', '1', '2')); + } +} \ No newline at end of file diff --git a/test/core/mockApcEmulation.php b/test/core/mockApcEmulation.php new file mode 100644 index 000000000..ff9730305 --- /dev/null +++ b/test/core/mockApcEmulation.php @@ -0,0 +1,52 @@ + +// + +/** + * Created by PhpStorm. + * User: Eric + * Date: 03/11/2017 + * Time: 09:55 + */ + +// --------------- Mock +class utils +{ + public static function GetCachePath() + { + return APPROOT.'data/cache-unittest/'; + } +} + +class UnitConfig +{ + public function Get($sPropCode) + { + return UNIT_MAX_CACHE_FILES; + } +} + +class MetaModel +{ + public static function GetConfig() + { + return new UnitConfig; + } +} + +// --------------- End Mock \ No newline at end of file diff --git a/test/core/mockDict.php b/test/core/mockDict.php new file mode 100644 index 000000000..c50c1a5a9 --- /dev/null +++ b/test/core/mockDict.php @@ -0,0 +1,26 @@ + +// + +class utils +{ + public static function GetCurrentEnvironment() + { + return 'production'; + } +} \ No newline at end of file diff --git a/test/core/ormLinkSetTest.php b/test/core/ormLinkSetTest.php new file mode 100644 index 000000000..9d62ac70b --- /dev/null +++ b/test/core/ormLinkSetTest.php @@ -0,0 +1,245 @@ + +// + + +/** + * Created by PhpStorm. + * User: Eric + * Date: 20/12/2017 + * Time: 11:56 + */ + + +namespace Combodo\iTop\Test\UnitTest\Core; + +use Combodo\iTop\Test\UnitTest\ItopDataTestCase; +use Exception; +use ormLinkSet; +use PHPUnit\Framework\TestCase; + + +/** + * Tests of the ormLinkSet class using N-N links between FunctionalCI and Contact + * + * @runTestsInSeparateProcesses + * @preserveGlobalState disabled + * @backupGlobals disabled + */ +class ormLinkSetTest extends ItopDataTestCase +{ + + /** + * @throws Exception + */ + protected function setUp() + { + parent::setUp(); + } + + /** + * + */ + public function testConstruct() + { + $oOrmLink = new ormLinkSet('UserRequest', 'contacts_list'); + $this->assertNotNull($oOrmLink); + } + + /** + * + */ + public function testConstruct2() + { + $this->expectException('Exception'); + + new ormLinkSet('UserRequest', 'ref'); + } + + /** + * @throws Exception + */ + public function testBasic() + { + $oServer = $this->CreateServer(1); + $aPersons = array(); + for ($i = 0; $i < 3; $i++) + { + $oPerson = $this->CreatePerson($i); + $aPersons[] = $oPerson; + $this->AddContactToCI($oPerson, $oServer); + } + $oServer->DBUpdate(); + $this->ReloadObject($oServer); + + $oContactsSet = $oServer->Get('contacts_list'); + $this->assertEquals(3, $oContactsSet->Count()); + } + + /** + * @throws Exception + */ + public function testSuccesiveAdds() + { + $oServer = $this->CreateServer(1); + $aPersons = array(); + for ($i = 0; $i < 3; $i++) + { + $oPerson = $this->CreatePerson($i); + $aPersons[] = $oPerson; + $this->AddContactToCI($oPerson, $oServer); + } + $oServer->DBUpdate(); + $this->ReloadObject($oServer); + + $oContactsSet = $oServer->Get('contacts_list'); + $this->assertEquals(3, $oContactsSet->Count()); + + $this->AddContactToCI($this->CreatePerson($i), $oServer); + $oContactsSet = $oServer->Get('contacts_list'); + $this->assertEquals(4, $oContactsSet->Count()); + + $oServer->DBUpdate(); + $this->ReloadObject($oServer); + + $oContactsSet = $oServer->Get('contacts_list'); + $this->assertEquals(4, $oContactsSet->Count()); + } + + /** + * @throws Exception + */ + public function testRemove() + { + $oServer = $this->CreateServer(1); + $aPersons = array(); + for ($i = 0; $i < 3; $i++) + { + $oPerson = $this->CreatePerson($i); + $aPersons[] = $oPerson; + $this->AddContactToCI($oPerson, $oServer); + } + $oServer->DBUpdate(); + $this->ReloadObject($oServer); + + $oContactsSet = $oServer->Get('contacts_list'); + $this->assertEquals(3, $oContactsSet->Count()); + + for ($i = 0; $i < 3; $i++) + { + $this->RemoveContactFromCI($aPersons[$i], $oServer); + } + $oContactsSet = $oServer->Get('contacts_list'); + $this->assertEquals(0, $oContactsSet->Count()); + + $oServer->DBUpdate(); + $this->ReloadObject($oServer); + + $oContactsSet = $oServer->Get('contacts_list'); + $this->assertEquals(0, $oContactsSet->Count()); + } + + /** + * @throws Exception + */ + public function testAddThenRemove() + { + $oServer = $this->CreateServer(1); + for ($i = 0; $i < 3; $i++) + { + $oPerson = $this->CreatePerson($i); + $this->AddContactToCI($oPerson, $oServer); + $this->RemoveContactFromCI($oPerson, $oServer); + } + $oContactsSet = $oServer->Get('contacts_list'); + $this->assertEquals(0, $oContactsSet->Count()); + + $oServer->DBUpdate(); + $this->ReloadObject($oServer); + + $oContactsSet = $oServer->Get('contacts_list'); + $this->assertEquals(0, $oContactsSet->Count()); + } + + /** + * @throws Exception + */ + public function testRemoveThenAdd() + { + $oServer = $this->CreateServer(1); + $aPersons = array(); + for ($i = 0; $i < 3; $i++) + { + $oPerson = $this->CreatePerson($i); + $aPersons[] = $oPerson; + $this->AddContactToCI($oPerson, $oServer); + } + $oServer->DBUpdate(); + $this->ReloadObject($oServer); + + $oContactsSet = $oServer->Get('contacts_list'); + $this->assertEquals(3, $oContactsSet->Count()); + + for ($i = 0; $i < 3; $i++) + { + $this->RemoveContactFromCI($aPersons[$i], $oServer); + $this->AddContactToCI($aPersons[$i], $oServer); + } + $oContactsSet = $oServer->Get('contacts_list'); + $this->assertEquals(3, $oContactsSet->Count()); + + $oServer->DBUpdate(); + $this->ReloadObject($oServer); + + $oContactsSet = $oServer->Get('contacts_list'); + $this->assertEquals(3, $oContactsSet->Count()); + } + + /** + * @throws Exception + */ + public function testAddDuplicate() + { + $oServer = $this->CreateServer(1); + $aPersons = array(); + for ($i = 0; $i < 3; $i++) + { + $oPerson = $this->CreatePerson($i); + $aPersons[] = $oPerson; + $this->AddContactToCI($oPerson, $oServer); + } + $oServer->DBUpdate(); + $this->ReloadObject($oServer); + + $oContactsSet = $oServer->Get('contacts_list'); + $this->assertEquals(3, $oContactsSet->Count()); + + for ($i = 0; $i < 3; $i++) + { + $this->AddContactToCI($aPersons[$i], $oServer); + } + $oContactsSet = $oServer->Get('contacts_list'); + $this->assertEquals(6, $oContactsSet->Count()); + + $oServer->DBUpdate(); + $this->ReloadObject($oServer); + + $oContactsSet = $oServer->Get('contacts_list'); + $this->assertEquals(3, $oContactsSet->Count()); + } +} diff --git a/test/itop-tickets/itopTicketTest.php b/test/itop-tickets/itopTicketTest.php new file mode 100644 index 000000000..f710dd0ba --- /dev/null +++ b/test/itop-tickets/itopTicketTest.php @@ -0,0 +1,929 @@ + +// + +/** + * Created by PhpStorm. + * User: Eric + * Date: 18/12/2017 + * Time: 13:34 + */ + +namespace Combodo\iTop\Test\UnitTest\iTopTickets; + +use Combodo\iTop\Test\UnitTest\ItopDataTestCase; +use Exception; +use PHPUnit\Framework\TestCase; + + +/** + * @runTestsInSeparateProcesses + * @preserveGlobalState disabled + * @backupGlobals disabled + */ +class ItopTicketTest extends ItopDataTestCase +{ + /** + * @throws Exception + */ + protected function setUp() + { + parent::setUp(); + } + + /** + *
+	 * Given:
+	 *
+	 * Server1+---->Hypervisor1+---->Person1
+	 *
+	 * Ticket+---->Server1 (manual)
+	 *
+	 * Result:
+	 *
+	 * Ticket+====>Server1,Hypervisor1
+	 *       |
+	 *       +====>Person1
+	 * 
+ * + * @throws Exception + */ + public function testUpdateImpactedItems_Basic() + { + $oTicket = $this->CreateTicket(1); + $oServer1 = $this->CreateServer(1); + $oHypervisor1 = $this->CreateHypervisor(1, $oServer1); + $oPerson1 = $this->CreatePerson(1); + $this->AddContactToCI($oPerson1, $oHypervisor1); + $oHypervisor1->DBUpdate(); + + $aAwaitedCIs = $this->AddCIToTicket($oServer1, $oTicket, 'manual'); + $oTicket->DBUpdate(); // trigger the impact update + $this->ReloadObject($oTicket); // reload the links + + // Add the computed CIs + $aAwaitedCIs = $aAwaitedCIs + array($oHypervisor1->GetKey() => 'computed'); + + $this->CheckFunctionalCIList($oTicket, $aAwaitedCIs); + + // Computed Contacts + $aAwaitedContacts = array($oPerson1->GetKey() => array('role_code' => 'computed')); + $this->CheckContactList($oTicket, $aAwaitedContacts); + $this->assertEquals(2, $oTicket->Get('functionalcis_list')->Count()); + $this->assertEquals(1, $oTicket->Get('contacts_list')->Count()); + } + + /** + *
+     * Given:
+     *
+     * Server1+---->Hypervisor1+---->Person1
+     *
+     * Ticket+---->Server1 (manual)
+     *
+     * Result:
+     *
+     * Ticket+====>Server1,Hypervisor1
+     *       |
+     *       +====>Person1
+     * 
+ * + * @throws Exception + */ + public function testUpdateImpactedItems_Basic2() + { + $oTicket = $this->CreateTicket(1); + $oServer1 = $this->CreateServer(1); + $oHypervisor1 = $this->CreateHypervisor(1, $oServer1); + $oPerson1 = $this->CreatePerson(1); + $this->AddContactToCI($oPerson1, $oHypervisor1); + $oHypervisor1->DBUpdate(); + + $this->AddCIToTicket($oServer1, $oTicket, 'manual'); + $oTicket->DBUpdate(); // trigger the impact update + + $this->ReloadObject($oTicket); // reload the links + + $this->CheckFunctionalCIList($oTicket); + $this->CheckContactList($oTicket); + $this->assertEquals(2, $oTicket->Get('functionalcis_list')->Count()); + $this->assertEquals(1, $oTicket->Get('contacts_list')->Count()); + + // Try removing computed entries + $this->RemoveCIFromTicket($oHypervisor1, $oTicket); + $this->RemoveContactFromTicket($oPerson1, $oTicket); + $oTicket->DBUpdate(); // trigger the impact update + $this->ReloadObject($oTicket); // reload the links + + $this->CheckFunctionalCIList($oTicket); + $this->CheckContactList($oTicket); + $this->assertEquals(2, $oTicket->Get('functionalcis_list')->Count()); + $this->assertEquals(1, $oTicket->Get('contacts_list')->Count()); + } + + /** + *
+     * Given:
+     *
+     * Server1+---->Hypervisor1+---->Person1
+     *
+     * Server2    Person2
+     *
+     * Ticket+---->Server1 (manual), Server2 (computed)
+     *
+     * Result:
+     *
+     * Ticket+====>Server1,Hypervisor1
+     *       |
+     *       +====>Person1
+     * 
+ * + * @throws Exception + */ + public function testUpdateImpactedItems_RemoveUnecessaryEntries() + { + $oTicket = $this->CreateTicket(1); + $oServer1 = $this->CreateServer(1); + $oHypervisor1 = $this->CreateHypervisor(1, $oServer1); + $oPerson1 = $this->CreatePerson(1); + $this->AddContactToCI($oPerson1, $oHypervisor1); + $oHypervisor1->DBUpdate(); + + $oServer2 = $this->CreateServer(2); + $oPerson2 = $this->CreatePerson(2); + + $this->AddCIToTicket($oServer1, $oTicket, 'manual'); + $this->AddCIToTicket($oServer2, $oTicket, 'computed'); + $this->AddContactToTicket($oPerson2, $oTicket, 'computed'); + $oTicket->DBUpdate(); // trigger the impact update + $this->ReloadObject($oTicket); // reload the links + + $this->CheckFunctionalCIList($oTicket); + $this->CheckContactList($oTicket); + $this->assertEquals(2, $oTicket->Get('functionalcis_list')->Count()); + $this->assertEquals(1, $oTicket->Get('contacts_list')->Count()); + } + + /** + * Create a first impact chain then remove the root cause, all the chain should be removed. + * + *
+     * Given:
+     *
+     * Server1+---->Hypervisor1+---->Person1
+     *
+     * Ticket+---->Server1 (manual)
+     *
+     * Result:
+     *
+     * Ticket+====>Server1,Hypervisor1
+     *       |
+     *       +====>Person1
+     *
+     * Then remove Server1
+     *
+     * Result:
+     *
+     * Ticket+====>
+     *       |
+     *       +====>
+     *
+     * 
+ * + * @throws Exception + */ + public function testUpdateImpactedItems_RemoveUnecessaryEntries2() + { + $oTicket = $this->CreateTicket(1); + $oServer1 = $this->CreateServer(1); + $oHypervisor1 = $this->CreateHypervisor(1, $oServer1); + $oPerson1 = $this->CreatePerson(1); + $this->AddContactToCI($oPerson1, $oHypervisor1); + $oHypervisor1->DBUpdate(); + + $this->AddCIToTicket($oServer1, $oTicket, 'manual'); + $oTicket->DBUpdate(); // trigger the impact update + $this->ReloadObject($oTicket); // reload the links + + $this->CheckFunctionalCIList($oTicket); + $this->CheckContactList($oTicket); + $this->assertEquals(2, $oTicket->Get('functionalcis_list')->Count()); + $this->assertEquals(1, $oTicket->Get('contacts_list')->Count()); + + $this->RemoveCIFromTicket($oServer1, $oTicket); + $oTicket->DBUpdate(); // trigger the impact update + $this->ReloadObject($oTicket); // reload the links + + $this->CheckFunctionalCIList($oTicket); + $this->CheckContactList($oTicket); + $this->assertEquals(0, $oTicket->Get('functionalcis_list')->Count()); + $this->assertEquals(0, $oTicket->Get('contacts_list')->Count()); + } + + + /** + *
+     *
+     * Server2+---->Hypervisor2+---->Person2
+     *
+     * Ticket+---->(empty)
+     *
+     * Result:
+     *
+     * Ticket+====>(empty)
+     * 
+ * + * @throws Exception + */ + public function testUpdateImpactedItems_NoImpact() + { + $oTicket = $this->CreateTicket(1); + $oServer2 = $this->CreateServer(2); + $oPerson2 = $this->CreatePerson(2); + $oHypervisor2 = $this->CreateHypervisor(2, $oServer2); + $this->AddContactToCI($oPerson2, $oHypervisor2); + $oHypervisor2->DBUpdate(); + + $oTicket->DBUpdate(); // trigger the impact update + $this->ReloadObject($oTicket); // reload the links + + $this->CheckFunctionalCIList($oTicket); + $this->CheckContactList($oTicket); + $this->assertEquals(0, $oTicket->Get('functionalcis_list')->Count()); + $this->assertEquals(0, $oTicket->Get('contacts_list')->Count()); + } + + /** + *
+	 * Server1
+	 *
+	 * Server2+---->Hypervisor2+---->Person2
+	 *
+	 * Ticket+---->Server1 (manual)
+	 *
+	 * Result:
+	 *
+	 * Ticket+====>Server1
+	 * 
+ * + * @throws Exception + */ + public function testUpdateImpactedItems_NoImpact2() + { + $oTicket = $this->CreateTicket(1); + $oServer1 = $this->CreateServer(1); + $oServer2 = $this->CreateServer(2); + $oPerson2 = $this->CreatePerson(2); + $oHypervisor2 = $this->CreateHypervisor(2, $oServer2); + $this->AddContactToCI($oPerson2, $oHypervisor2); + $oHypervisor2->DBUpdate(); + + $this->AddCIToTicket($oServer1, $oTicket, 'manual'); + $oTicket->DBUpdate(); // trigger the impact update + $this->ReloadObject($oTicket); // reload the links + + $this->CheckFunctionalCIList($oTicket); + $this->CheckContactList($oTicket); + $this->assertEquals(1, $oTicket->Get('functionalcis_list')->Count()); + $this->assertEquals(0, $oTicket->Get('contacts_list')->Count()); + } + + /** + *
+	 *                    +-->Person1
+	 *                    |
+	 *                    +
+	 * Server1+---->Hypervisor1+--+
+	 *                            |
+	 *                            v
+	 *                            Farm (1)
+	 *                            ^
+	 *                            |
+	 * Server2+---->Hypervisor2+--+
+	 *                    +
+	 *                    |
+	 *                    +-->Person2
+	 *
+	 * Ticket+---->Server1 (manual)
+	 *
+	 * Result:
+	 *
+	 * Ticket+====>Server1,Hypervisor1
+	 *       |
+	 *       +====>Person1
+	 * 
+ * + * @throws Exception + */ + public function testUpdateImpactedItems_Redundancy() + { + $oFarm = $this->CreateFarm(1); + + $oServer1 = $this->CreateServer(1); + $oHypervisor1 = $this->CreateHypervisor(1, $oServer1, $oFarm); + $oContact1 = $this->CreatePerson(1); + $this->AddContactToCI($oContact1, $oHypervisor1); + $oHypervisor1->DBUpdate(); + + $oServer2 = $this->CreateServer(2); + $oHypervisor2 = $this->CreateHypervisor(2, $oServer2, $oFarm); + $oContact2 = $this->CreatePerson(2); + $this->AddContactToCI($oContact2, $oHypervisor2); + $oHypervisor2->DBUpdate(); + + $oTicket = $this->CreateTicket(1); + $this->AddCIToTicket($oServer1, $oTicket, 'manual'); + $oTicket->DBUpdate(); // trigger the impact update + $this->ReloadObject($oTicket); // reload the links + + $this->CheckFunctionalCIList($oTicket); + $this->CheckContactList($oTicket); + $this->assertEquals(2, $oTicket->Get('functionalcis_list')->Count()); + $this->assertEquals(1, $oTicket->Get('contacts_list')->Count()); + } + + /** + *
+	 *                    +-->Person1
+	 *                    |
+	 *                    +
+	 * Server1+---->Hypervisor1+--+
+	 *                            |
+	 *                            v
+	 *                            Farm (1)
+	 *                            ^
+	 *                            |
+	 * Server2+---->Hypervisor2+--+
+	 *                    +
+	 *                    |
+	 *                    +-->Person2
+	 *
+	 * Ticket+---->Server1 (manual), Hypervisor2 (manual)
+	 *
+	 * Result:
+	 *
+	 * Ticket+====>Server1,Hypervisor1,Farm,Hypervisor2
+	 *       |
+	 *       +====>Person1,Person2
+	 * 
+ * + * @throws Exception + */ + public function testUpdateImpactedItems_Redundancy2() + { + $oFarm = $this->CreateFarm(1); + + $oServer1 = $this->CreateServer(1); + $oHypervisor1 = $this->CreateHypervisor(1, $oServer1, $oFarm); + $oContact1 = $this->CreatePerson(1); + $this->AddContactToCI($oContact1, $oHypervisor1); + $oHypervisor1->DBUpdate(); + + $oServer2 = $this->CreateServer(2); + $oHypervisor2 = $this->CreateHypervisor(2, $oServer2, $oFarm); + $oContact2 = $this->CreatePerson(2); + $this->AddContactToCI($oContact2, $oHypervisor2); + $oHypervisor2->DBUpdate(); + + $oTicket = $this->CreateTicket(1); + $this->AddCIToTicket($oServer1, $oTicket, 'manual'); + $this->AddCIToTicket($oHypervisor2, $oTicket, 'manual'); + $oTicket->DBUpdate(); // trigger the impact update + $this->ReloadObject($oTicket); // reload the links + + $this->CheckFunctionalCIList($oTicket); + $this->CheckContactList($oTicket); + $this->assertEquals(4, $oTicket->Get('functionalcis_list')->Count()); + $this->assertEquals(2, $oTicket->Get('contacts_list')->Count()); + } + + + /** + *
+	 *                    +-->Person1
+	 *                    |
+	 *                    +
+	 * Server1+---->Hypervisor1+--+  +-->VM1
+	 *                            |  |
+	 *                            v  +
+	 *                            Farm (1)
+	 *                            ^  +
+	 *                            |  |
+	 * Server2+---->Hypervisor2+--+  +-->VM2
+	 *                    +
+	 *                    |
+	 *                    +-->Person2
+	 *
+	 * Ticket+---->Server1 (manual), Hypervisor2 (manual)
+	 *
+	 * Result:
+	 *
+	 * Ticket+====>Server1,Hypervisor1,Farm,Hypervisor2,VM1,VM2
+	 *       |
+	 *       +====>Person1,Person2
+	 * 
+ * + * @throws Exception + */ + public function testUpdateImpactedItems_Redundancy3() + { + $oFarm = $this->CreateFarm(1); + + $oServer1 = $this->CreateServer(1); + $oHypervisor1 = $this->CreateHypervisor(1, $oServer1, $oFarm); + $oContact1 = $this->CreatePerson(1); + $this->AddContactToCI($oContact1, $oHypervisor1); + $oHypervisor1->DBUpdate(); + + $oServer2 = $this->CreateServer(2); + $oHypervisor2 = $this->CreateHypervisor(2, $oServer2, $oFarm); + $oContact2 = $this->CreatePerson(2); + $this->AddContactToCI($oContact2, $oHypervisor2); + $oHypervisor2->DBUpdate(); + + $this->CreateVirtualMachine(1, $oFarm); + $this->CreateVirtualMachine(2, $oFarm); + + $oTicket = $this->CreateTicket(1); + $this->AddCIToTicket($oServer1, $oTicket, 'manual'); + $this->AddCIToTicket($oHypervisor2, $oTicket, 'manual'); + $oTicket->DBUpdate(); // trigger the impact update + $this->ReloadObject($oTicket); // reload the links + + $this->CheckFunctionalCIList($oTicket); + $this->CheckContactList($oTicket); + $this->assertEquals(6, $oTicket->Get('functionalcis_list')->Count()); + $this->assertEquals(2, $oTicket->Get('contacts_list')->Count()); + } + + /** + *
+     *                    +-->Person1
+     *                    |
+     *                    +
+     * Server1+---->Hypervisor1+--+  +-->VM1
+     *                            |  |
+     *                            v  +
+     *                            Farm (1)
+     *                            ^  +
+     *                            |  |
+     * Server2+---->Hypervisor2+--+  +-->VM2
+     *                    +
+     *                    |
+     *                    +-->Person2
+     *
+     * Ticket+---->Server1 (manual), Server2 (manual), Hypervisor2 (not_impacted)
+     *
+     * Result:
+     *
+     * Ticket+====>Server1,Hypervisor1,Server2,Hypervisor2
+     *       |
+     *       +====>Person1
+     * 
+ * + * @throws Exception + */ + public function testUpdateImpactedItems_Exclusion() + { + $oFarm = $this->CreateFarm(1); + + $oServer1 = $this->CreateServer(1); + $oHypervisor1 = $this->CreateHypervisor(1, $oServer1, $oFarm); + $oContact1 = $this->CreatePerson(1); + $this->AddContactToCI($oContact1, $oHypervisor1); + $oHypervisor1->DBUpdate(); + + $oServer2 = $this->CreateServer(2); + $oHypervisor2 = $this->CreateHypervisor(2, $oServer2, $oFarm); + $oContact2 = $this->CreatePerson(2); + $this->AddContactToCI($oContact2, $oHypervisor2); + $oHypervisor2->DBUpdate(); + + $this->CreateVirtualMachine(1, $oFarm); + $this->CreateVirtualMachine(2, $oFarm); + + $oTicket = $this->CreateTicket(1); + $this->AddCIToTicket($oServer1, $oTicket, 'manual'); + $this->AddCIToTicket($oServer2, $oTicket, 'manual'); + $this->AddCIToTicket($oHypervisor2, $oTicket, 'not_impacted'); + $oTicket->DBUpdate(); // trigger the impact update + $this->ReloadObject($oTicket); // reload the links + + $this->CheckFunctionalCIList($oTicket); + $this->CheckContactList($oTicket); + $this->assertEquals(4, $oTicket->Get('functionalcis_list')->Count()); + $this->assertEquals(1, $oTicket->Get('contacts_list')->Count()); + } + + /** + *
+     *                    +-->Person1
+     *                    |
+     *                    +
+     * Server1+---->Hypervisor1+--+  +-->VM1
+     *                            |  |
+     *                            v  +
+     *                            Farm (1)
+     *                            ^  +
+     *                            |  |
+     * Server2+---->Hypervisor2+--+  +-->VM2
+     *                    +
+     *                    |
+     *                    +-->Person2
+     *
+     * Ticket+---->Server1 (manual), Server2 (manual)
+     *       |
+     *       +---->Person2 (do_not_notify)
+     *
+     * Result:
+     *
+     * Ticket+====>Server1,Hypervisor1,Server2,Hypervisor2,Farm,VM1,VM2
+     *       |
+     *       +====>Person1,Person2 (do_not_notify)
+     * 
+ * + * @throws Exception + */ + public function testUpdateImpactedItems_Exclusion2() + { + $oFarm = $this->CreateFarm(1); + + $oServer1 = $this->CreateServer(1); + $oHypervisor1 = $this->CreateHypervisor(1, $oServer1, $oFarm); + $oContact1 = $this->CreatePerson(1); + $this->AddContactToCI($oContact1, $oHypervisor1); + $oHypervisor1->DBUpdate(); + + $oServer2 = $this->CreateServer(2); + $oHypervisor2 = $this->CreateHypervisor(2, $oServer2, $oFarm); + $oContact2 = $this->CreatePerson(2); + $this->AddContactToCI($oContact2, $oHypervisor2); + $oHypervisor2->DBUpdate(); + + $this->CreateVirtualMachine(1, $oFarm); + $this->CreateVirtualMachine(2, $oFarm); + + $oTicket = $this->CreateTicket(1); + $this->AddCIToTicket($oServer1, $oTicket, 'manual'); + $this->AddCIToTicket($oServer2, $oTicket, 'manual'); + $this->AddContactToTicket($oContact2, $oTicket, 'do_not_notify'); + $oTicket->DBUpdate(); // trigger the impact update + $this->ReloadObject($oTicket); // reload the links + + $this->CheckFunctionalCIList($oTicket); + $this->CheckContactList($oTicket); + $this->assertEquals(7, $oTicket->Get('functionalcis_list')->Count()); + $this->assertEquals(2, $oTicket->Get('contacts_list')->Count()); + } + + /** + *
+     *                    +-->Person1
+     *                    |
+     *                    +
+     * Server1+---->Hypervisor1+--+  +-->VM1
+     *                            |  |
+     *                            v  +
+     *                            Farm (1)
+     *                            ^  +
+     *                            |  |
+     * Server2+---->Hypervisor2+--+  +-->VM2
+     *                    +
+     *                    |
+     *                    +-->Person2
+     *
+     * Ticket+---->Server1 (manual), Server2 (manual), Hypervisor2 (not_impacted)
+     *       |
+     *       +---->Person2 (do_not_notify)
+     *
+     * Result:
+     *
+     * Ticket+====>Server1,Hypervisor1,Server2,Hypervisor2
+     *       |
+     *       +====>Person1,Person2 (do_not_notify)
+     * 
+ * + * @throws Exception + */ + public function testUpdateImpactedItems_Exclusion3() + { + $oFarm = $this->CreateFarm(1); + + $oServer1 = $this->CreateServer(1); + $oHypervisor1 = $this->CreateHypervisor(1, $oServer1, $oFarm); + $oContact1 = $this->CreatePerson(1); + $this->AddContactToCI($oContact1, $oHypervisor1); + $oHypervisor1->DBUpdate(); + + $oServer2 = $this->CreateServer(2); + $oHypervisor2 = $this->CreateHypervisor(2, $oServer2, $oFarm); + $oContact2 = $this->CreatePerson(2); + $this->AddContactToCI($oContact2, $oHypervisor2); + $oHypervisor2->DBUpdate(); + + $this->CreateVirtualMachine(1, $oFarm); + $this->CreateVirtualMachine(2, $oFarm); + + $oTicket = $this->CreateTicket(1); + $this->AddCIToTicket($oServer1, $oTicket, 'manual'); + $this->AddCIToTicket($oServer2, $oTicket, 'manual'); + $this->AddCIToTicket($oHypervisor2, $oTicket, 'not_impacted'); + $this->AddContactToTicket($oContact2, $oTicket, 'do_not_notify'); + $oTicket->DBUpdate(); // trigger the impact update + $this->ReloadObject($oTicket); // reload the links + + $this->CheckFunctionalCIList($oTicket); + $this->CheckContactList($oTicket); + $this->assertEquals(4, $oTicket->Get('functionalcis_list')->Count()); + $this->assertEquals(2, $oTicket->Get('contacts_list')->Count()); + } + + /** + *
+     *                    +-->Person1
+     *                    |
+     *                    +
+     * Server1+---->Hypervisor1+--+  +-->VM1
+     *                            |  |
+     *                            v  +
+     *                            Farm (1)
+     *                            ^  +
+     *                            |  |
+     * Server2+---->Hypervisor2+--+  +-->VM2
+     *                    +
+     *                    |
+     *                    +-->Person2
+     *
+     * Ticket+---->Server1 (manual), Hypervisor2 (not_impacted)
+     *
+     * Result:
+     *
+     * Ticket+====>Server1,Hypervisor1, Hypervisor2 (not_impacted)
+     *       |
+     *       +====>Person1
+     * 
+ * + * @throws Exception + */ + public function testUpdateImpactedItems_Exclusion4() + { + $oFarm = $this->CreateFarm(1); + + $oServer1 = $this->CreateServer(1); + $oHypervisor1 = $this->CreateHypervisor(1, $oServer1, $oFarm); + $oContact1 = $this->CreatePerson(1); + $this->AddContactToCI($oContact1, $oHypervisor1); + $oHypervisor1->DBUpdate(); + + $oServer2 = $this->CreateServer(2); + $oHypervisor2 = $this->CreateHypervisor(2, $oServer2, $oFarm); + $oContact2 = $this->CreatePerson(2); + $this->AddContactToCI($oContact2, $oHypervisor2); + $oHypervisor2->DBUpdate(); + + $this->CreateVirtualMachine(1, $oFarm); + $this->CreateVirtualMachine(2, $oFarm); + + $oTicket = $this->CreateTicket(1); + $this->AddCIToTicket($oServer1, $oTicket, 'manual'); + $this->AddCIToTicket($oHypervisor2, $oTicket, 'not_impacted'); + $oTicket->DBUpdate(); // trigger the impact update + $this->ReloadObject($oTicket); // reload the links + + $this->CheckFunctionalCIList($oTicket); + $this->CheckContactList($oTicket); + $this->assertEquals(3, $oTicket->Get('functionalcis_list')->Count()); + $this->assertEquals(1, $oTicket->Get('contacts_list')->Count()); + } + + /** + *
+     *                    +-->Person1
+     *                    |
+     *                    +
+     * Server1+---->Hypervisor1+--+  +-->VM1
+     *                            |  |
+     *                            v  +
+     *                            Farm (1)
+     *                            ^  +
+     *                            |  |
+     * Server2+---->Hypervisor2+--+  +-->VM2
+     *                    +
+     *                    |
+     *                    +-->Person2
+     *
+     * Ticket1+---->Server1 (manual)
+     *
+     * Result:
+     *
+     * Ticket1+====>Server1,Hypervisor1
+     *        |
+     *        +====>Person1
+     *
+     * Then:
+     *
+     * Ticket2+---->Server2 (manual)
+     *
+     * Result:
+     *
+     * Ticket2+====>Server2,Hypervisor2,Farm,VM1,VM2
+     *        |
+     *        +====>Person2
+     * 
+ * + * @throws ArchivedObjectException + * @throws Exception + */ + public function testUpdateImpactedItems_Redundancy_two_tickets() + { + $oFarm = $this->CreateFarm(1); + + $oServer1 = $this->CreateServer(1); + $oHypervisor1 = $this->CreateHypervisor(1, $oServer1, $oFarm); + $oContact1 = $this->CreatePerson(1); + $this->AddContactToCI($oContact1, $oHypervisor1); + $oHypervisor1->DBUpdate(); + + $oServer2 = $this->CreateServer(2); + $oHypervisor2 = $this->CreateHypervisor(2, $oServer2, $oFarm); + $oContact2 = $this->CreatePerson(2); + $this->AddContactToCI($oContact2, $oHypervisor2); + $oHypervisor2->DBUpdate(); + + $oVM1 = $this->CreateVirtualMachine(1, $oFarm); + $oVM2 = $this->CreateVirtualMachine(2, $oFarm); + + // Ticket1+---->Server1 (manual) + $oTicket1 = $this->CreateTicket(1); + $this->AddCIToTicket($oServer1, $oTicket1, 'manual'); + $oTicket1->DBUpdate(); // trigger the impact update + $this->ReloadObject($oTicket1); // reload the links + + // Ticket1+====>Server1,Hypervisor1 + // | + // +====>Person1 + $this->CheckFunctionalCIList($oTicket1); + $this->CheckContactList($oTicket1); + $this->assertEquals(2, $oTicket1->Get('functionalcis_list')->Count()); + $this->assertEquals(1, $oTicket1->Get('contacts_list')->Count()); + + // Ticket2+---->Hypervisor2 (manual) + $oTicket2 = $this->CreateTicket(2); + $this->AddCIToTicket($oServer2, $oTicket2, 'manual'); + $oTicket2->DBUpdate(); // trigger the impact update + $this->ReloadObject($oTicket2); // reload the links + + // Ticket2+====>Farm,Hypervisor2,VM1,VM2,Server2 + // | + // +====>Person2 + $aWaitedCIList = array( + $oFarm->GetKey() => 'computed', + $oVM1->GetKey() => 'computed', + $oVM2->GetKey() => 'computed', + $oHypervisor2->GetKey() => 'computed', + $oServer2->GetKey() => 'manual'); + $this->CheckFunctionalCIList($oTicket2, $aWaitedCIList); + $this->CheckContactList($oTicket2); + $this->assertEquals(5, $oTicket2->Get('functionalcis_list')->Count()); + $this->assertEquals(1, $oTicket2->Get('contacts_list')->Count()); + + // The first ticket is not impacted + $this->debug("\nCheck that the first ticket has not changed.\n"); + $this->ReloadObject($oTicket1); // reload the links + + // Ticket1+====>Server1,Hypervisor1 + // | + // +====>Person1 + $this->CheckFunctionalCIList($oTicket1); + $this->CheckContactList($oTicket1); + $this->assertEquals(2, $oTicket1->Get('functionalcis_list')->Count()); + $this->assertEquals(1, $oTicket1->Get('contacts_list')->Count()); + } + + /** + *
+     *                    +-->Person1
+     *                    |
+     *                    +
+     * Server1+---->Hypervisor1+--+  +-->VM1
+     *                            |  |
+     *                            v  +
+     * Server3+---->Hypervisor3+->Farm (1)
+     *                            ^  +
+     *                            |  |
+     * Server2+---->Hypervisor2+--+  +-->VM2
+     *                    +
+     *                    |
+     *                    +-->Person2
+     *
+     * Ticket1+---->Server1 (manual), Hypervisor2(manual)
+     *
+     * Result:
+     *
+     * Ticket1+====>Server1,Hypervisor1, Hypervisor2
+     *        |
+     *        +====>Person1, Person2
+     *
+     * Then:
+     *
+     * Ticket2+---->Server2 (manual), Hypervisor3 (manual)
+     *
+     * Result:
+     *
+     * Ticket2+====>Server2,Hypervisor2,Hypervisor3,Farm,VM1,VM2
+     *        |
+     *        +====>Person2
+     * 
+ * + * @throws ArchivedObjectException + * @throws Exception + */ + public function testUpdateImpactedItems_Redundancy_two_tickets2() + { + $oFarm = $this->CreateFarm(1); + + $oServer1 = $this->CreateServer(1); + $oHypervisor1 = $this->CreateHypervisor(1, $oServer1, $oFarm); + $oContact1 = $this->CreatePerson(1); + $this->AddContactToCI($oContact1, $oHypervisor1); + $oHypervisor1->DBUpdate(); + + $oServer2 = $this->CreateServer(2); + $oHypervisor2 = $this->CreateHypervisor(2, $oServer2, $oFarm); + $oContact2 = $this->CreatePerson(2); + $this->AddContactToCI($oContact2, $oHypervisor2); + $oHypervisor2->DBUpdate(); + + $oServer3 = $this->CreateServer(3); + $oHypervisor3 = $this->CreateHypervisor(3, $oServer3, $oFarm); + + + $oVM1 = $this->CreateVirtualMachine(1, $oFarm); + $oVM2 = $this->CreateVirtualMachine(2, $oFarm); + + // Ticket1+---->Server1 (manual), Hypervisor2(manual) + $oTicket1 = $this->CreateTicket(1); + $this->AddCIToTicket($oServer1, $oTicket1, 'manual'); + $this->AddCIToTicket($oHypervisor2, $oTicket1, 'manual'); + $oTicket1->DBUpdate(); // trigger the impact update + $this->ReloadObject($oTicket1); // reload the links + + // Ticket1+====>Server1,Hypervisor1,Hypervisor2 + // | + // +====>Person1,Person2 + $this->CheckFunctionalCIList($oTicket1); + $this->CheckContactList($oTicket1); + $this->assertEquals(3, $oTicket1->Get('functionalcis_list')->Count()); + $this->assertEquals(2, $oTicket1->Get('contacts_list')->Count()); + + // Ticket2+---->Server2 (manual) + $oTicket2 = $this->CreateTicket(2); + $this->AddCIToTicket($oServer2, $oTicket2, 'manual'); + $this->AddCIToTicket($oHypervisor3, $oTicket2, 'manual'); + $oTicket2->DBUpdate(); // trigger the impact update + $this->ReloadObject($oTicket2); // reload the links + + // Ticket2+====>Farm,Hypervisor2,VM1,VM2,Server2 + // | + // +====>Person2 + $aWaitedCIList = array( + $oFarm->GetKey() => 'computed', + $oVM1->GetKey() => 'computed', + $oVM2->GetKey() => 'computed', + $oHypervisor2->GetKey() => 'computed', + $oHypervisor3->GetKey() => 'manual', + $oServer2->GetKey() => 'manual'); + $this->CheckFunctionalCIList($oTicket2, $aWaitedCIList); + $this->CheckContactList($oTicket2); + $this->assertEquals(6, $oTicket2->Get('functionalcis_list')->Count()); + $this->assertEquals(1, $oTicket2->Get('contacts_list')->Count()); + + // The first ticket is not impacted + $this->debug("\nCheck that the first ticket has not changed.\n"); + $this->ReloadObject($oTicket1); // reload the links + + // Ticket1+====>Server1,Hypervisor1,Hypervisor2 + // | + // +====>Person1,Person2 + $this->CheckFunctionalCIList($oTicket1); + $this->CheckContactList($oTicket1); + $this->assertEquals(3, $oTicket1->Get('functionalcis_list')->Count()); + $this->assertEquals(2, $oTicket1->Get('contacts_list')->Count()); + } + +} diff --git a/test/unittestautoload.php b/test/unittestautoload.php new file mode 100644 index 000000000..fa955c28e --- /dev/null +++ b/test/unittestautoload.php @@ -0,0 +1,5 @@ +