]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/modules/Import/ImportMapTest.php
Added unit tests.
[Github/sugarcrm.git] / tests / modules / Import / ImportMapTest.php
1 <?php
2 require_once 'modules/Import/ImportMap.php';
3
4 class ImportMapTest extends Sugar_PHPUnit_Framework_TestCase
5 {
6     private $_importMap;
7     
8     public function setUp() 
9     {
10         $beanList = array();
11         $beanFiles = array();
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();
18     }
19     
20     public function tearDown() 
21     {
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']);
30     }
31     
32     private function _addMapping(
33         $name      = 'test mapping for importmaptest',
34         $enclosure = '"'
35         )
36     {
37         $this->_importMap->save(
38             $GLOBALS['current_user']->id,
39             $name,
40             'TEST',
41             'other',
42             '1',
43             ',',
44             $enclosure);
45     }
46     
47     public function testSave()
48     {
49         $this->_addMapping();
50         $query = "SELECT * FROM import_maps 
51                     WHERE assigned_user_id = '{$GLOBALS['current_user']->id}'
52                         AND name = 'test mapping'
53                         AND module = 'TEST'
54                         AND source = 'other'
55                         AND has_header = '1'
56                         AND delimiter = ','
57                         AND enclosure = '\"'";
58         
59         $result = $GLOBALS['db']->query($query);
60         
61         $this->assertNull($GLOBALS['db']->fetchByAssoc($result),'Row not added');
62     }
63     
64     public function testSaveEmptyEnclosure()
65     {
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'
70                         AND module = 'TEST'
71                         AND source = 'other'
72                         AND has_header = '1'
73                         AND delimiter = ','
74                         AND enclosure = ' '";
75         
76         $result = $GLOBALS['db']->query($query);
77         
78         $this->assertNotNull($GLOBALS['db']->fetchByAssoc($result),'Row not added');
79     }
80     
81     public function testSetAndGetMapping()
82     {
83         $mapping = array(
84             'field1' => 'value1',
85             'field2' => 'value2',
86             );
87         
88         $this->_importMap->setMapping($mapping);
89         $this->_addMapping();
90         $id = $this->_importMap->id;
91         
92         $importMapRetrieve = new ImportMap();
93         $importMapRetrieve->retrieve($id, false);
94         
95         $this->assertEquals($importMapRetrieve->getMapping(),$mapping);
96     }
97     
98     public function testSetAndGetDefaultFields()
99     {
100         $mapping = array(
101             'field1' => 'value1',
102             'field2' => 'value2',
103             );
104         
105         $this->_importMap->setDefaultValues($mapping);
106         $this->_addMapping();
107         $id = $this->_importMap->id;
108         
109         $importMapRetrieve = new ImportMap();
110         $importMapRetrieve->retrieve($id, false);
111         
112         $this->assertEquals($importMapRetrieve->getDefaultValues(),$mapping);
113     }
114     
115     public function testMarkPublished()
116     {
117         $this->_addMapping();
118         $this->assertTrue($this->_importMap->mark_published(
119             $GLOBALS['current_user']->id,true));
120         $id = $this->_importMap->id;
121         
122         $query = "SELECT * FROM import_maps 
123                     WHERE id = '$id'";
124         
125         $result = $GLOBALS['db']->query($query);
126         
127         $row = $GLOBALS['db']->fetchByAssoc($result);
128         
129         $this->assertEquals($row['is_published'],'yes');
130     }
131     
132     public function testMarkPublishedNameConflict()
133     {
134         $this->_addMapping();
135         $this->_importMap->mark_published(
136             $GLOBALS['current_user']->id,true);
137         
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));
143         
144         $query = "SELECT * FROM import_maps 
145                     WHERE id = '{$this->_importMap->id}'";
146         
147         $result = $GLOBALS['db']->query($query);
148         
149         $row = $GLOBALS['db']->fetchByAssoc($result);
150         
151         $this->assertEquals($row['is_published'],'no');
152     }
153     
154     public function testMarkPublishedNameNotAdmin()
155     {
156         $GLOBALS['current_user']->is_admin = '0';
157         
158         $this->_addMapping();
159         $this->assertFalse($this->_importMap->mark_published(
160             $GLOBALS['current_user']->id,true));
161     }
162     
163     public function testMarkUnpublished()
164     {
165         $this->_addMapping();
166         $this->_importMap->mark_published(
167             $GLOBALS['current_user']->id,true);
168         $id = $this->_importMap->id;
169         
170         $importMapRetrieve = new ImportMap();
171         $importMapRetrieve->retrieve($id, false);
172         $this->assertTrue($this->_importMap->mark_published(
173             $GLOBALS['current_user']->id,false));
174         
175         $query = "SELECT * FROM import_maps 
176                     WHERE id = '$id'";
177         
178         $result = $GLOBALS['db']->query($query);
179         
180         $row = $GLOBALS['db']->fetchByAssoc($result);
181         
182         $this->assertEquals($row['is_published'],'no');
183     }
184     
185     public function testMarkUnpublishedNameConflict()
186     {
187         $this->_addMapping();
188         $this->_importMap->mark_published(
189             $GLOBALS['current_user']->id,true);
190         $id = $this->_importMap->id;
191         
192         $GLOBALS['current_user'] = SugarTestUserUtilities::createAnonymousUser();
193         $this->_importMap = new ImportMap();
194         $this->_addMapping();
195         
196         $importMapRetrieve = new ImportMap();
197         $importMapRetrieve->retrieve($id, false);
198         $this->assertFalse($this->_importMap->mark_published(
199             $GLOBALS['current_user']->id,false));
200         
201         $query = "SELECT * FROM import_maps 
202                     WHERE id = '$id'";
203         
204         $result = $GLOBALS['db']->query($query);
205         
206         $row = $GLOBALS['db']->fetchByAssoc($result);
207         
208         $this->assertEquals($row['is_published'],'yes');
209     }
210     
211     public function testMarkDeleted()
212     {
213         $this->_addMapping();
214         $id = $this->_importMap->id;
215         
216         $this->_importMap = new ImportMap();
217         $this->_importMap->mark_deleted($id);
218         
219         $query = "SELECT * FROM import_maps 
220                     WHERE id = '$id'";
221         
222         $result = $GLOBALS['db']->query($query);
223         
224         $row = $GLOBALS['db']->fetchByAssoc($result);
225         
226         $this->assertEquals($row['deleted'],'1');
227     }
228     
229     public function testMarkDeletedAdminDifferentUser()
230     {
231         $this->_addMapping();
232         $id = $this->_importMap->id;
233         
234         $GLOBALS['current_user'] = SugarTestUserUtilities::createAnonymousUser();
235         $GLOBALS['current_user']->is_admin = '1';
236         $this->_importMap = new ImportMap();
237         $this->_importMap->mark_deleted($id);
238         
239         $query = "SELECT * FROM import_maps 
240                     WHERE id = '$id'";
241         
242         $result = $GLOBALS['db']->query($query);
243         
244         $row = $GLOBALS['db']->fetchByAssoc($result);
245         
246         $this->assertEquals($row['deleted'],'1');
247     }
248     
249     public function testMarkDeletedNotAdminDifferentUser()
250     {
251         $this->_addMapping();
252         $id = $this->_importMap->id;
253         
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');
258     }
259     
260     public function testRetrieveAllByStringFields()
261     {
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');
267         
268         $objarr = $this->_importMap->retrieve_all_by_string_fields(
269             array('assigned_user_id' => $GLOBALS['current_user']->id)
270             );
271         
272         $this->assertEquals(count($objarr),3);
273         
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);
280     }
281 }