]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/service/RESTAPI3Test.php
Release 6.2.0
[Github/sugarcrm.git] / tests / service / RESTAPI3Test.php
1 <?php
2 /*********************************************************************************
3  * SugarCRM Community Edition is a customer relationship management program developed by
4  * SugarCRM, Inc. Copyright (C) 2004-2011 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 require_once('service/v3/SugarWebServiceUtilv3.php');
39 require_once('tests/service/APIv3Helper.php');
40
41
42 class RESTAPI3Test extends Sugar_PHPUnit_Framework_TestCase
43 {
44     protected $_user;
45
46     protected $_lastRawResponse;
47
48     private static $helperObject;
49
50     public function setUp()
51     {
52         //Reload langauge strings
53         $GLOBALS['app_strings'] = return_application_language($GLOBALS['current_language']);
54         $GLOBALS['app_list_strings'] = return_app_list_strings_language($GLOBALS['current_language']);
55         $GLOBALS['mod_strings'] = return_module_language($GLOBALS['current_language'], 'Accounts');
56         //Create an anonymous user for login purposes/
57         $this->_user = SugarTestUserUtilities::createAnonymousUser();
58         $this->_user->status = 'Active';
59         $this->_user->is_admin = 1;
60         $this->_user->save();
61         $GLOBALS['current_user'] = $this->_user;
62
63         self::$helperObject = new APIv3Helper();
64     }
65
66     public function tearDown()
67         {
68             if(isset($GLOBALS['listViewDefs'])) unset($GLOBALS['listViewDefs']);
69             if(isset($GLOBALS['viewdefs'])) unset($GLOBALS['viewdefs']);
70             unset($GLOBALS['app_list_strings']);
71             unset($GLOBALS['app_strings']);
72             unset($GLOBALS['mod_strings']);
73         }
74
75     protected function _makeRESTCall($method,$parameters)
76     {
77         // specify the REST web service to interact with
78         $url = $GLOBALS['sugar_config']['site_url'].'/service/v3/rest.php';
79         // Open a curl session for making the call
80         $curl = curl_init($url);
81         // set URL and other appropriate options
82         curl_setopt($curl, CURLOPT_URL, $url);
83         curl_setopt($curl, CURLOPT_POST, 1);
84         curl_setopt($curl, CURLOPT_HEADER, 0);
85         curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
86         curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
87         curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 0);
88         curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0 );
89         // build the request URL
90         $json = json_encode($parameters);
91         $postArgs = "method=$method&input_type=JSON&response_type=JSON&rest_data=$json";
92         curl_setopt($curl, CURLOPT_POSTFIELDS, $postArgs);
93         // Make the REST call, returning the result
94         $response = curl_exec($curl);
95         // Close the connection
96         curl_close($curl);
97
98         $this->_lastRawResponse = $response;
99
100         // Convert the result from JSON format to a PHP array
101         return json_decode($response,true);
102     }
103
104     protected function _returnLastRawResponse()
105     {
106         return "Error in web services call. Response was: {$this->_lastRawResponse}";
107     }
108
109     protected function _login()
110     {
111         return $this->_makeRESTCall('login',
112             array(
113                 'user_auth' =>
114                     array(
115                         'user_name' => $this->_user->user_name,
116                         'password' => $this->_user->user_hash,
117                         'version' => '.01',
118                         ),
119                 'application_name' => 'SugarTestRunner',
120                 'name_value_list' => array(),
121                 )
122             );
123     }
124
125     public function testSearchByModule()
126     {
127         $result = $this->_login();
128         $session = $result['id'];
129
130         $seedData = self::$helperObject->populateSeedDataForSearchTest($this->_user->id);
131
132         $searchModules = array('Accounts','Contacts','Opportunities');
133         $searchString = "UNIT TEST";
134         $offSet = 0;
135         $maxResults = 10;
136
137         $results = $this->_makeRESTCall('search_by_module',
138                         array(
139                             'session' => $session,
140                             'search'  => $searchString,
141                             'modules' => $searchModules,
142                             'offset'  => $offSet,
143                             'max'     => $maxResults,
144                             'user'    => $this->_user->id)
145                         );
146
147         $this->assertTrue( self::$helperObject->findBeanIdFromEntryList($results['entry_list'],$seedData[0]['id'],'Accounts') );
148         $this->assertFalse( self::$helperObject->findBeanIdFromEntryList($results['entry_list'],$seedData[1]['id'],'Accounts') );
149         $this->assertTrue( self::$helperObject->findBeanIdFromEntryList($results['entry_list'],$seedData[2]['id'],'Contacts') );
150         $this->assertTrue( self::$helperObject->findBeanIdFromEntryList($results['entry_list'],$seedData[3]['id'],'Opportunities') );
151         $this->assertFalse( self::$helperObject->findBeanIdFromEntryList($results['entry_list'],$seedData[4]['id'],'Opportunities') );
152         $GLOBALS['db']->query("DELETE FROM accounts WHERE name like 'UNIT TEST%' ");
153         $GLOBALS['db']->query("DELETE FROM opportunities WHERE name like 'UNIT TEST%' ");
154         $GLOBALS['db']->query("DELETE FROM contacts WHERE first_name like 'UNIT TEST%' ");
155     }
156
157     public function testSearchByModuleWithReturnFields()
158     {
159         $result = $this->_login();
160         $session = $result['id'];
161
162         $seedData = self::$helperObject->populateSeedDataForSearchTest($this->_user->id);
163
164         $returnFields = array('name','id','deleted');
165         $searchModules = array('Accounts','Contacts','Opportunities');
166         $searchString = "UNIT TEST";
167         $offSet = 0;
168         $maxResults = 10;
169
170         $results = $this->_makeRESTCall('search_by_module',
171                         array(
172                             'session' => $session,
173                             'search'  => $searchString,
174                             'modules' => $searchModules,
175                             'offset'  => $offSet,
176                             'max'     => $maxResults,
177                             'user'    => $this->_user->id,
178                             'selectFields' => $returnFields)
179                         );
180
181
182         $this->assertEquals($seedData[0]['fieldValue'], self::$helperObject->findFieldByNameFromEntryList($results['entry_list'],$seedData[0]['id'],'Accounts', $seedData[0]['fieldName']));
183         $this->assertFalse(self::$helperObject->findFieldByNameFromEntryList($results['entry_list'],$seedData[1]['id'],'Accounts', $seedData[1]['fieldName']));
184         $this->assertEquals($seedData[2]['fieldValue'], self::$helperObject->findFieldByNameFromEntryList($results['entry_list'],$seedData[2]['id'],'Contacts', $seedData[2]['fieldName']));
185         $this->assertEquals($seedData[3]['fieldValue'], self::$helperObject->findFieldByNameFromEntryList($results['entry_list'],$seedData[3]['id'],'Opportunities', $seedData[3]['fieldName']));
186         $this->assertFalse(self::$helperObject->findFieldByNameFromEntryList($results['entry_list'],$seedData[4]['id'],'Opportunities', $seedData[4]['fieldName']));
187
188         $GLOBALS['db']->query("DELETE FROM accounts WHERE name like 'UNIT TEST%' ");
189         $GLOBALS['db']->query("DELETE FROM opportunities WHERE name like 'UNIT TEST%' ");
190         $GLOBALS['db']->query("DELETE FROM contacts WHERE first_name like 'UNIT TEST%' ");
191     }
192
193     public function testGetServerInformation()
194     {
195         require('sugar_version.php');
196
197         $result = $this->_login();
198         $session = $result['id'];
199
200         $result = $this->_makeRESTCall('get_server_info',array());
201
202         $this->assertEquals($sugar_version, $result['version'],'Unable to get server information');
203         $this->assertEquals($sugar_flavor, $result['flavor'],'Unable to get server information');
204     }
205
206     public function testGetModuleList()
207     {
208         $result = $this->_login();
209         $session = $result['id'];
210
211         $account = new Account();
212         $account->id = uniqid();
213         $account->new_with_id = TRUE;
214         $account->name = "Test " . $account->id;
215         $account->save();
216
217         $whereClause = "accounts.name='{$account->name}'";
218         $module = 'Accounts';
219         $orderBy = 'name';
220         $offset = 0;
221         $returnFields = array('name');
222         $result = $this->_makeRESTCall('get_entry_list', array($session, $module, $whereClause, $orderBy,$offset, $returnFields));
223
224         $this->assertEquals($account->id, $result['entry_list'][0]['id'],'Unable to retrieve account list during search.');
225
226         $GLOBALS['db']->query("DELETE FROM accounts WHERE id = '{$account->id}'");
227
228     }
229
230     public function testLogin()
231     {
232         $result = $this->_login();
233         $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
234     }
235
236     public static function _multipleModuleLayoutProvider()
237     {
238         return array(
239                         array(
240                             'module' => array('Accounts','Contacts'),
241                             'type' => array('default'),
242                             'view' => array('list'),
243                             'expected_file' => array(
244                                 'Accounts' => array( 'default' => array('list' => 'modules/Accounts/metadata/listviewdefs.php')),
245                                 'Contacts' => array( 'default' => array('list' => 'modules/Contacts/metadata/listviewdefs.php')))
246                         ),
247                         array(
248                             'module' => array('Accounts','Contacts'),
249                             'type' => array('default'),
250                             'view' => array('list','detail'),
251                             'expected_file' => array(
252                                 'Accounts' => array(
253                                     'default' => array(
254                                                 'list' => 'modules/Accounts/metadata/listviewdefs.php',
255                                                 'detail' => 'modules/Accounts/metadata/detailviewdefs.php')),
256                                 'Contacts' => array(
257                                     'default' => array(
258                                                 'list' => 'modules/Contacts/metadata/listviewdefs.php',
259                                                 'detail' => 'modules/Contacts/metadata/detailviewdefs.php'))
260                         ))
261         );
262     }
263
264     /**
265      * @dataProvider _multipleModuleLayoutProvider
266      */
267     public function testGetMultipleModuleLayout($a_module, $a_type, $a_view, $a_expected_file)
268     {
269         $result = $this->_login();
270         $session = $result['id'];
271
272         $results = $this->_makeRESTCall('get_module_layout',
273                         array(
274                             'session' => $session,
275                             'module' => $a_module,
276                             'type' => $a_type,
277                             'view' => $a_view)
278                         );
279
280         foreach ($results as $module => $moduleResults )
281         {
282             foreach ($moduleResults as $type => $viewResults)
283             {
284                 foreach ($viewResults as $view => $result)
285                 {
286                     $expected_file = $a_expected_file[$module][$type][$view];
287                     if ( is_file('custom'  . DIRECTORY_SEPARATOR . $expected_file) )
288                         require('custom'  . DIRECTORY_SEPARATOR . $expected_file);
289                     else
290                         require($expected_file);
291
292                     if($view == 'list')
293                         $expectedResults = $listViewDefs[$module];
294                     else
295                         $expectedResults = $viewdefs[$module][ucfirst($view) .'View' ];
296
297                     $this->assertEquals(md5(serialize($expectedResults)), md5(serialize($result)), "Unable to retrieve module layout: module {$module}, type $type, view $view");
298                 }
299                 }
300         }
301    }
302
303     public static function _moduleLayoutProvider()
304     {
305         return array(
306                     array('module' => 'Accounts','type' => 'default', 'view' => 'list','expected_file' => 'modules/Accounts/metadata/listviewdefs.php' ),
307                     array('module' => 'Accounts','type' => 'default', 'view' => 'edit','expected_file' => 'modules/Accounts/metadata/editviewdefs.php' ),
308                     array('module' => 'Accounts','type' => 'default', 'view' => 'detail','expected_file' => 'modules/Accounts/metadata/detailviewdefs.php' ),
309         );
310     }
311
312     /**
313      * @dataProvider _moduleLayoutProvider
314      */
315     public function testGetModuleLayout($module, $type, $view, $expected_file)
316     {
317         $result = $this->_login();
318         $session = $result['id'];
319
320         $result = $this->_makeRESTCall('get_module_layout',
321                         array(
322                             'session' => $session,
323                             'module' => array($module),
324                             'type' => array($type),
325                             'view' => array($view))
326                         );
327
328         if ( is_file('custom'  . DIRECTORY_SEPARATOR . $expected_file) )
329                 require('custom'  . DIRECTORY_SEPARATOR . $expected_file);
330         else
331             require($expected_file);
332
333         if($view == 'list')
334             $expectedResults = $listViewDefs[$module];
335         else
336             $expectedResults = $viewdefs[$module][ucfirst($view) .'View' ];
337
338         $a_expectedResults = array();
339         $a_expectedResults[$module][$type][$view] = $expectedResults;
340
341         $this->assertEquals(md5(serialize($a_expectedResults)), md5(serialize($result)), "Unable to retrieve module layout: module {$module}, type $type, view $view");
342     }
343
344      /**
345      * @dataProvider _moduleLayoutProvider
346      */
347     public function testGetModuleLayoutMD5($module, $type, $view, $expected_file)
348     {
349         $result = $this->_login();
350         $session = $result['id'];
351
352         $fullResult = $this->_makeRESTCall('get_module_layout_md5',
353                         array(
354                             'session' => $session,
355                             'module' => array($module),
356                             'type' => array($type),
357                             'view' => array($view) )
358                         );
359         $result = $fullResult['md5'];
360         if ( is_file('custom'  . DIRECTORY_SEPARATOR . $expected_file) )
361                 require('custom'  . DIRECTORY_SEPARATOR . $expected_file);
362         else
363             require($expected_file);
364
365         if($view == 'list')
366             $expectedResults = $listViewDefs[$module];
367         else
368             $expectedResults = $viewdefs[$module][ucfirst($view) .'View' ];
369
370         $a_expectedResults = array();
371         $a_expectedResults[$module][$type][$view] = $expectedResults;
372
373         $this->assertEquals(md5(serialize($expectedResults)), $result[$module][$type][$view], "Unable to retrieve module layout md5: module {$module}, type $type, view $view");
374
375     }
376
377     public function testGetAvailableModules()
378     {
379         $this->markTestSkipped('Will be updated week of June 21, 2010');
380
381         $result = $this->_login();
382         $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
383         $session = $result['id'];
384
385         $fullResult = $this->_makeRESTCall('get_available_modules', array('session' => $session, 'filter' => 'all' ));
386         $this->assertTrue(in_array('ACLFields', $fullResult['modules']), "Unable to get all available modules");
387         $this->assertTrue(in_array('Schedulers', $fullResult['modules']), "Unable to get all available modules");
388         $this->assertTrue(in_array('Roles', $fullResult['modules']), "Unable to get all available modules");
389
390         $sh = new SugarWebServiceUtilv3();
391
392         $mobileResult = $this->_makeRESTCall('get_available_modules', array('session' => $session, 'filter' => 'mobile' ));
393         $mobileResultExpected = $sh->get_visible_mobile_modules($fullResult['modules']);
394         $mobileResultExpected = md5(serialize(array('modules' => $mobileResultExpected)));
395         $mobileResult = md5(serialize($mobileResult));
396         $this->assertEquals($mobileResultExpected, $mobileResult, "Unable to get all visible mobile modules");
397
398         $defaultResult = $this->_makeRESTCall('get_available_modules', array('session' => $session, 'filter' => 'default' ));
399         $defaultResult = md5(serialize($defaultResult['modules']));
400         $defaultResultExpected = $sh->get_visible_modules($fullResult['modules']);
401         $defaultResultExpected = md5(serialize($defaultResultExpected));
402         $this->assertEquals($defaultResultExpected, $defaultResult, "Unable to get all visible default modules");
403
404     }
405
406     public function testGetVardefsMD5()
407     {
408         $GLOBALS['reload_vardefs'] = TRUE;
409         $result = $this->_login();
410         $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
411         $session = $result['id'];
412
413         //Test a regular module
414         $fullResult = $this->_makeRESTCall('get_module_fields_md5', array('session' => $session, 'module' => 'Accounts' ));
415         $result = $fullResult['Accounts'];
416         $a = new Account();
417         $soapHelper = new SugarWebServiceUtilv3();
418         $actualVardef = $soapHelper->get_return_module_fields($a,'Accounts','');
419         $actualMD5 = md5(serialize($actualVardef));
420         $this->assertEquals($actualMD5, $result, "Unable to retrieve vardef md5.");
421
422         //Test a fake module
423         $result = $this->_makeRESTCall('get_module_fields_md5', array('session' => $session, 'module' => 'BadModule' ));
424         $this->assertTrue($result['name'] == 'Module Does Not Exist');
425         unset($GLOBALS['reload_vardefs']);
426     }
427
428     public function testAddNewAccountAndThenDeleteIt()
429     {
430         $result = $this->_login();
431         $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
432         $session = $result['id'];
433
434         $result = $this->_makeRESTCall('set_entry',
435             array(
436                 'session' => $session,
437                 'module' => 'Accounts',
438                 'name_value_list' => array(
439                     array('name' => 'name', 'value' => 'New Account'),
440                     array('name' => 'description', 'value' => 'This is an account created from a REST web services call'),
441                     ),
442                 )
443             );
444
445         $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
446
447         $accountId = $result['id'];
448
449         // verify record was created
450         $result = $this->_makeRESTCall('get_entry',
451             array(
452                 'session' => $session,
453                 'module' => 'Accounts',
454                 'id' => $accountId,
455                 )
456             );
457
458         $this->assertEquals($result['entry_list'][0]['id'],$accountId,$this->_returnLastRawResponse());
459
460         // delete the record
461         $result = $this->_makeRESTCall('set_entry',
462             array(
463                 'session' => $session,
464                 'module' => 'Accounts',
465                 'name_value_list' => array(
466                     array('name' => 'id', 'value' => $accountId),
467                     array('name' => 'deleted', 'value' => '1'),
468                     ),
469                 )
470             );
471
472         $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
473
474         // try to retrieve again to validate it is deleted
475         $result = $this->_makeRESTCall('get_entry',
476             array(
477                 'session' => $session,
478                 'module' => 'Accounts',
479                 'id' => $accountId,
480                 )
481             );
482
483         $GLOBALS['db']->query("DELETE FROM accounts WHERE id= '{$accountId}'");
484         
485         $this->assertTrue(!empty($result['entry_list'][0]['id']) && $result['entry_list'][0]['id'] != -1,$this->_returnLastRawResponse());
486         $this->assertEquals($result['entry_list'][0]['name_value_list'][0]['name'],'warning',$this->_returnLastRawResponse());
487         $this->assertEquals($result['entry_list'][0]['name_value_list'][0]['value'],"Access to this object is denied since it has been deleted or does not exist",$this->_returnLastRawResponse());
488         $this->assertEquals($result['entry_list'][0]['name_value_list'][1]['name'],'deleted',$this->_returnLastRawResponse());
489         $this->assertEquals($result['entry_list'][0]['name_value_list'][1]['value'],1,$this->_returnLastRawResponse());
490     }
491
492     public function testRelateAccountToTwoContacts()
493     {
494         $result = $this->_login();
495         $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
496         $session = $result['id'];
497
498         $result = $this->_makeRESTCall('set_entry',
499             array(
500                 'session' => $session,
501                 'module' => 'Accounts',
502                 'name_value_list' => array(
503                     array('name' => 'name', 'value' => 'New Account'),
504                     array('name' => 'description', 'value' => 'This is an account created from a REST web services call'),
505                     ),
506                 )
507             );
508
509         $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
510
511         $accountId = $result['id'];
512
513         $result = $this->_makeRESTCall('set_entry',
514             array(
515                 'session' => $session,
516                 'module' => 'Contacts',
517                 'name_value_list' => array(
518                     array('name' => 'last_name', 'value' => 'New Contact 1'),
519                     array('name' => 'description', 'value' => 'This is a contact created from a REST web services call'),
520                     ),
521                 )
522             );
523
524         $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
525
526         $contactId1 = $result['id'];
527
528         $result = $this->_makeRESTCall('set_entry',
529             array(
530                 'session' => $session,
531                 'module' => 'Contacts',
532                 'name_value_list' => array(
533                     array('name' => 'last_name', 'value' => 'New Contact 2'),
534                     array('name' => 'description', 'value' => 'This is a contact created from a REST web services call'),
535                     ),
536                 )
537             );
538
539         $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
540
541         $contactId2 = $result['id'];
542
543         // now relate them together
544         $result = $this->_makeRESTCall('set_relationship',
545             array(
546                 'session' => $session,
547                 'module' => 'Accounts',
548                 'module_id' => $accountId,
549                 'link_field_name' => 'contacts',
550                 'related_ids' => array($contactId1,$contactId2),
551                 )
552             );
553
554         $this->assertEquals($result['created'],1,$this->_returnLastRawResponse());
555
556         // check the relationship
557         $result = $this->_makeRESTCall('get_relationships',
558             array(
559                 'session' => $session,
560                 'module' => 'Accounts',
561                 'module_id' => $accountId,
562                 'link_field_name' => 'contacts',
563                 'related_module_query' => '',
564                 'related_fields' => array('last_name','description'),
565                 'related_module_link_name_to_fields_array' => array(),
566                 'deleted' => false,
567                 )
568             );
569
570         $returnedValues = array();
571         $returnedValues[] = $result['entry_list'][0]['name_value_list']['last_name']['value'];
572         $returnedValues[] = $result['entry_list'][1]['name_value_list']['last_name']['value'];
573
574         $GLOBALS['db']->query("DELETE FROM accounts WHERE id= '{$accountId}'");
575         $GLOBALS['db']->query("DELETE FROM contacts WHERE id= '{$contactId1}'");
576         $GLOBALS['db']->query("DELETE FROM contacts WHERE id= '{$contactId2}'");
577         $GLOBALS['db']->query("DELETE FROM accounts_contacts WHERE account_id= '{$accountId}'");
578
579         $this->assertContains('New Contact 1',$returnedValues,$this->_returnLastRawResponse());
580         $this->assertContains('New Contact 2',$returnedValues,$this->_returnLastRawResponse());
581     }
582
583     /**
584      * @ticket 36658
585      */
586     public function testOrderByClauseOfGetRelationship()
587     {
588         $result = $this->_login();
589         $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
590         $session = $result['id'];
591
592         $result = $this->_makeRESTCall('set_entry',
593             array(
594                 'session' => $session,
595                 'module' => 'Accounts',
596                 'name_value_list' => array(
597                     array('name' => 'name', 'value' => 'New Account'),
598                     array('name' => 'description', 'value' => 'This is an account created from a REST web services call'),
599                     ),
600                 )
601             );
602
603         $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
604
605         $accountId = $result['id'];
606
607         $result = $this->_makeRESTCall('set_entry',
608             array(
609                 'session' => $session,
610                 'module' => 'Contacts',
611                 'name_value_list' => array(
612                     array('name' => 'last_name', 'value' => 'New Contact 1'),
613                     array('name' => 'description', 'value' => 'This is a contact created from a REST web services call'),
614                     ),
615                 )
616             );
617
618         $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
619
620         $contactId1 = $result['id'];
621
622         $result = $this->_makeRESTCall('set_entry',
623             array(
624                 'session' => $session,
625                 'module' => 'Contacts',
626                 'name_value_list' => array(
627                     array('name' => 'last_name', 'value' => 'New Contact 2'),
628                     array('name' => 'description', 'value' => 'This is a contact created from a REST web services call'),
629                     ),
630                 )
631             );
632
633         $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
634
635         $contactId2 = $result['id'];
636
637         // now relate them together
638         $result = $this->_makeRESTCall('set_relationship',
639             array(
640                 'session' => $session,
641                 'module' => 'Accounts',
642                 'module_id' => $accountId,
643                 'link_field_name' => 'contacts',
644                 'related_ids' => array($contactId1,$contactId2),
645                 )
646             );
647
648         $this->assertEquals($result['created'],1,$this->_returnLastRawResponse());
649
650         // check the relationship
651         $result = $this->_makeRESTCall('get_relationships',
652             array(
653                 'session' => $session,
654                 'module' => 'Accounts',
655                 'module_id' => $accountId,
656                 'link_field_name' => 'contacts',
657                 'related_module_query' => '',
658                 'related_fields' => array('last_name','description'),
659                 'related_module_link_name_to_fields_array' => array(),
660                 'deleted' => false,
661                 'order_by' => 'last_name',
662                 )
663             );
664
665         $GLOBALS['db']->query("DELETE FROM accounts WHERE id= '{$accountId}'");
666         $GLOBALS['db']->query("DELETE FROM contacts WHERE id= '{$contactId1}'");
667         $GLOBALS['db']->query("DELETE FROM contacts WHERE id= '{$contactId2}'");
668         $GLOBALS['db']->query("DELETE FROM accounts_contacts WHERE account_id= '{$accountId}'");
669         
670         $this->assertEquals($result['entry_list'][0]['name_value_list']['last_name']['value'],'New Contact 1',$this->_returnLastRawResponse());
671         $this->assertEquals($result['entry_list'][1]['name_value_list']['last_name']['value'],'New Contact 2',$this->_returnLastRawResponse());
672     }
673
674     public static function _subpanelLayoutProvider()
675     {
676         return array(
677             array(
678                 'module' => 'Contacts',
679                 'type' => 'default',
680                 'view' => 'subpanel',
681             ),
682             array(
683                 'module' => 'Leads',
684                 'type' => 'wireless',
685                 'view' => 'subpanel',
686             ),
687         );
688     }
689
690     /**
691      * @dataProvider _subpanelLayoutProvider
692      */
693     public function testGetSubpanelLayout($module, $type, $view)
694     {
695         $result = $this->_login();
696         $session = $result['id'];
697
698         $results = $this->_makeRESTCall('get_module_layout',
699             array(
700                 'session' => $session,
701                 'module' => array($module),
702                 'type' => array($type),
703                 'view' => array($view))
704         );
705
706         $this->assertTrue(isset($results[$module][$type][$view]), "Unable to get subpanel defs");
707     }
708      public function testGetUpcomingActivities()
709      {
710          $result = $this->_login();
711          $this->assertTrue(!empty($result['id']) && $result['id'] != -1,$this->_returnLastRawResponse());
712          $session = $result['id'];
713          $expected = $this->_createUpcomingActivities(); //Seed the data.
714          $results = $this->_makeRESTCall('get_upcoming_activities',
715                              array(
716                              'session' => $session,
717                              )
718          );
719
720          $this->assertEquals($expected[0] ,$results[0]['id'] , "Unable to get upcoming activities");
721          $this->assertEquals($expected[1] ,$results[1]['id'] , "Unable to get upcoming activities");
722
723          $this->_removeUpcomingActivities();
724      }
725
726      private function _removeUpcomingActivities()
727      {
728          $GLOBALS['db']->query("DELETE FROM calls where name = 'UNIT TEST'");
729          $GLOBALS['db']->query("DELETE FROM tasks where name = 'UNIT TEST'");
730      }
731
732      private function _createUpcomingActivities()
733      {
734          $GLOBALS['current_user']->setPreference('datef','Y-m-d') ;
735          $GLOBALS['current_user']->setPreference('timef','H:i') ;
736
737          $date1 = $GLOBALS['timedate']->to_display_date_time(gmdate("Y-m-d H:i:s", (gmmktime() + (3600 * 24 * 2) ) ),true,true, $GLOBALS['current_user']) ; //Two days from today
738          $date2 = $GLOBALS['timedate']->to_display_date_time(gmdate("Y-m-d H:i:s", (gmmktime() + (3600 * 24 * 4) ) ),true,true, $GLOBALS['current_user']) ; //Two days from today
739
740          $callID = uniqid();
741          $c = new Call();
742          $c->id = $callID;
743          $c->new_with_id = TRUE;
744          $c->status = 'Not Planned';
745          $c->date_start = $date1;
746          $c->name = "UNIT TEST";
747          $c->assigned_user_id = $this->_user->id;
748          $c->save(FALSE);
749
750          $callID = uniqid();
751          $c = new Call();
752          $c->id = $callID;
753          $c->new_with_id = TRUE;
754          $c->status = 'Planned';
755          $c->date_start = $date1;
756          $c->name = "UNIT TEST";
757          $c->assigned_user_id = $this->_user->id;
758          $c->save(FALSE);
759
760          $taskID = uniqid();
761          $t = new Task();
762          $t->id = $taskID;
763          $t->new_with_id = TRUE;
764          $t->status = 'Not Started';
765          $t->date_due = $date2;
766          $t->name = "UNIT TEST";
767          $t->assigned_user_id = $this->_user->id;
768          $t->save(FALSE);
769
770          return array($callID, $taskID);
771      }
772 }