public function testValidateAttributeNameDoesNotExists()
 {
     Yii::app()->user->userModel = User::getByUsername('super');
     $compareAttributeLabels = array('de' => 'sameattribute de', 'en' => 'sameattribute en');
     $customFieldDataData = array('a', 'b', 'c');
     $customFieldDataLabels = array('fr' => array('Afr', 'Bfr', 'Cfr'), 'de' => array('Ade', 'Bde', 'Cde'));
     $attributeForm = new DropDownAttributeForm();
     $attributeForm->attributeName = 'same';
     $attributeForm->attributeLabels = $compareAttributeLabels;
     $attributeForm->customFieldDataData = $customFieldDataData;
     $attributeForm->customFieldDataLabels = $customFieldDataLabels;
     $modelAttributesAdapterClassName = $attributeForm::getModelAttributeAdapterNameForSavingAttributeFormData();
     $adapter = new $modelAttributesAdapterClassName(new Account());
     $adapter->setAttributeMetadataFromForm($attributeForm);
     $attributeForm = new DropDownAttributeForm();
     $attributeForm->attributeName = 'same';
     $attributeForm->attributeLabels = $compareAttributeLabels;
     $attributeForm->customFieldDataData = $customFieldDataData;
     $attributeForm->customFieldDataLabels = $customFieldDataLabels;
     $attributeForm->modelClassName = 'Contact';
     $attributeForm->setScenario('createAttribute');
     $attributeForm->validate();
     $this->assertContains('A field with this name and data is already used in another module', $attributeForm->getError('attributeName'));
 }
Beispiel #2
0
 /**
  * @depends testSetAndGetDecimalAttribute
  */
 public function testSetAndGetDropDownAttribute()
 {
     $values = array('747', 'A380', 'Seaplane', 'Dive Bomber');
     $labels = array('fr' => array('747 fr', 'A380 fr', 'Seaplane fr', 'Dive Bomber fr'), 'de' => array('747 de', 'A380 de', 'Seaplane de', 'Dive Bomber de'));
     $airplanesFieldData = CustomFieldData::getByName('Airplanes');
     $airplanesFieldData->serializedData = serialize($values);
     $this->assertTrue($airplanesFieldData->save());
     $attributeForm = new DropDownAttributeForm();
     $attributeForm->attributeName = 'testAirPlane';
     $attributeForm->attributeLabels = array('de' => 'Test Airplane 2 de', 'en' => 'Test Airplane 2 en', 'es' => 'Test Airplane 2 es', 'fr' => 'Test Airplane 2 fr', 'it' => 'Test Airplane 2 it');
     $attributeForm->isAudited = true;
     $attributeForm->isRequired = true;
     $attributeForm->defaultValueOrder = 1;
     $attributeForm->customFieldDataData = $values;
     $attributeForm->customFieldDataName = 'Airplanes';
     $attributeForm->customFieldDataLabels = $labels;
     $modelAttributesAdapterClassName = $attributeForm::getModelAttributeAdapterNameForSavingAttributeFormData();
     $adapter = new $modelAttributesAdapterClassName(new Account());
     try {
         $adapter->setAttributeMetadataFromForm($attributeForm);
     } catch (FailedDatabaseSchemaChangeException $e) {
         echo $e->getMessage();
         $this->fail();
     }
     $account = new Account();
     $attributeForm = AttributesFormFactory::createAttributeFormByAttributeName($account, 'testAirPlaneCstm');
     $this->assertEquals('DropDown', $attributeForm->getAttributeTypeName());
     $this->assertEquals('testAirPlaneCstm', $attributeForm->attributeName);
     $compareAttributeLabels = array('de' => 'Test Airplane 2 de', 'en' => 'Test Airplane 2 en', 'es' => 'Test Airplane 2 es', 'fr' => 'Test Airplane 2 fr', 'it' => 'Test Airplane 2 it');
     $this->assertEquals($compareAttributeLabels, $attributeForm->attributeLabels);
     $this->assertEquals(true, $attributeForm->isAudited);
     $this->assertEquals(true, $attributeForm->isRequired);
     $this->assertEquals('A380', $attributeForm->defaultValue);
     $this->assertEquals(1, $attributeForm->defaultValueOrder);
     $this->assertEquals('Airplanes', $attributeForm->customFieldDataName);
     $this->assertEquals($values, $attributeForm->customFieldDataData);
     $this->assertEquals($labels, $attributeForm->customFieldDataLabels);
     //Test that validation on completely new picklists works correctly and is inline with the rules from
     //the CustomFieldData model.
     $attributeForm = new DropDownAttributeForm();
     $attributeForm->attributeName = 's';
     //name to short. test that this fails.
     $attributeForm->attributeLabels = array('de' => 'Test Airplane 3 de', 'en' => 'Test Airplane 3 en', 'es' => 'Test Airplane 3 es', 'fr' => 'Test Airplane 3 fr', 'it' => 'Test Airplane 3 it');
     $attributeForm->isAudited = true;
     $attributeForm->isRequired = true;
     $attributeForm->defaultValueOrder = 1;
     $attributeForm->customFieldDataData = array('a', 'b', 'c');
     $modelAttributesAdapterClassName = $attributeForm::getModelAttributeAdapterNameForSavingAttributeFormData();
     $adapter = new $modelAttributesAdapterClassName(new Account());
     $this->assertFalse($attributeForm->validate());
     $attributeForm->attributeName = 'camelcased';
     $this->assertTrue($attributeForm->validate());
     try {
         $adapter->setAttributeMetadataFromForm($attributeForm);
     } catch (Exception $e) {
         echo $e->getMessage();
         $this->fail();
     }
 }
 public static function createDropDownAttribute($attributeName, $withDefaultData, $modelClassName)
 {
     assert('isset($attributeName) && $attributeName != null');
     assert('isset($withDefaultData) && is_bool($withDefaultData)');
     $values = array('747', 'A380', 'Seaplane', 'Dive Bomber');
     $labels = array('fr' => array('747 fr', 'A380 fr', 'Seaplane fr', 'Dive Bomber fr'), 'de' => array('747 de', 'A380 de', 'Seaplane de', 'Dive Bomber de'));
     $airplanesFieldData = CustomFieldData::getByName('Airplanes');
     $airplanesFieldData->serializedData = serialize($values);
     $airplanesFieldData->save();
     $attributeForm = new DropDownAttributeForm();
     $attributeForm->attributeName = $attributeName;
     $attributeForm->attributeLabels = array('de' => 'Test' . $attributeName . 'de', 'en' => 'Test' . $attributeName . 'en', 'es' => 'Test' . $attributeName . 'es', 'fr' => 'Test' . $attributeName . 'fr', 'it' => 'Test' . $attributeName . 'it');
     $attributeForm->isAudited = true;
     $attributeForm->isRequired = false;
     $attributeForm->defaultValueOrder = 1;
     $attributeForm->customFieldDataData = $values;
     $attributeForm->customFieldDataName = 'Airplanes';
     $attributeForm->customFieldDataLabels = $labels;
     $validate = $attributeForm->validate();
     if ($validate == false) {
         throw new FailedToValidateException();
     }
     $modelAttributesAdapterClassName = $attributeForm::getModelAttributeAdapterNameForSavingAttributeFormData();
     $adapter = new $modelAttributesAdapterClassName(new $modelClassName());
     try {
         $adapter->setAttributeMetadataFromForm($attributeForm);
     } catch (FailedDatabaseSchemaChangeException $e) {
         echo $e->getMessage();
         Yii::app()->end(0, false);
     }
 }