Skip to content

Commit 641cb75

Browse files
committed
Deprecate not setting disableMD5 to true when using GridFS
1 parent 8f562a7 commit 641cb75

File tree

4 files changed

+73
-11
lines changed

4 files changed

+73
-11
lines changed

UPGRADE-1.18.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# UPGRADE FROM 1.x to 1.18
2+
3+
## Required options
4+
5+
### GridFS disableMD5 option must be true
6+
7+
Starting with 1.18, not passing the `disableMD5` option to `Database::getGridFSBucket()`
8+
is deprecated. You should explicitly pass `true` to this option to disable
9+
checksum computation. This feature is deprecated in the GridFS specification.
10+
In the next major version, the default value will be `true` and an exception
11+
will be thrown if you set it to another value.

src/GridFS/Bucket.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,11 @@
5858
use function stream_copy_to_stream;
5959
use function stream_get_meta_data;
6060
use function stream_get_wrappers;
61+
use function trigger_error;
6162
use function urlencode;
6263

64+
use const E_USER_DEPRECATED;
65+
6366
/**
6467
* Bucket provides a public API for interacting with the GridFS files and chunks
6568
* collections.
@@ -112,7 +115,8 @@ class Bucket
112115
* 261120 (i.e. 255 KiB).
113116
*
114117
* * disableMD5 (boolean): When true, no MD5 sum will be generated for
115-
* each stored file. Defaults to "false".
118+
* each stored file. Defaults to "false", will be "true" in the next
119+
* major version.
116120
*
117121
* * readConcern (MongoDB\Driver\ReadConcern): Read concern.
118122
*
@@ -155,6 +159,10 @@ public function __construct(Manager $manager, string $databaseName, array $optio
155159
throw InvalidArgumentException::invalidType('"disableMD5" option', $options['disableMD5'], 'boolean');
156160
}
157161

162+
if (! $options['disableMD5']) {
163+
@trigger_error('Not setting "disableMD5" option to "true" is deprecated. Checksum is deprecated by GridFS spec and will be removed in mongodb/mongodb 2.0', E_USER_DEPRECATED);
164+
}
165+
158166
if (isset($options['readConcern']) && ! $options['readConcern'] instanceof ReadConcern) {
159167
throw InvalidArgumentException::invalidType('"readConcern" option', $options['readConcern'], ReadConcern::class);
160168
}

tests/GridFS/BucketFunctionalTest.php

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace MongoDB\Tests\GridFS;
44

5+
use Generator;
56
use MongoDB\BSON\Binary;
67
use MongoDB\Collection;
78
use MongoDB\Driver\ReadConcern;
@@ -19,6 +20,7 @@
1920
use MongoDB\Tests\Fixtures\Codec\TestFileCodec;
2021
use MongoDB\Tests\Fixtures\Document\TestFile;
2122
use stdClass;
23+
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
2224

2325
use function array_merge;
2426
use function call_user_func;
@@ -48,6 +50,8 @@
4850
*/
4951
class BucketFunctionalTest extends FunctionalTestCase
5052
{
53+
use ExpectDeprecationTrait;
54+
5155
/** @doesNotPerformAssertions */
5256
public function testValidConstructorOptions(): void
5357
{
@@ -86,14 +90,15 @@ public function testConstructorShouldRequireChunkSizeBytesOptionToBePositive():
8690
{
8791
$this->expectException(InvalidArgumentException::class);
8892
$this->expectExceptionMessage('Expected "chunkSizeBytes" option to be >= 1, 0 given');
89-
new Bucket($this->manager, $this->getDatabaseName(), ['chunkSizeBytes' => 0]);
93+
new Bucket($this->manager, $this->getDatabaseName(), ['chunkSizeBytes' => 0, 'disableMD5' => true]);
9094
}
9195

9296
public function testConstructorWithCodecAndTypeMapOptions(): void
9397
{
9498
$options = [
9599
'codec' => new TestDocumentCodec(),
96100
'typeMap' => ['root' => 'array', 'document' => 'array'],
101+
'disableMD5' => true,
97102
];
98103

99104
$this->expectExceptionObject(InvalidArgumentException::cannotCombineCodecAndTypeMap());
@@ -347,7 +352,10 @@ public function testFindUsesCodec(): void
347352

348353
public function testFindInheritsBucketCodec(): void
349354
{
350-
$bucket = new Bucket($this->manager, $this->getDatabaseName(), ['codec' => new TestFileCodec()]);
355+
$bucket = new Bucket($this->manager, $this->getDatabaseName(), [
356+
'codec' => new TestFileCodec(),
357+
'disableMD5' => true,
358+
]);
351359
$bucket->uploadFromStream('a', $this->createStream('foo'));
352360

353361
$cursor = $bucket->find();
@@ -359,7 +367,10 @@ public function testFindInheritsBucketCodec(): void
359367

360368
public function testFindResetsInheritedBucketCodec(): void
361369
{
362-
$bucket = new Bucket($this->manager, $this->getDatabaseName(), ['codec' => new TestFileCodec()]);
370+
$bucket = new Bucket($this->manager, $this->getDatabaseName(), [
371+
'codec' => new TestFileCodec(),
372+
'disableMD5' => true,
373+
]);
363374
$bucket->uploadFromStream('a', $this->createStream('foo'));
364375

365376
$cursor = $bucket->find([], ['codec' => null]);
@@ -412,7 +423,10 @@ public function testFindOneUsesCodec(): void
412423

413424
public function testFindOneInheritsBucketCodec(): void
414425
{
415-
$bucket = new Bucket($this->manager, $this->getDatabaseName(), ['codec' => new TestFileCodec()]);
426+
$bucket = new Bucket($this->manager, $this->getDatabaseName(), [
427+
'codec' => new TestFileCodec(),
428+
'disableMD5' => true,
429+
]);
416430

417431
$bucket->uploadFromStream('a', $this->createStream('foo'));
418432
$bucket->uploadFromStream('b', $this->createStream('foobar'));
@@ -430,7 +444,10 @@ public function testFindOneInheritsBucketCodec(): void
430444

431445
public function testFindOneResetsInheritedBucketCodec(): void
432446
{
433-
$bucket = new Bucket($this->manager, $this->getDatabaseName(), ['codec' => new TestFileCodec()]);
447+
$bucket = new Bucket($this->manager, $this->getDatabaseName(), [
448+
'codec' => new TestFileCodec(),
449+
'disableMD5' => true,
450+
]);
434451

435452
$bucket->uploadFromStream('a', $this->createStream('foo'));
436453
$bucket->uploadFromStream('b', $this->createStream('foobar'));
@@ -451,7 +468,10 @@ public function testFindOneResetsInheritedBucketCodec(): void
451468

452469
public function testGetBucketNameWithCustomValue(): void
453470
{
454-
$bucket = new Bucket($this->manager, $this->getDatabaseName(), ['bucketName' => 'custom_fs']);
471+
$bucket = new Bucket($this->manager, $this->getDatabaseName(), [
472+
'bucketName' => 'custom_fs',
473+
'disableMD5' => true,
474+
]);
455475

456476
$this->assertEquals('custom_fs', $bucket->getBucketName());
457477
}
@@ -471,7 +491,10 @@ public function testGetChunksCollection(): void
471491

472492
public function testGetChunkSizeBytesWithCustomValue(): void
473493
{
474-
$bucket = new Bucket($this->manager, $this->getDatabaseName(), ['chunkSizeBytes' => 8192]);
494+
$bucket = new Bucket($this->manager, $this->getDatabaseName(), [
495+
'chunkSizeBytes' => 8192,
496+
'disableMD5' => true,
497+
]);
475498

476499
$this->assertEquals(8192, $bucket->getChunkSizeBytes());
477500
}
@@ -500,7 +523,10 @@ public function testGetFileDocumentForStreamUsesTypeMap(): void
500523

501524
public function testGetFileDocumentForStreamUsesCodec(): void
502525
{
503-
$bucket = new Bucket($this->manager, $this->getDatabaseName(), ['codec' => new TestFileCodec()]);
526+
$bucket = new Bucket($this->manager, $this->getDatabaseName(), [
527+
'codec' => new TestFileCodec(),
528+
'disableMD5' => true,
529+
]);
504530

505531
$metadata = ['foo' => 'bar'];
506532
$stream = $bucket->openUploadStream('filename', ['_id' => 1, 'metadata' => $metadata]);
@@ -799,6 +825,23 @@ public function testDisableMD5OptionInConstructor(): void
799825
$this->assertArrayNotHasKey('md5', $fileDocument);
800826
}
801827

828+
/**
829+
* @dataProvider provideDeprecatedDisableMD5Options
830+
* @group legacy
831+
*/
832+
public function testNotDisablingMD5IsDeprecated(array $options): void
833+
{
834+
$this->expectDeprecation('Not setting "disableMD5" option to "true" is deprecated. Checksum is deprecated by GridFS spec and will be removed in mongodb/mongodb 2.0');
835+
836+
new Bucket($this->manager, $this->getDatabaseName(), $options);
837+
}
838+
839+
public static function provideDeprecatedDisableMD5Options(): Generator
840+
{
841+
yield 'set to false' => [['disableMD5' => false]];
842+
yield 'not set' => [[]];
843+
}
844+
802845
public function testUploadingFirstFileCreatesIndexes(): void
803846
{
804847
$this->bucket->uploadFromStream('filename', $this->createStream('foo'));
@@ -859,7 +902,7 @@ public function testDanglingOpenWritableStream(): void
859902
require '%s';
860903
$client = MongoDB\Tests\FunctionalTestCase::createTestClient();
861904
$database = $client->selectDatabase(getenv('MONGODB_DATABASE') ?: 'phplib_test');
862-
$gridfs = $database->selectGridFSBucket();
905+
$gridfs = $database->selectGridFSBucket(['disableMD5' => true]);
863906
$stream = $gridfs->openUploadStream('hello.txt', ['disableMD5' => true]);
864907
fwrite($stream, 'Hello MongoDB!');
865908
PHP;

tests/GridFS/FunctionalTestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function setUp(): void
2727
{
2828
parent::setUp();
2929

30-
$this->bucket = new Bucket($this->manager, $this->getDatabaseName());
30+
$this->bucket = new Bucket($this->manager, $this->getDatabaseName(), ['disableMD5' => true]);
3131
$this->bucket->drop();
3232

3333
$this->chunksCollection = $this->createCollection($this->getDatabaseName(), 'fs.chunks');

0 commit comments

Comments
 (0)