]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/modules/MassUpdate/Bug51596Test.php
Release $ver
[Github/sugarcrm.git] / tests / modules / MassUpdate / Bug51596Test.php
1 <?php
2 /*********************************************************************************
3  * SugarCRM Community Edition is a customer relationship management program developed by
4  * SugarCRM, Inc. Copyright (C) 2004-2012 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 /**
39 * @ticket 51596
40 */
41 class Bug51596Test extends Sugar_PHPUnit_Framework_TestCase
42 {
43     /**
44 * @var Contact
45 */
46     protected $contact1,
47         $contact2;
48
49     /**
50 * @var Account
51 */
52     protected $account1,
53         $account2;
54
55     protected $field_name = 'bug51596test';
56
57     /**
58 * Sets up the fixture, for example, open a network connection.
59 * This method is called before a test is executed.
60 *
61 * @return void
62 */
63     public function setUp()
64     {
65         $this->markTestIncomplete("Disabling broken test on CI. Working with Sergei to get it fixed");
66
67         $GLOBALS['current_user'] = SugarTestUserUtilities::createAnonymousUser(true, true);
68
69         // add an extra relationship that will be used for search
70         self::registerExtension('Contacts', 'bug51596test.php', array(
71             'Contact' => array(
72                 'fields' => array(
73                     $this->field_name => array (
74                         'name' => $this->field_name,
75                         'rname' => 'name',
76                         'id_name' => 'account_id',
77                         'join_name' => 'accounts',
78                         'type' => 'relate',
79                         'link' => 'accounts',
80                         'table' => 'accounts',
81                         'module' => 'Accounts',
82                         'source' => 'non-db',
83                     ),
84                 ),
85             ),
86         ));
87
88         // this is needed for newly created extension to be loaded for new beans
89         $_SESSION['developerMode'] = true;
90
91         // create a set of contacts and related accounts
92         $this->contact1 = new Contact();
93         $this->contact1->do_not_call = 0;
94         $this->contact1->save();
95
96         $this->contact2 = new Contact();
97         $this->contact2->do_not_call = 0;
98         $this->contact2->save();
99
100         $this->account1 = new Account();
101         $this->account1->name = 'Bug51596Test_Account1';
102         $this->account1->save();
103
104         $this->account2 = new Account();
105         $this->account2->name = 'Bug51596Test_Account2';
106         $this->account2->save();
107
108         $this->contact1->load_relationship('accounts');
109         $this->contact2->load_relationship('accounts');
110
111         /** @var Link2 $accounts1 */
112         $accounts1 = $this->contact1->accounts;
113         $accounts1->add(array($this->account1->id));
114
115         /** @var Link2 $accounts2 */
116         $accounts2 = $this->contact2->accounts;
117         $accounts2->add(array($this->account2->id));
118
119         // will update "do_not_call" attribute of found contacts
120         $_REQUEST['massupdate'] = 'true';
121         $_REQUEST['entire'] = true;
122         $_REQUEST['module'] = 'Contacts';
123         $_POST['do_not_call'] = 1;
124     }
125
126     /**
127 * Tears down the fixture, for example, close a network connection.
128 * This method is called after a test is executed.
129 *
130 * @return void
131 */
132     public function tearDown()
133     {
134         unset($_REQUEST['massupdate'], $_REQUEST['entire'], $_REQUEST['module'], $_POST['do_not_call']);
135
136         if (!empty($this->account2))
137         {
138             $this->account2->mark_deleted($this->account2->id);
139         }
140         if (!empty($this->account1))
141         {
142             $this->account1->mark_deleted($this->account1->id);
143         }
144         if (!empty($this->contact2))
145         {
146             $this->contact2->mark_deleted($this->contact2->id);
147         }
148         if (!empty($this->contact1))
149         {
150             $this->contact1->mark_deleted($this->contact1->id);
151         }
152
153
154
155         self::unregisterExtension('Contacts', 'bug51596test.php');
156         SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
157
158         unset($_SESSION['developerMode']);
159     }
160
161     /**
162 * Verifies that objects are found and updated by name of custom related
163 * object
164 *
165 * @return void
166 */
167     public function testSearchAndUpdate()
168     {
169         $contact = new Contact();
170
171         require_once 'include/MassUpdate.php';
172         $mass_update = new MassUpdate();
173         $mass_update->sugarbean = $contact;
174
175         // search for contacts related to Bug51596Test_Account1 (e.g. Contact1)
176         $current_query_by_page = array (
177             'searchFormTab' => 'basic_search',
178             $this->field_name . '_basic' => 'Bug51596Test_Account1',
179         );
180
181         // perform mass update
182         $current_query_by_page = base64_encode(serialize($current_query_by_page));
183         $mass_update->generateSearchWhere('Contacts', $current_query_by_page);
184         $mass_update->handleMassUpdate();
185
186         // ensure that "do_not_call" attribute of Contact1 has been changed
187         $contact->retrieve($this->contact1->id);
188         $this->assertEquals(1, $contact->do_not_call);
189
190         // ensure that "do_not_call" attribute of Contact2 has not been changed
191         $contact->retrieve($this->contact2->id);
192         $this->assertEquals(0, $contact->do_not_call);
193     }
194
195     /**
196 * Utility function. Registers vardef extension for specified module.
197 *
198 * @static
199 * @param string $module
200 * @param string $filename
201 * @param array $data
202 * @return void
203 */
204     protected static function registerExtension($module, $filename, array $data)
205     {
206         $directory = 'custom/Extension/modules/' . $module . '/Ext/Vardefs';
207
208         if (!file_exists($directory))
209         {
210             mkdir($directory, 0777, true);
211         }
212
213         $path = $directory . '/' . $filename;
214         $data = var_export($data, true);
215
216         $contents = <<<HERE
217 <?php
218 \$dictionary = array_merge_recursive(\$dictionary, {$data});
219 HERE;
220
221         file_put_contents($path, $contents);
222
223         self::rebuildExtensions($module);
224     }
225
226     /**
227 * Utility function. Unregisters vardef extension for specified module.
228 *
229 * @static
230 * @param string $module
231 * @param string $filename
232 * @return void
233 */
234     protected static function unregisterExtension($module, $filename)
235     {
236         $directory = 'custom/Extension/modules/' . $module . '/Ext/Vardefs';
237
238         if (!file_exists($directory))
239         {
240             mkdir($directory, 0777, true);
241         }
242
243         $path = $directory . '/' . $filename;
244         unlink($path);
245
246         self::rebuildExtensions($module);
247     }
248
249     /**
250 * Utility function. Rebuilds extensions for specified module.
251 *
252 * @static
253 * @param string $module
254 * @return void
255 */
256     protected static function rebuildExtensions($module)
257     {
258         $rc = new RepairAndClear();
259         $rc->repairAndClearAll(array('rebuildExtensions'), array($module), false, false);
260     }
261 }