]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/modules/Import/ImportMapTest.php
Release 6.2.0
[Github/sugarcrm.git] / tests / modules / Import / ImportMapTest.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/ImportMap.php';
39
40 class ImportMapTest extends Sugar_PHPUnit_Framework_TestCase
41 {
42     private $_importMap;
43     
44     public function setUp() 
45     {
46         $beanList = array();
47         $beanFiles = array();
48         require('include/modules.php');
49         $GLOBALS['beanList'] = $beanList;
50         $GLOBALS['beanFiles'] = $beanFiles;
51         $GLOBALS['current_user'] = SugarTestUserUtilities::createAnonymousUser();
52         $GLOBALS['current_user']->is_admin = '1';
53         $this->_importMap = new ImportMap();
54     }
55     
56     public function tearDown() 
57     {
58         unset($GLOBALS['beanList']);
59         unset($GLOBALS['beanFiles']);
60         $GLOBALS['db']->query(
61             'DELETE FROM import_maps 
62                 WHERE assigned_user_id IN (\'' . 
63                     implode("','",SugarTestUserUtilities::getCreatedUserIds()) . '\')');
64         SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
65         unset($GLOBALS['current_user']);
66     }
67     
68     private function _addMapping(
69         $name      = 'test mapping for importmaptest',
70         $enclosure = '"'
71         )
72     {
73         $this->_importMap->save(
74             $GLOBALS['current_user']->id,
75             $name,
76             'TEST',
77             'other',
78             '1',
79             ',',
80             $enclosure);
81     }
82     
83     public function testSave()
84     {
85         $this->_addMapping();
86         $query = "SELECT * FROM import_maps 
87                     WHERE assigned_user_id = '{$GLOBALS['current_user']->id}'
88                         AND name = 'test mapping'
89                         AND module = 'TEST'
90                         AND source = 'other'
91                         AND has_header = '1'
92                         AND delimiter = ','
93                         AND enclosure = '\"'";
94         
95         $result = $GLOBALS['db']->query($query);
96         
97         $this->assertNull($GLOBALS['db']->fetchByAssoc($result),'Row not added');
98     }
99     
100     public function testSaveEmptyEnclosure()
101     {
102         $this->_addMapping('test mapping','');
103         $query = "SELECT * FROM import_maps 
104                     WHERE assigned_user_id = '{$GLOBALS['current_user']->id}'
105                         AND name = 'test mapping'
106                         AND module = 'TEST'
107                         AND source = 'other'
108                         AND has_header = '1'
109                         AND delimiter = ','
110                         AND enclosure = ' '";
111         
112         $result = $GLOBALS['db']->query($query);
113         
114         $this->assertNotNull($GLOBALS['db']->fetchByAssoc($result),'Row not added');
115     }
116     
117     public function testSetAndGetMapping()
118     {
119         $mapping = array(
120             'field1' => 'value1',
121             'field2' => 'value2',
122             );
123         
124         $this->_importMap->setMapping($mapping);
125         $this->_addMapping();
126         $id = $this->_importMap->id;
127         
128         $importMapRetrieve = new ImportMap();
129         $importMapRetrieve->retrieve($id, false);
130         
131         $this->assertEquals($importMapRetrieve->getMapping(),$mapping);
132     }
133     
134     public function testSetAndGetDefaultFields()
135     {
136         $mapping = array(
137             'field1' => 'value1',
138             'field2' => 'value2',
139             );
140         
141         $this->_importMap->setDefaultValues($mapping);
142         $this->_addMapping();
143         $id = $this->_importMap->id;
144         
145         $importMapRetrieve = new ImportMap();
146         $importMapRetrieve->retrieve($id, false);
147         
148         $this->assertEquals($importMapRetrieve->getDefaultValues(),$mapping);
149     }
150     
151     public function testMarkPublished()
152     {
153         $this->_addMapping();
154         $this->assertTrue($this->_importMap->mark_published(
155             $GLOBALS['current_user']->id,true));
156         $id = $this->_importMap->id;
157         
158         $query = "SELECT * FROM import_maps 
159                     WHERE id = '$id'";
160         
161         $result = $GLOBALS['db']->query($query);
162         
163         $row = $GLOBALS['db']->fetchByAssoc($result);
164         
165         $this->assertEquals($row['is_published'],'yes');
166     }
167     
168     public function testMarkPublishedNameConflict()
169     {
170         $this->_addMapping();
171         $this->_importMap->mark_published(
172             $GLOBALS['current_user']->id,true);
173         
174         $GLOBALS['current_user'] = SugarTestUserUtilities::createAnonymousUser();
175         $this->_importMap = new ImportMap();
176         $this->_addMapping();
177         $this->assertFalse($this->_importMap->mark_published(
178             $GLOBALS['current_user']->id,true));
179         
180         $query = "SELECT * FROM import_maps 
181                     WHERE id = '{$this->_importMap->id}'";
182         
183         $result = $GLOBALS['db']->query($query);
184         
185         $row = $GLOBALS['db']->fetchByAssoc($result);
186         
187         $this->assertEquals($row['is_published'],'no');
188     }
189     
190     public function testMarkPublishedNameNotAdmin()
191     {
192         $GLOBALS['current_user']->is_admin = '0';
193         
194         $this->_addMapping();
195         $this->assertFalse($this->_importMap->mark_published(
196             $GLOBALS['current_user']->id,true));
197     }
198     
199     public function testMarkUnpublished()
200     {
201         $this->_addMapping();
202         $this->_importMap->mark_published(
203             $GLOBALS['current_user']->id,true);
204         $id = $this->_importMap->id;
205         
206         $importMapRetrieve = new ImportMap();
207         $importMapRetrieve->retrieve($id, false);
208         $this->assertTrue($this->_importMap->mark_published(
209             $GLOBALS['current_user']->id,false));
210         
211         $query = "SELECT * FROM import_maps 
212                     WHERE id = '$id'";
213         
214         $result = $GLOBALS['db']->query($query);
215         
216         $row = $GLOBALS['db']->fetchByAssoc($result);
217         
218         $this->assertEquals($row['is_published'],'no');
219     }
220     
221     public function testMarkUnpublishedNameConflict()
222     {
223         $this->_addMapping();
224         $this->_importMap->mark_published(
225             $GLOBALS['current_user']->id,true);
226         $id = $this->_importMap->id;
227         
228         $GLOBALS['current_user'] = SugarTestUserUtilities::createAnonymousUser();
229         $this->_importMap = new ImportMap();
230         $this->_addMapping();
231         
232         $importMapRetrieve = new ImportMap();
233         $importMapRetrieve->retrieve($id, false);
234         $this->assertFalse($this->_importMap->mark_published(
235             $GLOBALS['current_user']->id,false));
236         
237         $query = "SELECT * FROM import_maps 
238                     WHERE id = '$id'";
239         
240         $result = $GLOBALS['db']->query($query);
241         
242         $row = $GLOBALS['db']->fetchByAssoc($result);
243         
244         $this->assertEquals($row['is_published'],'yes');
245     }
246     
247     public function testMarkDeleted()
248     {
249         $this->_addMapping();
250         $id = $this->_importMap->id;
251         
252         $this->_importMap = new ImportMap();
253         $this->_importMap->mark_deleted($id);
254         
255         $query = "SELECT * FROM import_maps 
256                     WHERE id = '$id'";
257         
258         $result = $GLOBALS['db']->query($query);
259         
260         $row = $GLOBALS['db']->fetchByAssoc($result);
261         
262         $this->assertEquals($row['deleted'],'1');
263     }
264     
265     public function testMarkDeletedAdminDifferentUser()
266     {
267         $this->_addMapping();
268         $id = $this->_importMap->id;
269         
270         $GLOBALS['current_user'] = SugarTestUserUtilities::createAnonymousUser();
271         $GLOBALS['current_user']->is_admin = '1';
272         $this->_importMap = new ImportMap();
273         $this->_importMap->mark_deleted($id);
274         
275         $query = "SELECT * FROM import_maps 
276                     WHERE id = '$id'";
277         
278         $result = $GLOBALS['db']->query($query);
279         
280         $row = $GLOBALS['db']->fetchByAssoc($result);
281         
282         $this->assertEquals($row['deleted'],'1');
283     }
284     
285     public function testMarkDeletedNotAdminDifferentUser()
286     {
287         $this->_addMapping();
288         $id = $this->_importMap->id;
289         
290         $GLOBALS['current_user'] = SugarTestUserUtilities::createAnonymousUser();
291         $GLOBALS['current_user']->is_admin = '0';
292         $this->_importMap = new ImportMap();
293         $this->assertFalse($this->_importMap->mark_deleted($id),'Record should not be allowed to be deleted');
294     }
295     
296     public function testRetrieveAllByStringFields()
297     {
298         $this->_addMapping();
299         $this->_importMap = new ImportMap();
300         $this->_addMapping('test mapping 2');
301         $this->_importMap = new ImportMap();
302         $this->_addMapping('test mapping 3');
303         
304         $objarr = $this->_importMap->retrieve_all_by_string_fields(
305             array('assigned_user_id' => $GLOBALS['current_user']->id)
306             );
307         
308         $this->assertEquals(count($objarr),3);
309         
310         $this->assertEquals($objarr[0]->assigned_user_id,
311             $GLOBALS['current_user']->id);
312         $this->assertEquals($objarr[1]->assigned_user_id,
313             $GLOBALS['current_user']->id);
314         $this->assertEquals($objarr[2]->assigned_user_id,
315             $GLOBALS['current_user']->id);
316     }
317 }