Completed
Push — CI ( ee6bd7...0f01dd )
by Adam
22:32
created

ContactTest::testprocess_sync_to_outlook()   B

Complexity

Conditions 3
Paths 9

Size

Total Lines 26
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 26
rs 8.8571
c 1
b 0
f 1
cc 3
eloc 12
nc 9
nop 0
1
<?php
2
3
class ContactTest extends PHPUnit_Framework_TestCase {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
5
	public function testContact() {
6
7
		//execute the contructor and check for the Object type and  attributes
8
		$contact = new Contact();
9
		$this->assertInstanceOf('Contact',$contact);
10
		$this->assertInstanceOf('Person',$contact);
11
		$this->assertInstanceOf('SugarBean',$contact);
12
			
13
		$this->assertAttributeEquals('Contacts', 'module_dir', $contact);
14
		$this->assertAttributeEquals('Contact', 'object_name', $contact);
15
		$this->assertAttributeEquals('contacts', 'table_name', $contact);
16
		$this->assertAttributeEquals('accounts_contacts', 'rel_account_table', $contact);
17
		$this->assertAttributeEquals('opportunities_contacts', 'rel_opportunity_table', $contact);
18
		$this->assertAttributeEquals(true, 'importable', $contact);
19
		$this->assertAttributeEquals(true, 'new_schema', $contact);
20
		
21
	}
22
23
	public function testadd_list_count_joins()
24
	{
25
		error_reporting(E_ERROR | E_PARSE);
26
		
27
		$contact = new Contact();
28
		
29
		//test with empty strings
30
		$query = "";
31
		$contact->add_list_count_joins($query, '');
32
		$this->assertEquals("",$query);
33
		
34
		
35
		//test with valid string
36
		$query = "";
37
		$expected = "\n	            LEFT JOIN accounts_contacts\n	            ON contacts.id=accounts_contacts.contact_id\n	            LEFT JOIN accounts\n	            ON accounts_contacts.account_id=accounts.id\n			";
38
		$contact->add_list_count_joins($query, 'accounts.name');
39
		$this->assertSame($expected,$query);
40
		
41
		
42
		//test with valid string
43
		$query = "";
44
		$expected = "\n	            LEFT JOIN accounts_contacts\n	            ON contacts.id=accounts_contacts.contact_id\n	            LEFT JOIN accounts\n	            ON accounts_contacts.account_id=accounts.id\n			";
0 ignored issues
show
Unused Code introduced by
$expected is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
45
		$contact->add_list_count_joins($query, 'contacts.name');
46
		$this->assertSame("",$query);
47
		
48
		
49
	}
50
51
	public function testlistviewACLHelper()
52
	{
53
		$contact = new Contact();
54
		
55
		$expected = array( "MAIN"=>"a", "ACCOUNT"=>"a");
56
		$actual = $contact->listviewACLHelper();
57
		$this->assertSame($expected,$actual);
58
				
59
	}
60
61
	public function testcreate_new_list_query()
62
	{
63
		$contact = new Contact();
64
		
65
		//test without request action parameter
66
		$expected =" SELECT  contacts.* , '                                                                                                                                                                                                                                                              ' opportunity_role_fields , '                                    '  opportunity_id , '                                                                                                                                                                                                                                                              ' c_accept_status_fields , '                                    '  call_id , '                                                                                                                                                                                                                                                              ' e_invite_status_fields , '                                    '  fp_events_contactsfp_events_ida , '                                                                                                                                                                                                                                                              ' e_accept_status_fields , LTRIM(RTRIM(CONCAT(IFNULL(contacts.first_name,''),' ',IFNULL(contacts.last_name,'')))) as name , jt4.user_name modified_by_name , jt4.created_by modified_by_name_owner  , 'Users' modified_by_name_mod , jt5.user_name created_by_name , jt5.created_by created_by_name_owner  , 'Users' created_by_name_mod , jt6.user_name assigned_user_name , jt6.created_by assigned_user_name_owner  , 'Users' assigned_user_name_mod, LTRIM(RTRIM(CONCAT(IFNULL(contacts.first_name,''),' ',IFNULL(contacts.last_name,'')))) as full_name, '                                                                                                                                                                                                                                                              ' account_name , '                                    '  account_id  , jt8.last_name report_to_name , jt8.assigned_user_id report_to_name_owner  , 'Contacts' report_to_name_mod , jt9.name campaign_name , jt9.assigned_user_id campaign_name_owner  , 'Campaigns' campaign_name_mod, '                                                                                                                                                                                                                                                              ' m_accept_status_fields , '                                    '  meeting_id  FROM contacts   LEFT JOIN  users jt4 ON contacts.modified_user_id=jt4.id AND jt4.deleted=0\n\n AND jt4.deleted=0  LEFT JOIN  users jt5 ON contacts.created_by=jt5.id AND jt5.deleted=0\n\n AND jt5.deleted=0  LEFT JOIN  users jt6 ON contacts.assigned_user_id=jt6.id AND jt6.deleted=0\n\n AND jt6.deleted=0  LEFT JOIN  contacts jt8 ON contacts.reports_to_id=jt8.id AND jt8.deleted=0\n\n AND jt8.deleted=0  LEFT JOIN  campaigns jt9 ON contacts.campaign_id=jt9.id AND jt9.deleted=0\n\n AND jt9.deleted=0 where (account.name is null) AND contacts.deleted=0";
67
		$actual = $contact->create_new_list_query("account.name","account.name is null");
68
		$this->assertSame($expected,$actual);
69
70
		//test with request action parameter = ContactAddressPopup
71
		$_REQUEST['action'] = 'ContactAddressPopup';
72
		$expected = "SELECT LTRIM(RTRIM(CONCAT(IFNULL(contacts.first_name,''),'',IFNULL(contacts.last_name,'')))) name, \n				contacts.*,\n                accounts.name as account_name,\n                accounts.id as account_id,\n                accounts.assigned_user_id account_id_owner,\n                users.user_name as assigned_user_name \n                FROM contacts LEFT JOIN users\n	                    ON contacts.assigned_user_id=users.id\n	                    LEFT JOIN accounts_contacts\n	                    ON contacts.id=accounts_contacts.contact_id  and accounts_contacts.deleted = 0\n	                    LEFT JOIN accounts\n	                    ON accounts_contacts.account_id=accounts.id AND accounts.deleted=0 LEFT JOIN email_addr_bean_rel eabl  ON eabl.bean_id = contacts.id AND eabl.bean_module = 'Contacts' and eabl.primary_address = 1 and eabl.deleted=0 LEFT JOIN email_addresses ea ON (ea.id = eabl.email_address_id) where (account.name is null) AND  contacts.deleted=0 ";		
73
		$actual = $contact->create_new_list_query("account.name","account.name is null");
74
		$this->assertSame($expected,$actual);
75
		
76
	}
77
78
79
	public function testaddress_popup_create_new_list_query()
80
	{
81
82
		$contact = new Contact();
83
		
84
		//test with empty string params
85
		$expected = "SELECT LTRIM(RTRIM(CONCAT(IFNULL(contacts.first_name,''),'',IFNULL(contacts.last_name,'')))) name, \n				contacts.*,\n                accounts.name as account_name,\n                accounts.id as account_id,\n                accounts.assigned_user_id account_id_owner,\n                users.user_name as assigned_user_name \n                FROM contacts LEFT JOIN users\n	                    ON contacts.assigned_user_id=users.id\n	                    LEFT JOIN accounts_contacts\n	                    ON contacts.id=accounts_contacts.contact_id  and accounts_contacts.deleted = 0\n	                    LEFT JOIN accounts\n	                    ON accounts_contacts.account_id=accounts.id AND accounts.deleted=0 LEFT JOIN email_addr_bean_rel eabl  ON eabl.bean_id = contacts.id AND eabl.bean_module = 'Contacts' and eabl.primary_address = 1 and eabl.deleted=0 LEFT JOIN email_addresses ea ON (ea.id = eabl.email_address_id) where  contacts.deleted=0 ";
86
		$actual = $contact->address_popup_create_new_list_query('','');		
87
		$this->assertSame($expected,$actual);
88
		
89
		
90
		//test with valid string params
91
		$expected = "SELECT LTRIM(RTRIM(CONCAT(IFNULL(contacts.first_name,''),'',IFNULL(contacts.last_name,'')))) name, \n				contacts.*,\n                accounts.name as account_name,\n                accounts.id as account_id,\n                accounts.assigned_user_id account_id_owner,\n                users.user_name as assigned_user_name \n                FROM contacts LEFT JOIN users\n	                    ON contacts.assigned_user_id=users.id\n	                    LEFT JOIN accounts_contacts\n	                    ON contacts.id=accounts_contacts.contact_id  and accounts_contacts.deleted = 0\n	                    LEFT JOIN accounts\n	                    ON accounts_contacts.account_id=accounts.id AND accounts.deleted=0 LEFT JOIN email_addr_bean_rel eabl  ON eabl.bean_id = contacts.id AND eabl.bean_module = 'Contacts' and eabl.primary_address = 1 and eabl.deleted=0 LEFT JOIN email_addresses ea ON (ea.id = eabl.email_address_id) where (contacts.name=\"\") AND  contacts.deleted=0 ";
92
		$actual = $contact->address_popup_create_new_list_query('contacts.id','contacts.name=""');
93
		$this->assertSame($expected,$actual);
94
		
95
	}
96
97
	public function testcreate_export_query()
98
	{
99
		$contact = new Contact();
100
		
101
		//test with empty string params
102
		$expected = "SELECT\n                                contacts.*,\n                                email_addresses.email_address email_address,\n                                '' email_addresses_non_primary, accounts.name as account_name,\n                                users.user_name as assigned_user_name  FROM contacts LEFT JOIN users\n	                                ON contacts.assigned_user_id=users.id LEFT JOIN accounts_contacts\n	                                ON ( contacts.id=accounts_contacts.contact_id and (accounts_contacts.deleted is null or accounts_contacts.deleted = 0))\n	                                LEFT JOIN accounts\n	                                ON accounts_contacts.account_id=accounts.id  LEFT JOIN  email_addr_bean_rel on contacts.id = email_addr_bean_rel.bean_id and email_addr_bean_rel.bean_module='Contacts' and email_addr_bean_rel.deleted=0 and email_addr_bean_rel.primary_address=1  LEFT JOIN email_addresses on email_addresses.id = email_addr_bean_rel.email_address_id where ( accounts.deleted IS NULL OR accounts.deleted=0 )\n                      AND contacts.deleted=0 ";
103
		$actual = $contact->create_export_query('','');
104
		$this->assertSame($expected,$actual);
105
		
106
		
107
		//test with valid string params
108
		$expected = "SELECT\n                                contacts.*,\n                                email_addresses.email_address email_address,\n                                '' email_addresses_non_primary, accounts.name as account_name,\n                                users.user_name as assigned_user_name  FROM contacts LEFT JOIN users\n	                                ON contacts.assigned_user_id=users.id LEFT JOIN accounts_contacts\n	                                ON ( contacts.id=accounts_contacts.contact_id and (accounts_contacts.deleted is null or accounts_contacts.deleted = 0))\n	                                LEFT JOIN accounts\n	                                ON accounts_contacts.account_id=accounts.id  LEFT JOIN  email_addr_bean_rel on contacts.id = email_addr_bean_rel.bean_id and email_addr_bean_rel.bean_module='Contacts' and email_addr_bean_rel.deleted=0 and email_addr_bean_rel.primary_address=1  LEFT JOIN email_addresses on email_addresses.id = email_addr_bean_rel.email_address_id where (contacts.name=\"\") AND ( accounts.deleted IS NULL OR accounts.deleted=0 )\n                      AND contacts.deleted=0 ";
109
		$actual = $contact->create_export_query('contacts.id','contacts.name=""');
110
		$this->assertSame($expected,$actual);
111
		
112
	}
113
114
	public function testfill_in_additional_list_fields() {
115
116
		$contact = new Contact();
117
		
118
		//test with attributes preset and verify attributes are set accordingly
119
		$contact->first_name = "firstn";
120
		$contact->last_name = "lastn";
121
		$contact->email1 = "[email protected]";
122
		$contact->email2 = "[email protected]";
123
		
124
125
		$contact->fill_in_additional_list_fields();
126
	
127
		$this->assertEquals("firstn lastn",$contact->full_name);
128
		$this->assertEquals("firstn lastn &lt;[email protected]&gt;",$contact->email_and_name1);
129
		$this->assertEquals("firstn lastn &lt;[email protected]&gt;",$contact->email_and_name2);
130
		
131
	}
132
133
	public function testfill_in_additional_detail_fields() {
134
135
		$contact = new Contact();
136
		
137
		//test with attributes preset and verify attributes are set accordingly
138
		$contact->id = "1";
139
		
140
		$contact->fill_in_additional_detail_fields();
141
142
		$this->assertEquals("",$contact->account_name);
143
		$this->assertEquals("",$contact->account_id);
144
		$this->assertEquals("",$contact->report_to_name);
145
		
146
	}
147
148
149
	public function testload_contacts_users_relationship(){
150
151
		$contact = new Contact();
152
		
153
		//execute the method and test if it works and does not throws an exception.
154
		try {
155
			$contact->load_contacts_users_relationship();
156
			$this->assertTrue(true);
157
		}
158
		catch (Exception $e) {
159
			$this->fail();
160
		}
161
		
162
	}
163
164
	public function testget_list_view_data() {
165
		
166
		$contact = new Contact();
167
		
168
		//test with attributes preset and verify attributes are set accordingly
169
		$contact->first_name = "first";
170
		$contact->last_name = "last";
171
		
172
		$expected = array (
173
					  'NAME' => 'first last',
174
					  'DELETED' => 0,
175
					  'FIRST_NAME' => 'first',
176
					  'LAST_NAME' => 'last',
177
					  'FULL_NAME' => 'first last',
178
					  'DO_NOT_CALL' => '0',
179
					  'PORTAL_USER_TYPE' => 'Single user',
180
					  'ENCODED_NAME' => 'first last',
181
					  'EMAIL1' => '',
182
					  'EMAIL1_LINK' => '<a href=\'javascript:void(0);\' onclick=\'SUGAR.quickCompose.init({"fullComposeUrl":"contact_id=\\u0026parent_type=Contacts\\u0026parent_id=\\u0026parent_name=first+last\\u0026to_addrs_ids=\\u0026to_addrs_names=first+last\\u0026to_addrs_emails=\\u0026to_email_addrs=first+last%26nbsp%3B%26lt%3B%26gt%3B\\u0026return_module=Contacts\\u0026return_action=ListView\\u0026return_id=","composePackage":{"contact_id":"","parent_type":"Contacts","parent_id":"","parent_name":"first last","to_addrs_ids":"","to_addrs_names":"first last","to_addrs_emails":"","to_email_addrs":"first last \\u003C\\u003E","return_module":"Contacts","return_action":"ListView","return_id":""}});\' class=\'\'>',
183
					  'EMAIL_AND_NAME1' => 'first last &lt;&gt;',
184
					);
185
		
186
		$actual = $contact->get_list_view_data();
187
		//$this->assertSame($expected, $actual);
0 ignored issues
show
Unused Code Comprehensibility introduced by
80% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
188
		$this->assertEquals($expected['NAME'], $actual['NAME']);
189
		$this->assertEquals($expected['FIRST_NAME'], $actual['FIRST_NAME']);
190
		$this->assertEquals($expected['LAST_NAME'], $actual['LAST_NAME']);
191
		$this->assertEquals($expected['FULL_NAME'], $actual['FULL_NAME']);
192
		$this->assertEquals($expected['ENCODED_NAME'], $actual['ENCODED_NAME']);
193
		$this->assertEquals($expected['EMAIL_AND_NAME1'], $actual['EMAIL_AND_NAME1']);
194
		
195
	}
196
197
198
	public function testbuild_generic_where_clause ()
199
	{
200
201
		$contact = new Contact();
202
		
203
		//test with string
204
		$expected = "contacts.last_name like 'test%' or contacts.first_name like 'test%' or accounts.name like 'test%' or contacts.assistant like 'test%' or ea.email_address like 'test%'";
205
		$actual = $contact->build_generic_where_clause('test');
206
		$this->assertSame($expected,$actual);
207
		
208
209
		//test with number
210
		$expected = "contacts.last_name like '1%' or contacts.first_name like '1%' or accounts.name like '1%' or contacts.assistant like '1%' or ea.email_address like '1%' or contacts.phone_home like '%1%' or contacts.phone_mobile like '%1%' or contacts.phone_work like '%1%' or contacts.phone_other like '%1%' or contacts.phone_fax like '%1%' or contacts.assistant_phone like '%1%'";
211
		$actual = $contact->build_generic_where_clause(1);
212
		$this->assertSame($expected,$actual);
213
		
214
	}
215
216
	public function testset_notification_body()
217
	{
218
		$contact = new Contact();
219
		
220
		//test with attributes preset and verify attributes are set accordingly
221
		$contact->first_name = "first";
222
		$contact->last_name = "last";
223
		$contact->description = "some text";
224
		$contact->fill_in_additional_list_fields();
225
		
226
		$result = $contact->set_notification_body(new Sugar_Smarty(), $contact);
227
		
228
		$this->assertEquals($contact->full_name ,$result->_tpl_vars['CONTACT_NAME']);
229
		$this->assertEquals($contact->description ,$result->_tpl_vars['CONTACT_DESCRIPTION']);
230
		
231
	}
232
233
	public function testget_contact_id_by_email()
234
	{
235
		$contact = new Contact();
236
		
237
		$result = $contact->get_contact_id_by_email("");
238
		$this->assertEquals(null,$result);
239
240
	
241
		//$result = $contact->get_contact_id_by_email("[email protected]");
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
242
		//$this->assertEquals(null,$result);
0 ignored issues
show
Unused Code Comprehensibility introduced by
89% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
243
		
244
		$this->markTestSkipped('Invalid Columns(email1,email2) in Query ');
245
		
246
	}
247
248
	public function testsave_relationship_changes() {
249
250
		$contact = new Contact();
251
		
252
		//execute the method and test if it works and does not throws an exception.
253
		try {
254
			$contact->save_relationship_changes(true);
255
			$contact->save_relationship_changes(false);
256
			$this->assertTrue(true);
257
		}
258
		catch (Exception $e) {
259
			$this->fail();
260
		}
261
		
262
	}
263
264
	public function testbean_implements()
265
	{
266
		$contact = new Contact();
267
		$this->assertEquals(false, $contact->bean_implements('')); //test with blank value
268
		$this->assertEquals(false, $contact->bean_implements('test')); //test with invalid value
269
		$this->assertEquals(true, $contact->bean_implements('ACL')); //test with valid value
270
	}
271
272
	public function testget_unlinked_email_query()
273
	{
274
		$contact = new Contact();
275
		
276
		//execute the method and verify that it retunrs expected results
277
		$expected = "SELECT emails.id FROM emails  JOIN (select DISTINCT email_id from emails_email_addr_rel eear\n\n	join email_addr_bean_rel eabr on eabr.bean_id ='' and eabr.bean_module = 'Contacts' and\n	eabr.email_address_id = eear.email_address_id and eabr.deleted=0\n	where eear.deleted=0 and eear.email_id not in\n	(select eb.email_id from emails_beans eb where eb.bean_module ='Contacts' and eb.bean_id = '')\n	) derivedemails on derivedemails.email_id = emails.id";
278
		$actual = $contact->get_unlinked_email_query();
279
		$this->assertSame($expected,$actual);
280
				
281
	}
282
283
284
    public function testprocess_sync_to_outlook()
285
    {
286
    	$contact = new Contact();
287
    	
288
    	//execute the method and test if it works and does not throws an exception.
289
    	try {
290
   			$contact->process_sync_to_outlook("all");
291
    		$this->assertTrue(true);
292
    	}
293
    	catch (Exception $e) {
294
    		$this->fail();
295
    	}
296
    	 
297
298
    	//execute the method and test if it works and does not throws an exception.
299
    	try {
300
    		$contact->process_sync_to_outlook("1");
301
    		$this->assertTrue(true);
302
    	}
303
    	catch (Exception $e) {
304
    		$this->fail();
305
    	}
306
    	 	
307
    	
308
    	
309
	}
310
311
}
312