]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/modules/Import/ImportFileTest.php
Release 6.2.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/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,',','');
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,',','"');
77         $this->assertTrue($importFile->fileExists());
78     }
79     
80     /**
81      * @ticket 39494
82      */
83     public function testLoadFileWithByteOrderMark()
84     {
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'));
91     }
92     
93     public function testGetNextRow()
94     {
95         $file = SugarTestImportUtilities::createFile(3,2);
96         $importFile = new ImportFile($file,',','"');
97         
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);
104     }
105     
106     /**
107          * @ticket 41361
108          */
109     public function testGetNextRowWithEOL()
110     {
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);
116     }
117     
118     public function testLoadEmptyFile()
119     {
120         $emptyFile = $GLOBALS['sugar_config']['import_dir'].'/empty'.date("YmdHis").'.csv';
121         file_put_contents($emptyFile,'');
122         
123         $importFile = new ImportFile($emptyFile,',','"',false);
124         
125         $this->assertFalse($importFile->getNextRow());
126         
127         $importFile = new ImportFile($emptyFile,',','',false);
128         
129         $this->assertFalse($importFile->getNextRow());
130         
131         @unlink($emptyFile);
132     }
133     
134     public function testDeleteFileOnDestroy()
135     {
136         $file = SugarTestImportUtilities::createFile(3,2);
137         $importFile = new ImportFile($file,',','"',true);
138         
139         unset($importFile);
140         
141         $this->assertFalse(is_file($file));
142     }
143     
144     public function testNotDeleteFileOnDestroy()
145     {
146         $file = SugarTestImportUtilities::createFile(3,2);
147         $importFile = new ImportFile($file,',','"',false);
148         
149         unset($importFile);
150         
151         $this->assertTrue(is_file($file));
152     }
153     
154     public function testGetFieldCount()
155     {
156         $file = SugarTestImportUtilities::createFile(3,2);
157         $importFile = new ImportFile($file,',','"');
158         
159         $importFile->getNextRow();
160         $this->assertEquals($importFile->getFieldCount(),2);
161     }
162     
163     public function testMarkRowAsDuplicate()
164     {
165         $file = SugarTestImportUtilities::createFile(3,2);
166         $importFile = new ImportFile($file,',','"');
167         
168         $row = $importFile->getNextRow();
169         $importFile->markRowAsDuplicate();
170         
171         $fp = sugar_fopen(ImportCacheFiles::getDuplicateFileName(),'r');
172         $duperow = fgetcsv($fp);
173         fclose($fp);
174         
175         $this->assertEquals($row,$duperow);
176     }
177     
178     public function testWriteError()
179     {
180         $file = SugarTestImportUtilities::createFile(3,2);
181         $importFile = new ImportFile($file,',','"');
182         
183         $row = $importFile->getNextRow();
184         $importFile->writeError('Some Error','field1','foo');
185         
186         $fp = sugar_fopen(ImportCacheFiles::getErrorFileName(),'r');
187         $errorrow = fgetcsv($fp);
188         fclose($fp);
189         
190         $this->assertEquals(array('Some Error','field1','foo',1),$errorrow);
191         
192         $fp = sugar_fopen(ImportCacheFiles::getErrorRecordsFileName(),'r');
193         $errorrecordrow = fgetcsv($fp);
194         fclose($fp);
195         
196         $this->assertEquals($row,$errorrecordrow);
197     }
198     
199     public function testWriteErrorRecord()
200     {
201         $file = SugarTestImportUtilities::createFile(3,2);
202         $importFile = new ImportFile($file,',','"');
203         
204         $row = $importFile->getNextRow();
205         $importFile->writeErrorRecord();
206         
207         $fp = sugar_fopen(ImportCacheFiles::getErrorRecordsFileName(),'r');
208         $errorrecordrow = fgetcsv($fp);
209         fclose($fp);
210         
211         $this->assertEquals($row,$errorrecordrow);
212     }
213     
214     public function testWriteStatus()
215     {
216         $file = SugarTestImportUtilities::createFile(3,2);
217         $importFile = new ImportFile($file,',','"');
218         
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();
226         
227         $fp = sugar_fopen(ImportCacheFiles::getStatusFileName(),'r');
228         $statusrow = fgetcsv($fp);
229         fclose($fp);
230         
231         $this->assertEquals(array(3,1,1,1,0,$file),$statusrow);
232     }
233     
234     public function testWriteStatusWithTwoErrorsInOneRow()
235     {
236         $file = SugarTestImportUtilities::createFile(3,2);
237         $importFile = new ImportFile($file,',','"');
238         
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();
247         
248         $fp = sugar_fopen(ImportCacheFiles::getStatusFileName(),'r');
249         $statusrow = fgetcsv($fp);
250         fclose($fp);
251         
252         $this->assertEquals(array(3,1,0,2,0,$file),$statusrow);
253         
254         $fp = sugar_fopen(ImportCacheFiles::getErrorRecordsFileName(),'r');
255         $errorrecordrow = fgetcsv($fp);
256         
257         $this->assertEquals($row,$errorrecordrow);
258         $this->assertFalse(fgetcsv($fp),'Should be only 1 record in the csv file');
259         fclose($fp);
260         
261     }
262     
263     public function testWriteStatusWithTwoUpdatedRecords()
264     {
265         $file = SugarTestImportUtilities::createFile(3,2);
266         $importFile = new ImportFile($file,',','"');
267         
268         $row = $importFile->getNextRow();
269         $importFile->markRowAsImported(false);
270         $importFile->getNextRow();
271         $importFile->markRowAsImported();
272         $importFile->getNextRow();
273         $importFile->markRowAsImported();
274         $importFile->writeStatus();
275         
276         $fp = sugar_fopen(ImportCacheFiles::getStatusFileName(),'r');
277         $statusrow = fgetcsv($fp);
278         fclose($fp);
279         
280         $this->assertEquals(array(3,0,0,2,1,$file),$statusrow);
281     }
282     
283     public function testWriteRowToLastImport()
284     {
285         $file = SugarTestImportUtilities::createFile(3,2);
286         $importFile = new ImportFile($file,',','"');
287         $record = $importFile->writeRowToLastImport("Tests","Test","TestRunner");
288         
289         $query = "SELECT * 
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'
295                             AND id = '$record'
296                             AND deleted=0";
297
298                 $result = $GLOBALS['db']->query($query);
299         
300         $this->assertNotNull($GLOBALS['db']->fetchByAssoc($result));
301         
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'
307                             AND id = '$record'
308                             AND deleted=0";
309         $GLOBALS['db']->query($query);
310     }
311 }