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