]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/modules/Import/UsersLastImportTest.php
Release 6.2.0
[Github/sugarcrm.git] / tests / modules / Import / UsersLastImportTest.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/UsersLastImport.php';
39
40 class UsersLastImportTest extends Sugar_PHPUnit_Framework_TestCase
41 {
42     private $_importModule;
43     private $_importRecordCount;
44     private $_importIds;
45     private $_usersLastImport;
46     private $_usersLastImportIds;
47     
48     public function setUp() 
49     {
50         $beanList = array();
51         $beanFiles = array();
52         require('include/modules.php');
53         $GLOBALS['beanList'] = $beanList;
54         $GLOBALS['beanFiles'] = $beanFiles;
55         
56         $GLOBALS['current_user'] = SugarTestUserUtilities::createAnonymousUser();
57         $this->_importModule = 'Notes';
58         $this->_importObject = 'Note';
59         $this->_importRecordCount = 3;
60         $this->_importIds = array();
61         $this->_usersLastImport = new UsersLastImport();
62         $this->_addImportedRecords();
63     }
64     
65     public function tearDown() 
66     {
67         $focus = $this->_loadBean($this->_importModule);
68         $GLOBALS['db']->query(
69             'DELETE FROM ' . $focus->table_name . ' 
70                 WHERE id IN (\'' . 
71                     implode("','",$this->_importIds) . '\')');
72         $GLOBALS['db']->query(
73             'DELETE FROM users_last_import 
74                 WHERE id IN (\'' . 
75                     implode("','",$this->_usersLastImportIds) . '\')');
76         SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
77         unset($GLOBALS['current_user']);
78         unset($GLOBALS['beanList']);
79         unset($GLOBALS['beanFiles']);
80     }
81     
82     private function _loadBean()
83     {
84         return loadBean($this->_importModule);
85     }
86     
87     private function _addImportedRecords()
88     {
89         for ( $i = 0; $i < $this->_importRecordCount; $i++ ) {
90             $focus = $this->_loadBean($this->_importModule);
91             $focus->name = "record $i";
92             $focus->save();
93             $this->_importIds[$i] = $focus->id;
94             
95             $last_import = new UsersLastImport();
96             $last_import->assigned_user_id = $GLOBALS['current_user']->id;
97             $last_import->import_module = $this->_importModule;
98             $last_import->bean_type = $this->_importObject;
99             $last_import->bean_id = $this->_importIds[$i];
100             $this->_usersLastImportIds[] = $last_import->save();
101         }
102     }
103     
104     public function testMarkDeletedByUserId()
105     {
106         $this->_usersLastImport->mark_deleted_by_user_id($GLOBALS['current_user']->id);
107         
108         $query = "SELECT * FROM users_last_import 
109                     WHERE assigned_user_id = '{$GLOBALS['current_user']->id}'";
110         
111         $result = $GLOBALS['db']->query($query);
112        
113         $this->assertNull($GLOBALS['db']->fetchByAssoc($result),'There should not be any records in the table now');
114     }
115     
116     public function testUndo()
117     {
118         $this->assertTrue(
119             $this->_usersLastImport->undo(
120                 $this->_importModule
121                 )
122             );
123         
124         $focus = $this->_loadBean($this->_importModule);
125         
126         $query = "SELECT * FROM {$focus->table_name}
127                     WHERE id IN ('" . 
128                         implode("','",$this->_importIds) . "')";
129         
130         $result = $GLOBALS['db']->query($query);
131         
132         $this->assertNull($GLOBALS['db']->fetchByAssoc($result),'There should not be any records in the table now');
133     }
134     
135     /**
136      * @ticket 21828
137      */
138     public function testUndoRemovedAddedEmailAddresses()
139     {
140         $time = date('Y-m-d H:i:s');
141         $unid = uniqid();
142         
143         $focus = new Account();
144         $focus->id = "Account_".$unid;
145         
146         $last_import = new UsersLastImport();
147         $last_import->assigned_user_id = $GLOBALS['current_user']->id;
148         $last_import->import_module = 'Accounts';
149         $last_import->bean_type = 'Account';
150         $last_import->bean_id = $focus->id;
151         $last_import->save();
152         
153         $this->email_addr_bean_rel_id = 'email_addr_bean_rel_'.$unid;
154         $this->email_address_id = 'email_address_id_'.$unid;
155         $GLOBALS['db']->query("insert into email_addr_bean_rel (id , email_address_id, bean_id, bean_module, primary_address, date_created , date_modified) values ('{$this->email_addr_bean_rel_id}', '{$this->email_address_id}', '{$focus->id}', 'Accounts', 1, '$time', '$time')");
156                                 
157         $GLOBALS['db']->query("insert into email_addresses (id , email_address, email_address_caps, date_created, date_modified) values ('{$this->email_address_id}', 'test@g.com', 'TEST@G.COM', '$time', '$time')");
158
159         // setup
160         require('include/modules.php');
161         $GLOBALS['beanList'] = $beanList;
162         $GLOBALS['beanFiles'] = $beanFiles;
163         
164         $this->assertTrue(
165             $last_import->undo(
166                 $last_import->import_module
167                 )
168             );
169         
170         // teardown
171         unset($GLOBALS['beanList']);
172         unset($GLOBALS['beanFiles']);
173         
174         $result = $GLOBALS['db']->query("SELECT * FROM email_addr_bean_rel where id = '{$this->email_addr_bean_rel_id}'");
175                 $rows = $GLOBALS['db']->fetchByAssoc($result);
176         $this->assertNull($rows);
177         
178         $result = $GLOBALS['db']->query("SELECT * FROM email_addresses where id = '{$this->email_address_id}'");
179                 $rows = $GLOBALS['db']->fetchByAssoc($result);
180         $this->assertNull($rows);
181         
182         $GLOBALS['db']->query("DELETE FROM users_last_import WHERE id = '{$last_import->id}'");
183     }
184     
185     public function testUndoById()
186     {
187         $this->assertTrue(
188             $this->_usersLastImport->undoById(
189                 $this->_usersLastImportIds[0]
190                 )
191             );
192         
193         $focus = $this->_loadBean($this->_importModule);
194         
195         $query = "SELECT * FROM {$focus->table_name}
196                     WHERE id = '{$this->_importIds[0]}'";
197         
198         $result = $GLOBALS['db']->query($query);
199         
200         $this->assertNull($GLOBALS['db']->fetchByAssoc($result),'There should not be any records in the table now');
201
202     }
203     
204     public function testGetBeansByImport()
205     {
206         foreach ( UsersLastImport::getBeansByImport('Notes') as $objectName )
207             $this->assertEquals($objectName,'Note');
208     }
209 }