3
3
namespace MongoDB \Tests \Operation ;
4
4
5
5
use MongoDB \BSON \Binary ;
6
+ use MongoDB \BSON \Document ;
6
7
use MongoDB \Client ;
7
8
use MongoDB \ClientEncryption ;
8
9
use MongoDB \Driver \WriteConcern ;
10
+ use MongoDB \Model \BSONArray ;
11
+ use MongoDB \Model \BSONDocument ;
9
12
use MongoDB \Operation \CreateEncryptedCollection ;
10
13
11
14
use function base64_decode ;
@@ -61,61 +64,100 @@ public function setUp(): void
61
64
]);
62
65
}
63
66
64
- public function testCreateDataKeysNopIfFieldsArrayIsMissing (): void
67
+ /** @dataProvider provideEncryptedFieldsAndFieldsIsMissing */
68
+ public function testCreateDataKeysNopIfFieldsIsMissing ($ input , array $ expectedOutput ): void
65
69
{
66
70
$ operation = new CreateEncryptedCollection (
67
71
$ this ->getDatabaseName (),
68
72
$ this ->getCollectionName (),
69
- ['encryptedFields ' => [] ]
73
+ ['encryptedFields ' => $ input ]
70
74
);
71
75
72
76
$ operation ->createDataKeys (
73
77
$ this ->clientEncryption ,
74
78
'local ' ,
75
79
null ,
76
- $ encryptedFields
80
+ $ encryptedFieldsOutput
77
81
);
78
82
79
- $ this ->assertSame ([] , $ encryptedFields );
83
+ $ this ->assertSame ($ expectedOutput , $ encryptedFieldsOutput );
80
84
}
81
85
82
- public function testCreateDataKeysNopIfFieldsArrayIsInvalid (): void
86
+ public function provideEncryptedFieldsAndFieldsIsMissing (): array
87
+ {
88
+ $ ef = [];
89
+
90
+ return [
91
+ 'array ' => [$ ef , $ ef ],
92
+ 'object ' => [(object ) $ ef , $ ef ],
93
+ 'Serializable ' => [new BSONDocument ($ ef ), $ ef ],
94
+ 'Document ' => [Document::fromPHP ($ ef ), $ ef ],
95
+ ];
96
+ }
97
+
98
+ /** @dataProvider provideEncryptedFieldsAndFieldsHasInvalidType */
99
+ public function testCreateDataKeysNopIfFieldsHasInvalidType ($ input , array $ expectedOutput ): void
83
100
{
84
101
$ operation = new CreateEncryptedCollection (
85
102
$ this ->getDatabaseName (),
86
103
$ this ->getCollectionName (),
87
- ['encryptedFields ' => [ ' fields ' => ' not-an-array ' ] ]
104
+ ['encryptedFields ' => $ input ]
88
105
);
89
106
90
107
$ operation ->createDataKeys (
91
108
$ this ->clientEncryption ,
92
109
'local ' ,
93
110
null ,
94
- $ encryptedFields
111
+ $ encryptedFieldsOutput
95
112
);
96
113
97
- $ this ->assertSame ([ ' fields ' => ' not-an-array ' ] , $ encryptedFields );
114
+ $ this ->assertSame ($ expectedOutput , $ encryptedFieldsOutput );
98
115
}
99
116
100
- public function testCreateDataKeysSkipsNonDocumentFields (): void
117
+ public function provideEncryptedFieldsAndFieldsHasInvalidType (): array
118
+ {
119
+ $ ef = ['fields ' => 'not-an-array ' ];
120
+
121
+ return [
122
+ 'array ' => [$ ef , $ ef ],
123
+ 'object ' => [(object ) $ ef , $ ef ],
124
+ 'Serializable ' => [new BSONDocument ($ ef ), $ ef ],
125
+ 'Document ' => [Document::fromPHP ($ ef ), $ ef ],
126
+ ];
127
+ }
128
+
129
+ /** @dataProvider provideEncryptedFieldsElementHasInvalidType */
130
+ public function testCreateDataKeysSkipsNonDocumentFields ($ input , array $ expectedOutput ): void
101
131
{
102
132
$ operation = new CreateEncryptedCollection (
103
133
$ this ->getDatabaseName (),
104
134
$ this ->getCollectionName (),
105
- ['encryptedFields ' => [ ' fields ' => [ ' not-an-array-or-object ' ]]]
135
+ ['encryptedFields ' => $ input ],
106
136
);
107
137
108
138
$ operation ->createDataKeys (
109
139
$ this ->clientEncryption ,
110
140
'local ' ,
111
141
null ,
112
- $ encryptedFields
142
+ $ encryptedFieldsOutput
113
143
);
114
144
115
- $ this ->assertSame (['fields ' => ['not-an-array-or-object ' ]], $ encryptedFields );
145
+ $ this ->assertSame ($ expectedOutput , $ encryptedFieldsOutput );
146
+ }
147
+
148
+ public function provideEncryptedFieldsElementHasInvalidType (): array
149
+ {
150
+ $ ef = ['fields ' => ['not-an-array-or-object ' ]];
151
+
152
+ return [
153
+ 'array ' => [$ ef , $ ef ],
154
+ 'object ' => [(object ) $ ef , $ ef ],
155
+ 'Serializable ' => [new BSONDocument (['fields ' => new BSONArray (['not-an-array-or-object ' ])]), $ ef ],
156
+ 'Document ' => [Document::fromPHP ($ ef ), $ ef ],
157
+ ];
116
158
}
117
159
118
- public function testCreateDataKeysDoesNotModifyEncryptedFieldsObjectOption (): void
160
+ public function testCreateDataKeysDoesNotModifyOriginalEncryptedFieldsOption (): void
119
161
{
120
162
$ originalField = (object ) ['path ' => 'ssn ' , 'bsonType ' => 'string ' , 'keyId ' => null ];
121
163
$ originalEncryptedFields = (object ) ['fields ' => [$ originalField ]];
@@ -139,6 +181,37 @@ public function testCreateDataKeysDoesNotModifyEncryptedFieldsObjectOption(): vo
139
181
$ this ->assertInstanceOf (Binary::class, $ modifiedEncryptedFields ['fields ' ][0 ]['keyId ' ] ?? null );
140
182
}
141
183
184
+ /** @dataProvider provideEncryptedFields */
185
+ public function testEncryptedFieldsDocuments ($ input ): void
186
+ {
187
+ $ operation = new CreateEncryptedCollection (
188
+ $ this ->getDatabaseName (),
189
+ $ this ->getCollectionName (),
190
+ ['encryptedFields ' => $ input ]
191
+ );
192
+
193
+ $ operation ->createDataKeys (
194
+ $ this ->clientEncryption ,
195
+ 'local ' ,
196
+ null ,
197
+ $ modifiedEncryptedFields
198
+ );
199
+
200
+ $ this ->assertInstanceOf (Binary::class, $ modifiedEncryptedFields ['fields ' ][0 ]['keyId ' ] ?? null );
201
+ }
202
+
203
+ public function provideEncryptedFields (): array
204
+ {
205
+ $ ef = ['fields ' => [['path ' => 'ssn ' , 'bsonType ' => 'string ' , 'keyId ' => null ]]];
206
+
207
+ return [
208
+ 'array ' => [$ ef ],
209
+ 'object ' => [(object ) $ ef ],
210
+ 'Serializable ' => [new BSONDocument (['fields ' => new BSONArray ([new BSONDocument ($ ef ['fields ' ][0 ])])])],
211
+ 'Document ' => [Document::fromPHP ($ ef )],
212
+ ];
213
+ }
214
+
142
215
public static function createTestClient (?string $ uri = null , array $ options = [], array $ driverOptions = []): Client
143
216
{
144
217
if (isset ($ driverOptions ['autoEncryption ' ]) && getenv ('CRYPT_SHARED_LIB_PATH ' )) {
0 commit comments