public static function createWorkflowModelTestItem($lastName, $string)
 {
     assert('is_string($lastName)');
     assert('is_string($string)');
     $model = new WorkflowModelTestItem();
     $model->lastName = $lastName;
     $model->string = $string;
     $saved = $model->save();
     assert($saved);
     // Not Coding Standard
     return $model;
 }
 public function setUp()
 {
     parent::setUp();
     $this->super = $this->logoutCurrentUserLoginNewUserAndGetByUsername('super');
     $savedWorkflows = SavedWorkflow::getAll();
     $this->savedWorkflow = $savedWorkflows[0];
     $models = WorkflowModelTestItem::getAll();
     $this->model = $models[0];
 }
 public static function setUpBeforeClass()
 {
     parent::setUpBeforeClass();
     $savedWorkflow = new SavedWorkflow();
     $savedWorkflow->name = 'some workflow';
     $savedWorkflow->description = 'description';
     $savedWorkflow->moduleClassName = 'moduleClassName';
     $savedWorkflow->triggerOn = Workflow::TRIGGER_ON_NEW;
     $savedWorkflow->type = 'some type';
     $savedWorkflow->serializedData = serialize(array('something'));
     $saved = $savedWorkflow->save();
     if (!$saved) {
         throw new FailedToSaveModelException();
     }
     self::$savedWorkflow = $savedWorkflow;
     $model = new WorkflowModelTestItem();
     $model->lastName = 'Green';
     $model->string = 'string';
     $saved = $model->save();
     if (!$saved) {
         throw new FailedToSaveModelException();
     }
     self::$testModel = $model;
 }
 public function testWorkflowMessageInQueueProperlySavesWithoutTrashingRelatedModelItem()
 {
     $model = WorkflowTestHelper::createWorkflowModelTestItem('Jason', 'Green');
     $savedWorkflow = WorkflowTestHelper::createByTimeSavedWorkflow();
     $workflowMessageInQueue = WorkflowTestHelper::createExpiredWorkflowMessageInQueue($model, $savedWorkflow);
     $correctItemId = $workflowMessageInQueue->getClassId('Item');
     $this->assertEquals((int) $correctItemId, (int) $workflowMessageInQueue->getClassId('Item'));
     $this->assertNotEquals((int) $model->getClassId('Item'), (int) $workflowMessageInQueue->getClassId('Item'));
     $modelId = $model->id;
     $queueId = $workflowMessageInQueue->id;
     RedBeanModelsCache::forgetAll(true);
     //simulates page change, required to confirm Item does not get trashed
     $workflowMessageInQueue = WorkflowMessageInQueue::getById($queueId);
     $deleted = $workflowMessageInQueue->delete();
     $this->assertTrue($deleted);
     $model = WorkflowModelTestItem::getById($modelId);
     $this->assertTrue($model->getClassId('Item') > 0);
 }
 public function testEvaluateTimeTriggerBeforeSave()
 {
     $model = new WorkflowModelTestItem();
     $triggerForm = new TriggerForWorkflowForm('WorkflowTestModule', 'WorkflowModelTestItem', Workflow::TYPE_ON_SAVE);
     $triggerForm->valueType = MixedDateTypesSearchFormAttributeMappingRules::TYPE_IS_TIME_FOR;
     $rules = new DateTimeTriggerRules($triggerForm);
     //New model
     $this->assertTrue($rules->evaluateTimeTriggerBeforeSave($model, 'createdDateTime', true));
     $this->assertTrue($rules->evaluateTimeTriggerBeforeSave($model, 'modifiedDateTime', true));
     $this->assertFalse($rules->evaluateTimeTriggerBeforeSave($model, 'dateTime', true));
     $model->lastName = 'test';
     $model->string = 'test';
     $this->assertTrue($model->save());
     $modelId = $model->id;
     $model->forget();
     $model = WorkflowModelTestItem::getById($modelId);
     //Existing model
     $this->assertFalse($rules->evaluateTimeTriggerBeforeSave($model, 'createdDateTime', true));
     $this->assertTrue($rules->evaluateTimeTriggerBeforeSave($model, 'modifiedDateTime', true));
     $this->assertFalse($rules->evaluateTimeTriggerBeforeSave($model, 'dateTime', true));
     //Modify dateTime
     $model->dateTime = '2000-02-02 05:05:05';
     $this->assertTrue($rules->evaluateTimeTriggerBeforeSave($model, 'dateTime', true));
 }
 /**
  * ModifiedDateTime is special because of how it is automatically modified
  * @depends testTriggerBeforeSaveChanges
  */
 public function testTriggerBeforeSaveCreatedAndModifiedDateTimeChanges()
 {
     $workflow = self::makeOnSaveWorkflowAndTriggerForDateOrDateTime('modifiedDateTime', 'Changes', null);
     $model = new WorkflowModelTestItem();
     $model->lastName = 'someLastName';
     $model->string = 'someString';
     //New model
     $this->assertTrue(WorkflowTriggersUtil::areTriggersTrueBeforeSave($workflow, $model));
     $model->save();
     //Existing model
     $model->string = 'someString2';
     $this->assertTrue(WorkflowTriggersUtil::areTriggersTrueBeforeSave($workflow, $model));
     //Fresh existing model
     $modelId = $model->id;
     $model->forget();
     $model = WorkflowModelTestItem::getById($modelId);
     $model->string = 'someString3';
     $this->assertTrue(WorkflowTriggersUtil::areTriggersTrueBeforeSave($workflow, $model));
     $workflow = self::makeOnSaveWorkflowAndTriggerForDateOrDateTime('createdDateTime', 'Changes', null);
     $model = new WorkflowModelTestItem();
     $model->lastName = 'someLastName';
     $model->string = 'someString';
     //Test createdDateTime
     //New model
     $this->assertTrue(WorkflowTriggersUtil::areTriggersTrueBeforeSave($workflow, $model));
     $model->save();
     //Existing model
     $model->string = 'someString2';
     $this->assertFalse(WorkflowTriggersUtil::areTriggersTrueBeforeSave($workflow, $model));
     //Fresh existing model
     $modelId = $model->id;
     $model->forget();
     $model = WorkflowModelTestItem::getById($modelId);
     $model->string = 'someString3';
     $this->assertFalse(WorkflowTriggersUtil::areTriggersTrueBeforeSave($workflow, $model));
     //Test createdDateTime
 }
 /**
  * @depends testTriggerBeforeSaveHasManyNotOwnedEquals
  */
 public function testTriggerBeforeSaveManyManyNotOwnedEquals()
 {
     $workflow = self::makeOnSaveWorkflowAndTriggerWithoutValueType('hasMany1___boolean', 'equals', '1', 'WorkflowsTestModule', 'WorkflowModelTestItem3');
     $model = new WorkflowModelTestItem3();
     $relatedModel = new WorkflowModelTestItem();
     $relatedModel->lastName = 'someLastName';
     $relatedModel->string = 'someString';
     $this->assertTrue($relatedModel->save());
     $model->hasMany1->add($relatedModel);
     $model->hasMany1[0]->boolean = '1';
     $this->assertTrue(WorkflowTriggersUtil::areTriggersTrueBeforeSave($workflow, $model));
     $model->hasMany1[0]->boolean = '0';
     $this->assertFalse(WorkflowTriggersUtil::areTriggersTrueBeforeSave($workflow, $model));
     $model = self::saveAndReloadModel($model);
     $this->assertFalse(WorkflowTriggersUtil::areTriggersTrueBeforeSave($workflow, $model));
     $model->hasMany1[0]->boolean = '1';
     $this->assertTrue(WorkflowTriggersUtil::areTriggersTrueBeforeSave($workflow, $model));
 }
 /**
  * @depends testMakeRecipientsForAHasManyRelation
  */
 public function testMakeRecipientsForAHasOneRelation()
 {
     $form = new DynamicTriggeredModelRelationUserWorkflowEmailMessageRecipientForm('WorkflowModelTestItem', Workflow::TYPE_ON_SAVE);
     $form->relation = 'hasOne';
     $form->dynamicUserType = DynamicTriggeredModelRelationUserWorkflowEmailMessageRecipientForm::DYNAMIC_USER_TYPE_OWNER;
     $model = new WorkflowModelTestItem();
     $model->lastName = 'lastName';
     $model->string = 'string';
     $this->assertTrue($model->save());
     //Test without any related model
     $recipients = $form->makeRecipients($model, self::$sarah);
     $this->assertEquals(0, count($recipients));
     //Test with a related model
     $model2 = new WorkflowModelTestItem2();
     $model2->name = 'model 2';
     $this->assertTrue($model2->save());
     $this->assertTrue($model2->id > 0);
     $model->hasOne = $model2;
     $this->assertTrue($model->save());
     $this->assertTrue($model->hasOne->id > 0);
     $recipients = $form->makeRecipients($model, self::$sarah);
     $this->assertEquals(1, count($recipients));
     $this->assertTrue($recipients[0]->personOrAccount->isSame(self::$super));
 }
 /**
  * @depends testProcessWithTriggeredModelOwnerSender
  */
 public function testInvalidMergeTagsReturnOriginalContent()
 {
     self::$emailTemplate->subject = 'bad subject [[LASTNAME]]';
     self::$emailTemplate->textContent = 'bad text [[LASTNAME]]';
     self::$emailTemplate->htmlContent = 'bad html [[LASTNAME]]';
     $message = new EmailMessageForWorkflowForm('WorkflowModelTestItem', Workflow::TYPE_ON_SAVE);
     $recipients = array(array('type' => WorkflowEmailMessageRecipientForm::TYPE_DYNAMIC_TRIGGERED_MODEL_USER, 'audienceType' => EmailMessageRecipient::TYPE_TO, 'dynamicUserType' => DynamicTriggeredModelUserWorkflowEmailMessageRecipientForm::DYNAMIC_USER_TYPE_CREATED_BY_USER));
     $message->emailTemplateId = self::$emailTemplate->id;
     $message->sendFromType = EmailMessageForWorkflowForm::SEND_FROM_TYPE_CUSTOM;
     $message->sendFromAddress = '*****@*****.**';
     $message->sendFromName = 'Jason';
     $message->setAttributes(array(EmailMessageForWorkflowForm::EMAIL_MESSAGE_RECIPIENTS => $recipients));
     $model = new WorkflowModelTestItem();
     $model->lastName = 'the lastName';
     $model->string = 'the string';
     $model->phone = 'the phone';
     $saved = $model->save();
     $this->assertTrue($saved);
     $helper = new WorkflowEmailMessageProcessingHelper($message, $model, Yii::app()->user->userModel);
     $this->assertEquals(0, Yii::app()->emailHelper->getQueuedCount());
     $this->assertEquals(0, Yii::app()->emailHelper->getSentCount());
     $helper->process();
     $this->assertEquals(1, Yii::app()->emailHelper->getQueuedCount());
     $this->assertEquals(0, Yii::app()->emailHelper->getSentCount());
     $emailMessages = EmailMessage::getAllByFolderType(EmailFolder::TYPE_OUTBOX);
     $this->assertEquals('bad subject [[LASTNAME]]', $emailMessages[0]->subject);
     $this->assertEquals('bad text [[LASTNAME]]', $emailMessages[0]->content->textContent);
     $this->assertEquals('bad html [[LASTNAME]]', $emailMessages[0]->content->htmlContent);
     $this->assertEquals('Jason', $emailMessages[0]->sender->fromName);
     $this->assertEquals('*****@*****.**', $emailMessages[0]->sender->fromAddress);
     $this->assertEquals(1, $emailMessages[0]->recipients->count());
     $this->assertEquals('*****@*****.**', $emailMessages[0]->recipients[0]->toAddress);
     $this->assertEquals(self::$emailTemplate->files[0]->fileContent->content, $emailMessages[0]->files[0]->fileContent->content);
     $emailMessages[0]->delete();
 }
 public function testMakeRecipientsForDynamicTriggeredModelUserManagerOfOwner()
 {
     $form = new DynamicTriggeredModelUserWorkflowEmailMessageRecipientForm('WorkflowModelTestItem', Workflow::TYPE_ON_SAVE);
     $form->dynamicUserType = DynamicTriggeredModelUserWorkflowEmailMessageRecipientForm::DYNAMIC_USER_TYPE_MANAGER_OF_OWNER;
     $model = new WorkflowModelTestItem();
     $model->setScenario('importModel');
     $model->lastName = 'lastName';
     $model->string = 'string';
     $model->createdByUser = User::getById(self::$bobbyUserId);
     $model->modifiedByUser = User::getById(self::$sarahUserId);
     $this->assertTrue($model->save());
     $modelId = $model->id;
     $model->forget();
     $model = WorkflowModelTestItem::getById($modelId);
     $recipients = $form->makeRecipients($model, Yii::app()->user->userModel);
     $this->assertEquals(1, count($recipients));
     $this->assertEquals('superBoss superBossson', $recipients[0]->toName);
     $this->assertEquals('*****@*****.**', $recipients[0]->toAddress);
     $this->assertEquals(self::$superBossUserId, $recipients[0]->personsOrAccounts[0]->id);
 }
 public function testUserResolveValueAndSetToModelUpdateAsDynamicCreatedByUser()
 {
     //Setup a triggered model that has Sarah creating and owning it.
     $super = Yii::app()->user->userModel;
     Yii::app()->user->userModel = User::getByUsername('sarah');
     $triggeredModel = new WorkflowModelTestItem();
     $triggeredModel->lastName = 'test';
     $triggeredModel->string = 'test';
     $saved = $triggeredModel->save();
     $this->assertTrue($saved);
     Yii::app()->user->userModel = $super;
     //Now the super is who modified it
     $triggeredModel->string = 'test2';
     $saved = $triggeredModel->save();
     $this->assertTrue($saved);
     //Test created by user
     $form = new UserWorkflowActionAttributeForm('WorkflowsTestModule', 'WorkflowModelTestItem');
     $form->type = UserWorkflowActionAttributeForm::TYPE_DYNAMIC_CREATED_BY_USER;
     $model = new WorkflowModelTestItem();
     $adapter = new WorkflowActionProcessingModelAdapter($model, Yii::app()->user->userModel, $triggeredModel);
     $this->assertTrue($model->user->id < 0);
     $form->resolveValueAndSetToModel($adapter, 'user');
     $this->assertEquals(self::$sarahUserId, $model->user->id);
     //Test modified by user
     $form = new UserWorkflowActionAttributeForm('WorkflowsTestModule', 'WorkflowModelTestItem');
     $form->type = UserWorkflowActionAttributeForm::TYPE_DYNAMIC_MODIFIED_BY_USER;
     $model = new WorkflowModelTestItem();
     $adapter = new WorkflowActionProcessingModelAdapter($model, User::getByUsername('bobby'), $triggeredModel);
     $this->assertTrue($model->user->id < 0);
     $form->resolveValueAndSetToModel($adapter, 'user');
     $this->assertEquals(self::$superUserId, $model->user->id);
     //Test triggered by user
     $form = new UserWorkflowActionAttributeForm('WorkflowsTestModule', 'WorkflowModelTestItem');
     $form->type = UserWorkflowActionAttributeForm::TYPE_DYNAMIC_TRIGGERED_BY_USER;
     $model = new WorkflowModelTestItem();
     $adapter = new WorkflowActionProcessingModelAdapter($model, User::getByUsername('bobby'), $triggeredModel);
     $this->assertTrue($model->user->id < 0);
     $form->resolveValueAndSetToModel($adapter, 'user');
     $this->assertEquals(self::$bobbyUserId, $model->user->id);
     //Test owner of triggered model
     $form = new UserWorkflowActionAttributeForm('WorkflowsTestModule', 'WorkflowModelTestItem');
     $form->type = UserWorkflowActionAttributeForm::TYPE_DYNAMIC_OWNER_OF_TRIGGERED_MODEL;
     $model = new WorkflowModelTestItem();
     $adapter = new WorkflowActionProcessingModelAdapter($model, User::getByUsername('bobby'), $triggeredModel);
     $this->assertTrue($model->user->id < 0);
     $form->resolveValueAndSetToModel($adapter, 'user');
     $this->assertEquals(self::$sarahUserId, $model->user->id);
 }
 /**
  * @depends testSetAndGet
  */
 public function testGetModelsToProcess($pageSize)
 {
     $this->assertEquals(1, WorkflowMessageInQueue::getCount());
     $models = WorkflowMessageInQueue::getModelsToProcess(10);
     $this->assertEquals(1, count($models));
     //Now have one that is not ready for processing. It should still only get 1
     $model = new WorkflowModelTestItem();
     $model->lastName = 'Green2';
     $model->string = 'string2';
     $saved = $model->save();
     $this->assertTrue($saved);
     $savedWorkflows = SavedWorkflow::getByName('some workflow');
     $workflowMessageInQueue = new WorkflowMessageInQueue();
     $workflowMessageInQueue->modelClassName = get_class($model);
     $workflowMessageInQueue->modelItem = $model;
     $workflowMessageInQueue->processDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime(time() + 86400);
     $workflowMessageInQueue->savedWorkflow = $savedWorkflows[0];
     $workflowMessageInQueue->serializedData = serialize(array('something'));
     $saved = $workflowMessageInQueue->save();
     $this->assertTrue($saved);
     $models = WorkflowMessageInQueue::getModelsToProcess(10);
     $this->assertEquals(1, count($models));
 }
 /**
  * @depends testResolveAfterSaveByModel
  */
 public function testResolveAfterSaveByModelForByTime()
 {
     //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_BY_TIME);
     $workflow->setTriggersStructure('1');
     $workflow->setIsActive(true);
     //Add time trigger
     $trigger = new TimeTriggerForWorkflowForm('WorkflowsTestModule', 'WorkflowModelTestItem', $workflow->getType());
     $trigger->attributeIndexOrDerivedType = 'date';
     $trigger->durationSeconds = '500';
     $trigger->valueType = 'Is Time For';
     $workflow->setTimeTrigger($trigger);
     //Create the saved Workflow
     $savedWorkflow = new SavedWorkflow();
     SavedWorkflowToWorkflowAdapter::resolveWorkflowToSavedWorkflow($workflow, $savedWorkflow);
     $saved = $savedWorkflow->save();
     $this->assertTrue($saved);
     $workflow->setId($savedWorkflow->id);
     //set Id back.
     $model = new WorkflowModelTestItem();
     $model->lastName = 'something';
     $model->string = 'aValue';
     $model->date = '2013-03-03';
     $saved = $model->save();
     $this->assertTrue($saved);
     $model->addWorkflowToProcessAfterSave($workflow);
     $this->assertEquals(0, count(ByTimeWorkflowInQueue::getAll()));
     SavedWorkflowsUtil::resolveAfterSaveByModel($model, Yii::app()->user->userModel);
     $this->assertEquals(1, count(ByTimeWorkflowInQueue::getAll()));
 }
 /**
  * @depends testProcessAfterSaveWhenSendIsInFuture
  */
 public function testProcessAfterSaveWhenSendIsImmediate()
 {
     $this->assertEquals(0, Yii::app()->emailHelper->getQueuedCount());
     $this->assertEquals(0, Yii::app()->emailHelper->getSentCount());
     $emailTemplate = new EmailTemplate();
     $emailTemplate->name = 'the name';
     $emailTemplate->modelClassName = 'Account';
     $emailTemplate->textContent = 'some content';
     $emailTemplate->type = 2;
     $emailTemplate->subject = 'subject';
     $saved = $emailTemplate->save();
     $this->assertTrue($saved);
     $workflow = new Workflow();
     $workflow->setId(self::$savedWorkflow->id);
     $workflow->type = Workflow::TYPE_ON_SAVE;
     $emailMessageForm = new EmailMessageForWorkflowForm('WorkflowModelTestItem', Workflow::TYPE_ON_SAVE);
     $emailMessageForm->sendAfterDurationSeconds = 0;
     $emailMessageForm->emailTemplateId = $emailTemplate->id;
     $emailMessageForm->sendFromType = EmailMessageForWorkflowForm::SEND_FROM_TYPE_DEFAULT;
     $recipients = array(array('type' => WorkflowEmailMessageRecipientForm::TYPE_DYNAMIC_TRIGGERED_BY_USER, 'audienceType' => EmailMessageRecipient::TYPE_TO));
     $emailMessageForm->setAttributes(array(EmailMessageForWorkflowForm::EMAIL_MESSAGE_RECIPIENTS => $recipients));
     $workflow->addEmailMessage($emailMessageForm);
     $model = new WorkflowModelTestItem();
     $model->lastName = 'lastName';
     $model->string = 'string';
     $this->assertTrue($model->save());
     WorkflowEmailMessagesUtil::processAfterSave($workflow, $model, Yii::app()->user->userModel);
     $this->assertEquals(1, Yii::app()->emailHelper->getQueuedCount());
     $this->assertEquals(0, Yii::app()->emailHelper->getSentCount());
 }
 /**
  * @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);
 }