PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/zurmo/zurmo/
PHP | 248 lines | 184 code | 21 blank | 43 comment | 0 complexity | c50cdea4ef6f4480537e5cad17ee98e7 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 WorkflowValidityCheckJobTest extends WorkflowBaseTest
  37. {
  38. public function setUp()
  39. {
  40. parent::setUp();
  41. Yii::app()->user->userModel->primaryEmail->emailAddress = 'super@zurmo.com';
  42. }
  43. public function atestRunWithValidAndInvalidAction()
  44. {
  45. //Create workflow
  46. $workflow = new Workflow();
  47. $workflow->setDescription ('aDescription');
  48. $workflow->setIsActive (true);
  49. $workflow->setOrder (5);
  50. $workflow->setModuleClassName('WorkflowsTest2Module');
  51. $workflow->setName ('myFirstWorkflow');
  52. $workflow->setTriggerOn (Workflow::TRIGGER_ON_NEW_AND_EXISTING);
  53. $workflow->setType (Workflow::TYPE_ON_SAVE);
  54. $workflow->setTriggersStructure('1');
  55. //Add action that is missing required owner
  56. $action = new ActionForWorkflowForm('WorkflowModelTestItem2', Workflow::TYPE_ON_SAVE);
  57. $action->type = ActionForWorkflowForm::TYPE_CREATE;
  58. $action->relation = 'hasMany2';
  59. $attributes = array('string' => array('shouldSetValue' => '1',
  60. 'type' => WorkflowActionAttributeForm::TYPE_STATIC,
  61. 'value' => 'jason'),
  62. 'lastName' => array('shouldSetValue' => '1',
  63. 'type' => WorkflowActionAttributeForm::TYPE_STATIC,
  64. 'value' => 'jason'));
  65. $action->setAttributes(array(ActionForWorkflowForm::ACTION_ATTRIBUTES => $attributes));
  66. $workflow->addAction($action);
  67. //Create the saved Workflow
  68. $savedWorkflow = new SavedWorkflow();
  69. SavedWorkflowToWorkflowAdapter::resolveWorkflowToSavedWorkflow($workflow, $savedWorkflow);
  70. $saved = $savedWorkflow->save();
  71. $this->assertTrue($saved);
  72. $this->assertEquals(0, Notification::getCount());
  73. $this->assertEquals(0, EmailMessage::getCount());
  74. $job = new WorkflowValidityCheckJob();
  75. $this->assertTrue($job->run());
  76. $this->assertEquals(1, Notification::getCount());
  77. $this->assertEquals(1, EmailMessage::getCount());
  78. }
  79. public function testRunWithMissingTriggerPicklistValue()
  80. {
  81. $this->clearNotificationsWorkflowsAndEmailMessages();
  82. $this->createStageValues();
  83. //Create workflow
  84. $workflow = new Workflow();
  85. $workflow->setDescription ('bDescription');
  86. $workflow->setIsActive (true);
  87. $workflow->setOrder (5);
  88. $workflow->setModuleClassName('OpportunitiesModule');
  89. $workflow->setName ('mySecondWorkflow');
  90. $workflow->setTriggerOn (Workflow::TRIGGER_ON_NEW_AND_EXISTING);
  91. $workflow->setType (Workflow::TYPE_ON_SAVE);
  92. $workflow->setTriggersStructure('1');
  93. $trigger = new TriggerForWorkflowForm('OpportunitiesModule', 'Opportunity', Workflow::TYPE_ON_SAVE);
  94. $trigger->attributeIndexOrDerivedType = 'stage';
  95. $trigger->value = 'Closed Won';
  96. $trigger->operator = OperatorRules::TYPE_BECOMES;
  97. $trigger->relationFilter = TriggerForWorkflowForm::RELATION_FILTER_ANY;
  98. $workflow->addTrigger($trigger);
  99. //Create the saved Workflow
  100. $savedWorkflow = new SavedWorkflow();
  101. SavedWorkflowToWorkflowAdapter::resolveWorkflowToSavedWorkflow($workflow, $savedWorkflow);
  102. $saved = $savedWorkflow->save();
  103. $this->assertTrue($saved);
  104. $this->assertEquals(0, Notification::getCount());
  105. $this->assertEquals(0, EmailMessage::getCount());
  106. $job = new WorkflowValidityCheckJob();
  107. $this->assertTrue($job->run());
  108. $this->assertEquals(0, Notification::getCount());
  109. $this->assertEquals(0, EmailMessage::getCount());
  110. $this->clearNotificationsWorkflowsAndEmailMessages();
  111. $workflow = new Workflow();
  112. $workflow->setDescription ('cDescription');
  113. $workflow->setIsActive (true);
  114. $workflow->setOrder (5);
  115. $workflow->setModuleClassName('OpportunitiesModule');
  116. $workflow->setName ('mySecondWorkflow');
  117. $workflow->setTriggerOn (Workflow::TRIGGER_ON_NEW_AND_EXISTING);
  118. $workflow->setType (Workflow::TYPE_ON_SAVE);
  119. $workflow->setTriggersStructure('1');
  120. $trigger = new TriggerForWorkflowForm('OpportunitiesModule', 'Opportunity', Workflow::TYPE_ON_SAVE);
  121. $trigger->attributeIndexOrDerivedType = 'stage';
  122. $trigger->value = 'Unexisting state';
  123. $trigger->operator = OperatorRules::TYPE_BECOMES;
  124. $trigger->relationFilter = TriggerForWorkflowForm::RELATION_FILTER_ANY;
  125. $workflow->addTrigger($trigger);
  126. //Create the saved Workflow
  127. $savedWorkflow = new SavedWorkflow();
  128. SavedWorkflowToWorkflowAdapter::resolveWorkflowToSavedWorkflow($workflow, $savedWorkflow);
  129. $saved = $savedWorkflow->save();
  130. $this->assertTrue($saved);
  131. $this->assertEquals(0, Notification::getCount());
  132. $this->assertEquals(0, EmailMessage::getCount());
  133. $job = new WorkflowValidityCheckJob();
  134. $this->assertTrue($job->run());
  135. $this->assertEquals(1, Notification::getCount());
  136. $this->assertEquals(1, EmailMessage::getCount());
  137. }
  138. public function testRunWithMissingTriggerMultiselectPicklistValue()
  139. {
  140. $this->clearNotificationsWorkflowsAndEmailMessages();
  141. $this->createStageValues();
  142. //Create workflow
  143. $workflow = new Workflow();
  144. $workflow->setDescription ('bDescription');
  145. $workflow->setIsActive (true);
  146. $workflow->setOrder (5);
  147. $workflow->setModuleClassName('OpportunitiesModule');
  148. $workflow->setName ('mySecondWorkflow');
  149. $workflow->setTriggerOn (Workflow::TRIGGER_ON_NEW_AND_EXISTING);
  150. $workflow->setType (Workflow::TYPE_ON_SAVE);
  151. $workflow->setTriggersStructure('1');
  152. $trigger = new TriggerForWorkflowForm('OpportunitiesModule', 'Opportunity', Workflow::TYPE_ON_SAVE);
  153. $trigger->attributeIndexOrDerivedType = 'stage';
  154. $trigger->value = 'Closed Won,Negotiating'; // Not Coding Standard
  155. $trigger->operator = OperatorRules::TYPE_ONE_OF;
  156. $trigger->relationFilter = TriggerForWorkflowForm::RELATION_FILTER_ANY;
  157. $workflow->addTrigger($trigger);
  158. //Create the saved Workflow
  159. $savedWorkflow = new SavedWorkflow();
  160. SavedWorkflowToWorkflowAdapter::resolveWorkflowToSavedWorkflow($workflow, $savedWorkflow);
  161. $saved = $savedWorkflow->save();
  162. $this->assertTrue($saved);
  163. $this->assertEquals(0, Notification::getCount());
  164. $this->assertEquals(0, EmailMessage::getCount());
  165. $job = new WorkflowValidityCheckJob();
  166. $this->assertTrue($job->run());
  167. $this->assertEquals(0, Notification::getCount());
  168. $this->assertEquals(0, EmailMessage::getCount());
  169. $this->clearNotificationsWorkflowsAndEmailMessages();
  170. $workflow = new Workflow();
  171. $workflow->setDescription ('cDescription');
  172. $workflow->setIsActive (true);
  173. $workflow->setOrder (5);
  174. $workflow->setModuleClassName('OpportunitiesModule');
  175. $workflow->setName ('mySecondWorkflow');
  176. $workflow->setTriggerOn (Workflow::TRIGGER_ON_NEW_AND_EXISTING);
  177. $workflow->setType (Workflow::TYPE_ON_SAVE);
  178. $workflow->setTriggersStructure('1');
  179. $trigger = new TriggerForWorkflowForm('OpportunitiesModule', 'Opportunity', Workflow::TYPE_ON_SAVE);
  180. $trigger->attributeIndexOrDerivedType = 'stage';
  181. $trigger->value = 'Closed Won,Unexisting state'; // Not Coding Standard
  182. $trigger->operator = OperatorRules::TYPE_BECOMES;
  183. $trigger->relationFilter = TriggerForWorkflowForm::RELATION_FILTER_ANY;
  184. $workflow->addTrigger($trigger);
  185. //Create the saved Workflow
  186. $savedWorkflow = new SavedWorkflow();
  187. SavedWorkflowToWorkflowAdapter::resolveWorkflowToSavedWorkflow($workflow, $savedWorkflow);
  188. $saved = $savedWorkflow->save();
  189. $this->assertTrue($saved);
  190. $this->assertEquals(0, Notification::getCount());
  191. $this->assertEquals(0, EmailMessage::getCount());
  192. $job = new WorkflowValidityCheckJob();
  193. $this->assertTrue($job->run());
  194. $this->assertEquals(1, Notification::getCount());
  195. $this->assertEquals(1, EmailMessage::getCount());
  196. }
  197. protected function clearNotificationsWorkflowsAndEmailMessages()
  198. {
  199. $notifications = Notification::getAll();
  200. foreach ($notifications as $notification)
  201. {
  202. $notification->delete();
  203. }
  204. $emailMessages = EmailMessage::getAll();
  205. foreach ($emailMessages as $emailMessage)
  206. {
  207. $emailMessage->delete();
  208. }
  209. $workflows = SavedWorkflow::getAll();
  210. foreach ($workflows as $workflow)
  211. {
  212. $workflow->delete();
  213. }
  214. }
  215. protected function createStageValues()
  216. {
  217. $stageValues = array(
  218. 'Prospecting',
  219. 'Negotiating',
  220. 'Closed Won',
  221. );
  222. $stageFieldData = CustomFieldData::getByName('SalesStages');
  223. $stageFieldData->serializedData = serialize($stageValues);
  224. $this->assertTrue($stageFieldData->save());
  225. }
  226. }
  227. ?>