]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/service/Bug39234Test.php
Added unit tests.
[Github/sugarcrm.git] / tests / service / Bug39234Test.php
1 <?php 
2 require_once('include/nusoap/nusoap.php');
3
4
5 /**
6  * @group bug39234
7  */
8 class Bug39234Test extends Sugar_PHPUnit_Framework_TestCase
9 {
10         public $_user = null;
11         public $_soapClient = null;
12         public $_session = null;
13         public $_sessionId = '';
14     public $_contactId = '';
15     var $c1 = null;
16     var $c2 = null;
17         var $a1 = null;
18         
19     /**
20      * Create test user
21      *
22      */
23         public function setUp() 
24     {
25         $this->_soapClient = new nusoapclient($GLOBALS['sugar_config']['site_url'].'/soap.php',false,false,false,false,false,600,600);
26         $this->_setupTestUser();
27         
28         $beanList = array();
29         $beanFiles = array();
30         require('include/modules.php');
31         $GLOBALS['beanList'] = $beanList;
32         $GLOBALS['beanFiles'] = $beanFiles;
33         
34         $unid = uniqid();
35                 $time = date('Y-m-d H:i:s');
36
37                 $contact = new Contact();
38                 $contact->id = 'c_'.$unid;
39         $contact->first_name = 'testfirst';
40         $contact->last_name = 'testlast';
41         $contact->email1 = 'fred@rogers.com';
42         $contact->new_with_id = true;
43         $contact->disable_custom_fields = true;
44         $contact->save();
45                 $this->c1 = $contact;
46                 
47                 $account = new Account();
48                 $account->id = 'a_'.$unid;
49         $account->name = 'acctfirst';
50         $account->assigned_user_id = 'SugarUser';
51         $account->new_with_id = true;
52         $account->disable_custom_fields = true;
53         $account->save();
54         $this->a1 = $account;
55         
56        $this->c1->load_relationship('accounts');
57        $this->c1->accounts->add($this->a1->id);
58        
59        $contact2 = new Contact();
60                 $contact2->id = 'c2_'.$unid;
61        $contact2->first_name = 'testfirst';
62         $contact2->last_name = 'testlast';
63         $contact2->email1 = 'fred@rogers.com';
64         $contact2->new_with_id = true;
65         $contact2->disable_custom_fields = true;
66         $contact2->save();
67                 $this->c2 = $contact2;
68     }
69
70     /**
71      * Remove anything that was used during this test
72      *
73      */
74     public function tearDown() {
75         global $soap_version_test_accountId, $soap_version_test_opportunityId, $soap_version_test_contactId;
76         $this->_tearDownTestUser();
77         $this->_user = null;
78         $this->_sessionId = '';
79         $GLOBALS['db']->query("DELETE FROM contacts WHERE id= '{$this->c1->id}'");
80         $GLOBALS['db']->query("DELETE FROM contacts WHERE id= '{$this->c2->id}'");
81         $GLOBALS['db']->query("DELETE FROM accounts_contacts WHERE contact_id= '{$this->c1->id}'");
82         $GLOBALS['db']->query("DELETE FROM accounts_contacts WHERE contact_id= '{$this->c2->id}'");
83         $GLOBALS['db']->query("DELETE FROM accounts WHERE id= '{$this->a1->id}'");
84         
85         unset($this->c1);
86         unset($this->c2);
87         unset($this->a1);
88         unset($soap_version_test_accountId);
89         unset($soap_version_test_opportunityId);
90         unset($soap_version_test_contactId);
91         unset($GLOBALS['beanList']);
92         unset($GLOBALS['beanFiles']);
93     }   
94     
95     public function testSetEntries() {
96         $this->_login();
97                 $result = $this->_soapClient->call('set_entries',array('session'=>$this->_sessionId,'module_name' => 'Contacts','name_value_lists' => array(array(array('name'=>'last_name' , 'value'=>$this->c1->last_name), array('name'=>'email1' , 'value'=>$this->c1->email1), array('name'=>'first_name' , 'value'=>$this->c1->first_name), array('name'=>'account_name' , 'value'=>$this->a1->name)))));
98                 $this->assertTrue(isset($result['ids']) && $result['ids'][0] == $this->c1->id);
99     } // fn
100     
101      public function testSetEntries2() {
102         $this->_login();
103                 $result = $this->_soapClient->call('set_entries',array('session'=>$this->_sessionId,'module_name' => 'Contacts','name_value_lists' => array(array(array('name'=>'last_name' , 'value'=>$this->c2->last_name), array('name'=>'email1' , 'value'=>$this->c2->email1), array('name'=>'first_name' , 'value'=>$this->c2->first_name), array('name'=>'account_name' , 'value'=>'joe pizza')))));
104                 $this->assertTrue(isset($result['ids']) && $result['ids'][0] != $this->c1->id);
105     } // fn
106     
107         /**********************************
108      * HELPER PUBLIC FUNCTIONS
109      **********************************/
110     
111     /**
112      * Attempt to login to the soap server
113      *
114      * @return $set_entry_result - this should contain an id and error.  The id corresponds
115      * to the session_id.
116      */
117     public function _login(){
118                 global $current_user;   
119         $result = $this->_soapClient->call('login',
120             array('user_auth' => 
121                 array('user_name' => $current_user->user_name,
122                     'password' => $current_user->user_hash, 
123                     'version' => '.01'), 
124                 'application_name' => 'SoapTest')
125             );
126         $this->_sessionId = $result['id'];
127                 return $result;
128     }
129     
130  /**
131      * Create a test user
132      *
133      */
134         public function _setupTestUser() {
135         $this->_user = SugarTestUserUtilities::createAnonymousUser();
136         $this->_user->status = 'Active';
137         $this->_user->is_admin = 1;
138         $this->_user->save();
139         $GLOBALS['current_user'] = $this->_user;
140     }
141         
142     /**
143      * Remove user created for test
144      *
145      */
146         public function _tearDownTestUser() {
147        SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
148        unset($GLOBALS['current_user']);
149     }
150         
151 }
152 ?>