]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/modules/Import/ImportFileTest.php
Release 6.4.0
[Github/sugarcrm.git] / tests / modules / Import / ImportFileTest.php
1 <?php
2 /*********************************************************************************
3  * SugarCRM Community Edition is a customer relationship management program developed by
4  * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
5  * 
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.
12  * 
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
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 SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
24  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
25  * 
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.
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 "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  ********************************************************************************/
36
37
38 require_once('modules/Import/sources/ImportFile.php');
39
40 class ImportFileTest extends Sugar_PHPUnit_Framework_TestCase
41 {
42     protected $unlink = array();
43
44         public function setUp()
45         {
46         $GLOBALS['current_user'] = SugarTestUserUtilities::createAnonymousUser();
47     }
48
49     public function tearDown()
50     {
51         SugarTestImportUtilities::removeAllCreatedFiles();
52         SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
53         foreach($this->unlink as $file) {
54             @unlink($file);
55         }
56         unset($GLOBALS['current_user']);
57     }
58
59     /**
60          * @ticket 23380
61          */
62         public function testFileImportNoEnclosers()
63     {
64         $file = SugarTestImportUtilities::createFile(2,1);
65         $importFile = new ImportFile($file,',','', TRUE, FALSE);
66         $row = $importFile->getNextRow();
67         $this->assertEquals($row, array('foo00'));
68         $row = $importFile->getNextRow();
69         $this->assertEquals($row,array('foo10'));
70     }
71
72     public function testLoadNonExistantFile()
73     {
74         $importFile = new ImportFile(ImportCacheFiles::getImportDir().'/thisfileisntthere'.date("YmdHis").'.csv',',','"');
75         $this->assertFalse($importFile->fileExists());
76     }
77
78     public function testLoadGoodFile()
79     {
80         $file = SugarTestImportUtilities::createFile(2,1);
81         $importFile = new ImportFile($file,',','"', TRUE, FALSE);
82         $this->assertTrue($importFile->fileExists());
83     }
84
85     /**
86      * @ticket 39494
87      */
88     public function testLoadFileWithByteOrderMark()
89     {
90         $sample_file = 'upload://Bug39494ImportFile.txt';
91         copy('tests/modules/Import/Bug39494ImportFile.txt', $sample_file);
92         $this->unlink[] = $sample_file;
93         $importFile = new ImportFile($sample_file,"\t",'',false);
94         $this->assertTrue($importFile->fileExists());
95         $row = $importFile->getNextRow();
96         $this->assertEquals($row,array('name','city'));
97         $row = $importFile->getNextRow();
98         $this->assertEquals($row,array('tester1','wuhan'));
99     }
100
101     public function testGetNextRow()
102     {
103         $file = SugarTestImportUtilities::createFile(3,2);
104         $importFile = new ImportFile($file,',','"', TRUE, FALSE);
105
106         $row = $importFile->getNextRow();
107         $this->assertEquals(array("foo00","foo01"),$row);
108         $row = $importFile->getNextRow();
109         $this->assertEquals(array("foo10","foo11"),$row);
110         $row = $importFile->getNextRow();
111         $this->assertEquals(array("foo20","foo21"),$row);
112     }
113
114     /**
115          * @ticket 41361
116          */
117     public function testGetNextRowWithEOL()
118     {
119         $file = SugarTestImportUtilities::createFileWithEOL(1, 1);
120         $importFile = new ImportFile($file,',','"', TRUE, FALSE);
121         $row = $importFile->getNextRow();
122         // both \r\n and \n should be properly replaced with PHP_EOL
123         $this->assertEquals(array("start0".PHP_EOL."0".PHP_EOL."end"), $row);
124     }
125
126     public function testLoadEmptyFile()
127     {
128         $emptyFile = ImportCacheFiles::getImportDir().'/empty'.date("YmdHis").'.csv';
129         file_put_contents($emptyFile,'');
130         $this->unlink[] = $emptyFile;
131
132         $importFile = new ImportFile($emptyFile,',','"',false);
133
134         $this->assertFalse($importFile->getNextRow());
135
136         $importFile = new ImportFile($emptyFile,',','',false);
137
138         $this->assertFalse($importFile->getNextRow());
139     }
140
141     public function testDeleteFileOnDestroy()
142     {
143         $file = SugarTestImportUtilities::createFile(3,2);
144         $importFile = new ImportFile($file,',','"',true, FALSE);
145
146         unset($importFile);
147
148         $this->assertFalse(is_file($file));
149     }
150
151     public function testNotDeleteFileOnDestroy()
152     {
153         $file = SugarTestImportUtilities::createFile(3,2);
154         $importFile = new ImportFile($file,',','"',false);
155
156         unset($importFile);
157
158         $this->assertTrue(is_file($file));
159     }
160
161     public function testGetFieldCount()
162     {
163         $file = SugarTestImportUtilities::createFile(3,2);
164         $importFile = new ImportFile($file,',','"',TRUE, FALSE);
165
166         $importFile->getNextRow();
167         $this->assertEquals(2,$importFile->getFieldCount());
168     }
169
170     public function testMarkRowAsDuplicate()
171     {
172         $file = SugarTestImportUtilities::createFile(3,2);
173         $importFile = new ImportFile($file,',','"', TRUE, FALSE);
174
175         $row = $importFile->getNextRow();
176         $importFile->markRowAsDuplicate();
177
178         $fp = sugar_fopen(ImportCacheFiles::getDuplicateFileName(),'r');
179         $duperow = fgetcsv($fp);
180         fclose($fp);
181
182         $this->assertEquals($row,$duperow);
183     }
184
185     public function testWriteError()
186     {
187         $file = SugarTestImportUtilities::createFile(3,2);
188         $importFile = new ImportFile($file,',','"', TRUE, FALSE);
189
190         $row = $importFile->getNextRow();
191         $importFile->writeError('Some Error','field1','foo');
192
193         $fp = sugar_fopen(ImportCacheFiles::getErrorFileName(),'r');
194         $errorrow = fgetcsv($fp);
195         fclose($fp);
196
197         $this->assertEquals(array('Some Error','field1','foo',1),$errorrow);
198
199         $fp = sugar_fopen(ImportCacheFiles::getErrorRecordsWithoutErrorFileName(),'r');
200         $errorrecordrow = fgetcsv($fp);
201         fclose($fp);
202
203         $this->assertEquals($row,$errorrecordrow);
204     }
205
206     public function testWriteErrorRecord()
207     {
208         $file = SugarTestImportUtilities::createFile(3,2);
209         $importFile = new ImportFile($file,',','"', TRUE, FALSE);
210
211         $row = $importFile->getNextRow();
212         $importFile->writeErrorRecord();
213
214         $fp = sugar_fopen(ImportCacheFiles::getErrorRecordsWithoutErrorFileName(),'r');
215         $errorrecordrow = fgetcsv($fp);
216         fclose($fp);
217
218         $this->assertEquals($row,$errorrecordrow);
219     }
220
221     public function testWriteStatus()
222     {
223         $file = SugarTestImportUtilities::createFile(3,2);
224         $importFile = new ImportFile($file,',','"', TRUE, FALSE);
225
226         $importFile->getNextRow();
227         $importFile->writeError('Some Error','field1','foo');
228         $importFile->getNextRow();
229         $importFile->markRowAsDuplicate();
230         $importFile->getNextRow();
231         $importFile->markRowAsImported();
232         $importFile->writeStatus();
233
234         $fp = sugar_fopen(ImportCacheFiles::getStatusFileName(),'r');
235         $statusrow = fgetcsv($fp);
236         fclose($fp);
237
238         $this->assertEquals(array(3,1,1,1,0,$file),$statusrow);
239     }
240
241     public function testWriteStatusWithTwoErrorsInOneRow()
242     {
243         $file = SugarTestImportUtilities::createFile(3,2);
244         $importFile = new ImportFile($file,',','"', TRUE, FALSE);
245
246         $row = $importFile->getNextRow();
247         $importFile->writeError('Some Error','field1','foo');
248         $importFile->writeError('Some Error','field1','foo');
249         $importFile->getNextRow();
250         $importFile->markRowAsImported();
251         $importFile->getNextRow();
252         $importFile->markRowAsImported();
253         $importFile->writeStatus();
254
255         $fp = sugar_fopen(ImportCacheFiles::getStatusFileName(),'r');
256         $statusrow = fgetcsv($fp);
257         fclose($fp);
258
259         $this->assertEquals(array(3,1,0,2,0,$file),$statusrow);
260
261         $fp = sugar_fopen(ImportCacheFiles::getErrorRecordsWithoutErrorFileName(),'r');
262         $errorrecordrow = fgetcsv($fp);
263
264         $this->assertEquals($row,$errorrecordrow);
265
266         $this->assertFalse(fgetcsv($fp),'Should be only 1 record in the csv file');
267         fclose($fp);
268
269     }
270
271     public function testWriteStatusWithTwoUpdatedRecords()
272     {
273         $file = SugarTestImportUtilities::createFile(3,2);
274         $importFile = new ImportFile($file,',','"', TRUE, FALSE);
275
276         $row = $importFile->getNextRow();
277         $importFile->markRowAsImported(false);
278         $importFile->getNextRow();
279         $importFile->markRowAsImported();
280         $importFile->getNextRow();
281         $importFile->markRowAsImported();
282         $importFile->writeStatus();
283
284         $fp = sugar_fopen(ImportCacheFiles::getStatusFileName(),'r');
285         $statusrow = fgetcsv($fp);
286         fclose($fp);
287
288         $this->assertEquals(array(3,0,0,2,1,$file),$statusrow);
289     }
290
291     public function testWriteRowToLastImport()
292     {
293         $file = SugarTestImportUtilities::createFile(3,2);
294         $importFile = new ImportFile($file,',','"');
295         $record = $importFile->writeRowToLastImport("Tests","Test","TestRunner");
296
297         $query = "SELECT *
298                         FROM users_last_import
299                         WHERE assigned_user_id = '{$GLOBALS['current_user']->id}'
300                             AND import_module = 'Tests'
301                             AND bean_type = 'Test'
302                             AND bean_id = 'TestRunner'
303                             AND id = '$record'
304                             AND deleted=0";
305
306                 $result = $GLOBALS['db']->query($query);
307
308         $this->assertNotNull($GLOBALS['db']->fetchByAssoc($result));
309
310         $query = "DELETE FROM users_last_import
311                         WHERE assigned_user_id = '{$GLOBALS['current_user']->id}'
312                             AND import_module = 'Tests'
313                             AND bean_type = 'Test'
314                             AND bean_id = 'TestRunner'
315                             AND id = '$record'
316                             AND deleted=0";
317         $GLOBALS['db']->query($query);
318     }
319
320     public function providerEncodingData()
321     {
322         return array(
323             array('TestCharset.csv', 'UTF-8'),
324             array('TestCharset2.csv', 'ISO-8859-1'),
325             );
326     }
327
328     /**
329      * @dataProvider providerEncodingData
330      */
331     public function testCharsetDetection($file, $encoding) {
332         // create the test file
333         $sample_file = $GLOBALS['sugar_config']['upload_dir'].'/'.$file;
334         copy('tests/modules/Import/'.$file, $sample_file);
335         $this->unlink[] = $sample_file;
336
337         // auto detect charset
338         $importFile = new ImportFile($sample_file, ",", '', false, false);
339         $this->assertTrue($importFile->fileExists());
340         $charset = $importFile->autoDetectCharacterSet();
341         $this->assertEquals($encoding, $charset, 'detected char encoding is incorrect.');
342     }
343
344     public function providerRowCountData()
345     {
346         return array(
347             array('TestCharset.csv', 2, false),
348             array('TestCharset2.csv', 11, true),
349             array('TestCharset2.csv', 12, false),
350             );
351     }
352
353     /**
354      * @dataProvider providerRowCountData
355      */
356     public function testRowCount($file, $count, $hasHeader) {
357         // create the test file
358         $sample_file = $GLOBALS['sugar_config']['upload_dir'].'/'.$file;
359         copy('tests/modules/Import/'.$file, $sample_file);
360         $this->unlink[] = $sample_file;
361
362         $importFile = new ImportFile($sample_file, ",", '', false, false);
363         $this->assertTrue($importFile->fileExists());
364         $importFile->setHeaderRow($hasHeader);
365         $c = $importFile->getTotalRecordCount();
366         $this->assertEquals($count, $c, 'incorrect row count.');
367     }
368
369     public function providerFieldCountData()
370     {
371         return array(
372             array('TestCharset.csv', 2),
373             array('TestCharset2.csv', 5),
374             );
375     }
376
377     /**
378      * @dataProvider providerFieldCountData
379      */
380     public function testFieldCount($file, $count) {
381         // create the test file
382         $sample_file = $GLOBALS['sugar_config']['upload_dir'].'/'.$file;
383         copy('tests/modules/Import/'.$file, $sample_file);
384         $this->unlink[] = $sample_file;
385
386         $importFile = new ImportFile($sample_file, ",", '"', false, false);
387         $this->assertTrue($importFile->fileExists());
388         $c = $importFile->getNextRow();
389         $c = $importFile->getFieldCount();
390         $this->assertEquals($count, $c, 'incorrect row count.');
391     }
392
393     public function providerLineCountData()
394     {
395         return array(
396             array('TestCharset.csv', 2),
397             array('TestCharset2.csv', 12),
398             );
399     }
400
401     /**
402      * @dataProvider providerLineCountData
403      */
404     public function testLineCount($file, $count) {
405         // create the test file
406         $sample_file = $GLOBALS['sugar_config']['upload_dir'].'/'.$file;
407         copy('tests/modules/Import/'.$file, $sample_file);
408         $this->unlink[] = $sample_file;
409
410         $importFile = new ImportFile($sample_file, ",", '"', false, false);
411         $this->assertTrue($importFile->fileExists());
412         $c = $importFile->getNumberOfLinesInfile();
413         $this->assertEquals($count, $c, 'incorrect row count.');
414     }
415
416     public function providerDateFormatData()
417     {
418         return array(
419             array('TestCharset.csv', 'd/m/Y'),
420             array('TestCharset2.csv', 'm/d/Y'),
421             );
422     }
423
424     /**
425      * @dataProvider providerDateFormatData
426      */
427     public function testDateFormat($file, $format) {
428         // create the test file
429         $sample_file = $GLOBALS['sugar_config']['upload_dir'].'/'.$file;
430         copy('tests/modules/Import/'.$file, $sample_file);
431         $this->unlink[] = $sample_file;
432
433         $importFile = new ImportFile($sample_file, ",", '"', false, false);
434         $this->assertTrue($importFile->fileExists());
435         $ret = $importFile->autoDetectCSVProperties();
436         $this->assertTrue($ret, 'Failed to auto detect properties.');
437         $c = $importFile->getDateFormat();
438         $this->assertEquals($format, $c, 'incorrect date format.');
439     }
440
441     public function providerTimeFormatData()
442     {
443         return array(
444             array('TestCharset.csv', 'h:ia'),
445             array('TestCharset2.csv', 'H:i'),
446             );
447     }
448
449     /**
450      * @dataProvider providerTimeFormatData
451      */
452     public function testTimeFormat($file, $format) {
453         // create the test file
454         $sample_file = $GLOBALS['sugar_config']['upload_dir'].'/'.$file;
455         copy('tests/modules/Import/'.$file, $sample_file);
456         $this->unlink[] = $sample_file;
457
458         $importFile = new ImportFile($sample_file, ",", '"', false, false);
459         $this->assertTrue($importFile->fileExists());
460         $ret = $importFile->autoDetectCSVProperties();
461         $this->assertTrue($ret, 'Failed to auto detect properties.');
462         $c = $importFile->getTimeFormat();
463         $this->assertEquals($format, $c, 'incorrect time format.');
464     }
465
466     /**
467      * @ticket 48289
468      */
469     public function testTabDelimiter() {
470         // create the test file
471         $sample_file = $GLOBALS['sugar_config']['upload_dir'].'/TestCharset.csv';
472         copy('tests/modules/Import/TestCharset.csv', $sample_file);
473
474         // use '\t' to simulate the bug
475         $importFile = new ImportFile($sample_file, '\t', '"', false, false);
476         $this->assertTrue($importFile->fileExists());
477         $c = $importFile->getNextRow();
478         $this->assertTrue(is_array($c), 'incorrect return type.');
479         $this->assertEquals(1, count($c), 'incorrect array count.');
480
481         // cleanup
482         $this->unlink[] = $sample_file;
483     }
484 }