Complex classes like EmailTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use EmailTest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
5 | class EmailTest extends PHPUnit_Framework_TestCase { |
||
6 | |||
7 | public function testEmail() |
||
8 | { |
||
9 | |||
10 | //execute the contructor and check for the Object type and attributes |
||
11 | $email = new Email(); |
||
12 | $this->assertInstanceOf('Email',$email); |
||
13 | $this->assertInstanceOf('SugarBean',$email); |
||
14 | |||
15 | $this->assertAttributeEquals('Emails', 'module_dir', $email); |
||
16 | $this->assertAttributeEquals('Email', 'object_name', $email); |
||
17 | $this->assertAttributeEquals('emails', 'table_name', $email); |
||
18 | $this->assertAttributeEquals('Emails', 'module_name', $email); |
||
19 | |||
20 | $this->assertAttributeEquals(true, 'new_schema', $email); |
||
21 | $this->assertAttributeEquals('archived', 'type', $email); |
||
22 | |||
23 | } |
||
24 | |||
25 | public function testemail2init() { |
||
26 | |||
27 | error_reporting(E_ERROR | E_PARSE); |
||
28 | |||
29 | $email = new Email(); |
||
30 | $email->email2init(); |
||
31 | |||
32 | $this->assertInstanceOf('EmailUI',$email->et); |
||
33 | |||
34 | } |
||
35 | |||
36 | public function testbean_implements(){ |
||
37 | |||
38 | $email = new Email(); |
||
39 | $this->assertEquals(false, $email->bean_implements('')); //test with blank value |
||
40 | $this->assertEquals(false, $email->bean_implements('test')); //test with invalid value |
||
41 | $this->assertEquals(true, $email->bean_implements('ACL')); //test with valid value |
||
42 | |||
43 | } |
||
44 | |||
45 | |||
46 | public function testemail2saveAttachment() |
||
47 | { |
||
48 | |||
49 | $email = new Email(); |
||
50 | $result = $email->email2saveAttachment(); |
||
51 | $this->assertTrue(is_array($result)); |
||
52 | |||
53 | } |
||
54 | |||
55 | public function testsafeAttachmentName() { |
||
56 | |||
57 | $email = new Email(); |
||
58 | |||
59 | $this->assertEquals(false,$email->safeAttachmentName("test.ext")); |
||
60 | $this->assertEquals(false,$email->safeAttachmentName("test.exe")); |
||
61 | $this->assertEquals(true,$email->safeAttachmentName("test.cgi")); |
||
62 | |||
63 | } |
||
64 | |||
65 | |||
66 | public function testemail2ParseAddresses() { |
||
67 | |||
68 | |||
69 | $email = new Email(); |
||
70 | |||
71 | $email->email2init(); |
||
72 | $addresses = "abc<[email protected]>,xyz<[email protected]>"; |
||
73 | $expected = array (array('email' =>'[email protected]', 'display' => 'abc'), array('email' => '[email protected]','display' => 'xyz')); |
||
74 | |||
75 | $result = $email->email2ParseAddresses($addresses); |
||
76 | $this->assertSame($expected, $result); |
||
77 | |||
78 | } |
||
79 | |||
80 | |||
81 | public function testemail2ParseAddressesForAddressesOnly() { |
||
82 | |||
83 | $email = new Email(); |
||
84 | |||
85 | //test with simplest format |
||
86 | $addresses = "[email protected],[email protected]"; |
||
87 | $result = $email->email2ParseAddressesForAddressesOnly($addresses); |
||
88 | $this->assertEquals(array("[email protected]", "[email protected]"), $result ); |
||
89 | |||
90 | //test with more used format |
||
91 | $addresses = "abc<[email protected]>,xyz<[email protected]>"; |
||
92 | $result = $email->email2ParseAddressesForAddressesOnly($addresses); |
||
93 | $this->assertEquals(array("[email protected]", "[email protected]"), $result ); |
||
94 | |||
95 | } |
||
96 | |||
97 | |||
98 | |||
99 | public function testemail2GetMime() { |
||
100 | |||
101 | $email = new Email(); |
||
102 | |||
103 | //test without a filename |
||
104 | $result = $email->email2GetMime(); |
||
105 | $this->assertEquals("application/octet-stream", $result ); |
||
106 | |||
107 | //test with a filename |
||
108 | $result = $email->email2GetMime('config.php'); |
||
109 | $this->assertEquals("text/x-php", $result); |
||
110 | |||
111 | } |
||
112 | |||
113 | |||
114 | public function testdecodeDuringSend() { |
||
115 | |||
116 | $email = new Email(); |
||
117 | |||
118 | $this->assertEquals("some text", $email->decodeDuringSend("some text") ); |
||
119 | $this->assertEquals("< some text >", $email->decodeDuringSend("sugarLessThan some text sugarGreaterThan") ); |
||
120 | |||
121 | } |
||
122 | |||
123 | |||
124 | public function testisDraftEmail() |
||
125 | { |
||
126 | $email = new Email(); |
||
127 | |||
128 | //test with required parametr set |
||
129 | $this->assertEquals(true, $email->isDraftEmail(array('saveDraft'=>'1')) ); |
||
130 | |||
131 | |||
132 | //test with one of required attribute set |
||
133 | $email->type = 'draft'; |
||
134 | $this->assertEquals(false, $email->isDraftEmail()); |
||
135 | |||
136 | |||
137 | //test with both of required attribute set |
||
138 | $email->status = 'draft'; |
||
139 | $this->assertEquals(true, $email->isDraftEmail()); |
||
140 | |||
141 | } |
||
142 | |||
143 | |||
144 | public function testgetNamePlusEmailAddressesForCompose() |
||
145 | { |
||
146 | $email = new Email(); |
||
147 | |||
148 | $result = $email->getNamePlusEmailAddressesForCompose("Users", array(1)); |
||
149 | $this->assertGreaterThanOrEqual(0, strlen($result)); |
||
150 | |||
151 | } |
||
152 | |||
153 | public function test_arrayToDelimitedString() |
||
154 | { |
||
155 | $email = new Email(); |
||
156 | |||
157 | //test with empty array |
||
158 | $result = $email->_arrayToDelimitedString(array()); |
||
159 | $this->assertEquals("", $result); |
||
160 | |||
161 | //test with valid array |
||
162 | $result = $email->_arrayToDelimitedString(array('value1','value2')); |
||
163 | $this->assertEquals("value1,value2", $result); |
||
164 | |||
165 | } |
||
166 | |||
167 | |||
168 | public function testsendEmailTest() { |
||
169 | $this->markTestIncomplete("Not testing sending email currently"); |
||
170 | /* |
||
171 | $email = new Email(); |
||
172 | |||
173 | $result = $email->sendEmailTest('mail.someserver.com', 25, 425, false, '', '', '[email protected]', '[email protected]', 'smtp', 'admin'); |
||
174 | |||
175 | $expected = array( "status"=>false, "errorMessage"=> "Error:SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting"); |
||
176 | $this->assertSame($expected, $result); |
||
177 | */ |
||
178 | } |
||
179 | |||
180 | |||
181 | public function testemail2Send() { |
||
182 | $this->markTestIncomplete("Not testing sending email currently"); |
||
183 | /* $email = new Email(); |
||
184 | |||
185 | $_REQUEST['sendSubject'] = "test subject"; |
||
186 | $_REQUEST['sendDescription'] = "test text"; |
||
187 | $_REQUEST['fromAccount'] = "[email protected]"; |
||
188 | $_REQUEST['setEditor'] = 1; |
||
189 | $_REQUEST['description_html'] = "test html"; |
||
190 | $_REQUEST['sendTo'] = "[email protected]"; |
||
191 | |||
192 | $result = $email->email2Send($_REQUEST); |
||
193 | |||
194 | $this->assertEquals(false, $result); |
||
195 | */ |
||
196 | } |
||
197 | |||
198 | public function testsend() { |
||
199 | $this->markTestIncomplete("Not testing sending email currently"); |
||
200 | /* |
||
201 | $email = new Email(); |
||
202 | |||
203 | $email->to_addrs_arr = array('email' =>'[email protected]', 'display' => 'abc'); |
||
204 | $email->cc_addrs_arr = array('email' =>'[email protected]', 'display' => 'abc'); |
||
205 | $email->bcc_addrs_arr = array('email' =>'[email protected]', 'display' => 'abc'); |
||
206 | |||
207 | $email->from_addr = "[email protected]"; |
||
208 | $email->from_name = "abc"; |
||
209 | $email->reply_to_name = "xyz"; |
||
210 | |||
211 | $result = $email->send(); |
||
212 | $this->assertEquals(false, $result); |
||
213 | */ |
||
214 | } |
||
215 | |||
216 | public function testsaveAndOthers() |
||
217 | { |
||
218 | |||
219 | $email = new Email(); |
||
220 | |||
221 | $email->from_addr = "[email protected]"; |
||
222 | $email->to_addrs = "[email protected]"; |
||
223 | $email->cc_addrs = "[email protected]"; |
||
224 | $email->bcc_addrs = "[email protected]"; |
||
225 | |||
226 | $email->from_addr_name = "from"; |
||
227 | $email->to_addrs_names = "to"; |
||
228 | $email->cc_addrs_names = "cc"; |
||
229 | $email->bcc_addrs_names = "bcc"; |
||
230 | $email->reply_to_addr = "[email protected]"; |
||
231 | $email->description = "test description"; |
||
232 | $email->description_html = "test html description"; |
||
233 | $email->raw_source = "test raw source"; |
||
234 | |||
235 | $result = $email->save(); |
||
236 | |||
237 | //test for record ID to verify that record is saved |
||
238 | $this->assertTrue(isset($email->id)); |
||
239 | $this->assertEquals(36, strlen($email->id)); |
||
240 | |||
241 | |||
242 | //test retrieve method |
||
243 | $this->retrieve($email->id); |
||
244 | |||
245 | |||
246 | //test retrieveEmailAddresses method |
||
247 | $this->retrieveEmailAddresses($email->id); |
||
248 | |||
249 | |||
250 | //test retrieveEmailText method |
||
251 | $this->retrieveEmailText($email->id); |
||
252 | |||
253 | |||
254 | //test saveEmailAddresses method |
||
255 | $this->saveEmailAddresses($email->id); |
||
256 | |||
257 | |||
258 | //test linkEmailToAddres method |
||
259 | $this->linkEmailToAddress($email->id); |
||
260 | |||
261 | |||
262 | //test handleAttachments method |
||
263 | $this->handleAttachments($email->id); |
||
264 | |||
265 | //test delete method |
||
266 | $this->delete($email->id); |
||
267 | |||
268 | } |
||
269 | |||
270 | public function retrieve($id) |
||
271 | { |
||
272 | $email = new Email(); |
||
273 | |||
274 | $result = $email->retrieve($id); |
||
275 | |||
276 | $this->assertTrue(isset($result->id)); |
||
277 | $this->assertEquals(36, strlen($result->id)); |
||
278 | |||
279 | |||
280 | $this->assertTrue(isset($result->from_addr)); |
||
281 | $this->assertTrue(isset($result->to_addrs)); |
||
282 | $this->assertTrue(isset($result->cc_addrs)); |
||
283 | $this->assertTrue(isset($result->bcc_addrs)); |
||
284 | |||
285 | |||
286 | $this->assertTrue(isset($result->raw_source)); |
||
287 | $this->assertTrue(isset($result->description_html)); |
||
288 | |||
289 | } |
||
290 | |||
291 | |||
292 | public function saveEmailAddresses($id) { |
||
293 | |||
294 | $email = new Email(); |
||
295 | |||
296 | $email->id = $id; |
||
297 | $email->from_addr = "[email protected]"; |
||
298 | $email->to_addrs = "[email protected]"; |
||
299 | $email->cc_addrs = "[email protected]"; |
||
300 | $email->bcc_addrs = "[email protected]"; |
||
301 | |||
302 | $email->saveEmailAddresses(); |
||
303 | |||
304 | |||
305 | //retrieve and verify that email addresses were saved properly |
||
306 | $email->retrieveEmailAddresses(); |
||
307 | |||
308 | $this->assertNotSame(false, strpos($email->from_addr, "[email protected]") ); |
||
309 | $this->assertNotSame(false, strpos($email->to_addrs, "[email protected]") ); |
||
310 | $this->assertNotSame(false, strpos($email->cc_addrs, "[email protected]") ); |
||
311 | $this->assertNotSame(false, strpos($email->bcc_addrs, "[email protected]") ); |
||
312 | |||
313 | } |
||
314 | |||
315 | |||
316 | public function retrieveEmailAddresses($id) { |
||
317 | |||
318 | $email = new Email(); |
||
319 | |||
320 | $email->id = $id; |
||
321 | $email->retrieveEmailAddresses(); |
||
322 | |||
323 | $this->assertTrue(isset($email->from_addr)); |
||
324 | $this->assertTrue(isset($email->to_addrs)); |
||
325 | $this->assertTrue(isset($email->cc_addrs)); |
||
326 | $this->assertTrue(isset($email->bcc_addrs)); |
||
327 | |||
328 | } |
||
329 | |||
330 | public function linkEmailToAddress($id) { |
||
331 | |||
332 | $email = new Email(); |
||
333 | |||
334 | $email->id = $id; |
||
335 | |||
336 | $result = $email->linkEmailToAddress(1, 'from'); |
||
337 | |||
338 | $this->assertTrue(isset($result)); |
||
339 | $this->assertEquals(36, strlen($result)); |
||
340 | |||
341 | } |
||
342 | |||
343 | |||
344 | public function retrieveEmailText($id) { |
||
345 | |||
346 | $email = new Email(); |
||
347 | |||
348 | $email->id = $id; |
||
349 | |||
350 | $email->retrieveEmailText(); |
||
351 | |||
352 | $this->assertTrue(isset($email->from_addr_name)); |
||
353 | $this->assertTrue(isset($email->reply_to_addr)); |
||
354 | $this->assertTrue(isset($email->to_addrs_names)); |
||
355 | $this->assertTrue(isset($email->cc_addrs_names)); |
||
356 | $this->assertTrue(isset($email->bcc_addrs_names)); |
||
357 | |||
358 | $this->assertTrue(isset($email->raw_source)); |
||
359 | $this->assertTrue(isset($email->description_html)); |
||
360 | |||
361 | } |
||
362 | |||
363 | |||
364 | public function handleAttachments($id) { |
||
365 | |||
366 | $email = new Email(); |
||
367 | |||
368 | $email = $email->retrieve($id); |
||
369 | |||
370 | $email->type = 'out'; |
||
371 | $email->status ='draft'; |
||
372 | $_REQUEST['record'] = $id; |
||
373 | |||
374 | $email->handleAttachments(); |
||
375 | |||
376 | |||
377 | $this->assertTrue(is_array($email->attachments)); |
||
378 | |||
379 | } |
||
380 | |||
381 | public function delete($id) { |
||
382 | |||
383 | $email = new Email(); |
||
384 | |||
385 | $email->delete($id); |
||
386 | |||
387 | $result = $email->retrieve($id); |
||
388 | $this->assertEquals(null,$result); |
||
389 | |||
390 | } |
||
391 | |||
392 | |||
393 | public function testSaveTempNoteAttachmentsAndGetNotesAndDoesImportedEmailHaveAttachment() |
||
394 | { |
||
395 | |||
396 | $email = new Email(); |
||
397 | |||
398 | $email->id = 1; |
||
399 | |||
400 | //test saveTempNoteAttachments method to create a note for email |
||
401 | $email->saveTempNoteAttachments("test_file","test", "text/plain"); |
||
402 | |||
403 | |||
404 | |||
405 | //test doesImportedEmailHaveAttachment method to verify note created. |
||
406 | $result = $email->doesImportedEmailHaveAttachment($email->id); |
||
407 | $this->assertEquals(1,$result); |
||
408 | |||
409 | |||
410 | |||
411 | //test getNotes method and verify that it retrieves the created note. |
||
412 | $email->getNotes($email->id); |
||
413 | $this->assertTrue(is_array($email->attachments)); |
||
414 | foreach($email->attachments as $note){ |
||
415 | $this->assertTrue(isset($note)); |
||
416 | $this->assertInstanceOf('Note',$note); |
||
417 | } |
||
418 | |||
419 | |||
420 | //finally cleanup |
||
421 | $email->delete($email->id); |
||
422 | |||
423 | } |
||
424 | |||
425 | |||
426 | public function testcleanEmails() |
||
427 | { |
||
428 | $email = new Email(); |
||
429 | |||
430 | //test with simplest format |
||
431 | $addresses = "[email protected],[email protected]"; |
||
432 | $result = $email->cleanEmails($addresses); |
||
433 | $this->assertEquals('[email protected], [email protected]', $result); |
||
434 | |||
435 | |||
436 | //test with more used format |
||
437 | $addresses = "abc<[email protected]>,xyz<[email protected]>"; |
||
438 | $result = $email->cleanEmails($addresses); |
||
439 | $this->assertEquals('abc <[email protected]>, xyz <[email protected]>', $result ); |
||
440 | |||
441 | } |
||
442 | |||
443 | |||
444 | public function testgetForwardHeader() { |
||
445 | |||
446 | $email = new Email(); |
||
447 | |||
448 | $email->from_name = "from test"; |
||
449 | $email->name = "test"; |
||
450 | $email->date_sent = "2016-01-01"; |
||
451 | $email->to_addrs = "[email protected]"; |
||
452 | $email->cc_addrs = "[email protected]"; |
||
453 | |||
454 | $expected = "<br /><br />> from test<br />> 2016-01-01<br />> [email protected]<br />> [email protected]<br />> test<br />> <br />"; |
||
455 | |||
456 | $actual = $email->getForwardHeader(); |
||
457 | $this->assertSame($expected, $actual); |
||
458 | |||
459 | } |
||
460 | |||
461 | |||
462 | public function testgetReplyHeader() { |
||
463 | |||
464 | $email = new Email(); |
||
465 | |||
466 | $email->from_name = "from test"; |
||
467 | $email->time_start = "01:01:00"; |
||
468 | $email->date_start = "2016-01-01"; |
||
469 | |||
470 | $expected = "<br> 2016-01-01, 01:01:00, from test "; |
||
471 | |||
472 | $actual = $email->getReplyHeader(); |
||
473 | $this->assertSame($expected, $actual); |
||
474 | |||
475 | } |
||
476 | |||
477 | |||
478 | |||
479 | public function testquotePlainTextEmail() { |
||
480 | |||
481 | $email = new Email(); |
||
482 | |||
483 | //test with plain string containing no line breaks |
||
484 | $expected = "\n> some text\r"; |
||
485 | $actual = $email->quotePlainTextEmail("some text"); |
||
486 | $this->assertSame($expected, $actual); |
||
487 | |||
488 | |||
489 | //test with string containing line breaks |
||
490 | $expected = "\n> some\r> text\r> with\r> new\r> lines\r"; |
||
491 | $actual = $email->quotePlainTextEmail("some\ntext\nwith\nnew\nlines"); |
||
492 | $this->assertSame($expected, $actual); |
||
493 | |||
494 | } |
||
495 | |||
496 | |||
497 | |||
498 | public function testquoteHtmlEmail() { |
||
499 | |||
500 | $email = new Email(); |
||
501 | |||
502 | //test with empty string |
||
503 | $expected = ""; |
||
504 | $actual = $email->quoteHtmlEmail(""); |
||
505 | $this->assertSame($expected, $actual); |
||
506 | |||
507 | |||
508 | //test with plain string |
||
509 | $expected = "<div style='border-left:1px solid #00c; padding:5px; margin-left:10px;'>some test</div>"; |
||
510 | $actual = $email->quoteHtmlEmail("some test"); |
||
511 | $this->assertSame($expected, $actual); |
||
512 | |||
513 | |||
514 | //test with string containing special charecters |
||
515 | $expected = "<div style='border-left:1px solid #00c; padding:5px; margin-left:10px;'>some test with <&</div>"; |
||
516 | $actual = $email->quoteHtmlEmail("some test with <&"); |
||
517 | $this->assertSame($expected, $actual); |
||
518 | |||
519 | } |
||
520 | |||
521 | |||
522 | public function testquoteHtmlEmailForNewEmailUI() { |
||
523 | |||
524 | $email = new Email(); |
||
525 | |||
526 | //test with empty string |
||
527 | $expected = ""; |
||
528 | $actual = $email->quoteHtmlEmailForNewEmailUI(""); |
||
529 | $this->assertSame($expected, $actual); |
||
530 | |||
531 | |||
532 | //test with plain string |
||
533 | $expected = "<div style='border-left:1px solid #00c; padding:5px; margin-left:10px;'>some test</div>"; |
||
534 | $actual = $email->quoteHtmlEmailForNewEmailUI("some test"); |
||
535 | $this->assertSame($expected, $actual); |
||
536 | |||
537 | |||
538 | //test with string containing special charecters |
||
539 | $expected = "<div style='border-left:1px solid #00c; padding:5px; margin-left:10px;'>some test with</div>"; |
||
540 | $actual = $email->quoteHtmlEmailForNewEmailUI("some test with \n"); |
||
541 | $this->assertSame($expected, $actual); |
||
542 | |||
543 | } |
||
544 | |||
545 | |||
546 | public function testcheck_email_settings() { |
||
547 | |||
548 | global $current_user; |
||
549 | |||
550 | $email = new Email(); |
||
551 | |||
552 | //test without a valid current user |
||
553 | $result = $email->check_email_settings(); |
||
554 | $this->assertEquals(false, $result); |
||
555 | |||
556 | |||
557 | //test with a valid current user |
||
558 | $current_user = new User(1); |
||
559 | $result = $email->check_email_settings(); |
||
560 | $this->assertEquals(false, $result); |
||
561 | |||
562 | } |
||
563 | |||
564 | |||
565 | public function testjs_set_archived() { |
||
566 | |||
567 | $email = new Email(); |
||
568 | |||
569 | $actual = $email->js_set_archived(); |
||
570 | $this->assertGreaterThan(0, strlen($actual)); |
||
571 | |||
572 | } |
||
573 | |||
574 | |||
575 | public function testu_get_clear_form_js() { |
||
576 | |||
577 | $email = new Email(); |
||
578 | |||
579 | //with empty params |
||
580 | $expected = "\n <script type=\"text/javascript\" language=\"JavaScript\"><!-- Begin\n function clear_form(form) {\n var newLoc = \"index.php?action=\" + form.action.value + \"&module=\" + form.module.value + \"&query=true&clear_query=true\";\n if(typeof(form.advanced) != \"undefined\"){\n newLoc += \"&advanced=\" + form.advanced.value;\n }\n document.location.href= newLoc;\n }\n // End --></script>"; |
||
581 | $actual = $email->u_get_clear_form_js('', '', ''); |
||
582 | $this->assertSame($expected, $actual); |
||
583 | |||
584 | |||
585 | //with valid params |
||
586 | $expected = "\n <script type=\"text/javascript\" language=\"JavaScript\"><!-- Begin\n function clear_form(form) {\n var newLoc = \"index.php?action=\" + form.action.value + \"&module=\" + form.module.value + \"&query=true&clear_query=true&type=out&assigned_user_id=1\";\n if(typeof(form.advanced) != \"undefined\"){\n newLoc += \"&advanced=\" + form.advanced.value;\n }\n document.location.href= newLoc;\n }\n // End --></script>"; |
||
587 | $actual = $email->u_get_clear_form_js('out', '', '1'); |
||
588 | $this->assertSame($expected, $actual); |
||
589 | |||
590 | } |
||
591 | |||
592 | public function testpickOneButton() { |
||
593 | |||
594 | $email = new Email(); |
||
595 | |||
596 | $expected = "<div><input title=\"\"\n class=\"button\"\n type=\"button\" name=\"button\"\n onClick=\"window.location='index.php?module=Emails&action=Grab';\"\n style=\"margin-bottom:2px\"\n value=\" \"></div>"; |
||
597 | $actual = $email->pickOneButton(); |
||
598 | $this->assertSame($expected, $actual); |
||
599 | |||
600 | } |
||
601 | |||
602 | |||
603 | public function testgetUserEditorPreference() { |
||
604 | |||
605 | $email = new Email(); |
||
606 | |||
607 | $result = $email->getUserEditorPreference(); |
||
608 | $this->assertEquals("html",$result); |
||
609 | |||
610 | } |
||
611 | |||
612 | public function testparse_addrs() { |
||
613 | |||
614 | $email = new Email(); |
||
615 | |||
616 | $addrs = "abc<[email protected]>;xyz<[email protected]>"; |
||
617 | $addrs_ids = "1;2"; |
||
618 | $addrs_names = "abc;xyz"; |
||
619 | $addrs_emails = "[email protected];[email protected]"; |
||
620 | |||
621 | $expected = array( array ('email' => '[email protected]', 'display' => 'abc', 'contact_id' => '1' ), array ('email' => '[email protected]', 'display' => 'xyz', 'contact_id' => '2' ) ); |
||
622 | |||
623 | $actual = $email->parse_addrs($addrs , $addrs_ids, $addrs_names, $addrs_emails); |
||
624 | |||
625 | $this->assertSame($expected, $actual); |
||
626 | |||
627 | } |
||
628 | |||
629 | public function testremove_empty_fields() { |
||
630 | |||
631 | $email = new Email(); |
||
632 | |||
633 | //test for array with empty values |
||
634 | $expected = array("val1", "val2"); |
||
635 | $fields = array("val1", " ", "val2"); |
||
636 | $actual = $email->remove_empty_fields($fields); |
||
637 | $this->assertSame($expected, $actual); |
||
638 | |||
639 | |||
640 | //test for array without empty values |
||
641 | $expected = array("val1","val2"); |
||
642 | $fields = array("val1", "val2"); |
||
643 | $actual = $email->remove_empty_fields($fields); |
||
644 | $this->assertSame($expected, $actual); |
||
645 | |||
646 | } |
||
647 | |||
648 | |||
649 | public function testhasSignatureInBody() { |
||
650 | |||
651 | $email = new Email(); |
||
652 | |||
653 | $email->description_html = "some html text with <b>sign</b>"; |
||
654 | $email->description = "some text with sign"; |
||
655 | |||
656 | //test for strings with signature present |
||
657 | $sig = array("signature_html"=>"sign", "signature"=>"sign" ); |
||
658 | $result = $email->hasSignatureInBody($sig); |
||
659 | $this->assertEquals(true, $result); |
||
660 | |||
661 | //test for strings with signature absent |
||
662 | $sig = array("signature_html"=>"signature", "signature"=>"signature" ); |
||
663 | $result = $email->hasSignatureInBody($sig); |
||
664 | $this->assertEquals(false, $result); |
||
665 | |||
666 | } |
||
667 | |||
668 | |||
669 | |||
670 | public function testremoveAllNewlines() { |
||
671 | |||
672 | $email = new Email(); |
||
673 | |||
674 | $this->assertEquals("", $email->removeAllNewlines("")); |
||
675 | $this->assertEquals("some text", $email->removeAllNewlines("some text")); |
||
676 | $this->assertEquals("some text", $email->removeAllNewlines("some text<br>")); |
||
677 | $this->assertEquals("some text", $email->removeAllNewlines("some\n text\n")); |
||
678 | $this->assertEquals("some text", $email->removeAllNewlines("some\r\n text\r\n")); |
||
679 | |||
680 | } |
||
681 | |||
682 | |||
683 | public function testgetStartPage() { |
||
684 | |||
685 | $email = new Email(); |
||
686 | |||
687 | //test without assigned_user_id in url |
||
688 | $url = "index.php?module=Users&offset=6&stamp=1453274421025259800&return_module=Users&action=DetailView&record=seed_max_id"; |
||
689 | $expected = array ( |
||
690 | 'module' => 'Users', |
||
691 | 'action' => 'DetailView', |
||
692 | 'group' => '', |
||
693 | 'record' => 'seed_max_id', |
||
694 | 'type' => '', |
||
695 | 'offset' => '6', |
||
696 | 'stamp' => '1453274421025259800', |
||
697 | 'return_module' => 'Users', |
||
698 | ); |
||
699 | $actual = $email->getStartPage($url); |
||
700 | $this->assertSame($expected,$actual); |
||
701 | |||
702 | |||
703 | |||
704 | //test with assigned_user_id in url |
||
705 | $url = "index.php?module=Users&offset=6&stamp=1453274421025259800&return_module=Users&action=DetailView&record=seed_max_id&assigned_user_id=1"; |
||
706 | $expected = array ( |
||
707 | 'module' => 'Users', |
||
708 | 'action' => 'DetailView', |
||
709 | 'group' => '', |
||
710 | 'record' => 'seed_max_id', |
||
711 | 'type' => '', |
||
712 | 'offset' => '6', |
||
713 | 'stamp' => '1453274421025259800', |
||
714 | 'return_module' => 'Users', |
||
715 | 'assigned_user_id' => '1', |
||
716 | 'current_view' => 'DetailView&module=Users&assigned_user_id=1&type=' |
||
717 | ); |
||
718 | $actual = $email->getStartPage($url); |
||
719 | $this->assertSame($expected,$actual); |
||
720 | |||
721 | } |
||
722 | |||
723 | |||
724 | |||
725 | public function testsetMailer() { |
||
726 | |||
727 | $email = new Email(); |
||
728 | |||
729 | $result = $email->setMailer(new SugarPHPMailer(),'',''); |
||
730 | |||
731 | $this->assertInstanceOf('SugarPHPMailer',$result); |
||
732 | $this->assertInstanceOf('OutboundEmail',$result->oe); |
||
733 | |||
734 | } |
||
735 | |||
736 | |||
737 | |||
738 | public function testhandleBody() { |
||
739 | |||
740 | $email = new Email(); |
||
741 | |||
742 | //test without setting REQUEST parameters |
||
743 | $email->description = "some email description containing email text & ' <br> "; |
||
744 | |||
745 | $result = $email->handleBody(new SugarPHPMailer()); |
||
746 | |||
747 | $this->assertEquals("some email description containing email text & ' \n ", $email->description); |
||
748 | $this->assertInstanceOf('SugarPHPMailer',$result); |
||
749 | |||
750 | |||
751 | //test with REQUEST parameters set |
||
752 | $_REQUEST['setEditor'] =1; |
||
753 | $_REQUEST['description_html'] = "1"; |
||
754 | $email->description_html = "some email description containing email text & ' <br> "; |
||
755 | |||
756 | $result = $email->handleBody(new SugarPHPMailer()); |
||
757 | |||
758 | $this->assertEquals("some email description containing email text & ' \n ", $email->description); |
||
759 | $this->assertInstanceOf('SugarPHPMailer',$result); |
||
760 | |||
761 | } |
||
762 | |||
763 | |||
764 | |||
765 | public function testhandleBodyInHTMLformat() { |
||
766 | |||
767 | $email = new Email(); |
||
768 | |||
769 | $mailer = new SugarPHPMailer(); |
||
770 | $email->description_html = "some email description containing email text & ' <br> "; |
||
771 | |||
772 | $result = $email->handleBodyInHTMLformat($mailer); |
||
773 | |||
774 | $this->assertEquals("some email description containing email text & ' \n ", $email->description); |
||
775 | $this->assertEquals("some email description containing email text & ' <br> ", $mailer->Body); |
||
776 | |||
777 | } |
||
778 | |||
779 | |||
780 | |||
781 | public function testlistviewACLHelper(){ |
||
782 | |||
783 | $email = new Email(); |
||
784 | |||
785 | $expected = array("MAIN"=>"a", "PARENT"=>"a", "CONTACT"=>"a" ); |
||
786 | $actual = $email->listviewACLHelper(); |
||
787 | $this->assertSame($expected,$actual); |
||
788 | |||
789 | } |
||
790 | |||
791 | public function testgetSystemDefaultEmail() { |
||
792 | |||
793 | $email = new Email(); |
||
794 | |||
795 | $expected = array( 'email' => '[email protected]', 'name' => 'SuiteCRM'); |
||
796 | $actual = $email->getSystemDefaultEmail(); |
||
797 | |||
798 | $this->assertSame($expected,$actual); |
||
799 | |||
800 | } |
||
801 | |||
802 | |||
803 | public function testcreate_new_list_query() { |
||
804 | |||
805 | $email = new Email(); |
||
806 | |||
807 | //test with empty string params |
||
808 | $expected = "SELECT emails.*, users.user_name as assigned_user_name\n FROM emails\n LEFT JOIN users ON emails.assigned_user_id=users.id \nWHERE emails.deleted=0 \n ORDER BY date_sent DESC"; |
||
809 | $actual = $email->create_new_list_query('',''); |
||
810 | $this->assertSame($expected,$actual); |
||
811 | |||
812 | |||
813 | //test with valid string params |
||
814 | $expected = "SELECT emails.*, users.user_name as assigned_user_name\n FROM emails\n LEFT JOIN users ON emails.assigned_user_id=users.id \nWHERE users.user_name = \"\" AND emails.deleted=0 \n ORDER BY emails.id"; |
||
815 | $actual = $email->create_new_list_query('emails.id','users.user_name = ""'); |
||
816 | $this->assertSame($expected,$actual); |
||
817 | |||
818 | } |
||
819 | |||
820 | |||
821 | public function testfill_in_additional_list_fields() { |
||
822 | |||
823 | $email = new Email(); |
||
824 | |||
825 | $email->parent_id = '1'; |
||
826 | $email->parent_name = "test"; |
||
827 | $email->parent_type = 'Contacts'; |
||
828 | |||
829 | $email->fill_in_additional_list_fields(); |
||
830 | |||
831 | $this->assertEquals('DetailView', $email->link_action); |
||
832 | $this->assertEquals('', $email->attachment_image ); |
||
833 | |||
834 | |||
835 | } |
||
836 | |||
837 | public function testfill_in_additional_detail_fields() { |
||
838 | |||
839 | $email = new Email(); |
||
840 | |||
841 | $email->created_by = '1'; |
||
842 | $email->modified_user_id = "1"; |
||
843 | $email->type = 'out'; |
||
844 | $email->status = 'send_error'; |
||
845 | |||
846 | $email->fill_in_additional_list_fields(); |
||
847 | |||
848 | $this->assertEquals('Administrator', $email->created_by_name); |
||
849 | $this->assertEquals('Administrator', $email->modified_by_name ); |
||
850 | $this->assertEquals('', $email->type_name); |
||
851 | $this->assertEquals('', $email->name); |
||
852 | $this->assertEquals('DetailView', $email->link_action); |
||
853 | |||
854 | } |
||
855 | |||
856 | |||
857 | |||
858 | public function testcreate_export_query() |
||
859 | { |
||
860 | |||
861 | $email = new Email(); |
||
862 | |||
863 | //test with empty string params |
||
864 | $expected = "SELECT emails.* FROM emails where emails.deleted=0 ORDER BY emails.name"; |
||
865 | $actual = $email->create_export_query('',''); |
||
866 | $this->assertSame($expected,$actual); |
||
867 | |||
868 | //test with valid string params |
||
869 | $expected = "SELECT emails.* FROM emails where users.user_name = \"\" AND emails.deleted=0 ORDER BY emails.id"; |
||
870 | $actual = $email->create_export_query('emails.id','users.user_name = ""'); |
||
871 | $this->assertSame($expected,$actual); |
||
872 | |||
873 | } |
||
874 | |||
875 | public function testget_list_view_data() { |
||
876 | |||
877 | $email = new Email(); |
||
878 | |||
879 | $email->from_addr_name = "Admin"; |
||
880 | $email->id = 1; |
||
881 | $email->intent = 'support'; |
||
882 | $email->to_addrs = "[email protected]"; |
||
883 | $email->link_action = "DetailView"; |
||
884 | $email->type_name = "out"; |
||
885 | |||
886 | $expected = array ( |
||
887 | 'ID' => 1, |
||
888 | 'FROM_ADDR_NAME' => 'Admin', |
||
889 | 'TYPE' => 'Archived', |
||
890 | 'INTENT' => 'support', |
||
891 | 'FROM_ADDR' => NULL, |
||
892 | 'QUICK_REPLY' => '<a href="index.php?module=Emails&action=Compose&replyForward=true&reply=reply&record=1&inbound_email_id=1">Reply</a>', |
||
893 | 'STATUS' => NULL, |
||
894 | 'CREATE_RELATED' => "~".preg_quote('<a href="index.php?module=Cases&action=EditView&inbound_email_id=1" ><img src="themes/SuiteR/images/CreateCases.gif?v=').'[\w-]+'.preg_quote('" border="0" alt="Create Cases" />Create Case</a>')."~", |
||
895 | 'CONTACT_NAME' => '</a>[email protected]<a>', |
||
896 | 'CONTACT_ID' => '', |
||
897 | 'ATTACHMENT_IMAGE' => NULL, |
||
898 | 'LINK_ACTION' => 'DetailView', |
||
899 | 'TYPE_NAME' => 'out', |
||
900 | ); |
||
901 | |||
902 | $actual = $email->get_list_view_data(); |
||
903 | foreach($expected as $expectedKey => $expectedVal){ |
||
904 | if($expectedKey == 'CREATE_RELATED'){ |
||
905 | $this->assertRegExp($expected[$expectedKey],$actual[$expectedKey]); |
||
906 | }else { |
||
907 | $this->assertSame($expected[$expectedKey], $actual[$expectedKey]); |
||
908 | } |
||
909 | } |
||
910 | } |
||
911 | |||
912 | public function testquickCreateForm() { |
||
913 | |||
914 | $email = new Email(); |
||
915 | |||
916 | $expected = "~".preg_quote("Quick Create <a id='' onclick='return quick_create_overlib(\"\", \"SuiteR\", this);' href=\"#\" ><img src=\"themes/SuiteR/images/advanced_search.gif?v=") |
||
917 | . '[\w-]+' |
||
918 | .preg_quote("\" border='0' align='absmiddle' alt=\"Quick Create\" /></a>") |
||
919 | ."~"; |
||
920 | |||
921 | $actual = $email->quickCreateForm(); |
||
922 | $this->assertRegExp($expected,$actual); |
||
923 | } |
||
924 | |||
925 | |||
926 | |||
927 | public function testsearchImportedEmails() |
||
928 | { |
||
929 | $email = new Email(); |
||
930 | |||
931 | $actual = $email->searchImportedEmails(); |
||
932 | $this->assertTrue(is_array($actual)); |
||
933 | |||
934 | } |
||
935 | |||
936 | |||
937 | public function test_genereateSearchImportedEmailsQuery() |
||
938 | { |
||
939 | $email = new Email(); |
||
940 | |||
941 | $expected = "SELECT emails.id , emails.mailbox_id, emails.name, emails.date_sent, emails.status, emails.type, emails.flagged, emails.reply_to_status,\n emails_text.from_addr, emails_text.to_addrs FROM emails JOIN emails_text on emails.id = emails_text.email_id WHERE (emails.type= 'inbound' OR emails.type='archived' OR emails.type='out') AND emails.deleted = 0 "; |
||
942 | $actual = $email->_genereateSearchImportedEmailsQuery(); |
||
943 | $this->assertSame($expected,$actual); |
||
944 | |||
945 | } |
||
946 | |||
947 | |||
948 | public function test_generateSearchImportWhereClause() |
||
949 | { |
||
950 | |||
951 | $email = new Email(); |
||
952 | |||
953 | //test without request params |
||
954 | $expected = ""; |
||
955 | $actual = $email->_generateSearchImportWhereClause(); |
||
956 | $this->assertSame($expected,$actual); |
||
957 | |||
958 | |||
959 | //test with searchDateFrom request param only |
||
960 | $_REQUEST['searchDateFrom']= "2015-01-01 00:00:00"; |
||
961 | $expected = "emails.date_sent >= '' "; |
||
962 | $actual = $email->_generateSearchImportWhereClause(); |
||
963 | $this->assertSame($expected,$actual); |
||
964 | |||
965 | |||
966 | //test with searchDateTo request param only |
||
967 | $_REQUEST['searchDateFrom'] = ""; |
||
968 | $_REQUEST['searchDateTo']= "2015-01-01 00:00:00"; |
||
969 | $expected = "emails.date_sent <= '' "; |
||
970 | $actual = $email->_generateSearchImportWhereClause(); |
||
971 | $this->assertSame($expected,$actual); |
||
972 | |||
973 | |||
974 | //test with both request params |
||
975 | $_REQUEST['searchDateFrom']= "2015-01-01 00:00:00"; |
||
976 | $_REQUEST['searchDateTo']= "2015-01-01 00:00:00"; |
||
977 | $expected = "( emails.date_sent >= '' AND\n emails.date_sent <= '' )"; |
||
978 | $actual = $email->_generateSearchImportWhereClause(); |
||
979 | $this->assertSame($expected,$actual); |
||
980 | |||
981 | } |
||
982 | |||
983 | |||
984 | public function testtrimLongTo() { |
||
985 | |||
986 | $email = new Email(); |
||
987 | |||
988 | $this->assertEquals("test string", $email->trimLongTo("test string")); //test without any separator |
||
989 | $this->assertEquals("test string 1...", $email->trimLongTo("test string 1, test string2")); //test with , separator |
||
990 | $this->assertEquals("test string 1...", $email->trimLongTo("test string 1; test string2"));//test with ; separator |
||
991 | |||
992 | } |
||
993 | |||
994 | public function testget_summary_text() { |
||
995 | |||
996 | $email = new Email(); |
||
997 | |||
998 | //test without setting name |
||
999 | $this->assertEquals(Null,$email->get_summary_text()); |
||
1000 | |||
1001 | //test with name set |
||
1002 | $email->name = "test"; |
||
1003 | $this->assertEquals('test',$email->get_summary_text()); |
||
1004 | |||
1005 | } |
||
1006 | |||
1007 | public function testdistributionForm() { |
||
1008 | require_once('include/utils/layout_utils.php'); |
||
1009 | $email = new Email(); |
||
1010 | |||
1011 | //test with empty string |
||
1012 | $result = $email->distributionForm(''); |
||
1013 | $this->assertGreaterThan(0, strlen($result)); |
||
1014 | |||
1015 | //test with valid string |
||
1016 | $result = $email->distributionForm('test'); |
||
1017 | $this->assertGreaterThan(0, strlen($result)); |
||
1018 | |||
1019 | } |
||
1020 | |||
1021 | |||
1022 | public function testuserSelectTable() { |
||
1023 | |||
1024 | $email = new Email(); |
||
1025 | |||
1026 | $result = $email->userSelectTable(); |
||
1027 | $this->assertGreaterThan(0, strlen($result)); |
||
1028 | |||
1029 | } |
||
1030 | |||
1031 | |||
1032 | public function testcheckInbox() { |
||
1033 | |||
1034 | |||
1035 | $email = new Email(); |
||
1036 | |||
1037 | //test with empty string |
||
1038 | $expected = "<div><input title=\"\"\n class=\"button\"\n type=\"button\" name=\"button\"\n onClick=\"window.location='index.php?module=Emails&action=Check&type=';\"\n style=\"margin-bottom:2px\"\n value=\" \"></div>"; |
||
1039 | $actual = $email->checkInbox(''); |
||
1040 | $this->assertSame($expected,$actual); |
||
1041 | |||
1042 | |||
1043 | //test with valid string |
||
1044 | $expected = "<div><input title=\"\"\n class=\"button\"\n type=\"button\" name=\"button\"\n onClick=\"window.location='index.php?module=Emails&action=Check&type=test';\"\n style=\"margin-bottom:2px\"\n value=\" \"></div>"; |
||
1045 | $actual = $email->checkInbox('test'); |
||
1046 | $this->assertSame($expected,$actual); |
||
1047 | |||
1048 | } |
||
1049 | |||
1050 | |||
1051 | |||
1052 | public function testfillPrimaryParentFields() { |
||
1053 | |||
1054 | $email = new Email(); |
||
1055 | |||
1056 | //execute the method and test if it works and does not throws an exception. |
||
1057 | try { |
||
1058 | $email->fillPrimaryParentFields(); |
||
1059 | $this->assertTrue(true); |
||
1060 | } |
||
1061 | catch (Exception $e) { |
||
1062 | $this->fail(); |
||
1063 | } |
||
1064 | |||
1065 | } |
||
1066 | |||
1067 | |||
1068 | public function testcid2Link() |
||
1069 | { |
||
1070 | $email = new Email(); |
||
1071 | |||
1072 | $email->description_html = "<img class=\"image\" src=\"cid:1\">"; |
||
1073 | $email->imagePrefix = "prfx"; |
||
1074 | |||
1075 | //execute the method and test if it works and does not throws an exception. |
||
1076 | try { |
||
1077 | $email->cid2Link("1", "image/png"); |
||
1078 | $this->assertTrue(true); |
||
1079 | } |
||
1080 | catch (Exception $e) { |
||
1081 | $this->fail(); |
||
1082 | } |
||
1083 | |||
1084 | |||
1085 | } |
||
1086 | |||
1087 | |||
1088 | public function testcids2Links() |
||
1089 | { |
||
1090 | $email = new Email(); |
||
1091 | |||
1092 | $email->description_html = "<img class=\"image\" src=\"cid:1\">"; |
||
1093 | $email->imagePrefix = "prfx"; |
||
1094 | |||
1095 | //execute the method and test if it works and does not throws an exception. |
||
1096 | try { |
||
1097 | $email->cids2Links(); |
||
1098 | $this->assertTrue(true); |
||
1099 | } |
||
1100 | catch (Exception $e) { |
||
1101 | $this->fail(); |
||
1102 | } |
||
1103 | |||
1104 | } |
||
1105 | |||
1106 | |||
1107 | public function testsetFieldNullable() |
||
1108 | { |
||
1109 | $email = new Email(); |
||
1110 | |||
1111 | //execute the method and test if it works and does not throws an exception. |
||
1112 | try { |
||
1113 | $email->setFieldNullable("description"); |
||
1114 | $this->assertTrue(true); |
||
1115 | } |
||
1116 | catch (Exception $e) { |
||
1117 | $this->fail(); |
||
1118 | } |
||
1119 | |||
1120 | |||
1121 | } |
||
1122 | |||
1123 | |||
1124 | public function testrevertFieldNullable() |
||
1125 | { |
||
1126 | $email = new Email(); |
||
1127 | |||
1128 | //execute the method and test if it works and does not throws an exception. |
||
1129 | try { |
||
1130 | $email->revertFieldNullable("description"); |
||
1131 | $this->assertTrue(true); |
||
1132 | } |
||
1133 | catch (Exception $e) { |
||
1134 | $this->fail(); |
||
1135 | } |
||
1136 | |||
1137 | } |
||
1138 | |||
1139 | } |
||
1140 |