/**
  * @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());
 }
 /**
  * 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());
 }
 public function testRun()
 {
     $quote = DatabaseCompatibilityUtil::getQuote();
     //Create 2 export items, and set one with a date over a week ago (8 days ago) for the modifiedDateTime
     $exportItem = new ExportItem();
     $exportItem->isCompleted = 0;
     $exportItem->exportFileType = 'csv';
     $exportItem->exportFileName = 'test';
     $exportItem->modelClassName = 'Account';
     $exportItem->serializedData = serialize(array('test', 'test2'));
     $this->assertTrue($exportItem->save());
     $fileContent = new FileContent();
     $fileContent->content = 'test';
     $exportFileModel = new ExportFileModel();
     $exportFileModel->fileContent = $fileContent;
     $exportFileModel->name = $exportItem->exportFileName . ".csv";
     $exportFileModel->type = 'application/octet-stream';
     $exportFileModel->size = strlen($fileContent->content);
     $this->assertTrue($exportFileModel->save());
     $exportFileModel1Id = $exportFileModel->id;
     $exportItem->exportFileModel = $exportFileModel;
     $this->assertTrue($exportItem->save());
     $modifiedDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime(time() - 60 * 60 * 24 * 8);
     $sql = "Update item set modifieddatetime = '" . $modifiedDateTime . "' where id = " . $exportItem->getClassId('Item');
     ZurmoRedBean::exec($sql);
     // Second exportItem, that shouldn't be deleted.
     $exportItem2 = new ExportItem();
     $exportItem2->isCompleted = 0;
     $exportItem2->exportFileType = 'csv';
     $exportItem2->exportFileName = 'test';
     $exportItem2->modelClassName = 'Account';
     $exportItem2->serializedData = serialize(array('test', 'test2'));
     $this->assertTrue($exportItem2->save());
     $fileContent2 = new FileContent();
     $fileContent2->content = 'test';
     $exportFileModel2 = new ExportFileModel();
     $exportFileModel2->fileContent = $fileContent2;
     $exportFileModel2->name = $exportItem->exportFileName . ".csv";
     $exportFileModel2->type = 'application/octet-stream';
     $exportFileModel2->size = strlen($fileContent->content);
     $this->assertTrue($exportFileModel2->save());
     $exportFileModel2Id = $exportFileModel2->id;
     $exportItem2->exportFileModel = $exportFileModel2;
     $this->assertTrue($exportItem2->save());
     $job = new ExportCleanupJob();
     $this->assertTrue($job->run());
     $exportItems = ExportItem::getAll();
     $this->assertEquals(1, count($exportItems));
     $this->assertEquals($exportItem2->id, $exportItems[0]->id);
 }
 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);
     }
 }