diff --git a/src/Client.php b/src/Client.php index 159f0caf2..72ffaf6a6 100644 --- a/src/Client.php +++ b/src/Client.php @@ -135,6 +135,10 @@ public function __construct(?string $uri = null, array $uriOptions = [], array $ $this->uri = $uri ?? self::DEFAULT_URI; $this->builderEncoder = $driverOptions['builderEncoder'] ?? new BuilderEncoder(); $this->typeMap = $driverOptions['typeMap']; + + /* Database and Collection objects may need to know whether auto + * encryption is enabled for dropping collections. Track this via an + * internal option until PHPC-2615 is implemented. */ $this->autoEncryptionEnabled = isset($driverOptions['autoEncryption']['keyVaultNamespace']); $driverOptions = array_diff_key($driverOptions, ['builderEncoder' => 1, 'typeMap' => 1]); diff --git a/src/Collection.php b/src/Collection.php index 1b51efe66..1253f0f32 100644 --- a/src/Collection.php +++ b/src/Collection.php @@ -1063,6 +1063,7 @@ public function watch(array|Pipeline $pipeline = [], array $options = []): Chang public function withOptions(array $options = []): Collection { $options += [ + 'autoEncryptionEnabled' => $this->autoEncryptionEnabled, 'builderEncoder' => $this->builderEncoder, 'codec' => $this->codec, 'readConcern' => $this->readConcern, diff --git a/src/Database.php b/src/Database.php index aba7c03a5..ea5dd39cc 100644 --- a/src/Database.php +++ b/src/Database.php @@ -632,6 +632,7 @@ public function watch(array|Pipeline $pipeline = [], array $options = []): Chang public function withOptions(array $options = []): Database { $options += [ + 'autoEncryptionEnabled' => $this->autoEncryptionEnabled, 'builderEncoder' => $this->builderEncoder, 'readConcern' => $this->readConcern, 'readPreference' => $this->readPreference, diff --git a/tests/Collection/CollectionFunctionalTest.php b/tests/Collection/CollectionFunctionalTest.php index 7172e915c..a4ee7b148 100644 --- a/tests/Collection/CollectionFunctionalTest.php +++ b/tests/Collection/CollectionFunctionalTest.php @@ -17,6 +17,7 @@ use MongoDB\Operation\Count; use MongoDB\Tests\CommandObserver; use PHPUnit\Framework\Attributes\DataProvider; +use ReflectionClass; use TypeError; use function array_filter; @@ -405,6 +406,16 @@ public function testWithOptionsInheritsOptions(): void foreach ($collectionOptions as $key => $value) { $this->assertSame($value, $debug[$key]); } + + // autoEncryptionEnabled is an internal option not reported via debug info + $collection = new Collection($this->manager, $this->getDatabaseName(), $this->getCollectionName(), ['autoEncryptionEnabled' => true]); + $clone = $collection->withOptions(); + + $rc = new ReflectionClass($clone); + $rp = $rc->getProperty('autoEncryptionEnabled'); + $rp->setAccessible(true); + + $this->assertSame(true, $rp->getValue($clone)); } public function testWithOptionsPassesOptions(): void @@ -424,6 +435,15 @@ public function testWithOptionsPassesOptions(): void foreach ($collectionOptions as $key => $value) { $this->assertSame($value, $debug[$key]); } + + // autoEncryptionEnabled is an internal option not reported via debug info + $clone = $this->collection->withOptions(['autoEncryptionEnabled' => true]); + + $rc = new ReflectionClass($clone); + $rp = $rc->getProperty('autoEncryptionEnabled'); + $rp->setAccessible(true); + + $this->assertSame(true, $rp->getValue($clone)); } public static function collectionMethodClosures() diff --git a/tests/Database/DatabaseFunctionalTest.php b/tests/Database/DatabaseFunctionalTest.php index 7929f0350..f2e1e86f0 100644 --- a/tests/Database/DatabaseFunctionalTest.php +++ b/tests/Database/DatabaseFunctionalTest.php @@ -15,6 +15,7 @@ use MongoDB\Operation\CreateIndexes; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Group; +use ReflectionClass; use TypeError; use function array_key_exists; @@ -385,6 +386,16 @@ public function testWithOptionsInheritsOptions(): void foreach ($databaseOptions as $key => $value) { $this->assertSame($value, $debug[$key]); } + + // autoEncryptionEnabled is an internal option not reported via debug info + $database = new Database($this->manager, $this->getDatabaseName(), ['autoEncryptionEnabled' => true]); + $clone = $database->withOptions(); + + $rc = new ReflectionClass($clone); + $rp = $rc->getProperty('autoEncryptionEnabled'); + $rp->setAccessible(true); + + $this->assertSame(true, $rp->getValue($clone)); } public function testWithOptionsPassesOptions(): void @@ -403,5 +414,14 @@ public function testWithOptionsPassesOptions(): void foreach ($databaseOptions as $key => $value) { $this->assertSame($value, $debug[$key]); } + + // autoEncryptionEnabled is an internal option not reported via debug info + $clone = $this->database->withOptions(['autoEncryptionEnabled' => true]); + + $rc = new ReflectionClass($clone); + $rp = $rc->getProperty('autoEncryptionEnabled'); + $rp->setAccessible(true); + + $this->assertSame(true, $rp->getValue($clone)); } }