public function getData() { $data = parent::getData(); if ($this->model instanceof SecurableItem) { $element = new DerivedExplicitReadWriteModelPermissionsElement($this->model, null, null); $permissions = $element->resolveModelPermissionsArray(); $data = CMap::mergeArray($permissions, $data); } return $data; }
/** * @depends testGetRole */ public function testListRoles() { $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'); $roles = Role::getAll(); $compareData = array(); foreach ($roles as $role) { $redBeanModelToApiDataUtil = new RedBeanModelToApiDataUtil($role); $compareData[] = $redBeanModelToApiDataUtil->getData(); } //Test List $response = ApiRestTestHelper::createApiCall($this->serverUrl . '/test.php/zurmo/role/api/list/', 'GET', $headers); $response = json_decode($response, true); $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']); $this->assertEquals(count($roles), count($response['data']['items'])); $this->assertEquals(count($roles), $response['data']['totalCount']); $this->assertEquals(1, $response['data']['currentPage']); $this->assertEquals($compareData, $response['data']['items']); }
/** * @depends testUpdateLead */ public function testListLead() { RedBeanModel::forgetAll(); $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'); $leads = Contact::getByName('Michael Smith'); $this->assertEquals(1, count($leads)); $redBeanModelToApiDataUtil = new RedBeanModelToApiDataUtil($leads[0]); $compareData = $redBeanModelToApiDataUtil->getData(); $response = ApiRestTestHelper::createApiCall($this->serverUrl . '/test.php/leads/contact/api/list/', 'GET', $headers); $response = json_decode($response, true); $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']); $this->assertEquals(1, count($response['data']['items'])); $this->assertEquals(1, $response['data']['totalCount']); $this->assertEquals(1, $response['data']['currentPage']); $this->assertEquals(array($compareData), $response['data']['items']); }
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); }
/** * @depends testCreateWithRelations */ public function testUpdateWithRelations() { $super = User::getByUsername('super'); Yii::app()->user->userModel = $super; $authenticationData = $this->login(); $headers = array('Accept: application/xml', 'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'], 'ZURMO_TOKEN: ' . $authenticationData['token'], 'ZURMO_API_REQUEST_TYPE: REST'); $account = AccountTestHelper::createAccountByNameForOwner('Factor X', $super); $account1 = AccountTestHelper::createAccountByNameForOwner('Miko', $super); $account2 = AccountTestHelper::createAccountByNameForOwner('Troter', $super); $contact = ContactTestHelper::createContactByNameForOwner('Simon', $super); $redBeanModelToApiDataUtil = new RedBeanModelToApiDataUtil($account); $compareData = $redBeanModelToApiDataUtil->getData(); $account->forget(); $data['modelRelations'] = array('accounts' => array(array('action' => 'add', 'modelId' => $account1->id, 'modelClassName' => 'Account'), array('action' => 'add', 'modelId' => $account2->id, 'modelClassName' => 'Account')), 'contacts' => array(array('action' => 'add', 'modelId' => $contact->id, 'modelClassName' => 'Contact'))); $data['name'] = 'Zurmo Inc.'; $response = ApiRestTestHelper::createXmlApiCall($this->serverUrl . '/test.php/accounts/account/api/update/' . $compareData['id'], 'PUT', $headers, array('data' => $data)); $response = XML2Array::createArray($response); unset($response['data']['modifiedDateTime']); unset($compareData['modifiedDateTime']); $compareData['name'] = 'Zurmo Inc.'; $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']); $this->assertEquals($compareData, $response['data']); RedBeanModel::forgetAll(); $account = Account::getById($compareData['id']); $this->assertEquals(2, count($account->accounts)); $this->assertEquals($account1->id, $account->accounts[0]->id); $this->assertEquals($account2->id, $account->accounts[1]->id); $this->assertEquals(1, count($account->contacts)); $this->assertEquals($contact->id, $account->contacts[0]->id); $account1 = Account::getById($account1->id); $this->assertEquals($account->id, $account1->account->id); $account2 = Account::getById($account2->id); $this->assertEquals($account->id, $account2->account->id); $contact = Contact::getById($contact->id); $this->assertEquals($account->id, $contact->account->id); // Now test remove relations $data['modelRelations'] = array('accounts' => array(array('action' => 'remove', 'modelId' => $account1->id, 'modelClassName' => 'Account'), array('action' => 'remove', 'modelId' => $account2->id, 'modelClassName' => 'Account')), 'contacts' => array(array('action' => 'remove', 'modelId' => $contact->id, 'modelClassName' => 'Contact'))); $response = ApiRestTestHelper::createXmlApiCall($this->serverUrl . '/test.php/accounts/account/api/update/' . $compareData['id'], 'PUT', $headers, array('data' => $data)); $response = XML2Array::createArray($response); $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']); RedBeanModel::forgetAll(); $updatedModel = Account::getById($compareData['id']); $this->assertEquals(0, count($updatedModel->accounts)); $this->assertEquals(0, count($updatedModel->contacts)); $account1 = Account::getById($account1->id); $this->assertLessThanOrEqual(0, $account1->account->id); $account2 = Account::getById($account2->id); $this->assertLessThanOrEqual(0, $account2->account->id); $contact = Contact::getById($contact->id); $this->assertLessThanOrEqual(0, $contact->account->id); }
/** * @depends testCreateUser */ public function testUpdateUser() { $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'); $user = User::getByUsername('diggy011'); $redBeanModelToApiDataUtil = new RedBeanModelToApiDataUtil($user); $compareData = $redBeanModelToApiDataUtil->getData(); $user->forget(); $data['firstName'] = "John"; $data['password'] = "******"; $response = ApiRestTestHelper::createApiCall($this->serverUrl . '/test.php/users/user/api/update/' . $compareData['id'], 'PUT', $headers, array('data' => $data)); $response = json_decode($response, true); $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']); // We need to unset some empty values from response and dates. unset($response['data']['modifiedDateTime']); unset($compareData['modifiedDateTime']); unset($response['data']['lastLoginDateTime']); unset($compareData['lastLoginDateTime']); $compareData['firstName'] = "John"; ksort($compareData); ksort($response['data']); $this->assertEquals($compareData, $response['data']); // Check if password is updated RedBeanModel::forgetAll(); $updatedUser = User::getByUsername('diggy011'); $this->assertEquals(User::encryptPassword($data['password']), $updatedUser->hash); $response = ApiRestTestHelper::createApiCall($this->serverUrl . '/test.php/users/user/api/read/' . $user->id, 'GET', $headers); $response = json_decode($response, true); $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']); unset($response['data']['modifiedDateTime']); unset($response['data']['lastLoginDateTime']); ksort($response['data']); $this->assertEquals($compareData, $response['data']); }
/** * Get states by type. * @param string $state * @throws ApiException */ protected function sendStatesByLeadOrContact($state = 'contact') { try { $states = array(); if ($state == 'contact') { $states = ContactsUtil::getContactStateDataFromStartingStateLabelByLanguage(Yii::app()->language); } elseif ($state == 'lead') { $states = LeadsUtil::getLeadStateDataFromStartingStateLabelByLanguage(Yii::app()->language); } foreach ($states as $model) { $redBeanModelToApiDataUtil = new RedBeanModelToApiDataUtil($model); $data['items'][] = $redBeanModelToApiDataUtil->getData(); } $data['totalCount'] = count($data['items']); $data['currentPage'] = 1; $result = new ApiResult(ApiResponse::STATUS_SUCCESS, $data, null, null); Yii::app()->apiHelper->sendResponse($result); } catch (Exception $e) { $message = $e->getMessage(); throw new ApiException($message); } }
/** * @depends testCreateWithRelations */ 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'); $contact = ContactTestHelper::createContactByNameForOwner('Mark', $super); $contactItemId = $contact->getClassId('Item'); $note = NoteTestHelper::createNoteByNameForOwner('Note update test.', $super); $redBeanModelToApiDataUtil = new RedBeanModelToApiDataUtil($note); $compareData = $redBeanModelToApiDataUtil->getData(); $this->assertEquals(0, count($note->activityItems)); $note->forget(); $data['modelRelations'] = array('activityItems' => array(array('action' => 'add', 'modelId' => $contact->id, 'modelClassName' => 'Contact'))); $data['description'] = "Updated note description"; $response = ApiRestTestHelper::createApiCall($this->serverUrl . '/test.php/notes/note/api/update/' . $compareData['id'], 'PUT', $headers, array('data' => $data)); $response = json_decode($response, true); unset($response['data']['modifiedDateTime']); unset($compareData['modifiedDateTime']); $compareData['description'] = "Updated note description"; $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']); $this->assertEquals($compareData, $response['data']); RedBeanModel::forgetAll(); $note = Note::getById($compareData['id']); $this->assertEquals(1, count($note->activityItems)); $this->assertEquals($contactItemId, $note->activityItems[0]->id); // Now test remove relations $data['modelRelations'] = array('activityItems' => array(array('action' => 'remove', 'modelId' => $contact->id, 'modelClassName' => 'Contact'))); $response = ApiRestTestHelper::createApiCall($this->serverUrl . '/test.php/notes/note/api/update/' . $compareData['id'], 'PUT', $headers, array('data' => $data)); $response = json_decode($response, true); $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']); RedBeanModel::forgetAll(); $note = Note::getById($compareData['id']); $this->assertEquals(0, count($note->activityItems)); }
/** * @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()); $redBeanModelToApiDataUtil = new RedBeanModelToApiDataUtil($testModel); $compareData = $redBeanModelToApiDataUtil->getData(); $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'; $response = ApiRestTestHelper::createApiCall($this->serverUrl . '/test.php/api/testModelItem/api/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 = ApiRestTestHelper::createApiCall($this->serverUrl . '/test.php/api/testModelItem/api/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 = ApiRestTestHelper::createApiCall($this->serverUrl . '/test.php/api/testModelItem/api/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 = ApiRestTestHelper::createApiCall($this->serverUrl . '/test.php/api/testModelItem/api/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 = ApiRestTestHelper::createApiCall($this->serverUrl . '/test.php/api/testModelItem/api/update/' . $compareData['id'], 'PUT', $headers, array('data' => $data)); $response = json_decode($response, true); $this->assertEquals(ApiResponse::STATUS_FAILURE, $response['status']); }
/** * Update model * @param int $id * @param array $data * @throws ApiException * @return ApiResult */ protected function processUpdate($id, $data) { assert('is_int($id)'); $modelClassName = $this->getModelName(); if (isset($data['modelRelations'])) { $modelRelations = $data['modelRelations']; unset($data['modelRelations']); } try { $model = $modelClassName::getById($id); } catch (NotFoundException $e) { $message = Zurmo::t('ZurmoModule', 'The ID specified was invalid.'); throw new ApiException($message); } try { ApiControllerSecurityUtil::resolveAccessCanCurrentUserWriteModel($model); } catch (SecurityException $e) { $message = $e->getMessage(); throw new ApiException($message); } try { $model = $this->attemptToSaveModelFromData($model, $data, null, false); $id = $model->id; if (!count($model->getErrors())) { if (isset($modelRelations) && count($modelRelations)) { try { $this->manageModelRelations($model, $modelRelations); $model->save(); } catch (Exception $e) { $message = Zurmo::t('ZurmoModule', 'Model was updated, but there were issues with relations.'); $message .= ' ' . $e->getMessage(); throw new ApiException($message); } } $model = $modelClassName::getById($id); $redBeanModelToApiDataUtil = new RedBeanModelToApiDataUtil($model); $data = $redBeanModelToApiDataUtil->getData(); $result = new ApiResult(ApiResponse::STATUS_SUCCESS, $data, null, null); } else { $errors = $model->getErrors(); $message = Zurmo::t('ZurmoModule', 'Model was not updated.'); // To-Do: How to pass $errors and $message to exception //throw new ApiException($message); $result = new ApiResult(ApiResponse::STATUS_FAILURE, null, $message, $errors); } } catch (Exception $e) { $message = $e->getMessage(); throw new ApiException($message); } return $result; }
/** * @depends testCreateWithRelations */ 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'); $contact = ContactTestHelper::createContactByNameForOwner('Simon', $super); $opportunity = OpportunityTestHelper::createOpportunityByNameForOwner('My Opportunity', $super); $redBeanModelToApiDataUtil = new RedBeanModelToApiDataUtil($contact); $compareData = $redBeanModelToApiDataUtil->getData(); $contact->forget(); $data['modelRelations'] = array('opportunities' => array(array('action' => 'add', 'modelId' => $opportunity->id, 'modelClassName' => 'Opportunity'))); $data['firstName'] = 'Fred'; $response = ApiRestTestHelper::createApiCall($this->serverUrl . '/test.php/contacts/contact/api/update/' . $compareData['id'], 'PUT', $headers, array('data' => $data)); $response = json_decode($response, true); unset($response['data']['modifiedDateTime']); unset($compareData['modifiedDateTime']); $compareData['firstName'] = 'Fred'; $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']); $this->assertEquals($compareData, $response['data']); RedBeanModel::forgetAll(); $contact = Contact::getById($compareData['id']); $this->assertEquals(1, count($contact->opportunities)); $this->assertEquals($opportunity->id, $contact->opportunities[0]->id); $opportunity = Opportunity::getById($opportunity->id); $this->assertEquals(1, count($opportunity->contacts)); $this->assertEquals($contact->id, $opportunity->contacts[0]->id); // Now test remove relations $data['modelRelations'] = array('opportunities' => array(array('action' => 'remove', 'modelId' => $opportunity->id, 'modelClassName' => 'Opportunity'))); $response = ApiRestTestHelper::createApiCall($this->serverUrl . '/test.php/contacts/contact/api/update/' . $compareData['id'], 'PUT', $headers, array('data' => $data)); $response = json_decode($response, true); $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']); RedBeanModel::forgetAll(); $contact = Contact::getById($compareData['id']); $this->assertEquals(0, count($contact->opportunities)); $opportunity = Opportunity::getById($opportunity->id); $this->assertEquals(0, count($opportunity->contacts)); }