PageRenderTime 48ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/x2engine/protected/tests/X2FlowTestBase.php

https://gitlab.com/e0/X2CRM
PHP | 237 lines | 140 code | 20 blank | 77 comment | 11 complexity | 24d5288ad80d8c766685c1168a7abd9d MD5 | raw file
  1. <?php
  2. /***********************************************************************************
  3. * X2CRM is a customer relationship management program developed by
  4. * X2Engine, Inc. Copyright (C) 2011-2016 X2Engine Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it under
  7. * the terms of the GNU Affero General Public License version 3 as published by the
  8. * Free Software Foundation with the addition of the following permission added
  9. * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
  10. * IN WHICH THE COPYRIGHT IS OWNED BY X2ENGINE, X2ENGINE DISCLAIMS THE WARRANTY
  11. * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
  12. *
  13. * This program is distributed in the hope that it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  15. * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License along with
  19. * this program; if not, see http://www.gnu.org/licenses or write to the Free
  20. * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  21. * 02110-1301 USA.
  22. *
  23. * You can contact X2Engine, Inc. P.O. Box 66752, Scotts Valley,
  24. * California 95067, USA. on our website at www.x2crm.com, or at our
  25. * email address: contact@x2engine.com.
  26. *
  27. * The interactive user interfaces in modified source and object code versions
  28. * of this program must display Appropriate Legal Notices, as required under
  29. * Section 5 of the GNU Affero General Public License version 3.
  30. *
  31. * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
  32. * these Appropriate Legal Notices must retain the display of the "Powered by
  33. * X2Engine" logo. If the display of the logo is not reasonably feasible for
  34. * technical reasons, the Appropriate Legal Notices must display the words
  35. * "Powered by X2Engine".
  36. **********************************************************************************/
  37. Yii::import ('application.modules.contacts.models.*');
  38. Yii::import ('application.modules.workflow.models.*');
  39. Yii::import ('application.components.*');
  40. Yii::import ('application.components.x2flow.*');
  41. Yii::import ('application.components.x2flow.actions.*');
  42. Yii::import ('application.components.x2flow.triggers.*');
  43. Yii::import ('application.components.permissions.*');
  44. /**
  45. * Base class for X2Flow unit tests. These methods should probably be moved to a static utility
  46. * class so that they can be used by non-descendants
  47. * @package application.tests.unit.components.x2flow.triggers
  48. */
  49. class X2FlowTestBase extends X2DbTestCase {
  50. public function assertNoFlowError ($arr) {
  51. if (!$arr[0]) {
  52. X2_TEST_DEBUG_LEVEL > 1 && print_r ($arr[1]);
  53. }
  54. $this->assertTrue ($arr[0]);
  55. return $arr;
  56. }
  57. public function assertFlowError ($arr) {
  58. $this->assertFalse ($arr[0]);
  59. return $arr;
  60. }
  61. /**
  62. * Clears all trigger logs
  63. * @deprecated Use X2FlowTestingAuxLib::clearLogs instead
  64. */
  65. public function clearLogs () {
  66. return X2FlowTestingAuxLib::clearLogs ();
  67. }
  68. /**
  69. * Returns trace of log for specified flow
  70. * @return null|array
  71. * @deprecated Use X2FlowTestingAuxLib::getTraceByFlowId instead
  72. */
  73. public function getTraceByFlowId ($flowId) {
  74. return X2FlowTestingAuxLib::getTraceByFlowId ($flowId);
  75. }
  76. /**
  77. * Decodes flow from flow fixture record.
  78. * @param X2FlowTestBase $context
  79. * @param string $rowAlias The row within the fixture to get
  80. * @param string $fixtureName Name of the fixture from which to get data
  81. * @return array decoded flow JSON string
  82. */
  83. public function getFlow ($context,$rowAlias=null,$fixtureName = 'x2flow') {
  84. if (!$rowAlias) {
  85. $aliases = array_keys ($context->{$fixtureName});
  86. $rowAlias = $aliases[0];
  87. }
  88. return CJSON::decode ($context->{$fixtureName}[$rowAlias]['flow']);
  89. }
  90. /**
  91. * Checks each entry in triggerLog looking for errors
  92. * @param array $trace One of the return value of executeFlow ()
  93. * @return bool false if an error was found in the log, true otherwise
  94. * @deprecated Use X2FlowTestingAuxLib::checkTrace instead
  95. */
  96. public function checkTrace ($trace) {
  97. return X2FlowTestingAuxLib::checkTrace ($trace);
  98. }
  99. private function _flattenTrace ($trace) {
  100. $flattenedTrace = array ();
  101. while (true) {
  102. $complete = true;
  103. foreach ($trace as $action) {
  104. if ($action[0] === 'X2FlowSwitch') {
  105. array_push ($flattenedTrace, array (
  106. 'action' => $action[0],
  107. 'branch' => $action[1],
  108. ));
  109. $trace = $action[2];
  110. $complete = false;
  111. break;
  112. } elseif ($action[0] === 'X2FlowSplitter') {
  113. array_push ($flattenedTrace, array (
  114. 'action' => $action[0],
  115. 'branch' => $action[1],
  116. ));
  117. $flattenedTrace = array_merge (
  118. $flattenedTrace, $this->_flattenTrace ($action[2]));
  119. } else {
  120. array_push ($flattenedTrace, array (
  121. 'action' => $action[0],
  122. 'error' => !$action[1][0],
  123. 'message' => $action[1][1],
  124. ));
  125. }
  126. }
  127. if ($complete) break;
  128. }
  129. return $flattenedTrace;
  130. }
  131. /**
  132. * Flattens the X2Flow trace, making it much easier to read programmatically.
  133. * @param array $trace One of the return value of executeFlow ()
  134. * @return array flattened trace
  135. */
  136. public function flattenTrace ($trace) {
  137. $that = $this;
  138. if (!is_array ($trace[0])) $trace = array ($trace);
  139. $flattened = array ();
  140. foreach ($trace as $segment) {
  141. $flattened = array_merge (
  142. $flattened,
  143. !$segment[0] ? false : array_merge (
  144. array (
  145. array (
  146. 'action' => 'start',
  147. 'error' => !$segment[0],
  148. ),
  149. ),
  150. $this->_flattenTrace ($segment[1])
  151. )
  152. );
  153. }
  154. return $flattened;
  155. }
  156. /**
  157. * Returns array of decoded flows from fixture records
  158. * @param X2FlowTestBase $context A test case for which to obtain data
  159. * @param string $fixtureName The name of the fixture to pull from
  160. * @return <array of arrays> decoded flow JSON strings
  161. */
  162. public function getFlows ($context,$fixtureName = 'x2flow') {
  163. return array_map (function ($a) {
  164. return CJSON::decode ($a['flow']);
  165. }, $context->{$fixtureName});
  166. }
  167. /**
  168. * Executes a specified flow, ensuring that flows won't get triggered recursively
  169. * @param object $flow An X2Flow model
  170. */
  171. public function executeFlow ($flow, $params) {
  172. $X2Flow = new ReflectionClass ('X2Flow');
  173. $_triggerDepth = $X2Flow->getProperty ('_triggerDepth');
  174. $_triggerDepth->setAccessible (TRUE);
  175. $_triggerDepth->setValue (1);
  176. $fn = TestingAuxLib::setPublic (
  177. 'X2Flow', '_executeFlow', false, function ($method, $class) {
  178. return function () use ($method, $class) {
  179. $args = func_get_args ();
  180. $args = array (
  181. &$args[0],
  182. &$args[1],
  183. );
  184. return $method->invokeArgs ($class, $args);
  185. };
  186. });
  187. $returnVal = $fn ($flow, $params);
  188. $_triggerDepth->setValue (0);
  189. return $returnVal;
  190. }
  191. public function assertGetInstances ($context, $subClass,$ignoreClassFiles) {
  192. $items = call_user_func("X2Flow{$subClass}::get{$subClass}Instances");
  193. $allFiles = scandir(
  194. $actionsPath =
  195. Yii::getPathOfAlias('application.components.x2flow.'.strtolower($subClass).'s'));
  196. $classFiles = array();
  197. foreach($allFiles as $file) {
  198. $classPath = $actionsPath.DIRECTORY_SEPARATOR.$file;
  199. if(preg_match ("/\.php$/", $file) && is_file($classPath) && !is_dir($classPath)) {
  200. $classFiles[] = substr($file,0,-4);
  201. }
  202. }
  203. $classesLoaded = array();
  204. foreach($items as $itemObject) {
  205. $classesLoaded[] = get_class($itemObject);
  206. }
  207. $classesNotLoaded = array_diff($classFiles,$classesLoaded);
  208. $classesShouldBeLoaded = array_diff($classesNotLoaded,$ignoreClassFiles);
  209. $context->assertEquals(
  210. array(),$classesShouldBeLoaded,'Some classes were not instantiated.');
  211. }
  212. public static function tearDownAfterClass() {
  213. X2FlowTestingAuxLib::clearLogs();
  214. parent::tearDownAfterClass();
  215. }
  216. }
  217. ?>