Skip to content

Commit 0e80d17

Browse files
authored
PHPLIB-1702: Ensure withOptions preserves autoEncryptionEnabled (#1747)
1 parent 75eb24a commit 0e80d17

File tree

5 files changed

+46
-0
lines changed

5 files changed

+46
-0
lines changed

src/Client.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ public function __construct(?string $uri = null, array $uriOptions = [], array $
136136
$this->uri = $uri ?? self::DEFAULT_URI;
137137
$this->builderEncoder = $driverOptions['builderEncoder'] ?? new BuilderEncoder();
138138
$this->typeMap = $driverOptions['typeMap'];
139+
140+
/* Database and Collection objects may need to know whether auto
141+
* encryption is enabled for dropping collections. Track this via an
142+
* internal option until PHPC-2615 is implemented. */
139143
$this->autoEncryptionEnabled = isset($driverOptions['autoEncryption']['keyVaultNamespace']);
140144

141145
$driverOptions = array_diff_key($driverOptions, ['builderEncoder' => 1, 'typeMap' => 1]);

src/Collection.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,6 +1145,7 @@ public function watch(array $pipeline = [], array $options = [])
11451145
public function withOptions(array $options = [])
11461146
{
11471147
$options += [
1148+
'autoEncryptionEnabled' => $this->autoEncryptionEnabled,
11481149
'builderEncoder' => $this->builderEncoder,
11491150
'codec' => $this->codec,
11501151
'readConcern' => $this->readConcern,

src/Database.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,7 @@ public function watch(array $pipeline = [], array $options = [])
689689
public function withOptions(array $options = [])
690690
{
691691
$options += [
692+
'autoEncryptionEnabled' => $this->autoEncryptionEnabled,
692693
'builderEncoder' => $this->builderEncoder,
693694
'readConcern' => $this->readConcern,
694695
'readPreference' => $this->readPreference,

tests/Collection/CollectionFunctionalTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use MongoDB\Tests\CommandObserver;
2121
use PHPUnit\Framework\Attributes\DataProvider;
2222
use PHPUnit\Framework\Attributes\Group;
23+
use ReflectionClass;
2324
use TypeError;
2425

2526
use function array_filter;
@@ -396,6 +397,16 @@ public function testWithOptionsInheritsOptions(): void
396397
foreach ($collectionOptions as $key => $value) {
397398
$this->assertSame($value, $debug[$key]);
398399
}
400+
401+
// autoEncryptionEnabled is an internal option not reported via debug info
402+
$collection = new Collection($this->manager, $this->getDatabaseName(), $this->getCollectionName(), ['autoEncryptionEnabled' => true]);
403+
$clone = $collection->withOptions();
404+
405+
$rc = new ReflectionClass($clone);
406+
$rp = $rc->getProperty('autoEncryptionEnabled');
407+
$rp->setAccessible(true);
408+
409+
$this->assertSame(true, $rp->getValue($clone));
399410
}
400411

401412
public function testWithOptionsPassesOptions(): void
@@ -415,6 +426,15 @@ public function testWithOptionsPassesOptions(): void
415426
foreach ($collectionOptions as $key => $value) {
416427
$this->assertSame($value, $debug[$key]);
417428
}
429+
430+
// autoEncryptionEnabled is an internal option not reported via debug info
431+
$clone = $this->collection->withOptions(['autoEncryptionEnabled' => true]);
432+
433+
$rc = new ReflectionClass($clone);
434+
$rp = $rc->getProperty('autoEncryptionEnabled');
435+
$rp->setAccessible(true);
436+
437+
$this->assertSame(true, $rp->getValue($clone));
418438
}
419439

420440
#[Group('matrix-testing-exclude-server-4.4-driver-4.0')]

tests/Database/DatabaseFunctionalTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use MongoDB\Operation\CreateIndexes;
1616
use PHPUnit\Framework\Attributes\DataProvider;
1717
use PHPUnit\Framework\Attributes\Group;
18+
use ReflectionClass;
1819
use TypeError;
1920

2021
use function array_key_exists;
@@ -389,6 +390,16 @@ public function testWithOptionsInheritsOptions(): void
389390
foreach ($databaseOptions as $key => $value) {
390391
$this->assertSame($value, $debug[$key]);
391392
}
393+
394+
// autoEncryptionEnabled is an internal option not reported via debug info
395+
$database = new Database($this->manager, $this->getDatabaseName(), ['autoEncryptionEnabled' => true]);
396+
$clone = $database->withOptions();
397+
398+
$rc = new ReflectionClass($clone);
399+
$rp = $rc->getProperty('autoEncryptionEnabled');
400+
$rp->setAccessible(true);
401+
402+
$this->assertSame(true, $rp->getValue($clone));
392403
}
393404

394405
public function testWithOptionsPassesOptions(): void
@@ -407,5 +418,14 @@ public function testWithOptionsPassesOptions(): void
407418
foreach ($databaseOptions as $key => $value) {
408419
$this->assertSame($value, $debug[$key]);
409420
}
421+
422+
// autoEncryptionEnabled is an internal option not reported via debug info
423+
$clone = $this->database->withOptions(['autoEncryptionEnabled' => true]);
424+
425+
$rc = new ReflectionClass($clone);
426+
$rp = $rc->getProperty('autoEncryptionEnabled');
427+
$rp->setAccessible(true);
428+
429+
$this->assertSame(true, $rp->getValue($clone));
410430
}
411431
}

0 commit comments

Comments
 (0)