]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/service/SOAPAPI1Test.php
Added unit tests.
[Github/sugarcrm.git] / tests / service / SOAPAPI1Test.php
1 <?php
2 require_once('include/nusoap/nusoap.php');
3 /**
4  * This class is meant to test everything SOAP
5  *
6  */
7 class SOAPAPI1Test extends Sugar_PHPUnit_Framework_TestCase
8 {
9         public $_user = null;
10         public $_contact = null;
11         public $_meeting = null;
12         public $_soapClient = null;
13         public $_session = null;
14         public $_userUtils = null;
15         public $_sessionId = '';
16
17     /**
18      * Create test user
19      *
20      */
21         public function setUp() 
22     {
23         $this->_soapClient = new nusoapclient($GLOBALS['sugar_config']['site_url'].'/soap.php',false,false,false,false,false,60,60);
24         $this->_setupTestUser();
25         $this->_setupTestContact();
26         $this->_meeting = SugarTestMeetingUtilities::createMeeting();
27     }
28
29     /**
30      * Remove anything that was used during this test
31      *
32      */
33     public function tearDown() 
34     {
35         SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
36         $this->_user = null;
37         SugarTestContactUtilities::removeAllCreatedContacts();
38         SugarTestContactUtilities::removeCreatedContactsUsersRelationships();
39         $this->_contact = null;
40         SugarTestMeetingUtilities::removeAllCreatedMeetings();
41          SugarTestMeetingUtilities::removeMeetingContacts();
42         $this->_meeting = null;
43     }
44
45         /**
46          * Ensure we can create a session on the server.
47          *
48          */
49     public function testCanLogin()
50     {
51                 $result = $this->_login();
52         $this->assertTrue(!empty($result['id']) && $result['id'] != -1, 
53             'SOAP Session not created. Error ('.$result['error']['number'].'): '.$result['error']['name'].': '.$result['error']['description'].'. HTTP Response: '.$this->_soapClient->response);
54     }
55     
56     public function testSearchContactByEmail()
57     {
58         $result = $this->_soapClient->call('contact_by_email', array('user_name' => $this->_user->user_name, 'password' => $this->_user->user_hash, 'email_address' => $this->_contact->email1));
59         $this->assertTrue(!empty($result) && count($result) > 0, 'Incorrect number of results returned. HTTP Response: '.$this->_soapClient->response); 
60         $this->assertEquals($result[0]['name1'], $this->_contact->first_name, 'Incorrect result found'); 
61     }
62     
63         public function testSearchByModule()
64     {
65                 $modules = array('Contacts');
66         $result = $this->_soapClient->call('search_by_module', array('user_name' => $this->_user->user_name, 'password' => $this->_user->user_hash, 'search_string' => $this->_contact->email1, 'modules' => $modules, 'offset' => 0, 'max_results' => 10));
67         $this->assertTrue(!empty($result) && count($result['entry_list']) > 0, 'Incorrect number of results returned. HTTP Response: '.$this->_soapClient->response); 
68         $this->assertEquals($result['entry_list'][0]['name_value_list'][1]['name'], 'first_name' && $result['entry_list'][0]['name_value_list'][1]['value'] == $this->_contact->first_name, 'Incorrect result returned'); 
69     }
70     
71         public function testSearchBy()
72     {
73         $this->markTestSkipped('SOAP call "search" is deprecated');
74         
75                 $result = $this->_soapClient->call('search', array('user_name' => $this->_user->user_name, 'password' => $this->_user->user_hash, 'name' => $this->_contact->first_name));
76         $this->assertTrue(!empty($result) && count($result) > 0, "Incorrect number of results returned - Returned $result results. HTTP Response: ".$this->_soapClient->response); 
77         $this->assertEquals($result[0]['name1'], $this->_contact->first_name, "Contact First name does not match data returnd from SOAP_test"); 
78     }
79     
80         public function testGetModifiedEntries()
81     {
82                 $this->_login();
83                 $ids = array($this->_contact->id);
84         $result = $this->_soapClient->call('get_modified_entries', array('session' => $this->_sessionId, 'module_name' => 'Contacts', 'ids' => $ids, 'select_fields' => array()));
85         $decoded = base64_decode($result['result']);
86     }
87     
88         public function testGetAttendeeList()
89     {
90         $this->_login();
91         $this->_meeting->load_relationship('contacts');
92         $this->_meeting->contacts->add($this->_contact->id);
93                 $result = $this->_soapClient->call('get_attendee_list', array('session' => $this->_sessionId, 'module_name' => 'Meetings', 'id' => $this->_meeting->id));
94         $decoded = base64_decode($result['result']);
95         $decoded = simplexml_load_string($decoded);
96         $this->assertTrue(!empty($result['result']), 'Results not returned. HTTP Response: '.$this->_soapClient->response); 
97                 $this->assertEquals(urldecode($decoded->attendee->first_name), $this->_contact->first_name, 'Incorrect Result returned expected: '.$this->_contact->first_name.' Found: '.urldecode($decoded->attendee->first_name)); 
98         }
99     
100     public function testSyncGetModifiedRelationships()
101     {
102         $this->_login();
103         $ids = array($this->_contact->id);
104         $yesterday = date('Y-m-d', strtotime('last year')); 
105         $tomorrow = date('Y-m-d', mktime(0, 0, 0, date("m") , date("d") + 1, date("Y"))); 
106         $result = $this->_soapClient->call('sync_get_modified_relationships', array('session' => $this->_sessionId, 'module_name' => 'Users', 'related_module' => 'Contacts', 'from_date' => $yesterday, 'to_date' => $tomorrow, 'offset' => 0, 'max_results' => 10, 'deleted' => 0, 'module_id' => $this->_user->id, 'select_fields'=> array(), 'ids' => $ids, 'relationship_name' => 'contacts_users', 'deletion_date' => $yesterday, 'php_serialize' => 0));
107         $this->assertTrue(!empty($result['entry_list']), 'Results not returned. HTTP Response: '.$this->_soapClient->response); 
108         $decoded = base64_decode($result['entry_list']);
109         $decoded = simplexml_load_string($decoded);
110         if (isset($decoded->item[0]) ) {
111             $this->assertEquals(urlencode($decoded->item->name_value_list->name_value[1]->name), 'contact_id', "testSyncGetModifiedRelationships - could not retrieve contact_id column name");
112             $this->assertEquals(urlencode($decoded->item->name_value_list->name_value[1]->value), $this->_contact->id, "vlue of contact id is not same as returned via SOAP");
113         }
114     }
115     
116     /**********************************
117      * HELPER PUBLIC FUNCTIONS
118      **********************************/
119     
120     /**
121      * Attempt to login to the soap server
122      *
123      * @return $set_entry_result - this should contain an id and error.  The id corresponds
124      * to the session_id.
125      */
126     private function _login(){
127         $result = $this->_soapClient->call('login',
128             array('user_auth' => 
129                 array('user_name' => $this->_user->user_name,
130                     'password' => $this->_user->user_hash, 
131                     'version' => '.01'), 
132                 'application_name' => 'SoapTest')
133             );
134         $this->_sessionId = $result['id'];
135                 return $result;
136     }
137     
138     /**
139      * Create a test user
140      *
141      */
142         private function _setupTestUser() {
143         $this->_user = SugarTestUserUtilities::createAnonymousUser();
144         $this->_user->status = 'Active';
145          $this->_user->is_admin = 1;
146         $this->_user->save();
147         $GLOBALS['current_user'] = $this->_user;
148     }
149     
150         private function _setupTestContact() {
151         $this->_contact = SugarTestContactUtilities::createContact();
152         $this->_contact->contacts_users_id = $this->_user->id;
153         $this->_contact->save();
154     }
155     
156 }
157 ?>