public static function setUpBeforeClass()
 {
     parent::setUpBeforeClass();
     SecurityTestHelper::createSuperAdmin();
     $super = User::getByUsername('super');
     Yii::app()->user->userModel = $super;
     //Setup test data owned by the super user.
     $account = AccountTestHelper::createAccountByNameForOwner('superAccount', $super);
     AccountTestHelper::createAccountByNameForOwner('superAccount2', $super);
     ContactTestHelper::createContactWithAccountByNameForOwner('superContact', $super, $account);
     ContactTestHelper::createContactWithAccountByNameForOwner('superContact2', $super, $account);
     ContractTestHelper::createContractStagesIfDoesNotExist();
     ContractTestHelper::createContractWithAccountByNameForOwner('superOpp', $super, $account);
     ContractTestHelper::createContractWithAccountByNameForOwner('superOpp2', $super, $account);
     ContractTestHelper::createContractWithAccountByNameForOwner('superOpp3', $super, $account);
     ContractTestHelper::createContractWithAccountByNameForOwner('superOpp4', $super, $account);
     ContractTestHelper::createContractWithAccountByNameForOwner('superOpp5', $super, $account);
     ContractTestHelper::createContractWithAccountByNameForOwner('superOpp6', $super, $account);
     ContractTestHelper::createContractWithAccountByNameForOwner('superOpp7', $super, $account);
     ContractTestHelper::createContractWithAccountByNameForOwner('superOpp8', $super, $account);
     ContractTestHelper::createContractWithAccountByNameForOwner('superOpp9', $super, $account);
     ContractTestHelper::createContractWithAccountByNameForOwner('superOpp10', $super, $account);
     ContractTestHelper::createContractWithAccountByNameForOwner('superOpp11', $super, $account);
     ContractTestHelper::createContractWithAccountByNameForOwner('superOpp12', $super, $account);
     //Setup default dashboard.
     Dashboard::getByLayoutIdAndUser(Dashboard::DEFAULT_USER_LAYOUT_ID, $super);
 }
 public static function setUpBeforeClass()
 {
     parent::setUpBeforeClass();
     SecurityTestHelper::createSuperAdmin();
     $super = User::getByUsername('super');
     Yii::app()->user->userModel = $super;
     //Setup test data owned by the super user.
     $account = AccountTestHelper::createAccountByNameForOwner('superAccount', $super);
     AccountTestHelper::createAccountByNameForOwner('superAccount2', $super);
     ContractTestHelper::createContractStagesIfDoesNotExist();
     ContractTestHelper::createContractWithAccountByNameForOwner('superOpp', $super, $account);
 }
 public static function setUpBeforeClass()
 {
     parent::setUpBeforeClass();
     SecurityTestHelper::createSuperAdmin();
     $super = User::getByUsername('super');
     Yii::app()->user->userModel = $super;
     Currency::makeBaseCurrency();
     //Create a account for testing.
     $account = AccountTestHelper::createAccountByNameForOwner('superAccount', $super);
     //Create a Contract for testing.
     ContractTestHelper::createContractWithAccountByNameForOwner('superOpp', $super, $account);
 }
 /**
  * Walkthrough test for synchronous download
  */
 public function testDownloadDefaultControllerActions()
 {
     $super = $this->logoutCurrentUserLoginNewUserAndGetByUsername('super');
     $account = AccountTestHelper::createAccountByNameForOwner('superAccount', $super);
     $contracts = array();
     for ($i = 0; $i < 2; $i++) {
         $contracts[] = ContractTestHelper::createContractWithAccountByNameForOwner('superOpp' . $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('contracts/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('ContractsModule', ContractsModule::RIGHT_ACCESS_CONTRACTS);
     $nobody->setRight('ContractsModule', ContractsModule::RIGHT_CREATE_CONTRACTS);
     $nobody->setRight('ContractsModule', ContractsModule::RIGHT_DELETE_CONTRACTS);
     $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('contracts/default/list');
     $this->setGetArray(array('Contract_page' => '1', 'export' => '', 'ajax' => '', 'selectAll' => '', 'selectedIds' => ''));
     $response = $this->runControllerWithRedirectExceptionAndGetUrl('contracts/default/export');
     $this->assertTrue(strstr($response, 'contracts/default/index') !== false);
     $this->setGetArray(array('ContractsSearchForm' => array('anyMixedAttributesScope' => array(0 => 'All'), 'anyMixedAttributes' => '', 'name' => 'superOpp', 'officePhone' => ''), 'Contract_page' => '1', 'export' => '', 'ajax' => '', 'selectAll' => '1', 'selectedIds' => ''));
     $response = $this->runControllerWithRedirectExceptionAndGetUrl('contracts/default/export');
     $this->assertTrue(strstr($response, 'contracts/default/index') !== false);
     $this->setGetArray(array('ContractsSearchForm' => array('anyMixedAttributesScope' => array(0 => 'All'), 'anyMixedAttributes' => '', 'name' => 'superOpp', 'officePhone' => ''), 'Contract_page' => '1', 'export' => '', 'ajax' => '', 'selectAll' => '', 'selectedIds' => "{$contracts[0]->id}, {$contracts[1]->id}"));
     $response = $this->runControllerWithRedirectExceptionAndGetUrl('contracts/default/export');
     $this->assertTrue(strstr($response, 'contracts/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 ($contracts as $contract) {
         $contract->addPermissions($nobody, Permission::READ_WRITE_CHANGE_PERMISSIONS);
         AllPermissionsOptimizationUtil::securableItemGivenPermissionsForUser($contract, $nobody);
         $this->assertTrue($contract->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('ContractsSearchForm' => array('anyMixedAttributesScope' => array(0 => 'All'), 'anyMixedAttributes' => '', 'name' => 'superOpp', 'officePhone' => ''), 'Contract_page' => '1', 'export' => '', 'ajax' => '', 'selectAll' => '1', 'selectedIds' => ''));
     $response = $this->runControllerWithExitExceptionAndGetContent('contracts/default/export');
     $this->assertEquals('Testing download.', $response);
     $this->setGetArray(array('ContractsSearchForm' => array('anyMixedAttributesScope' => array(0 => 'All'), 'anyMixedAttributes' => '', 'name' => 'superOpp', 'officePhone' => ''), 'Contract_page' => '1', 'export' => '', 'ajax' => '', 'selectAll' => '', 'selectedIds' => "{$contracts[0]->id}, {$contracts[1]->id}"));
     $response = $this->runControllerWithExitExceptionAndGetContent('contracts/default/export');
     $this->assertEquals('Testing download.', $response);
     // No mathces
     $this->setGetArray(array('ContractsSearchForm' => array('anyMixedAttributesScope' => array(0 => 'All'), 'anyMixedAttributes' => '', 'name' => 'missingName', 'officePhone' => ''), 'Contract_page' => '1', 'export' => '', 'ajax' => '', 'selectAll' => '1', 'selectedIds' => ''));
     $response = $this->runControllerWithRedirectExceptionAndGetUrl('contracts/default/export');
     $this->assertTrue(strstr($response, 'contracts/default/index') !== false);
 }
 /**
  * Walkthrough test for synchronous download
  */
 public function testAsynchronousDownloadDefaultControllerActions()
 {
     $super = $this->logoutCurrentUserLoginNewUserAndGetByUsername('super');
     $account = AccountTestHelper::createAccountByNameForOwner('superAccount2', $super);
     $notificationsBeforeCount = Notification::getCount();
     $notificationMessagesBeforeCount = NotificationMessage::getCount();
     $contracts = Contract::getAll();
     if (count($contracts)) {
         foreach ($contracts as $contract) {
             $contract->delete();
         }
     }
     $contracts = array();
     for ($i = 0; $i <= ExportModule::$asynchronousThreshold + 1; $i++) {
         $contracts[] = ContractTestHelper::createContractWithAccountByNameForOwner('contract' . $i, $super, $account);
     }
     $this->setGetArray(array('Contract_page' => '1', 'export' => '', 'ajax' => '', 'selectAll' => '1', 'selectedIds' => ''));
     $this->runControllerWithRedirectExceptionAndGetUrl('contracts/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('contracts', $exportItems[0]->exportFileName);
     $this->assertTrue($fileModel instanceof ExportFileModel);
     $this->assertEquals($notificationsBeforeCount + 1, Notification::getCount());
     $this->assertEquals($notificationMessagesBeforeCount + 1, NotificationMessage::getCount());
     // Check export job, when many ids are selected.
     // This will probably never happen, but we need test for this case too.
     $notificationsBeforeCount = Notification::getCount();
     $notificationMessagesBeforeCount = NotificationMessage::getCount();
     // Now test case when multiple ids are selected
     $exportItems = ExportItem::getAll();
     if (count($exportItems)) {
         foreach ($exportItems as $exportItem) {
             $exportItem->delete();
         }
     }
     $selectedIds = "";
     foreach ($contracts as $contract) {
         $selectedIds .= $contract->id . ",";
         // Not Coding Standard
     }
     $this->setGetArray(array('ContractsSearchForm' => array('anyMixedAttributesScope' => array(0 => 'All'), 'anyMixedAttributes' => '', 'name' => '', 'officePhone' => ''), 'Contract_page' => '1', 'export' => '', 'ajax' => '', 'selectAll' => '', 'selectedIds' => "{$selectedIds}"));
     $this->runControllerWithRedirectExceptionAndGetUrl('contracts/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('contracts', $exportItems[0]->exportFileName);
     $this->assertTrue($fileModel instanceof ExportFileModel);
     $this->assertEquals($notificationsBeforeCount + 1, Notification::getCount());
     $this->assertEquals($notificationMessagesBeforeCount + 1, NotificationMessage::getCount());
 }
 /**
  * @depends testRegularUserControllerActionsWithElevationToModels
  */
 public function testRegularUserViewingContractWithoutAccessToAccount()
 {
     $super = $this->logoutCurrentUserLoginNewUserAndGetByUsername('super');
     $aUser = UserTestHelper::createBasicUser('aUser');
     $aUser->setRight('ContractsModule', ContractsModule::RIGHT_ACCESS_CONTRACTS);
     $aUser->setRight('AccountsModule', AccountsModule::RIGHT_ACCESS_ACCOUNTS);
     $this->assertTrue($aUser->save());
     $aUser = User::getByUsername('aUser');
     $account = AccountTestHelper::createAccountByNameForOwner('superTestAccount', $super);
     $contract = ContractTestHelper::createContractWithAccountByNameForOwner('contractOwnedByaUser', $aUser, $account);
     $account->forget();
     $id = $contract->id;
     $contract->forget();
     unset($contract);
     $this->logoutCurrentUserLoginNewUserAndGetByUsername('aUser');
     $content = $this->runControllerWithNoExceptionsAndGetContent('contracts/default');
     $this->assertNotContains('Fatal error: Method Account::__toString() must not throw an exception', $content);
 }