2 require_once 'modules/Import/UsersLastImport.php';
4 class UsersLastImportTest extends Sugar_PHPUnit_Framework_TestCase
6 private $_importModule;
7 private $_importRecordCount;
9 private $_usersLastImport;
10 private $_usersLastImportIds;
12 public function setUp()
16 require('include/modules.php');
17 $GLOBALS['beanList'] = $beanList;
18 $GLOBALS['beanFiles'] = $beanFiles;
20 $GLOBALS['current_user'] = SugarTestUserUtilities::createAnonymousUser();
21 $this->_importModule = 'Notes';
22 $this->_importObject = 'Note';
23 $this->_importRecordCount = 3;
24 $this->_importIds = array();
25 $this->_usersLastImport = new UsersLastImport();
26 $this->_addImportedRecords();
29 public function tearDown()
31 $focus = $this->_loadBean($this->_importModule);
32 $GLOBALS['db']->query(
33 'DELETE FROM ' . $focus->table_name . '
35 implode("','",$this->_importIds) . '\')');
36 $GLOBALS['db']->query(
37 'DELETE FROM users_last_import
39 implode("','",$this->_usersLastImportIds) . '\')');
40 SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
41 unset($GLOBALS['current_user']);
42 unset($GLOBALS['beanList']);
43 unset($GLOBALS['beanFiles']);
46 private function _loadBean()
48 return loadBean($this->_importModule);
51 private function _addImportedRecords()
53 for ( $i = 0; $i < $this->_importRecordCount; $i++ ) {
54 $focus = $this->_loadBean($this->_importModule);
55 $focus->name = "record $i";
57 $this->_importIds[$i] = $focus->id;
59 $last_import = new UsersLastImport();
60 $last_import->assigned_user_id = $GLOBALS['current_user']->id;
61 $last_import->import_module = $this->_importModule;
62 $last_import->bean_type = $this->_importObject;
63 $last_import->bean_id = $this->_importIds[$i];
64 $this->_usersLastImportIds[] = $last_import->save();
68 public function testMarkDeletedByUserId()
70 $this->_usersLastImport->mark_deleted_by_user_id($GLOBALS['current_user']->id);
72 $query = "SELECT * FROM users_last_import
73 WHERE assigned_user_id = '{$GLOBALS['current_user']->id}'";
75 $result = $GLOBALS['db']->query($query);
77 $this->assertNull($GLOBALS['db']->fetchByAssoc($result),'There should not be any records in the table now');
80 public function testUndo()
83 $this->_usersLastImport->undo(
88 $focus = $this->_loadBean($this->_importModule);
90 $query = "SELECT * FROM {$focus->table_name}
92 implode("','",$this->_importIds) . "')";
94 $result = $GLOBALS['db']->query($query);
96 $this->assertNull($GLOBALS['db']->fetchByAssoc($result),'There should not be any records in the table now');
102 public function testUndoRemovedAddedEmailAddresses()
104 $time = date('Y-m-d H:i:s');
107 $focus = new Account();
108 $focus->id = "Account_".$unid;
110 $last_import = new UsersLastImport();
111 $last_import->assigned_user_id = $GLOBALS['current_user']->id;
112 $last_import->import_module = 'Accounts';
113 $last_import->bean_type = 'Account';
114 $last_import->bean_id = $focus->id;
115 $last_import->save();
117 $this->email_addr_bean_rel_id = 'email_addr_bean_rel_'.$unid;
118 $this->email_address_id = 'email_address_id_'.$unid;
119 $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')");
121 $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')");
124 require('include/modules.php');
125 $GLOBALS['beanList'] = $beanList;
126 $GLOBALS['beanFiles'] = $beanFiles;
130 $last_import->import_module
135 unset($GLOBALS['beanList']);
136 unset($GLOBALS['beanFiles']);
138 $result = $GLOBALS['db']->query("SELECT * FROM email_addr_bean_rel where id = '{$this->email_addr_bean_rel_id}'");
139 $rows = $GLOBALS['db']->fetchByAssoc($result);
140 $this->assertNull($rows);
142 $result = $GLOBALS['db']->query("SELECT * FROM email_addresses where id = '{$this->email_address_id}'");
143 $rows = $GLOBALS['db']->fetchByAssoc($result);
144 $this->assertNull($rows);
146 $GLOBALS['db']->query("DELETE FROM users_last_import WHERE id = '{$last_import->id}'");
149 public function testUndoById()
152 $this->_usersLastImport->undoById(
153 $this->_usersLastImportIds[0]
157 $focus = $this->_loadBean($this->_importModule);
159 $query = "SELECT * FROM {$focus->table_name}
160 WHERE id = '{$this->_importIds[0]}'";
162 $result = $GLOBALS['db']->query($query);
164 $this->assertNull($GLOBALS['db']->fetchByAssoc($result),'There should not be any records in the table now');
168 public function testGetBeansByImport()
170 foreach ( UsersLastImport::getBeansByImport('Notes') as $objectName )
171 $this->assertEquals($objectName,'Note');