Commits

Ivica Nedeljkovic committed 44304d2

Test jobs in export module

Comments (0)

Files changed (5)

app/protected/modules/export/ExportModule.php

 
         // Used to determine if data will be exported directly in browser
         // or to be exported via asynchronous via background job.
-        const ASYNCHRONOUS_THRESHOLD = 10000;
+        const ASYNCHRONOUS_THRESHOLD = 1;
 
         public function getDependencies()
         {

app/protected/modules/export/jobs/ExportJob.php

         */
         public static function getRunTimeThresholdInSeconds()
         {
-            return 600;
+            return 10;
         }
 
         public function run()
                         $fileContent->content = $output;
 
                         $exportFileModel = new ExportFileModel();
-                        $exportFileModel->exportItem = $exportItem;
                         $exportFileModel->fileContent = $fileContent;
                         $exportFileModel->name = $exportItem->exportFileName . ".csv";
                         $exportFileModel->type    = 'application/octet-stream';

app/protected/modules/export/tests/unit/ExportCleanupJobTest.php

+<?php
+    /*********************************************************************************
+     * Zurmo is a customer relationship management program developed by
+     * Zurmo, Inc. Copyright (C) 2012 Zurmo Inc.
+     *
+     * Zurmo is free software; you can redistribute it and/or modify it under
+     * the terms of the GNU General Public License version 3 as published by the
+     * Free Software Foundation with the addition of the following permission added
+     * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
+     * IN WHICH THE COPYRIGHT IS OWNED BY ZURMO, ZURMO DISCLAIMS THE WARRANTY
+     * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
+     *
+     * Zurmo is distributed in the hope that it will be useful, but WITHOUT
+     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+     * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
+     * details.
+     *
+     * You should have received a copy of the GNU General Public License along with
+     * this program; if not, see http://www.gnu.org/licenses or write to the Free
+     * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+     * 02110-1301 USA.
+     *
+     * You can contact Zurmo, Inc. with a mailing address at 113 McHenry Road Suite 207,
+     * Buffalo Grove, IL 60089, USA. or at email address contact@zurmo.com.
+     ********************************************************************************/
+
+    class ExportCleanupJobTest extends BaseTest
+    {
+        public static function setUpBeforeClass()
+        {
+            parent::setUpBeforeClass();
+            SecurityTestHelper::createSuperAdmin();
+        }
+
+        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->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');
+            R::exec($sql);
+
+            // Second exportItem, that shouldn't be deleted.
+            $exportItem2 = new ExportItem();
+            $exportItem2->isCompleted = 0;
+            $exportItem2->exportFileType = 'csv';
+            $exportItem2->exportFileName = 'test';
+            $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);
+        }
+    }
+?>

app/protected/modules/export/tests/unit/ExportJobTest.php

+<?php
+    /*********************************************************************************
+     * Zurmo is a customer relationship management program developed by
+     * Zurmo, Inc. Copyright (C) 2012 Zurmo Inc.
+     *
+     * Zurmo is free software; you can redistribute it and/or modify it under
+     * the terms of the GNU General Public License version 3 as published by the
+     * Free Software Foundation with the addition of the following permission added
+     * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
+     * IN WHICH THE COPYRIGHT IS OWNED BY ZURMO, ZURMO DISCLAIMS THE WARRANTY
+     * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
+     *
+     * Zurmo is distributed in the hope that it will be useful, but WITHOUT
+     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+     * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
+     * details.
+     *
+     * You should have received a copy of the GNU General Public License along with
+     * this program; if not, see http://www.gnu.org/licenses or write to the Free
+     * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+     * 02110-1301 USA.
+     *
+     * You can contact Zurmo, Inc. with a mailing address at 113 McHenry Road Suite 207,
+     * Buffalo Grove, IL 60089, USA. or at email address contact@zurmo.com.
+     ********************************************************************************/
+
+    class ExportJobTest extends BaseTest
+    {
+        public static function setUpBeforeClass()
+        {
+            parent::setUpBeforeClass();
+            SecurityTestHelper::createSuperAdmin();
+        }
+
+        /**
+         * 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::getUnreadCountByTypeAndUser('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->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();
+            foreach ($rows as $model)
+            {
+                $redBeanModelToExportAdapter  = new RedBeanModelToExportAdapter($model);
+                $data[] = $redBeanModelToExportAdapter->getData();
+            }
+            $output = ExportItemToCsvFileUtil::export($data, '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::getUnreadCountByTypeAndUser('ExportProcessCompleted', Yii::app()->user->userModel));
+        }
+    }
+?>

app/protected/modules/export/tests/unit/ExportTestHelper.php

+<?php
+    /*********************************************************************************
+     * Zurmo is a customer relationship management program developed by
+     * Zurmo, Inc. Copyright (C) 2012 Zurmo Inc.
+     *
+     * Zurmo is free software; you can redistribute it and/or modify it under
+     * the terms of the GNU General Public License version 3 as published by the
+     * Free Software Foundation with the addition of the following permission added
+     * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
+     * IN WHICH THE COPYRIGHT IS OWNED BY ZURMO, ZURMO DISCLAIMS THE WARRANTY
+     * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
+     *
+     * Zurmo is distributed in the hope that it will be useful, but WITHOUT
+     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+     * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
+     * details.
+     *
+     * You should have received a copy of the GNU General Public License along with
+     * this program; if not, see http://www.gnu.org/licenses or write to the Free
+     * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+     * 02110-1301 USA.
+     *
+     * You can contact Zurmo, Inc. with a mailing address at 113 McHenry Road Suite 207,
+     * Buffalo Grove, IL 60089, USA. or at email address contact@zurmo.com.
+     ********************************************************************************/
+
+    class ExportTestHelper
+    {
+         /**
+         * Method to help us simulate Controller::makeRedBeanDataProviderFromGet method.
+         */
+        public static function makeRedBeanDataProvider(
+            $searchModel,
+            $listModelClassName,
+            $pageSize,
+            $userId,
+            $stateMetadataAdapterClassName = null)
+        {
+            assert('is_int($pageSize)');
+            assert('$stateMetadataAdapterClassName == null || is_string($stateMetadataAdapterClassName)');
+
+            $metadataAdapter = new SearchDataProviderMetadataAdapter(
+                $searchModel,
+                $userId,
+                array()
+            );
+
+            return RedBeanModelDataProviderUtil::makeDataProvider(
+                $metadataAdapter,
+                $listModelClassName,
+                'RedBeanModelDataProvider',
+                null,
+                false,
+                $pageSize,
+                $stateMetadataAdapterClassName
+            );
+        }
+    }
+?>