Exemplo n.º 1
0
 public function testGenerateWorkflowFields()
 {
     $entityClass = 'TestEntity';
     $this->entityConnector->expects($this->once())->method('isWorkflowAware')->with($entityClass)->will($this->returnValue(false));
     $extendConfigProvider = $this->getMock('Oro\\Bundle\\EntityConfigBundle\\Provider\\ConfigProviderInterface');
     $entityConfigProvider = $this->getMock('Oro\\Bundle\\EntityConfigBundle\\Provider\\ConfigProviderInterface');
     $formConfigProvider = $this->getMock('Oro\\Bundle\\EntityConfigBundle\\Provider\\ConfigProviderInterface');
     $viewConfigProvider = $this->getMock('Oro\\Bundle\\EntityConfigBundle\\Provider\\ConfigProviderInterface');
     $importExportConfigProvider = $this->getMock('Oro\\Bundle\\EntityConfigBundle\\Provider\\ConfigProviderInterface');
     $providerMap = array(array('extend', $extendConfigProvider), array('entity', $entityConfigProvider), array('form', $formConfigProvider), array('view', $viewConfigProvider), array('importexport', $importExportConfigProvider));
     $this->configManager->expects($this->any())->method('getProvider')->will($this->returnValueMap($providerMap));
     $entityConfig = $this->getMock('Oro\\Bundle\\EntityConfigBundle\\Config\\ConfigInterface');
     $entityConfig->expects($this->at(0))->method('is')->with('is_extend')->will($this->returnValue(true));
     $extendConfigProvider->expects($this->at(0))->method('getConfig')->with($entityClass)->will($this->returnValue($entityConfig));
     $workflowItemClass = 'Oro\\Bundle\\WorkflowBundle\\Entity\\WorkflowItem';
     $workflowStepClass = 'Oro\\Bundle\\WorkflowBundle\\Entity\\WorkflowStep';
     $this->addFieldAssertions($entityConfigProvider, $extendConfigProvider, $formConfigProvider, $viewConfigProvider, $importExportConfigProvider, $entityClass, FieldGenerator::PROPERTY_WORKFLOW_ITEM, ConfigHelper::getTranslationKey('entity', 'label', $workflowItemClass, 'related_entity'), ConfigHelper::getTranslationKey('entity', 'description', $workflowItemClass, 'related_entity'), 'Oro\\Bundle\\WorkflowBundle\\Entity\\WorkflowItem', 'id', 0);
     $this->addFieldAssertions($entityConfigProvider, $extendConfigProvider, $formConfigProvider, $viewConfigProvider, $importExportConfigProvider, $entityClass, FieldGenerator::PROPERTY_WORKFLOW_STEP, ConfigHelper::getTranslationKey('entity', 'label', $workflowStepClass, 'related_entity'), ConfigHelper::getTranslationKey('entity', 'description', $workflowStepClass, 'related_entity'), 'Oro\\Bundle\\WorkflowBundle\\Entity\\WorkflowStep', 'label', 1);
     $entityConfig->expects($this->at(1))->method('set')->with('state', ExtendScope::STATE_UPDATE);
     $entityConfig->expects($this->at(2))->method('set')->with('upgradeable', true);
     $this->configManager->expects($this->at(15))->method('persist')->with($entityConfig);
     $this->configManager->expects($this->at(16))->method('flush');
     $this->entityProcessor->expects($this->once())->method('updateDatabase');
     /*
     $this->addHideAssertions($entityClass, FieldGenerator::PROPERTY_WORKFLOW_ITEM, 0);
     $this->addHideAssertions($entityClass, FieldGenerator::PROPERTY_WORKFLOW_STEP, 1);
     $this->configManager->expects($this->at(17))->method('flush');
     */
     // run test
     $this->generator->generateWorkflowFields($entityClass);
 }
Exemplo n.º 2
0
 /**
  * @param string $sourceEntityClass
  * @param string $targetEntityClass
  * @param string $associationKind
  */
 public function createManyToOneAssociation($sourceEntityClass, $targetEntityClass, $associationKind)
 {
     $relationName = ExtendHelper::buildAssociationName($targetEntityClass, $associationKind);
     $entityConfigProvider = $this->configManager->getProvider('entity');
     $targetEntityConfig = $entityConfigProvider->getConfig($targetEntityClass);
     $label = $targetEntityConfig->get('label', false, ConfigHelper::getTranslationKey('entity', 'label', $targetEntityClass, $relationName));
     $description = ConfigHelper::getTranslationKey('entity', 'description', $targetEntityClass, $relationName);
     $targetEntityPrimaryKeyColumns = $this->getPrimaryKeyColumnNames($targetEntityClass);
     $targetFieldName = array_shift($targetEntityPrimaryKeyColumns);
     // add relation to owning entity
     $this->relationBuilder->addManyToOneRelation($this->configManager->getProvider('extend')->getConfig($sourceEntityClass), $targetEntityClass, $relationName, $targetFieldName, ['entity' => ['label' => $label, 'description' => $description], 'view' => ['is_displayable' => false], 'form' => ['is_enabled' => false]]);
 }
Exemplo n.º 3
0
 /**
  * @param string $entityClass
  * @throws WorkflowException
  */
 public function generateWorkflowFields($entityClass)
 {
     if ($this->entityConnector->isWorkflowAware($entityClass)) {
         return;
     }
     $extendConfigProvider = $this->configManager->getProvider('extend');
     $entityConfig = $extendConfigProvider->getConfig($entityClass);
     if (!$entityConfig || !$entityConfig->is('is_extend')) {
         throw new WorkflowException(sprintf('Class %s can not be extended', $entityClass));
     }
     $workflowItemClass = 'Oro\\Bundle\\WorkflowBundle\\Entity\\WorkflowItem';
     $workflowStepClass = 'Oro\\Bundle\\WorkflowBundle\\Entity\\WorkflowStep';
     // add fields
     $hasWorkflowItemField = $this->configManager->hasConfig($entityClass, self::PROPERTY_WORKFLOW_ITEM);
     if (!$hasWorkflowItemField) {
         $this->addRelationField($entityClass, self::PROPERTY_WORKFLOW_ITEM, ConfigHelper::getTranslationKey('entity', 'label', $workflowItemClass, 'related_entity'), ConfigHelper::getTranslationKey('entity', 'description', $workflowItemClass, 'related_entity'), $workflowItemClass, 'id');
     }
     $hasWorkflowStepField = $this->configManager->hasConfig($entityClass, self::PROPERTY_WORKFLOW_STEP);
     if (!$hasWorkflowStepField) {
         $this->addRelationField($entityClass, self::PROPERTY_WORKFLOW_STEP, ConfigHelper::getTranslationKey('entity', 'label', $workflowStepClass, 'related_entity'), ConfigHelper::getTranslationKey('entity', 'description', $workflowStepClass, 'related_entity'), $workflowStepClass, 'label');
     }
     // update entity config
     $entityConfig->set('state', ExtendScope::STATE_UPDATE);
     $entityConfig->set('upgradeable', true);
     $this->configManager->persist($entityConfig);
     $this->configManager->flush();
     // update database
     $this->entityProcessor->updateDatabase();
     // make fields hidden
     // TODO: Fields can be hidden only after schema update due to a bug in DoctrineSubscriber
     // TODO: Should be fixed in scope of https://magecore.atlassian.net/browse/BAP-3621
     // TODO: If make fields hidden then these fields will be created only for the first extended entity
     // TODO: Should be fixed in scope of https://magecore.atlassian.net/browse/BAP-3632
     /*
     if (!$hasWorkflowItemField) {
         $this->hideRelationField($entityClass, self::PROPERTY_WORKFLOW_ITEM);
     }
     if (!$hasWorkflowStepField) {
         $this->hideRelationField($entityClass, self::PROPERTY_WORKFLOW_STEP);
     }
     $this->configManager->flush();
     */
 }
 /**
  * @param string      $labelKey
  * @param string      $className
  * @param string|null $fieldName
  *
  * @return string
  */
 protected function getLabel($labelKey, $className, $fieldName = null)
 {
     return ConfigHelper::getTranslationKey('entity', $labelKey, $className, $fieldName);
 }
Exemplo n.º 5
0
 /**
  * @param FormBuilderInterface|FormInterface $builder
  * @param                                    $dataClass
  * @param string                             $permission
  * @param array                              $data
  * @param int                                $entityId
  */
 protected function addUserOwnerField($builder, $dataClass, $permission = "CREATE", $data = null, $entityId = 0)
 {
     /**
      * Showing user owner box for entities with owner type USER if assign permission is
      * granted.
      */
     if ($this->isAssignGranted || $permission == 'ASSIGN') {
         $formBuilder = $builder instanceof FormInterface ? $builder->getConfig() : $builder;
         $isRequired = $formBuilder->getOption('required');
         $options = ['label' => ConfigHelper::getTranslationKey('entity', 'label', $dataClass, $this->fieldName), 'required' => true, 'constraints' => $isRequired ? [new NotBlank()] : [], 'autocomplete_alias' => 'acl_users', 'configs' => ['placeholder' => 'oro.user.form.choose_user', 'result_template_twig' => 'OroUserBundle:User:Autocomplete/result.html.twig', 'selection_template_twig' => 'OroUserBundle:User:Autocomplete/selection.html.twig', 'component' => 'acl-user-autocomplete', 'permission' => $permission, 'entity_name' => str_replace('\\', '_', $dataClass), 'entity_id' => $entityId]];
         if (null !== $data) {
             $options['data'] = $data;
         }
         $builder->add($this->fieldName, 'oro_user_acl_select', $options);
     }
 }
Exemplo n.º 6
0
 /**
  * Extracts field default values from an annotation and config file
  *
  * @param string             $scope
  * @param string             $className
  * @param string             $fieldName
  * @param string             $fieldType
  * @param FieldMetadata|null $metadata
  *
  * @return array
  */
 protected function getFieldDefaultValues($scope, $className, $fieldName, $fieldType, $metadata = null)
 {
     $propertyConfig = $this->getPropertyConfig($scope);
     // try to get default values from an annotation
     if ($metadata && isset($metadata->defaultValues[$scope])) {
         // combine them with default values from a config file
         $defaultValues = array_merge($propertyConfig->getDefaultValues(PropertyConfigContainer::TYPE_FIELD, $fieldType), $metadata->defaultValues[$scope]);
     } else {
         $defaultValues = $propertyConfig->getDefaultValues(PropertyConfigContainer::TYPE_FIELD, $fieldType);
     }
     // process translatable values
     $translatablePropertyNames = $propertyConfig->getTranslatableValues(PropertyConfigContainer::TYPE_FIELD);
     foreach ($translatablePropertyNames as $propertyName) {
         if (empty($defaultValues[$propertyName])) {
             $defaultValues[$propertyName] = ConfigHelper::getTranslationKey($scope, $propertyName, $className, $fieldName);
         }
     }
     return $defaultValues;
 }
 /**
  * Makes sure virtual fields for the given entity were loaded
  *
  * @param string $className
  */
 protected function ensureVirtualFieldsInitialized($className)
 {
     if (!isset($this->virtualFields[$className])) {
         $this->ensureDictionariesInitialized();
         $this->virtualFields[$className] = [];
         $metadata = $this->getManagerForClass($className)->getClassMetadata($className);
         $associationNames = $metadata->getAssociationNames();
         foreach ($associationNames as $associationName) {
             $targetClassName = $metadata->getAssociationTargetClass($associationName);
             if ($metadata->isSingleValuedAssociation($associationName) && isset($this->dictionaries[$targetClassName])) {
                 $fields = $this->dictionaries[$targetClassName];
                 $isCombinedLabelName = count($fields) > 1;
                 foreach ($fields as $fieldName => $fieldType) {
                     $virtualFieldName = Inflector::tableize(sprintf('%s_%s', $associationName, $fieldName));
                     $label = $isCombinedLabelName ? $virtualFieldName : Inflector::tableize($associationName);
                     $label = ConfigHelper::getTranslationKey('entity', 'label', $className, $label);
                     $this->virtualFields[$className][$virtualFieldName] = ['query' => ['select' => ['expr' => sprintf('target.%s', $fieldName), 'return_type' => $fieldType, 'label' => $label], 'join' => ['left' => [['join' => sprintf('entity.%s', $associationName), 'alias' => 'target']]]]];
                 }
             }
         }
     }
 }
 /**
  * Gets a field label
  *
  * @param ClassMetadata $metadata
  * @param string        $fieldName
  *
  * @return string
  */
 protected function getFieldLabel(ClassMetadata $metadata, $fieldName)
 {
     $className = $metadata->getName();
     if (!$metadata->hasField($fieldName) && !$metadata->hasAssociation($fieldName)) {
         // virtual field or relation
         return ConfigHelper::getTranslationKey('entity', 'label', $className, $fieldName);
     }
     $label = $this->entityConfigProvider->hasConfig($className, $fieldName) ? $this->entityConfigProvider->getConfig($className, $fieldName)->get('label') : null;
     return !empty($label) ? $label : ConfigHelper::getTranslationKey('entity', 'label', $className, $fieldName);
 }
Exemplo n.º 9
0
 /**
  * @param string          $labelKey
  * @param string          $entityClass
  * @param string          $relationName
  * @param ConfigInterface $targetEntityConfig
  *
  * @return string
  */
 protected function getAssociationLabel($labelKey, $entityClass, $relationName, ConfigInterface $targetEntityConfig)
 {
     $label = $targetEntityConfig->get($labelKey);
     if (!$label) {
         $label = ConfigHelper::getTranslationKey('entity', $labelKey, $entityClass, $relationName);
     }
     return $label;
 }
Exemplo n.º 10
0
 /**
  * Gets a field label
  *
  * @param string $className
  * @param string $fieldName
  *
  * @return string
  */
 protected function getFieldLabel($className, $fieldName)
 {
     $label = $this->entityConfigProvider->hasConfig($className, $fieldName) ? $this->entityConfigProvider->getConfig($className, $fieldName)->get('label') : null;
     return !empty($label) ? $label : ConfigHelper::getTranslationKey('entity', 'label', $className, $fieldName);
 }
Exemplo n.º 11
0
 /**
  * @dataProvider getTranslationKeyProvider
  */
 public function testGetTranslationKey($expected, $scope, $propertyName, $className, $fieldName)
 {
     $result = ConfigHelper::getTranslationKey($scope, $propertyName, $className, $fieldName);
     $this->assertEquals($expected, $result);
 }
 /**
  * Makes sure virtual fields for the given entity were loaded
  *
  * @param string $className
  */
 protected function ensureVirtualFieldsInitialized($className)
 {
     if (!isset($this->virtualFields[$className])) {
         $this->ensureDictionariesInitialized();
         $this->virtualFields[$className] = [];
         $metadata = $this->getManagerForClass($className)->getClassMetadata($className);
         $associationNames = $metadata->getAssociationNames();
         foreach ($associationNames as $associationName) {
             $targetClassName = $metadata->getAssociationTargetClass($associationName);
             if (isset($this->dictionaries[$targetClassName])) {
                 $fields = $this->dictionaries[$targetClassName];
                 $isCombinedLabelName = count($fields) > 1;
                 $fieldNames = array_keys($fields);
                 $target = Inflector::tableize(uniqid(sprintf('t_%s', $associationName), false));
                 foreach ($fieldNames as $fieldName) {
                     $virtualFieldName = Inflector::tableize(sprintf('%s_%s', $associationName, $fieldName));
                     $fieldName = Inflector::tableize($fieldName);
                     $label = $isCombinedLabelName ? $virtualFieldName : Inflector::tableize($associationName);
                     $label = ConfigHelper::getTranslationKey('entity', 'label', $className, $label);
                     $this->virtualFields[$className][$virtualFieldName] = ['query' => ['select' => ['expr' => sprintf('%s.%s', $target, $fieldName), 'return_type' => GroupingScope::GROUP_DICTIONARY, 'related_entity_name' => $targetClassName, 'label' => $label], 'join' => ['left' => [['join' => sprintf('entity.%s', $associationName), 'alias' => $target]]]]];
                 }
             }
         }
     }
 }