2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2011 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 return $this->_makeRESTCall('login',
97 'user_name' => $this->_user->user_name,
98 'password' => $this->_user->user_hash,
101 'application_name' => 'SugarTestRunner',
102 'name_value_list' => array(),
107 public function testGetEntryListReturnsRSScorrectly()
109 $result = $this->_login();
110 $sessionId = $result['id'];
112 $rss = $this->_makeRESTCall('get_entry_list',
114 'session' => $sessionId,
115 'module' => 'Contacts',
116 'query' => "contacts.id = '{$this->_contact->id}'",
121 $this->assertContains('<description>1 record(s) found</description>',$rss);
122 $this->assertContains("<title>{$this->_contact->name}</title>",$rss);
123 $this->assertContains("<guid>{$this->_contact->id}</guid>",$rss);
126 public function testGetEntryReturnsRSScorrectly()
128 $result = $this->_login();
129 $sessionId = $result['id'];
131 $rss = $this->_makeRESTCall('get_entry',
133 'session' => $sessionId,
134 'module' => 'Contacts',
135 'id' => $this->_contact->id,
140 $this->assertContains('<description>1 record(s) found</description>',$rss);
141 $this->assertContains("<title>{$this->_contact->name}</title>",$rss);
142 $this->assertContains("<guid>{$this->_contact->id}</guid>",$rss);
145 public function testGetEntriesReturnsRSScorrectly()
147 $result = $this->_login();
148 $sessionId = $result['id'];
150 $rss = $this->_makeRESTCall('get_entries',
152 'session' => $sessionId,
153 'module' => 'Contacts',
154 'ids' => array($this->_contact->id),
159 $this->assertContains('<description>1 record(s) found</description>',$rss);
160 $this->assertContains("<title>{$this->_contact->name}</title>",$rss);
161 $this->assertContains("<guid>{$this->_contact->id}</guid>",$rss);