From e954006f4e83ea953f416507171373363bd394a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Wed, 14 Jun 2023 12:34:47 +0200 Subject: [PATCH 1/2] PHPLIB-1142: Support for the disableMD5 option of to the Bucket constructor for file uploads Bugfix, the option value set to the constructor was ignored. --- src/GridFS/Bucket.php | 6 +++++- tests/GridFS/BucketFunctionalTest.php | 14 ++++++++++++++ tests/GridFS/FunctionalTestCase.php | 4 ++-- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/GridFS/Bucket.php b/src/GridFS/Bucket.php index 3dba068e8..b58c7bda8 100644 --- a/src/GridFS/Bucket.php +++ b/src/GridFS/Bucket.php @@ -198,6 +198,7 @@ public function __debugInfo() return [ 'bucketName' => $this->bucketName, 'databaseName' => $this->databaseName, + 'disableMD5' => $this->disableMD5, 'manager' => $this->manager, 'chunkSizeBytes' => $this->chunkSizeBytes, 'readConcern' => $this->readConcern, @@ -547,7 +548,10 @@ public function openDownloadStreamByName(string $filename, array $options = []) */ public function openUploadStream(string $filename, array $options = []) { - $options += ['chunkSizeBytes' => $this->chunkSizeBytes]; + $options += [ + 'chunkSizeBytes' => $this->chunkSizeBytes, + 'disableMD5' => $this->disableMD5, + ]; $path = $this->createPathForUpload(); $context = stream_context_create([ diff --git a/tests/GridFS/BucketFunctionalTest.php b/tests/GridFS/BucketFunctionalTest.php index 395feaa95..3570612e8 100644 --- a/tests/GridFS/BucketFunctionalTest.php +++ b/tests/GridFS/BucketFunctionalTest.php @@ -53,6 +53,7 @@ public function testValidConstructorOptions(): void 'readConcern' => new ReadConcern(ReadConcern::LOCAL), 'readPreference' => new ReadPreference(ReadPreference::PRIMARY), 'writeConcern' => new WriteConcern(WriteConcern::MAJORITY, 1000), + 'disableMD5' => true, ]); } @@ -669,6 +670,19 @@ public function testUploadingAnEmptyFile(): void $this->assertSameDocument($expected, $fileDocument); } + public function testDisableMD5(): void + { + $options = ['disableMD5' => true]; + $id = $this->bucket->uploadFromStream('filename', $this->createStream('data'), $options); + $this->assertCollectionCount($this->filesCollection, 1); + + $fileDocument = $this->filesCollection->findOne( + ['_id' => $id] + ); + + $this->assertArrayNotHasKey('md5', $fileDocument); + } + public function testUploadingFirstFileCreatesIndexes(): void { $this->bucket->uploadFromStream('filename', $this->createStream('foo')); diff --git a/tests/GridFS/FunctionalTestCase.php b/tests/GridFS/FunctionalTestCase.php index d78b1e6b1..c22143f20 100644 --- a/tests/GridFS/FunctionalTestCase.php +++ b/tests/GridFS/FunctionalTestCase.php @@ -33,8 +33,8 @@ public function setUp(): void $this->bucket = new Bucket($this->manager, $this->getDatabaseName()); $this->bucket->drop(); - $this->chunksCollection = new Collection($this->manager, $this->getDatabaseName(), 'fs.chunks'); - $this->filesCollection = new Collection($this->manager, $this->getDatabaseName(), 'fs.files'); + $this->chunksCollection = $this->createCollection($this->getDatabaseName(), 'fs.chunks'); + $this->filesCollection = $this->createCollection($this->getDatabaseName(), 'fs.files'); } /** From 638de884607de7954647315ed4d887186cceedc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Wed, 14 Jun 2023 16:38:26 +0200 Subject: [PATCH 2/2] Add functional test on gridfs option passed to the constructor --- tests/GridFS/BucketFunctionalTest.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/tests/GridFS/BucketFunctionalTest.php b/tests/GridFS/BucketFunctionalTest.php index 3570612e8..27a22ff90 100644 --- a/tests/GridFS/BucketFunctionalTest.php +++ b/tests/GridFS/BucketFunctionalTest.php @@ -674,7 +674,20 @@ public function testDisableMD5(): void { $options = ['disableMD5' => true]; $id = $this->bucket->uploadFromStream('filename', $this->createStream('data'), $options); - $this->assertCollectionCount($this->filesCollection, 1); + + $fileDocument = $this->filesCollection->findOne( + ['_id' => $id] + ); + + $this->assertArrayNotHasKey('md5', $fileDocument); + } + + public function testDisableMD5OptionInConstructor(): void + { + $options = ['disableMD5' => true]; + + $this->bucket = new Bucket($this->manager, $this->getDatabaseName(), $options); + $id = $this->bucket->uploadFromStream('filename', $this->createStream('data')); $fileDocument = $this->filesCollection->findOne( ['_id' => $id]