|
| 1 | +--TEST-- |
| 2 | +Test for PHP-1464: GridFS should not drop dupes when creating index |
| 3 | +--SKIPIF-- |
| 4 | +<?php require_once "tests/utils/standalone.inc" ?> |
| 5 | +--FILE-- |
| 6 | +<?php |
| 7 | +require_once "tests/utils/server.inc"; |
| 8 | + |
| 9 | +$host = MongoShellServer::getStandaloneInfo(); |
| 10 | + |
| 11 | +$mc = new MongoClient($host); |
| 12 | +$db = $mc->selectDB(dbname()); |
| 13 | + |
| 14 | +$filesId = new MongoId('000000000000000000000002'); |
| 15 | +$data = new MongoBinData('foo', MongoBinData::BYTE_ARRAY); |
| 16 | + |
| 17 | +// Drop files collection |
| 18 | +$filesCollection = $db->selectCollection('fs.files'); |
| 19 | +$filesCollection->drop(); |
| 20 | + |
| 21 | +// Drop chunks collection and insert duplicate, orphan chunks |
| 22 | +$chunksCollection = $db->selectCollection('fs.chunks'); |
| 23 | +$chunksCollection->drop(); |
| 24 | +$chunksCollection->insert(array( |
| 25 | + '_id' => new MongoId('000000000000000000000002'), |
| 26 | + 'files_id' => new MongoId('000000000000000000000001'), |
| 27 | + 'n' => 0, |
| 28 | + 'data' => new MongoBinData('foo', MongoBinData::BYTE_ARRAY), |
| 29 | +)); |
| 30 | +$chunksCollection->insert(array( |
| 31 | + '_id' => new MongoId('000000000000000000000003'), |
| 32 | + 'files_id' => new MongoId('000000000000000000000001'), |
| 33 | + 'n' => 0, |
| 34 | + 'data' => new MongoBinData('bar', MongoBinData::BYTE_ARRAY), |
| 35 | +)); |
| 36 | + |
| 37 | +// Test three methods that start by ensuring the unique index |
| 38 | +echo "MongoGridFS::storeBytes():\n"; |
| 39 | + |
| 40 | +try { |
| 41 | + $db->getGridFS()->storeBytes('foo'); |
| 42 | +} catch (MongoGridFSException $e) { |
| 43 | + echo $e->getMessage(), "\n"; |
| 44 | +} |
| 45 | + |
| 46 | +echo "\nMongoGridFS::storeFile():\n"; |
| 47 | + |
| 48 | +try { |
| 49 | + $db->getGridFS()->storeFile(__FILE__); |
| 50 | +} catch (MongoGridFSException $e) { |
| 51 | + echo $e->getMessage(), "\n"; |
| 52 | +} |
| 53 | + |
| 54 | +echo "\nMongoGridFS::remove():\n"; |
| 55 | + |
| 56 | +try { |
| 57 | + $db->getGridFS()->remove(); |
| 58 | +} catch (MongoGridFSException $e) { |
| 59 | + echo $e->getMessage(), "\n"; |
| 60 | +} |
| 61 | + |
| 62 | +echo "\nDumping fs.files:\n"; |
| 63 | + |
| 64 | +foreach ($filesCollection->find() as $file) { |
| 65 | + var_dump($file); |
| 66 | +} |
| 67 | + |
| 68 | +echo "\nDumping fs.chunks:\n"; |
| 69 | + |
| 70 | +foreach ($chunksCollection->find() as $chunk) { |
| 71 | + var_dump($chunk); |
| 72 | +} |
| 73 | + |
| 74 | +?> |
| 75 | +==DONE== |
| 76 | +--CLEAN-- |
| 77 | +<?php |
| 78 | +require_once "tests/utils/server.inc"; |
| 79 | + |
| 80 | +// Ensure our duplicate chunks are removed |
| 81 | +$host = MongoShellServer::getStandaloneInfo(); |
| 82 | +$mc = new MongoClient($host); |
| 83 | +$mc->selectDB(dbname())->getGridFS()->drop(); |
| 84 | + |
| 85 | +?> |
| 86 | +--EXPECTF-- |
| 87 | +MongoGridFS::storeBytes(): |
| 88 | +Could not store file: %s:%d: %SE11000 duplicate key error index: %s.fs.chunks.$files_id_1_n_1%sdup key: { : ObjectId('000000000000000000000001'), : 0 } |
| 89 | + |
| 90 | +MongoGridFS::storeFile(): |
| 91 | +Could not store file: %s:%d: %SE11000 duplicate key error index: %s.fs.chunks.$files_id_1_n_1%sdup key: { : ObjectId('000000000000000000000001'), : 0 } |
| 92 | + |
| 93 | +MongoGridFS::remove(): |
| 94 | +Could not store file: %s:%d: %SE11000 duplicate key error index: %s.fs.chunks.$files_id_1_n_1%sdup key: { : ObjectId('000000000000000000000001'), : 0 } |
| 95 | + |
| 96 | +Dumping fs.files: |
| 97 | + |
| 98 | +Dumping fs.chunks: |
| 99 | +array(4) { |
| 100 | + ["_id"]=> |
| 101 | + object(MongoId)#%d (1) { |
| 102 | + ["$id"]=> |
| 103 | + string(24) "000000000000000000000002" |
| 104 | + } |
| 105 | + ["files_id"]=> |
| 106 | + object(MongoId)#%d (1) { |
| 107 | + ["$id"]=> |
| 108 | + string(24) "000000000000000000000001" |
| 109 | + } |
| 110 | + ["n"]=> |
| 111 | + int(0) |
| 112 | + ["data"]=> |
| 113 | + object(MongoBinData)#%d (2) { |
| 114 | + ["bin"]=> |
| 115 | + string(3) "foo" |
| 116 | + ["type"]=> |
| 117 | + int(2) |
| 118 | + } |
| 119 | +} |
| 120 | +array(4) { |
| 121 | + ["_id"]=> |
| 122 | + object(MongoId)#%d (1) { |
| 123 | + ["$id"]=> |
| 124 | + string(24) "000000000000000000000003" |
| 125 | + } |
| 126 | + ["files_id"]=> |
| 127 | + object(MongoId)#%d (1) { |
| 128 | + ["$id"]=> |
| 129 | + string(24) "000000000000000000000001" |
| 130 | + } |
| 131 | + ["n"]=> |
| 132 | + int(0) |
| 133 | + ["data"]=> |
| 134 | + object(MongoBinData)#%d (2) { |
| 135 | + ["bin"]=> |
| 136 | + string(3) "bar" |
| 137 | + ["type"]=> |
| 138 | + int(2) |
| 139 | + } |
| 140 | +} |
| 141 | +==DONE== |
0 commit comments