public function testMetadata() { $metadata = new DropDownDependencyDerivedAttributeMetadata(); $metadata->setScenario('nonAutoBuild'); $metadata->name = 'someName'; $metadata->modelClassName = 'Whatever'; $metadata->serializedMetadata = serialize(array('stuff', 1, 'attributeLabels' => array())); $this->assertTrue($metadata->save()); unset($metadata); $metadata = DropDownDependencyDerivedAttributeMetadata::getByNameAndModelClassName('someName', 'Whatever'); $metadata->setScenario('nonAutoBuild'); $this->assertEquals('someName', $metadata->name); $this->assertEquals('Whatever', $metadata->modelClassName); $this->assertEquals('a:3:{i:0;s:5:"stuff";i:1;i:1;s:15:"attributeLabels";a:0:{}}', $metadata->serializedMetadata); $metadata->serializedMetadata = serialize(array('stuffx', 1, 'attributeLabels' => array())); $this->assertTrue($metadata->save()); }
/** * Returns the a type that is derived by looking at several different components of an attribute. This * includes the metadata element data, validator data, and relation information. The type returned can * be utilized by different aspects of the application where an attribute type is needed. * @return string type */ public static function getType($modelClassName, $attributeName) { assert('is_string($modelClassName) && $modelClassName != ""'); assert('is_string($attributeName) && $attributeName != ""'); try { $models = CalculatedDerivedAttributeMetadata::getByNameAndModelClassName($attributeName, $modelClassName); if (count($models) == 1) { return 'CalculatedNumber'; } } catch (NotFoundException $e) { } try { $models = DropDownDependencyDerivedAttributeMetadata::getByNameAndModelClassName($attributeName, $modelClassName); if (count($models) == 1) { return 'DropDownDependency'; } } catch (NotFoundException $e) { throw new NotImplementedException($attributeName . 'M' . $modelClassName); } }
/** * @depends testUpdateValueInMappingByOldAndNewValue */ public function testResolveValuesInMappingWhenValueWasRemoved() { //Remove a1 $customFieldDataData = array('a2New', 'a3', 'a4'); DropDownDependencyDerivedAttributeDesignerUtil::resolveValuesInMappingWhenValueWasRemoved('aModelClassName', 'a', $customFieldDataData); //Confirm a1 has been removed from the mapping. $metadata = DropDownDependencyDerivedAttributeMetadata::getByNameAndModelClassName('aName', 'aModelClassName'); $unserializedData = unserialize($metadata->serializedMetadata); $compareData = array(array('attributeName' => 'a'), array('attributeName' => 'b', 'valuesToParentValues' => array('b1' => null, 'b2' => 'a2New', 'b3New' => 'a3', 'b4' => 'a4')), array('attributeName' => 'c', 'valuesToParentValues' => array('c1New' => 'b1', 'c2New' => 'b2', 'c3' => 'b3New', 'c4' => 'b4'))); $this->assertEquals(array('a' => 'b'), $unserializedData['attributeLabels']); $this->assertEquals($compareData, $unserializedData['mappingData']); //Remove b4 $customFieldDataData = array('b1', 'b2', 'b3New'); DropDownDependencyDerivedAttributeDesignerUtil::resolveValuesInMappingWhenValueWasRemoved('aModelClassName', 'b', $customFieldDataData); //Confirm b4 has been removed from the mapping. $metadata = DropDownDependencyDerivedAttributeMetadata::getByNameAndModelClassName('aName', 'aModelClassName'); $unserializedData = unserialize($metadata->serializedMetadata); $compareData = array(array('attributeName' => 'a'), array('attributeName' => 'b', 'valuesToParentValues' => array('b1' => null, 'b2' => 'a2New', 'b3New' => 'a3')), array('attributeName' => 'c', 'valuesToParentValues' => array('c1New' => 'b1', 'c2New' => 'b2', 'c3' => 'b3New', 'c4' => null))); $this->assertEquals(array('a' => 'b'), $unserializedData['attributeLabels']); $this->assertEquals($compareData, $unserializedData['mappingData']); }
protected function areAllPlacedAttributesUnique() { $moduleClassName = $this->moduleClassName; $modelClassName = $moduleClassName::getPrimaryModelName(); foreach ($this->placedDirectAttributeNamesAsElements as $attributeName) { if (isset($this->placeableLayoutAttributes[$attributeName])) { $elementType = $this->placeableLayoutAttributes[$attributeName]['elementType']; if ($elementType == 'DropDownDependency') { $metadata = DropDownDependencyDerivedAttributeMetadata::getByNameAndModelClassName($attributeName, $modelClassName); $usedAttributeNames = $metadata->getUsedAttributeNames(); foreach ($usedAttributeNames as $usedAttribute) { if (in_array($usedAttribute, $this->placedDirectAttributeNamesAsElements)) { $this->message = Zurmo::t('DesignerModule', 'All fields placed must be unique. Two of the placed ' . 'fields: {field1Label} and {field2Label} contain the same fields.', array('{field1Label}' => $this->placeableLayoutAttributes[$attributeName]['attributeLabel'], '{field2Label}' => $this->placeableLayoutAttributes[$usedAttribute]['attributeLabel'])); return false; } } } } else { //Do nothing. This means a real attribute is only placable as part of a derived attribute. } } return true; }
protected function makeMetadata() { assert('$this->attribute != null'); assert('$this->model instanceof RedBeanModel'); $this->dropDownDependencyDerivedAttributeMetadata = DropDownDependencyDerivedAttributeMetadata::getByNameAndModelClassName($this->attribute, get_class($this->model)); }
/** * (non-PHPdoc) * @see AttributeForm::validateAttributeNameDoesNotExists() */ public function validateAttributeNameDoesNotExists() { assert('$this->modelClassName != null'); try { $models = DropDownDependencyDerivedAttributeMetadata::getByNameAndModelClassName($this->attributeName, $this->modelClassName); $this->addError('attributeName', Zurmo::t('DesignerModule', 'A field with this name is already used.')); } catch (NotFoundException $e) { } }
/** * @depends testUsingTheAdapterAsAWrapperToUpdateValueInMappingByOldAndNewValue */ public function testUsingTheAdapterAsAWrapperToResolveValuesInMappingWhenValueWasRemoved() { Yii::app()->user->userModel = User::getByUsername('super'); $account = new Account(); //Remove a1 $attributeForm = AttributesFormFactory::createAttributeFormByAttributeName($account, 'aaaCstm'); $attributeForm->customFieldDataData = array('a2New', 'a3', 'a4'); $modelAttributesAdapterClassName = $attributeForm::getModelAttributeAdapterNameForSavingAttributeFormData(); $adapter = new $modelAttributesAdapterClassName($account); try { $adapter->setAttributeMetadataFromForm($attributeForm); } catch (FailedDatabaseSchemaChangeException $e) { echo $e->getMessage(); $this->fail(); } //Confirm a1 has been removed from the mapping. $metadata = DropDownDependencyDerivedAttributeMetadata::getByNameAndModelClassName('aName', 'Account'); $unserializedData = unserialize($metadata->serializedMetadata); $compareData = array(array('attributeName' => 'aaaCstm'), array('attributeName' => 'bbbCstm', 'valuesToParentValues' => array('b1' => null, 'b2' => 'a2New', 'b3New' => 'a3', 'b4' => 'a4')), array('attributeName' => 'cccCstm', 'valuesToParentValues' => array('c1New' => 'b1', 'c2New' => 'b2', 'c3' => 'b3New', 'c4' => 'b4'))); $this->assertEquals(array('a' => 'b'), $unserializedData['attributeLabels']); $this->assertEquals($compareData, $unserializedData['mappingData']); //Remove b4 $attributeForm = AttributesFormFactory::createAttributeFormByAttributeName($account, 'bbbCstm'); $attributeForm->customFieldDataData = array('b1', 'b2', 'b3New'); $modelAttributesAdapterClassName = $attributeForm::getModelAttributeAdapterNameForSavingAttributeFormData(); $adapter = new $modelAttributesAdapterClassName($account); try { $adapter->setAttributeMetadataFromForm($attributeForm); } catch (FailedDatabaseSchemaChangeException $e) { echo $e->getMessage(); $this->fail(); } //Confirm b4 has been removed from the mapping. $metadata = DropDownDependencyDerivedAttributeMetadata::getByNameAndModelClassName('aName', 'Account'); $unserializedData = unserialize($metadata->serializedMetadata); $compareData = array(array('attributeName' => 'aaaCstm'), array('attributeName' => 'bbbCstm', 'valuesToParentValues' => array('b1' => null, 'b2' => 'a2New', 'b3New' => 'a3')), array('attributeName' => 'cccCstm', 'valuesToParentValues' => array('c1New' => 'b1', 'c2New' => 'b2', 'c3' => 'b3New', 'c4' => null))); $this->assertEquals(array('a' => 'b'), $unserializedData['attributeLabels']); $this->assertEquals($compareData, $unserializedData['mappingData']); }