|
8 | 8 | */ |
9 | 9 | namespace OCA\Files_External\Tests\Storage; |
10 | 10 |
|
| 11 | +use OC\Files\Cache\Scanner; |
11 | 12 | use OCA\Files_External\Lib\Storage\AmazonS3; |
12 | 13 |
|
13 | 14 | /** |
@@ -38,7 +39,84 @@ protected function tearDown(): void { |
38 | 39 | parent::tearDown(); |
39 | 40 | } |
40 | 41 |
|
41 | | - public function testStat(): void { |
42 | | - $this->markTestSkipped('S3 doesn\'t update the parents folder mtime'); |
| 42 | + /** |
| 43 | + * Regression test for the '.' vs '' root path mismatch in getDirectoryMetaData. |
| 44 | + * |
| 45 | + * normalizePath('') returns '.' for S3 object keys, but the filecache stores the |
| 46 | + * storage root under the key ''. Before the fix, getCache()->get('.') returned false, |
| 47 | + * causing getDirectoryMetaData to return a fabricated time() on every call, which |
| 48 | + * made getCacheEntry always see a changed storage_mtime and fire propagateChange. |
| 49 | + */ |
| 50 | + public function testStatRootPreservesStorageMtimeFromCache(): void { |
| 51 | + $this->instance->getScanner()->scan('', Scanner::SCAN_SHALLOW); |
| 52 | + |
| 53 | + $cachedRoot = $this->instance->getCache()->get(''); |
| 54 | + $this->assertNotFalse($cachedRoot, 'Root entry must exist in cache after scan'); |
| 55 | + |
| 56 | + $cachedStorageMtime = $cachedRoot['storage_mtime']; |
| 57 | + |
| 58 | + $stat = $this->instance->stat(''); |
| 59 | + $this->assertNotFalse($stat, 'stat(\'\') must return data'); |
| 60 | + $this->assertEquals( |
| 61 | + $cachedStorageMtime, |
| 62 | + $stat['storage_mtime'], |
| 63 | + 'stat(\'\') must return storage_mtime from the cache entry, not a fabricated time()' |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Regression test for uncached S3 folders with a real directory marker object. |
| 69 | + * |
| 70 | + * mkdir() creates a `<path>/` object in S3. Before the fix, getDirectoryMetaData() |
| 71 | + * ignored that marker on a cache miss and returned time(), so simply reading the |
| 72 | + * folder could stamp it as "few seconds ago". stat() must use the marker metadata |
| 73 | + * instead, which stays stable across repeated reads. |
| 74 | + */ |
| 75 | + public function testStatDirectoryMarkerPreservesStorageMtimeWithoutCache(): void { |
| 76 | + $this->instance->mkdir('markerdir'); |
| 77 | + |
| 78 | + $firstStat = $this->instance->stat('markerdir'); |
| 79 | + $this->assertNotFalse($firstStat); |
| 80 | + |
| 81 | + sleep(2); |
| 82 | + |
| 83 | + $secondStat = $this->instance->stat('markerdir'); |
| 84 | + $this->assertNotFalse($secondStat); |
| 85 | + $this->assertEquals( |
| 86 | + $firstStat['storage_mtime'], |
| 87 | + $secondStat['storage_mtime'], |
| 88 | + 'stat() for an uncached S3 directory marker must not synthesize a fresh timestamp on every read' |
| 89 | + ); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Regression test for Common::getMetaData discarding storage_mtime for S3 directories. |
| 94 | + * |
| 95 | + * Common::getMetaData sets storage_mtime = mtime unconditionally. For S3 virtual |
| 96 | + * directories, mtime can be updated by mtime propagation while storage_mtime reflects |
| 97 | + * the actual last storage change. Without the AmazonS3::getMetaData override the |
| 98 | + * scanner would see a spurious storage_mtime change on every read, triggering |
| 99 | + * propagateChange and stamping every ancestor folder with the current timestamp. |
| 100 | + */ |
| 101 | + public function testGetMetaDataDirectoryPreservesStorageMtimeSeparateFromMtime(): void { |
| 102 | + $this->instance->mkdir('testmtimedir'); |
| 103 | + $this->instance->getScanner()->scan('testmtimedir', Scanner::SCAN_SHALLOW); |
| 104 | + |
| 105 | + $cachedEntry = $this->instance->getCache()->get('testmtimedir'); |
| 106 | + $this->assertNotFalse($cachedEntry); |
| 107 | + $originalStorageMtime = $cachedEntry['storage_mtime']; |
| 108 | + |
| 109 | + // Simulate what mtime propagation does: bump mtime without touching storage_mtime. |
| 110 | + // This mirrors the state after a child file is written and propagateChange fires. |
| 111 | + $bumpedMtime = $originalStorageMtime + 100; |
| 112 | + $this->instance->getCache()->put('testmtimedir', ['mtime' => $bumpedMtime]); |
| 113 | + |
| 114 | + $data = $this->instance->getMetaData('testmtimedir'); |
| 115 | + $this->assertNotNull($data); |
| 116 | + $this->assertEquals( |
| 117 | + $originalStorageMtime, |
| 118 | + $data['storage_mtime'], |
| 119 | + 'getMetaData for an S3 directory must return the cached storage_mtime, not the (possibly propagation-updated) mtime' |
| 120 | + ); |
43 | 121 | } |
44 | 122 | } |
0 commit comments