spout/tests/Spout/Common/Escaper/XLSXTest.php
Adrien Loison 048105461c Fix shared strings XML Entities auto decode (#411)
When converting an XMLReader node to a SimpleXMLElement, the conversion would automatically decode the XML entities. This resulted in a double decode.
For example: """ was converted to """ when imported into a SimpleXMLElement and was again converted into " (quote).

This commit changes the way the XLSX Shared Strings file is processed. It also changes the unescaping logic for both XLSX and ODS.

Finally, it removes any usage of the SimpleXML library (yay!).
2017-04-28 02:27:33 +02:00

84 lines
2.5 KiB
PHP

<?php
namespace Box\Spout\Common\Escaper;
/**
* Class XLSXTest
*
* @package Box\Spout\Common\Escaper
*/
class XLSXTest extends \PHPUnit_Framework_TestCase
{
/**
* @return array
*/
public function dataProviderForTestEscape()
{
return [
['test', 'test'],
['adam\'s "car"', 'adam\'s "car"'],
["\n", "\n"],
["\r", "\r"],
["\t", "\t"],
[chr(0), '_x0000_'],
[chr(4), '_x0004_'],
['_x0000_', '_x005F_x0000_'],
[chr(21), '_x0015_'],
['control '.chr(21).' character', 'control _x0015_ character'],
['control\'s '.chr(21).' "character"', 'control\'s _x0015_ "character"'],
];
}
/**
* @dataProvider dataProviderForTestEscape
*
* @param string $stringToEscape
* @param string $expectedEscapedString
* @return void
*/
public function testEscape($stringToEscape, $expectedEscapedString)
{
/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
$escaper = \Box\Spout\Common\Escaper\XLSX::getInstance();
$escapedString = $escaper->escape($stringToEscape);
$this->assertEquals($expectedEscapedString, $escapedString, 'Incorrect escaped string');
}
/**
* @return array
*/
public function dataProviderForTestUnescape()
{
return [
['test', 'test'],
['adam&#039;s &quot;car&quot;', 'adam&#039;s &quot;car&quot;'],
["\n", "\n"],
["\r", "\r"],
["\t", "\t"],
['_x0000_', chr(0)],
['_x0004_', chr(4)],
['_x005F_x0000_', '_x0000_'],
['_x0015_', chr(21)],
['control _x0015_ character', 'control '.chr(21).' character'],
['control&#039;s _x0015_ &quot;character&quot;', 'control&#039;s '.chr(21).' &quot;character&quot;'],
];
}
/**
* @dataProvider dataProviderForTestUnescape
*
* @param string $stringToUnescape
* @param string $expectedUnescapedString
* @return void
*/
public function testUnescape($stringToUnescape, $expectedUnescapedString)
{
/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
$escaper = \Box\Spout\Common\Escaper\XLSX::getInstance();
$unescapedString = $escaper->unescape($stringToUnescape);
$this->assertEquals($expectedUnescapedString, $unescapedString, 'Incorrect escaped string');
}
}