This is a Test with non-breaking space!
', ENT_COMPAT, 'UTF-8'); + + $this->assertTrue(mb_check_encoding(Convert::html2raw($problematicText), 'UTF-8')); + } + public function testUpperCamelToLowerCamel() { $this->assertEquals( diff --git a/tests/php/ORM/DBHTMLTextTest.php b/tests/php/ORM/DBHTMLTextTest.php index b7ab4e9a7..46f0e0b0f 100644 --- a/tests/php/ORM/DBHTMLTextTest.php +++ b/tests/php/ORM/DBHTMLTextTest.php @@ -15,10 +15,15 @@ use SilverStripe\View\Parsers\ShortcodeParser; class DBHTMLTextTest extends SapphireTest { + private $previousLocaleSetting = null; + protected function setUp() { parent::setUp(); + // clear the previous locale setting + $this->previousLocaleSetting = null; + // Set test handler ShortcodeParser::get('htmltest') ->register('test_shortcode', array(TestShortcode::class, 'handle_shortcode')); @@ -27,6 +32,12 @@ class DBHTMLTextTest extends SapphireTest protected function tearDown() { + + // If a test sets the locale, reset it on teardown + if ($this->previousLocaleSetting) { + setlocale(LC_CTYPE, $this->previousLocaleSetting); + } + ShortcodeParser::set_active('default'); parent::tearDown(); } @@ -581,4 +592,31 @@ class DBHTMLTextTest extends SapphireTest ShortcodeParser::set_active('default'); } + + public function testValidUtf8() + { + // Install a UTF-8 locale + $this->previousLocaleSetting = setlocale(LC_CTYPE, 0); + $locales = array('en_US.UTF-8', 'en_NZ.UTF-8', 'de_DE.UTF-8'); + $localeInstalled = false; + foreach ($locales as $locale) { + if ($localeInstalled = setlocale(LC_CTYPE, $locale)) { + break; + } + } + + // If the system doesn't have any of the UTF-8 locales, exit early + if ($localeInstalled === false) { + $this->markTestIncomplete('Unable to run this test because of missing locale!'); + return; + } + + $problematicText = html_entity_decode('This is a Test with non-breaking space!
', ENT_COMPAT, 'UTF-8'); + + $textObj = new DBHTMLText('Test'); + $textObj->setValue($problematicText); + + $this->assertTrue(mb_check_encoding($textObj->FirstSentence(), 'UTF-8')); + $this->assertTrue(mb_check_encoding($textObj->Summary(), 'UTF-8')); + } } diff --git a/tests/php/ORM/DBTextTest.php b/tests/php/ORM/DBTextTest.php index ccb458c1a..d3fbc3d4a 100644 --- a/tests/php/ORM/DBTextTest.php +++ b/tests/php/ORM/DBTextTest.php @@ -4,6 +4,7 @@ namespace SilverStripe\ORM\Tests; use SilverStripe\ORM\FieldType\DBField; use SilverStripe\Dev\SapphireTest; +use SilverStripe\ORM\FieldType\DBText; /** * Tests parsing and summary methods on DBText @@ -11,6 +12,24 @@ use SilverStripe\Dev\SapphireTest; class DBTextTest extends SapphireTest { + private $previousLocaleSetting = null; + + public function setUp() + { + parent::setUp(); + // clear the previous locale setting + $this->previousLocaleSetting = null; + } + + public function tearDown() + { + parent::tearDown(); + // If a test sets the locale, reset it on teardown + if ($this->previousLocaleSetting) { + setlocale(LC_CTYPE, $this->previousLocaleSetting); + } + } + /** * Test {@link Text->LimitCharacters()} */ @@ -272,4 +291,30 @@ class DBTextTest extends SapphireTest $data = DBField::create_field('Text', '"this is a test"'); $this->assertEquals($data->ATT(), '"this is a test"'); } + + public function testValidUtf8() + { + // Install a UTF-8 locale + $this->previousLocaleSetting = setlocale(LC_CTYPE, 0); + $locales = array('en_US.UTF-8', 'en_NZ.UTF-8', 'de_DE.UTF-8'); + $localeInstalled = false; + foreach ($locales as $locale) { + if ($localeInstalled = setlocale(LC_CTYPE, $locale)) { + break; + } + } + + // If the system doesn't have any of the UTF-8 locales, exit early + if ($localeInstalled === false) { + $this->markTestIncomplete('Unable to run this test because of missing locale!'); + return; + } + + $problematicText = html_entity_decode('This is a Test with non-breaking space!', ENT_COMPAT, 'UTF-8'); + + $textObj = new DBText('Test'); + $textObj->setValue($problematicText); + + $this->assertTrue(mb_check_encoding($textObj->FirstSentence(), 'UTF-8')); + } }