]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/service/RESTAPIRSSTest.php
Release 6.2.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         return $this->_makeRESTCall('login',
94             array(
95                 'user_auth' =>
96                     array(
97                         'user_name' => $this->_user->user_name,
98                         'password' => $this->_user->user_hash,
99                         'version' => '.01',
100                         ),
101                 'application_name' => 'SugarTestRunner',
102                 'name_value_list' => array(),
103                 )
104             );
105     }
106
107     public function testGetEntryListReturnsRSScorrectly()
108     {
109         $result = $this->_login();
110         $sessionId = $result['id'];
111
112         $rss = $this->_makeRESTCall('get_entry_list',
113                         array(
114                             'session' => $sessionId,
115                             'module' => 'Contacts',
116                             'query' => "contacts.id = '{$this->_contact->id}'",
117                             ),
118                         'RSS'
119                         );
120
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);
124     }
125
126     public function testGetEntryReturnsRSScorrectly()
127     {
128         $result = $this->_login();
129         $sessionId = $result['id'];
130
131         $rss = $this->_makeRESTCall('get_entry',
132                         array(
133                             'session' => $sessionId,
134                             'module' => 'Contacts',
135                             'id' => $this->_contact->id,
136                             ),
137                         'RSS'
138                         );
139
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);
143     }
144
145     public function testGetEntriesReturnsRSScorrectly()
146     {
147         $result = $this->_login();
148         $sessionId = $result['id'];
149
150         $rss = $this->_makeRESTCall('get_entries',
151                         array(
152                             'session' => $sessionId,
153                             'module' => 'Contacts',
154                             'ids' => array($this->_contact->id),
155                             ),
156                         'RSS'
157                         );
158
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);
162     }
163 }