2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
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.
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
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
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.
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.
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 ********************************************************************************/
38 require_once('service/v3/SugarWebServiceUtilv3.php');
39 require_once('tests/service/APIv3Helper.php');
42 class RESTAPIRSSTest extends Sugar_PHPUnit_Framework_TestCase
44 public function setUp()
46 $GLOBALS['current_user'] = SugarTestUserUtilities::createAnonymousUser();
47 $this->_user = SugarTestUserUtilities::createAnonymousUser();
48 $this->_user->status = 'Active';
49 $this->_user->is_admin = 1;
51 $this->_contact = SugarTestContactUtilities::createContact();
54 public function tearDown()
56 SugarTestContactUtilities::removeAllCreatedContacts();
57 unset($GLOBALS['current_user']);
58 SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
61 protected function _makeRESTCall($method,$parameters,$response_type = 'JSON',$api = 'v3_1')
63 // specify the REST web service to interact with
64 $url = $GLOBALS['sugar_config']['site_url']."/service/$api/rest.php";
65 // Open a curl session for making the call
66 $curl = curl_init($url);
67 // set URL and other appropriate options
68 curl_setopt($curl, CURLOPT_URL, $url);
69 curl_setopt($curl, CURLOPT_POST, 1);
70 curl_setopt($curl, CURLOPT_HEADER, 0);
71 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
72 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
73 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 0);
74 curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0 );
75 // build the request URL
76 $json = json_encode($parameters);
77 $postArgs = "method=$method&input_type=JSON&response_type=$response_type&rest_data=$json";
78 curl_setopt($curl, CURLOPT_POSTFIELDS, $postArgs);
79 // Make the REST call, returning the result
80 $response = curl_exec($curl);
81 // Close the connection
84 if ( $response_type == 'JSON' ) {
85 return json_decode($response,true);
91 protected function _login()
93 $GLOBALS['db']->commit(); // Making sure we commit any changes before logging in
94 return $this->_makeRESTCall('login',
98 'user_name' => $this->_user->user_name,
99 'password' => $this->_user->user_hash,
102 'application_name' => 'SugarTestRunner',
103 'name_value_list' => array(),
108 public function testGetEntryListReturnsRSScorrectly()
110 $result = $this->_login();
111 $sessionId = $result['id'];
113 $rss = $this->_makeRESTCall('get_entry_list',
115 'session' => $sessionId,
116 'module' => 'Contacts',
117 'query' => "contacts.id = '{$this->_contact->id}'",
122 $this->assertContains('<description>1 record(s) found</description>',$rss);
123 $this->assertContains("<title>{$this->_contact->name}</title>",$rss);
124 $this->assertContains("<guid>{$this->_contact->id}</guid>",$rss);
127 public function testGetEntryReturnsRSScorrectly()
129 $result = $this->_login();
130 $sessionId = $result['id'];
132 $rss = $this->_makeRESTCall('get_entry',
134 'session' => $sessionId,
135 'module' => 'Contacts',
136 'id' => $this->_contact->id,
141 $this->assertContains('<description>1 record(s) found</description>',$rss);
142 $this->assertContains("<title>{$this->_contact->name}</title>",$rss);
143 $this->assertContains("<guid>{$this->_contact->id}</guid>",$rss);
146 public function testGetEntriesReturnsRSScorrectly()
148 $result = $this->_login();
149 $sessionId = $result['id'];
151 $rss = $this->_makeRESTCall('get_entries',
153 'session' => $sessionId,
154 'module' => 'Contacts',
155 'ids' => array($this->_contact->id),
160 $this->assertContains('<description>1 record(s) found</description>',$rss);
161 $this->assertContains("<title>{$this->_contact->name}</title>",$rss);
162 $this->assertContains("<guid>{$this->_contact->id}</guid>",$rss);