2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
6 * This program 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 SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
11 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13 * This program 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
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
23 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
24 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26 * The interactive user interfaces in modified source and object code 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.
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 "Powered by
32 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
33 * technical reasons, the Appropriate Legal Notices must display the words
34 * "Powered by SugarCRM".
35 ********************************************************************************/
38 require_once 'modules/Import/ImportFile.php';
40 class ImportFileTest extends Sugar_PHPUnit_Framework_TestCase
42 public function setUp()
44 $GLOBALS['current_user'] = SugarTestUserUtilities::createAnonymousUser();
47 public function tearDown()
49 SugarTestImportUtilities::removeAllCreatedFiles();
50 SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
51 unset($GLOBALS['current_user']);
57 public function testFileImportNoEnclosers()
59 $file = SugarTestImportUtilities::createFile(2,1);
60 $importFile = new ImportFile($file,',','');
61 $row = $importFile->getNextRow();
62 $this->assertEquals($row, array('foo00'));
63 $row = $importFile->getNextRow();
64 $this->assertEquals($row,array('foo10'));
67 public function testLoadNonExistantFile()
69 $importFile = new ImportFile($GLOBALS['sugar_config']['import_dir'].'/thisfileisntthere'.date("YmdHis").'.csv',',','"');
70 $this->assertFalse($importFile->fileExists());
73 public function testLoadGoodFile()
75 $file = SugarTestImportUtilities::createFile(2,1);
76 $importFile = new ImportFile($file,',','"');
77 $this->assertTrue($importFile->fileExists());
83 public function testLoadFileWithByteOrderMark()
85 $importFile = new ImportFile('tests/modules/Import/Bug39494ImportFile.txt',"\t",'',false);
86 $this->assertTrue($importFile->fileExists());
87 $row = $importFile->getNextRow();
88 $this->assertEquals($row,array('name','city'));
89 $row = $importFile->getNextRow();
90 $this->assertEquals($row,array('tester1','wuhan'));
93 public function testGetNextRow()
95 $file = SugarTestImportUtilities::createFile(3,2);
96 $importFile = new ImportFile($file,',','"');
98 $row = $importFile->getNextRow();
99 $this->assertEquals(array("foo00","foo01"),$row);
100 $row = $importFile->getNextRow();
101 $this->assertEquals(array("foo10","foo11"),$row);
102 $row = $importFile->getNextRow();
103 $this->assertEquals(array("foo20","foo21"),$row);
109 public function testGetNextRowWithEOL()
111 $file = SugarTestImportUtilities::createFileWithEOL(1, 1);
112 $importFile = new ImportFile($file,',','"');
113 $row = $importFile->getNextRow();
114 // both \r\n and \n should be properly replaced with PHP_EOL
115 $this->assertEquals(array("start0".PHP_EOL."0".PHP_EOL."end"), $row);
118 public function testLoadEmptyFile()
120 $emptyFile = $GLOBALS['sugar_config']['import_dir'].'/empty'.date("YmdHis").'.csv';
121 file_put_contents($emptyFile,'');
123 $importFile = new ImportFile($emptyFile,',','"',false);
125 $this->assertFalse($importFile->getNextRow());
127 $importFile = new ImportFile($emptyFile,',','',false);
129 $this->assertFalse($importFile->getNextRow());
134 public function testDeleteFileOnDestroy()
136 $file = SugarTestImportUtilities::createFile(3,2);
137 $importFile = new ImportFile($file,',','"',true);
141 $this->assertFalse(is_file($file));
144 public function testNotDeleteFileOnDestroy()
146 $file = SugarTestImportUtilities::createFile(3,2);
147 $importFile = new ImportFile($file,',','"',false);
151 $this->assertTrue(is_file($file));
154 public function testGetFieldCount()
156 $file = SugarTestImportUtilities::createFile(3,2);
157 $importFile = new ImportFile($file,',','"');
159 $importFile->getNextRow();
160 $this->assertEquals($importFile->getFieldCount(),2);
163 public function testMarkRowAsDuplicate()
165 $file = SugarTestImportUtilities::createFile(3,2);
166 $importFile = new ImportFile($file,',','"');
168 $row = $importFile->getNextRow();
169 $importFile->markRowAsDuplicate();
171 $fp = sugar_fopen(ImportCacheFiles::getDuplicateFileName(),'r');
172 $duperow = fgetcsv($fp);
175 $this->assertEquals($row,$duperow);
178 public function testWriteError()
180 $file = SugarTestImportUtilities::createFile(3,2);
181 $importFile = new ImportFile($file,',','"');
183 $row = $importFile->getNextRow();
184 $importFile->writeError('Some Error','field1','foo');
186 $fp = sugar_fopen(ImportCacheFiles::getErrorFileName(),'r');
187 $errorrow = fgetcsv($fp);
190 $this->assertEquals(array('Some Error','field1','foo',1),$errorrow);
192 $fp = sugar_fopen(ImportCacheFiles::getErrorRecordsFileName(),'r');
193 $errorrecordrow = fgetcsv($fp);
196 $this->assertEquals($row,$errorrecordrow);
199 public function testWriteErrorRecord()
201 $file = SugarTestImportUtilities::createFile(3,2);
202 $importFile = new ImportFile($file,',','"');
204 $row = $importFile->getNextRow();
205 $importFile->writeErrorRecord();
207 $fp = sugar_fopen(ImportCacheFiles::getErrorRecordsFileName(),'r');
208 $errorrecordrow = fgetcsv($fp);
211 $this->assertEquals($row,$errorrecordrow);
214 public function testWriteStatus()
216 $file = SugarTestImportUtilities::createFile(3,2);
217 $importFile = new ImportFile($file,',','"');
219 $importFile->getNextRow();
220 $importFile->writeError('Some Error','field1','foo');
221 $importFile->getNextRow();
222 $importFile->markRowAsDuplicate();
223 $importFile->getNextRow();
224 $importFile->markRowAsImported();
225 $importFile->writeStatus();
227 $fp = sugar_fopen(ImportCacheFiles::getStatusFileName(),'r');
228 $statusrow = fgetcsv($fp);
231 $this->assertEquals(array(3,1,1,1,0,$file),$statusrow);
234 public function testWriteStatusWithTwoErrorsInOneRow()
236 $file = SugarTestImportUtilities::createFile(3,2);
237 $importFile = new ImportFile($file,',','"');
239 $row = $importFile->getNextRow();
240 $importFile->writeError('Some Error','field1','foo');
241 $importFile->writeError('Some Error','field1','foo');
242 $importFile->getNextRow();
243 $importFile->markRowAsImported();
244 $importFile->getNextRow();
245 $importFile->markRowAsImported();
246 $importFile->writeStatus();
248 $fp = sugar_fopen(ImportCacheFiles::getStatusFileName(),'r');
249 $statusrow = fgetcsv($fp);
252 $this->assertEquals(array(3,1,0,2,0,$file),$statusrow);
254 $fp = sugar_fopen(ImportCacheFiles::getErrorRecordsFileName(),'r');
255 $errorrecordrow = fgetcsv($fp);
257 $this->assertEquals($row,$errorrecordrow);
258 $this->assertFalse(fgetcsv($fp),'Should be only 1 record in the csv file');
263 public function testWriteStatusWithTwoUpdatedRecords()
265 $file = SugarTestImportUtilities::createFile(3,2);
266 $importFile = new ImportFile($file,',','"');
268 $row = $importFile->getNextRow();
269 $importFile->markRowAsImported(false);
270 $importFile->getNextRow();
271 $importFile->markRowAsImported();
272 $importFile->getNextRow();
273 $importFile->markRowAsImported();
274 $importFile->writeStatus();
276 $fp = sugar_fopen(ImportCacheFiles::getStatusFileName(),'r');
277 $statusrow = fgetcsv($fp);
280 $this->assertEquals(array(3,0,0,2,1,$file),$statusrow);
283 public function testWriteRowToLastImport()
285 $file = SugarTestImportUtilities::createFile(3,2);
286 $importFile = new ImportFile($file,',','"');
287 $record = $importFile->writeRowToLastImport("Tests","Test","TestRunner");
290 FROM users_last_import
291 WHERE assigned_user_id = '{$GLOBALS['current_user']->id}'
292 AND import_module = 'Tests'
293 AND bean_type = 'Test'
294 AND bean_id = 'TestRunner'
298 $result = $GLOBALS['db']->query($query);
300 $this->assertNotNull($GLOBALS['db']->fetchByAssoc($result));
302 $query = "DELETE FROM users_last_import
303 WHERE assigned_user_id = '{$GLOBALS['current_user']->id}'
304 AND import_module = 'Tests'
305 AND bean_type = 'Test'
306 AND bean_id = 'TestRunner'
309 $GLOBALS['db']->query($query);