@@ -2814,6 +2814,54 @@ public function testGetDocument(Document $document): Document
28142814 return $document;
28152815 }
28162816
2817+ public function testEncryptAttributes(): void
2818+ {
2819+ // Add custom encrypt filter
2820+ static::getDatabase()->addFilter(
2821+ 'encrypt',
2822+ function (mixed $value) {
2823+ return json_encode([
2824+ 'data' => base64_encode($value),
2825+ 'method' => 'base64',
2826+ 'version' => 'v1',
2827+ ]);
2828+ },
2829+ function (mixed $value) {
2830+ if (is_null($value)) {
2831+ return;
2832+ }
2833+ $value = json_decode($value, true);
2834+ return base64_decode($value['data']);
2835+ }
2836+ );
2837+
2838+ $col = static::getDatabase()->createCollection(__FUNCTION__);
2839+ $this->assertNotNull($col->getId());
2840+
2841+ static::getDatabase()->createAttribute($col->getId(), 'title', Database::VAR_STRING, 255, true);
2842+ static::getDatabase()->createAttribute($col->getId(), 'encrypt', Database::VAR_STRING, 128, true, filters: ['encrypt']);
2843+
2844+ static::getDatabase()->createDocument($col->getId(), new Document([
2845+ 'title' => 'Sample Title',
2846+ 'encrypt' => 'secret',
2847+ ]));
2848+ // query against encrypt
2849+ try {
2850+ $queries = [Query::equal('encrypt', ['test'])];
2851+ $doc = static::getDatabase()->find($col->getId(), $queries);
2852+ $this->fail('Queried against encrypt field. Failed to throw exeception.');
2853+ } catch (Throwable $e) {
2854+ $this->assertTrue($e instanceof QueryException);
2855+ }
2856+
2857+ try {
2858+ $queries = [Query::equal('title', ['test'])];
2859+ static::getDatabase()->find($col->getId(), $queries);
2860+ } catch (Throwable $e) {
2861+ $this->fail('Should not have thrown error');
2862+ }
2863+ }
2864+
28172865 /**
28182866 * @depends testCreateDocument
28192867 */
0 commit comments