public function testWorkflowName()
 {
     $this->assertNull($this->workflowItem->getWorkflowName());
     $value = 'example_workflow';
     $this->workflowItem->setWorkflowName($value);
     $this->assertEquals($value, $this->workflowItem->getWorkflowName());
 }
 /**
  * Updates WorkflowItem with WorkflowDefinition
  *
  * @param EntityManager $em
  * @param WorkflowItem $workflowItem
  * @throws WorkflowException
  */
 protected function updateWorkflowDefinition(EntityManager $em, WorkflowItem $workflowItem)
 {
     $workflowDefinition = $em->find('OroWorkflowBundle:WorkflowDefinition', $workflowItem->getWorkflowName());
     if (!$workflowDefinition) {
         throw new WorkflowException(sprintf('Cannot find workflow definition "%s"', $workflowItem->getWorkflowName()));
     }
     $workflowItem->setDefinition($workflowDefinition);
 }
Ejemplo n.º 3
0
 /**
  * @param WorkflowItem $workflowItem
  * @return WorkflowItem
  * @throws WorkflowException
  */
 public function updateAclIdentities(WorkflowItem $workflowItem)
 {
     $workflow = $this->workflowRegistry->getWorkflow($workflowItem->getWorkflowName());
     $definition = $workflowItem->getDefinition();
     $currentStepName = $workflowItem->getCurrentStep()->getName();
     $aclIdentities = array();
     foreach ($definition->getEntityAcls() as $entityAcl) {
         if ($entityAcl->getStep()->getName() == $currentStepName) {
             $attributeName = $entityAcl->getAttribute();
             $attribute = $workflow->getAttributeManager()->getAttribute($attributeName);
             $entity = $workflowItem->getData()->get($attributeName);
             if (!$entity) {
                 continue;
             }
             if (!is_object($entity)) {
                 throw new WorkflowException(sprintf('Value of attribute "%s" must be an object', $attributeName));
             }
             $aclIdentity = new WorkflowEntityAclIdentity();
             $aclIdentity->setAcl($entityAcl)->setEntityClass($attribute->getOption('class'))->setEntityId($this->doctrineHelper->getSingleEntityIdentifier($entity));
             $aclIdentities[] = $aclIdentity;
         }
     }
     $workflowItem->setAclIdentities($aclIdentities);
     return $workflowItem;
 }
 /**
  * Serialize data of WorkflowItem
  *
  * @param WorkflowItem $workflowItem
  * @param UnitOfWork $uow
  */
 protected function serialize(WorkflowItem $workflowItem, UnitOfWork $uow)
 {
     if ($workflowItem->getData()->isModified()) {
         $oldValue = $workflowItem->getSerializedData();
         $this->serializer->setWorkflowName($workflowItem->getWorkflowName());
         $serializedData = $this->serializer->serialize($workflowItem->getData(), $this->format);
         $workflowItem->setSerializedData($serializedData);
         $uow->propertyChanged($workflowItem, 'serializedData', $oldValue, $serializedData);
     }
 }
Ejemplo n.º 5
0
 /**
  * Bind entities to workflow item
  *
  * @param WorkflowItem $workflowItem
  * @return bool Returns true if new entities were bound
  */
 public function bindEntities(WorkflowItem $workflowItem)
 {
     $workflowData = $workflowItem->getData();
     if (!$workflowData->isModified()) {
         return false;
     }
     $workflow = $this->workflowRegistry->getWorkflow($workflowItem->getWorkflowName());
     $bindAttributeNames = $workflow->getAttributeManager()->getBindEntityAttributeNames();
     $entitiesToBind = $workflowData->getValues($bindAttributeNames);
     return $workflowItem->syncBindEntities($this->createBindEntities($entitiesToBind));
 }
 public function testOnFlush()
 {
     $entity1 = new WorkflowItem();
     $entity1->setWorkflowName('workflow_1');
     $entity1->setSerializedData('_old_serialized_data');
     $data1 = new WorkflowData();
     $data1->foo = 'foo';
     $entity1->setData($data1);
     $entity2 = new WorkflowItem();
     $entity2->setWorkflowName('workflow_2');
     $data2 = new WorkflowData();
     $data2->bar = 'bar';
     $entity2->setData($data2);
     $entity3 = new \stdClass();
     $entity4 = new WorkflowItem();
     $entity4->setWorkflowName('workflow_4');
     $data4 = new WorkflowData();
     $data4->foo = 'baz';
     $entity4->setData($data4);
     $entity5 = new WorkflowItem();
     $data5 = new WorkflowData();
     // Leave this data not modified
     $entity5->setData($data5);
     $entity6 = new \stdClass();
     $expectedSerializedData1 = 'serialized_data_1';
     $expectedSerializedData2 = 'serialized_data_2';
     $expectedSerializedData4 = 'serialized_data_4';
     $this->serializer->expects($this->never())->method('deserialize');
     $this->serializer->expects($this->at(0))->method('setWorkflowName')->with($entity1->getWorkflowName());
     $this->serializer->expects($this->at(1))->method('serialize')->with($data1, 'json')->will($this->returnValue($expectedSerializedData1));
     $this->serializer->expects($this->at(2))->method('setWorkflowName')->with($entity2->getWorkflowName());
     $this->serializer->expects($this->at(3))->method('serialize')->with($data2, 'json')->will($this->returnValue($expectedSerializedData2));
     $this->serializer->expects($this->at(4))->method('setWorkflowName')->with($entity4->getWorkflowName());
     $this->serializer->expects($this->at(5))->method('serialize')->with($data4, 'json')->will($this->returnValue($expectedSerializedData4));
     $this->subscriber->onFlush(new OnFlushEventArgs($this->getOnFlushEntityManagerMock(array(array('getScheduledEntityInsertions', array(), $this->returnValue(array($entity1, $entity2, $entity3))), array('propertyChanged', array($entity1, 'serializedData', $entity1->getSerializedData(), $expectedSerializedData1)), array('propertyChanged', array($entity2, 'serializedData', $entity2->getSerializedData(), $expectedSerializedData2)), array('getScheduledEntityUpdates', array(), $this->returnValue(array($entity4, $entity5, $entity6))), array('propertyChanged', array($entity4, 'serializedData', $entity4->getSerializedData(), $expectedSerializedData4))))));
     $this->assertAttributeEquals($expectedSerializedData1, 'serializedData', $entity1);
     $this->assertAttributeEquals($expectedSerializedData2, 'serializedData', $entity2);
     $this->assertAttributeEquals($expectedSerializedData4, 'serializedData', $entity4);
     $this->assertAttributeEquals(null, 'serializedData', $entity5);
 }
Ejemplo n.º 7
0
 /**
  * Checks whether transition is valid in context of workflow item state.
  *
  * Transition is considered invalid when workflow item is new and transition is not "start".
  * Also transition is considered invalid when current step doesn't contain such allowed transition.
  *
  * @param Transition $transition
  * @param WorkflowItem $workflowItem
  * @param bool $fireExceptions
  * @return bool
  * @throws InvalidTransitionException
  */
 protected function checkTransitionValid(Transition $transition, WorkflowItem $workflowItem, $fireExceptions)
 {
     // get current step
     $currentStep = null;
     if ($workflowItem->getCurrentStep() && ($currentStepName = $workflowItem->getCurrentStep()->getName())) {
         $currentStep = $this->stepManager->getStep($currentStepName);
     }
     // if there is no current step - consider transition as a start transition
     if (!$currentStep) {
         if (!$transition->isStart()) {
             if ($fireExceptions) {
                 throw InvalidTransitionException::notStartTransition($workflowItem->getWorkflowName(), $transition->getName());
             }
             return false;
         }
     } elseif (!$currentStep->isAllowedTransition($transition->getName())) {
         // if transition is not allowed for current step
         if ($fireExceptions) {
             throw InvalidTransitionException::stepHasNoAllowedTransition($workflowItem->getWorkflowName(), $currentStep->getName(), $transition->getName());
         }
         return false;
     }
     return true;
 }
 /**
  * Serialize data of WorkflowItem
  *
  * @param WorkflowItem $workflowItem
  */
 protected function serialize(WorkflowItem $workflowItem)
 {
     $this->serializer->setWorkflowName($workflowItem->getWorkflowName());
     // Cloning workflow data instance to prevent changing of original data.
     $workflowData = clone $workflowItem->getData();
     // entity attribute must not be serialized
     $workflowData->remove($workflowItem->getDefinition()->getEntityAttributeName());
     $serializedData = $this->serializer->serialize($workflowData, $this->format);
     $workflowItem->setSerializedData($serializedData);
     $workflowItem->getData()->setModified(false);
 }
 /**
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  */
 public function testOnFlushAndPostFlush()
 {
     $definition = $this->getMockBuilder('Oro\\Bundle\\WorkflowBundle\\Entity\\WorkflowDefinition')->disableOriginalConstructor()->getMock();
     $definition->expects($this->any())->method('getEntityAttributeName')->will($this->returnValue('entity'));
     $entity1 = new WorkflowItem();
     $entity1->setDefinition($definition);
     $entity1->setWorkflowName('workflow_1');
     $entity1->setSerializedData('_old_serialized_data');
     $data1 = new WorkflowData();
     $data1->foo = 'foo';
     $entity1->setData($data1);
     $entity2 = new WorkflowItem();
     $entity2->setDefinition($definition);
     $entity2->setWorkflowName('workflow_2');
     $data2 = new WorkflowData();
     $data2->bar = 'bar';
     $entity2->setData($data2);
     $entity3 = new \stdClass();
     $entity4 = new WorkflowItem();
     $entity4->setDefinition($definition);
     $entity4->setWorkflowName('workflow_4');
     $data4 = new WorkflowData();
     $data4->foo = 'baz';
     $entity4->setData($data4);
     $entity5 = new WorkflowItem();
     $entity5->setDefinition($definition);
     $data5 = new WorkflowData();
     // Leave this data not modified
     $entity5->setData($data5);
     $entity6 = new \stdClass();
     $expectedSerializedData1 = 'serialized_data_1';
     $expectedSerializedData2 = 'serialized_data_2';
     $expectedSerializedData4 = 'serialized_data_4';
     $this->serializer->expects($this->never())->method('deserialize');
     $this->serializer->expects($this->at(0))->method('setWorkflowName')->with($entity1->getWorkflowName());
     $this->serializer->expects($this->at(1))->method('serialize')->with($this->isInstanceOf('Oro\\Bundle\\WorkflowBundle\\Model\\WorkflowData'), 'json')->will($this->returnCallback(function ($data) use($data1, $expectedSerializedData1) {
         $this->assertEquals($data1, $data);
         return $expectedSerializedData1;
     }));
     $this->serializer->expects($this->at(2))->method('setWorkflowName')->with($entity2->getWorkflowName());
     $this->serializer->expects($this->at(3))->method('serialize')->with($this->isInstanceOf('Oro\\Bundle\\WorkflowBundle\\Model\\WorkflowData'), 'json')->will($this->returnCallback(function ($data) use($data2, $expectedSerializedData2) {
         $this->assertEquals($data2, $data);
         return $expectedSerializedData2;
     }));
     $this->serializer->expects($this->at(4))->method('setWorkflowName')->with($entity4->getWorkflowName());
     $this->serializer->expects($this->at(5))->method('serialize')->with($this->isInstanceOf('Oro\\Bundle\\WorkflowBundle\\Model\\WorkflowData'), 'json')->will($this->returnCallback(function ($data) use($data4, $expectedSerializedData4) {
         $this->assertEquals($data4, $data);
         return $expectedSerializedData4;
     }));
     $entityManager = $this->getPostFlushEntityManagerMock(array(array('getScheduledEntityInsertions', array(), $this->returnValue(array($entity1, $entity2, $entity3))), array('getScheduledEntityUpdates', array(), $this->returnValue(array($entity4, $entity5, $entity6)))));
     $this->listener->onFlush(new OnFlushEventArgs($entityManager));
     $this->listener->postFlush(new PostFlushEventArgs($entityManager));
     $this->assertAttributeEquals($expectedSerializedData1, 'serializedData', $entity1);
     $this->assertAttributeEquals($expectedSerializedData2, 'serializedData', $entity2);
     $this->assertAttributeEquals($expectedSerializedData4, 'serializedData', $entity4);
     $this->assertAttributeEquals(null, 'serializedData', $entity5);
     $this->assertFalse($entity1->getData()->isModified());
     $this->assertFalse($entity2->getData()->isModified());
     $this->assertFalse($entity4->getData()->isModified());
     $this->assertFalse($entity5->getData()->isModified());
 }