2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
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.
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
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
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.
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.
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 ********************************************************************************/
39 require_once 'modules/Import/Importer.php';
40 require_once 'modules/Import/sources/ImportFile.php';
42 class ImporterTest extends Sugar_PHPUnit_Framework_TestCase
44 private $_importModule;
45 private $_importObject;
47 // date_entered and last_name
48 private static $CsvContent = array (
49 0 => "\"3/26/2011 10:02am\",\"Doe\"",
50 1 => "\"2011-3-26 10:2 am\",\"Doe\"",
51 2 => "\"3.26.2011 10.02\",\"Doe\"",
54 public function setUp()
58 require('include/modules.php');
59 $GLOBALS['beanList'] = $beanList;
60 $GLOBALS['beanFiles'] = $beanFiles;
62 $GLOBALS['current_user'] = SugarTestUserUtilities::createAnonymousUser();
63 $this->_importModule = 'Contacts';
64 $this->_importObject = 'Contact';
67 public function tearDown()
69 $GLOBALS['db']->query("DELETE FROM contacts where created_by='{$GLOBALS['current_user']->id}'");
71 SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
72 restore_error_handler();
75 public function providerCsvData()
78 array(0, '2011-03-26 10:02:00', 'm/d/Y', 'h:ia'),
79 array(1, '2011-03-26 10:02:00', 'Y-m-d', 'h:ia'),
80 array(2, '2011-03-26 10:02:00', 'm.d.Y', 'H.i'),
85 * @dataProvider providerCsvData
87 public function testDateTimeImport($content_idx, $expected_datetime, $date_format, $time_format)
89 $file = $GLOBALS['sugar_config']['upload_dir'].'test.csv';
90 $ret = file_put_contents($file, self::$CsvContent[$content_idx]);
91 $this->assertGreaterThan(0, $ret, 'Failed to write to '.$file .' for content '.$content_idx);
93 $importSource = new ImportFile($file, ',', '"');
95 $bean = loadBean($this->_importModule);
97 $_REQUEST['columncount'] = 2;
98 $_REQUEST['colnum_0'] = 'date_entered';
99 $_REQUEST['colnum_1'] = 'last_name';
100 $_REQUEST['import_module'] = 'Contacts';
101 $_REQUEST['importlocale_charset'] = 'UTF-8';
102 $_REQUEST['importlocale_dateformat'] = $date_format;
103 $_REQUEST['importlocale_timeformat'] = $time_format;
104 $_REQUEST['importlocale_timezone'] = 'GMT';
105 $_REQUEST['importlocale_default_currency_significant_digits'] = '2';
106 $_REQUEST['importlocale_currency'] = '-99';
107 $_REQUEST['importlocale_dec_sep'] = '.';
108 $_REQUEST['importlocale_currency'] = '-99';
109 $_REQUEST['importlocale_default_locale_name_format'] = 's f l';
110 $_REQUEST['importlocale_num_grp_sep'] = ',';
112 $importer = new Importer($importSource, $bean);
115 $query = "SELECT date_entered from contacts where created_by='{$GLOBALS['current_user']->id}'";
116 $result = $GLOBALS['db']->query($query);
117 $row = $GLOBALS['db']->fetchByAssoc($result);
119 $this->assertEquals($expected_datetime, $GLOBALS['db']->fromConvert($row['date_entered'], 'datetime'), 'Got incorrect date_entered.');