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/UsersLastImport.php';
40 class UsersLastImportTest extends Sugar_PHPUnit_Framework_TestCase
42 private $_importModule;
43 private $_importRecordCount;
45 private $_usersLastImport;
46 private $_usersLastImportIds;
48 public function setUp()
52 require('include/modules.php');
53 $GLOBALS['beanList'] = $beanList;
54 $GLOBALS['beanFiles'] = $beanFiles;
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();
65 public function tearDown()
67 $focus = $this->_loadBean($this->_importModule);
68 $GLOBALS['db']->query(
69 'DELETE FROM ' . $focus->table_name . '
71 implode("','",$this->_importIds) . '\')');
72 $GLOBALS['db']->query(
73 'DELETE FROM users_last_import
75 implode("','",$this->_usersLastImportIds) . '\')');
76 SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
77 unset($GLOBALS['current_user']);
78 unset($GLOBALS['beanList']);
79 unset($GLOBALS['beanFiles']);
82 private function _loadBean()
84 return loadBean($this->_importModule);
87 private function _addImportedRecords()
89 for ( $i = 0; $i < $this->_importRecordCount; $i++ ) {
90 $focus = $this->_loadBean($this->_importModule);
91 $focus->name = "record $i";
93 $this->_importIds[$i] = $focus->id;
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();
104 public function testMarkDeletedByUserId()
106 $this->_usersLastImport->mark_deleted_by_user_id($GLOBALS['current_user']->id);
108 $query = "SELECT * FROM users_last_import
109 WHERE assigned_user_id = '{$GLOBALS['current_user']->id}'";
111 $result = $GLOBALS['db']->query($query);
113 $this->assertFalse($GLOBALS['db']->fetchByAssoc($result),'There should not be any records in the table now');
116 public function testUndo()
119 $this->_usersLastImport->undo(
124 $focus = $this->_loadBean($this->_importModule);
126 $query = "SELECT * FROM {$focus->table_name}
128 implode("','",$this->_importIds) . "')";
130 $result = $GLOBALS['db']->query($query);
132 $this->assertFalse($GLOBALS['db']->fetchByAssoc($result),'There should not be any records in the table now');
138 public function testUndoRemovedAddedEmailAddresses()
140 $time = date('Y-m-d H:i:s');
143 $focus = new Account();
144 $focus->id = "Account_".$unid;
147 $last_import = new UsersLastImport();
148 $last_import->assigned_user_id = $GLOBALS['current_user']->id;
149 $last_import->import_module = 'Accounts';
150 $last_import->bean_type = 'Account';
151 $last_import->bean_id = $focus->id;
152 $last_import->save();
154 $this->email_addr_bean_rel_id = 'email_addr_bean_rel_'.$unid;
155 $this->email_address_id = 'email_address_id_'.$unid;
156 $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')");
158 $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')");
161 require('include/modules.php');
162 $GLOBALS['beanList'] = $beanList;
163 $GLOBALS['beanFiles'] = $beanFiles;
167 $last_import->import_module
172 unset($GLOBALS['beanList']);
173 unset($GLOBALS['beanFiles']);
175 $result = $GLOBALS['db']->query("SELECT * FROM email_addr_bean_rel where id = '{$this->email_addr_bean_rel_id}'");
176 $rows = $GLOBALS['db']->fetchByAssoc($result);
177 $this->assertFalse($rows);
179 $result = $GLOBALS['db']->query("SELECT * FROM email_addresses where id = '{$this->email_address_id}'");
180 $rows = $GLOBALS['db']->fetchByAssoc($result);
181 $this->assertFalse($rows);
183 $GLOBALS['db']->query("DELETE FROM users_last_import WHERE id = '{$last_import->id}'");
186 public function testUndoById()
189 $this->_usersLastImport->undoById(
190 $this->_usersLastImportIds[0]
194 $focus = $this->_loadBean($this->_importModule);
196 $query = "SELECT * FROM {$focus->table_name}
197 WHERE id = '{$this->_importIds[0]}'";
199 $result = $GLOBALS['db']->query($query);
201 $this->assertFalse($GLOBALS['db']->fetchByAssoc($result),'There should not be any records in the table now');
205 public function testGetBeansByImport()
207 foreach ( UsersLastImport::getBeansByImport('Notes') as $objectName )
208 $this->assertEquals($objectName,'Note');