]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/modules/Import/ImportFileTest.php
Release 6.3.0beta1
[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 }