|
6 | 6 | use Throwable; |
7 | 7 | use Utopia\Database\Adapter\SQL; |
8 | 8 | use Utopia\Database\Database; |
9 | | -use Utopia\Database\DateTime; |
10 | 9 | use Utopia\Database\Document; |
11 | 10 | use Utopia\Database\Exception as DatabaseException; |
12 | 11 | use Utopia\Database\Exception\Authorization as AuthorizationException; |
| 12 | +use Utopia\Database\Exception\Character as CharacterException; |
13 | 13 | use Utopia\Database\Exception\Conflict as ConflictException; |
14 | 14 | use Utopia\Database\Exception\Duplicate as DuplicateException; |
15 | 15 | use Utopia\Database\Exception\Limit as LimitException; |
|
22 | 22 |
|
23 | 23 | trait DocumentTests |
24 | 24 | { |
| 25 | + public function testNonUtfChars(): void |
| 26 | + { |
| 27 | + /** @var Database $database */ |
| 28 | + $database = $this->getDatabase(); |
| 29 | + |
| 30 | + if (!$database->getAdapter()->getSupportNonUtfCharacters()) { |
| 31 | + $this->expectNotToPerformAssertions(); |
| 32 | + return; |
| 33 | + } |
| 34 | + |
| 35 | + $database->createCollection(__FUNCTION__); |
| 36 | + $this->assertEquals(true, $database->createAttribute(__FUNCTION__, 'title', Database::VAR_STRING, 128, true)); |
| 37 | + |
| 38 | + $nonUtfString = "Hello\x00World\xC3\x28\xFF\xFE\xA0Test\x00End"; |
| 39 | + |
| 40 | + try { |
| 41 | + $database->createDocument(__FUNCTION__, new Document([ |
| 42 | + 'title' => $nonUtfString, |
| 43 | + ])); |
| 44 | + $this->fail('Failed to throw exception'); |
| 45 | + } catch (Throwable $e) { |
| 46 | + $this->assertTrue($e instanceof CharacterException); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Convert to UTF-8 and replace invalid bytes with empty string |
| 51 | + */ |
| 52 | + $nonUtfString = mb_convert_encoding($nonUtfString, 'UTF-8', 'UTF-8'); |
| 53 | + |
| 54 | + /** |
| 55 | + * Remove null bytes |
| 56 | + */ |
| 57 | + $nonUtfString = str_replace("\0", '', $nonUtfString); |
| 58 | + |
| 59 | + $document = $database->createDocument(__FUNCTION__, new Document([ |
| 60 | + 'title' => $nonUtfString, |
| 61 | + ])); |
| 62 | + |
| 63 | + $this->assertFalse($document->isEmpty()); |
| 64 | + $this->assertEquals('HelloWorld?(???TestEnd', $document->getAttribute('title')); |
| 65 | + } |
| 66 | + |
25 | 67 | public function testBigintSequence(): void |
26 | 68 | { |
27 | 69 | /** @var Database $database */ |
|
0 commit comments