@@ -168,9 +168,12 @@ public function provideTests()
168168 */
169169 public function testDataKeyAndDoubleEncryption (Closure $ test )
170170 {
171- $ client = new Client (static ::getUri ());
171+ $ this ->setContext (Context::fromClientSideEncryption ((object ) [], 'db ' , 'coll ' ));
172+ $ client = $ this ->getContext ()->getClient ();
172173
173- $ client ->selectCollection ('keyvault ' , 'datakeys ' )->drop ();
174+ // This empty call ensures that the key vault is dropped with a majority
175+ // write concern
176+ $ this ->insertKeyVaultData ([]);
174177 $ client ->selectCollection ('db ' , 'coll ' )->drop ();
175178
176179 $ encryptionOpts = [
@@ -312,13 +315,17 @@ function ($command) use (&$commands) {
312315 */
313316 public function testExternalKeyVault ($ withExternalKeyVault )
314317 {
315- $ client = new Client (static ::getUri ());
316-
317- $ client ->selectCollection ('keyvault ' , 'datakeys ' )->drop ();
318+ $ this ->setContext (Context::fromClientSideEncryption ((object ) [], 'db ' , 'coll ' ));
319+ $ client = $ this ->getContext ()->getClient ();
318320 $ client ->selectCollection ('db ' , 'coll ' )->drop ();
319321
320- $ keyId = $ client
321- ->selectCollection ('keyvault ' , 'datakeys ' )
322+ $ keyVaultCollection = $ client ->selectCollection (
323+ 'keyvault ' ,
324+ 'datakeys ' ,
325+ ['writeConcern ' => new WriteConcern (WriteConcern::MAJORITY )] + $ this ->getContext ()->defaultWriteOptions
326+ );
327+ $ keyVaultCollection ->drop ();
328+ $ keyId = $ keyVaultCollection
322329 ->insertOne ($ this ->decodeJson (file_get_contents (__DIR__ . '/client-side-encryption/external/external-key.json ' )))
323330 ->getInsertedId ();
324331
@@ -370,13 +377,15 @@ public function testExternalKeyVault($withExternalKeyVault)
370377 */
371378 public function testBSONSizeLimitsAndBatchSplitting ()
372379 {
373- $ client = new Client (static ::getUri ());
380+ $ this ->setContext (Context::fromClientSideEncryption ((object ) [], 'db ' , 'coll ' ));
381+ $ client = $ this ->getContext ()->getClient ();
374382
375- $ client ->selectCollection ('keyvault ' , 'datakeys ' )->drop ();
376383 $ client ->selectCollection ('db ' , 'coll ' )->drop ();
377-
378384 $ client ->selectDatabase ('db ' )->createCollection ('coll ' , ['validator ' => ['$jsonSchema ' => $ this ->decodeJson (file_get_contents (__DIR__ . '/client-side-encryption/limits/limits-schema.json ' ))]]);
379- $ client ->selectCollection ('keyvault ' , 'datakeys ' )->insertOne ($ this ->decodeJson (file_get_contents (__DIR__ . '/client-side-encryption/limits/limits-key.json ' )));
385+
386+ $ this ->insertKeyVaultData ([
387+ $ this ->decodeJson (file_get_contents (__DIR__ . '/client-side-encryption/limits/limits-key.json ' )),
388+ ]);
380389
381390 $ autoEncryptionOpts = [
382391 'keyVaultNamespace ' => 'keyvault.datakeys ' ,
@@ -491,7 +500,8 @@ public function testViewsAreProhibited()
491500 */
492501 public function testCorpus ($ schemaMap = true )
493502 {
494- $ client = new Client (static ::getUri ());
503+ $ this ->setContext (Context::fromClientSideEncryption ((object ) [], 'db ' , 'coll ' ));
504+ $ client = $ this ->getContext ()->getClient ();
495505
496506 $ client ->selectDatabase ('db ' )->dropCollection ('coll ' );
497507
@@ -503,8 +513,7 @@ public function testCorpus($schemaMap = true)
503513 ->createCollection ('coll ' , ['validator ' => ['$jsonSchema ' => $ schema ]]);
504514 }
505515
506- $ client ->selectDatabase ('keyvault ' )->dropCollection ('datakeys ' );
507- $ client ->selectCollection ('keyvault ' , 'datakeys ' )->insertMany ([
516+ $ this ->insertKeyVaultData ([
508517 $ this ->decodeJson (file_get_contents (__DIR__ . '/client-side-encryption/corpus/corpus-key-local.json ' )),
509518 $ this ->decodeJson (file_get_contents (__DIR__ . '/client-side-encryption/corpus/corpus-key-aws.json ' )),
510519 ]);
@@ -738,16 +747,15 @@ private function encryptCorpusValue(stdClass $data, ClientEncryption $clientEncr
738747
739748 private function insertKeyVaultData (array $ keyVaultData = null )
740749 {
750+ $ context = $ this ->getContext ();
751+ $ collection = $ context ->selectCollection ('keyvault ' , 'datakeys ' , ['writeConcern ' => new WriteConcern (WriteConcern::MAJORITY )] + $ context ->defaultWriteOptions );
752+ $ collection ->drop ();
753+
741754 if (empty ($ keyVaultData )) {
742755 return ;
743756 }
744757
745- $ context = $ this ->getContext ();
746- $ collection = $ context ->selectCollection ('keyvault ' , 'datakeys ' , ['writeConcern ' => new WriteConcern (WriteConcern::MAJORITY )] + $ context ->defaultWriteOptions );
747- $ collection ->drop ();
748758 $ collection ->insertMany ($ keyVaultData );
749-
750- return ;
751759 }
752760
753761 private function prepareCorpusData (stdClass $ data , ClientEncryption $ clientEncryption )
0 commit comments