2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2013 SugarCRM Inc.
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.
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
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
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.
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.
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 ********************************************************************************/
38 require_once 'modules/Import/maps/ImportMap.php';
40 class ImportMapTest extends Sugar_PHPUnit_Framework_TestCase
44 public function setUp()
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();
56 public function tearDown()
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']);
68 private function _addMapping(
69 $name = 'test mapping for importmaptest',
73 $this->_importMap->save(
74 $GLOBALS['current_user']->id,
83 public function testSave()
86 $query = "SELECT * FROM import_maps
87 WHERE assigned_user_id = '{$GLOBALS['current_user']->id}'
88 AND name = 'test mapping'
93 AND enclosure = '\"'";
95 $result = $GLOBALS['db']->query($query);
97 $this->assertFalse($GLOBALS['db']->fetchByAssoc($result),'Row not added');
100 public function testSaveEmptyEnclosure()
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'
110 AND enclosure = ' '";
112 $result = $GLOBALS['db']->query($query);
114 $this->assertTrue($GLOBALS['db']->fetchByAssoc($result) != false,'Row not added');
117 public function testSetAndGetMapping()
120 'field1' => 'value1',
121 'field2' => 'value2',
124 $this->_importMap->setMapping($mapping);
125 $this->_addMapping();
126 $id = $this->_importMap->id;
128 $importMapRetrieve = new ImportMap();
129 $importMapRetrieve->retrieve($id, false);
131 $this->assertEquals($importMapRetrieve->getMapping(),$mapping);
134 public function testSetAndGetDefaultFields()
137 'field1' => 'value1',
138 'field2' => 'value2',
141 $this->_importMap->setDefaultValues($mapping);
142 $this->_addMapping();
143 $id = $this->_importMap->id;
145 $importMapRetrieve = new ImportMap();
146 $importMapRetrieve->retrieve($id, false);
148 $this->assertEquals($importMapRetrieve->getDefaultValues(),$mapping);
151 public function testMarkPublished()
153 $this->_addMapping();
154 $this->assertTrue($this->_importMap->mark_published(
155 $GLOBALS['current_user']->id,true));
156 $id = $this->_importMap->id;
158 $query = "SELECT * FROM import_maps
161 $result = $GLOBALS['db']->query($query);
163 $row = $GLOBALS['db']->fetchByAssoc($result);
165 $this->assertEquals($row['is_published'],'yes');
168 public function testMarkPublishedNameConflict()
170 $this->_addMapping();
171 $this->_importMap->mark_published(
172 $GLOBALS['current_user']->id,true);
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));
180 $query = "SELECT * FROM import_maps
181 WHERE id = '{$this->_importMap->id}'";
183 $result = $GLOBALS['db']->query($query);
185 $row = $GLOBALS['db']->fetchByAssoc($result);
187 $this->assertEquals($row['is_published'],'no');
190 public function testMarkPublishedNameNotAdmin()
192 $GLOBALS['current_user']->is_admin = '0';
194 $this->_addMapping();
195 $this->assertFalse($this->_importMap->mark_published(
196 $GLOBALS['current_user']->id,true));
199 public function testMarkUnpublished()
201 $this->_addMapping();
202 $this->_importMap->mark_published(
203 $GLOBALS['current_user']->id,true);
204 $id = $this->_importMap->id;
206 $importMapRetrieve = new ImportMap();
207 $importMapRetrieve->retrieve($id, false);
208 $this->assertTrue($this->_importMap->mark_published(
209 $GLOBALS['current_user']->id,false));
211 $query = "SELECT * FROM import_maps
214 $result = $GLOBALS['db']->query($query);
216 $row = $GLOBALS['db']->fetchByAssoc($result);
218 $this->assertEquals($row['is_published'],'no');
221 public function testMarkUnpublishedNameConflict()
223 $this->_addMapping();
224 $this->_importMap->mark_published(
225 $GLOBALS['current_user']->id,true);
226 $id = $this->_importMap->id;
228 $GLOBALS['current_user'] = SugarTestUserUtilities::createAnonymousUser();
229 $this->_importMap = new ImportMap();
230 $this->_addMapping();
232 $importMapRetrieve = new ImportMap();
233 $importMapRetrieve->retrieve($id, false);
234 $this->assertFalse($this->_importMap->mark_published(
235 $GLOBALS['current_user']->id,false));
237 $query = "SELECT * FROM import_maps
240 $result = $GLOBALS['db']->query($query);
242 $row = $GLOBALS['db']->fetchByAssoc($result);
244 $this->assertEquals($row['is_published'],'yes');
247 public function testMarkDeleted()
249 $this->_addMapping();
250 $id = $this->_importMap->id;
252 $this->_importMap = new ImportMap();
253 $this->_importMap->mark_deleted($id);
255 $query = "SELECT * FROM import_maps
258 $result = $GLOBALS['db']->query($query);
260 $row = $GLOBALS['db']->fetchByAssoc($result);
262 $this->assertEquals($row['deleted'],'1');
265 public function testMarkDeletedAdminDifferentUser()
267 $this->_addMapping();
268 $id = $this->_importMap->id;
270 $GLOBALS['current_user'] = SugarTestUserUtilities::createAnonymousUser();
271 $GLOBALS['current_user']->is_admin = '1';
272 $this->_importMap = new ImportMap();
273 $this->_importMap->mark_deleted($id);
275 $query = "SELECT * FROM import_maps
278 $result = $GLOBALS['db']->query($query);
280 $row = $GLOBALS['db']->fetchByAssoc($result);
282 $this->assertEquals($row['deleted'],'1');
285 public function testMarkDeletedNotAdminDifferentUser()
287 $this->_addMapping();
288 $id = $this->_importMap->id;
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');
296 public function testRetrieveAllByStringFields()
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');
304 $objarr = $this->_importMap->retrieve_all_by_string_fields(
305 array('assigned_user_id' => $GLOBALS['current_user']->id)
308 $this->assertEquals(count($objarr),3);
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);