/** * Get array of deleted items since beginning or since datetime in past * @param array $params * @return ApiResult * @throws ApiException */ protected function processGetDeletedItems($params) { $modelClassName = $this->getModelName(); $stateMetadataAdapterClassName = $this->resolveStateMetadataAdapterClassName(); if (!isset($params['sinceTimestamp'])) { $sinceTimestamp = 0; } else { $sinceTimestamp = (int) $params['sinceTimestamp']; } $pageSize = Yii::app()->pagination->getGlobalValueByType('apiListPageSize'); if (isset($params['pagination']['pageSize'])) { $pageSize = (int) $params['pagination']['pageSize']; } // Get offset. Please note that API client provide page number, and we need to convert it into offset, // which is parameter of RedBeanModel::getSubset function if (isset($params['pagination']['page']) && (int) $params['pagination']['page'] > 0) { $currentPage = (int) $params['pagination']['page']; } else { $currentPage = 1; } $offset = $this->getOffsetFromCurrentPageAndPageSize($currentPage, $pageSize); $modelIds = ModelStateChangesSubscriptionUtil::getDeletedModelIds('API', $modelClassName, $pageSize, $offset, $sinceTimestamp, $stateMetadataAdapterClassName); $totalItems = ModelStateChangesSubscriptionUtil::getDeletedModelsCount('API', $modelClassName, $sinceTimestamp, $stateMetadataAdapterClassName); $data = array('totalCount' => $totalItems, 'pageSize' => $pageSize, 'currentPage' => $currentPage); if ($totalItems > 0 && is_array($modelIds) && !empty($modelIds)) { foreach ($modelIds as $modelId) { $data['items'][] = $modelId; } $result = new ApiResult(ApiResponse::STATUS_SUCCESS, $data, null, null); } else { $result = new ApiResult(ApiResponse::STATUS_SUCCESS, $data, null, null); } return $result; }
public function testGetDeletedModelIds() { ReadPermissionsSubscriptionUtil::recreateTable('account_read_subscription'); ModelCreationApiSyncUtil::buildTable(); $timestamp = time(); sleep(1); $account1 = AccountTestHelper::createAccountByNameForOwner('First Test Delete Account', Yii::app()->user->userModel); ReadPermissionsSubscriptionUtil::updateAllReadSubscriptionTables(); $models = ModelStateChangesSubscriptionUtil::getDeletedModelIds('apiTest', 'Account', 2, 0, $timestamp); $this->assertTrue(is_array($models)); $this->assertTrue(empty($models)); $account1Id = $account1->id; $this->assertTrue($account1->delete()); $models = ModelStateChangesSubscriptionUtil::getDeletedModelIds('apiTest', 'Account', 2, 0, $timestamp); $this->assertTrue(is_array($models)); $this->assertTrue(empty($models)); ReadPermissionsSubscriptionUtil::updateAllReadSubscriptionTables(); $models = ModelStateChangesSubscriptionUtil::getDeletedModelIds('apiTest', 'Account', 2, 0, $timestamp); $this->assertTrue(is_array($models)); $this->assertEquals(1, count($models)); $this->assertEquals($account1Id, $models[0]); // Check pagination sleep(1); $timestamp = time(); sleep(1); $account2 = AccountTestHelper::createAccountByNameForOwner('Second Test Delete Account', Yii::app()->user->userModel); $account3 = AccountTestHelper::createAccountByNameForOwner('Third Test Delete Account', Yii::app()->user->userModel); $account4 = AccountTestHelper::createAccountByNameForOwner('Forth Test Delete Account', Yii::app()->user->userModel); ReadPermissionsSubscriptionUtil::updateAllReadSubscriptionTables(); $account2Id = $account2->id; $account3Id = $account3->id; $account4Id = $account4->id; $this->assertTrue($account2->delete()); $this->assertTrue($account3->delete()); $this->assertTrue($account4->delete()); ReadPermissionsSubscriptionUtil::updateAllReadSubscriptionTables(); $models = ModelStateChangesSubscriptionUtil::getDeletedModelIds('apiTest', 'Account', 2, 0, $timestamp); $this->assertTrue(is_array($models)); $this->assertEquals(2, count($models)); $this->assertEquals($account2Id, $models[0]); $this->assertEquals($account3Id, $models[1]); // Second page $models = ModelStateChangesSubscriptionUtil::getDeletedModelIds('apiTest', 'Account', 2, 2, $timestamp); $this->assertTrue(is_array($models)); $this->assertEquals(1, count($models)); $this->assertEquals($account4Id, $models[0]); }