/**
  * @depends testUpdate
  */
 public function testUpdateWithRelations()
 {
     $super = User::getByUsername('super');
     Yii::app()->user->userModel = $super;
     $authenticationData = $this->login();
     $headers = array('Accept: application/json', 'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'], 'ZURMO_TOKEN: ' . $authenticationData['token'], 'ZURMO_API_REQUEST_TYPE: REST');
     $testModel = new ApiTestModelItem();
     $testModel->firstName = "Ruth";
     $testModel->lastName = "Tester";
     $testModel->string = "aString";
     $this->assertTrue($testModel->save());
     $compareData = $this->getModelToApiDataUtilData($testModel);
     $testModel->forget();
     $testItem4 = new ApiTestModelItem4();
     $testItem4->name = 'John7';
     $this->assertTrue($testItem4->save());
     $testItem3_1 = new ApiTestModelItem3();
     $testItem3_1->name = 'Kevin7';
     $this->assertTrue($testItem3_1->save());
     $testItem3_2 = new ApiTestModelItem3();
     $testItem3_2->name = 'Jim7';
     $this->assertTrue($testItem3_2->save());
     $testItemRelated = new ApiTestModelItem();
     $testItemRelated->lastName = 'Cohens7';
     $testItemRelated->string = 'aString';
     $this->assertTrue($testItemRelated->save());
     $data['modelRelations'] = array('modelItems3' => array(array('action' => 'add', 'modelId' => $testItem3_1->id, 'modelClassName' => 'ApiTestModelItem3'), array('action' => 'add', 'modelId' => $testItem3_2->id, 'modelClassName' => 'ApiTestModelItem3')), 'modelItems4' => array(array('action' => 'add', 'modelId' => $testItem4->id, 'modelClassName' => 'ApiTestModelItem4')), 'modelItems' => array(array('action' => 'add', 'modelId' => $testItemRelated->id, 'modelClassName' => 'ApiTestModelItem')));
     $data['firstName'] = 'Michael6';
     $group = static::$randomNonEveryoneNonAdministratorsGroup;
     $explicitReadWriteModelPermissions = array('type' => 2, 'nonEveryoneGroup' => $group->id);
     $data['explicitReadWriteModelPermissions'] = $explicitReadWriteModelPermissions;
     $compareData['explicitReadWriteModelPermissions'] = $explicitReadWriteModelPermissions;
     $response = $this->createApiCallWithRelativeUrl('update/' . $compareData['id'], 'PUT', $headers, array('data' => $data));
     $response = json_decode($response, true);
     unset($response['data']['modifiedDateTime']);
     unset($compareData['modifiedDateTime']);
     $compareData['firstName'] = 'Michael6';
     $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']);
     $this->assertEquals($compareData, $response['data']);
     RedBeanModel::forgetAll();
     $updatedModel = ApiTestModelItem::getById($compareData['id']);
     $this->assertEquals(2, count($updatedModel->modelItems3));
     $this->assertEquals($testItem3_1->id, $updatedModel->modelItems3[0]->id);
     $this->assertEquals($testItem3_2->id, $updatedModel->modelItems3[1]->id);
     $this->assertEquals(1, count($updatedModel->modelItems4));
     $this->assertEquals($testItem4->id, $updatedModel->modelItems4[0]->id);
     $this->assertEquals(1, count($updatedModel->modelItems));
     $this->assertEquals($testItemRelated->id, $updatedModel->modelItems[0]->id);
     // Now test remove relations
     $data['modelRelations'] = array('modelItems3' => array(array('action' => 'remove', 'modelId' => $testItem3_1->id, 'modelClassName' => 'ApiTestModelItem3'), array('action' => 'remove', 'modelId' => $testItem3_2->id, 'modelClassName' => 'ApiTestModelItem3')), 'modelItems4' => array(array('action' => 'remove', 'modelId' => $testItem4->id, 'modelClassName' => 'ApiTestModelItem4')), 'modelItems' => array(array('action' => 'remove', 'modelId' => $testItemRelated->id, 'modelClassName' => 'ApiTestModelItem')));
     $response = $this->createApiCallWithRelativeUrl('update/' . $compareData['id'], 'PUT', $headers, array('data' => $data));
     $response = json_decode($response, true);
     $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']);
     RedBeanModel::forgetAll();
     $updatedModel = ApiTestModelItem::getById($compareData['id']);
     $this->assertEquals(0, count($updatedModel->modelItems3));
     $this->assertEquals(0, count($updatedModel->modelItems4));
     $this->assertEquals(0, count($updatedModel->modelItems));
     // Test with invalid action
     $data['modelRelations'] = array('modelItems' => array(array('action' => 'invalidAction', 'modelId' => $testItemRelated->id, 'modelClassName' => 'ApiTestModelItem')));
     $response = $this->createApiCallWithRelativeUrl('update/' . $compareData['id'], 'PUT', $headers, array('data' => $data));
     $response = json_decode($response, true);
     $this->assertEquals(ApiResponse::STATUS_FAILURE, $response['status']);
     // Test with invalid relation
     $data['modelRelations'] = array('aaad' => array(array('action' => 'remove', 'modelId' => $testItemRelated->id, 'modelClassName' => 'ApiTestModelItem')));
     $response = $this->createApiCallWithRelativeUrl('update/' . $compareData['id'], 'PUT', $headers, array('data' => $data));
     $response = json_decode($response, true);
     $this->assertEquals(ApiResponse::STATUS_FAILURE, $response['status']);
     // Test with invalid related model id
     $data['modelRelations'] = array('modelItems3' => array(array('action' => 'remove', 'modelId' => 345, 'modelClassName' => 'ApiTestModelItem3')));
     $response = $this->createApiCallWithRelativeUrl('update/' . $compareData['id'], 'PUT', $headers, array('data' => $data));
     $response = json_decode($response, true);
     $this->assertEquals(ApiResponse::STATUS_FAILURE, $response['status']);
 }
 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 ApiTestModelItem2();
     $testItem2->name = 'John';
     $this->assertTrue($testItem2->save());
     $testItem4 = new ApiTestModelItem4();
     $testItem4->name = 'John';
     $this->assertTrue($testItem4->save());
     //HAS_MANY and MANY_MANY relationships should be ignored.
     $testItem3_1 = new ApiTestModelItem3();
     $testItem3_1->name = 'Kevin';
     $this->assertTrue($testItem3_1->save());
     $testItem3_2 = new ApiTestModelItem3();
     $testItem3_2->name = 'Jim';
     $this->assertTrue($testItem3_2->save());
     $testItem = new ApiTestModelItem();
     $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->modelItem2 = $testItem2;
     $testItem->modelItems3->add($testItem3_1);
     $testItem->modelItems3->add($testItem3_2);
     $testItem->modelItems4->add($testItem4);
     $createStamp = strtotime(DateTimeUtil::convertTimestampToDbFormatDateTime(time()));
     $this->assertTrue($testItem->save());
     $id = $testItem->id;
     $testItem->forget();
     unset($testItem);
     $testItem = ApiTestModelItem::getById($id);
     $adapter = new RedBeanModelToApiDataUtil($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, 'multiDropDown' => array('values' => null), 'tagCloud' => array('values' => null), 'modelItem2' => array('id' => $testItem2->id), 'primaryEmail' => null, 'primaryAddress' => null, 'secondaryEmail' => null, 'language' => null, 'owner' => array('id' => $super->id, 'username' => 'super'), 'createdByUser' => array('id' => $super->id, 'username' => 'super'), 'modifiedByUser' => array('id' => $super->id, 'username' => 'super'));
     // 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);
 }