]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/modules/Import/UsersLastImportTest.php
Added unit tests.
[Github/sugarcrm.git] / tests / modules / Import / UsersLastImportTest.php
1 <?php
2 require_once 'modules/Import/UsersLastImport.php';
3
4 class UsersLastImportTest extends Sugar_PHPUnit_Framework_TestCase
5 {
6     private $_importModule;
7     private $_importRecordCount;
8     private $_importIds;
9     private $_usersLastImport;
10     private $_usersLastImportIds;
11     
12     public function setUp() 
13     {
14         $beanList = array();
15         $beanFiles = array();
16         require('include/modules.php');
17         $GLOBALS['beanList'] = $beanList;
18         $GLOBALS['beanFiles'] = $beanFiles;
19         
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();
27     }
28     
29     public function tearDown() 
30     {
31         $focus = $this->_loadBean($this->_importModule);
32         $GLOBALS['db']->query(
33             'DELETE FROM ' . $focus->table_name . ' 
34                 WHERE id IN (\'' . 
35                     implode("','",$this->_importIds) . '\')');
36         $GLOBALS['db']->query(
37             'DELETE FROM users_last_import 
38                 WHERE id IN (\'' . 
39                     implode("','",$this->_usersLastImportIds) . '\')');
40         SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
41         unset($GLOBALS['current_user']);
42         unset($GLOBALS['beanList']);
43         unset($GLOBALS['beanFiles']);
44     }
45     
46     private function _loadBean()
47     {
48         return loadBean($this->_importModule);
49     }
50     
51     private function _addImportedRecords()
52     {
53         for ( $i = 0; $i < $this->_importRecordCount; $i++ ) {
54             $focus = $this->_loadBean($this->_importModule);
55             $focus->name = "record $i";
56             $focus->save();
57             $this->_importIds[$i] = $focus->id;
58             
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();
65         }
66     }
67     
68     public function testMarkDeletedByUserId()
69     {
70         $this->_usersLastImport->mark_deleted_by_user_id($GLOBALS['current_user']->id);
71         
72         $query = "SELECT * FROM users_last_import 
73                     WHERE assigned_user_id = '{$GLOBALS['current_user']->id}'";
74         
75         $result = $GLOBALS['db']->query($query);
76         
77         $this->assertNull($GLOBALS['db']->fetchByAssoc($result),'There should not be any records in the table now');
78     }
79     
80     public function testUndo()
81     {
82         $this->assertTrue(
83             $this->_usersLastImport->undo(
84                 $this->_importModule
85                 )
86             );
87         
88         $focus = $this->_loadBean($this->_importModule);
89         
90         $query = "SELECT * FROM {$focus->table_name}
91                     WHERE id IN ('" . 
92                         implode("','",$this->_importIds) . "')";
93         
94         $result = $GLOBALS['db']->query($query);
95         
96         $this->assertNull($GLOBALS['db']->fetchByAssoc($result),'There should not be any records in the table now');
97     }
98     
99     /**
100      * @group bug21828
101      */
102     public function testUndoRemovedAddedEmailAddresses()
103     {
104         $time = date('Y-m-d H:i:s');
105         $unid = uniqid();
106         
107         $focus = new Account();
108         $focus->id = "Account_".$unid;
109         
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();
116         
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')");
120                                 
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')");
122
123         // setup
124         require('include/modules.php');
125         $GLOBALS['beanList'] = $beanList;
126         $GLOBALS['beanFiles'] = $beanFiles;
127         
128         $this->assertTrue(
129             $last_import->undo(
130                 $last_import->import_module
131                 )
132             );
133         
134         // teardown
135         unset($GLOBALS['beanList']);
136         unset($GLOBALS['beanFiles']);
137         
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);
141         
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);
145         
146         $GLOBALS['db']->query("DELETE FROM users_last_import WHERE id = '{$last_import->id}'");
147     }
148     
149     public function testUndoById()
150     {
151         $this->assertTrue(
152             $this->_usersLastImport->undoById(
153                 $this->_usersLastImportIds[0]
154                 )
155             );
156         
157         $focus = $this->_loadBean($this->_importModule);
158         
159         $query = "SELECT * FROM {$focus->table_name}
160                     WHERE id = '{$this->_importIds[0]}'";
161         
162         $result = $GLOBALS['db']->query($query);
163         
164         $this->assertNull($GLOBALS['db']->fetchByAssoc($result),'There should not be any records in the table now');
165
166     }
167     
168     public function testGetBeansByImport()
169     {
170         foreach ( UsersLastImport::getBeansByImport('Notes') as $objectName )
171             $this->assertEquals($objectName,'Note');
172     }
173 }