Skip to content

Commit

Permalink
Fix utf chars
Browse files Browse the repository at this point in the history
  • Loading branch information
fogelito committed Oct 19, 2023
1 parent 2d7e4a0 commit 8aeaea3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 30 deletions.
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
stopOnFailure="true">
<testsuites>
<testsuite name="Application Test Suite">
<directory>./tests/</directory>
Expand Down
37 changes: 8 additions & 29 deletions tests/Database/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -1090,46 +1090,25 @@ public function testInvalidUtfCharacters(): void
static::getDatabase()->createAttribute($collection, 'str', Database::VAR_STRING, 128, true);

// Test insert of null
$str = "\x00";
$str = "\x00"; // Use double quotes!
$this->assertInstanceOf('Utopia\Database\Document', static::getDatabase()->createDocument($collection, new Document([
'str' => $str,
'str' => $str
])));

// Test insert of null
$str = "\u0000";
$str = "\u0000"; // Use double quotes!
$this->assertInstanceOf('Utopia\Database\Document', static::getDatabase()->createDocument($collection, new Document([
'str' => $str,
'str' => $str
])));

// Test insert of null
$str = "\000";
// Test insert of null Use double quotes
$str = "\000"; // Use double quotes!
$this->assertInstanceOf('Utopia\Database\Document', static::getDatabase()->createDocument($collection, new Document([
'str' => $str,
])));

// Test fail to insert non-utf chars
$str = "\xE2\x94";
try {
$this->assertInstanceOf('Utopia\Database\Document', static::getDatabase()->createDocument($collection, new Document([
'str' => $str,
])));
$this->fail('Failed to throw Exception');
} catch (Exception $e) {
$messages = [
"SQLSTATE[22021]: Character not in repertoire: 7 ERROR: invalid byte sequence for encoding \"UTF8\": 0xe2 0x94 0x20", // Postgres
'SQLSTATE[HY000]: General error: 1366 Incorrect string value: \'\xE2\x94\' for column \'str\' at row 1', // MySQL // Use single quotes!
"Detected invalid UTF-8 for field path \"documents.0.str\": \xE2\x94", // Mongo, Use double quotes!
];

$codes = [
'22007', // MariaDB
];

$this->assertTrue(in_array($e->getMessage(), $messages) || in_array($e->getCode(), $codes));
}

// Suggestion for fix after cleanup non
// Test success to insert non-utf chars after removal of non-utf chars
// Suggestion for fix: cleanup non-utf chars
$str = "\xE2\x94"; // Use double quotes!
$str = mb_convert_encoding($str, 'UTF-8', 'UTF-8');
$this->assertInstanceOf('Utopia\Database\Document', static::getDatabase()->createDocument($collection, new Document([
'str' => $str
Expand Down

0 comments on commit 8aeaea3

Please sign in to comment.