All Data Structures Functions Variables Pages
ProjectImportTest.php
1 <?php
2  /*********************************************************************************
3  * Zurmo is a customer relationship management program developed by
4  * Zurmo, Inc. Copyright (C) 2017 Zurmo Inc.
5  *
6  * Zurmo is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY ZURMO, ZURMO DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  *
13  * Zurmo is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  *
23  * You can contact Zurmo, Inc. with a mailing address at 27 North Wacker Drive
24  * Suite 370 Chicago, IL 60606. or at email address contact@zurmo.com.
25  *
26  * The interactive user interfaces in original and modified versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  *
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the Zurmo
32  * logo and Zurmo copyright notice. If the display of the logo is not reasonably
33  * feasible for technical reasons, the Appropriate Legal Notices must display the words
34  * "Copyright Zurmo Inc. 2017. All rights reserved".
35  ********************************************************************************/
36 
38  {
39  public static function setUpBeforeClass()
40  {
41  parent::setUpBeforeClass();
42  SecurityTestHelper::createSuperAdmin();
43  Yii::import('application.core.data.*');
44  Yii::import('application.modules.projects.data.*');
45  }
46 
47  public function testSimpleUserImportWhereAllRowsSucceed()
48  {
49  Yii::app()->user->userModel = User::getByUsername('super');
50  $import = new Import();
51  $serializedData['importRulesType'] = 'Projects';
52  $serializedData['firstRowIsHeaderRow'] = true;
53  $import->serializedData = serialize($serializedData);
54  $this->assertTrue($import->save());
55 
56  ImportTestHelper::
57  createTempTableByFileNameAndTableName('projectsSample.csv', $import->getTempTableName(), true,
58  Yii::getPathOfAlias('application.modules.projects.tests.unit.files'));
59 
60  $this->assertEquals(3, ImportDatabaseUtil::getCount($import->getTempTableName())); // includes header rows.
61 
62  $ownerColumnMappingData = array('attributeIndexOrDerivedType' => 'owner',
63  'type' => 'importColumn', 'mappingRulesData' => array(
64  'DefaultModelNameIdMappingRuleForm' =>
65  array('defaultModelId' => null),
66  'UserValueTypeModelAttributeMappingRuleForm' =>
67  array('type' =>
68  UserValueTypeModelAttributeMappingRuleForm::ZURMO_USERNAME)));
69 
70  $mappingData = array(
71  'column_0' => $ownerColumnMappingData,
73  'column_2' => ImportMappingUtil::makeTextAreaColumnMappingData ('description'),
74  'column_3' => ImportMappingUtil::makeDropDownColumnMappingData ('status'),
75  );
76 
77  $importRules = ImportRulesUtil::makeImportRulesByType('Projects');
78  $page = 0;
79  $config = array('pagination' => array('pageSize' => 50)); //This way all rows are processed.
80  $dataProvider = new ImportDataProvider($import->getTempTableName(), true, $config);
81  $dataProvider->getPagination()->setCurrentPage($page);
82  $importResultsUtil = new ImportResultsUtil($import);
83  $messageLogger = new ImportMessageLogger();
85  $importRules,
86  $mappingData,
87  $importResultsUtil,
89  $messageLogger);
90  $importResultsUtil->processStatusAndMessagesForEachRow();
91  //Confirm that 2 models where created.
92  $projects = Project::getAll();
93  $this->assertEquals(2, count($projects));
94 
95  $projects = Project::getByName('My first project');
96  $this->assertEquals(1, count($projects[0]));
97  $this->assertEquals('super', $projects[0]->owner->username);
98  $this->assertEquals('My first project', $projects[0]->name);
99  $this->assertEquals(2, $projects[0]->status);
100  //todo ask Jason for it
101  //$this->assertEquals('My first project Desc', $projects[0]->description);
102  $projects[0]->delete();
103 
104  $projects = Project::getByName('My second project');
105  $this->assertEquals(1, count($projects[0]));
106  $this->assertEquals('super', $projects[0]->owner->username);
107  $this->assertEquals('My second project', $projects[0]->name);
108  $this->assertEquals(1, $projects[0]->status);
109  //$this->assertEquals('My second project Desc', $projects[0]->description);
110 
111  $projects[0]->delete();
112 
113  //Confirm that 2 rows were processed as 'updated'.
114  $this->assertEquals(0, ImportDatabaseUtil::getCount($import->getTempTableName(), "status = "
115  . ImportRowDataResultsUtil::UPDATED));
116 
117  //Confirm 2 rows were processed as 'errors'.
118  $this->assertEquals(0, ImportDatabaseUtil::getCount($import->getTempTableName(), "status = "
119  . ImportRowDataResultsUtil::ERROR));
120 
121  $beansWithErrors = ImportDatabaseUtil::getSubset($import->getTempTableName(), "status = "
122  . ImportRowDataResultsUtil::ERROR);
123  $this->assertEquals(0, count($beansWithErrors));
124  }
125  }
126 ?>
static makeImportRulesByType($importRulesType)
static getByUsername($username)
Definition: User.php:49
static getCount($tableName, $where=null)
static importByDataProvider(ImportDataProvider $dataProvider, ImportRules $importRules, $mappingData, ImportResultsUtil $importResultsUtil, ExplicitReadWriteModelPermissions $explicitReadWriteModelPermissions, ImportMessageLogger $messageLogger)
Definition: ImportUtil.php:51
static makeStringColumnMappingData($attributeName, $defaultValue=null)
static makeDropDownColumnMappingData($attributeName, $defaultValue=null, $customFieldsInstructionData=null)
static getSubset($tableName, $where=null, $count=null, $offset=null)
static getAll($orderBy=null, $sortDescending=false, $modelClassName=null)
static makeTextAreaColumnMappingData($attributeName)
static getByName($name)
Definition: Project.php:50
Generated on Fri Jun 5 2020 07:10:34