testlist.inc.php 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * Core test list
  18. *
  19. * @author Erwan Taloc <erwan.taloc@combodo.com>
  20. * @author Romain Quetiez <romain.quetiez@combodo.com>
  21. * @author Denis Flaven <denis.flaven@combodo.com>
  22. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  23. */
  24. class TestSQLQuery extends TestScenarioOnDB
  25. {
  26. static public function GetName() {return 'SQLQuery';}
  27. static public function GetDescription() {return 'SQLQuery does not depend on the rest of the framework, therefore it makes sense to have a separate test framework for it';}
  28. static public function GetDBHost() {return 'localhost';}
  29. static public function GetDBUser() {return 'root';}
  30. static public function GetDBPwd() {return '';}
  31. static public function GetDBName() {return 'TestSQLQuery';}
  32. static public function GetDBSubName() {return 'taratata';}
  33. protected function DoPrepare()
  34. {
  35. parent::DoPrepare();
  36. cmdbSource::CreateTable('CREATE TABLE `myTable` (myKey INT(11) NOT NULL auto_increment, column1 VARCHAR(255), column2 VARCHAR(255), PRIMARY KEY (`myKey`)) ENGINE = '.MYSQL_ENGINE);
  37. cmdbSource::CreateTable('CREATE TABLE `myTable1` (myKey1 INT(11) NOT NULL auto_increment, column1_1 VARCHAR(255), column1_2 VARCHAR(255), PRIMARY KEY (`myKey1`)) ENGINE = '.MYSQL_ENGINE);
  38. cmdbSource::CreateTable('CREATE TABLE `myTable2` (myKey2 INT(11) NOT NULL auto_increment, column2_1 VARCHAR(255), column2_2 VARCHAR(255), PRIMARY KEY (`myKey2`)) ENGINE = '.MYSQL_ENGINE);
  39. }
  40. protected function DoExecute()
  41. {
  42. $oQuery = new SQLQuery(
  43. $sTable = 'myTable',
  44. $sTableAlias = 'myTableAlias',
  45. $aFields = array('column1'=>new FieldExpression('column1', 'myTableAlias'), 'column2'=>new FieldExpression('column2', 'myTableAlias')),
  46. $oCondition = new BinaryExpression(new FieldExpression('column1', 'myTableAlias'), 'LIKE', new ScalarExpression('trash')),
  47. $aFullTextNeedles = array('column1'),
  48. $bToDelete = false,
  49. $aValues = array()
  50. );
  51. $oQuery->AddCondition(Expression::FromOQL('DATE(NOW() - 1200 * 2) > \'2008-07-31\''));
  52. $oSubQuery1 = new SQLQuery(
  53. $sTable = 'myTable1',
  54. $sTableAlias = 'myTable1Alias',
  55. $aFields = array('column1_1'=>new FieldExpression('column1', 'myTableAlias'), 'column1_2'=>new FieldExpression('column1', 'myTableAlias')),
  56. $oCondition = new TrueSQLExpression,
  57. $aFullTextNeedles = array(),
  58. $bToDelete = false,
  59. $aValues = array()
  60. );
  61. $oSubQuery2 = new SQLQuery(
  62. $sTable = 'myTable2',
  63. $sTableAlias = 'myTable2Alias',
  64. $aFields = array('column2_1'=>new FieldExpression('column2', 'myTableAlias'), 'column2_2'=>new FieldExpression('column2', 'myTableAlias')),
  65. $oCondition = new TrueSQLExpression,
  66. $aFullTextNeedles = array(),
  67. $bToDelete = false,
  68. $aValues = array()
  69. );
  70. $oQuery->AddInnerJoin($oSubQuery1, 'column1', 'column1_1');
  71. $oQuery->AddLeftJoin($oSubQuery2, 'column2', 'column2_2');
  72. $oQuery->DisplayHtml();
  73. $oQuery->RenderDelete();
  74. $oQuery->RenderUpdate();
  75. echo '<p>'.$oQuery->RenderSelect().'</p>';
  76. $oQuery->RenderSelect(array('column1'));
  77. $oQuery->RenderSelect(array('column1', 'column2'));
  78. }
  79. }
  80. class TestOQLParser extends TestFunction
  81. {
  82. static public function GetName() {return 'Check OQL parsing';}
  83. static public function GetDescription() {return 'Attempts a series of queries, and in particular those with a bad syntax';}
  84. protected function CheckQuery($sQuery, $bIsCorrectQuery)
  85. {
  86. $oOql = new OqlInterpreter($sQuery);
  87. try
  88. {
  89. $oTrash = $oOql->Parse(); // Not expecting a given format, otherwise use ParseExpression/ParseObjectQuery/ParseValueSetQuery
  90. self::DumpVariable($oTrash);
  91. }
  92. catch (OQLException $OqlException)
  93. {
  94. if ($bIsCorrectQuery)
  95. {
  96. echo "<p>More info on this unexpected failure:<br/>".$OqlException->getHtmlDesc()."</p>\n";
  97. throw $OqlException;
  98. return false;
  99. }
  100. else
  101. {
  102. // Everything is fine :-)
  103. echo "<p>More info on this expected failure:<br/>".$OqlException->getHtmlDesc()."</p>\n";
  104. return true;
  105. }
  106. }
  107. // The query was correctly parsed, was it expected to be correct ?
  108. if ($bIsCorrectQuery)
  109. {
  110. return true;
  111. }
  112. else
  113. {
  114. throw new UnitTestException("The query '$sQuery' was parsed with success, while it shouldn't (?)");
  115. return false;
  116. }
  117. }
  118. protected function TestQuery($sQuery, $bIsCorrectQuery)
  119. {
  120. if (!$this->CheckQuery($sQuery, $bIsCorrectQuery))
  121. {
  122. return false;
  123. }
  124. return true;
  125. }
  126. public function DoExecute()
  127. {
  128. $aQueries = array(
  129. 'SELECT toto' => true,
  130. 'SELECT toto WHERE toto.a = 1' => true,
  131. 'SELECT toto WHERE toto.a=1' => true,
  132. 'SELECT toto WHERE toto.a = "1"' => true,
  133. 'SELECT toto WHHHERE toto.a = "1"' => false,
  134. 'SELECT toto WHERE toto.a == "1"' => false,
  135. 'SELECT toto WHERE toto.a % 1' => false,
  136. //'SELECT toto WHERE toto.a LIKE 1' => false,
  137. 'SELECT toto WHERE toto.a like \'arg\'' => false,
  138. 'SELECT toto WHERE toto.a NOT LIKE "That\'s it"' => true,
  139. 'SELECT toto WHERE toto.a NOT LIKE "That\'s "it""' => false,
  140. 'SELECT toto WHERE toto.a NOT LIKE "That\'s \\"it\\""' => true,
  141. 'SELECT toto WHERE toto.a NOT LIKE \'That"s it\'' => true,
  142. 'SELECT toto WHERE toto.a NOT LIKE \'That\'s it\'' => false,
  143. 'SELECT toto WHERE toto.a NOT LIKE \'That\\\'s it\'' => true,
  144. 'SELECT toto WHERE toto.a NOT LIKE "blah \\ truc"' => false,
  145. 'SELECT toto WHERE toto.a NOT LIKE "blah \\\\ truc"' => true,
  146. 'SELECT toto WHERE toto.a NOT LIKE \'blah \\ truc\'' => false,
  147. 'SELECT toto WHERE toto.a NOT LIKE \'blah \\\\ truc\'' => true,
  148. 'SELECT toto WHERE toto.a NOT LIKE "\\\\"' => true,
  149. 'SELECT toto WHERE toto.a NOT LIKE "\\""' => true,
  150. 'SELECT toto WHERE toto.a NOT LIKE "\\"\\\\"' => true,
  151. 'SELECT toto WHERE toto.a NOT LIKE "\\\\\\""' => true,
  152. 'SELECT toto WHERE toto.a NOT LIKE ""' => true,
  153. 'SELECT toto WHERE toto.a NOT LIKE "\\\\"' => true,
  154. "SELECT UserRightsMatrixClassGrant WHERE UserRightsMatrixClassGrant.class = 'lnkContactRealObject' AND UserRightsMatrixClassGrant.action = 'modify' AND UserRightsMatrixClassGrant.login = 'Denis'" => true,
  155. "SELECT A WHERE A.col1 = 'lit1' AND A.col2 = 'lit2' AND A.col3 = 'lit3'" => true,
  156. 'SELECT toto WHERE toto.a NOT LIKE "blah" AND toto.b LIKE "foo"' => true,
  157. //'SELECT toto WHERE toto.a > \'asd\'' => false,
  158. 'SELECT toto WHERE toto.a = 1 AND toto.b LIKE "x" AND toto.f >= 12345' => true,
  159. 'SELECT Device JOIN Site ON Device.site = Site.id' => true,
  160. 'SELECT Device JOIN Site ON Device.site = Site.id JOIN Country ON Site.location = Country.id' => true,
  161. "SELECT A JOIN B ON A.myB = B.id WHERE (A.col1 = 123 AND B.col1 = 'aa') OR (A.col3 = 'zzz' AND B.col4 > 100)" => true,
  162. "SELECT A JOIN B ON A.myB = B.id WHERE (A.col1 = B.col2 AND B.col1 = A.col2) OR (A.col3 = '' AND B.col4 > 100)" => true,
  163. "SELECT A JOIN B ON A.myB = B.id WHERE A.col1 + B.col2 * B.col1 = A.col2" => true,
  164. "SELECT A JOIN B ON A.myB = B.id WHERE A.col1 + (B.col2 * B.col1) = A.col2" => true,
  165. "SELECT A JOIN B ON A.myB = B.id WHERE (A.col1 + B.col2) * B.col1 = A.col2" => true,
  166. 'SELECT Device AS D_ JOIN Site AS S_ ON D_.site = S_.id WHERE S_.country = "Francia"' => true,
  167. // Several objects in a row...
  168. //
  169. 'SELECT A FROM A' => true,
  170. 'SELECT A JOIN B ON A.myB = B.id WHERE A.col1 = 2' => true,
  171. 'SELECT A FROM A JOIN B ON A.myB = B.id WHERE A.col1 = 2' => true,
  172. 'SELECT B FROM A JOIN B ON A.myB = B.id WHERE A.col1 = 2' => true,
  173. 'SELECT A,B FROM A JOIN B ON A.myB = B.id WHERE A.col1 = 2' => true,
  174. 'SELECT A, B FROM A JOIN B ON A.myB = B.id WHERE A.col1 = 2' => true,
  175. 'SELECT B,A FROM A JOIN B ON A.myB = B.id WHERE A.col1 = 2' => true,
  176. 'SELECT A, B,C FROM A JOIN B ON A.myB = B.id' => false,
  177. 'SELECT C FROM A JOIN B ON A.myB = B.id WHERE A.col1 = 2' => false,
  178. );
  179. $iErrors = 0;
  180. foreach($aQueries as $sQuery => $bIsCorrectQuery)
  181. {
  182. $sIsOk = $bIsCorrectQuery ? 'good' : 'bad';
  183. echo "<h4>Testing query: $sQuery ($sIsOk)</h4>\n";
  184. $bRet = $this->TestQuery($sQuery, $bIsCorrectQuery);
  185. if (!$bRet) $iErrors++;
  186. }
  187. return ($iErrors == 0);
  188. }
  189. }
  190. class TestCSVParser extends TestFunction
  191. {
  192. static public function GetName() {return 'Check CSV parsing';}
  193. static public function GetDescription() {return 'Loads a set of CSV data';}
  194. public function DoExecute()
  195. {
  196. $sDataFile = '?field1?;?field2?;?field3?
  197. ?a?;?b?;?c?
  198. a;b;c
  199. ? a ? ; ? b ? ; ? c ?
  200. a ; b ; c
  201. ??;??;??
  202. ;;
  203. ?a"?;?b?;?c?
  204. ?a1
  205. a2?;?b?;?c?
  206. ?a1,a2?;?b?;?c?
  207. ?a?;?b?;?c1,",c2
  208. ,c3?
  209. ?a?;?b?;?ouf !?
  210. Espace sur la fin ; 1234; e@taloc.com ';
  211. self::DumpVariable($sDataFile);
  212. $aExpectedResult = array(
  213. //array('field1', 'field2', 'field3'),
  214. array('a', 'b', 'c'),
  215. array('a', 'b', 'c'),
  216. array(' a ', ' b ', ' c '),
  217. array('a', 'b', 'c'),
  218. array('', '', ''),
  219. array('', '', ''),
  220. array('a"', 'b', 'c'),
  221. array("a1\na2", 'b', 'c'),
  222. array('a1,a2', 'b', 'c'),
  223. array('a', 'b', "c1,\",c2\n,c3"),
  224. array('a', 'b', 'ouf !'),
  225. array('Espace sur la fin', '1234', 'e@taloc.com'),
  226. );
  227. $oCSVParser = new CSVParser($sDataFile, ';', '?');
  228. $aData = $oCSVParser->ToArray(1, null, 0);
  229. $iIssues = 0;
  230. echo "<table border=\"1\">\n";
  231. foreach ($aData as $iRow => $aRow)
  232. {
  233. echo "<tr>\n";
  234. foreach ($aRow as $iCol => $sCell)
  235. {
  236. if (empty($sCell))
  237. {
  238. $sCellValue = '&nbsp;';
  239. }
  240. else
  241. {
  242. $sCellValue = htmlentities($sCell);
  243. }
  244. if (!isset($aExpectedResult[$iRow][$iCol]))
  245. {
  246. $iIssues++;
  247. $sCellValue = "<span style =\"color: red; background-color: grey;\">$sCellValue</span>";
  248. }
  249. elseif ($aExpectedResult[$iRow][$iCol] != $sCell)
  250. {
  251. $iIssues++;
  252. $sCellValue = "<span style =\"color: red; background-color: lightgrey;\">$sCellValue</span>, expecting '<span style =\"color: green; background-color: lightgrey;\">".$aExpectedResult[$iRow][$iCol]."</span>'";
  253. }
  254. echo "<td><pre>$sCellValue</pre></td>";
  255. }
  256. echo "</tr>\n";
  257. }
  258. echo "</table>\n";
  259. return ($iIssues > 0);
  260. }
  261. }
  262. class TestGenericItoMyModel extends TestBizModelGeneric
  263. {
  264. static public function GetName()
  265. {
  266. return 'Generic RO test on '.self::GetConfigFile();
  267. }
  268. static public function GetConfigFile() {return '/config-test-mymodel.php';}
  269. }
  270. class TestGenericItopBigModel extends TestBizModelGeneric
  271. {
  272. static public function GetName()
  273. {
  274. return 'Generic RO test on '.self::GetConfigFile();
  275. }
  276. static public function GetConfigFile() {return '/config-test-itopv06.php';}
  277. }
  278. class TestUserRightsMatrixItop extends TestUserRights
  279. {
  280. static public function GetName()
  281. {
  282. return 'User rights test on user rights matrix';
  283. }
  284. static public function GetDescription()
  285. {
  286. return 'blah blah blah';
  287. }
  288. public function DoPrepare()
  289. {
  290. parent::DoPrepare();
  291. MetaModel::Startup('../config-test-itopv06.php');
  292. }
  293. protected function DoExecute()
  294. {
  295. $sUser = 'Romain';
  296. echo "<p>Totor: ".(UserRights::CheckCredentials('Totor', 'toto') ? 'ok' : 'NO')."</p>\n";
  297. echo "<p>Romain: ".(UserRights::CheckCredentials('Romain', 'toto') ? 'ok' : 'NO')."</p>\n";
  298. echo "<p>User: ".UserRights::GetUser()."</p>\n";
  299. echo "<p>On behalf of...".UserRights::GetRealUser()."</p>\n";
  300. echo "<p>Denis (impersonate) : ".(UserRights::Impersonate('Denis', 'tutu') ? 'ok' : 'NO')."</p>\n";
  301. echo "<p>User: ".UserRights::GetUser()."</p>\n";
  302. echo "<p>On behalf of...".UserRights::GetRealUser()."</p>\n";
  303. $oSet = new DBObjectSet(DBObjectSearch::FromOQL("SELECT bizOrganization"));
  304. echo "<p>IsActionAllowed...".(UserRights::IsActionAllowed('bizOrganization', UR_ACTION_MODIFY, $oSet) == UR_ALLOWED_YES ? 'ok' : 'NO')."</p>\n";
  305. echo "<p>IsStimulusAllowed...".(UserRights::IsStimulusAllowed('bizOrganization', 'myStimulus', $oSet) == UR_ALLOWED_YES ? 'ok' : 'NO')."</p>\n";
  306. echo "<p>IsActionAllowedOnAttribute...".(UserRights::IsActionAllowedOnAttribute('bizOrganization', 'myattribute', UR_ACTION_MODIFY, $oSet) == UR_ALLOWED_YES ? 'ok' : 'NO')."</p>\n";
  307. return true;
  308. }
  309. }
  310. ///////////////////////////////////////////////////////////////////////////
  311. // Test a complex biz model on the fly
  312. ///////////////////////////////////////////////////////////////////////////
  313. class TestMyBizModel extends TestBizModel
  314. {
  315. static public function GetName()
  316. {
  317. return 'A series of tests on a weird business model';
  318. }
  319. static public function GetDescription()
  320. {
  321. return 'Attempts various operations and build complex queries';
  322. }
  323. static public function GetConfigFile() {return '/config-test-mymodel.php';}
  324. function test_linksinfo()
  325. {
  326. echo "<h4>Enum links</h4>";
  327. self::DumpVariable(MetaModel::EnumReferencedClasses("cmdbTeam"));
  328. self::DumpVariable(MetaModel::EnumReferencingClasses("Organization"));
  329. self::DumpVariable(MetaModel::EnumLinkingClasses());
  330. self::DumpVariable(MetaModel::EnumLinkingClasses("cmdbContact"));
  331. self::DumpVariable(MetaModel::EnumLinkingClasses("cmdWorkshop"));
  332. self::DumpVariable(MetaModel::GetLinkLabel("Liens_entre_contacts_et_workshop", "toworkshop"));
  333. }
  334. function test_list_attributes()
  335. {
  336. echo "<h4>List attributes</h4>";
  337. foreach(MetaModel::ListAttributeDefs("cmdbTeam") as $sAttCode=>$oAttDef)
  338. {
  339. echo $oAttDef->GetLabel()." / ".$oAttDef->GetDescription()." / ".$oAttDef->GetType()."</br>\n";
  340. }
  341. }
  342. function test_search()
  343. {
  344. echo "<h4>Two searches</h4>";
  345. $oFilterAllDevs = new DBObjectSearch("cmdbTeam");
  346. $oAllDevs = new DBObjectSet($oFilterAllDevs);
  347. echo "Found ".$oAllDevs->Count()." items.</br>\n";
  348. while ($oDev = $oAllDevs->Fetch())
  349. {
  350. $aValues = array();
  351. foreach(MetaModel::GetAttributesList($oAllDevs->GetClass()) as $sAttCode)
  352. {
  353. $aValues[] = MetaModel::GetLabel(get_class($oDev), $sAttCode)." (".MetaModel::GetDescription(get_class($oDev), $sAttCode).") = ".$oDev->GetAsHTML($sAttCode);
  354. }
  355. echo $oDev->GetKey()." => ".implode(", ", $aValues)."</br>\n";
  356. }
  357. // a second one
  358. $oMyFilter = new DBObjectSearch("cmdbContact");
  359. //$oMyFilter->AddCondition("name", "aii", "Finishes with");
  360. $oMyFilter->AddCondition("name", "aii");
  361. $this->search_and_show_list($oMyFilter);
  362. }
  363. function test_reload()
  364. {
  365. echo "<h4>Reload</h4>";
  366. $team = MetaModel::GetObject("cmdbContact", "2");
  367. echo "Chargement de l'attribut headcount: {$team->Get("headcount")}</br>\n";
  368. self::DumpVariable($team);
  369. }
  370. function test_setattribute()
  371. {
  372. echo "<h4>Set attribute and update</h4>";
  373. $team = MetaModel::GetObject("cmdbTeam", "2");
  374. $team->Set("headcount", rand(1,1000));
  375. $team->Set("email", "Luis ".rand(9,250));
  376. self::DumpVariable($team->ListChanges());
  377. echo "New headcount = {$team->Get("headcount")}</br>\n";
  378. echo "Computed name = {$team->Get("name")}</br>\n";
  379. $oMyChange = MetaModel::NewObject("CMDBChange");
  380. $oMyChange->Set("date", time());
  381. $oMyChange->Set("userinfo", "test_setattribute / Made by robot #".rand(1,100));
  382. $iChangeId = $oMyChange->DBInsert();
  383. //MetaModel::StartDebugQuery();
  384. $team->DBUpdateTracked($oMyChange);
  385. //MetaModel::StopDebugQuery();
  386. echo "<h4>Check the modified team</h4>";
  387. $oTeam = MetaModel::GetObject("cmdbTeam", "2");
  388. self::DumpVariable($oTeam);
  389. }
  390. function test_newobject()
  391. {
  392. $oMyChange = MetaModel::NewObject("CMDBChange");
  393. $oMyChange->Set("date", time());
  394. $oMyChange->Set("userinfo", "test_newobject / Made by robot #".rand(1,100));
  395. $iChangeId = $oMyChange->DBInsert();
  396. echo "<h4>Create a new object (team)</h4>";
  397. $oNewTeam = MetaModel::NewObject("cmdbTeam");
  398. $oNewTeam->Set("name", "ekip2choc #".rand(1000, 2000));
  399. $oNewTeam->Set("email", "machin".rand(1,100)."@tnut.com");
  400. $oNewTeam->Set("email", null);
  401. $oNewTeam->Set("owner", "ITOP");
  402. $oNewTeam->Set("headcount", "0".rand(38000, 38999)); // should be reset to an int value
  403. $iId = $oNewTeam->DBInsertTracked($oMyChange);
  404. echo "Created new team: $iId</br>";
  405. echo "<h4>Delete team #$iId</h4>";
  406. $oTeam = MetaModel::GetObject("cmdbTeam", $iId);
  407. $oTeam->DBDeleteTracked($oMyChange);
  408. echo "Deleted team: $iId</br>";
  409. self::DumpVariable($oTeam);
  410. }
  411. function test_updatecolumn()
  412. {
  413. $oMyChange = MetaModel::NewObject("CMDBChange");
  414. $oMyChange->Set("date", time());
  415. $oMyChange->Set("userinfo", "test_updatecolumn / Made by robot #".rand(1,100));
  416. $iChangeId = $oMyChange->DBInsert();
  417. $sNewEmail = "updatecol".rand(9,250)."@quedlaballe.com";
  418. echo "<h4>Update a the email: set to '$sNewEmail'</h4>";
  419. $oMyFilter = new DBObjectSearch("cmdbContact");
  420. $oMyFilter->AddCondition("name", "o", "Contains");
  421. echo "Candidates before:</br>";
  422. $this->search_and_show_list($oMyFilter);
  423. MetaModel::BulkUpdateTracked($oMyChange, $oMyFilter, array("email" => $sNewEmail));
  424. echo "Candidates after:</br>";
  425. $this->search_and_show_list($oMyFilter);
  426. }
  427. function test_error()
  428. {
  429. trigger_error("Stop requested", E_USER_ERROR);
  430. }
  431. function test_changetracking()
  432. {
  433. echo "<h4>Create a change</h4>";
  434. $oMyChange = MetaModel::NewObject("CMDBChange");
  435. $oMyChange->Set("date", time());
  436. $oMyChange->Set("userinfo", "Made by robot #".rand(1,100));
  437. $iChangeId = $oMyChange->DBInsert();
  438. echo "Created new change: $iChangeId</br>";
  439. self::DumpVariable($oMyChange);
  440. echo "<h4>Create a new object (team)</h4>";
  441. $oNewTeam = MetaModel::NewObject("cmdbTeam");
  442. $oNewTeam->Set("name", "ekip2choc #".rand(1000, 2000));
  443. $oNewTeam->Set("email", "machin".rand(1,100)."@tnut.com");
  444. $oNewTeam->Set("email", null);
  445. $oNewTeam->Set("owner", "ITOP");
  446. $oNewTeam->Set("headcount", "0".rand(38000, 38999)); // should be reset to an int value
  447. $iId = $oNewTeam->DBInsertTracked($oMyChange);
  448. echo "Created new team: $iId</br>";
  449. echo "<h4>Delete team #$iId</h4>";
  450. $oTeam = MetaModel::GetObject("cmdbTeam", $iId);
  451. $oTeam->DBDeleteTracked($oMyChange);
  452. echo "Deleted team: $iId</br>";
  453. self::DumpVariable($oTeam);
  454. }
  455. function test_zlist()
  456. {
  457. echo "<h4>Test ZLists</h4>";
  458. $aZLists = MetaModel::EnumZLists();
  459. foreach ($aZLists as $sListCode)
  460. {
  461. $aListInfos = MetaModel::GetZListInfo($sListCode);
  462. echo "<h4>List '".$sListCode."' (".$aListInfos["description"].") of type '".$aListInfos["type"]."'</h5>\n";
  463. foreach (MetaModel::GetSubclasses("cmdbObjectHomeMade") as $sKlass)
  464. {
  465. $aItems = MetaModel::GetZListItems($sKlass, $sListCode);
  466. if (count($aItems) == 0) continue;
  467. echo "$sKlass - $sListCode : {".implode(", ", $aItems)."}</br>\n";
  468. }
  469. }
  470. echo "<h4>IsAttributeInZList()... </h4>";
  471. echo "Liens_entre_contacts_et_workshop::ws_info in list1 ? ".(MetaModel::IsAttributeInZList("Liens_entre_contacts_et_workshop", "list1", "ws_info") ? "yes" : "no")."</br>\n";
  472. echo "Liens_entre_contacts_et_workshop::toworkshop in list1 ? ".(MetaModel::IsAttributeInZList("Liens_entre_contacts_et_workshop", "list1", "toworkshop") ? "yes" : "no")."</br>\n";
  473. }
  474. function test_pkey()
  475. {
  476. echo "<h4>Test search on pkey</h4>";
  477. $sExpr1 = "SELECT cmdbContact WHERE id IN (40, 42)";
  478. $sExpr2 = "SELECT cmdbContact WHERE IN NOT IN (40, 42)";
  479. $this->search_and_show_list_from_oql($sExpr1);
  480. $this->search_and_show_list_from_oql($sExpr2);
  481. echo "Et maintenant, on fusionne....</br>\n";
  482. $oSet1 = new CMDBObjectSet(DBObjectSearch::FromOQL($sExpr1));
  483. $oSet2 = new CMDBObjectSet(DBObjectSearch::FromOQL($sExpr2));
  484. $oIntersect = $oSet1->CreateIntersect($oSet2);
  485. $oDelta = $oSet1->CreateDelta($oSet2);
  486. $oMerge = clone $oSet1;
  487. $oMerge->Merge($oSet2);
  488. $oMerge->Merge($oSet2);
  489. echo "Set1 - Found ".$oSet1->Count()." items.</br>\n";
  490. echo "Set2 - Found ".$oSet2->Count()." items.</br>\n";
  491. echo "Intersect - Found ".$oIntersect->Count()." items.</br>\n";
  492. echo "Delta - Found ".$oDelta->Count()." items.</br>\n";
  493. echo "Merge - Found ".$oMerge->Count()." items.</br>\n";
  494. //$this->show_list($oObjSet);
  495. }
  496. function test_relations()
  497. {
  498. echo "<h4>Test relations</h4>";
  499. //self::DumpVariable(MetaModel::EnumRelationQueries("cmdbObjectHomeMade", "Potes"));
  500. self::DumpVariable(MetaModel::EnumRelationQueries("cmdbContact", "Potes"));
  501. $iMaxDepth = 9;
  502. echo "Max depth = $iMaxDepth</br>\n";
  503. $oObj = MetaModel::GetObject("cmdbContact", 18);
  504. $aRels = $oObj->GetRelatedObjects("Potes", $iMaxDepth);
  505. echo $oObj->Get('name')." has some 'Potes'...</br>\n";
  506. foreach ($aRels as $sClass => $aObjs)
  507. {
  508. echo "$sClass, count = ".count($aObjs)." =&gt; ".implode(', ', array_keys($aObjs))."</br>\n";
  509. $oObjectSet = CMDBObjectSet::FromArray($sClass, $aObjs);
  510. $this->show_list($oObjectSet);
  511. }
  512. echo "<h4>Test relations - same results, by the mean of a OQL</h4>";
  513. $this->search_and_show_list_from_oql("cmdbContact: RELATED (Potes, $iMaxDepth) TO (cmdbContact: pkey = 18)");
  514. }
  515. function test_linkedset()
  516. {
  517. echo "<h4>Linked set attributes</h4>\n";
  518. $oObj = MetaModel::GetObject("cmdbContact", 18);
  519. echo "<h5>Current workshops</h5>\n";
  520. $oSetWorkshopsCurr = $oObj->Get("myworkshops");
  521. $this->show_list($oSetWorkshopsCurr);
  522. echo "<h5>Setting workshops</h5>\n";
  523. $oNewLink = new cmdbLiens();
  524. $oNewLink->Set('toworkshop', 2);
  525. $oNewLink->Set('function', 'mafonctioooon');
  526. $oNewLink->Set('a1', 'tralala1');
  527. $oNewLink->Set('a2', 'F7M');
  528. $oSetWorkshops = CMDBObjectSet::FromArray("cmdbLiens", array($oNewLink));
  529. $oObj->Set("myworkshops", $oSetWorkshops);
  530. $this->show_list($oSetWorkshops);
  531. echo "<h5>New workshops</h5>\n";
  532. $oSetWorkshopsCurr = $oObj->Get("myworkshops");
  533. $this->show_list($oSetWorkshopsCurr);
  534. $oMyChange = MetaModel::NewObject("CMDBChange");
  535. $oMyChange->Set("date", time());
  536. $oMyChange->Set("userinfo", "test_linkedset / Made by robot #".rand(1,100));
  537. $iChangeId = $oMyChange->DBInsert();
  538. $oObj->DBUpdateTracked($oMyChange);
  539. $oObj = MetaModel::GetObject("cmdbContact", 18);
  540. echo "<h5>After the write</h5>\n";
  541. $oSetWorkshopsCurr = $oObj->Get("myworkshops");
  542. $this->show_list($oSetWorkshopsCurr);
  543. }
  544. function test_object_lifecycle()
  545. {
  546. echo "<h4>Test object lifecycle</h4>";
  547. self::DumpVariable(MetaModel::GetStateAttributeCode("cmdbContact"));
  548. self::DumpVariable(MetaModel::EnumStates("cmdbContact"));
  549. self::DumpVariable(MetaModel::EnumStimuli("cmdbContact"));
  550. foreach(MetaModel::EnumStates("cmdbContact") as $sStateCode => $aStateDef)
  551. {
  552. echo "<p>Transition from <strong>$sStateCode</strong></p>\n";
  553. self::DumpVariable(MetaModel::EnumTransitions("cmdbContact", $sStateCode));
  554. }
  555. $oObj = MetaModel::GetObject("cmdbContact", 18);
  556. echo "Current state: ".$oObj->GetState()."... let's go to school...";
  557. self::DumpVariable($oObj->EnumTransitions());
  558. $oObj->ApplyStimulus("toschool");
  559. echo "New state: ".$oObj->GetState()."... let's get older...";
  560. self::DumpVariable($oObj->EnumTransitions());
  561. $oObj->ApplyStimulus("raise");
  562. echo "New state: ".$oObj->GetState()."... let's try to go further... (should give an error)";
  563. self::DumpVariable($oObj->EnumTransitions());
  564. $oObj->ApplyStimulus("raise"); // should give an error
  565. }
  566. protected function DoExecute()
  567. {
  568. // $this->ReportError("Found two different OQL expression out of the (same?) filter: <em>$sExpr1</em> != <em>$sExpr2</em>");
  569. // $this->ReportSuccess('Found '.$oSet->Count()." objects of class $sClassName");
  570. //$this->test_linksinfo();
  571. //$this->test_list_attributes();
  572. //$this->test_search();
  573. //$this->test_reload();
  574. //$this->test_newobject();
  575. $this->test_setattribute();
  576. //$this->test_updatecolumn();
  577. //$this->test_error();
  578. //$this->test_changetracking();
  579. $this->test_zlist();
  580. $this->test_OQL();
  581. //$this->test_pkey();
  582. $this->test_relations();
  583. $this->test_linkedset();
  584. $this->test_object_lifecycle();
  585. }
  586. }
  587. ///////////////////////////////////////////////////////////////////////////
  588. // Test a complex biz model on the fly
  589. ///////////////////////////////////////////////////////////////////////////
  590. abstract class MyFarm extends TestBizModel
  591. {
  592. static public function GetConfigFile() {return '/config-test-farm.php';}
  593. protected function DoPrepare()
  594. {
  595. parent::DoPrepare();
  596. $this->ResetDB();
  597. MetaModel::DBCheckIntegrity();
  598. }
  599. protected function InsertMammal($sSpecies, $sSex, $iSpeed, $iMotherid, $iFatherId, $sName, $iHeight, $sBirth)
  600. {
  601. $oNew = MetaModel::NewObject('Mammal');
  602. $oNew->Set('species', $sSpecies);
  603. $oNew->Set('sex', $sSex);
  604. $oNew->Set('speed', $iSpeed);
  605. $oNew->Set('mother', $iMotherid);
  606. $oNew->Set('father', $iFatherId);
  607. $oNew->Set('name', $sName);
  608. $oNew->Set('height', $iHeight);
  609. $oNew->Set('birth', $sBirth);
  610. return $this->ObjectToDB($oNew);
  611. }
  612. protected function InsertBird($sSpecies, $sSex, $iSpeed, $iMotherid, $iFatherId)
  613. {
  614. $oNew = MetaModel::NewObject('Bird');
  615. $oNew->Set('species', $sSpecies);
  616. $oNew->Set('sex', $sSex);
  617. $oNew->Set('speed', $iSpeed);
  618. $oNew->Set('mother', $iMotherid);
  619. $oNew->Set('father', $iFatherId);
  620. return $this->ObjectToDB($oNew);
  621. }
  622. protected function InsertFlyingBird($sSpecies, $sSex, $iSpeed, $iMotherid, $iFatherId, $iFlyingSpeed)
  623. {
  624. $oNew = MetaModel::NewObject('FlyingBird');
  625. $oNew->Set('species', $sSpecies);
  626. $oNew->Set('sex', $sSex);
  627. $oNew->Set('speed', $iSpeed);
  628. $oNew->Set('mother', $iMotherid);
  629. $oNew->Set('father', $iFatherId);
  630. $oNew->Set('flyingspeed', $iFlyingSpeed);
  631. return $this->ObjectToDB($oNew);
  632. }
  633. private function InsertGroup($sName, $iLeaderId)
  634. {
  635. $oNew = MetaModel::NewObject('Group');
  636. $oNew->Set('name', $sName);
  637. $oNew->Set('leader', $iLeaderId);
  638. $iId = $oNew->DBInsertNoReload();
  639. return $iId;
  640. }
  641. }
  642. class TestQueriesOnFarm extends MyFarm
  643. {
  644. static public function GetName()
  645. {
  646. return 'Farm test';
  647. }
  648. static public function GetDescription()
  649. {
  650. return 'A series of tests on the farm business model (SQL generation)';
  651. }
  652. protected function CheckQuery($sQuery, $bIsCorrectQuery)
  653. {
  654. if ($bIsCorrectQuery)
  655. {
  656. echo "<h4 style=\"color:green;\">$sQuery</h4>\n";
  657. }
  658. else
  659. {
  660. echo "<h4 style=\"color:red;\">$sQuery</h3>\n";
  661. }
  662. try
  663. {
  664. //$oOql = new OqlInterpreter($sQuery);
  665. //$oTrash = $oOql->ParseObjectQuery();
  666. //self::DumpVariable($oTrash, true);
  667. $oMyFilter = DBObjectSearch::FromOQL($sQuery);
  668. }
  669. catch (OQLException $oOqlException)
  670. {
  671. if ($bIsCorrectQuery)
  672. {
  673. echo "<p>More info on this unexpected failure:<br/>".$oOqlException->getHtmlDesc()."</p>\n";
  674. throw $oOqlException;
  675. return false;
  676. }
  677. else
  678. {
  679. // Everything is fine :-)
  680. echo "<p>More info on this expected failure:\n";
  681. echo "<ul>\n";
  682. echo "<li>".get_class($oOqlException)."</li>\n";
  683. echo "<li>".$oOqlException->getMessage()."</li>\n";
  684. echo "<li>".$oOqlException->getHtmlDesc()."</li>\n";
  685. echo "</ul>\n";
  686. echo "</p>\n";
  687. return true;
  688. }
  689. }
  690. // The query was correctly parsed, was it expected to be correct ?
  691. if (!$bIsCorrectQuery)
  692. {
  693. throw new UnitTestException("The query '$sQuery' was parsed with success, while it shouldn't (?)");
  694. return false;
  695. }
  696. echo "<p>To OQL: ".$oMyFilter->ToOQL()."</p>";
  697. $this->search_and_show_list($oMyFilter);
  698. //echo "<p>first pass<p>\n";
  699. //self::DumpVariable($oMyFilter, true);
  700. $sQuery1 = MetaModel::MakeSelectQuery($oMyFilter);
  701. //echo "<p>second pass<p>\n";
  702. //self::DumpVariable($oMyFilter, true);
  703. //$sQuery1 = MetaModel::MakeSelectQuery($oMyFilter);
  704. $sSerialize = $oMyFilter->serialize();
  705. echo "<p>Serialized:$sSerialize</p>\n";
  706. $oFilter2 = DBObjectSearch::unserialize($sSerialize);
  707. try
  708. {
  709. $sQuery2 = MetaModel::MakeSelectQuery($oFilter2);
  710. }
  711. catch (Exception $e)
  712. {
  713. echo "<p>Could not compute the query after unserialize</p>\n";
  714. echo "<p>Query 1: $sQuery1</p>\n";
  715. MyHelpers::var_cmp_html($oMyFilter, $oFilter2);
  716. throw $e;
  717. }
  718. //if ($oFilter2 != $oMyFilter) no, they may differ while the resulting query is the same!
  719. if ($sQuery1 != $sQuery2)
  720. {
  721. echo "<p>serialize/unserialize mismatch :-(</p>\n";
  722. MyHelpers::var_cmp_html($sQuery1, $sQuery2);
  723. MyHelpers::var_cmp_html($oMyFilter, $oFilter2);
  724. return false;
  725. }
  726. return true;
  727. }
  728. protected function DoExecute()
  729. {
  730. // $this->ReportError("Found two different OQL expression out of the (same?) filter: <em>$sExpr1</em> != <em>$sExpr2</em>");
  731. // $this->ReportSuccess('Found '.$oSet->Count()." objects of class $sClassName");
  732. echo "<h3>Create protagonists...</h3>";
  733. $iId1 = $this->InsertMammal('human', 'male', 10, 0, 0, 'romanoff', 192, '1971-07-19');
  734. $iId2 = $this->InsertMammal('human', 'female', 9, 0, 0, 'rouanita', 165, '1983-01-23');
  735. $this->InsertMammal('human', 'female', 3, $iId2, $iId1, 'pomme', 169, '2008-02-23');
  736. $this->InsertMammal('pig', 'female', 3, 0, 0, 'grouinkette', 85, '2006-06-01');
  737. $this->InsertMammal('donkey', 'female', 3, 0, 0, 'muleta', 124, '2003-11-11');
  738. $this->InsertBird('rooster', 'male', 12, 0, 0);
  739. $this->InsertFlyingBird('pie', 'female', 11, 0, 0, 35);
  740. // Benchmarking
  741. //
  742. if (false)
  743. {
  744. define ('COUNT_BENCHMARK', 10);
  745. echo "<h3>Parsing a long query, ".COUNT_BENCHMARK." times</h3>";
  746. $sQuery = "SELECT Animal AS Child JOIN Mammal AS Dad ON Child.father = Dad.id JOIN Animal AS Mum ON Child.mother = Mum.id WHERE Dad.birth < DATE_SUB(CURRENT_DATE(), INTERVAL 10 YEAR) AND Dad.height * 2 <= ROUND(TO_DAYS(Dad.birth) / (3 + 1) * 5 - 3)";
  747. $fStart = MyHelpers::getmicrotime();
  748. for($i=0 ; $i < COUNT_BENCHMARK ; $i++)
  749. {
  750. $oMyFilter = DBObjectSearch::FromOQL($sQuery);
  751. }
  752. $fDuration = MyHelpers::getmicrotime() - $fStart;
  753. $fParsingDuration = $fDuration / COUNT_BENCHMARK;
  754. echo "<p>Mean time by op: $fParsingDuration</p>";
  755. }
  756. echo "<h3>Test queries...</h3>";
  757. $aQueries = array(
  758. 'SELECT Animal' => true,
  759. 'SELECT Animal WHERE Animal.pkey = 1' => false,
  760. 'SELECT Animal WHERE Animal.id = 1' => true,
  761. 'SELECT Aniiimal' => false,
  762. 'SELECTe Animal' => false,
  763. 'SELECT * FROM Animal' => false,
  764. 'SELECT Animal AS zoo WHERE zoo.species = \'human\'' => true,
  765. 'SELECT Animal AS zoo WHERE species = \'human\'' => true,
  766. 'SELECT Animal AS zoo WHERE espece = \'human\'' => false,
  767. 'SELECT Animal AS zoo WHERE zoo.species IN (\'human\', "pig")' => true,
  768. 'SELECT Animal AS zoo WHERE CONCATENATION(zoo.species, zoo.sex) LIKE "hum%male"' => false,
  769. 'SELECT Animal AS zoo WHERE CONCAT(zoo.species, zoo.sex) LIKE "hum%male"' => true,
  770. 'SELECT Animal AS zoo WHERE zoo.species NOT IN (\'human\', "pig")' => true,
  771. 'SELECT Animal AS zoo WHERE zoo.kind = \'human\'' => false,
  772. 'SELECT Animal WHERE Animal.species = \'human\' AND Animal.sex = \'female\'' => true,
  773. 'SELECT Mammal AS x WHERE (x.species = \'human\' AND x.name LIKE \'ro%\') OR (x.species = \'donkey\' AND x.name LIKE \'po%\')' => true,
  774. 'SELECT Mammal AS x WHERE x.species = \'human\' AND x.name LIKE \'ro%\' OR x.species = \'donkey\' AND x.name LIKE \'po%\'' => true,
  775. 'SELECT Mammal AS m WHERE MONTH(m.birth) = 7' => true,
  776. 'SELECT Mammal AS m WHERE DAY(m.birth) = 19' => true,
  777. 'SELECT Mammal AS m WHERE YEAR(m.birth) = 1971' => true,
  778. 'SELECT Mammal AS m WHERE m.birth < DATE_SUB(CURRENT_DATE(), INTERVAL 10 YEAR)' => true,
  779. 'SELECT Mammal AS m WHERE m.birth > DATE_SUB(NOW(), INTERVAL 2000 DAY)' => true,
  780. 'SELECT Mammal AS m WHERE (TO_DAYS(NOW()) - TO_DAYS(m.birth)) > 2000' => true,
  781. 'SELECT Mammal AS m WHERE m.name = IF(FLOOR(ROUND(m.height)) > 2, "pomme", "romain")' => true,
  782. 'SELECT Mammal AS m WHERE (1 + 2' => false,
  783. 'SELECT Mammal AS m WHERE (1 + 2 * 4 / 23) > 0' => true,
  784. 'SELECT Mammal AS m WHERE (4 / 23 * 2 + 1) > 0' => true,
  785. 'SELECT Mammal AS m WHERE 1/0' => true,
  786. 'SELECT Mammal AS m WHERE MONTH(m.birth) = 7' => true,
  787. 'SELECT Animal JOIN Group ON Group.leader = Animal.id' => true,
  788. 'SELECT Group JOIN Animal ON Group.leader = Animal.id' => true,
  789. 'SELECT Animal AS A JOIN Group AS G1 ON G1.leader = A.id' => true,
  790. 'SELECT Animal AS A JOIN Group AS G ON FooClass.leader = A.id' => false,
  791. 'SELECT Animal AS A JOIN Group AS G ON G.leader = FooClass.id' => false,
  792. 'SELECT Animal AS A JOIN Group AS G ON G.masterchief = A.id' => false,
  793. 'SELECT Animal AS A JOIN Group AS G ON A.id = G.leader' => false,
  794. 'SELECT Animal AS A JOIN Group AS G ON G.leader = A.id WHERE A.sex=\'male\' OR G.qwerty = 123' => false,
  795. 'SELECT Animal AS A JOIN Group AS G ON G.leader = A.id WHERE A.sex=\'male\' OR G.name LIKE "a%"' => true,
  796. 'SELECT Animal AS A JOIN Group AS G ON G.leader = A.id WHERE A.id = 1' => true,
  797. 'SELECT Animal AS A JOIN Group AS G ON G.leader = A.id WHERE id = 1' => false,
  798. 'SELECT Animal AS A JOIN Group AS G ON A.member = G.id' => false,
  799. 'SELECT Mammal AS M JOIN Group AS G ON M.member = G.id' => true,
  800. 'SELECT Mammal AS M JOIN Group AS G ON A.member = G.id' => false,
  801. 'SELECT Mammal AS myAlias JOIN Group AS myAlias ON myAlias.member = myAlias.id' => false,
  802. 'SELECT Mammal AS Mammal JOIN Group AS Mammal ON Mammal.member = Mammal.id' => false,
  803. 'SELECT Group AS G WHERE G.leader_name LIKE "%"' => true,
  804. 'SELECT Group AS G WHERE G.leader_speed < 100000' => true,
  805. 'SELECT Mammal AS M JOIN Group AS G ON M.member = G.id WHERE G.leader_name LIKE "%"' => true,
  806. 'SELECT Mammal AS M JOIN Group AS G ON M.member = G.id WHERE G.leader_speed < 100000' => true,
  807. 'SELECT Mammal AS Child JOIN Mammal AS Dad ON Child.father = Dad.id' => true,
  808. 'SELECT Mammal AS Child JOIN Animal AS Dad ON Child.father = Dad.id' => true,
  809. 'SELECT Animal AS Child JOIN Mammal AS Dad ON Child.father = Dad.id' => true,
  810. 'SELECT Animal AS Child JOIN Animal AS Dad ON Child.father = Dad.id' => true,
  811. 'SELECT Animal AS Dad JOIN Animal AS Child ON Child.father = Dad.id' => true,
  812. 'SELECT Animal AS Child JOIN Animal AS Dad ON Child.father = Dad.id JOIN Animal AS Mum ON Child.mother = Mum.id' => true,
  813. 'SELECT Animal AS Child JOIN Animal AS Dad ON Child.father = Dad.id JOIN Animal AS Mum ON Child.mother = Mum.id WHERE Dad.id = 1' => true,
  814. 'SELECT Animal AS Child JOIN Animal AS Dad ON Child.father = Dad.id JOIN Animal AS Mum ON Child.mother = Mum.id WHERE Dad.name = \'romanoff\'' => false,
  815. 'SELECT Animal AS Child JOIN Mammal AS Dad ON Child.father = Dad.id' => true,
  816. 'SELECT Animal AS Child JOIN Mammal AS Dad ON Child.father = Dad.id JOIN Animal AS Mum ON Child.mother = Mum.id WHERE Dad.name = \'romanoff\' OR Mum.speed = 0' => true,
  817. 'SELECT Animal AS Dad JOIN Animal AS Child ON Child.father = Dad.id JOIN Animal AS Mum ON Child.mother = Mum.id' => true,
  818. 'SELECT Mammal AS Dad JOIN Mammal AS Child ON Child.father = Dad.id' => true,
  819. 'SELECT Mammal AS Dad JOIN Mammal AS Child ON Child.father = Dad.id JOIN Mammal AS Mum ON Child.mother = Mum.id WHERE Dad.name = \'romanoff\' OR Mum.name=\'chloe\' OR Child.name=\'bizounours\'' => true,
  820. // Specifying multiple objects
  821. 'SELECT Animal FROM Animal' => true,
  822. 'SELECT yelele FROM Animal' => false,
  823. 'SELECT Animal FROM Animal AS A' => false,
  824. 'SELECT A FROM Animal AS A' => true,
  825. );
  826. //$aQueries = array(
  827. // 'SELECT Mammal AS M JOIN Group AS G ON M.member = G.id WHERE G.leader_name LIKE "%"' => true,
  828. //);
  829. foreach($aQueries as $sQuery => $bIsCorrect)
  830. {
  831. $this->CheckQuery($sQuery, $bIsCorrect);
  832. }
  833. return true;
  834. }
  835. }
  836. ///////////////////////////////////////////////////////////////////////////
  837. // Test data load
  838. ///////////////////////////////////////////////////////////////////////////
  839. class TestBulkChangeOnFarm extends TestBizModel
  840. {
  841. static public function GetName()
  842. {
  843. return 'Farm test - data load';
  844. }
  845. static public function GetDescription()
  846. {
  847. return 'Bulk load';
  848. }
  849. static public function GetConfigFile() {return '/config-test-farm.php';}
  850. protected function DoPrepare()
  851. {
  852. parent::DoPrepare();
  853. $this->ResetDB();
  854. MetaModel::DBCheckIntegrity();
  855. }
  856. protected function DoExecute()
  857. {
  858. // $this->ReportError("Found two different OQL expression out of the (same?) filter: <em>$sExpr1</em> != <em>$sExpr2</em>");
  859. // $this->ReportSuccess('Found '.$oSet->Count()." objects of class $sClassName");
  860. $oParser = new CSVParser("denomination,hauteur,age
  861. suzy,123,2009-01-01
  862. chita,456,
  863. ", ',', '"');
  864. $aData = $oParser->ToArray(1, array('_name', '_height', '_birth'));
  865. self::DumpVariable($aData);
  866. $oBulk = new BulkChange(
  867. 'Mammal',
  868. $aData,
  869. // attributes
  870. array('name' => '_name', 'height' => '_height', 'birth' => '_birth'),
  871. // ext keys
  872. array(),
  873. // reconciliation
  874. array('name')
  875. );
  876. $oMyChange = MetaModel::NewObject("CMDBChange");
  877. $oMyChange->Set("date", time());
  878. $oMyChange->Set("userinfo", "Testor");
  879. $iChangeId = $oMyChange->DBInsert();
  880. // echo "Created new change: $iChangeId</br>";
  881. echo "<h3>Planned for loading...</h3>";
  882. $aRes = $oBulk->Process();
  883. self::DumpVariable($aRes);
  884. echo "<h3>Go for loading...</h3>";
  885. $aRes = $oBulk->Process($oMyChange);
  886. self::DumpVariable($aRes);
  887. return;
  888. $oRawData = array(
  889. 'Mammal',
  890. array('species', 'sex', 'speed', 'mother', 'father', 'name', 'height', 'birth'),
  891. "human,male,23,0,0,romulus,192,1971
  892. human,male,23,0,0,remus,154,-50
  893. human,male,23,0,0,julius,160,-49
  894. human,female,23,0,0,cleopatra,142,-50
  895. pig,female,23,0,0,confucius,50,2003"
  896. );
  897. }
  898. }
  899. ///////////////////////////////////////////////////////////////////////////
  900. // Test data load
  901. ///////////////////////////////////////////////////////////////////////////
  902. class TestFullTextSearchOnFarm extends MyFarm
  903. {
  904. static public function GetName()
  905. {
  906. return 'Farm test - full text search';
  907. }
  908. static public function GetDescription()
  909. {
  910. return 'Focus on the full text search feature';
  911. }
  912. protected function DoExecute()
  913. {
  914. echo "<h3>Create protagonists...</h3>";
  915. $iId1 = $this->InsertMammal('human', 'male', 10, 0, 0, 'romanoff', 192, '1971-07-19');
  916. $iId2 = $this->InsertMammal('human', 'female', 9, 0, 0, 'rouanita', 165, '1983-01-23');
  917. $this->InsertMammal('human', 'female', 3, $iId2, $iId1, 'pomme', 169, '2008-02-23');
  918. $this->InsertMammal('pig', 'female', 3, 0, 0, 'grouinkette', 85, '2006-06-01');
  919. $this->InsertMammal('donkey', 'female', 3, 0, 0, 'muleta', 124, '2003-11-11');
  920. $this->InsertBird('rooster', 'male', 12, 0, 0);
  921. $this->InsertFlyingBird('pie', 'female', 11, 0, 0, 35);
  922. echo "<h3>Search...</h3>";
  923. $oSearch = new DBObjectSearch('Mammal');
  924. $oSearch->AddCondition_FullText('manof');
  925. //$oResultSet = new DBObjectSet($oSearch);
  926. $this->search_and_show_list($oSearch);
  927. }
  928. }
  929. ///////////////////////////////////////////////////////////////////////////
  930. // Benchmark queries
  931. ///////////////////////////////////////////////////////////////////////////
  932. class TestItopEfficiency extends TestBizModel
  933. {
  934. static public function GetName()
  935. {
  936. return 'Itop - benchmark';
  937. }
  938. static public function GetDescription()
  939. {
  940. return 'Measure time to perform the queries';
  941. }
  942. static public function GetConfigFile() {return '/config-itop.php';}
  943. protected function DoBenchmark($sOqlQuery)
  944. {
  945. echo "<h3>Testing query: $sOqlQuery</h3>";
  946. $fStart = MyHelpers::getmicrotime();
  947. for($i=0 ; $i < COUNT_BENCHMARK ; $i++)
  948. {
  949. $oFilter = DBObjectSearch::FromOQL($sOqlQuery);
  950. }
  951. $fDuration = MyHelpers::getmicrotime() - $fStart;
  952. $fParsingDuration = $fDuration / COUNT_BENCHMARK;
  953. $fStart = MyHelpers::getmicrotime();
  954. for($i=0 ; $i < COUNT_BENCHMARK ; $i++)
  955. {
  956. $sSQL = MetaModel::MakeSelectQuery($oFilter);
  957. }
  958. $fDuration = MyHelpers::getmicrotime() - $fStart;
  959. $fBuildDuration = $fDuration / COUNT_BENCHMARK;
  960. $fStart = MyHelpers::getmicrotime();
  961. for($i=0 ; $i < COUNT_BENCHMARK ; $i++)
  962. {
  963. $res = CMDBSource::Query($sSQL);
  964. }
  965. $fDuration = MyHelpers::getmicrotime() - $fStart;
  966. $fQueryDuration = $fDuration / COUNT_BENCHMARK;
  967. // The fetch could not be repeated with the same results
  968. // But we've seen so far that is was very very quick to exec
  969. // So it makes sense to benchmark it a single time
  970. $fStart = MyHelpers::getmicrotime();
  971. $aRow = CMDBSource::FetchArray($res);
  972. $fDuration = MyHelpers::getmicrotime() - $fStart;
  973. $fFetchDuration = $fDuration;
  974. $fStart = MyHelpers::getmicrotime();
  975. for($i=0 ; $i < COUNT_BENCHMARK ; $i++)
  976. {
  977. $sOql = $oFilter->ToOQL();
  978. }
  979. $fDuration = MyHelpers::getmicrotime() - $fStart;
  980. $fToOqlDuration = $fDuration / COUNT_BENCHMARK;
  981. echo "<ul>\n";
  982. echo "<li>Parsing: $fParsingDuration</li>\n";
  983. echo "<li>Build: $fBuildDuration</li>\n";
  984. echo "<li>Query: $fQueryDuration</li>\n";
  985. echo "<li>Fetch: $fFetchDuration</li>\n";
  986. echo "<li>ToOql: $fToOqlDuration</li>\n";
  987. echo "</ul>\n";
  988. // Everything but the ToOQL (wich is interesting, anyhow)
  989. $fTotal = $fParsingDuration + $fBuildDuration + $fQueryDuration + $fFetchDuration;
  990. return array(
  991. 'rows' => CMDBSource::NbRows($res),
  992. 'duration (s)' => round($fTotal, 4),
  993. 'parsing (%)' => round(100 * $fParsingDuration / $fTotal, 1),
  994. 'build SQL (%)' => round(100 * $fBuildDuration / $fTotal, 1),
  995. 'query exec (%)' => round(100 * $fQueryDuration / $fTotal, 1),
  996. 'fetch (%)' => round(100 * $fFetchDuration / $fTotal, 1),
  997. 'to OQL (%)' => round(100 * $fToOqlDuration / $fTotal, 1),
  998. 'parsing+build (%)' => round(100 * ($fParsingDuration + $fBuildDuration) / $fTotal, 1),
  999. );
  1000. }
  1001. protected function DoExecute()
  1002. {
  1003. define ('COUNT_BENCHMARK', 3);
  1004. echo "<p>The test will be repeated ".COUNT_BENCHMARK." times</p>";
  1005. $aQueries = array(
  1006. 'SELECT CMDBChangeOpSetAttribute',
  1007. 'SELECT CMDBChangeOpSetAttribute WHERE id=10',
  1008. 'SELECT CMDBChangeOpSetAttribute WHERE id=123456789',
  1009. 'SELECT CMDBChangeOpSetAttribute WHERE CMDBChangeOpSetAttribute.id=10',
  1010. 'SELECT Ticket',
  1011. 'SELECT Ticket WHERE id=1',
  1012. 'SELECT Person',
  1013. 'SELECT Person WHERE id=1',
  1014. 'SELECT Server',
  1015. 'SELECT Server WHERE id=1',
  1016. 'SELECT Incident JOIN Person ON Incident.agent_id = Person.id WHERE Person.id = 5',
  1017. );
  1018. $aStats = array();
  1019. foreach ($aQueries as $sOQL)
  1020. {
  1021. $aStats[$sOQL] = $this->DoBenchmark($sOQL);
  1022. }
  1023. $aData = array();
  1024. foreach ($aStats as $sOQL => $aResults)
  1025. {
  1026. $aValues = array();
  1027. $aValues['OQL'] = htmlentities($sOQL);
  1028. foreach($aResults as $sDesc => $sInfo)
  1029. {
  1030. $aValues[$sDesc] = htmlentities($sInfo);
  1031. }
  1032. $aData[] = $aValues;
  1033. }
  1034. echo MyHelpers::make_table_from_assoc_array($aData);
  1035. }
  1036. }
  1037. ///////////////////////////////////////////////////////////////////////////
  1038. // Test bulk load API
  1039. ///////////////////////////////////////////////////////////////////////////
  1040. class TestItopBulkLoad extends TestBizModel
  1041. {
  1042. static public function GetName()
  1043. {
  1044. return 'Itop - test BulkChange class';
  1045. }
  1046. static public function GetDescription()
  1047. {
  1048. return 'Execute a bulk change at the Core API level';
  1049. }
  1050. static public function GetConfigFile() {return '/config-itop.php';}
  1051. protected function DoExecute()
  1052. {
  1053. $oParser = new CSVParser("name,org_id->name,brand,model
  1054. Server1,Demo,,
  1055. server4,Demo,,
  1056. ", ',', '"');
  1057. $aData = $oParser->ToArray(1, array('_name', '_org_name', '_brand', '_model'));
  1058. self::DumpVariable($aData);
  1059. $oBulk = new BulkChange(
  1060. 'Server',
  1061. $aData,
  1062. // attributes
  1063. array('name' => '_name', 'brand' => '_brand', 'model' => '_model'),
  1064. // ext keys
  1065. array('org_id' => array('name' => '_org_name')),
  1066. // reconciliation
  1067. array('name'),
  1068. // Synchro - scope
  1069. "SELECT Server",
  1070. // Synchro - set attribute on missing objects
  1071. array ('brand' => 'you let package', 'model' => 'tpe', 'cpu' => 'it is pay you')
  1072. );
  1073. if (false)
  1074. {
  1075. $oMyChange = MetaModel::NewObject("CMDBChange");
  1076. $oMyChange->Set("date", time());
  1077. $oMyChange->Set("userinfo", "Testor");
  1078. $iChangeId = $oMyChange->DBInsert();
  1079. // echo "Created new change: $iChangeId</br>";
  1080. }
  1081. echo "<h3>Planned for loading...</h3>";
  1082. $aRes = $oBulk->Process();
  1083. self::DumpVariable($aRes);
  1084. if (false)
  1085. {
  1086. echo "<h3>Go for loading...</h3>";
  1087. $aRes = $oBulk->Process($oMyChange);
  1088. self::DumpVariable($aRes);
  1089. }
  1090. return;
  1091. }
  1092. }
  1093. ///////////////////////////////////////////////////////////////////////////
  1094. // Test data load
  1095. ///////////////////////////////////////////////////////////////////////////
  1096. class TestImportREST extends TestWebServices
  1097. {
  1098. static public function GetName()
  1099. {
  1100. return 'CSV import (REST)';
  1101. }
  1102. static public function GetDescription()
  1103. {
  1104. return 'Test various options and fonctionality of import.php';
  1105. }
  1106. protected function DoExecSingleLoad($aLoadSpec)
  1107. {
  1108. $sCsvData = $aLoadSpec['csvdata'];
  1109. echo "<div style=\"padding: 10;\">\n";
  1110. echo "<h3 style=\"background-color: #ddddff; padding: 10;\">{$aLoadSpec['desc']}</h3>\n";
  1111. $aPostData = array('csvdata' => $sCsvData);
  1112. $aGetParams = array();
  1113. $aGetParamReport = array();
  1114. foreach($aLoadSpec['args'] as $sArg => $sValue)
  1115. {
  1116. $aGetParams[] = $sArg.'='.urlencode($sValue);
  1117. $aGetParamReport[] = $sArg.'='.$sValue;
  1118. }
  1119. $sGetParams = implode('&', $aGetParams);
  1120. $sLogin = isset($aLoadSpec['login']) ? $aLoadSpec['login'] : 'admin';
  1121. $sPassword = isset($aLoadSpec['password']) ? $aLoadSpec['password'] : 'admin';
  1122. $sRes = self::DoPostRequestAuth('../webservices/import.php?'.$sGetParams, $aPostData, $sLogin, $sPassword);
  1123. $sArguments = implode('<br/>', $aGetParamReport);
  1124. if (strlen($sCsvData) > 5000)
  1125. {
  1126. $sCsvDataViewable = 'INPUT TOO LONG TO BE DISPLAYED ('.strlen($sCsvData).")\n".substr($sCsvData, 0, 500)."\n... TO BE CONTINUED";
  1127. }
  1128. else
  1129. {
  1130. $sCsvDataViewable = $sCsvData;
  1131. }
  1132. echo "<div style=\"\">\n";
  1133. echo " <div style=\"float:left; width:20%; padding:5; background-color:#eeeeff;\">\n";
  1134. echo " $sArguments\n";
  1135. echo " </div>\n";
  1136. echo " <div style=\"float:right; width:75%; padding:5; background-color:#eeeeff\">\n";
  1137. echo " <pre class=\"vardump\">$sCsvDataViewable</pre>\n";
  1138. echo " </div>\n";
  1139. echo "</div>\n";
  1140. echo "<pre class=\"vardump\" style=\"clear: both; padding: 15; background-color: black; color: green;\">$sRes</pre>\n";
  1141. echo "</div>\n";
  1142. }
  1143. protected function DoExecute()
  1144. {
  1145. $aLoads = array(
  1146. array(
  1147. 'desc' => 'Missing class',
  1148. 'login' => 'admin',
  1149. 'password' => 'admin',
  1150. 'args' => array(
  1151. ),
  1152. 'csvdata' => "xxx",
  1153. ),
  1154. array(
  1155. 'desc' => 'Wrong class',
  1156. 'login' => 'admin',
  1157. 'password' => 'admin',
  1158. 'args' => array(
  1159. 'class' => 'toto',
  1160. ),
  1161. 'csvdata' => "xxx",
  1162. ),
  1163. array(
  1164. 'desc' => 'Wrong output type',
  1165. 'login' => 'admin',
  1166. 'password' => 'admin',
  1167. 'args' => array(
  1168. 'class' => 'NetworkDevice',
  1169. 'output' => 'onthefly',
  1170. ),
  1171. 'csvdata' => "xxx",
  1172. ),
  1173. array(
  1174. 'desc' => 'Wrong report level',
  1175. 'login' => 'admin',
  1176. 'password' => 'admin',
  1177. 'args' => array(
  1178. 'class' => 'NetworkDevice',
  1179. 'reportlevel' => 'errors|ouarnings|changed',
  1180. ),
  1181. 'csvdata' => "xxx",
  1182. ),
  1183. array(
  1184. 'desc' => 'Weird format, working anyhow...',
  1185. 'login' => 'admin',
  1186. 'password' => 'admin',
  1187. 'args' => array(
  1188. 'class' => 'Server',
  1189. 'output' => 'details',
  1190. 'separator' => '*',
  1191. 'qualifier' => '@',
  1192. 'reconciliationkeys' => 'org_id,name',
  1193. ),
  1194. 'csvdata' => 'name*org_id
  1195. server01*2
  1196. @server02@@combodo@* 2
  1197. server45*99',
  1198. ),
  1199. array(
  1200. 'desc' => 'Load an organization',
  1201. 'login' => 'admin',
  1202. 'password' => 'admin',
  1203. 'args' => array(
  1204. 'class' => 'Organization',
  1205. 'output' => 'details',
  1206. 'reconciliationkeys' => '',
  1207. ),
  1208. 'csvdata' => "name;code\nWorldCompany;WCY",
  1209. ),
  1210. array(
  1211. 'desc' => 'Load a location',
  1212. 'login' => 'admin',
  1213. 'password' => 'admin',
  1214. 'args' => array(
  1215. 'class' => 'Location',
  1216. 'output' => 'details',
  1217. 'reconciliationkeys' => '',
  1218. ),
  1219. 'csvdata' => "name;org_id;address\nParis;1;Centre de la Franca",
  1220. ),
  1221. array(
  1222. 'desc' => 'Load a person',
  1223. 'login' => 'admin',
  1224. 'password' => 'admin',
  1225. 'args' => array(
  1226. 'class' => 'Person',
  1227. 'output' => 'details',
  1228. 'reconciliationkeys' => '',
  1229. ),
  1230. 'csvdata' => "email;name;first_name;org_id;phone\njohn.foo@starac.com;Foo;John;1;+33(1)23456789",
  1231. ),
  1232. array(
  1233. 'desc' => 'Load a person - wrong email format',
  1234. 'login' => 'admin',
  1235. 'password' => 'admin',
  1236. 'args' => array(
  1237. 'class' => 'Person',
  1238. 'output' => 'details',
  1239. 'reconciliationkeys' => '',
  1240. ),
  1241. 'csvdata' => "email;name;first_name;org_id\nemailPASbon;Foo;John;1",
  1242. ),
  1243. array(
  1244. 'desc' => 'Load a team',
  1245. 'login' => 'admin',
  1246. 'password' => 'admin',
  1247. 'args' => array(
  1248. 'class' => 'Team',
  1249. 'output' => 'details',
  1250. 'reconciliationkeys' => '',
  1251. ),
  1252. 'csvdata' => "name;org_id;location_name\nSquadra Azzura2;1;Paris",
  1253. ),
  1254. array(
  1255. 'desc' => 'Load server',
  1256. 'login' => 'admin',
  1257. 'password' => 'admin',
  1258. 'args' => array(
  1259. 'class' => 'Server',
  1260. 'output' => 'details',
  1261. 'reconciliationkeys' => '',
  1262. ),
  1263. 'csvdata' => "name;status;owner_name;location_name;location_id->org_name;os_family;os_version;management_ip;cpu;ram;brand;model;serial_number\nlocalhost.;production;Demo;Grenoble;Demo;Ubuntu 9.10;2.6.31-19-generic-#56-Ubuntu SMP Thu Jan 28 01:26:53 UTC 2010;16.16.230.232;Intel(R) Core(TM)2 Duo CPU T7100 @ 1.80GHz;2005;Hewlett-Packard;HP Compaq 6510b (GM108UC#ABF);CNU7370BNP",
  1264. ),
  1265. array(
  1266. 'desc' => 'Load server - wrong value for status',
  1267. 'login' => 'admin',
  1268. 'password' => 'admin',
  1269. 'args' => array(
  1270. 'class' => 'Server',
  1271. 'output' => 'details',
  1272. 'reconciliationkeys' => '',
  1273. ),
  1274. 'csvdata' => "name;status;owner_name;location_name;location_id->org_name;os_family;os_version;management_ip;cpu;ram;brand;model;serial_number\nlocalhost.;Production;Demo;Grenoble;Demo;Ubuntu 9.10;2.6.31-19-generic-#56-Ubuntu SMP Thu Jan 28 01:26:53 UTC 2010;16.16.230.232;Intel(R) Core(TM)2 Duo CPU T7100 @ 1.80GHz;2005;Hewlett-Packard;HP Compaq 6510b (GM108UC#ABF);CNU7370BNP",
  1275. ),
  1276. array(
  1277. 'desc' => 'Load NW if',
  1278. 'login' => 'admin',
  1279. 'password' => 'admin',
  1280. 'args' => array(
  1281. 'class' => 'NetworkInterface',
  1282. 'output' => 'details',
  1283. 'reconciliationkeys' => '',
  1284. ),
  1285. 'csvdata' => "name;status;org_id;device_name;physical_type;ip_address;ip_mask;mac_address;speed\neth0;implementation;2;localhost.;ethernet;16.16.230.232;255.255.240.0;00:1a:4b:68:e3:97;\nlo;implementation;2;localhost.;ethernet;127.0.0.1;255.0.0.0;;",
  1286. ),
  1287. // Data Bruno
  1288. array(
  1289. 'desc' => 'Load NW devices from real life',
  1290. 'login' => 'admin',
  1291. 'password' => 'admin',
  1292. 'args' => array(
  1293. 'class' => 'NetworkDevice',
  1294. 'output' => 'details',
  1295. 'reconciliationkeys' => 'org_id->name,name',
  1296. ),
  1297. 'csvdata' => 'name;management_ip;importance;org_id->name;type
  1298. truc-machin-bidule;172.15.255.150;high;My Company/Department;switch
  1299. 10.15.255.222;10.15.255.222;high;My Company/Department;switch',
  1300. ),
  1301. array(
  1302. 'desc' => 'Load NW ifs',
  1303. 'login' => 'admin',
  1304. 'password' => 'admin',
  1305. 'args' => array(
  1306. 'class' => 'NetworkInterface',
  1307. 'output' => 'details',
  1308. 'reconciliationkeys' => 'device_id->name,name',
  1309. ),
  1310. 'csvdata' => 'device_id->name;org_id->name;name;ip_address;ip_mask;speed;link_type;mac_address;physical_type
  1311. truc-machin-bidule;My Company/Department;"GigabitEthernet44";;;0;downlink;00 12 F2 CB C4 EB ;ethernet
  1312. truc-machin-bidule;My Company/Department;"GigabitEthernet38";;;0;downlink;00 12 F2 CB C4 E5 ;ethernet
  1313. un-autre;My Company/Department;"GigabitEthernet2/3";;;1000000000;uplink;00 12 F2 20 0F 1A ;ethernet',
  1314. ),
  1315. array(
  1316. 'desc' => 'The simplest data load',
  1317. 'login' => 'admin',
  1318. 'password' => 'admin',
  1319. 'args' => array(
  1320. 'class' => 'Location',
  1321. 'output' => 'details',
  1322. ),
  1323. 'csvdata' => "name\nParis",
  1324. ),
  1325. array(
  1326. 'desc' => 'The simplest data load + org',
  1327. 'login' => 'admin',
  1328. 'password' => 'admin',
  1329. 'args' => array(
  1330. 'class' => 'Location',
  1331. 'output' => 'details',
  1332. ),
  1333. 'csvdata' => "name;org_id\nParis;2",
  1334. ),
  1335. array(
  1336. 'desc' => 'The simplest data load + org (name)',
  1337. 'login' => 'admin',
  1338. 'password' => 'admin',
  1339. 'args' => array(
  1340. 'class' => 'Location',
  1341. 'output' => 'details',
  1342. ),
  1343. 'csvdata' => "name;org_name\nParis;Demo",
  1344. ),
  1345. array(
  1346. 'desc' => 'The simplest data load + org (code)',
  1347. 'login' => 'admin',
  1348. 'password' => 'admin',
  1349. 'args' => array(
  1350. 'class' => 'Location',
  1351. 'output' => 'details',
  1352. ),
  1353. 'csvdata' => "name;org_id->code\nParis;DEMO",
  1354. ),
  1355. array(
  1356. 'desc' => 'Ouput: summary',
  1357. 'login' => 'admin',
  1358. 'password' => 'admin',
  1359. 'args' => array(
  1360. 'class' => 'Location',
  1361. 'output' => 'summary',
  1362. ),
  1363. 'csvdata' => "name;org_id->code\nParis;DEMO",
  1364. ),
  1365. array(
  1366. 'desc' => 'Ouput: retcode',
  1367. 'login' => 'admin',
  1368. 'password' => 'admin',
  1369. 'args' => array(
  1370. 'class' => 'Location',
  1371. 'output' => 'retcode',
  1372. ),
  1373. 'csvdata' => "name;org_id->code\nParis;DEMO",
  1374. ),
  1375. array(
  1376. 'desc' => 'Error in reconciliation list',
  1377. 'login' => 'admin',
  1378. 'password' => 'admin',
  1379. 'args' => array(
  1380. 'class' => 'Location',
  1381. 'output' => 'details',
  1382. 'reconciliationkeys' => 'org_id',
  1383. ),
  1384. 'csvdata' => "org_name;name\nDemo;Paris",
  1385. ),
  1386. array(
  1387. 'desc' => 'Error in attribute list that does not allow to compute reconciliation scheme',
  1388. 'login' => 'admin',
  1389. 'password' => 'admin',
  1390. 'args' => array(
  1391. 'class' => 'Location',
  1392. 'output' => 'details',
  1393. ),
  1394. 'csvdata' => "org_name;country\nDemo;France",
  1395. ),
  1396. array(
  1397. 'desc' => 'Error in attribute list - case A',
  1398. 'login' => 'admin',
  1399. 'password' => 'admin',
  1400. 'args' => array(
  1401. 'class' => 'Location',
  1402. 'output' => 'details',
  1403. ),
  1404. 'csvdata' => "name;org\nParis;2",
  1405. ),
  1406. array(
  1407. 'desc' => 'Error in attribute list - case B1 (key->attcode)',
  1408. 'login' => 'admin',
  1409. 'password' => 'admin',
  1410. 'args' => array(
  1411. 'class' => 'Location',
  1412. 'output' => 'details',
  1413. ),
  1414. 'csvdata' => "name;org->code\nParis;DEMO",
  1415. ),
  1416. array(
  1417. 'desc' => 'Error in attribute list - case B2 (key->attcode)',
  1418. 'login' => 'admin',
  1419. 'password' => 'admin',
  1420. 'args' => array(
  1421. 'class' => 'Location',
  1422. 'output' => 'details',
  1423. ),
  1424. 'csvdata' => "name;org_id->duns\nParis;DEMO",
  1425. ),
  1426. array(
  1427. 'desc' => 'Always changing... special comment in change tracking',
  1428. 'login' => 'admin',
  1429. 'password' => 'admin',
  1430. 'args' => array(
  1431. 'class' => 'Location',
  1432. 'output' => 'details',
  1433. 'comment' => 'automated testing'
  1434. ),
  1435. 'csvdata' => "org_name;name;address\nDemo;Le pantheon;Addresse bidon:".((string)microtime(true)),
  1436. ),
  1437. array(
  1438. 'desc' => 'Always changing... but "simulate"',
  1439. 'login' => 'admin',
  1440. 'password' => 'admin',
  1441. 'args' => array(
  1442. 'class' => 'Location',
  1443. 'output' => 'details',
  1444. 'simulate' => '1',
  1445. 'comment' => 'SHOULD NEVER APPEAR IN THE HISTORY'
  1446. ),
  1447. 'csvdata' => "org_name;name;address\nDemo;Le pantheon;restore address?",
  1448. ),
  1449. );
  1450. foreach ($aLoads as $aLoadSpec)
  1451. {
  1452. $this->DoExecSingleLoad($aLoadSpec);
  1453. }
  1454. }
  1455. }
  1456. ///////////////////////////////////////////////////////////////////////////
  1457. // Test massive data load
  1458. ///////////////////////////////////////////////////////////////////////////
  1459. define('IMPORT_COUNT', 4000);
  1460. class TestImportRESTMassive extends TestImportREST
  1461. {
  1462. static public function GetName()
  1463. {
  1464. return 'CSV import (REST) - HUGE data set ('.IMPORT_COUNT.' PCs)';
  1465. }
  1466. static public function GetDescription()
  1467. {
  1468. return 'Stress import.php';
  1469. }
  1470. protected function DoExecute()
  1471. {
  1472. $aLoadSpec = array(
  1473. 'desc' => 'Loading PCs: '.IMPORT_COUNT,
  1474. 'args' => array(
  1475. 'class' => 'PC',
  1476. 'output' => 'summary',
  1477. ),
  1478. 'csvdata' => "name;org_id;brand\n",
  1479. );
  1480. for($i = 0 ; $i <= IMPORT_COUNT ; $i++)
  1481. {
  1482. $aLoadSpec['csvdata'] .= "pc.import.$i;2;Combodo\n";
  1483. }
  1484. $this->DoExecSingleLoad($aLoadSpec);
  1485. }
  1486. }
  1487. ///////////////////////////////////////////////////////////////////////////
  1488. // Test SOAP services
  1489. ///////////////////////////////////////////////////////////////////////////
  1490. $aCreateTicketSpecs = array(
  1491. array(
  1492. 'service_category' => 'BasicServices',
  1493. 'verb' => 'GetVersion',
  1494. // 'expected result' => '1.0.1',
  1495. 'expected result' => '$ITOP_VERSION$ [dev]',
  1496. 'explain result' => 'no comment!',
  1497. 'args' => array(),
  1498. ),
  1499. array(
  1500. 'service_category' => '',
  1501. 'verb' => 'CreateIncidentTicket',
  1502. 'expected result' => true,
  1503. 'explain result' => 'link attribute unknown + a CI not found',
  1504. 'args' => array(
  1505. 'admin', /* sLogin */
  1506. 'admin', /* sPassword */
  1507. 'desc of ticket', /* sDescription */
  1508. 'initial situation blah blah blah', /* sInitialSituation */
  1509. new SOAPExternalKeySearch(array(new SOAPSearchCondition('id', 1))), /* aCallerDesc */
  1510. new SOAPExternalKeySearch(array(new SOAPSearchCondition('name', 'Demo'))), /* aCustomerDesc */
  1511. new SOAPExternalKeySearch(array(new SOAPSearchCondition('name', 'Computers and peripherals'))), /* aServiceDesc */
  1512. new SOAPExternalKeySearch(array(new SOAPSearchCondition('id', 1))), /* aServiceSubcategoryDesc */
  1513. 'sub product of the service', /* sProduct */
  1514. new SOAPExternalKeySearch(array(new SOAPSearchCondition('name', 'Hardware support'))), /* aWorkgroupDesc */
  1515. array(
  1516. new SOAPLinkCreationSpec(
  1517. 'InfrastructureCI',
  1518. array(new SOAPSearchCondition('name', 'dbserver1.demo.com')),
  1519. array(new SOAPAttributeValue('impacting', 'very critical'))
  1520. ),
  1521. new SOAPLinkCreationSpec(
  1522. 'NetworkDevice',
  1523. array(new SOAPSearchCondition('name', 'switch01')),
  1524. array(new SOAPAttributeValue('impact', 'who cares'))
  1525. ),
  1526. new SOAPLinkCreationSpec(
  1527. 'Server',
  1528. array(new SOAPSearchCondition('name', 'thisone')),
  1529. array(new SOAPAttributeValue('impact', 'our lives'))
  1530. ),
  1531. ), /* aImpact */
  1532. '1', /* sImpact */
  1533. '1', /* sUrgency */
  1534. ),
  1535. ),
  1536. array(
  1537. 'service_category' => '',
  1538. 'verb' => 'CreateIncidentTicket',
  1539. 'expected result' => true,
  1540. 'explain result' => 'caller not specified',
  1541. 'args' => array(
  1542. 'admin', /* sLogin */
  1543. 'admin', /* sPassword */
  1544. 'PC burning', /* sDescription */
  1545. 'The power supply suddenly started to warm up', /* sInitialSituation */
  1546. new SOAPExternalKeySearch(), /* aCallerDesc */
  1547. new SOAPExternalKeySearch(array(new SOAPSearchCondition('name', 'Demo'))), /* aCustomerDesc */
  1548. new SOAPExternalKeySearch(array(new SOAPSearchCondition('name', 'Computers and peripherals'))), /* aServiceDesc */
  1549. new SOAPExternalKeySearch(array(new SOAPSearchCondition('id', 1))), /* aServiceSubcategoryDesc */
  1550. 'sub product of the service', /* sProduct */
  1551. new SOAPExternalKeySearch(array(new SOAPSearchCondition('name', 'Hardware support'))), /* aWorkgroupDesc */
  1552. array(
  1553. new SOAPLinkCreationSpec(
  1554. 'InfrastructureCI',
  1555. array(new SOAPSearchCondition('name', 'dbserver1.demo.com')),
  1556. array()
  1557. ), /* aImpact */
  1558. ),
  1559. '1', /* sImpact */
  1560. '1', /* sUrgency */
  1561. ),
  1562. ),
  1563. array(
  1564. 'service_category' => '',
  1565. 'verb' => 'CreateIncidentTicket',
  1566. 'expected result' => false,
  1567. 'explain result' => 'wrong class on CI to attach',
  1568. 'args' => array(
  1569. 'admin', /* sLogin */
  1570. 'admin', /* sPassword */
  1571. 'PC burning', /* sDescription */
  1572. 'The power supply suddenly started to warm up', /* sInitialSituation */
  1573. new SOAPExternalKeySearch(array(new SOAPSearchCondition('id', 1))), /* aCallerDesc */
  1574. new SOAPExternalKeySearch(array(new SOAPSearchCondition('name', 'Demo'))), /* aCustomerDesc */
  1575. new SOAPExternalKeySearch(array(new SOAPSearchCondition('name', 'Computers and peripherals'))), /* aServiceDesc */
  1576. new SOAPExternalKeySearch(array(new SOAPSearchCondition('id', 1))), /* aServiceSubcategoryDesc */
  1577. 'sub product of the service', /* sProduct */
  1578. new SOAPExternalKeySearch(array(new SOAPSearchCondition('name', 'Hardware support'))), /* aWorkgroupDesc */
  1579. array(
  1580. new SOAPLinkCreationSpec(
  1581. 'logInfra',
  1582. array(new SOAPSearchCondition('dummyfiltercode', 2)),
  1583. array(new SOAPAttributeValue('impact', 'very critical'))
  1584. ),
  1585. ), /* aImpact */
  1586. '1', /* sImpact */
  1587. '1', /* sUrgency */
  1588. ),
  1589. ),
  1590. array(
  1591. 'service_category' => '',
  1592. 'verb' => 'CreateIncidentTicket',
  1593. 'expected result' => false,
  1594. 'explain result' => 'wrong search condition on CI to attach',
  1595. 'args' => array(
  1596. 'admin', /* sLogin */
  1597. 'admin', /* sPassword */
  1598. 'PC burning', /* sDescription */
  1599. 'The power supply suddenly started to warm up', /* sInitialSituation */
  1600. new SOAPExternalKeySearch(array(new SOAPSearchCondition('id', 1))), /* aCallerDesc */
  1601. new SOAPExternalKeySearch(array(new SOAPSearchCondition('name', 'Demo'))), /* aCustomerDesc */
  1602. new SOAPExternalKeySearch(array(new SOAPSearchCondition('name', 'Computers and peripherals'))), /* aServiceDesc */
  1603. new SOAPExternalKeySearch(array(new SOAPSearchCondition('id', 1))), /* aServiceSubcategoryDesc */
  1604. 'sub product of the service', /* sProduct */
  1605. new SOAPExternalKeySearch(array(new SOAPSearchCondition('name', 'Hardware support'))), /* aWorkgroupDesc */
  1606. array(
  1607. new SOAPLinkCreationSpec(
  1608. 'InfrastructureCI',
  1609. array(new SOAPSearchCondition('dummyfiltercode', 2)),
  1610. array(new SOAPAttributeValue('impact', 'very critical'))
  1611. ),
  1612. ), /* aImpact */
  1613. '1', /* sImpact */
  1614. '1', /* sUrgency */
  1615. ),
  1616. ),
  1617. array(
  1618. 'service_category' => '',
  1619. 'verb' => 'CreateIncidentTicket',
  1620. 'expected result' => true,
  1621. 'explain result' => 'no CI to attach (empty array)',
  1622. 'args' => array(
  1623. 'admin', /* sLogin */
  1624. 'admin', /* sPassword */
  1625. 'Houston not reachable', /* sDescription */
  1626. 'Tried to join the shuttle', /* sInitialSituation */
  1627. new SOAPExternalKeySearch(array(new SOAPSearchCondition('id', 1))), /* aCallerDesc */
  1628. new SOAPExternalKeySearch(array(new SOAPSearchCondition('name', 'Demo'))), /* aCustomerDesc */
  1629. new SOAPExternalKeySearch(array(new SOAPSearchCondition('name', 'Computers and peripherals'))), /* aServiceDesc */
  1630. new SOAPExternalKeySearch(array(new SOAPSearchCondition('id', 1))), /* aServiceSubcategoryDesc */
  1631. 'sub product of the service', /* sProduct */
  1632. new SOAPExternalKeySearch(array(new SOAPSearchCondition('name', 'Hardware support'))), /* aWorkgroupDesc */
  1633. array(
  1634. ), /* aImpact */
  1635. '1', /* sImpact */
  1636. '1', /* sUrgency */
  1637. ),
  1638. ),
  1639. array(
  1640. 'service_category' => '',
  1641. 'verb' => 'CreateIncidentTicket',
  1642. 'expected result' => true,
  1643. 'explain result' => 'no CI to attach (null)',
  1644. 'args' => array(
  1645. 'admin', /* sLogin */
  1646. 'admin', /* sPassword */
  1647. 'Houston not reachable', /* sDescription */
  1648. 'Tried to join the shuttle', /* sInitialSituation */
  1649. new SOAPExternalKeySearch(array(new SOAPSearchCondition('id', 1))), /* aCallerDesc */
  1650. new SOAPExternalKeySearch(array(new SOAPSearchCondition('name', 'Demo'))), /* aCustomerDesc */
  1651. new SOAPExternalKeySearch(array(new SOAPSearchCondition('name', 'Computers and peripherals'))), /* aServiceDesc */
  1652. new SOAPExternalKeySearch(array(new SOAPSearchCondition('id', 1))), /* aServiceSubcategoryDesc */
  1653. 'sub product of the service', /* sProduct */
  1654. new SOAPExternalKeySearch(array(new SOAPSearchCondition('name', 'Hardware support'))), /* aWorkgroupDesc */
  1655. null, /* aImpact */
  1656. '1', /* sImpact */
  1657. '1', /* sUrgency */
  1658. ),
  1659. ),
  1660. array(
  1661. 'service_category' => '',
  1662. 'verb' => 'CreateIncidentTicket',
  1663. 'expected result' => true,
  1664. 'explain result' => 'caller unknown',
  1665. 'args' => array(
  1666. 'admin', /* sLogin */
  1667. 'admin', /* sPassword */
  1668. 'Houston not reachable', /* sDescription */
  1669. 'Tried to join the shuttle', /* sInitialSituation */
  1670. new SOAPExternalKeySearch(array(new SOAPSearchCondition('id', 1000))), /* aCallerDesc */
  1671. new SOAPExternalKeySearch(array(new SOAPSearchCondition('name', 'Demo'))), /* aCustomerDesc */
  1672. new SOAPExternalKeySearch(array(new SOAPSearchCondition('name', 'Computers and peripherals'))), /* aServiceDesc */
  1673. new SOAPExternalKeySearch(array(new SOAPSearchCondition('id', 1))), /* aServiceSubcategoryDesc */
  1674. 'sub product of the service', /* sProduct */
  1675. new SOAPExternalKeySearch(array(new SOAPSearchCondition('name', 'Hardware support'))), /* aWorkgroupDesc */
  1676. array(
  1677. ), /* aImpact */
  1678. '1', /* sImpact */
  1679. '1', /* sUrgency */
  1680. ),
  1681. ),
  1682. array(
  1683. 'service_category' => '',
  1684. 'verb' => 'CreateIncidentTicket',
  1685. 'expected result' => false,
  1686. 'explain result' => 'wrong values for impact and urgency',
  1687. 'args' => array(
  1688. 'admin', /* sLogin */
  1689. 'admin', /* sPassword */
  1690. 'Houston not reachable', /* sDescription */
  1691. 'Tried to join the shuttle', /* sInitialSituation */
  1692. new SOAPExternalKeySearch(array(new SOAPSearchCondition('id', 1))), /* aCallerDesc */
  1693. new SOAPExternalKeySearch(array(new SOAPSearchCondition('name', 'Demo'))), /* aCustomerDesc */
  1694. new SOAPExternalKeySearch(array(new SOAPSearchCondition('name', 'Computers and peripherals'))), /* aServiceDesc */
  1695. new SOAPExternalKeySearch(array(new SOAPSearchCondition('id', 1))), /* aServiceSubcategoryDesc */
  1696. 'sub product of the service', /* sProduct */
  1697. new SOAPExternalKeySearch(array(new SOAPSearchCondition('name', 'Hardware support'))), /* aWorkgroupDesc */
  1698. array(
  1699. ), /* aImpact */
  1700. '6', /* sImpact */
  1701. '7', /* sUrgency */
  1702. ),
  1703. ),
  1704. array(
  1705. 'service_category' => '',
  1706. 'verb' => 'CreateIncidentTicket',
  1707. 'expected result' => false,
  1708. 'explain result' => 'wrong password',
  1709. 'args' => array(
  1710. 'admin', /* sLogin */
  1711. 'xxxxx', /* sPassword */
  1712. 'Houston not reachable', /* sDescription */
  1713. 'Tried to join the shuttle', /* sInitialSituation */
  1714. new SOAPExternalKeySearch(array(new SOAPSearchCondition('id', 1))), /* aCallerDesc */
  1715. new SOAPExternalKeySearch(array(new SOAPSearchCondition('name', 'Demo'))), /* aCustomerDesc */
  1716. new SOAPExternalKeySearch(array(new SOAPSearchCondition('name', 'Computers and peripherals'))), /* aServiceDesc */
  1717. new SOAPExternalKeySearch(array(new SOAPSearchCondition('id', 1))), /* aServiceSubcategoryDesc */
  1718. 'sub product of the service', /* sProduct */
  1719. new SOAPExternalKeySearch(array(new SOAPSearchCondition('name', 'Hardware support'))), /* aWorkgroupDesc */
  1720. array(
  1721. ), /* aImpact */
  1722. '1', /* sImpact */
  1723. '1', /* sUrgency */
  1724. ),
  1725. ),
  1726. array(
  1727. 'service_category' => '',
  1728. 'verb' => 'CreateIncidentTicket',
  1729. 'expected result' => false,
  1730. 'explain result' => 'wrong login',
  1731. 'args' => array(
  1732. 'xxxxx', /* sLogin */
  1733. 'yyyyy', /* sPassword */
  1734. 'Houston not reachable', /* sDescription */
  1735. 'Tried to join the shuttle', /* sInitialSituation */
  1736. new SOAPExternalKeySearch(array(new SOAPSearchCondition('id', 1))), /* aCallerDesc */
  1737. new SOAPExternalKeySearch(array(new SOAPSearchCondition('name', 'Demo'))), /* aCustomerDesc */
  1738. new SOAPExternalKeySearch(array(new SOAPSearchCondition('name', 'Computers and peripherals'))), /* aServiceDesc */
  1739. new SOAPExternalKeySearch(array(new SOAPSearchCondition('id', 1))), /* aServiceSubcategoryDesc */
  1740. 'sub product of the service', /* sProduct */
  1741. new SOAPExternalKeySearch(array(new SOAPSearchCondition('name', 'Hardware support'))), /* aWorkgroupDesc */
  1742. array(
  1743. ), /* aImpact */
  1744. '1', /* sImpact */
  1745. '1', /* sUrgency */
  1746. ),
  1747. ),
  1748. );
  1749. $aManageCloudUsersSpecs = array(
  1750. array(
  1751. 'service_category' => '',
  1752. 'verb' => 'SearchObjects',
  1753. 'expected result' => false,
  1754. 'explain result' => 'wrong OQL',
  1755. 'args' => array(
  1756. 'admin', /* sLogin */
  1757. 'admin', /* sPassword */
  1758. 'SELECT ThisClassDoesNotExist', /* sOQL */
  1759. ),
  1760. ),
  1761. array(
  1762. 'service_category' => '',
  1763. 'verb' => 'SearchObjects',
  1764. 'expected result' => true,
  1765. 'explain result' => 'ok',
  1766. 'args' => array(
  1767. 'admin', /* sLogin */
  1768. 'admin', /* sPassword */
  1769. 'SELECT Organization', /* sOQL */
  1770. ),
  1771. ),
  1772. array(
  1773. 'service_category' => 'CloudUsersManagementService',
  1774. 'verb' => 'CreateAccount',
  1775. 'expected result' => true,
  1776. 'explain result' => 'ok',
  1777. 'args' => array(
  1778. 'admin', /* sAdminLogin */
  1779. 'admin', /* sAdminPassword */
  1780. 'http://myserver.mydomain.fr:8080', /* sCloudMgrUrl */
  1781. 'andros@combodo.com', /* sLogin */
  1782. 'André', /* sFirstName */
  1783. 'Dupont', /* sLastName */
  1784. 1, /* iOrgId */
  1785. 'FR FR', /* sLanguage */
  1786. array(
  1787. array(
  1788. new SOAPKeyValue('profile_id', '2'),
  1789. new SOAPKeyValue('reason', 'whynot'),
  1790. ),
  1791. array(
  1792. new SOAPKeyValue('profile_id', '3'),
  1793. new SOAPKeyValue('reason', 'because'),
  1794. ),
  1795. ), /* aProfiles (array of key/value pairs) */
  1796. array(
  1797. ), /* aAllowedOrgs (array of key/value pairs) */
  1798. 'comment on the creation operation', /* sComment */
  1799. ),
  1800. ),
  1801. array(
  1802. 'service_category' => 'CloudUsersManagementService',
  1803. 'verb' => 'ModifyAccount',
  1804. 'expected result' => true,
  1805. 'explain result' => 'ok',
  1806. 'args' => array(
  1807. 'admin', /* sAdminLogin */
  1808. 'admin', /* sAdminPassword */
  1809. 'andros@combodo.com', /* sLogin */
  1810. 'nono', /* sFirstName */
  1811. 'robot', /* sLastName */
  1812. 2, /* iOrgId */
  1813. 'EN US', /* sLanguage */
  1814. array(
  1815. array(
  1816. new SOAPKeyValue('profile_id', '3'),
  1817. new SOAPKeyValue('reason', 'because'),
  1818. ),
  1819. ), /* aProfiles (array of key/value pairs) */
  1820. array(
  1821. ), /* aAllowedOrgs (array of key/value pairs) */
  1822. 'comment on the modify operation', /* sComment */
  1823. ),
  1824. ),
  1825. array(
  1826. 'service_category' => 'CloudUsersManagementService',
  1827. 'verb' => 'DeleteAccount',
  1828. 'expected result' => true,
  1829. 'explain result' => '',
  1830. 'args' => array(
  1831. 'admin', /* sAdminLogin */
  1832. 'admin', /* sAdminPassword */
  1833. 'andros@combodo.com', /* sLogin */
  1834. 'comment on the deletion operation', /* sComment */
  1835. ),
  1836. ),
  1837. array(
  1838. 'service_category' => 'CloudUsersManagementService',
  1839. 'verb' => 'DeleteAccount',
  1840. 'expected result' => false,
  1841. 'explain result' => 'wrong login',
  1842. 'args' => array(
  1843. 'admin', /* sAdminLogin */
  1844. 'admin', /* sAdminPassword */
  1845. 'taratatata@sdf.com', /* sLogin */
  1846. 'comment on the deletion operation', /* sComment */
  1847. ),
  1848. ),
  1849. );
  1850. abstract class TestSoap extends TestSoapWebService
  1851. {
  1852. static public function GetName() {return 'Test SOAP';}
  1853. static public function GetDescription() {return 'Do basic stuff to test the SOAP capability';}
  1854. protected $m_aTestSpecs;
  1855. protected function DoExecute()
  1856. {
  1857. echo "<p>Note: You may also want to try the sample SOAP client <a href=\"../webservices/itopsoap.examples.php\">itopsoap.examples.php</a></p>\n";
  1858. $aSOAPMapping = SOAPMapping::GetMapping();
  1859. // this file is generated dynamically with location = here
  1860. $sWsdlUri = 'http'.(isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS']!='off') ? 's' : '').'://'.$_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].dirname($_SERVER['SCRIPT_NAME']).'/../webservices/itop.wsdl.php';
  1861. ini_set("soap.wsdl_cache_enabled","0");
  1862. foreach ($this->m_aTestSpecs as $iPos => $aWebService)
  1863. {
  1864. echo "<h2>SOAP call #$iPos - {$aWebService['verb']}</h2>\n";
  1865. echo "<p>{$aWebService['explain result']}</p>\n";
  1866. $sWsdlUriForService = $sWsdlUri.'?service_category='.$aWebService['service_category'];
  1867. $this->m_SoapClient = new SoapClient
  1868. (
  1869. $sWsdlUriForService,
  1870. array(
  1871. 'classmap' => $aSOAPMapping,
  1872. 'trace' => 1,
  1873. )
  1874. );
  1875. if (false)
  1876. {
  1877. self::DumpVariable($this->m_SoapClient->__getTypes());
  1878. }
  1879. try
  1880. {
  1881. $oRes = call_user_func_array(array($this->m_SoapClient, $aWebService['verb']), $aWebService['args']);
  1882. }
  1883. catch(SoapFault $e)
  1884. {
  1885. print "<pre>\n";
  1886. print "Request: \n".htmlspecialchars($this->m_SoapClient->__getLastRequest()) ."\n";
  1887. print "Response: \n".htmlspecialchars($this->m_SoapClient->__getLastResponse())."\n";
  1888. print "</pre>";
  1889. print "Response in HTML: <p>".$this->m_SoapClient->__getLastResponse()."</p>";
  1890. throw $e;
  1891. }
  1892. self::DumpVariable($oRes);
  1893. print "<pre>\n";
  1894. print "Request: \n".htmlspecialchars($this->m_SoapClient->__getLastRequest()) ."\n";
  1895. print "Response: \n".htmlspecialchars($this->m_SoapClient->__getLastResponse())."\n";
  1896. print "</pre>";
  1897. if ($oRes instanceof SOAPResult)
  1898. {
  1899. $res = $oRes->status;
  1900. }
  1901. elseif ($oRes instanceof SOAPSimpleResult)
  1902. {
  1903. $res = $oRes->status;
  1904. }
  1905. else
  1906. {
  1907. $res = $oRes;
  1908. }
  1909. if ($res != $aWebService['expected result'])
  1910. {
  1911. echo "Expecting:<br/>\n";
  1912. var_dump($aWebService['expected result']);
  1913. echo "Obtained:<br/>\n";
  1914. var_dump($res);
  1915. throw new UnitTestException("Expecting result '{$aWebService['expected result']}', but got '$res'");
  1916. }
  1917. }
  1918. }
  1919. }
  1920. abstract class TestSoapDirect extends TestBizModel
  1921. {
  1922. static public function GetName() {return 'Test web services locally';}
  1923. static public function GetDescription() {return 'Invoke the service directly (troubleshooting)';}
  1924. static public function GetConfigFile() {return '/config-itop.php';}
  1925. protected $m_aTestSpecs;
  1926. protected function DoExecute()
  1927. {
  1928. foreach ($this->m_aTestSpecs as $iPos => $aWebService)
  1929. {
  1930. $sServiceClass = $aWebService['service_category'];
  1931. if (empty($sServiceClass)) $sServiceClass = 'BasicServices';
  1932. $oWebServices = new $sServiceClass();
  1933. echo "<h2>SOAP call #$iPos - {$aWebService['verb']}</h2>\n";
  1934. echo "<p>{$aWebService['explain result']}</p>\n";
  1935. $oRes = call_user_func_array(array($oWebServices, $aWebService['verb']), $aWebService['args']);
  1936. self::DumpVariable($oRes);
  1937. if ($oRes instanceof SOAPResult)
  1938. {
  1939. $res = $oRes->status;
  1940. }
  1941. elseif ($oRes instanceof SOAPSimpleResult)
  1942. {
  1943. $res = $oRes->status;
  1944. }
  1945. else
  1946. {
  1947. $res = $oRes;
  1948. }
  1949. if ($res != $aWebService['expected result'])
  1950. {
  1951. echo "Expecting:<br/>\n";
  1952. var_dump($aWebService['expected result']);
  1953. echo "Obtained:<br/>\n";
  1954. var_dump($res);
  1955. throw new UnitTestException("Expecting result '{$aWebService['expected result']}', but got '$res'");
  1956. }
  1957. }
  1958. return true;
  1959. }
  1960. }
  1961. class TestSoap_Tickets extends TestSoap
  1962. {
  1963. static public function GetName() {return 'Test SOAP - create ticket';}
  1964. protected function DoExecute()
  1965. {
  1966. global $aCreateTicketSpecs;
  1967. $this->m_aTestSpecs = $aCreateTicketSpecs;
  1968. return parent::DoExecute();
  1969. }
  1970. }
  1971. class TestSoapDirect_Tickets extends TestSoapDirect
  1972. {
  1973. static public function GetName() {return 'Test SOAP without SOAP - create ticket';}
  1974. protected function DoExecute()
  1975. {
  1976. global $aCreateTicketSpecs;
  1977. $this->m_aTestSpecs = $aCreateTicketSpecs;
  1978. return parent::DoExecute();
  1979. }
  1980. }
  1981. class TestSoap_ManageCloudUsers extends TestSoap
  1982. {
  1983. static public function GetName() {return 'Test SOAP - manage Cloud Users';}
  1984. protected function DoExecute()
  1985. {
  1986. global $aManageCloudUsersSpecs;
  1987. $this->m_aTestSpecs = $aManageCloudUsersSpecs;
  1988. return parent::DoExecute();
  1989. }
  1990. }
  1991. class TestSoapDirect_ManageCloudUsers extends TestSoapDirect
  1992. {
  1993. static public function GetName() {return 'Test SOAP without SOAP - manage Cloud Users';}
  1994. protected function DoExecute()
  1995. {
  1996. global $aManageCloudUsersSpecs;
  1997. $this->m_aTestSpecs = $aManageCloudUsersSpecs;
  1998. return parent::DoExecute();
  1999. }
  2000. }
  2001. ////////////////////// End of SOAP TESTS
  2002. class TestTriggerAndEmail extends TestBizModel
  2003. {
  2004. static public function GetName() {return 'Test trigger and email';}
  2005. static public function GetDescription() {return 'Create a trigger and an email, then activates the trigger';}
  2006. static public function GetConfigFile() {return '/config-itop.php';}
  2007. protected function CreateEmailSpec($oTrigger, $sStatus, $sTo, $sCC, $sTesterEmail)
  2008. {
  2009. $oAction = MetaModel::NewObject("ActionEmail");
  2010. $oAction->Set("status", $sStatus);
  2011. $oAction->Set("name", "New server");
  2012. $oAction->Set("test_recipient", $sTesterEmail);
  2013. $oAction->Set("from", $sTesterEmail);
  2014. $oAction->Set("reply_to", $sTesterEmail);
  2015. $oAction->Set("to", $sTo);
  2016. $oAction->Set("cc", $sCC);
  2017. $oAction->Set("bcc", "");
  2018. $oAction->Set("subject", "New server: '\$this->name()$'");
  2019. $oAction->Set("body", "<html><body><p>Dear customer,</p><p>We have created the server \$this->hyperlink()$ in the IT infrastructure database.</p><p>You will be further notified when it is in <strong>Production</strong>.</p><p>The IT infrastructure management team.</p><p>Here are some accentuated characters for french people: 'ééà'</p></body></html>");
  2020. $oAction->Set("importance", "low");
  2021. $iActionId = $this->ObjectToDB($oAction, true);
  2022. $oLink = MetaModel::NewObject("lnkTriggerAction");
  2023. $oLink->Set("trigger_id", $oTrigger->GetKey());
  2024. $oLink->Set("action_id", $iActionId);
  2025. $oLink->Set("order", "1");
  2026. $iLink = $this->ObjectToDB($oLink, true);
  2027. }
  2028. protected function DoExecute()
  2029. {
  2030. $oMyPerson = MetaModel::NewObject("bizPerson");
  2031. $oMyPerson->Set("name", "testemail1");
  2032. $oMyPerson->Set("org_id", "1");
  2033. $oMyPerson->Set("email", "romain.quetiez@hp.com");
  2034. $iPersonId = $this->ObjectToDB($oMyPerson, true);
  2035. $oMyPerson = MetaModel::NewObject("bizPerson");
  2036. $oMyPerson->Set("name", "testemail2");
  2037. $oMyPerson->Set("org_id", "1");
  2038. $oMyPerson->Set("email", "denis.flaven@hp.com");
  2039. $iPersonId = $this->ObjectToDB($oMyPerson, true);
  2040. $oMyPerson = MetaModel::NewObject("bizPerson");
  2041. $oMyPerson->Set("name", "testemail3");
  2042. $oMyPerson->Set("org_id", "1");
  2043. $oMyPerson->Set("email", "erwan.taloc@hp.com");
  2044. $iPersonId = $this->ObjectToDB($oMyPerson, true);
  2045. $oMyServer = MetaModel::NewObject("bizServer");
  2046. $oMyServer->Set("name", "wfr.terminator.com");
  2047. $oMyServer->Set("severity", "low");
  2048. $oMyServer->Set("status", "production");
  2049. $oMyServer->Set("org_id", 2);
  2050. $oMyServer->Set("location_id", 2);
  2051. $iServerId = $this->ObjectToDB($oMyServer, true);
  2052. $oMyTrigger = MetaModel::NewObject("TriggerOnStateEnter");
  2053. $oMyTrigger->Set("description", "Testor");
  2054. $oMyTrigger->Set("target_class", "bizServer");
  2055. $oMyTrigger->Set("state", "Shipped");
  2056. $iTriggerId = $this->ObjectToDB($oMyTrigger, true);
  2057. // Error in OQL field(s)
  2058. //
  2059. $this->CreateEmailSpec
  2060. (
  2061. $oMyTrigger,
  2062. 'test',
  2063. "SELECT bizPerson WHERE naime = 'Dali'",
  2064. "SELECT bizServer",
  2065. 'romain.quetiez@hp.com'
  2066. );
  2067. // Error: no recipient
  2068. //
  2069. $this->CreateEmailSpec
  2070. (
  2071. $oMyTrigger,
  2072. 'test',
  2073. "",
  2074. "",
  2075. 'romain.quetiez@hp.com'
  2076. );
  2077. // Test
  2078. //
  2079. $this->CreateEmailSpec
  2080. (
  2081. $oMyTrigger,
  2082. 'test',
  2083. "SELECT bizPerson WHERE name LIKE 'testemail%'",
  2084. "SELECT bizPerson",
  2085. 'romain.quetiez@hp.com'
  2086. );
  2087. // Test failing because of a wrong test recipient address
  2088. //
  2089. $this->CreateEmailSpec
  2090. (
  2091. $oMyTrigger,
  2092. 'test',
  2093. "SELECT bizPerson WHERE name LIKE 'testemail%'",
  2094. "",
  2095. 'toto@walibi.bg'
  2096. );
  2097. // Normal behavior
  2098. //
  2099. $this->CreateEmailSpec
  2100. (
  2101. $oMyTrigger,
  2102. 'enabled',
  2103. "SELECT bizPerson WHERE name LIKE 'testemail%'",
  2104. "",
  2105. 'romain.quetiez@hp.com'
  2106. );
  2107. // Does nothing, because it is disabled
  2108. //
  2109. $this->CreateEmailSpec
  2110. (
  2111. $oMyTrigger,
  2112. 'disabled',
  2113. "SELECT bizPerson WHERE name = 'testemail%'",
  2114. "",
  2115. 'romain.quetiez@hp.com'
  2116. );
  2117. $oMyTrigger->DoActivate($oMyServer->ToArgs('this'));
  2118. return true;
  2119. }
  2120. }
  2121. class TestDBProperties extends TestBizModel
  2122. {
  2123. static public function GetName()
  2124. {
  2125. return 'Itop - DB Properties';
  2126. }
  2127. static public function GetDescription()
  2128. {
  2129. return 'Write and read a dummy property';
  2130. }
  2131. static public function GetConfigFile() {return '/config-itop.php';}
  2132. protected function DoExecute()
  2133. {
  2134. $sName = 'test';
  2135. DBProperty::SetProperty($sName, 'unix time:'.time(), 'means nothing', 'done with the automated test utility');
  2136. $sValue = DBProperty::GetProperty($sName, 'defaults to this because the table has not been created (1.0.1 install?)');
  2137. echo "<p>Write... then read property <b>$sName</b>, found: '$sValue'</p>\n";
  2138. }
  2139. }
  2140. class TestCreateObjects extends TestBizModel
  2141. {
  2142. static public function GetName()
  2143. {
  2144. return 'Itop - create objects';
  2145. }
  2146. static public function GetDescription()
  2147. {
  2148. return 'Create weird objects (reproduce a bug?)';
  2149. }
  2150. static public function GetConfigFile() {return '/config-itop.php';}
  2151. protected function DoExecute()
  2152. {
  2153. $oMyObj = MetaModel::NewObject("Server");
  2154. $oMyObj->Set("name", "test".rand(1,1000));
  2155. $oMyObj->Set("org_id", 2);
  2156. $oMyObj->Set("status", 'production');
  2157. $this->ObjectToDB($oMyObj, $bReload = true);
  2158. echo "<p>Created: {$oMyObj->GetHyperLink()}</p>";
  2159. $sTicketRef = "I-abcdef";
  2160. echo "<p>Creating: $sTicketRef</p>";
  2161. $oMyObj = MetaModel::NewObject("Incident");
  2162. $oMyObj->Set("ref", $sTicketRef);
  2163. $oMyObj->Set("title", "my title");
  2164. $oMyObj->Set("description", "my description");
  2165. $oMyObj->Set("ticket_log", "my ticket log");
  2166. $oMyObj->Set("start_date", "2010-03-08 17:37:00");
  2167. $oMyObj->Set("status", "resolved");
  2168. $oMyObj->Set("caller_id", 1);
  2169. $oMyObj->Set("org_id", 1);
  2170. $oMyObj->Set("urgency", 3);
  2171. $oMyObj->Set("agent_id", 1);
  2172. $oMyObj->Set("close_date", "0000-00-00 00:00:00");
  2173. $oMyObj->Set("last_update", "2010-04-08 16:47:29");
  2174. $oMyObj->Set("solution", "branche ton pc!");
  2175. // External key given as a string -> should be casted to an integer
  2176. $oMyObj->Set("service_id", "1");
  2177. $oMyObj->Set("servicesubcategory_id", "1");
  2178. $oMyObj->Set("product", "");
  2179. $oMyObj->Set("impact", 2);
  2180. $oMyObj->Set("priority", 3);
  2181. $oMyObj->Set("related_problem_id", 0);
  2182. $oMyObj->Set("related_change_id", 0);
  2183. $oMyObj->Set("assignment_date", "");
  2184. $oMyObj->Set("resolution_date", "");
  2185. $oMyObj->Set("tto_escalation_deadline", "");
  2186. $oMyObj->Set("ttr_escalation_deadline", "");
  2187. $oMyObj->Set("closure_deadline", "");
  2188. $oMyObj->Set("resolution_code", "fixed");
  2189. $oMyObj->Set("user_satisfaction", "");
  2190. $oMyObj->Set("user_commment", "");
  2191. $oMyObj->Set("workgroup_id", 4);
  2192. $this->ObjectToDB($oMyObj, $bReload = true);
  2193. echo "<p>Created: {$oMyObj->GetHyperLink()}</p>";
  2194. }
  2195. }
  2196. ?>