]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/service/RESTAPIRSSTest.php
Release 6.4.0
[Github/sugarcrm.git] / tests / service / RESTAPIRSSTest.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 RESTAPIRSSTest extends Sugar_PHPUnit_Framework_TestCase
43 {
44     public function setUp()
45     {
46         $GLOBALS['current_user'] = SugarTestUserUtilities::createAnonymousUser();
47         $this->_user = SugarTestUserUtilities::createAnonymousUser();
48         $this->_user->status = 'Active';
49         $this->_user->is_admin = 1;
50         $this->_user->save();
51         $this->_contact = SugarTestContactUtilities::createContact();
52     }
53
54     public function tearDown()
55     {
56         SugarTestContactUtilities::removeAllCreatedContacts();
57         unset($GLOBALS['current_user']);
58         SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
59     }
60
61     protected function _makeRESTCall($method,$parameters,$response_type = 'JSON',$api = 'v3_1')
62     {
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
82         curl_close($curl);
83
84         if ( $response_type == 'JSON' ) {
85             return json_decode($response,true);
86         }
87
88         return $response;
89     }
90
91     protected function _login()
92     {
93         $GLOBALS['db']->commit(); // Making sure we commit any changes before logging in
94         return $this->_makeRESTCall('login',
95             array(
96                 'user_auth' =>
97                     array(
98                         'user_name' => $this->_user->user_name,
99                         'password' => $this->_user->user_hash,
100                         'version' => '.01',
101                         ),
102                 'application_name' => 'SugarTestRunner',
103                 'name_value_list' => array(),
104                 )
105             );
106     }
107
108     public function testGetEntryListReturnsRSScorrectly()
109     {
110         $result = $this->_login();
111         $sessionId = $result['id'];
112
113         $rss = $this->_makeRESTCall('get_entry_list',
114                         array(
115                             'session' => $sessionId,
116                             'module' => 'Contacts',
117                             'query' => "contacts.id = '{$this->_contact->id}'",
118                             ),
119                         'RSS'
120                         );
121
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);
125     }
126
127     public function testGetEntryReturnsRSScorrectly()
128     {
129         $result = $this->_login();
130         $sessionId = $result['id'];
131
132         $rss = $this->_makeRESTCall('get_entry',
133                         array(
134                             'session' => $sessionId,
135                             'module' => 'Contacts',
136                             'id' => $this->_contact->id,
137                             ),
138                         'RSS'
139                         );
140
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);
144     }
145
146     public function testGetEntriesReturnsRSScorrectly()
147     {
148         $result = $this->_login();
149         $sessionId = $result['id'];
150
151         $rss = $this->_makeRESTCall('get_entries',
152                         array(
153                             'session' => $sessionId,
154                             'module' => 'Contacts',
155                             'ids' => array($this->_contact->id),
156                             ),
157                         'RSS'
158                         );
159
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);
163     }
164 }