2 require_once 'modules/Import/ImportMap.php';
4 class ImportMapTest extends Sugar_PHPUnit_Framework_TestCase
8 public function setUp()
12 require('include/modules.php');
13 $GLOBALS['beanList'] = $beanList;
14 $GLOBALS['beanFiles'] = $beanFiles;
15 $GLOBALS['current_user'] = SugarTestUserUtilities::createAnonymousUser();
16 $GLOBALS['current_user']->is_admin = '1';
17 $this->_importMap = new ImportMap();
20 public function tearDown()
22 unset($GLOBALS['beanList']);
23 unset($GLOBALS['beanFiles']);
24 $GLOBALS['db']->query(
25 'DELETE FROM import_maps
26 WHERE assigned_user_id IN (\'' .
27 implode("','",SugarTestUserUtilities::getCreatedUserIds()) . '\')');
28 SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
29 unset($GLOBALS['current_user']);
32 private function _addMapping(
33 $name = 'test mapping for importmaptest',
37 $this->_importMap->save(
38 $GLOBALS['current_user']->id,
47 public function testSave()
50 $query = "SELECT * FROM import_maps
51 WHERE assigned_user_id = '{$GLOBALS['current_user']->id}'
52 AND name = 'test mapping'
57 AND enclosure = '\"'";
59 $result = $GLOBALS['db']->query($query);
61 $this->assertNull($GLOBALS['db']->fetchByAssoc($result),'Row not added');
64 public function testSaveEmptyEnclosure()
66 $this->_addMapping('test mapping','');
67 $query = "SELECT * FROM import_maps
68 WHERE assigned_user_id = '{$GLOBALS['current_user']->id}'
69 AND name = 'test mapping'
76 $result = $GLOBALS['db']->query($query);
78 $this->assertNotNull($GLOBALS['db']->fetchByAssoc($result),'Row not added');
81 public function testSetAndGetMapping()
88 $this->_importMap->setMapping($mapping);
90 $id = $this->_importMap->id;
92 $importMapRetrieve = new ImportMap();
93 $importMapRetrieve->retrieve($id, false);
95 $this->assertEquals($importMapRetrieve->getMapping(),$mapping);
98 public function testSetAndGetDefaultFields()
101 'field1' => 'value1',
102 'field2' => 'value2',
105 $this->_importMap->setDefaultValues($mapping);
106 $this->_addMapping();
107 $id = $this->_importMap->id;
109 $importMapRetrieve = new ImportMap();
110 $importMapRetrieve->retrieve($id, false);
112 $this->assertEquals($importMapRetrieve->getDefaultValues(),$mapping);
115 public function testMarkPublished()
117 $this->_addMapping();
118 $this->assertTrue($this->_importMap->mark_published(
119 $GLOBALS['current_user']->id,true));
120 $id = $this->_importMap->id;
122 $query = "SELECT * FROM import_maps
125 $result = $GLOBALS['db']->query($query);
127 $row = $GLOBALS['db']->fetchByAssoc($result);
129 $this->assertEquals($row['is_published'],'yes');
132 public function testMarkPublishedNameConflict()
134 $this->_addMapping();
135 $this->_importMap->mark_published(
136 $GLOBALS['current_user']->id,true);
138 $GLOBALS['current_user'] = SugarTestUserUtilities::createAnonymousUser();
139 $this->_importMap = new ImportMap();
140 $this->_addMapping();
141 $this->assertFalse($this->_importMap->mark_published(
142 $GLOBALS['current_user']->id,true));
144 $query = "SELECT * FROM import_maps
145 WHERE id = '{$this->_importMap->id}'";
147 $result = $GLOBALS['db']->query($query);
149 $row = $GLOBALS['db']->fetchByAssoc($result);
151 $this->assertEquals($row['is_published'],'no');
154 public function testMarkPublishedNameNotAdmin()
156 $GLOBALS['current_user']->is_admin = '0';
158 $this->_addMapping();
159 $this->assertFalse($this->_importMap->mark_published(
160 $GLOBALS['current_user']->id,true));
163 public function testMarkUnpublished()
165 $this->_addMapping();
166 $this->_importMap->mark_published(
167 $GLOBALS['current_user']->id,true);
168 $id = $this->_importMap->id;
170 $importMapRetrieve = new ImportMap();
171 $importMapRetrieve->retrieve($id, false);
172 $this->assertTrue($this->_importMap->mark_published(
173 $GLOBALS['current_user']->id,false));
175 $query = "SELECT * FROM import_maps
178 $result = $GLOBALS['db']->query($query);
180 $row = $GLOBALS['db']->fetchByAssoc($result);
182 $this->assertEquals($row['is_published'],'no');
185 public function testMarkUnpublishedNameConflict()
187 $this->_addMapping();
188 $this->_importMap->mark_published(
189 $GLOBALS['current_user']->id,true);
190 $id = $this->_importMap->id;
192 $GLOBALS['current_user'] = SugarTestUserUtilities::createAnonymousUser();
193 $this->_importMap = new ImportMap();
194 $this->_addMapping();
196 $importMapRetrieve = new ImportMap();
197 $importMapRetrieve->retrieve($id, false);
198 $this->assertFalse($this->_importMap->mark_published(
199 $GLOBALS['current_user']->id,false));
201 $query = "SELECT * FROM import_maps
204 $result = $GLOBALS['db']->query($query);
206 $row = $GLOBALS['db']->fetchByAssoc($result);
208 $this->assertEquals($row['is_published'],'yes');
211 public function testMarkDeleted()
213 $this->_addMapping();
214 $id = $this->_importMap->id;
216 $this->_importMap = new ImportMap();
217 $this->_importMap->mark_deleted($id);
219 $query = "SELECT * FROM import_maps
222 $result = $GLOBALS['db']->query($query);
224 $row = $GLOBALS['db']->fetchByAssoc($result);
226 $this->assertEquals($row['deleted'],'1');
229 public function testMarkDeletedAdminDifferentUser()
231 $this->_addMapping();
232 $id = $this->_importMap->id;
234 $GLOBALS['current_user'] = SugarTestUserUtilities::createAnonymousUser();
235 $GLOBALS['current_user']->is_admin = '1';
236 $this->_importMap = new ImportMap();
237 $this->_importMap->mark_deleted($id);
239 $query = "SELECT * FROM import_maps
242 $result = $GLOBALS['db']->query($query);
244 $row = $GLOBALS['db']->fetchByAssoc($result);
246 $this->assertEquals($row['deleted'],'1');
249 public function testMarkDeletedNotAdminDifferentUser()
251 $this->_addMapping();
252 $id = $this->_importMap->id;
254 $GLOBALS['current_user'] = SugarTestUserUtilities::createAnonymousUser();
255 $GLOBALS['current_user']->is_admin = '0';
256 $this->_importMap = new ImportMap();
257 $this->assertFalse($this->_importMap->mark_deleted($id),'Record should not be allowed to be deleted');
260 public function testRetrieveAllByStringFields()
262 $this->_addMapping();
263 $this->_importMap = new ImportMap();
264 $this->_addMapping('test mapping 2');
265 $this->_importMap = new ImportMap();
266 $this->_addMapping('test mapping 3');
268 $objarr = $this->_importMap->retrieve_all_by_string_fields(
269 array('assigned_user_id' => $GLOBALS['current_user']->id)
272 $this->assertEquals(count($objarr),3);
274 $this->assertEquals($objarr[0]->assigned_user_id,
275 $GLOBALS['current_user']->id);
276 $this->assertEquals($objarr[1]->assigned_user_id,
277 $GLOBALS['current_user']->id);
278 $this->assertEquals($objarr[2]->assigned_user_id,
279 $GLOBALS['current_user']->id);