diff --git a/src/Collection.php b/src/Collection.php index 04a61981c..aa105f85a 100644 --- a/src/Collection.php +++ b/src/Collection.php @@ -513,7 +513,7 @@ public function drop(array $options = []): void if (! isset($options['encryptedFields'])) { $options['encryptedFields'] = get_encrypted_fields_from_driver($this->databaseName, $this->collectionName, $this->manager) - ?? get_encrypted_fields_from_server($this->databaseName, $this->collectionName, $this->manager, $server); + ?? get_encrypted_fields_from_server($this->databaseName, $this->collectionName, $server); } $operation = isset($options['encryptedFields']) diff --git a/src/Database.php b/src/Database.php index 265772b53..edd2d2ea3 100644 --- a/src/Database.php +++ b/src/Database.php @@ -374,7 +374,7 @@ public function dropCollection(string $collectionName, array $options = []): voi if (! isset($options['encryptedFields'])) { $options['encryptedFields'] = get_encrypted_fields_from_driver($this->databaseName, $collectionName, $this->manager) - ?? get_encrypted_fields_from_server($this->databaseName, $collectionName, $this->manager, $server); + ?? get_encrypted_fields_from_server($this->databaseName, $collectionName, $server); } $operation = isset($options['encryptedFields']) diff --git a/src/functions.php b/src/functions.php index aca396723..d33760c81 100644 --- a/src/functions.php +++ b/src/functions.php @@ -198,13 +198,8 @@ function get_encrypted_fields_from_driver(string $databaseName, string $collecti * @see Collection::drop() * @see Database::dropCollection() */ -function get_encrypted_fields_from_server(string $databaseName, string $collectionName, Manager $manager, Server $server): array|object|null +function get_encrypted_fields_from_server(string $databaseName, string $collectionName, Server $server): array|object|null { - // No-op if the encryptedFieldsMap autoEncryption driver option was omitted - if ($manager->getEncryptedFieldsMap() === null) { - return null; - } - $collectionInfoIterator = (new ListCollections($databaseName, ['filter' => ['name' => $collectionName]]))->execute($server); foreach ($collectionInfoIterator as $collectionInfo) { diff --git a/tests/Collection/DropEncryptedCollectionFunctionalTest.php b/tests/Collection/DropEncryptedCollectionFunctionalTest.php new file mode 100644 index 000000000..d46b5e6e9 --- /dev/null +++ b/tests/Collection/DropEncryptedCollectionFunctionalTest.php @@ -0,0 +1,64 @@ +skipIfClientSideEncryptionIsNotSupported(); + + if ($this->isStandalone()) { + $this->markTestSkipped('Queryable encryption requires replica sets'); + } + + $this->skipIfServerVersion('<', '7.0.0', 'Queryable encryption requires MongoDB 7.0 or later'); + + $client = static::createTestClient(); + + // Ensure the key vault collection is dropped before each test + $collection = $client->selectCollection('keyvault', 'datakeys', ['writeConcern' => new WriteConcern(WriteConcern::MAJORITY)]); + $collection->drop(); + + $this->clientEncryption = $client->createClientEncryption([ + 'keyVaultNamespace' => 'keyvault.datakeys', + 'kmsProviders' => ['local' => ['key' => new Binary(str_repeat("\0", 96)) ]], + ]); + + $this->database = $client->getDatabase($this->getDatabaseName()); + } + + /** @see https://jira.mongodb.org/browse/PHPLIB-1702 */ + public function testDropConsultsEncryptedFieldsFromServer(): void + { + $originalNumCollections = iterator_count($this->database->listCollectionNames()); + + $this->database->createEncryptedCollection( + $this->getCollectionName(), + $this->clientEncryption, + 'local', + null, + ['encryptedFields' => ['fields' => []]], + ); + + // createEncryptedCollection should create three collections + $this->assertCount($originalNumCollections + 3, $this->database->listCollectionNames()); + + $this->collection->drop(); + + $this->assertCount($originalNumCollections, $this->database->listCollectionNames()); + } +} diff --git a/tests/Database/DropEncryptedCollectionFunctionalTest.php b/tests/Database/DropEncryptedCollectionFunctionalTest.php new file mode 100644 index 000000000..6d8ac3276 --- /dev/null +++ b/tests/Database/DropEncryptedCollectionFunctionalTest.php @@ -0,0 +1,60 @@ +skipIfClientSideEncryptionIsNotSupported(); + + if ($this->isStandalone()) { + $this->markTestSkipped('Queryable encryption requires replica sets'); + } + + $this->skipIfServerVersion('<', '7.0.0', 'Queryable encryption requires MongoDB 7.0 or later'); + + $client = static::createTestClient(); + + // Ensure the key vault collection is dropped before each test + $collection = $client->selectCollection('keyvault', 'datakeys', ['writeConcern' => new WriteConcern(WriteConcern::MAJORITY)]); + $collection->drop(); + + $this->clientEncryption = $client->createClientEncryption([ + 'keyVaultNamespace' => 'keyvault.datakeys', + 'kmsProviders' => ['local' => ['key' => new Binary(str_repeat("\0", 96)) ]], + ]); + } + + /** @see https://jira.mongodb.org/browse/PHPLIB-1702 */ + public function testDropCollectionConsultsEncryptedFieldsFromServer(): void + { + $originalNumCollections = iterator_count($this->database->listCollectionNames()); + + $this->database->createEncryptedCollection( + $this->getCollectionName(), + $this->clientEncryption, + 'local', + null, + ['encryptedFields' => ['fields' => []]], + ); + + // createEncryptedCollection should create three collections + $this->assertCount($originalNumCollections + 3, $this->database->listCollectionNames()); + + $this->database->dropCollection($this->getCollectionName()); + + $this->assertCount($originalNumCollections, $this->database->listCollectionNames()); + } +}