[BUGFIX] CsvUtility method csvToArray does not handle enclosures right
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Utility / CsvUtilityTest.php
1 <?php
2
3 /*
4 * This file is part of the TYPO3 CMS project.
5 *
6 * It is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU General Public License, either version 2
8 * of the License, or any later version.
9 *
10 * For the full copyright and license information, please read the
11 * LICENSE.txt file that was distributed with this source code.
12 *
13 * The TYPO3 project - inspiring people to share!
14 */
15
16 use TYPO3\CMS\Core\Tests\UnitTestCase;
17 use TYPO3\CMS\Core\Utility\CsvUtility;
18
19 /**
20 * Test cases of CsvUtility
21 */
22 class CsvUtilityTest extends UnitTestCase {
23
24 /**
25 * @return array
26 */
27 public function csvToArrayDataProvider() {
28 return array(
29 'Valid data' => array(
30 'input' => 'Column A, Column B, Column C' . LF . 'Value, Value2, Value 3',
31 'fieldDelimiter' => ',',
32 'fieldEnclosure' => '"',
33 'maximumColumns' => 0,
34 'expectedResult' => array(
35 array('Column A', ' Column B', ' Column C'),
36 array('Value', ' Value2', ' Value 3')
37 )
38 ),
39
40 'Valid data with enclosed "' => array(
41 'input' => '"Column A", "Column B", "Column C"' . LF . '"Value", "Value2", "Value 3"',
42 'fieldDelimiter' => ',',
43 'fieldEnclosure' => '"',
44 'maximumColumns' => 0,
45 'expectedResult' => array(
46 array('Column A', 'Column B', 'Column C'),
47 array('Value', 'Value2', 'Value 3')
48 )
49 ),
50
51 'Valid data with semicolons and enclosed "' => array(
52 'input' => '"Column A"; "Column B"; "Column C"' . LF . '"Value"; "Value2"; "Value 3"',
53 'fieldDelimiter' => ';',
54 'fieldEnclosure' => '"',
55 'maximumColumns' => 0,
56 'expectedResult' => array(
57 array('Column A', 'Column B', 'Column C'),
58 array('Value', 'Value2', 'Value 3')
59 )
60 ),
61
62 'Valid data with semicolons and enclosed " and two columns' => array(
63 'input' => '"Column A"; "Column B"; "Column C"; "Column D"' . LF . '"Value"; "Value2"; "Value 3"',
64 'fieldDelimiter' => ';',
65 'fieldEnclosure' => '"',
66 'maximumColumns' => 2,
67 'expectedResult' => array(
68 array('Column A', 'Column B'),
69 array('Value', 'Value2')
70 )
71 ),
72
73 'Data with comma but configured with semicolons and enclosed "' => array(
74 'input' => '"Column A", "Column B", "Column C"' . LF . '"Value", "Value2", "Value 3"',
75 'fieldDelimiter' => ';',
76 'fieldEnclosure' => '"',
77 'maximumColumns' => 0,
78 'expectedResult' => array(
79 array('Column A, "Column B", "Column C"'),
80 array('Value, "Value2", "Value 3"')
81 )
82 )
83 );
84 }
85
86 /**
87 * @dataProvider csvToArrayDataProvider
88 * @test
89 */
90 public function csvToArraySplitsAsExpected($input, $fieldDelimiter, $fieldEnclosure, $maximumColumns, $expectedResult) {
91 $this->assertEquals($expectedResult, CsvUtility::csvToArray($input, $fieldDelimiter, $fieldEnclosure, $maximumColumns));
92 }
93 }