15
15
use MongoDB \Driver \Exception \EncryptionException ;
16
16
use MongoDB \Driver \Exception \RuntimeException ;
17
17
use MongoDB \Driver \WriteConcern ;
18
- use MongoDB \Operation \CreateCollection ;
19
18
use MongoDB \Tests \CommandObserver ;
20
19
use PHPUnit \Framework \Assert ;
21
20
use PHPUnit \Framework \SkippedTestError ;
@@ -57,6 +56,8 @@ class ClientSideEncryptionSpecTest extends FunctionalTestCase
57
56
'awsTemporary: Insert a document with auto encryption using the AWS provider with temporary credentials ' => 'Not yet implemented (PHPC-1751) ' ,
58
57
'awsTemporary: Insert with invalid temporary credentials ' => 'Not yet implemented (PHPC-1751) ' ,
59
58
'azureKMS: Insert a document with auto encryption using Azure KMS provider ' => 'RHEL platform is missing Azure root certificate (PHPLIB-619) ' ,
59
+ 'timeoutMS: timeoutMS applied to listCollections to get collection schema ' => 'Not yet implemented (PHPC-1760) ' ,
60
+ 'timeoutMS: remaining timeoutMS applied to find to get keyvault data ' => 'Not yet implemented (PHPC-1760) ' ,
60
61
];
61
62
62
63
public function setUp (): void
@@ -90,7 +91,7 @@ public static function assertCommandMatches(stdClass $expected, stdClass $actual
90
91
* @param string $databaseName Name of database under test
91
92
* @param string $collectionName Name of collection under test
92
93
*/
93
- public function testClientSideEncryption (stdClass $ test , ?array $ runOn , array $ data , ?array $ keyVaultData = null , $ jsonSchema = null , ?string $ databaseName = null , ?string $ collectionName = null ): void
94
+ public function testClientSideEncryption (stdClass $ test , ?array $ runOn , array $ data , ?stdClass $ encryptedFields = null , ? array $ keyVaultData = null , ? stdClass $ jsonSchema = null , ?string $ databaseName = null , ?string $ collectionName = null ): void
94
95
{
95
96
if (isset (self ::$ incompleteTests [$ this ->dataDescription ()])) {
96
97
$ this ->markTestIncomplete (self ::$ incompleteTests [$ this ->dataDescription ()]);
@@ -107,6 +108,11 @@ public function testClientSideEncryption(stdClass $test, ?array $runOn, array $d
107
108
$ databaseName = $ databaseName ?? $ this ->getDatabaseName ();
108
109
$ collectionName = $ collectionName ?? $ this ->getCollectionName ();
109
110
111
+ // TODO: Remove this once SERVER-66901 is implemented (see: PHPLIB-884)
112
+ if (isset ($ test ->clientOptions ->autoEncryptOpts ->encryptedFieldsMap )) {
113
+ $ test ->clientOptions ->autoEncryptOpts ->encryptedFieldsMap = $ this ->prepareEncryptedFieldsMap ($ test ->clientOptions ->autoEncryptOpts ->encryptedFieldsMap );
114
+ }
115
+
110
116
try {
111
117
$ context = Context::fromClientSideEncryption ($ test , $ databaseName , $ collectionName );
112
118
} catch (SkippedTestError $ e ) {
@@ -116,15 +122,15 @@ public function testClientSideEncryption(stdClass $test, ?array $runOn, array $d
116
122
$ this ->setContext ($ context );
117
123
118
124
self ::insertKeyVaultData ($ context ->getClient (), $ keyVaultData );
119
- $ this ->dropTestAndOutcomeCollections ();
120
- $ this ->createTestCollection ($ jsonSchema );
125
+ $ this ->dropTestAndOutcomeCollections (empty ( $ encryptedFields ) ? [] : [ ' encryptedFields ' => $ encryptedFields ] );
126
+ $ this ->createTestCollection ($ encryptedFields , $ jsonSchema );
121
127
$ this ->insertDataFixtures ($ data );
122
128
123
129
if (isset ($ test ->failPoint )) {
124
130
$ this ->configureFailPoint ($ test ->failPoint );
125
131
}
126
132
127
- $ context ->enableEncryption () ;
133
+ $ context ->useEncryptedClientIfConfigured = true ;
128
134
129
135
if (isset ($ test ->expectations )) {
130
136
$ commandExpectations = CommandExpectations::fromClientSideEncryption ($ context ->getClient (), $ test ->expectations );
@@ -140,7 +146,7 @@ public function testClientSideEncryption(stdClass $test, ?array $runOn, array $d
140
146
$ commandExpectations ->assert ($ this , $ context );
141
147
}
142
148
143
- $ context ->disableEncryption () ;
149
+ $ context ->useEncryptedClientIfConfigured = false ;
144
150
145
151
if (isset ($ test ->outcome ->collection ->data )) {
146
152
$ this ->assertOutcomeCollectionData ($ test ->outcome ->collection ->data , ResultExpectation::ASSERT_DOCUMENTS_MATCH );
@@ -169,6 +175,7 @@ public function provideTests()
169
175
$ runOn = $ json ->runOn ?? null ;
170
176
$ data = $ json ->data ?? [];
171
177
// phpcs:disable Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
178
+ $ encryptedFields = $ json ->encrypted_fields ?? null ;
172
179
$ keyVaultData = $ json ->key_vault_data ?? null ;
173
180
$ jsonSchema = $ json ->json_schema ?? null ;
174
181
$ databaseName = $ json ->database_name ?? null ;
@@ -177,7 +184,7 @@ public function provideTests()
177
184
178
185
foreach ($ json ->tests as $ test ) {
179
186
$ name = $ group . ': ' . $ test ->description ;
180
- $ testArgs [$ name ] = [$ test , $ runOn , $ data , $ keyVaultData , $ jsonSchema , $ databaseName , $ collectionName ];
187
+ $ testArgs [$ name ] = [$ test , $ runOn , $ data , $ encryptedFields , $ keyVaultData , $ jsonSchema , $ databaseName , $ collectionName ];
181
188
}
182
189
}
183
190
@@ -1331,11 +1338,20 @@ private function createInt64(string $value): Int64
1331
1338
return unserialize ($ int64 );
1332
1339
}
1333
1340
1334
- private function createTestCollection ($ jsonSchema ): void
1341
+ private function createTestCollection (? stdClass $ encryptedFields = null , ? stdClass $ jsonSchema = null ): void
1335
1342
{
1336
- $ options = empty ($ jsonSchema ) ? [] : ['validator ' => ['$jsonSchema ' => $ jsonSchema ]];
1337
- $ operation = new CreateCollection ($ this ->getContext ()->databaseName , $ this ->getContext ()->collectionName , $ options );
1338
- $ operation ->execute ($ this ->getPrimaryServer ());
1343
+ $ context = $ this ->getContext ();
1344
+ $ options = $ context ->defaultWriteOptions ;
1345
+
1346
+ if (! empty ($ encryptedFields )) {
1347
+ $ options ['encryptedFields ' ] = $ this ->prepareEncryptedFields ($ encryptedFields );
1348
+ }
1349
+
1350
+ if (! empty ($ jsonSchema )) {
1351
+ $ options ['validator ' ] = ['$jsonSchema ' => $ jsonSchema ];
1352
+ }
1353
+
1354
+ $ context ->getDatabase ()->createCollection ($ context ->collectionName , $ options );
1339
1355
}
1340
1356
1341
1357
private function encryptCorpusValue (string $ fieldName , stdClass $ data , ClientEncryption $ clientEncryption )
@@ -1466,6 +1482,19 @@ private function prepareEncryptedFields(stdClass $encryptedFields): stdClass
1466
1482
return $ encryptedFields ;
1467
1483
}
1468
1484
1485
+ /**
1486
+ * @todo Remove this once SERVER-66901 is implemented
1487
+ * @see https://jira.mongodb.org/browse/PHPLIB-884
1488
+ */
1489
+ private function prepareEncryptedFieldsMap (stdClass $ encryptedFieldsMap ): stdClass
1490
+ {
1491
+ foreach ($ encryptedFieldsMap as $ namespace => $ encryptedFields ) {
1492
+ $ encryptedFieldsMap ->{$ namespace } = $ this ->prepareEncryptedFields ($ encryptedFields );
1493
+ }
1494
+
1495
+ return $ encryptedFieldsMap ;
1496
+ }
1497
+
1469
1498
private function skipIfLocalMongocryptdIsUnavailable (): void
1470
1499
{
1471
1500
$ paths = explode (PATH_SEPARATOR , getenv ("PATH " ));
0 commit comments