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