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/CsvAutoDetect.php';
41 class CsvAutoDetectTest extends Sugar_PHPUnit_Framework_TestCase
43 private static $CsvContent = array (
44 0 => "\"date_entered\",\"description\"\n\"3/26/2011 10:02am\",\"test description\"",
45 1 => "\"date_entered\"\t\"description\"\n\"2011-3-26 10:2 am\"\t\"test description\"",
46 2 => "\"date_entered\",\"description\"\n\"3.26.2011 15.02\",\"test description\"",
47 3 => "\"3/26/2011 10:02am\",\"some text\"\n\"4/26/2011 11:20am\",\"some more jim's text\"",
48 4 => "\"date_entered\",\"description\"\n\"2010/03/26 10:2am\",\"test description\"",
49 5 => "'date_entered','description'\n'26/3/2011 15:02','test description'",
50 6 => "\"date_entered\"|\"description\"\n\"3/26/2011 10:02am\"|\"test description\"",
53 public function setUp()
55 // if beanList got unset, set it back
56 if (!isset($GLOBALS['beanList'])) {
57 require('include/modules.php');
58 $GLOBALS['beanList'] = $beanList;
62 public function tearDown()
66 public function providerCsvData()
69 array(0, ',', '"', 'm/d/Y', 'h:ia', true),
70 array(1, "\t", '"', 'Y-m-d', 'h:i a', true),
71 array(2, ",", '"', 'm.d.Y', 'H.i', true),
72 array(3, ',', '"', 'm/d/Y', 'h:ia', false),
73 array(4, ',', '"', 'Y/m/d', 'h:ia', true),
74 array(5, ',', "'", 'd/m/Y', 'H:i', true),
75 array(6, '|', '"', 'm/d/Y', 'h:ia', true),
80 * @dataProvider providerCsvData
82 public function testGetCsvProperties($content_idx, $delimiter, $enclosure, $date, $time, $header)
84 $file = $GLOBALS['sugar_config']['tmp_dir'].'test.csv';
85 $ret = file_put_contents($file, self::$CsvContent[$content_idx]);
86 $this->assertGreaterThan(0, $ret, 'Failed to write to '.$file .' for content '.$content_idx);
88 $auto = new CsvAutoDetect($file);
89 $del = $enc = $hasHeader = false;
90 $ret = $auto->getCsvSettings($del, $enc);
91 $this->assertEquals(true, $ret, 'Failed to parse and get csv properties');
94 $this->assertEquals($delimiter, $del, 'Incorrect delimiter');
97 $this->assertEquals($enclosure, $enc, 'Incorrect enclosure');
100 $date_format = $auto->getDateFormat();
101 $this->assertEquals($date, $date_format, 'Incorrect date format');
104 $time_format = $auto->getTimeFormat();
105 $this->assertEquals($time, $time_format, 'Incorrect time format');
108 $ret = $auto->hasHeader($hasHeader, 'Contacts');
109 $this->assertTrue($ret, 'Failed to detect header');
110 $this->assertEquals($header, $hasHeader, 'Incorrect header');
113 unlink($GLOBALS['sugar_config']['tmp_dir'].'test.csv');