PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/app/protected/modules/workflows/tests/unit/WorkflowActionsUtilTest.php

https://bitbucket.org/zurmo/zurmo/
PHP | 247 lines | 174 code | 9 blank | 64 comment | 0 complexity | 4540c335a4dc299ca8c0bc16956934fd MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause, GPL-2.0, LGPL-3.0, LGPL-2.1, BSD-2-Clause
  1. <?php
  2. /*********************************************************************************
  3. * Zurmo is a customer relationship management program developed by
  4. * Zurmo, Inc. Copyright (C) 2015 Zurmo Inc.
  5. *
  6. * Zurmo 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 ZURMO, ZURMO DISCLAIMS THE WARRANTY
  11. * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
  12. *
  13. * Zurmo 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 Zurmo, Inc. with a mailing address at 27 North Wacker Drive
  24. * Suite 370 Chicago, IL 60606. or at email address contact@zurmo.com.
  25. *
  26. * The interactive user interfaces in original and modified versions
  27. * of this program must display Appropriate Legal Notices, as required under
  28. * Section 5 of the GNU Affero General Public License version 3.
  29. *
  30. * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
  31. * these Appropriate Legal Notices must retain the display of the Zurmo
  32. * logo and Zurmo copyright notice. If the display of the logo is not reasonably
  33. * feasible for technical reasons, the Appropriate Legal Notices must display the words
  34. * "Copyright Zurmo Inc. 2015. All rights reserved".
  35. ********************************************************************************/
  36. class WorkflowActionsUtilTest extends WorkflowBaseTest
  37. {
  38. public static function getDependentTestModelClassNames()
  39. {
  40. return array('WorkflowModelTestItem', 'WorkflowModelTestItem2');
  41. }
  42. public function testGetWorkflowsMissingRequiredActionAttributesWhereActionIsOk()
  43. {
  44. //Create workflow
  45. $workflow = new Workflow();
  46. $workflow->setDescription ('aDescription');
  47. $workflow->setIsActive (true);
  48. $workflow->setOrder (5);
  49. $workflow->setModuleClassName('WorkflowsTest2Module');
  50. $workflow->setName ('myFirstWorkflow');
  51. $workflow->setTriggerOn (Workflow::TRIGGER_ON_NEW_AND_EXISTING);
  52. $workflow->setType (Workflow::TYPE_ON_SAVE);
  53. $workflow->setTriggersStructure('1');
  54. //Add action
  55. $action = new ActionForWorkflowForm('WorkflowModelTestItem2', Workflow::TYPE_ON_SAVE);
  56. $action->type = ActionForWorkflowForm::TYPE_CREATE;
  57. $action->relation = 'hasMany2';
  58. $attributes = array('string' => array('shouldSetValue' => '1',
  59. 'type' => WorkflowActionAttributeForm::TYPE_STATIC,
  60. 'value' => 'jason'),
  61. 'lastName' => array('shouldSetValue' => '1',
  62. 'type' => WorkflowActionAttributeForm::TYPE_STATIC,
  63. 'value' => 'jason'),
  64. 'owner__User' => array('shouldSetValue' => '1',
  65. 'type' => WorkflowActionAttributeForm::TYPE_STATIC,
  66. 'value' => Yii::app()->user->userModel->id));
  67. $action->setAttributes(array(ActionForWorkflowForm::ACTION_ATTRIBUTES => $attributes));
  68. $workflow->addAction($action);
  69. //Create the saved Workflow
  70. $savedWorkflow = new SavedWorkflow();
  71. SavedWorkflowToWorkflowAdapter::resolveWorkflowToSavedWorkflow($workflow, $savedWorkflow);
  72. $saved = $savedWorkflow->save();
  73. $this->assertTrue($saved);
  74. $this->assertEquals(array(), WorkflowActionsUtil::getWorkflowsMissingRequiredActionAttributes());
  75. }
  76. /**
  77. * @depends testGetWorkflowsMissingRequiredActionAttributesWhereActionIsOk
  78. */
  79. public function testGetWorkflowsMissingRequiredActionAttributesWhereActionIsMissingRequiredAttribute()
  80. {
  81. //Create workflow
  82. $workflow = new Workflow();
  83. $workflow->setDescription ('aDescription');
  84. $workflow->setIsActive (true);
  85. $workflow->setOrder (5);
  86. $workflow->setModuleClassName('WorkflowsTest2Module');
  87. $workflow->setName ('myFirstWorkflow');
  88. $workflow->setTriggerOn (Workflow::TRIGGER_ON_NEW_AND_EXISTING);
  89. $workflow->setType (Workflow::TYPE_ON_SAVE);
  90. $workflow->setTriggersStructure('1');
  91. //Add action that is missing required owner
  92. $action = new ActionForWorkflowForm('WorkflowModelTestItem2', Workflow::TYPE_ON_SAVE);
  93. $action->type = ActionForWorkflowForm::TYPE_CREATE;
  94. $action->relation = 'hasMany2';
  95. $attributes = array('string' => array('shouldSetValue' => '1',
  96. 'type' => WorkflowActionAttributeForm::TYPE_STATIC,
  97. 'value' => 'jason'),
  98. 'lastName' => array('shouldSetValue' => '1',
  99. 'type' => WorkflowActionAttributeForm::TYPE_STATIC,
  100. 'value' => 'jason'));
  101. $action->setAttributes(array(ActionForWorkflowForm::ACTION_ATTRIBUTES => $attributes));
  102. $workflow->addAction($action);
  103. //Create the saved Workflow
  104. $savedWorkflow = new SavedWorkflow();
  105. SavedWorkflowToWorkflowAdapter::resolveWorkflowToSavedWorkflow($workflow, $savedWorkflow);
  106. $saved = $savedWorkflow->save();
  107. $workflow = SavedWorkflowToWorkflowAdapter::makeWorkflowBySavedWorkflow($savedWorkflow);
  108. $this->assertTrue($saved);
  109. $this->assertEquals(array($workflow), WorkflowActionsUtil::getWorkflowsMissingRequiredActionAttributes());
  110. }
  111. /**
  112. * @depends testGetWorkflowsMissingRequiredActionAttributesWhereActionIsMissingRequiredAttribute
  113. */
  114. public function testProcessBeforeSave()
  115. {
  116. //Create workflow
  117. $workflow = new Workflow();
  118. $workflow->setDescription ('aDescription');
  119. $workflow->setIsActive (true);
  120. $workflow->setOrder (5);
  121. $workflow->setModuleClassName('WorkflowsTestModule');
  122. $workflow->setName ('myFirstWorkflow');
  123. $workflow->setTriggerOn (Workflow::TRIGGER_ON_NEW_AND_EXISTING);
  124. $workflow->setType (Workflow::TYPE_ON_SAVE);
  125. $workflow->setTriggersStructure('1');
  126. //Add trigger
  127. $trigger = new TriggerForWorkflowForm('WorkflowsTestModule', 'WorkflowModelTestItem', $workflow->getType());
  128. $trigger->attributeIndexOrDerivedType = 'string';
  129. $trigger->value = 'aValue';
  130. $trigger->operator = 'equals';
  131. $workflow->addTrigger($trigger);
  132. //Add action
  133. $action = new ActionForWorkflowForm('WorkflowModelTestItem', Workflow::TYPE_ON_SAVE);
  134. $action->type = ActionForWorkflowForm::TYPE_UPDATE_SELF;
  135. $attributes = array('string' => array('shouldSetValue' => '1',
  136. 'type' => WorkflowActionAttributeForm::TYPE_STATIC,
  137. 'value' => 'jason'));
  138. $action->setAttributes(array(ActionForWorkflowForm::ACTION_ATTRIBUTES => $attributes));
  139. $workflow->addAction($action);
  140. //Create the saved Workflow
  141. $savedWorkflow = new SavedWorkflow();
  142. SavedWorkflowToWorkflowAdapter::resolveWorkflowToSavedWorkflow($workflow, $savedWorkflow);
  143. $saved = $savedWorkflow->save();
  144. $this->assertTrue($saved);
  145. //Confirm that the workflow processes and the attribute gets updated
  146. $model = new WorkflowModelTestItem();
  147. $model->string = 'aValue';
  148. WorkflowActionsUtil::processBeforeSave($workflow, $model, Yii::app()->user->userModel);
  149. $this->assertEquals('jason', $model->string);
  150. $this->assertTrue($model->id < 0);
  151. }
  152. /**
  153. * @depends testProcessBeforeSave
  154. */
  155. public function testProcessAfterSave()
  156. {
  157. //Save everyone group
  158. $group = Group::getByName(Group::EVERYONE_GROUP_NAME);
  159. $this->assertTrue($group->save());
  160. //Create workflow
  161. $workflow = new Workflow();
  162. $workflow->setDescription ('aDescription');
  163. $workflow->setIsActive (true);
  164. $workflow->setOrder (5);
  165. $workflow->setModuleClassName('WorkflowsTestModule');
  166. $workflow->setName ('myFirstWorkflow');
  167. $workflow->setTriggerOn (Workflow::TRIGGER_ON_NEW_AND_EXISTING);
  168. $workflow->setType (Workflow::TYPE_ON_SAVE);
  169. $workflow->setTriggersStructure('1');
  170. $workflow->setIsActive(true);
  171. //Add trigger
  172. $trigger = new TriggerForWorkflowForm('WorkflowsTestModule', 'WorkflowModelTestItem', $workflow->getType());
  173. $trigger->attributeIndexOrDerivedType = 'string';
  174. $trigger->value = 'aValue';
  175. $trigger->operator = 'equals';
  176. $workflow->addTrigger($trigger);
  177. //Add action
  178. $action = new ActionForWorkflowForm('WorkflowModelTestItem', Workflow::TYPE_ON_SAVE);
  179. $action->type = ActionForWorkflowForm::TYPE_CREATE;
  180. $action->relation = 'hasOne';
  181. $attributes = array('name' => array('shouldSetValue' => '1',
  182. 'type' => WorkflowActionAttributeForm::TYPE_STATIC,
  183. 'value' => 'jason'),
  184. 'permissions' => array('shouldSetValue' => '1',
  185. 'type' => ExplicitReadWriteModelPermissionsWorkflowActionAttributeForm::TYPE_DYNAMIC_EVERYONE_GROUP),
  186. );
  187. $action->setAttributes(array(ActionForWorkflowForm::ACTION_ATTRIBUTES => $attributes));
  188. $workflow->addAction($action);
  189. //Create the saved Workflow
  190. $savedWorkflow = new SavedWorkflow();
  191. SavedWorkflowToWorkflowAdapter::resolveWorkflowToSavedWorkflow($workflow, $savedWorkflow);
  192. $saved = $savedWorkflow->save();
  193. $this->assertTrue($saved);
  194. $model = new WorkflowModelTestItem();
  195. $model->string = 'aValue';
  196. $saved = $savedWorkflow->save();
  197. $this->assertTrue($saved);
  198. $this->assertEquals(0, WorkflowModelTestItem2::getCount());
  199. WorkflowActionsUtil::processAfterSave($workflow, $model, Yii::app()->user->userModel);
  200. $workflowModelTestItem2s = WorkflowModelTestItem2::getAll();
  201. $this->assertEquals(1, count($workflowModelTestItem2s));
  202. //Confirm permissions are on the everyone group
  203. $explicitReadWriteModelPermissions = ExplicitReadWriteModelPermissionsUtil::makeBySecurableItem($workflowModelTestItem2s[0]);
  204. $this->assertEquals(1, $explicitReadWriteModelPermissions->getReadWritePermitablesCount());
  205. $readWritePermitables = $explicitReadWriteModelPermissions->getReadWritePermitables();
  206. $everyoneGroup = Group::getByName(Group::EVERYONE_GROUP_NAME);
  207. $this->assertTrue(isset($readWritePermitables[$everyoneGroup->getClassId('Permitable')]));
  208. }
  209. /**
  210. * @depends testProcessAfterSave
  211. */
  212. public function testProcessOnByTimeWorkflowInQueueJob()
  213. {
  214. $model = WorkflowTestHelper::createWorkflowModelTestItem('Green', '514');
  215. $workflow = new Workflow();
  216. $workflow->setDescription ('aDescription');
  217. $workflow->setIsActive (true);
  218. $workflow->setOrder (5);
  219. $workflow->setModuleClassName('WorkflowsTestModule');
  220. $workflow->setName ('myFirstWorkflow');
  221. $workflow->setTriggerOn (Workflow::TRIGGER_ON_NEW_AND_EXISTING);
  222. $workflow->setType (Workflow::TYPE_ON_SAVE);
  223. $workflow->setTriggersStructure('1');
  224. $workflow->setIsActive(true);
  225. //Add action
  226. $action = new ActionForWorkflowForm('WorkflowModelTestItem', Workflow::TYPE_ON_SAVE);
  227. $action->type = ActionForWorkflowForm::TYPE_UPDATE_SELF;
  228. $action->relation = 'hasOne';
  229. $attributes = array('string' => array('shouldSetValue' => '1',
  230. 'type' => WorkflowActionAttributeForm::TYPE_STATIC,
  231. 'value' => 'jason'));
  232. $action->setAttributes(array(ActionForWorkflowForm::ACTION_ATTRIBUTES => $attributes));
  233. $workflow->addAction($action);
  234. WorkflowActionsUtil::processOnByTimeWorkflowInQueueJob($workflow, $model, Yii::app()->user->userModel);
  235. $this->assertEquals('jason', $model->string);
  236. }
  237. }
  238. ?>