Ejemplo n.º 1
0
 /**
  * Cancels export.
  * @param int $id
  */
 public function actionCancel($id)
 {
     $exportItem = ExportItem::getById(intval($id));
     $exportItem->cancelExport = true;
     $exportItem->save();
     $this->redirect(Yii::app()->createUrl('export/default/list'));
 }
Ejemplo n.º 2
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));
 }
Ejemplo n.º 3
0
 public function actionDownload($id)
 {
     try {
         $exportItem = ExportItem::getById((int) $id);
         if ($exportItem instanceof ExportItem) {
             $fileModel = $exportItem->exportFileModel;
             Yii::app()->request->sendFile($fileModel->name, $fileModel->fileContent->content, $fileModel->type, false);
         }
     } catch (Exception $e) {
         Yii::app()->user->setFlash('notification', Zurmo::t('ExportModule', 'Export file you requested is not available anymore.'));
         $this->redirect(Yii::app()->createUrl('home/default/index'));
     }
 }
Ejemplo n.º 4
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));
 }
Ejemplo n.º 5
0
 public function testCreateExportItemWithLargeData()
 {
     // anything larger than 99999 will have memcache complain about objects being too big to cache
     // why? because ExportItem isCacheable() is true(implicitly, inherited from RedBeanModel.php).
     // so during every save its re-cached: RedBeanModel.php:2017
     $idsToExport = range(0, 66500);
     $exportItem = new ExportItem();
     $exportItem->isCompleted = 0;
     $exportItem->exportFileType = 'csv';
     $exportItem->exportFileName = 'test';
     $exportItem->modelClassName = 'Account';
     $exportItem->serializedData = serialize($idsToExport);
     $this->assertTrue($exportItem->save());
     $exportItemId = $exportItem->id;
     $exportItem->forget();
     $exportItem = ExportItem::getById($exportItemId);
     $this->assertEquals($idsToExport, unserialize($exportItem->serializedData));
 }