public function atestRunWithValidAndInvalidAction() { //Create workflow $workflow = new Workflow(); $workflow->setDescription('aDescription'); $workflow->setIsActive(true); $workflow->setOrder(5); $workflow->setModuleClassName('WorkflowsTest2Module'); $workflow->setName('myFirstWorkflow'); $workflow->setTriggerOn(Workflow::TRIGGER_ON_NEW_AND_EXISTING); $workflow->setType(Workflow::TYPE_ON_SAVE); $workflow->setTriggersStructure('1'); //Add action that is missing required owner $action = new ActionForWorkflowForm('WorkflowModelTestItem2', Workflow::TYPE_ON_SAVE); $action->type = ActionForWorkflowForm::TYPE_CREATE; $action->relation = 'hasMany2'; $attributes = array('string' => array('shouldSetValue' => '1', 'type' => WorkflowActionAttributeForm::TYPE_STATIC, 'value' => 'jason'), 'lastName' => array('shouldSetValue' => '1', 'type' => WorkflowActionAttributeForm::TYPE_STATIC, 'value' => 'jason')); $action->setAttributes(array(ActionForWorkflowForm::ACTION_ATTRIBUTES => $attributes)); $workflow->addAction($action); //Create the saved Workflow $savedWorkflow = new SavedWorkflow(); SavedWorkflowToWorkflowAdapter::resolveWorkflowToSavedWorkflow($workflow, $savedWorkflow); $saved = $savedWorkflow->save(); $this->assertTrue($saved); $this->assertEquals(0, Notification::getCount()); $this->assertEquals(0, EmailMessage::getCount()); $job = new WorkflowValidityCheckJob(); $this->assertTrue($job->run()); $this->assertEquals(1, Notification::getCount()); $this->assertEquals(1, EmailMessage::getCount()); }
public function makeSampleOnTimeWorkflow() { Yii::app()->user->userModel = User::getByUsername('super'); $action = new ActionForWorkflowForm('Contact', Workflow::TYPE_BY_TIME); $action->type = ActionForWorkflowForm::TYPE_CREATE; $action->relation = 'tasks'; $action->relationFilter = ActionForWorkflowForm::RELATION_FILTER_ALL; $attributes = array('name' => array('shouldSetValue' => '1', 'type' => WorkflowActionAttributeForm::TYPE_STATIC, 'value' => 'Follow up with contact'), 'owner__User' => array('shouldSetValue' => '1', 'type' => UserWorkflowActionAttributeForm::TYPE_DYNAMIC_OWNER_OF_TRIGGERED_MODEL, 'value' => null), 'status' => array('shouldSetValue' => '1', 'type' => WorkflowActionAttributeForm::TYPE_STATIC, 'value' => Task::STATUS_NEW), 'permissions' => array('shouldSetValue' => '1', 'type' => ExplicitReadWriteModelPermissionsWorkflowActionAttributeForm::TYPE_DYNAMIC_SAME_AS_TRIGGERED_MODEL, 'value' => null)); $action->setAttributes(array(ActionForWorkflowForm::ACTION_ATTRIBUTES => $attributes)); $timeTrigger = new TimeTriggerForWorkflowForm('ContactsModule', 'Contact', Workflow::TYPE_BY_TIME); $timeTrigger->durationInterval = 1; $timeTrigger->durationType = TimeDurationUtil::DURATION_TYPE_MONTH; $timeTrigger->durationSign = TimeDurationUtil::DURATION_SIGN_POSITIVE; $timeTrigger->attributeIndexOrDerivedType = 'latestActivityDateTime'; $timeTrigger->valueType = MixedDateTypesSearchFormAttributeMappingRules::TYPE_IS_TIME_FOR; $workflow = new Workflow(); $workflow->setDescription('This will create a task for the Contact owner to follow up with a contact if there has been no activity for 1 month'); $workflow->setIsActive(false); $workflow->setOrder(1); $workflow->setModuleClassName('ContactsModule'); $workflow->setName('Contact follow up Task'); $workflow->setTriggerOn(Workflow::TRIGGER_ON_NEW_AND_EXISTING); $workflow->setType(Workflow::TYPE_BY_TIME); $workflow->setTriggersStructure('1'); $workflow->addAction($action); $workflow->setTimeTrigger($timeTrigger); $savedWorkflow = new SavedWorkflow(); SavedWorkflowToWorkflowAdapter::resolveWorkflowToSavedWorkflow($workflow, $savedWorkflow); $savedWorkflow->save(); }
protected function resolveAttributeRowsForLinkToTypes(array $inputPrefixData) { assert('is_array($inputPrefixData)'); $attributeRows = array(self::REQUIRED_ATTRIBUTES_INDEX => array(), self::NON_REQUIRED_ATTRIBUTES_INDEX => array()); foreach ($this->model->resolveAllRequiredActionAttributeFormsAndLabelsAndSort() as $attribute => $actionAttributeForm) { $elementAdapter = new WorkflowActionAttributeToElementAdapter($actionAttributeForm, $this->form, $this->model->type, array_merge($inputPrefixData, array($attribute)), true); $attributeRows[self::REQUIRED_ATTRIBUTES_INDEX][] = $elementAdapter->getContent(); } return $attributeRows; }
protected function processUnsubscribeFromListAction() { $actionAttributes = $this->action->getActionAttributes(); if (count($actionAttributes) > 1 || !isset($actionAttributes['marketingList']) || !$this->triggeredModel instanceof Contact) { throw new NotSupportedException(); } $marketingListId = $actionAttributes['marketingList']->value; $members = MarketingListMember::getByMarketingListIdContactIdAndUnsubscribed($marketingListId, (int) $this->triggeredModel->id, false); if ($members !== false) { $member = $members[0]; $member->unsubscribed = true; if (!$member->unrestrictedSave()) { throw new FailedToSaveModelException(); } } else { try { //Does the marketing list still exist MarketingList::getById((int) $marketingListId); } catch (NotFoundException $e) { $this->logAndNotifyOnMissingMarketingListModel(ActionForWorkflowForm::TYPE_UNSUBSCRIBE_FROM_LIST); } } }
/** * @return string * @throws NotSupportedException */ public function getDisplayLabel() { $typeDataAndLabels = ActionForWorkflowForm::getTypeDataAndLabels(); if ($this->type == self::TYPE_UPDATE_SELF || $this->type == self::TYPE_SUBSCRIBE_TO_LIST || $this->type == self::TYPE_UNSUBSCRIBE_FROM_LIST) { return $typeDataAndLabels[$this->type]; } elseif ($this->type == self::TYPE_UPDATE_RELATED) { $modelClassName = $this->resolveRealModelClassName($this->relation); return $typeDataAndLabels[$this->type] . ' ' . $modelClassName::getModelLabelByTypeAndLanguage('Plural'); } elseif ($this->type == self::TYPE_CREATE) { $modelClassName = $this->resolveRealModelClassName($this->relation); return $typeDataAndLabels[$this->type] . ' ' . $modelClassName::getModelLabelByTypeAndLanguage('Singular'); } elseif ($this->type == self::TYPE_CREATE_RELATED) { $modelClassName = $this->_modelClassName; $relationModelAdapter = ModelRelationsAndAttributesToWorkflowAdapter::make($modelClassName::getModuleClassName(), $modelClassName, $this->_workflowType); $modelClassName = $relationModelAdapter->getRelationModelClassName($this->relation); $relatedModelClassName = $this->getModelClassNameAndResolveForRelations(); $content = $typeDataAndLabels[$this->type] . ' ' . $modelClassName::getModelLabelByTypeAndLanguage('Plural'); $content .= ' ' . $relatedModelClassName::getModelLabelByTypeAndLanguage('Singular'); return $content; } else { throw new NotSupportedException(); } }
/** * @param string $modelClassName * @param array $actionData * @param string $workflowType * @return array */ public static function sanitizeActionData($modelClassName, $actionData, $workflowType) { assert('is_string($modelClassName)'); assert('is_array($actionData)'); assert('is_string($workflowType)'); if (!isset($actionData[ActionForWorkflowForm::ACTION_ATTRIBUTES])) { return $actionData; } $actionForSanitizing = new ActionForWorkflowForm($modelClassName, $workflowType); $actionForSanitizing->setAttributes($actionData); foreach ($actionData[ActionForWorkflowForm::ACTION_ATTRIBUTES] as $attribute => $attributeData) { if (isset($attributeData['value'])) { $type = $actionForSanitizing->getActionAttributesAttributeFormType($attribute); if ($type == 'Date' && $attributeData['type'] == DateWorkflowActionAttributeForm::TYPE_STATIC) { $actionData[ActionForWorkflowForm::ACTION_ATTRIBUTES][$attribute]['value'] = DateTimeUtil::resolveValueForDateDBFormatted($attributeData['value']); } elseif ($type == 'DateTime' && $attributeData['type'] == DateTimeWorkflowActionAttributeForm::TYPE_STATIC) { $actionData[ActionForWorkflowForm::ACTION_ATTRIBUTES][$attribute]['value'] = DateTimeUtil::convertDateTimeLocaleFormattedDisplayToDbFormattedDateTimeWithSecondsAsZero($attributeData['value']); } } } return $actionData; }
public static function renderScriptContentForModuleClassNameChange() { // Begin Not Coding Standard return "\n \$('#" . ActionsForWorkflowWizardView::ACTION_TYPE_NAME . " option[value=\"" . ActionForWorkflowForm::TYPE_SUBSCRIBE_TO_LIST . "\"]').remove();\n \$('#" . ActionsForWorkflowWizardView::ACTION_TYPE_NAME . " option[value=\"" . ActionForWorkflowForm::TYPE_UNSUBSCRIBE_FROM_LIST . "\"]').remove();\n if(\$(this).val() == 'ContactsModule')\n {\n \$('#" . ActionsForWorkflowWizardView::ACTION_TYPE_NAME . "').\n append(\"<option value='" . ActionForWorkflowForm::TYPE_SUBSCRIBE_TO_LIST . "'>" . ActionForWorkflowForm::getLabelForSubscribeToList() . "</option>\");\n \$('#" . ActionsForWorkflowWizardView::ACTION_TYPE_NAME . "').\n append(\"<option value='" . ActionForWorkflowForm::TYPE_UNSUBSCRIBE_FROM_LIST . "'>" . ActionForWorkflowForm::getLabelForUnsubscribeFromList() . "</option>\");\n }\n "; // End Not Coding Standard }
/** * @param array $componentFormsData * @param Workflow $workflow */ protected static function makeActionForWorkflowFormAndPopulateWorkflowFromData($componentFormsData, Workflow $workflow) { assert('is_array($componentFormsData)'); $moduleClassName = $workflow->getModuleClassName(); $rowKey = 0; foreach ($componentFormsData as $componentFormData) { $component = new ActionForWorkflowForm($moduleClassName::getPrimaryModelName(), $workflow->getType(), $rowKey); $component->setAttributes($componentFormData); $workflow->addAction($component); $rowKey++; } }
/** * A test to show that the modifiedByUser works ok as a trigger with 'equals' on a newly created model. * @see testProcessBeforeSaveOnCreatedByUserEquals */ public function testProcessBeforeSaveOnModifiedByUserEquals() { Yii::app()->user->userModel = User::getByUsername('super'); //Create workflow $workflow = new Workflow(); $workflow->setDescription('aDescription'); $workflow->setIsActive(true); $workflow->setOrder(5); $workflow->setModuleClassName('AccountsModule'); $workflow->setName('myFirstWorkflow'); $workflow->setTriggerOn(Workflow::TRIGGER_ON_NEW_AND_EXISTING); $workflow->setType(Workflow::TYPE_ON_SAVE); $workflow->setTriggersStructure('1'); //Add trigger $trigger = new TriggerForWorkflowForm('AccountsTestModule', 'Account', $workflow->getType()); $trigger->attributeIndexOrDerivedType = 'modifiedByUser'; $trigger->value = Yii::app()->user->userModel->id; $trigger->operator = 'equals'; $workflow->addTrigger($trigger); //Add action $action = new ActionForWorkflowForm('Account', Workflow::TYPE_ON_SAVE); $action->type = ActionForWorkflowForm::TYPE_UPDATE_SELF; $attributes = array('name' => array('shouldSetValue' => '1', 'type' => WorkflowActionAttributeForm::TYPE_STATIC, 'value' => 'jason')); $action->setAttributes(array(ActionForWorkflowForm::ACTION_ATTRIBUTES => $attributes)); $workflow->addAction($action); //Create the saved Workflow $savedWorkflow = new SavedWorkflow(); SavedWorkflowToWorkflowAdapter::resolveWorkflowToSavedWorkflow($workflow, $savedWorkflow); $saved = $savedWorkflow->save(); $this->assertTrue($saved); //Confirm that the workflow processes and the attribute gets updated $model = new Account(); $model->name = 'aValue'; $this->assertTrue($model->save()); $this->assertEquals('jason', $model->name); }
/** * @depends testResolveAllRequiredActionAttributeFormsAndLabelsAndSort */ public function testResolveAllNonRequiredActionAttributeFormsAndLabelsAndSort() { $form = new ActionForWorkflowForm('WorkflowModelTestItem2', Workflow::TYPE_ON_SAVE); $form->type = ActionForWorkflowForm::TYPE_UPDATE_RELATED; $form->relation = 'hasMany2'; $data = $form->resolveAllNonRequiredActionAttributeFormsAndLabelsAndSort(); $this->assertEquals(38, count($data)); //Test update a derived related model (this is like account's meetings) $form = new ActionForWorkflowForm('WorkflowModelTestItem', Workflow::TYPE_ON_SAVE); $form->type = ActionForWorkflowForm::TYPE_UPDATE_RELATED; $form->relation = 'model5ViaItem'; $data = $form->resolveAllNonRequiredActionAttributeFormsAndLabelsAndSort(); $this->assertEquals(2, count($data)); //Test update a inferred related model (this is like a meeting's accounts) $form = new ActionForWorkflowForm('WorkflowModelTestItem5', Workflow::TYPE_ON_SAVE); $form->type = ActionForWorkflowForm::TYPE_UPDATE_RELATED; $form->relation = 'WorkflowModelTestItem__workflowItems__Inferred'; $data = $form->resolveAllNonRequiredActionAttributeFormsAndLabelsAndSort(); $this->assertEquals(38, count($data)); //Test create a related, derived related model (this is like account's meetings) $form = new ActionForWorkflowForm('WorkflowModelTestItem2', Workflow::TYPE_ON_SAVE); $form->type = ActionForWorkflowForm::TYPE_CREATE_RELATED; $form->relation = 'hasMany2'; $form->relatedModelRelation = 'model5ViaItem'; $data = $form->resolveAllNonRequiredActionAttributeFormsAndLabelsAndSort(); $this->assertEquals(2, count($data)); //Test create a related, inferred related model (this is like a meeting's accounts) $form = new ActionForWorkflowForm('WorkflowModelTestItem7', Workflow::TYPE_ON_SAVE); $form->type = ActionForWorkflowForm::TYPE_CREATE_RELATED; $form->relation = 'model5'; $form->relatedModelRelation = 'WorkflowModelTestItem__workflowItems__Inferred'; $data = $form->resolveAllNonRequiredActionAttributeFormsAndLabelsAndSort(); $this->assertEquals(38, count($data)); }
/** * @param ActionForWorkflowForm $action * @param RedBeanModel $model * @param User $triggeredByUser * @param RedBeanModel $triggeredModel */ protected static function processActionAttributesForActionAfterSave(ActionForWorkflowForm $action, RedBeanModel $model, User $triggeredByUser, RedBeanModel $triggeredModel) { foreach ($action->getActionAttributes() as $attribute => $actionAttribute) { if (!$actionAttribute->resolveValueBeforeSave() && $actionAttribute->shouldSetValue) { if (null === ($relation = ActionForWorkflowForm::resolveFirstRelationName($attribute))) { $resolvedModel = $model; $resolvedAttribute = ActionForWorkflowForm::resolveRealAttributeName($attribute); } else { $resolvedModel = $model->{$relation}; $resolvedAttribute = ActionForWorkflowForm::resolveRealAttributeName($attribute); } $adapter = new WorkflowActionProcessingModelAdapter($resolvedModel, $triggeredByUser, $triggeredModel); $actionAttribute->resolveValueAndSetToModel($adapter, $resolvedAttribute); } } }
/** * Tests subscribing a contact to a marketing list */ public function testSubscribeToListAction() { Yii::app()->user->userModel = User::getByUsername('super'); //Create marketing list $marketingList = MarketingListTestHelper::createMarketingListByName('testList'); $marketingListId = $marketingList->id; $this->assertEquals(0, $marketingList->marketingListMembers->count()); //Create workflow $workflow = new Workflow(); $workflow->setDescription('aDescription'); $workflow->setIsActive(true); $workflow->setOrder(5); $workflow->setModuleClassName('ContactsModule'); $workflow->setName('myFirstWorkflow'); $workflow->setTriggerOn(Workflow::TRIGGER_ON_NEW_AND_EXISTING); $workflow->setType(Workflow::TYPE_ON_SAVE); $workflow->setTriggersStructure('1'); //Add action $action = new ActionForWorkflowForm('Contact', Workflow::TYPE_ON_SAVE); $action->type = ActionForWorkflowForm::TYPE_SUBSCRIBE_TO_LIST; $attributes = array('marketingList' => array('type' => WorkflowActionAttributeForm::TYPE_STATIC, 'value' => $marketingListId)); $action->setAttributes(array(ActionForWorkflowForm::ACTION_ATTRIBUTES => $attributes)); $workflow->addAction($action); //Create the saved Workflow $savedWorkflow = new SavedWorkflow(); SavedWorkflowToWorkflowAdapter::resolveWorkflowToSavedWorkflow($workflow, $savedWorkflow); $saved = $savedWorkflow->save(); $this->assertTrue($saved); //Confirm that the workflow processes and the contact gets updated $contact = ContactTestHelper::createContactByNameForOwner('jason', Yii::app()->user->userModel); $marketingList->forget(); $marketingList = MarketingList::getById($marketingListId); $this->assertEquals(1, $marketingList->marketingListMembers->count()); $this->assertEquals(0, $marketingList->marketingListMembers[0]->unsubscribed); }
public function testWorkflowDoesLinkRelatedModelWhenPermissionsIsSetToOwner() { $super = $this->logoutCurrentUserLoginNewUserAndGetByUsername('super'); $contactStates = ContactState::getAll(); $this->assertEquals(0, Contact::getCount()); //Create workflow $workflow = new Workflow(); $workflow->setDescription('aDescription'); $workflow->setIsActive(true); $workflow->setOrder(5); $workflow->setModuleClassName('AccountsModule'); $workflow->setName('myFirstWorkflow'); $workflow->setTriggerOn(Workflow::TRIGGER_ON_NEW_AND_EXISTING); $workflow->setType(Workflow::TYPE_ON_SAVE); $workflow->setTriggersStructure('1'); //Add action $action = new ActionForWorkflowForm('Account', Workflow::TYPE_ON_SAVE); $action->type = ActionForWorkflowForm::TYPE_CREATE; $action->relation = 'contacts'; $attributes = array('lastName' => array('shouldSetValue' => '1', 'type' => WorkflowActionAttributeForm::TYPE_STATIC, 'value' => 'smith'), 'firstName' => array('shouldSetValue' => '1', 'type' => WorkflowActionAttributeForm::TYPE_STATIC, 'value' => 'john'), 'owner__User' => array('shouldSetValue' => '1', 'type' => WorkflowActionAttributeForm::TYPE_STATIC, 'value' => Yii::app()->user->userModel->id), 'state' => array('shouldSetValue' => '1', 'type' => WorkflowActionAttributeForm::TYPE_STATIC, 'value' => $contactStates[0]->id)); $action->setAttributes(array(ActionForWorkflowForm::ACTION_ATTRIBUTES => $attributes)); $workflow->addAction($action); //Create the saved Workflow $savedWorkflow = new SavedWorkflow(); SavedWorkflowToWorkflowAdapter::resolveWorkflowToSavedWorkflow($workflow, $savedWorkflow); $saved = $savedWorkflow->save(); $this->assertTrue($saved); $account = new Account(); $account->name = 'myTestAccount'; $account->owner = $super; $account->save(); RedBeanModel::forgetAll(); $contacts = Contact::getAll(); $this->assertCount(1, $contacts); $this->assertEquals('myTestAccount', $contacts[0]->account->name); $this->assertEquals('john smith', strval($account->contacts[0])); $this->assertTrue($account->contacts[0]->id > 0); }
public function testResolveWorkflowToSavedWorkflow() { $workflow = new Workflow(); $workflow->setDescription('aDescription'); $workflow->setIsActive(true); $workflow->setOrder(5); $workflow->setModuleClassName('WorkflowsTestModule'); $workflow->setName('myFirstWorkflow'); $workflow->setTriggerOn(Workflow::TRIGGER_ON_NEW); $workflow->setType(Workflow::TYPE_ON_SAVE); $workflow->setTriggersStructure('1 and 2 or 3 or 4'); $trigger = new TriggerForWorkflowForm('WorkflowsTestModule', 'WorkflowModelTestItem', $workflow->getType()); $trigger->attributeIndexOrDerivedType = 'string'; $trigger->value = 'aValue'; $trigger->operator = 'equals'; $workflow->addTrigger($trigger); $trigger = new TriggerForWorkflowForm('WorkflowsTestModule', 'WorkflowModelTestItem', $workflow->getType()); $trigger->attributeIndexOrDerivedType = 'currencyValue'; $trigger->value = 'aValue'; $trigger->secondValue = 'bValue'; $trigger->operator = 'between'; $trigger->currencyIdForValue = '4'; $workflow->addTrigger($trigger); $trigger = new TriggerForWorkflowForm('WorkflowsTestModule', 'WorkflowModelTestItem', $workflow->getType()); $trigger->attributeIndexOrDerivedType = 'owner__User'; $trigger->value = 'aValue'; $trigger->stringifiedModelForValue = 'someName'; $workflow->addTrigger($trigger); $trigger = new TriggerForWorkflowForm('WorkflowsTestModule', 'WorkflowModelTestItem', $workflow->getType()); $trigger->attributeIndexOrDerivedType = 'createdDateTime'; $trigger->value = 'aValue'; $trigger->secondValue = 'bValue'; $trigger->operator = null; $trigger->currencyIdForValue = null; $trigger->valueType = 'Between'; $workflow->addTrigger($trigger); $trigger = new TimeTriggerForWorkflowForm('WorkflowsTestModule', 'WorkflowModelTestItem', $workflow->getType()); $trigger->attributeIndexOrDerivedType = 'date'; $trigger->durationInterval = 500; $trigger->valueType = 'Is Time For'; $workflow->setTimeTrigger($trigger); $action = new ActionForWorkflowForm('WorkflowModelTestItem', Workflow::TYPE_ON_SAVE); $action->type = ActionForWorkflowForm::TYPE_UPDATE_SELF; $attributes = array('string' => array('shouldSetValue' => '1', 'type' => WorkflowActionAttributeForm::TYPE_STATIC, 'value' => 'jason')); $action->setAttributes(array(ActionForWorkflowForm::ACTION_ATTRIBUTES => $attributes)); $workflow->addAction($action); $message = new EmailMessageForWorkflowForm('WorkflowModelTestItem', Workflow::TYPE_ON_SAVE); $message->sendAfterDurationInterval = 86400; $message->sendAfterDurationType = TimeDurationUtil::DURATION_TYPE_WEEK; $message->emailTemplateId = 5; $message->sendFromType = EmailMessageForWorkflowForm::SEND_FROM_TYPE_DEFAULT; $recipients = array(array('type' => WorkflowEmailMessageRecipientForm::TYPE_DYNAMIC_TRIGGERED_MODEL_USER, 'audienceType' => EmailMessageRecipient::TYPE_TO, 'dynamicUserType' => DynamicTriggeredModelUserWorkflowEmailMessageRecipientForm::DYNAMIC_USER_TYPE_CREATED_BY_USER)); $message->setAttributes(array(EmailMessageForWorkflowForm::EMAIL_MESSAGE_RECIPIENTS => $recipients)); $workflow->addEmailMessage($message); $savedWorkflow = new SavedWorkflow(); $this->assertNull($savedWorkflow->serializedData); SavedWorkflowToWorkflowAdapter::resolveWorkflowToSavedWorkflow($workflow, $savedWorkflow); $this->assertEquals('WorkflowsTestModule', $savedWorkflow->moduleClassName); $this->assertEquals('1', $savedWorkflow->isActive); $this->assertEquals('myFirstWorkflow', $savedWorkflow->name); $this->assertEquals('aDescription', $savedWorkflow->description); $this->assertEquals(5, $savedWorkflow->order); $this->assertEquals(Workflow::TRIGGER_ON_NEW, $savedWorkflow->triggerOn); $this->assertEquals(Workflow::TYPE_ON_SAVE, $savedWorkflow->type); $this->assertEquals('1 and 2 or 3 or 4', $workflow->getTriggersStructure()); $compareData = array('Triggers' => array(array('currencyIdForValue' => null, 'value' => 'aValue', 'secondValue' => null, 'stringifiedModelForValue' => null, 'valueType' => null, 'attributeIndexOrDerivedType' => 'string', 'operator' => 'equals', 'relationFilter' => TriggerForWorkflowForm::RELATION_FILTER_ANY), array('currencyIdForValue' => '4', 'value' => 'aValue', 'secondValue' => 'bValue', 'stringifiedModelForValue' => null, 'valueType' => null, 'attributeIndexOrDerivedType' => 'currencyValue', 'operator' => 'between', 'relationFilter' => TriggerForWorkflowForm::RELATION_FILTER_ANY), array('currencyIdForValue' => null, 'value' => 'aValue', 'secondValue' => null, 'stringifiedModelForValue' => 'someName', 'valueType' => null, 'attributeIndexOrDerivedType' => 'owner__User', 'operator' => null, 'relationFilter' => TriggerForWorkflowForm::RELATION_FILTER_ANY), array('value' => 'aValue', 'secondValue' => 'bValue', 'stringifiedModelForValue' => null, 'valueType' => 'Between', 'attributeIndexOrDerivedType' => 'createdDateTime', 'operator' => null, 'currencyIdForValue' => null, 'relationFilter' => TriggerForWorkflowForm::RELATION_FILTER_ANY))); $compareData['Actions'] = array(array('type' => ActionForWorkflowForm::TYPE_UPDATE_SELF, 'relation' => null, 'relationFilter' => ActionForWorkflowForm::RELATION_FILTER_ALL, 'relatedModelRelation' => null, 'ActionAttributes' => array('string' => array('type' => 'Static', 'value' => 'jason', 'shouldSetValue' => 1)))); $compareData['EmailMessages'] = array(array('emailTemplateId' => 5, 'sendAfterDurationInterval' => 86400, 'sendAfterDurationType' => TimeDurationUtil::DURATION_TYPE_WEEK, 'sendFromType' => 'Default', 'sendFromName' => null, 'sendFromAddress' => null, 'EmailMessageRecipients' => array(array('dynamicUserType' => 'CreatedByUser', 'type' => 'DynamicTriggeredModelUser', 'audienceType' => 1)))); $compareData['TimeTrigger'] = array('durationInterval' => 500, 'durationSign' => TimeDurationUtil::DURATION_SIGN_POSITIVE, 'durationType' => TimeDurationUtil::DURATION_TYPE_DAY, 'currencyIdForValue' => null, 'value' => null, 'secondValue' => null, 'valueType' => 'Is Time For', 'relationFilter' => 'RelationFilterAny', 'attributeIndexOrDerivedType' => 'date', 'operator' => null); $unserializedData = unserialize($savedWorkflow->serializedData); $this->assertEquals($compareData['Triggers'], $unserializedData['Triggers']); $this->assertEquals($compareData['Actions'], $unserializedData['Actions']); $this->assertEquals($compareData['EmailMessages'], $unserializedData['EmailMessages']); $this->assertEquals($compareData['TimeTrigger'], $unserializedData['TimeTrigger']); $this->assertEquals('1 and 2 or 3 or 4', $unserializedData['triggersStructure']); $saved = $savedWorkflow->save(); $this->assertTrue($saved); }
/** * @depends testCreateRelatedHasOnesInferredNonOwned * Similar to a meeting updating its related contacts */ public function testCreateRelatedInferredsHasOneNonOwned() { $action = new ActionForWorkflowForm('WorkflowModelTestItem5', Workflow::TYPE_ON_SAVE); $action->type = ActionForWorkflowForm::TYPE_CREATE_RELATED; $action->relation = 'WorkflowModelTestItem__workflowItems__Inferred'; $action->relatedModelRelation = 'hasOne'; $attributes = array('name' => array('shouldSetValue' => '1', 'type' => WorkflowActionAttributeForm::TYPE_STATIC, 'value' => 'some new model 2')); $action->setAttributes(array(ActionForWorkflowForm::ACTION_ATTRIBUTES => $attributes)); $model = new WorkflowModelTestItem5(); $relatedModel = new WorkflowModelTestItem(); $relatedModel->lastName = 'lastName'; $relatedModel->string = 'string'; $saved = $relatedModel->save(); $this->assertTrue($saved); $model->workflowItems->add($relatedModel); $saved = $model->save(); $this->assertTrue($saved); $this->assertTrue($model->workflowItems[0]->hasOne->id < 0); $helper = new WorkflowActionProcessingHelper(88, 'some name', $action, $model, Yii::app()->user->userModel); $helper->processNonUpdateSelfAction(); $this->assertTrue($model->workflowItems[0]->hasOne->id > 0); $this->assertEquals('some new model 2', $model->workflowItems[0]->hasOne->name); }
/** * @return array */ protected static function resolveTypeDataAndLabels() { $data = array(); return array_merge($data, ActionForWorkflowForm::getTypeDataAndLabels()); }
public function testUnsubscribeContactFromListUsingWorkflowAction() { $this->assertEquals(1, static::$marketingList->marketingListMembers->count()); $this->assertEquals(1, MarketingListMember::getCount()); //Try to unsubscribe the contact, this doesn't do anything useful as contact is already unsubscribed $action = new ActionForWorkflowForm('Contact', Workflow::TYPE_ON_SAVE); $action->type = ActionForWorkflowForm::TYPE_UNSUBSCRIBE_FROM_LIST; $attributes = array('marketingList' => array('shouldSetValue' => '1', 'type' => WorkflowActionAttributeForm::TYPE_STATIC, 'value' => static::$marketingList->id)); $action->setAttributes(array(ActionForWorkflowForm::ACTION_ATTRIBUTES => $attributes)); $helper = new WorkflowActionProcessingHelper(88, 'some name', $action, static::$contact, Yii::app()->user->userModel); $helper->processNonUpdateSelfAction(); $marketingListId = static::$marketingList->id; static::$marketingList->forget(); static::$marketingList = MarketingList::getById($marketingListId); $this->assertEquals(1, static::$marketingList->marketingListMembers->count()); $this->assertEquals(1, static::$marketingList->marketingListMembers[0]->unsubscribed); $this->assertEquals(1, MarketingListMember::getCount()); }
/** * @depends testProcessAfterSave */ public function testProcessOnByTimeWorkflowInQueueJob() { $model = WorkflowTestHelper::createWorkflowModelTestItem('Green', '514'); $workflow = new Workflow(); $workflow->setDescription('aDescription'); $workflow->setIsActive(true); $workflow->setOrder(5); $workflow->setModuleClassName('WorkflowsTestModule'); $workflow->setName('myFirstWorkflow'); $workflow->setTriggerOn(Workflow::TRIGGER_ON_NEW_AND_EXISTING); $workflow->setType(Workflow::TYPE_ON_SAVE); $workflow->setTriggersStructure('1'); $workflow->setIsActive(true); //Add action $action = new ActionForWorkflowForm('WorkflowModelTestItem', Workflow::TYPE_ON_SAVE); $action->type = ActionForWorkflowForm::TYPE_UPDATE_SELF; $action->relation = 'hasOne'; $attributes = array('string' => array('shouldSetValue' => '1', 'type' => WorkflowActionAttributeForm::TYPE_STATIC, 'value' => 'jason')); $action->setAttributes(array(ActionForWorkflowForm::ACTION_ATTRIBUTES => $attributes)); $workflow->addAction($action); WorkflowActionsUtil::processOnByTimeWorkflowInQueueJob($workflow, $model, Yii::app()->user->userModel); $this->assertEquals('jason', $model->string); }
/** * @depends testResolveBeforeSaveByModelForByTime */ public function testResolveAfterSaveByModel() { //Create workflow $workflow = new Workflow(); $workflow->setDescription('aDescription'); $workflow->setIsActive(true); $workflow->setOrder(5); $workflow->setModuleClassName('WorkflowsTestModule'); $workflow->setName('myFirstWorkflow'); $workflow->setTriggerOn(Workflow::TRIGGER_ON_NEW_AND_EXISTING); $workflow->setType(Workflow::TYPE_ON_SAVE); $workflow->setTriggersStructure('1'); $workflow->setIsActive(true); //Add trigger $trigger = new TriggerForWorkflowForm('WorkflowsTestModule', 'WorkflowModelTestItem', $workflow->getType()); $trigger->attributeIndexOrDerivedType = 'string'; $trigger->value = 'aValue'; $trigger->operator = 'equals'; $workflow->addTrigger($trigger); //Add action $action = new ActionForWorkflowForm('WorkflowModelTestItem', Workflow::TYPE_ON_SAVE); $action->type = ActionForWorkflowForm::TYPE_CREATE; $action->relation = 'hasOne'; $attributes = array('name' => array('shouldSetValue' => '1', 'type' => WorkflowActionAttributeForm::TYPE_STATIC, 'value' => 'jason')); $action->setAttributes(array(ActionForWorkflowForm::ACTION_ATTRIBUTES => $attributes)); $workflow->addAction($action); //Create the saved Workflow $savedWorkflow = new SavedWorkflow(); SavedWorkflowToWorkflowAdapter::resolveWorkflowToSavedWorkflow($workflow, $savedWorkflow); $saved = $savedWorkflow->save(); $this->assertTrue($saved); $model = new WorkflowModelTestItem(); $model->string = 'aValue'; $saved = $savedWorkflow->save(); $this->assertTrue($saved); $model->addWorkflowToProcessAfterSave($workflow); $this->assertEquals(0, count(WorkflowModelTestItem2::getAll())); SavedWorkflowsUtil::resolveAfterSaveByModel($model, Yii::app()->user->userModel); $this->assertEquals(1, count(WorkflowModelTestItem2::getAll())); }
public function testUpdateRelatedCatalogItemOnAProductBySellPriceCriteria() { $super = User::getByUsername('super'); $contactStates = ContactState::getAll(); //Create workflow $workflow = new Workflow(); $workflow->setDescription('aDescription'); $workflow->setIsActive(true); $workflow->setOrder(1); $workflow->setModuleClassName('ProductsModule'); $workflow->setName('myFirstProductWorkflow'); $workflow->setTriggerOn(Workflow::TRIGGER_ON_NEW_AND_EXISTING); $workflow->setType(Workflow::TYPE_ON_SAVE); $workflow->setTriggersStructure('1'); //Add Trigger $trigger = new TriggerForWorkflowForm('ProductsModule', 'Product', Workflow::TYPE_ON_SAVE); $trigger->attributeIndexOrDerivedType = 'sellPrice'; $trigger->value = 600; $trigger->operator = 'greaterThanOrEqualTo'; $workflow->addTrigger($trigger); //Add action $currencies = Currency::getAll(); $action = new ActionForWorkflowForm('Product', Workflow::TYPE_ON_SAVE); $action->type = ActionForWorkflowForm::TYPE_UPDATE_RELATED; $action->relation = 'productTemplate'; $attributes = array('description' => array('shouldSetValue' => '1', 'type' => WorkflowActionAttributeForm::TYPE_STATIC, 'value' => 'Set Price'), 'priceFrequency' => array('shouldSetValue' => '1', 'type' => WorkflowActionAttributeForm::TYPE_STATIC, 'value' => 2), 'listPrice' => array('shouldSetValue' => '1', 'type' => WorkflowActionAttributeForm::TYPE_STATIC, 'value' => 800, 'currencyId' => $currencies[0]->id), 'cost' => array('shouldSetValue' => '1', 'type' => WorkflowActionAttributeForm::TYPE_STATIC, 'value' => 700, 'currencyId' => $currencies[0]->id)); $action->setAttributes(array(ActionForWorkflowForm::ACTION_ATTRIBUTES => $attributes)); $workflow->addAction($action); //Create the saved Workflow $savedWorkflow = new SavedWorkflow(); SavedWorkflowToWorkflowAdapter::resolveWorkflowToSavedWorkflow($workflow, $savedWorkflow); $saved = $savedWorkflow->save(); $this->assertTrue($saved); $productTemplate = ProductTemplateTestHelper::createProductTemplateByName('superProductTemplate'); $productTemplates = ProductTemplate::getByName('superProductTemplate'); $product = ProductTestHelper::createProductByNameForOwner('Test Product', $super); $this->assertTrue($product->id > 0); $product->productTemplate = $productTemplates[0]; //Change product sell price $product->sellPrice->value = 650; $this->assertTrue(WorkflowTriggersUtil::areTriggersTrueBeforeSave($workflow, $product)); $saved = $product->save(); $this->assertTrue($saved); $productId = $product->id; $product->forget(); $product = Product::getById($productId); $this->assertEquals('Set Price', $product->productTemplate->description); $this->assertEquals(2, $product->productTemplate->priceFrequency); $this->assertEquals(700, $product->productTemplate->cost->value); $this->assertEquals(800, $product->productTemplate->listPrice->value); }