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