public function testGetSerializedDataForExport() { $super = User::getByUsername('super'); Yii::app()->user->userModel = $super; $account = new Account(false); $searchForm = new AccountsSearchForm($account); $dataProvider = ExportTestHelper::makeRedBeanDataProvider($searchForm, 'Account', 0, Yii::app()->user->userModel->id); $serializedData = ExportUtil::getSerializedDataForExport($dataProvider); $this->assertEquals($dataProvider, unserialize($serializedData)); }
/** * 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)); }
public function testSecurityExceptionThrownDuringExport() { $super = User::getByUsername('super'); Yii::app()->user->userModel = $super; SecurityTestHelper::createAccounts(); $billy = User::getByUsername('billy'); Yii::app()->user->userModel = $billy; AllPermissionsOptimizationUtil::rebuild(); $numberOfUserNotifications = Notification::getCountByTypeAndUser('ExportProcessCompleted', $billy); $account = new Account(false); $searchForm = new AccountsSearchForm($account); $dataProvider = ExportTestHelper::makeRedBeanDataProvider($searchForm, 'Account', 0, $billy->id); $totalItems = $dataProvider->getTotalItemCount(); $this->assertEquals(3, $totalItems); $exportItem = new ExportItem(); $exportItem->isCompleted = 0; $exportItem->exportFileType = 'csv'; $exportItem->exportFileName = 'test7'; $exportItem->modelClassName = 'Account'; $exportItem->serializedData = serialize($dataProvider); $exportItem->owner = $billy; $this->assertTrue($exportItem->save()); $id = $exportItem->id; $exportItem->forget(); unset($exportItem); //Delete queued jobs from test exportItems created above Yii::app()->jobQueue->deleteAll(); $accounts = Account::getByName('Microsoft'); $account = $accounts[0]; $account->owner = $super; $this->assertTrue($account->save()); $job = new ExportJob(); //ReadPermissionSubscriptionUpdate should get added to jobQueue $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('csv', $exportItem->exportFileType); $this->assertEquals('test7', $exportItem->exportFileName); $this->assertTrue($fileModel instanceof ExportFileModel); $data = array(); $rows = $dataProvider->getData(); $modelToExportAdapter = new ModelToExportAdapter($rows[0]); $headerData = $modelToExportAdapter->getHeaderData(); foreach ($rows as $model) { //billy lost access to Microsoft account if ($model->id != $account->id) { $modelToExportAdapter = new ModelToExportAdapter($model); $data[] = $modelToExportAdapter->getData(); } } $output = ExportItemToCsvFileUtil::export($data, $headerData, 'test7.csv', false); $this->assertEquals($output, $fileModel->fileContent->content); }