]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/modules/Import/ImportFieldSanitizeTest.php
Added unit tests.
[Github/sugarcrm.git] / tests / modules / Import / ImportFieldSanitizeTest.php
1 <?php
2 require_once('modules/Import/ImportFieldSanitize.php');
3 require_once("modules/Import/ImportFile.php");
4 require_once('tests/SugarTestLangPackCreator.php');
5
6 class ImportFieldSanitizeTest extends Sugar_PHPUnit_Framework_TestCase
7 {
8     public function setUp()
9     {
10         $this->_ifs = new ImportFieldSanitize();
11         $GLOBALS['app_list_strings'] = return_app_list_strings_language($GLOBALS['current_language']);
12         $GLOBALS['current_user'] = SugarTestUserUtilities::createAnonymousUser();
13         $GLOBALS['timedate'] = new TimeDate();
14         $beanList = array();
15         require('include/modules.php');
16         $GLOBALS['beanList'] = $beanList;
17     }
18     
19     public function tearDown()
20     {
21                 SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
22         unset($GLOBALS['current_user']);
23         unset($GLOBALS['app_list_strings']);
24         unset($GLOBALS['beanList']);
25         $GLOBALS['timedate'] = new TimeDate();
26     }
27     
28         public function testValidBool()
29     {
30         $this->assertEquals($this->_ifs->bool(0,array()),0);
31         $this->assertEquals($this->_ifs->bool('no',array()),0);
32         $this->assertEquals($this->_ifs->bool('off',array()),0);
33         $this->assertEquals($this->_ifs->bool('n',array()),0);
34         $this->assertEquals($this->_ifs->bool('yes',array()),1);
35         $this->assertEquals($this->_ifs->bool('y',array()),1);
36         $this->assertEquals($this->_ifs->bool('on',array()),1);
37         $this->assertEquals($this->_ifs->bool(1,array()),1);
38     }
39     
40     public function testValidBoolVarchar()
41     {
42         $vardefs = array('dbType' => 'varchar');
43         
44         $this->assertEquals($this->_ifs->bool(0,$vardefs),'off');
45         $this->assertEquals($this->_ifs->bool('no',$vardefs),'off');
46         $this->assertEquals($this->_ifs->bool('off',$vardefs),'off');
47         $this->assertEquals($this->_ifs->bool('n',$vardefs),'off');
48         $this->assertEquals($this->_ifs->bool('yes',$vardefs),'on');
49         $this->assertEquals($this->_ifs->bool('y',$vardefs),'on');
50         $this->assertEquals($this->_ifs->bool('on',$vardefs),'on');
51         $this->assertEquals($this->_ifs->bool(1,$vardefs),'on');
52     }
53     
54     public function testInvalidBool()
55     {
56         $this->assertFalse($this->_ifs->bool('OK',array()));
57         $this->assertFalse($this->_ifs->bool('yep',array()));
58     }
59     
60     public function testValidCurrency()
61     {
62         $this->_ifs->dec_sep = '.';
63         $this->_ifs->currency_symbol = '$';
64         
65         $this->assertEquals($this->_ifs->currency('$100',array()),100);
66     }
67     
68     public function testInvalidCurrency()
69     {
70         $this->_ifs->dec_sep = '.';
71         $this->_ifs->currency_symbol = '�';
72         
73         $this->assertNotEquals($this->_ifs->currency('$123.23',array()),123.23);
74     }
75     
76     public function testValidDatetimeSameFormat()
77     {
78         $_SESSION[$GLOBALS['current_user']->user_name.'_PREFERENCES']['global']['timezone'] = 'America/New_York';
79         
80         $this->_ifs->dateformat = $GLOBALS['timedate']->get_date_format();
81         $this->_ifs->timeformat = $GLOBALS['timedate']->get_time_format();
82         $this->_ifs->timezone = 'America/New_York';
83         $vardef = array('name' => 'some_date');
84         $date = date($this->_ifs->dateformat . ' ' .$this->_ifs->timeformat);
85
86         $comparedate = date(
87             $GLOBALS['timedate']->get_db_date_time_format(),
88             strtotime(
89                 $GLOBALS['timedate']->handle_offset(
90                     $date, $GLOBALS['timedate']->get_date_time_format(), false, 
91                     $GLOBALS['current_user'], 'America/New_York')
92                 )
93             );
94         
95         $this->assertEquals(
96             $this->_ifs->datetime(
97                 $date,
98                 $vardef),
99             $comparedate);
100         
101         unset($_SESSION[$GLOBALS['current_user']->user_name.'_PREFERENCES']['global']['timezone']);
102     }
103     
104     public function testValidDatetimeDifferentFormat()
105     {
106         $_SESSION[$GLOBALS['current_user']->user_name.'_PREFERENCES']['global']['timezone'] = 'America/New_York';
107         
108         $this->_ifs->dateformat   = 'm/d/Y';
109         if ( $this->_ifs->dateformat == $GLOBALS['timedate']->get_date_format() )
110             $this->_ifs->dateformat = 'Y/m/d';
111         $this->_ifs->timeformat   = 'h:ia';
112         if ( $this->_ifs->timeformat == $GLOBALS['timedate']->get_time_format() )
113             $this->_ifs->timeformat = 'h.ia';
114         $this->_ifs->timezone = 'America/New_York';
115         $vardef = array('name' => 'some_date');
116         $date = date($this->_ifs->dateformat . ' ' . $this->_ifs->timeformat);
117         
118         $comparedate = date(
119             $GLOBALS['timedate']->get_db_date_time_format(),
120             strtotime(
121                 $GLOBALS['timedate']->handle_offset(
122                     $date, $GLOBALS['timedate']->get_date_time_format(), false, 
123                     $GLOBALS['current_user'], 'America/New_York')
124                 ));
125         
126         $this->assertEquals(
127             $this->_ifs->datetime(
128                 $date,
129                 $vardef),
130             $comparedate);
131         
132         unset($_SESSION[$GLOBALS['current_user']->user_name.'_PREFERENCES']['global']['timezone']);
133     }
134     
135     public function testValidDatetimeDifferentTimezones()
136     {
137         $_SESSION[$GLOBALS['current_user']->user_name.'_PREFERENCES']['global']['timezone'] = 'America/New_York';
138         
139         $this->_ifs->dateformat = $GLOBALS['timedate']->get_date_format();
140         $this->_ifs->timeformat = $GLOBALS['timedate']->get_time_format();
141         $format = $GLOBALS['timedate']->get_date_time_format();
142         $this->_ifs->timezone = 'America/Denver';
143         $vardef = array('name' => 'some_date');
144         $date = date($format);
145         $comparedate = date(
146             $GLOBALS['timedate']->get_db_date_time_format(),
147             strtotime('+2 hours',strtotime(
148                 $GLOBALS['timedate']->handle_offset(
149                     $date, $GLOBALS['timedate']->get_date_time_format(), false, 
150                     $GLOBALS['current_user'], 'America/New_York')
151                 )));
152         
153         $this->assertEquals(
154             $this->_ifs->datetime(
155                 $date,
156                 $vardef),
157             $comparedate);
158         
159         unset($_SESSION[$GLOBALS['current_user']->user_name.'_PREFERENCES']['global']['timezone']);
160     }
161     
162     public function testValidDatetimeDateEntered()
163     {
164         $_SESSION[$GLOBALS['current_user']->id.'_PREFERENCES']['global']['timezone'] = 'Atlantic/Cape_Verde';
165         
166         $this->_ifs->dateformat = $GLOBALS['timedate']->get_date_format();
167         $this->_ifs->timeformat = $GLOBALS['timedate']->get_time_format();
168         $format = $GLOBALS['timedate']->get_date_time_format();
169         $this->_ifs->timezone = 'Atlantic/Cape_Verde';
170         $vardef = array('name' => 'date_entered');
171         $date = date($format);
172         $comparedate = date(
173             $GLOBALS['timedate']->get_db_date_time_format(),
174             strtotime('+1 hours',strtotime($date)));
175         
176         $this->assertEquals(
177             $this->_ifs->datetime(
178                 $date,
179                 $vardef),
180             $comparedate);
181         
182         unset($_SESSION[$GLOBALS['current_user']->user_name.'_PREFERENCES']['global']['timezone']);
183     }
184     
185     public function testValidDatetimeDateOnly()
186     {
187         $_SESSION[$GLOBALS['current_user']->user_name.'_PREFERENCES']['global']['timezone'] = 'America/New_York';
188         
189         $this->_ifs->dateformat = $GLOBALS['timedate']->get_date_format();
190         $this->_ifs->timeformat = $GLOBALS['timedate']->get_time_format();
191         $format = $GLOBALS['timedate']->get_date_format();
192         $this->_ifs->timezone = 'America/New_York';
193         $vardef = array('name' => 'date_entered');
194         $date = date($format);
195         $comparedate = date(
196             $GLOBALS['timedate']->get_db_date_time_format(),
197             strtotime($date));
198         
199         $this->assertTrue(
200             (bool) $this->_ifs->datetime(
201                 $date,
202                 $vardef));
203         
204         unset($_SESSION[$GLOBALS['current_user']->user_name.'_PREFERENCES']['global']['timezone']);
205     }
206     
207     public function testInvalidDatetime()
208     {
209         $this->_ifs->dateformat = 'm.d.Y';
210         $this->_ifs->timeformat = 'h:ia';
211         $this->_ifs->timezone = 'America/New_York';
212         
213         $this->assertFalse(
214             $this->_ifs->datetime(
215                 '11/22/2008 11:21',
216                 array('name' => 'some_date')));
217     }
218     
219     public function testInvalidDatetimeBadDayBadHour()
220     {
221         $this->_ifs->dateformat = 'm.d.Y';
222         $this->_ifs->timeformat = 'h:ia';
223         $this->_ifs->timezone = 'America/New_York';
224         
225         $this->assertFalse(
226             $this->_ifs->datetime(
227                 '11/40/2008 18:21',
228                 array('name' => 'some_date')));
229     }
230     
231     public function testValidDateSameFormat()
232     {
233         $this->_ifs->dateformat = $GLOBALS['timedate']->get_date_format();
234         $date = date($this->_ifs->dateformat);
235         
236         $this->assertEquals(
237             $this->_ifs->date(
238                 $date,
239                 array()),
240             $date);
241     }
242     
243     public function testValidDateDifferentFormat()
244     {
245         $this->_ifs->dateformat = 'm/d/Y';
246         if ( $this->_ifs->dateformat  == $GLOBALS['timedate']->get_date_format() )
247             $this->_ifs->dateformat  = 'Y/m/d';
248         $date = date($this->_ifs->dateformat );
249         $comparedate = date(
250             $GLOBALS['timedate']->get_date_format(),
251             strtotime($date));
252         
253         $this->assertEquals(
254             $this->_ifs->date(
255                 $date,
256                 array()),
257             $comparedate);
258     }
259     
260     public function testInvalidDate()
261     {
262         $this->_ifs->dateformat = 'm/d/Y';
263         
264         $this->assertFalse(
265             $this->_ifs->date(
266                 '11/22/08',
267                 array()));
268     }
269     
270     public function testInvalidDateBadMonth()
271     {
272         $this->_ifs->dateformat = 'm/d/Y';
273         
274         $this->assertFalse(
275             $this->_ifs->date(
276                 '22/11/08',
277                 array()));
278     }
279     
280     public function testValidEmail()
281     {
282         $this->assertEquals(
283             $this->_ifs->email(
284                 'sugas@sugarcrm.com',array()),
285             'sugas@sugarcrm.com');
286     }
287     
288     public function testInvalidEmail()
289     {
290         $this->assertFalse(
291             $this->_ifs->email(
292                 'sug$%$@as@sugarcrm.com',array()));
293     }
294     
295     public function testValidEnum()
296     {
297         $vardefs = array('options' => 'salutation_dom');
298         
299         $this->assertEquals(
300             $this->_ifs->enum(
301                 'Mr.',$vardefs),
302             'Mr.');
303     }
304     
305     public function testInvalidEnum()
306     {
307         $vardefs = array('options' => 'salutation_dom');
308         
309         $this->assertFalse(
310             $this->_ifs->enum(
311                 'Foo.',$vardefs));
312     }
313     
314     /**
315          * @group bug23485
316          */
317     public function testEnumWithDisplayValue()
318     {
319         $langpack = new SugarTestLangPackCreator();
320         $langpack->setAppListString('checkbox_dom',array(''=>'','1'=>'Yep','2'=>'Nada'));
321         $langpack->save();
322         
323         $GLOBALS['app_list_strings'] = return_app_list_strings_language($GLOBALS['current_language']);
324         
325         $vardefs = array('options' => 'checkbox_dom');
326         
327         $this->assertEquals(
328             $this->_ifs->enum(
329                 'Yep',$vardefs),
330             '1');
331     }
332     
333     /**
334      * @group bug27467
335      */
336     public function testEnumWithExtraSpacesAtTheEnd()
337     {
338         $langpack = new SugarTestLangPackCreator();
339         $langpack->setAppListString('checkbox_dom',array(''=>'','1'=>'Yep','2'=>'Nada'));
340         $langpack->save();
341         
342         $GLOBALS['app_list_strings'] = return_app_list_strings_language($GLOBALS['current_language']);
343         
344         $vardefs = array('options' => 'checkbox_dom');
345         
346         $this->assertEquals(
347             $this->_ifs->enum(
348                 '    1  ',$vardefs),
349             '1');
350     }
351     
352     /**
353      * @group bug33328
354      */
355     public function testEnumWithKeyInDifferentCase()
356     {
357         $langpack = new SugarTestLangPackCreator();
358         $langpack->setAppListString('gender_list',array('male' => 'Male','female' => 'Female',));
359         $langpack->save();
360         
361         $GLOBALS['app_list_strings'] = return_app_list_strings_language($GLOBALS['current_language']);
362         
363         $vardefs = array('options' => 'gender_list');
364         
365         $this->assertEquals(
366             $this->_ifs->enum(
367                 'MALE',$vardefs),
368             'male');
369     }
370     
371     /**
372      * @group bug33328
373      */
374     public function testEnumWithValueInDifferentCase()
375     {
376         $langpack = new SugarTestLangPackCreator();
377         $langpack->setAppListString('checkbox_dom',array(''=>'','1'=>'Yep','2'=>'Nada'));
378         $langpack->save();
379         
380         $GLOBALS['app_list_strings'] = return_app_list_strings_language($GLOBALS['current_language']);
381         
382         $vardefs = array('options' => 'checkbox_dom');
383         
384         $this->assertEquals(
385             $this->_ifs->enum(
386                 'YEP',$vardefs),
387             '1');
388     }
389     
390     public function testValidId()
391     {
392         $this->assertEquals(
393             $this->_ifs->id(
394                 '1234567890',array()),
395             '1234567890');
396     }
397     
398     public function testInvalidId()
399     {
400         $this->assertFalse(
401             $this->_ifs->id(
402                 '1234567890123456789012345678901234567890',array()));
403     }
404     
405     public function testValidInt()
406     {
407         $this->assertEquals($this->_ifs->int('100',array()),100);
408         
409         $this->_ifs->num_grp_sep = ',';
410         
411         $this->assertEquals($this->_ifs->int('1,123',array()),1123);
412     }
413     
414     public function testInvalidInt()
415     {
416         $this->_ifs->num_grp_sep = '.';
417         $this->assertFalse($this->_ifs->int('123,23',array()));
418         $this->_ifs->num_grp_sep = ',';
419         $this->assertFalse($this->_ifs->int('123.23',array()));
420     }
421     
422     public function testValidFloat()
423     {
424         $this->_ifs->dec_sep = '.';
425         
426         $this->assertEquals($this->_ifs->currency('100',array()),100);
427         $this->assertEquals($this->_ifs->currency('123.23',array()),123.23);
428         
429         $this->_ifs->dec_sep = ',';
430         
431         $this->assertEquals($this->_ifs->currency('123,23',array()),123.23);
432         
433         $this->_ifs->num_grp_sep = ',';
434         
435         $this->assertEquals($this->_ifs->currency('1,123.23',array()),1123.23);
436     }
437     
438     public function testInvalidFloat()
439     {
440         $this->_ifs->dec_sep = '.';
441         
442         $this->assertNotEquals($this->_ifs->currency('123,23',array()),123.23);
443     }
444     
445     public function testValidFullname()
446     {
447         $this->_ifs->default_locale_name_format = 'l f';
448         
449         $focus = loadBean('Contacts');
450         
451         $this->_ifs->fullname('Bar Foo',array(),$focus);
452         
453         $this->assertEquals($focus->first_name,'Foo');
454         $this->assertEquals($focus->last_name,'Bar');
455     }
456     
457     public function testInvalidFullname()
458     {
459         $this->_ifs->default_locale_name_format = 'f l';
460         
461         $focus = loadBean('Contacts');
462         
463         $this->_ifs->fullname('Bar Foo',array(),$focus);
464         
465         $this->assertNotEquals($focus->first_name,'Foo');
466         $this->assertNotEquals($focus->last_name,'Bar');
467     }
468     
469     public function testValidMultiEnum()
470     {
471         $vardefs = array('options' => 'salutation_dom');
472         
473         $this->assertEquals(
474             $this->_ifs->multienum(
475                 'Mr.,Mrs.',$vardefs),
476             encodeMultienumValue(array('Mr.', 'Mrs.')));
477         $this->assertEquals(
478             $this->_ifs->multienum(
479                 '^Mr.^,^Mrs.^',$vardefs),
480             encodeMultienumValue(array('Mr.', 'Mrs.')));
481     }
482     
483     /**
484      * @ticket 37842 
485      */
486     public function testValidMultiEnumWhenSpacesExistInTheValue()
487     {
488         $vardefs = array('options' => 'salutation_dom');
489         
490         $this->assertEquals(
491             $this->_ifs->multienum(
492                 'Mr., Mrs.',$vardefs),
493             encodeMultienumValue(array('Mr.', 'Mrs.')));
494     }
495     
496     public function testInvalidMultiEnum()
497     {
498         $vardefs = array('options' => 'salutation_dom');
499         
500         $this->assertFalse(
501             $this->_ifs->multienum(
502                 'Mr.,foo.',$vardefs));
503     }
504     
505     public function testValidName()
506     {
507         $this->assertEquals(
508             $this->_ifs->name(
509                 '1234567890',array('len' => 12)),
510             '1234567890');
511     }
512     
513     public function testInvalidName()
514     {
515         $this->assertEquals(
516             $this->_ifs->name(
517                 '1234567890123456789012345678901234567890',array('len' => 12)),
518             '123456789012');
519     }
520     
521     public function testParent()
522     {
523         $account_name = 'test case account'.date("YmdHis");
524         $focus = loadBean('Accounts');
525         $focus->name = $account_name;
526         $focus->save();
527         $account_id = $focus->id;
528         
529         $focus = loadBean('Contacts');
530         $vardef = array(
531           'required' => false,
532           'source' => 'non-db',
533           'name' => 'parent_name',
534           'vname' => 'LBL_FLEX_RELATE',
535           'type' => 'parent',
536           'massupdate' => 0,
537           'comments' => '',
538           'help' => '',
539           'importable' => 'false',
540           'duplicate_merge' => 'disabled',
541           'duplicate_merge_dom_value' => '0',
542           'audited' => 0,
543           'reportable' => 0,
544           'len' => 25,
545           'options' => 'parent_type_display',
546           'studio' => 'visible',
547           'type_name' => 'parent_type',
548           'id_name' => 'parent_id',
549           'parent_type' => 'record_type_display',
550         );
551         $focus->parent_name = '';
552         $focus->parent_id = '';
553         $focus->parent_type = 'Accounts';
554         
555         $this->_ifs->parent(
556             $account_name,
557             $vardef,
558             $focus);
559         
560         $this->assertEquals($focus->parent_id,$account_id);
561         
562         $GLOBALS['db']->query("DELETE FROM accounts where id = '$account_id'");
563     }
564     
565     public function testRelate()
566     {
567         $account_name = 'test case account'.date("YmdHis");
568         $focus = loadBean('Accounts');
569         $focus->name = $account_name;
570         $focus->save();
571         $account_id = $focus->id;
572         
573         $focus = loadBean('Contacts');
574         $vardef = array (
575                         'name' => 'account_name',
576                         'rname' => 'name',
577                         'id_name' => 'account_id',
578                         'vname' => 'LBL_ACCOUNT_NAME',
579                         'join_name'=>'accounts',
580                         'type' => 'relate',
581                         'link' => 'accounts',
582                         'table' => 'accounts',
583                         'isnull' => 'true',
584                         'module' => 'Accounts',
585                         'dbType' => 'varchar',
586                         'len' => '255',
587                         'source' => 'non-db',
588                         'unified_search' => true,
589                 );
590         
591         $this->_ifs->relate(
592             $account_name,
593             $vardef,
594             $focus);
595         
596         $this->assertEquals($focus->account_id,$account_id);
597         
598         $GLOBALS['db']->query("DELETE FROM accounts where id = '$account_id'");
599     }
600     
601     public function testRelateCreateRecord()
602     {
603         $account_name = 'test case account'.date("YmdHis");
604         
605         $focus = loadBean('Contacts');
606         $vardef = array (
607                         'name' => 'account_name',
608                         'rname' => 'name',
609                         'id_name' => 'account_id',
610                         'vname' => 'LBL_ACCOUNT_NAME',
611                         'join_name'=>'accounts',
612                         'type' => 'relate',
613                         'link' => 'accounts',
614                         'table' => 'accounts',
615                         'isnull' => 'true',
616                         'module' => 'Accounts',
617                         'dbType' => 'varchar',
618                         'len' => '255',
619                         'source' => 'non-db',
620                         'unified_search' => true,
621                 );
622         
623         // setup
624         $beanList = array();
625         require('include/modules.php');
626         $GLOBALS['beanList'] = $beanList;
627         
628         $this->_ifs->relate(
629             $account_name,
630             $vardef,
631             $focus);
632         
633         // teardown
634         unset($GLOBALS['beanList']);
635         
636         $result = $GLOBALS['db']->query(
637             "SELECT id FROM accounts where name = '$account_name'");
638         $relaterow = $focus->db->fetchByAssoc($result);
639         
640         $this->assertEquals($focus->account_id,$relaterow['id']);
641         
642         $GLOBALS['db']->query("DELETE FROM accounts where id = '{$relaterow['id']}'");
643     }
644     
645     /**
646      * @group bug38356
647      */
648     public function testRelateCreateRecordNoTableInVardef()
649     {
650         $account_name = 'test case account'.date("YmdHis");
651         
652         $focus = loadBean('Contacts');
653         $vardef = array (
654                         'name' => 'account_name',
655                         'rname' => 'name',
656                         'id_name' => 'account_id',
657                         'vname' => 'LBL_ACCOUNT_NAME',
658                         'join_name'=>'accounts',
659                         'type' => 'relate',
660                         'link' => 'accounts',
661                         'isnull' => 'true',
662                         'module' => 'Accounts',
663                         'dbType' => 'varchar',
664                         'len' => '255',
665                         'source' => 'non-db',
666                         'unified_search' => true,
667                 );
668         
669         // setup
670         $beanList = array();
671         require('include/modules.php');
672         $GLOBALS['beanList'] = $beanList;
673         
674         $this->_ifs->relate(
675             $account_name,
676             $vardef,
677             $focus);
678         
679         // teardown
680         unset($GLOBALS['beanList']);
681         
682         $result = $GLOBALS['db']->query(
683             "SELECT id FROM accounts where name = '$account_name'");
684         $relaterow = $focus->db->fetchByAssoc($result);
685         
686         $this->assertEquals($focus->account_id,$relaterow['id']);
687         
688         $GLOBALS['db']->query("DELETE FROM accounts where id = '{$relaterow['id']}'");
689     }
690     
691     /**
692      * @group bug32869
693      */
694     public function testRelateCreateRecordIfNoRnameParameter()
695     {
696         $account_name = 'test case account'.date("YmdHis");
697         
698         $focus = loadBean('Contacts');
699         $vardef = array (
700                         'name' => 'account_name',
701                         'id_name' => 'account_id',
702                         'vname' => 'LBL_ACCOUNT_NAME',
703                         'join_name'=>'accounts',
704                         'type' => 'relate',
705                         'link' => 'accounts',
706                         'table' => 'accounts',
707                         'isnull' => 'true',
708                         'module' => 'Accounts',
709                         'dbType' => 'varchar',
710                         'len' => '255',
711                         'source' => 'non-db',
712                         'unified_search' => true,
713                 );
714         
715         // setup
716         $beanList = array();
717         require('include/modules.php');
718         $GLOBALS['beanList'] = $beanList;
719         
720         $this->_ifs->relate(
721             $account_name,
722             $vardef,
723             $focus);
724         
725         // teardown
726         unset($GLOBALS['beanList']);
727         
728         $result = $GLOBALS['db']->query(
729             "SELECT id FROM accounts where name = '$account_name'");
730         $relaterow = $focus->db->fetchByAssoc($result);
731         
732         $this->assertEquals($focus->account_id,$relaterow['id']);
733         
734         $GLOBALS['db']->query("DELETE FROM accounts where id = '{$relaterow['id']}'");
735     }
736     
737     /**
738      * @group bug26897
739      */
740     public function testRelateCreateRecordCheckACL()
741     {
742         $account_name = 'test case account '.date("YmdHis");
743         
744         $focus = new Import_Bug26897_Mock;
745         $vardef = array (
746             'name' => 'account_name',
747             'rname' => 'name',
748             'id_name' => 'account_id',
749             'vname' => 'LBL_CATEGORY_NAME',
750             'join_name'=>'accounts',
751             'type' => 'relate',
752             'link' => 'accounts_link',
753             'table' => 'accounts',
754             'isnull' => 'true',
755             'module' => 'Import_Bug26897_Mock',
756             'dbType' => 'varchar',
757             'len' => '255',
758             'source' => 'non-db',
759             );
760         
761         // setup
762         $beanList = array();
763         require('include/modules.php');
764         $beanList['Import_Bug26897_Mock'] = 'Import_Bug26897_Mock';
765         $beanFiles['Import_Bug26897_Mock'] = 'modules/Accounts/Account.php';
766         $GLOBALS['beanList'] = $beanList;
767         $GLOBALS['beanFiles'] = $beanFiles;
768         
769         $this->_ifs->relate(
770             $account_name,
771             $vardef,
772             $focus);
773         
774         // teardown
775         unset($GLOBALS['beanList']);
776         unset($GLOBALS['beanFiles']);
777         
778         $result = $GLOBALS['db']->query(
779             "SELECT id FROM accounts where name = '$account_name'");
780         $relaterow = $focus->db->fetchByAssoc($result);
781         
782         $this->assertTrue(empty($focus->account_id),'Category ID should not be set');
783         $this->assertNull($relaterow,'Record should not be added to the related table');
784         
785         $GLOBALS['db']->query("DELETE FROM accounts where id = '{$relaterow['id']}'");
786     }
787     
788     /**
789      * @group bug33704
790      */
791     public function testRelateDoNotCreateRecordIfRelatedModuleIsUsers()
792     {
793         $account_name = 'test case account'.date("YmdHis");
794         $focus = new User;
795         $vardef = array (
796             'name' => 'account_name',
797             'rname' => 'name',
798             'id_name' => 'category_id',
799             'vname' => 'LBL_CATEGORY_NAME',
800             'join_name'=>'accounts',
801             'type' => 'relate',
802             'link' => 'account_link',
803             'table' => 'users',
804             'isnull' => 'true',
805             'module' => 'Users',
806             'dbType' => 'varchar',
807             'len' => '255',
808             'source' => 'non-db',
809             );
810         
811         $this->_ifs->relate(
812             $account_name,
813             $vardef,
814             $focus);
815         
816         // teardown
817         unset($GLOBALS['beanList']);
818         unset($GLOBALS['beanFiles']);
819         
820         $result = $GLOBALS['db']->query(
821             "SELECT id FROM accounts where name = '$account_name'");
822         $relaterow = $focus->db->fetchByAssoc($result);
823         
824         $this->assertTrue(empty($focus->account_id),'Category ID should not be set');
825         $this->assertNull($relaterow,'Record should not be added to the related table');
826         
827         $GLOBALS['db']->query("DELETE FROM accounts where id = '{$relaterow['id']}'");
828     }
829     
830     /**
831      * @group bug38885
832      */
833     public function testRelateToUserNameWhenFullNameIsGiven()
834     {
835         // setup
836         $beanList = array();
837         require('include/modules.php');
838         $GLOBALS['beanList'] = $beanList;
839         $GLOBALS['beanFiles'] = $beanFiles;
840         
841         $accountFocus = new Account;
842         $userFocus = SugarTestUserUtilities::createAnonymousUser();
843         $vardef = array(
844             "name" => "assigned_user_name",
845             "link" => "assigned_user_link",
846             "vname" => "LBL_ASSIGNED_TO_NAME",
847             "rname" => "user_name",
848             "type" => "relate",
849             "reportable" => false,
850             "source" => "non-db",
851             "table" => "users",
852             "id_name" => "assigned_user_id",
853             "module" => "Users",
854             "duplicate_merge" => "disabled",
855             );
856         
857         $this->assertEquals(
858             $userFocus->user_name,
859             $this->_ifs->relate(
860                 $userFocus->first_name.' '.$userFocus->last_name,
861                 $vardef,
862                 $accountFocus,
863                 false)
864             );
865         
866         // teardown
867         unset($GLOBALS['beanList']);
868         unset($GLOBALS['beanFiles']);
869     }
870     
871     /**
872      * @group bug27562
873      */
874     public function testRelateCreateRecordUsingMultipleFieldToLinkRecords()
875     {
876         $contact_name = 'testcase contact'.date("YmdHis");
877         
878         $focus = new Import_Bug27562_Mock;
879         
880         $vardef = array (
881             'name' => 'contact_name',
882             'rname' => 'name',
883             'id_name' => 'contact_id',
884             'vname' => 'LBL_CATEGORY_NAME',
885             'join_name'=>'contacts',
886             'type' => 'relate',
887             'link' => 'contact_link',
888             'table' => 'contacts',
889             'isnull' => 'true',
890             'module' => 'Import_Bug27562_Mock',
891             'dbType' => 'varchar',
892             'len' => '255',
893             'source' => 'non-db',
894             );
895         
896         // setup
897         $beanList = array();
898         require('include/modules.php');
899         $beanList['Import_Bug27562_Mock'] = 'Import_Bug27562_Mock';
900         $beanFiles['Import_Bug27562_Mock'] = 'modules/Contacts/Contact.php';
901         $GLOBALS['beanList'] = $beanList;
902         $GLOBALS['beanFiles'] = $beanFiles;
903         
904         $this->_ifs->relate(
905             $contact_name,
906             $vardef,
907             $focus);
908         
909         // teardown
910         unset($GLOBALS['beanList']);
911         unset($GLOBALS['beanFiles']);
912         
913         $nameParts = explode(' ',$contact_name);
914         $result = $GLOBALS['db']->query(
915             "SELECT id FROM contacts where first_name = '{$nameParts[0]}' and last_name = '{$nameParts[1]}'");
916         $relaterow = $focus->db->fetchByAssoc($result);
917         
918         $this->assertEquals($focus->contact_id,$relaterow['id']);
919         
920         $GLOBALS['db']->query("DELETE FROM contacts where id = '{$relaterow['id']}'");
921     }
922     
923     public function testRelateDontCreateRecord()
924     {
925         $account_name = 'test case account'.date("YmdHis");
926         
927         $focus = loadBean('Contacts');
928         $vardef = array (
929                         'name' => 'account_name',
930                         'rname' => 'name',
931                         'id_name' => 'account_id',
932                         'vname' => 'LBL_ACCOUNT_NAME',
933                         'join_name'=>'accounts',
934                         'type' => 'relate',
935                         'link' => 'accounts',
936                         'table' => 'accounts',
937                         'isnull' => 'true',
938                         'module' => 'Accounts',
939                         'dbType' => 'varchar',
940                         'len' => '255',
941                         'source' => 'non-db',
942                         'unified_search' => true,
943                 );
944         
945         // setup
946         $beanList = array();
947         require('include/modules.php');
948         $GLOBALS['beanList'] = $beanList;
949         
950         $this->assertFalse(
951             $this->_ifs->relate(
952                 $account_name,
953                 $vardef,
954                 $focus,
955                 false),
956             'Should return false since record could not be found'
957             );
958         
959         // teardown
960         unset($GLOBALS['beanList']);
961         
962         $result = $GLOBALS['db']->query(
963             "SELECT id FROM accounts where name = '$account_name'");
964         $relaterow = $focus->db->fetchByAssoc($result);
965         $this->assertNull($relaterow,'Record should not have been created');
966         if ( $relaterow )
967             $GLOBALS['db']->query("DELETE FROM accounts where id = '{$relaterow['id']}'");
968     }
969     
970     /**
971      * @group bug27046
972      */
973     public function testRelateWithInvalidDataFormatting()
974     {
975         $langpack = new SugarTestLangPackCreator();
976         $langpack->setAppListString('checkbox_dom',array(''=>'','1'=>'Yep','2'=>'Nada'));
977         $langpack->save();
978         
979         $GLOBALS['app_list_strings'] = return_app_list_strings_language($GLOBALS['current_language']);
980         
981         $account_name = 'test case category'.date("YmdHis");
982         
983         $focus = new Import_Bug27046_Mock;
984         $vardef = array (
985             'name' => 'account_name',
986             'rname' => 'name',
987             'id_name' => 'account_id',
988             'vname' => 'LBL_ACCOUNT_NAME',
989             'join_name'=>'accounts',
990             'type' => 'relate',
991             'link' => 'accounts_link',
992             'table' => 'accounts',
993             'isnull' => 'true',
994             'module' => 'Import_Bug27046_Mock',
995             'dbType' => 'varchar',
996             'len' => '255',
997             'source' => 'non-db',
998             'rtype' => 'int',
999             );
1000         
1001         // setup
1002         $beanList = array();
1003         require('include/modules.php');
1004         $beanList['Import_Bug27046_Mock'] = 'Import_Bug27046_Mock';
1005         $beanFiles['Import_Bug27046_Mock'] = 'modules/Accounts/Account.php';
1006         $GLOBALS['beanList'] = $beanList;
1007         $GLOBALS['beanFiles'] = $beanFiles;
1008         
1009         $this->assertFalse(
1010             $this->_ifs->relate(
1011                 $account_name,
1012                 $vardef,
1013                 $focus),
1014             'Should return false since field format is invalid'
1015             );
1016         
1017         // teardown
1018         unset($GLOBALS['beanList']);
1019         
1020         $result = $GLOBALS['db']->query(
1021             "SELECT id FROM accounts where name = '$account_name'");
1022         $relaterow = $focus->db->fetchByAssoc($result);
1023         $this->assertNull($relaterow,'Record should not have been created');
1024         if ( $relaterow )
1025             $GLOBALS['db']->query("DELETE FROM accounts where id = '{$relaterow['id']}'");
1026     }
1027     
1028     public function testValidSyncToOutlookUser()
1029     {
1030         $value = $GLOBALS['current_user']->id . ',' . $GLOBALS['current_user']->user_name;
1031         $bad_names = array();
1032         
1033         $this->assertTrue(
1034             (bool) $this->_ifs->synctooutlook(
1035                 $value,
1036                 array(),
1037                 $bad_names
1038                 ),
1039             'Test $this->_ifs->synctooutlook() not returning false');
1040         
1041         $this->assertEquals($bad_names,array());
1042     }
1043     public function testInvalidSyncToOutlook()
1044     {
1045         $value = "jghu8h8yhuh8hhi889898898";
1046         $bad_names = array();
1047         
1048         $this->assertFalse(
1049             $this->_ifs->synctooutlook(
1050                 $value,
1051                 array(),
1052                 $bad_names
1053                 ),
1054             'Test $this->_ifs->synctooutlook() should return false');
1055     }
1056     
1057     public function testValidTimeSameFormat()
1058     {
1059         $_SESSION[$GLOBALS['current_user']->user_name.'_PREFERENCES']['global']['timezone'] = 'America/New_York';
1060         
1061         $this->_ifs->timeformat = $GLOBALS['timedate']->get_time_format();
1062         $this->_ifs->timezone = 'America/New_York';
1063         $vardef = array('name' => 'some_date');
1064         $date = date($this->_ifs->timeformat);
1065         
1066         $this->assertEquals(
1067             $this->_ifs->time(
1068                 $date,
1069                 $vardef),
1070             $date);
1071         
1072         unset($_SESSION[$GLOBALS['current_user']->user_name.'_PREFERENCES']['global']['timezone']);
1073     }
1074     
1075     public function testValidTimeDifferentFormat()
1076     {
1077         $_SESSION[$GLOBALS['current_user']->user_name.'_PREFERENCES']['global']['timezone'] = 'America/New_York';
1078         
1079         $this->_ifs->timeformat = 'h:ia';
1080         if ( $this->_ifs->timeformat == $GLOBALS['timedate']->get_time_format() )
1081             $this->_ifs->timeformat = 'h.ia';
1082         $this->_ifs->timezone = 'America/New_York';
1083         $vardef = array('name' => 'some_date');
1084         
1085         $date = date($this->_ifs->timeformat);
1086         $comparedate = date(
1087             $GLOBALS['timedate']->get_time_format(),
1088             strtotime($date));
1089         
1090         $this->assertEquals(
1091             $this->_ifs->time(
1092                 $date,
1093                 $vardef),
1094             $comparedate);
1095         
1096         unset($_SESSION[$GLOBALS['current_user']->user_name.'_PREFERENCES']['global']['timezone']);
1097     }
1098     
1099     public function testValidTimeDifferentTimezones()
1100     {
1101         $_SESSION[$GLOBALS['current_user']->user_name.'_PREFERENCES']['global']['timezone'] = 'America/New_York';
1102         
1103         $this->_ifs->timeformat = $GLOBALS['timedate']->get_time_format();
1104         $this->_ifs->timezone = 'America/Denver';
1105         $vardef = array('name' => 'some_date');
1106         $date = date($this->_ifs->timeformat);
1107         $comparedate = date(
1108             $GLOBALS['timedate']->get_time_format(),
1109             strtotime('+2 hours',strtotime($date)));
1110         
1111         $this->assertEquals(
1112             $this->_ifs->time(
1113                 $date,
1114                 $vardef),
1115             $comparedate);
1116         
1117         unset($_SESSION[$GLOBALS['current_user']->user_name.'_PREFERENCES']['global']['timezone']);
1118     }
1119     
1120     public function testInvalidTime()
1121     {
1122         $this->_ifs->timeformat = 'h:ia';
1123         $this->_ifs->timezone = 'America/New_York';
1124         
1125         $this->assertFalse(
1126             $this->_ifs->time(
1127                 '11:21',
1128                 array('name' => 'some_date')));
1129     }
1130     
1131     public function testInvalidTimeBadSeconds()
1132     {
1133         $this->_ifs->timeformat = 'h:ia';
1134         $this->_ifs->timezone = 'America/New_York';
1135         
1136         $this->assertFalse(
1137             $this->_ifs->time(
1138                 '11:60',
1139                 array('name' => 'some_date')));
1140     }
1141 }
1142
1143 class Import_Bug26897_Mock extends Account
1144 {
1145     function ACLAccess($view,$is_owner='not_set')
1146     {
1147         return false;
1148     }
1149     
1150     function bean_implements($interface)
1151     {
1152                 return true;
1153     }
1154 }
1155
1156 class Import_Bug27562_Mock extends Contact
1157 {
1158     function ACLAccess($view,$is_owner='not_set')
1159     {
1160         return true;
1161     }
1162 }
1163
1164 class Import_Bug27046_Mock extends Account
1165 {
1166     function ACLAccess($view,$is_owner='not_set')
1167     {
1168         return false;
1169     }
1170     
1171     function bean_implements($interface)
1172     {
1173                 return true;
1174     }
1175     
1176     function getFieldDefintion($name)
1177     {
1178         return array(
1179             'name' => 'name',
1180             'type' => 'int',
1181             );
1182     }
1183 }