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