/** * @expectedException yii\base\InvalidConfigException */ public function testAttachBehaviorFails2() { //$this->specify('the status attribute cannot be empty', function () { $model = new Item01(); expect('model has a ActiveWorkflowBehavior attached', ActiveWorkflowBehavior::isAttachedTo($model))->true(); $model->detachBehavior('workflow'); expect('model has a NO ActiveWorkflowBehavior attached', ActiveWorkflowBehavior::isAttachedTo($model))->false(); $model->attachBehavior('workflow', ['class' => ActiveWorkflowBehavior::className(), 'statusAttribute' => '']); //},['throws' => 'yii\base\InvalidConfigException']); }
/** * Returns an associative array containing all statuses that can be reached by model. * * Note that the current model status is NOT included in this list. * @param Model|ActiveWorkflowBehavior * @param boolean $validate * @param boolean $beforeEvents * @throws WorkflowException * @return array */ public static function getNextStatusListData($model, $validate = false, $beforeEvents = false) { if (!ActiveWorkflowBehavior::isAttachedTo($model)) { throw new WorkflowException('The model does not have a ActiveWorkflowBehavior behavior'); } $listData = []; /** @var ActiveWorkflowBehavior $model */ $report = $model->getNextStatuses($validate, $beforeEvents); foreach ($report as $endStatusId => $info) { if (!isset($info['isValid']) || $info['isValid'] === true) { /** @var Status $sts */ $sts = $info['status']; $listData[$endStatusId] = $sts->getLabel(); } } return $listData; }
/** * Apply active validators for the current workflow event sequence. * * If a workflow event sequence is about to occur, this method scan all validators defined in the * owner model, and applies the ones which are valid for the upcomming events. * @param Model|ActiveWorkflowBehavior $object * @param string $attribute * @throws WorkflowException * * @see Validator::validateAttribute() * @see IEventSequence */ public function validateAttribute($object, $attribute) { if (!ActiveWorkflowBehavior::isAttachedTo($object)) { throw new WorkflowException('Validation error : the model does not have the ActiveWorkflowBehavior'); } try { $scenarioList = $object->getScenarioSequence($object->{$attribute}); } catch (WorkflowException $e) { $object->addError($attribute, 'Workflow validation failed : ' . $e->getMessage()); $scenarioList = []; } if (count($scenarioList) != 0) { foreach ($object->getValidators() as $validator) { foreach ($scenarioList as $scenario) { if ($this->_isActiveValidator($validator, $scenario)) { $validator->validateAttributes($object); } } } } }
/** * @inheritdoc */ public function parseWorkflowStatus($val, $wfId, $model, &$wfDef = null) { if (empty($val) || !is_string($val)) { throw new WorkflowException('Not a valid status id : a non-empty string is expected - status = ' . VarDumper::dumpAsString($val)); } $tokens = array_map('trim', explode(self::SEPARATOR_STATUS_NAME, $val)); $tokenCount = count($tokens); if ($tokenCount == 1) { $tokens[1] = $tokens[0]; $tokens[0] = null; if (isset($wfId) && is_string($wfId)) { $tokens[0] = $wfId; } elseif (isset($model) && ($model instanceof ActiveWorkflowBehavior || ActiveWorkflowBehavior::isAttachedTo($model)) && $model->hasWorkflowStatus()) { $tokens[0] = $model->getWorkflowStatus()->getWorkflowId(); } if ($tokens[0] === null) { throw new WorkflowException('Not a valid status id format: failed to get workflow id / status = ' . VarDumper::dumpAsString($val)); } } elseif ($tokenCount != 2) { throw new WorkflowException('Not a valid status id format: ' . VarDumper::dumpAsString($val)); } elseif (!isset($wfDef) && isset($model) && ($model instanceof ActiveWorkflowBehavior || ActiveWorkflowBehavior::isAttachedTo($model)) && $model->hasWorkflowStatus()) { $wfDef = $this->getWorkflowDefinition($wfId, $model); } if (!$this->isValidWorkflowId($tokens[0])) { throw new WorkflowException('Not a valid status id : incorrect workflow id format in ' . VarDumper::dumpAsString($val)); } elseif (!$this->isValidStatusLocalId($tokens[1])) { throw new WorkflowException('Not a valid status id : incorrect status local id format in ' . VarDumper::dumpAsString($val)); } return $tokens; }