/** * @depends testUnprivilegedUserViewUpdateDeleteMeetings */ public function testBasicSearchMeetings() { $super = User::getByUsername('super'); Yii::app()->user->userModel = $super; Meeting::deleteAll(); $anotherUser = User::getByUsername('steven'); $authenticationData = $this->login(); $headers = array('Accept: application/json', 'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'], 'ZURMO_TOKEN: ' . $authenticationData['token'], 'ZURMO_API_REQUEST_TYPE: REST'); $firstAccount = AccountTestHelper::createAccountByNameTypeAndIndustryForOwner('First Account', 'Customer', 'Automotive', $super); $secondAccount = AccountTestHelper::createAccountByNameTypeAndIndustryForOwner('Second Account', 'Customer', 'Automotive', $super); MeetingTestHelper::createMeetingWithOwnerAndRelatedAccount('First Meeting', $super, $firstAccount); $secondMeeting = MeetingTestHelper::createMeetingWithOwnerAndRelatedAccount('Second Meeting', $super, $firstAccount); MeetingTestHelper::createMeetingWithOwnerAndRelatedAccount('Third Meeting', $super, $secondAccount); MeetingTestHelper::createMeetingWithOwnerAndRelatedAccount('Forth Meeting', $anotherUser, $secondAccount); MeetingTestHelper::createMeetingWithOwnerAndRelatedAccount('Fifth Meeting', $super, $firstAccount); $billy = User::getByUsername('billy'); $secondMeeting->userAttendees->add($billy); $this->assertTrue($secondMeeting->save()); $searchParams = array('pagination' => array('page' => 1, 'pageSize' => 3), 'search' => array('name' => ''), 'sort' => 'name'); $searchParamsQuery = http_build_query($searchParams); $response = $this->createApiCallWithRelativeUrl('list/filter/' . $searchParamsQuery, 'GET', $headers); $response = json_decode($response, true); $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']); $this->assertEquals(3, count($response['data']['items'])); $this->assertEquals(5, $response['data']['totalCount']); $this->assertEquals(1, $response['data']['currentPage']); $this->assertEquals('Fifth Meeting', $response['data']['items'][0]['name']); $this->assertEquals('First Meeting', $response['data']['items'][1]['name']); $this->assertEquals('Forth Meeting', $response['data']['items'][2]['name']); // Second page $searchParams['pagination']['page'] = 2; $searchParamsQuery = http_build_query($searchParams); $response = $this->createApiCallWithRelativeUrl('list/filter/' . $searchParamsQuery, 'GET', $headers); $response = json_decode($response, true); $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']); $this->assertEquals(2, count($response['data']['items'])); $this->assertEquals(5, $response['data']['totalCount']); $this->assertEquals(2, $response['data']['currentPage']); $this->assertEquals('Second Meeting', $response['data']['items'][0]['name']); $this->assertEquals('Third Meeting', $response['data']['items'][1]['name']); // Search by name $searchParams['pagination']['page'] = 1; $searchParams['search']['name'] = 'First Meeting'; $searchParamsQuery = http_build_query($searchParams); $response = $this->createApiCallWithRelativeUrl('list/filter/' . $searchParamsQuery, '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('First Meeting', $response['data']['items'][0]['name']); // No results $searchParams['pagination']['page'] = 1; $searchParams['search']['name'] = 'First Meeting 2'; $searchParamsQuery = http_build_query($searchParams); $response = $this->createApiCallWithRelativeUrl('list/filter/' . $searchParamsQuery, 'GET', $headers); $response = json_decode($response, true); $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']); $this->assertEquals(0, $response['data']['totalCount']); $this->assertFalse(isset($response['data']['items'])); // Search by name desc. $searchParams = array('pagination' => array('page' => 1, 'pageSize' => 3), 'search' => array('name' => ''), 'sort' => 'name.desc'); $searchParamsQuery = http_build_query($searchParams); $response = $this->createApiCallWithRelativeUrl('list/filter/' . $searchParamsQuery, 'GET', $headers); $response = json_decode($response, true); $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']); $this->assertEquals(3, count($response['data']['items'])); $this->assertEquals(5, $response['data']['totalCount']); $this->assertEquals(1, $response['data']['currentPage']); $this->assertEquals('Third Meeting', $response['data']['items'][0]['name']); $this->assertEquals('Second Meeting', $response['data']['items'][1]['name']); $this->assertEquals('Forth Meeting', $response['data']['items'][2]['name']); // Second page $searchParams['pagination']['page'] = 2; $searchParamsQuery = http_build_query($searchParams); $response = $this->createApiCallWithRelativeUrl('list/filter/' . $searchParamsQuery, 'GET', $headers); $response = json_decode($response, true); $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']); $this->assertEquals(2, count($response['data']['items'])); $this->assertEquals(5, $response['data']['totalCount']); $this->assertEquals(2, $response['data']['currentPage']); $this->assertEquals('First Meeting', $response['data']['items'][0]['name']); $this->assertEquals('Fifth Meeting', $response['data']['items'][1]['name']); // Search by custom fields, order by name desc $searchParams = array('pagination' => array('page' => 1, 'pageSize' => 3), 'search' => array('owner' => array('id' => $super->id)), 'sort' => 'name.desc'); $searchParamsQuery = http_build_query($searchParams); $response = $this->createApiCallWithRelativeUrl('list/filter/' . $searchParamsQuery, 'GET', $headers); $response = json_decode($response, true); $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']); $this->assertEquals(4, $response['data']['totalCount']); $this->assertEquals(3, count($response['data']['items'])); $this->assertEquals(1, $response['data']['currentPage']); $this->assertEquals('Third Meeting', $response['data']['items'][0]['name']); $this->assertEquals('Second Meeting', $response['data']['items'][1]['name']); $this->assertEquals('First Meeting', $response['data']['items'][2]['name']); // Search by account, order by name desc $searchParams = array('pagination' => array('page' => 1, 'pageSize' => 3), 'search' => array('activityItems' => array('id' => $firstAccount->getClassId('Item'))), 'sort' => 'name.desc'); $searchParamsQuery = http_build_query($searchParams); $response = $this->createApiCallWithRelativeUrl('list/filter/' . $searchParamsQuery, 'GET', $headers); $response = json_decode($response, true); $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']); $this->assertEquals(3, $response['data']['totalCount']); $this->assertEquals(3, count($response['data']['items'])); $this->assertEquals(1, $response['data']['currentPage']); $this->assertEquals('Second Meeting', $response['data']['items'][0]['name']); $this->assertEquals('First Meeting', $response['data']['items'][1]['name']); $this->assertEquals('Fifth Meeting', $response['data']['items'][2]['name']); }