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