public function testGetDataWithHasOneRelatedModel()
 {
     $super = User::getByUsername('super');
     Yii::app()->user->userModel = $super;
     $currencies = Currency::getAll();
     $currencyValue = new CurrencyValue();
     $currencyValue->value = 100;
     $currencyValue->currency = $currencies[0];
     $this->assertEquals('USD', $currencyValue->currency->code);
     $testItem2 = new ModelToArrayAdapterTestItem2();
     $testItem2->name = 'John';
     $this->assertTrue($testItem2->save());
     $testItem4 = new ModelToArrayAdapterTestItem4();
     $testItem4->name = 'John';
     $this->assertTrue($testItem4->save());
     //HAS_MANY and MANY_MANY relationships should be ignored.
     $testItem3_1 = new ModelToArrayAdapterTestItem3();
     $testItem3_1->name = 'Kevin';
     $this->assertTrue($testItem3_1->save());
     $testItem3_2 = new ModelToArrayAdapterTestItem3();
     $testItem3_2->name = 'Jim';
     $this->assertTrue($testItem3_2->save());
     $testItem = new ModelToArrayAdapterTestItem();
     $testItem->firstName = 'Bob3';
     $testItem->lastName = 'Bob3';
     $testItem->boolean = true;
     $testItem->date = '2002-04-03';
     $testItem->dateTime = '2002-04-03 02:00:43';
     $testItem->float = 54.22;
     $testItem->integer = 10;
     $testItem->phone = '21313213';
     $testItem->string = 'aString';
     $testItem->textArea = 'Some Text Area';
     $testItem->url = 'http://www.asite.com';
     $testItem->owner = $super;
     $testItem->currencyValue = $currencyValue;
     $testItem->hasOne = $testItem2;
     $testItem->hasMany->add($testItem3_1);
     $testItem->hasMany->add($testItem3_2);
     $testItem->hasOneAlso = $testItem4;
     $createStamp = strtotime(DateTimeUtil::convertTimestampToDbFormatDateTime(time()));
     $this->assertTrue($testItem->save());
     $id = $testItem->id;
     $testItem->forget();
     unset($testItem);
     $testItem = ModelToArrayAdapterTestItem::getById($id);
     $adapter = new ModelToArrayAdapter($testItem);
     $data = $adapter->getData();
     $compareData = array('id' => $id, 'firstName' => 'Bob3', 'lastName' => 'Bob3', 'boolean' => 1, 'date' => '2002-04-03', 'dateTime' => '2002-04-03 02:00:43', 'float' => 54.22, 'integer' => 10, 'phone' => '21313213', 'string' => 'aString', 'textArea' => 'Some Text Area', 'url' => 'http://www.asite.com', 'currencyValue' => array('id' => $currencyValue->id, 'value' => 100, 'rateToBase' => 1, 'currency' => array('id' => $currencies[0]->id)), 'dropDown' => null, 'radioDropDown' => null, 'hasOne' => array('id' => $testItem2->id), 'hasOneAlso' => array('id' => $testItem4->id), 'primaryEmail' => null, 'primaryAddress' => null, 'secondaryEmail' => null, 'owner' => array('id' => $super->id, 'username' => 'super'), 'createdByUser' => array('id' => $super->id, 'username' => 'super'), 'modifiedByUser' => array('id' => $super->id, 'username' => 'super'), 'multiDropDown' => array('values' => null), 'tagCloud' => array('values' => null));
     // Because small delay in IO operation, allow tresholds
     $this->assertEquals($createStamp, strtotime($data['createdDateTime']), '', 2);
     $this->assertEquals($createStamp, strtotime($data['modifiedDateTime']), '', 2);
     unset($data['createdDateTime']);
     unset($data['modifiedDateTime']);
     $this->assertEquals($compareData, $data);
 }
 public function testCopy()
 {
     Yii::app()->user->userModel = User::getByUsername('sally');
     $currencyValue = new CurrencyValue();
     $currencyValue->value = 100;
     $currencyValue->currency = Currency::getByCode('EUR');
     $testItem = new ModelToArrayAdapterTestItem();
     $testItem->firstName = 'Bob';
     $testItem->lastName = 'Bobson';
     $testItem->boolean = true;
     $testItem->date = '2002-04-03';
     $testItem->dateTime = '2002-04-03 02:00:43';
     $testItem->float = 54.22;
     $testItem->integer = 10;
     $testItem->phone = '21313213';
     $testItem->string = 'aString';
     $testItem->textArea = 'Some Text Area';
     $testItem->url = 'http://www.asite.com';
     $testItem->primaryEmail->emailAddress = '*****@*****.**';
     $testItem->primaryAddress->street1 = 'some street';
     $testItem->owner = Yii::app()->user->userModel;
     $testItem->currencyValue = $currencyValue;
     $customFieldValue = new CustomFieldValue();
     $customFieldValue->value = 'Multi 1';
     $testItem->multiDropDown->values->add($customFieldValue);
     $customFieldValue = new CustomFieldValue();
     $customFieldValue->value = 'Multi 3';
     $testItem->multiDropDown->values->add($customFieldValue);
     $customFieldValue = new CustomFieldValue();
     $customFieldValue->value = 'Cloud 2';
     $testItem->tagCloud->values->add($customFieldValue);
     $customFieldValue = new CustomFieldValue();
     $customFieldValue->value = 'Cloud 3';
     $testItem->tagCloud->values->add($customFieldValue);
     $testItem->dropDown->value = 'Sample';
     $testItem2 = new ModelToArrayAdapterTestItem2();
     $testItem2->name = 'John';
     $this->assertTrue($testItem2->save());
     $testItem4 = new ModelToArrayAdapterTestItem4();
     $testItem4->name = 'John';
     $this->assertTrue($testItem4->save());
     //HAS_MANY and MANY_MANY relationships should be ignored.
     $testItem3_1 = new ModelToArrayAdapterTestItem3();
     $testItem3_1->name = 'Kevin';
     $this->assertTrue($testItem3_1->save());
     $testItem3_2 = new ModelToArrayAdapterTestItem3();
     $testItem3_2->name = 'Jim';
     $this->assertTrue($testItem3_2->save());
     $testItem->hasOne = $testItem2;
     $testItem->hasMany->add($testItem3_1);
     $testItem->hasMany->add($testItem3_2);
     $testItem->hasOneAlso = $testItem4;
     $this->assertTrue($testItem->save());
     $testItem->addPermissions(self::$groupA, Permission::READ_WRITE_CHANGE_PERMISSIONS_CHANGE_OWNER);
     $this->assertTrue($testItem->save());
     $id = $testItem->id;
     $testItem->forget();
     unset($testItem);
     //Switch to super and copy the model
     Yii::app()->user->userModel = User::getByUsername('super');
     $testItem = ModelToArrayAdapterTestItem::getById($id);
     $copyToItem = new ModelToArrayAdapterTestItem();
     ZurmoCopyModelUtil::copy($testItem, $copyToItem);
     $this->assertEquals('Bob', $copyToItem->firstName);
     $this->assertEquals('Bobson', $copyToItem->lastName);
     $this->assertEquals(true, $copyToItem->boolean);
     $this->assertEquals('2002-04-03', $copyToItem->date);
     $this->assertEquals('2002-04-03 02:00:43', $copyToItem->dateTime);
     $this->assertEquals(54.22, $copyToItem->float);
     $this->assertEquals(10, $copyToItem->integer);
     $this->assertEquals('21313213', $copyToItem->phone);
     $this->assertEquals('aString', $copyToItem->string);
     $this->assertEquals('Some Text Area', $copyToItem->textArea);
     $this->assertEquals('http://www.asite.com', $copyToItem->url);
     $this->assertEquals('*****@*****.**', $copyToItem->primaryEmail->emailAddress);
     $this->assertEquals('some street', $copyToItem->primaryAddress->street1);
     $this->assertEquals('Sample', $copyToItem->dropDown->value);
     $this->assertEquals(2, $copyToItem->multiDropDown->values->count());
     $this->assertTrue($copyToItem->multiDropDown->values[0] == 'Multi 1' || $copyToItem->multiDropDown->values[0] == 'Multi 3');
     $this->assertEquals(2, $copyToItem->tagCloud->values->count());
     $this->assertTrue($copyToItem->tagCloud->values[0] == 'Cloud 2' || $copyToItem->tagCloud->values[0] == 'Cloud 3');
     $this->assertEquals(100, $copyToItem->currencyValue->value);
     $this->assertEquals(2, $copyToItem->currencyValue->rateToBase);
     $this->assertEquals('EUR', $copyToItem->currencyValue->currency->code);
     $this->assertTrue($copyToItem->owner->isSame(self::$sally));
     $this->assertTrue($copyToItem->createdByUser->id < 0);
     $this->assertEquals(Yii::app()->user->userModel->id, $copyToItem->modifiedByUser->id);
     $this->assertEquals(0, $copyToItem->hasMany->count());
     $this->assertTrue($copyToItem->hasOne->isSame($testItem2));
     $this->assertTrue($copyToItem->hasOneAlso->isSame($testItem4));
     $explicitReadWriteModelPermissions = ExplicitReadWriteModelPermissionsUtil::makeBySecurableItem($copyToItem);
     $permitables = $explicitReadWriteModelPermissions->getReadWritePermitables();
     $this->assertEquals(1, count($permitables));
     $this->assertEquals('AAA', $permitables[self::$groupA->getClassId('Permitable')]->name);
 }