Exemplo n.º 1
0
 /**
  * Test if background export job generated csv file,
  * check if content of this csv file is correct, and
  * finally check if user got notification message that
  * his downloads are completed.
  */
 public function testRun()
 {
     $super = User::getByUsername('super');
     Yii::app()->user->userModel = $super;
     $numberOfUserNotifications = Notification::getCountByTypeAndUser('ExportProcessCompleted', Yii::app()->user->userModel);
     $account = new Account();
     $account->owner = $super;
     $account->name = 'Test Account';
     $account->officePhone = '1234567890';
     $this->assertTrue($account->save());
     $account = new Account();
     $account->owner = $super;
     $account->name = 'Test Account 2';
     $account->officePhone = '1234567899';
     $this->assertTrue($account->save());
     $account = new Account(false);
     $searchForm = new AccountsSearchForm($account);
     $dataProvider = ExportTestHelper::makeRedBeanDataProvider($searchForm, 'Account', 0, Yii::app()->user->userModel->id);
     $totalItems = $dataProvider->getTotalItemCount();
     $this->assertEquals(2, $totalItems);
     $exportItem = new ExportItem();
     $exportItem->isCompleted = 0;
     $exportItem->exportFileType = 'csv';
     $exportItem->exportFileName = 'test';
     $exportItem->modelClassName = 'Account';
     $exportItem->serializedData = serialize($dataProvider);
     $this->assertTrue($exportItem->save());
     $id = $exportItem->id;
     $exportItem->forget();
     unset($exportItem);
     $job = new ExportJob();
     $this->assertTrue($job->run());
     $exportItem = ExportItem::getById($id);
     $fileModel = $exportItem->exportFileModel;
     $this->assertEquals(1, $exportItem->isCompleted);
     $this->assertEquals('csv', $exportItem->exportFileType);
     $this->assertEquals('test', $exportItem->exportFileName);
     $this->assertTrue($fileModel instanceof ExportFileModel);
     // Get csv string via regular csv export process(directly, not in background)
     // We suppose that csv generated thisway is corrected, this function itself
     // is tested in another test.
     $data = array();
     $rows = $dataProvider->getData();
     $modelToExportAdapter = new ModelToExportAdapter($rows[0]);
     $headerData = $modelToExportAdapter->getHeaderData();
     foreach ($rows as $model) {
         $modelToExportAdapter = new ModelToExportAdapter($model);
         $data[] = $modelToExportAdapter->getData();
     }
     $output = ExportItemToCsvFileUtil::export($data, $headerData, 'test.csv', false);
     $this->assertEquals($output, $fileModel->fileContent->content);
     // Check if user got notification message, and if its type is ExportProcessCompleted
     $this->assertEquals($numberOfUserNotifications + 1, Notification::getCountByTypeAndUser('ExportProcessCompleted', Yii::app()->user->userModel));
 }
 /**
  * @depends testCreateActionForRowsAndColumns
  */
 public function testExportAction()
 {
     $notificationsBeforeCount = Notification::getCount();
     $notificationMessagesBeforeCount = NotificationMessage::getCount();
     $savedReports = SavedReport::getAll();
     $this->assertEquals(2, count($savedReports));
     $this->setGetArray(array('id' => $savedReports[0]->id));
     //Test where there is no data to export
     $this->runControllerWithRedirectExceptionAndGetContent('reports/default/export');
     $this->assertContains('There is no data to export.', Yii::app()->user->getFlash('notification'));
     $reportModelTestItem = new ReportModelTestItem();
     $reportModelTestItem->string = 'string1';
     $reportModelTestItem->lastName = 'xLast1';
     $this->assertTrue($reportModelTestItem->save());
     $reportModelTestItem = new ReportModelTestItem();
     $reportModelTestItem->string = 'string2';
     $reportModelTestItem->lastName = 'xLast2';
     $this->assertTrue($reportModelTestItem->save());
     $content = $this->runControllerWithExitExceptionAndGetContent('reports/default/export');
     $this->assertEquals('Testing download.', $content);
     ExportModule::$asynchronousThreshold = 1;
     $this->runControllerWithRedirectExceptionAndGetUrl('reports/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('reports', $exportItems[0]->exportFileName);
     $this->assertTrue($fileModel instanceof ExportFileModel);
     $this->assertEquals($notificationsBeforeCount + 1, Notification::getCount());
     $this->assertEquals($notificationMessagesBeforeCount + 1, NotificationMessage::getCount());
 }
 public function testExportWithSelectAllForMoreThan10Records()
 {
     $super = $this->logoutCurrentUserLoginNewUserAndGetByUsername('super');
     $accounts = Account::getAll();
     foreach ($accounts as $account) {
         $account->delete();
     }
     $exportItems = ExportItem::getAll();
     foreach ($exportItems as $exportItem) {
         $exportItem->delete();
     }
     $numberOfRecords = rand(12, 100);
     ExportModule::$asynchronusThreshold = $numberOfRecords - 1;
     for ($i = 1; $i <= $numberOfRecords; $i++) {
         $randomData = RandomDataUtil::getRandomDataByModuleAndModelClassNames('AccountsModule', 'Account');
         AccountTestHelper::createAccountByNameForOwner($randomData['names'][$i], $super);
     }
     $this->setGetArray(array('Account_page' => '1', 'export' => '', 'selectAll' => '1', 'selectedIds' => '', 'ajax' => ''));
     $this->runControllerWithRedirectExceptionAndGetUrl('accounts/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('accounts', $exportItems[0]->exportFileName);
     $this->assertTrue($fileModel instanceof ExportFileModel);
     for ($i = 1; $i <= $numberOfRecords; $i++) {
         $this->assertContains($randomData['names'][$i], $fileModel->fileContent->content);
     }
 }
 /**
  * Walkthrough test for synchronous download
  */
 public function testAsynchronousDownloadDefaultControllerActions()
 {
     $super = $this->logoutCurrentUserLoginNewUserAndGetByUsername('super');
     $notificationsBeforeCount = Notification::getCount();
     $notificationMessagesBeforeCount = NotificationMessage::getCount();
     $products = Product::getAll();
     if (count($products)) {
         foreach ($products as $product) {
             $product->delete();
         }
     }
     $products = array();
     for ($i = 0; $i <= ExportModule::$asynchronousThreshold + 1; $i++) {
         $products[] = ProductTestHelper::createProductByNameForOwner('superProduct' . $i, $super);
     }
     $this->setGetArray(array('Product_page' => '1', 'export' => '', 'ajax' => '', 'selectAll' => '1', 'selectedIds' => ''));
     $this->runControllerWithRedirectExceptionAndGetUrl('products/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('products', $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 ($products as $product) {
         $selectedIds .= $product->id . ",";
         // Not Coding Standard
     }
     $this->setGetArray(array('ProductsSearchForm' => array('anyMixedAttributesScope' => array(0 => 'All'), 'anyMixedAttributes' => '', 'quantity' => ''), 'multiselect_ProductsSearchForm_anyMixedAttributesScope' => 'All', 'Product_page' => '1', 'export' => '', 'ajax' => '', 'selectAll' => '', 'selectedIds' => "{$selectedIds}"));
     $this->runControllerWithRedirectExceptionAndGetUrl('products/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('products', $exportItems[0]->exportFileName);
     $this->assertTrue($fileModel instanceof ExportFileModel);
     $this->assertEquals($notificationsBeforeCount + 1, Notification::getCount());
     $this->assertEquals($notificationMessagesBeforeCount + 1, NotificationMessage::getCount());
 }
Exemplo n.º 5
0
 /**
  * @depends testExportByModelIds
  */
 public function testExportReportWithMultiplePagesOfData()
 {
     $super = User::getByUsername('super');
     Yii::app()->user->userModel = $super;
     $numberOfUserNotifications = Notification::getCountByTypeAndUser('ExportProcessCompleted', Yii::app()->user->userModel);
     $report = new Report();
     $report->setType(Report::TYPE_ROWS_AND_COLUMNS);
     $report->setModuleClassName('AccountsModule');
     $report->setFiltersStructure('');
     $displayAttribute = new DisplayAttributeForReportForm('AccountsModule', 'Account', Report::TYPE_ROWS_AND_COLUMNS);
     $displayAttribute->setModelAliasUsingTableAliasName('model1');
     $displayAttribute->attributeIndexOrDerivedType = 'name';
     $report->addDisplayAttribute($displayAttribute);
     $dataProvider = new RowsAndColumnsReportDataProvider($report);
     $exportItem = new ExportItem();
     $exportItem->isCompleted = 0;
     $exportItem->exportFileType = 'csv';
     $exportItem->exportFileName = 'rowAndColumnsTest2';
     $exportItem->modelClassName = 'SavedReport';
     $exportItem->serializedData = ExportUtil::getSerializedDataForExport($dataProvider);
     $this->assertTrue($exportItem->save());
     $id = $exportItem->id;
     $exportItem->forget();
     unset($exportItem);
     //Delete queued jobs from test exportItems created above
     Yii::app()->jobQueue->deleteAll();
     ExportModule::$asynchronousPageSize = 2;
     $job = new ExportJob();
     $this->assertEquals(0, count(Yii::app()->jobQueue->getAll()));
     $this->assertTrue($job->run());
     $this->assertEquals(0, count(Yii::app()->jobQueue->getAll()));
     $exportItem = ExportItem::getById($id);
     $fileModel = $exportItem->exportFileModel;
     $this->assertEquals(1, $exportItem->isCompleted);
     $this->assertEquals(0, $exportItem->processOffset);
     $this->assertEquals('csv', $exportItem->exportFileType);
     $this->assertEquals('rowAndColumnsTest2', $exportItem->exportFileName);
     $this->assertTrue($fileModel instanceof ExportFileModel);
     $accounts = Account::getAll();
     $headerData = array('Name');
     $data = array();
     foreach ($accounts as $account) {
         $data[] = array($account->name);
     }
     $output = ExportItemToCsvFileUtil::export($data, $headerData, 'rowAndColumnsTest2.csv', false);
     $this->assertEquals($output, $fileModel->fileContent->content);
     // Check if user got notification message, and if its type is ExportProcessCompleted
     $this->assertEquals($numberOfUserNotifications + 1, Notification::getCountByTypeAndUser('ExportProcessCompleted', Yii::app()->user->userModel));
     //Matrix report should not paginate
     $report = new Report();
     $report->setType(Report::TYPE_MATRIX);
     $report->setModuleClassName('AccountsModule');
     $report->setFiltersStructure('');
     $displayAttribute = new DisplayAttributeForReportForm('AccountsModule', 'Account', Report::TYPE_MATRIX);
     $displayAttribute->setModelAliasUsingTableAliasName('model1');
     $displayAttribute->attributeIndexOrDerivedType = 'Count';
     $report->addDisplayAttribute($displayAttribute);
     $groupBy = new GroupByForReportForm('AccountsModule', 'Account', Report::TYPE_MATRIX);
     $groupBy->attributeIndexOrDerivedType = 'name';
     $groupBy->axis = 'y';
     $report->addGroupBy($groupBy);
     $groupBy = new GroupByForReportForm('AccountsModule', 'Account', Report::TYPE_MATRIX);
     $groupBy->attributeIndexOrDerivedType = 'officePhone';
     $report->addGroupBy($groupBy);
     $dataProvider = new MatrixReportDataProvider($report);
     $exportItem = new ExportItem();
     $exportItem->isCompleted = 0;
     $exportItem->exportFileType = 'csv';
     $exportItem->exportFileName = 'matrixTest1';
     $exportItem->modelClassName = 'SavedReport';
     $exportItem->serializedData = ExportUtil::getSerializedDataForExport($dataProvider);
     $this->assertTrue($exportItem->save());
     $id = $exportItem->id;
     $exportItem->forget();
     unset($exportItem);
     //Delete queued jobs from test exportItems created above
     Yii::app()->jobQueue->deleteAll();
     ExportModule::$asynchronousPageSize = 2;
     $job = new ExportJob();
     $this->assertTrue($job->run());
     $exportItem = ExportItem::getById($id);
     $fileModel = $exportItem->exportFileModel;
     $this->assertEquals(1, $exportItem->isCompleted);
     $this->assertEquals(0, $exportItem->processOffset);
     $this->assertEquals('csv', $exportItem->exportFileType);
     $this->assertEquals('matrixTest1', $exportItem->exportFileName);
     $this->assertTrue($fileModel instanceof ExportFileModel);
     $fileContent = $fileModel->fileContent->content;
     $this->assertContains('Test Account', $fileContent);
     $this->assertContains('Test Account 2', $fileContent);
     $this->assertContains('Test Account 3', $fileContent);
     $this->assertContains('Test Account 4', $fileContent);
     // Check if user got notification message, and if its type is ExportProcessCompleted
     $this->assertEquals($numberOfUserNotifications + 2, Notification::getCountByTypeAndUser('ExportProcessCompleted', Yii::app()->user->userModel));
 }