|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace MongoDB\Tests\Functions; |
| 4 | + |
| 5 | +use MongoDB\BSON\Binary; |
| 6 | +use MongoDB\Collection; |
| 7 | +use MongoDB\Database; |
| 8 | +use MongoDB\Driver\ClientEncryption; |
| 9 | +use MongoDB\Driver\WriteConcern; |
| 10 | +use MongoDB\Tests\FunctionalTestCase; |
| 11 | + |
| 12 | +use function iterator_count; |
| 13 | +use function str_repeat; |
| 14 | + |
| 15 | +class GetEncryptedFieldsFromServerFunctionalTest extends FunctionalTestCase |
| 16 | +{ |
| 17 | + private ClientEncryption $clientEncryption; |
| 18 | + private Collection $keyVaultCollection; |
| 19 | + private Database $database; |
| 20 | + |
| 21 | + public function setUp(): void |
| 22 | + { |
| 23 | + parent::setUp(); |
| 24 | + |
| 25 | + $this->skipIfClientSideEncryptionIsNotSupported(); |
| 26 | + |
| 27 | + if ($this->isStandalone()) { |
| 28 | + $this->markTestSkipped('Queryable encryption requires replica sets'); |
| 29 | + } |
| 30 | + |
| 31 | + $this->skipIfServerVersion('<', '7.0.0', 'Queryable encryption requires MongoDB 7.0 or later'); |
| 32 | + |
| 33 | + $client = static::createTestClient(); |
| 34 | + |
| 35 | + // Ensure the key vault collection is dropped before each test |
| 36 | + $this->keyVaultCollection = $client->getCollection('keyvault', 'datakeys', ['writeConcern' => new WriteConcern(WriteConcern::MAJORITY)]); |
| 37 | + $this->keyVaultCollection->drop(); |
| 38 | + |
| 39 | + $this->clientEncryption = $client->createClientEncryption([ |
| 40 | + 'keyVaultNamespace' => $this->keyVaultCollection->getNamespace(), |
| 41 | + 'kmsProviders' => ['local' => ['key' => new Binary(str_repeat("\0", 96)) ]], |
| 42 | + ]); |
| 43 | + |
| 44 | + $this->database = $client->getDatabase($this->getDatabaseName()); |
| 45 | + } |
| 46 | + |
| 47 | + public function tearDown(): void |
| 48 | + { |
| 49 | + $this->keyVaultCollection->drop(); |
| 50 | + } |
| 51 | + |
| 52 | + /** @see https://jira.mongodb.org/browse/PHPLIB-1702 */ |
| 53 | + public function testDatabaseDropCollectionConsultsEncryptedFieldsFromServer(): void |
| 54 | + { |
| 55 | + $originalNumCollections = iterator_count($this->database->listCollectionNames()); |
| 56 | + |
| 57 | + $this->database->createEncryptedCollection( |
| 58 | + $this->getCollectionName(), |
| 59 | + $this->clientEncryption, |
| 60 | + 'local', |
| 61 | + null, |
| 62 | + ['encryptedFields' => ['fields' => []]], |
| 63 | + ); |
| 64 | + |
| 65 | + // createEncryptedCollection should create three collections |
| 66 | + $this->assertCount($originalNumCollections + 3, $this->database->listCollectionNames()); |
| 67 | + |
| 68 | + $this->database->dropCollection($this->getCollectionName()); |
| 69 | + |
| 70 | + $this->assertCount($originalNumCollections, $this->database->listCollectionNames()); |
| 71 | + } |
| 72 | + |
| 73 | + /** @see https://jira.mongodb.org/browse/PHPLIB-1702 */ |
| 74 | + public function testCollectionDropConsultsEncryptedFieldsFromServer(): void |
| 75 | + { |
| 76 | + $originalNumCollections = iterator_count($this->database->listCollectionNames()); |
| 77 | + |
| 78 | + $this->database->createEncryptedCollection( |
| 79 | + $this->getCollectionName(), |
| 80 | + $this->clientEncryption, |
| 81 | + 'local', |
| 82 | + null, |
| 83 | + ['encryptedFields' => ['fields' => []]], |
| 84 | + ); |
| 85 | + |
| 86 | + // createEncryptedCollection should create three collections |
| 87 | + $this->assertCount($originalNumCollections + 3, $this->database->listCollectionNames()); |
| 88 | + |
| 89 | + $this->database->getCollection($this->getCollectionName())->drop(); |
| 90 | + |
| 91 | + $this->assertCount($originalNumCollections, $this->database->listCollectionNames()); |
| 92 | + } |
| 93 | +} |
0 commit comments