/**
  * Walkthrough test for synchronous download
  */
 public function testDownloadDefaultControllerActions()
 {
     $super = $this->logoutCurrentUserLoginNewUserAndGetByUsername('super');
     $account = AccountTestHelper::createAccountByNameForOwner('superAccount', $super);
     $leads = array();
     for ($i = 0; $i < 2; $i++) {
         $leads[] = LeadTestHelper::createLeadWithAccountByNameForOwner('superContact' . $i, $super, $account);
     }
     // Check if access is denied if user doesn't have access privileges at all to export actions
     Yii::app()->user->userModel = User::getByUsername('nobody');
     $nobody = $this->logoutCurrentUserLoginNewUserAndGetByUsername('nobody');
     $this->runControllerShouldResultInAccessFailureAndGetContent('leads/default/list');
     // Check if user have access to module action, but not to export action
     // Now test peon with elevated rights to accounts
     $nobody->setRight('LeadsModule', LeadsModule::RIGHT_ACCESS_LEADS);
     $nobody->setRight('LeadsModule', LeadsModule::RIGHT_CREATE_LEADS);
     $nobody->setRight('LeadsModule', LeadsModule::RIGHT_DELETE_LEADS);
     $nobody->setRight('ExportModule', ExportModule::RIGHT_ACCESS_EXPORT);
     $this->assertTrue($nobody->save());
     // Check if access is denied if user doesn't have access privileges at all to export actions
     $nobody = $this->logoutCurrentUserLoginNewUserAndGetByUsername('nobody');
     Yii::app()->user->userModel = User::getByUsername('nobody');
     $this->runControllerWithNoExceptionsAndGetContent('leads/default/list');
     $this->setGetArray(array('Contact_page' => '1', 'export' => '', 'ajax' => '', 'selectAll' => '', 'selectedIds' => ''));
     $response = $this->runControllerWithRedirectExceptionAndGetUrl('leads/default/export');
     $this->assertTrue(strstr($response, 'leads/default/index') !== false);
     $this->setGetArray(array('LeadsSearchForm' => array('anyMixedAttributesScope' => array(0 => 'All'), 'anyMixedAttributes' => '', 'fullName' => 'superContact', 'officePhone' => ''), 'multiselect_ContactsSearchForm_anyMixedAttributesScope' => 'All', 'Contact_page' => '1', 'export' => '', 'ajax' => '', 'selectAll' => '1', 'selectedIds' => ''));
     $response = $this->runControllerWithRedirectExceptionAndGetUrl('leads/default/export');
     $this->assertTrue(strstr($response, 'leads/default/index') !== false);
     $this->setGetArray(array('LeadsSearchForm' => array('anyMixedAttributesScope' => array(0 => 'All'), 'anyMixedAttributes' => '', 'fullName' => 'superContact', 'officePhone' => ''), 'multiselect_ContactsSearchForm_anyMixedAttributesScope' => 'All', 'Contact_page' => '1', 'export' => '', 'ajax' => '', 'selectAll' => '', 'selectedIds' => "{$leads[0]->id}, {$leads[1]->id}"));
     $response = $this->runControllerWithRedirectExceptionAndGetUrl('leads/default/export');
     $this->assertTrue(strstr($response, 'leads/default/index') !== false);
     $this->assertContains('There is no data to export.', Yii::app()->user->getFlash('notification'));
     //give nobody access to read and write
     Yii::app()->user->userModel = $super;
     foreach ($leads as $lead) {
         $lead->addPermissions($nobody, Permission::READ_WRITE_CHANGE_PERMISSIONS);
         ReadPermissionsOptimizationUtil::securableItemGivenPermissionsForUser($lead, $nobody);
         $this->assertTrue($lead->save());
     }
     //Now the nobody user should be able to access the edit view and still the details view.
     Yii::app()->user->userModel = $nobody;
     $this->setGetArray(array('LeadsSearchForm' => array('anyMixedAttributesScope' => array(0 => 'All'), 'anyMixedAttributes' => '', 'fullName' => 'superContact', 'officePhone' => ''), 'multiselect_ContactsSearchForm_anyMixedAttributesScope' => 'All', 'Contact_page' => '1', 'export' => '', 'ajax' => '', 'selectAll' => '1', 'selectedIds' => ''));
     $response = $this->runControllerWithExitExceptionAndGetContent('leads/default/export');
     $this->assertEquals('Testing download.', $response);
     $this->setGetArray(array('LeadsSearchForm' => array('anyMixedAttributesScope' => array(0 => 'All'), 'anyMixedAttributes' => '', 'fullName' => 'superContact', 'officePhone' => ''), 'multiselect_ContactsSearchForm_anyMixedAttributesScope' => 'All', 'Contact_page' => '1', 'export' => '', 'ajax' => '', 'selectAll' => '', 'selectedIds' => "{$leads[0]->id}, {$leads[1]->id}"));
     $response = $this->runControllerWithExitExceptionAndGetContent('leads/default/export');
     $this->assertEquals('Testing download.', $response);
     // No mathces
     $this->setGetArray(array('LeadsSearchForm' => array('anyMixedAttributesScope' => array(0 => 'All'), 'anyMixedAttributes' => '', 'fullName' => 'missingName', 'officePhone' => ''), 'multiselect_ContactsSearchForm_anyMixedAttributesScope' => 'All', 'Contact_page' => '1', 'export' => '', 'ajax' => '', 'selectAll' => '1', 'selectedIds' => ''));
     $response = $this->runControllerWithRedirectExceptionAndGetUrl('leads/default/export');
     $this->assertTrue(strstr($response, 'leads/default/index') !== false);
 }
Beispiel #2
0
 /**
  * @depends testUnprivilegedUserViewUpdateDeleteLead
  */
 public function testSearchLeads()
 {
     $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');
     //Setup test data owned by the super user.
     $account = AccountTestHelper::createAccountByNameForOwner('superAccount', $super);
     $account2 = AccountTestHelper::createAccountByNameForOwner('superAccount2', $super);
     LeadTestHelper::createLeadWithAccountByNameForOwner('First Lead', $super, $account);
     LeadTestHelper::createLeadWithAccountByNameForOwner('Second Lead', $super, $account);
     LeadTestHelper::createLeadWithAccountByNameForOwner('Third Lead', $super, $account);
     LeadTestHelper::createLeadWithAccountByNameForOwner('Forth Lead', $super, $account2);
     LeadTestHelper::createLeadWithAccountByNameForOwner('Fifth Lead', $super, $account2);
     $searchParams = array('pagination' => array('page' => 1, 'pageSize' => 3), 'search' => array('firstName' => ''), 'sort' => 'firstName');
     $searchParamsQuery = http_build_query($searchParams);
     $response = ApiRestTestHelper::createApiCall($this->serverUrl . '/test.php/leads/contact/api/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 Lead', $response['data']['items'][0]['firstName']);
     $this->assertEquals('First Lead', $response['data']['items'][1]['firstName']);
     $this->assertEquals('Forth Lead', $response['data']['items'][2]['firstName']);
     // Second page
     $searchParams['pagination']['page'] = 2;
     $searchParamsQuery = http_build_query($searchParams);
     $response = ApiRestTestHelper::createApiCall($this->serverUrl . '/test.php/leads/contact/api/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 Lead', $response['data']['items'][0]['firstName']);
     $this->assertEquals('Third Lead', $response['data']['items'][1]['firstName']);
     // Search by name
     $searchParams['pagination']['page'] = 1;
     $searchParams['search']['firstName'] = 'First Lead';
     $searchParamsQuery = http_build_query($searchParams);
     $response = ApiRestTestHelper::createApiCall($this->serverUrl . '/test.php/leads/contact/api/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 Lead', $response['data']['items'][0]['firstName']);
     // No results
     $searchParams['pagination']['page'] = 1;
     $searchParams['search']['firstName'] = 'First Lead 2';
     $searchParamsQuery = http_build_query($searchParams);
     $response = ApiRestTestHelper::createApiCall($this->serverUrl . '/test.php/leads/contact/api/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('firstName' => ''), 'sort' => 'firstName.desc');
     $searchParamsQuery = http_build_query($searchParams);
     $response = ApiRestTestHelper::createApiCall($this->serverUrl . '/test.php/leads/contact/api/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 Lead', $response['data']['items'][0]['firstName']);
     $this->assertEquals('Second Lead', $response['data']['items'][1]['firstName']);
     $this->assertEquals('Forth Lead', $response['data']['items'][2]['firstName']);
     // Second page
     $searchParams['pagination']['page'] = 2;
     $searchParamsQuery = http_build_query($searchParams);
     $response = ApiRestTestHelper::createApiCall($this->serverUrl . '/test.php/leads/contact/api/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 Lead', $response['data']['items'][0]['firstName']);
     $this->assertEquals('Fifth Lead', $response['data']['items'][1]['firstName']);
     // Search by custom fields, order by name desc
     $searchParams = array('pagination' => array('page' => 1, 'pageSize' => 3), 'search' => array('account' => array('id' => $account2->id), 'owner' => array('id' => $super->id)), 'sort' => 'firstName.desc');
     $searchParamsQuery = http_build_query($searchParams);
     $response = ApiRestTestHelper::createApiCall($this->serverUrl . '/test.php/leads/contact/api/list/filter/' . $searchParamsQuery, 'GET', $headers);
     $response = json_decode($response, true);
     $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']);
     $this->assertEquals(2, $response['data']['totalCount']);
     $this->assertEquals(2, count($response['data']['items']));
     $this->assertEquals(1, $response['data']['currentPage']);
     $this->assertEquals('Forth Lead', $response['data']['items'][0]['firstName']);
     $this->assertEquals('Fifth Lead', $response['data']['items'][1]['firstName']);
 }
 /**
  * Walkthrough test for synchronous download
  */
 public function testAsynchronousDownloadDefaultControllerActions()
 {
     $super = $this->logoutCurrentUserLoginNewUserAndGetByUsername('super');
     $account = AccountTestHelper::createAccountByNameForOwner('superAccount2', $super);
     $notificationsBeforeCount = count(Notification::getAll());
     $notificationMessagesBeforeCount = count(NotificationMessage::getAll());
     $contacts = Contact::getAll();
     if (count($contacts)) {
         foreach ($contacts as $contact) {
             $contact->delete();
         }
     }
     $leads = array();
     for ($i = 0; $i <= ExportModule::$asynchronusThreshold + 1; $i++) {
         $leads[] = LeadTestHelper::createLeadWithAccountByNameForOwner('superContact' . $i, $super, $account);
     }
     $this->setGetArray(array('Contact_page' => '1', 'export' => '', 'ajax' => '', 'selectAll' => '1', 'selectedIds' => ''));
     $this->runControllerWithRedirectExceptionAndGetUrl('leads/default/export');
     // Start background job
     $job = new ExportJob();
     $this->assertTrue($job->run());
     $exportItems = ExportItem::getAll();
     $this->assertEquals(1, count($exportItems));
     $fileModel = $exportItems[0]->exportFileModel;
     $this->assertEquals(1, $exportItems[0]->isCompleted);
     $this->assertEquals('csv', $exportItems[0]->exportFileType);
     $this->assertEquals('leads', $exportItems[0]->exportFileName);
     $this->assertTrue($fileModel instanceof ExportFileModel);
     $this->assertEquals($notificationsBeforeCount + 1, count(Notification::getAll()));
     $this->assertEquals($notificationMessagesBeforeCount + 1, count(NotificationMessage::getAll()));
     // Check export job, when many ids are selected.
     // This will probably never happen, but we need test for this case too.
     $notificationsBeforeCount = count(Notification::getAll());
     $notificationMessagesBeforeCount = count(NotificationMessage::getAll());
     // Now test case when multiple ids are selected
     $exportItems = ExportItem::getAll();
     if (count($exportItems)) {
         foreach ($exportItems as $exportItem) {
             $exportItem->delete();
         }
     }
     $selectedIds = "";
     foreach ($leads as $lead) {
         $selectedIds .= $lead->id . ",";
         // Not Coding Standard
     }
     $this->setGetArray(array('LeadsSearchForm' => array('anyMixedAttributesScope' => array(0 => 'All'), 'anyMixedAttributes' => '', 'fullName' => '', 'officePhone' => ''), 'multiselect_ContactsSearchForm_anyMixedAttributesScope' => 'All', 'Contact_page' => '1', 'export' => '', 'ajax' => '', 'selectAll' => '', 'selectedIds' => "{$selectedIds}"));
     $this->runControllerWithRedirectExceptionAndGetUrl('leads/default/export');
     // Start background job
     $job = new ExportJob();
     $this->assertTrue($job->run());
     $exportItems = ExportItem::getAll();
     $this->assertEquals(1, count($exportItems));
     $fileModel = $exportItems[0]->exportFileModel;
     $this->assertEquals(1, $exportItems[0]->isCompleted);
     $this->assertEquals('csv', $exportItems[0]->exportFileType);
     $this->assertEquals('leads', $exportItems[0]->exportFileName);
     $this->assertTrue($fileModel instanceof ExportFileModel);
     $this->assertEquals($notificationsBeforeCount + 1, count(Notification::getAll()));
     $this->assertEquals($notificationMessagesBeforeCount + 1, count(NotificationMessage::getAll()));
 }