public function testLanguage() { $super = User::getByUsername('super'); Yii::app()->user->userModel = $super; $model = new ApiTestModelItem2(); $model->name = 'sample name'; $this->assertTrue($model->save()); $headers = array('Accept: application/json', 'ZURMO_API_REQUEST_TYPE: REST', 'ZURMO_LANG: fr'); $response = $this->createApiCallWithRelativeUrl('read/2/', 'GET', $headers); $response = json_decode($response, true); $this->assertEquals(ApiResponse::STATUS_FAILURE, $response['status']); $this->assertEquals('Sign in required.', $response['message']); $authenticationData = $this->login(); $headers = array('Accept: application/json', 'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'], 'ZURMO_TOKEN: ' . $authenticationData['token'], 'ZURMO_API_REQUEST_TYPE: REST', 'ZURMO_LANG: fr'); $response = $this->createApiCallWithRelativeUrl('read/2/', 'GET', $headers); $response = json_decode($response, true); $this->assertEquals(ApiResponse::STATUS_FAILURE, $response['status']); $this->assertEquals('The ID specified was invalid.', $response['message']); }
/** * @depends testCreate */ public function testCreateWithSpecificExplicitPermissions() { $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'); $currencies = Currency::getAll(); $currencyValue = new CurrencyValue(); $currencyValue->value = 100; $currencyValue->currency = $currencies[0]; $this->assertEquals('USD', $currencyValue->currency->code); $testItem2 = new ApiTestModelItem2(); $testItem2->name = 'John3'; $this->assertTrue($testItem2->save()); $testItem4 = new ApiTestModelItem4(); $testItem4->name = 'John3'; $this->assertTrue($testItem4->save()); //HAS_MANY and MANY_MANY relationships should be ignored. $testItem3_1 = new ApiTestModelItem3(); $testItem3_1->name = 'Kevin3'; $this->assertTrue($testItem3_1->save()); $testItem3_2 = new ApiTestModelItem3(); $testItem3_2->name = 'Jim3'; $this->assertTrue($testItem3_2->save()); $testItemRelated = new ApiTestModelItem(); $testItemRelated->lastName = 'AAAA3'; $testItemRelated->string = 'some string3'; $this->assertTrue($testItemRelated->save()); $testItem = new ApiTestModelItem(); $testItem->firstName = 'Bob5 with owner only'; $testItem->lastName = 'Bob53'; $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 = 'aString3'; $testItem->textArea = 'Some Text Area3'; $testItem->url = 'http://www.asite.com'; $testItem->currencyValue = $currencyValue; $testItem->modelItem2 = $testItem2; $testItem->modelItems3->add($testItem3_1); $testItem->modelItems3->add($testItem3_2); $testItem->modelItems4->add($testItem4); $testItem->modelItems->add($testItemRelated); $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); $this->assertTrue($testItem->save()); $data = $this->getModelToApiDataUtilData($testItem); unset($data['createdDateTime']); unset($data['modifiedDateTime']); unset($data['id']); unset($data['currencyValue']['id']); $data['owner'] = array('id' => $super->id); $compareData = $data; unset($data['createdByUser']); unset($data['modifiedByUser']); $this->assertTrue($testItemRelated->delete()); $testItem->delete(); $testItem->forget(); unset($testItem); // TODO: @Shoaibi/@Ivica: null does not work, empty works. null doesn't send it. $data['explicitReadWriteModelPermissions'] = array('nonEveryoneGroup' => '', 'type' => ''); $response = $this->createApiCallWithRelativeUrl('create/', 'POST', $headers, array('data' => $data)); $response = json_decode($response, true); $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']); $this->assertTrue(is_int($response['data']['id'])); $this->assertGreaterThan(0, $response['data']['id']); $modelId = $response['data']['id']; $this->assertArrayHasKey('owner', $response['data']); $this->assertCount(2, $response['data']['owner']); $this->assertArrayHasKey('id', $response['data']['owner']); $this->assertEquals($super->id, $response['data']['owner']['id']); $this->assertArrayHasKey('explicitReadWriteModelPermissions', $response['data']); $this->assertCount(2, $response['data']['explicitReadWriteModelPermissions']); $this->assertArrayHasKey('type', $response['data']['explicitReadWriteModelPermissions']); $this->assertEquals('', $response['data']['explicitReadWriteModelPermissions']['type']); // following also works. wonder why. //$this->assertTrue(null === $response['data']['explicitReadWriteModelPermissions']['type']); $this->assertArrayHasKey('nonEveryoneGroup', $response['data']['explicitReadWriteModelPermissions']); $this->assertEquals('', $response['data']['explicitReadWriteModelPermissions']['nonEveryoneGroup']); // unset explicit permissions, we won't use these in comparison. // compareData's version of permissions came from manual creation, would be none. // response's one came from api, would be default unset($response['data']['explicitReadWriteModelPermissions']); unset($compareData['explicitReadWriteModelPermissions']); unset($response['data']['createdDateTime']); unset($response['data']['modifiedDateTime']); unset($response['data']['id']); unset($response['data']['owner']['username']); unset($compareData['id']); unset($response['data']['currencyValue']['id']); unset($compareData['currencyValue']['id']); unset($compareData['createdDateTime']); unset($compareData['modifiedDateTime']); ksort($compareData); ksort($response['data']); $this->assertEquals($compareData, $response['data']); $response = $this->createApiCallWithRelativeUrl('read/' . $modelId, 'GET', $headers); $response = json_decode($response, true); $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']); $this->assertArrayHasKey('data', $response); $this->assertArrayHasKey('owner', $response['data']); $this->assertCount(2, $response['data']['owner']); $this->assertArrayHasKey('id', $response['data']['owner']); $this->assertEquals($super->id, $response['data']['owner']['id']); $this->assertArrayHasKey('explicitReadWriteModelPermissions', $response['data']); $this->assertCount(2, $response['data']['explicitReadWriteModelPermissions']); $this->assertArrayHasKey('type', $response['data']['explicitReadWriteModelPermissions']); $this->assertEquals('', $response['data']['explicitReadWriteModelPermissions']['type']); $this->assertArrayHasKey('nonEveryoneGroup', $response['data']['explicitReadWriteModelPermissions']); $this->assertEquals('', $response['data']['explicitReadWriteModelPermissions']['nonEveryoneGroup']); }
/** * @depends testCustomUpdateAction */ public function testCustomListAction() { $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'); $testModels = ApiTestModelItem2::getByName('new name 2 with just owner'); $this->assertEquals(1, count($testModels)); $compareData = $this->getModelToApiDataUtilData($testModels[0]); $response = $this->createApiCallWithRelativeUrl('customList/', 'GET', $headers); $response = json_decode($response, true); $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']); $this->assertEquals(3, count($response['data']['items'])); $this->assertEquals(3, $response['data']['totalCount']); $this->assertEquals(1, $response['data']['currentPage']); $this->assertEquals($compareData, $response['data']['items'][1]); }
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 testApiServerUrl */ public function testCreate() { $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'); $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()); $testItemRelated = new ApiTestModelItem(); $testItemRelated->lastName = 'AAAA'; $testItemRelated->string = 'some string'; $this->assertTrue($testItemRelated->save()); $testItem = new ApiTestModelItem(); $testItem->firstName = 'Bob5'; $testItem->lastName = 'Bob5'; $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->currencyValue = $currencyValue; $testItem->modelItem2 = $testItem2; $testItem->modelItems3->add($testItem3_1); $testItem->modelItems3->add($testItem3_2); $testItem->modelItems4->add($testItem4); $testItem->modelItems->add($testItemRelated); $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); $this->assertTrue($testItem->save()); $util = new RedBeanModelToApiDataUtil($testItem); $data = $util->getData(); unset($data['createdDateTime']); unset($data['modifiedDateTime']); unset($data['id']); unset($data['currencyValue']['id']); $data['owner'] = array('id' => $super->id); $compareData = $data; unset($data['createdByUser']); unset($data['modifiedByUser']); $this->assertTrue($testItemRelated->delete()); $testItem->delete(); $testItem->forget(); unset($testItem); $response = ApiRestTestHelper::createApiCall($this->serverUrl . '/test.php/api/testModelItem/api/create/', 'POST', $headers, array('data' => $data)); $response = json_decode($response, true); $id = $response['data']['id']; unset($response['data']['createdDateTime']); unset($response['data']['modifiedDateTime']); unset($response['data']['id']); unset($response['data']['owner']['username']); unset($compareData['id']); unset($response['data']['currencyValue']['id']); unset($compareData['currencyValue']['id']); unset($compareData['createdDateTime']); unset($compareData['modifiedDateTime']); $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']); ksort($compareData); ksort($response['data']); $this->assertEquals($compareData, $response['data']); }
/** * @depends testList */ public function testDelete() { $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'); $testModels = ApiTestModelItem2::getByName('new name 2'); $this->assertEquals(1, count($testModels)); //Test Delete $response = ApiRestTestHelper::createApiCall($this->serverUrl . '/test.php/api/testModelItem2/api/delete/' . $testModels[0]->id, 'DELETE', $headers); $response = json_decode($response, true); $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']); $response = ApiRestTestHelper::createApiCall($this->serverUrl . '/test.php/api/testModelItem2/api/read/' . $testModels[0]->id, 'GET', $headers); $response = json_decode($response, true); $this->assertEquals(ApiResponse::STATUS_FAILURE, $response['status']); }