|
14 | 14 | use Magento\Catalog\Model\View\Asset\PlaceholderFactory;
|
15 | 15 | use Magento\Framework\App\CacheInterface;
|
16 | 16 | use Magento\Framework\App\Filesystem\DirectoryList;
|
| 17 | +use Magento\Framework\Exception\FileSystemException; |
17 | 18 | use Magento\Framework\Filesystem;
|
18 | 19 | use Magento\Framework\Filesystem\Directory\Write;
|
19 | 20 | use Magento\Framework\Filesystem\Directory\WriteInterface;
|
20 |
| -use Magento\Framework\Filesystem\DriverInterface; |
21 | 21 | use Magento\Framework\Image\Factory;
|
22 | 22 | use Magento\Framework\Model\Context;
|
23 | 23 | use Magento\Framework\Serialize\SerializerInterface;
|
@@ -141,7 +141,7 @@ protected function setUp(): void
|
141 | 141 |
|
142 | 142 | $this->mediaDirectory = $this->getMockBuilder(Write::class)
|
143 | 143 | ->disableOriginalConstructor()
|
144 |
| - ->onlyMethods(['create', 'isFile', 'isExist', 'getAbsolutePath', 'isDirectory', 'getDriver', 'delete']) |
| 144 | + ->onlyMethods(['create', 'isFile', 'isExist', 'getAbsolutePath', 'delete']) |
145 | 145 | ->getMock();
|
146 | 146 |
|
147 | 147 | $this->filesystem = $this->createMock(Filesystem::class);
|
@@ -504,54 +504,35 @@ public function testIsCached(): void
|
504 | 504 | }
|
505 | 505 |
|
506 | 506 | /**
|
507 |
| - * @param bool $isRenameSuccessful |
508 |
| - * @param string $expectedDirectoryToDelete |
509 | 507 | * @return void
|
510 |
| - * @throws \Magento\Framework\Exception\FileSystemException |
511 |
| - * @dataProvider clearCacheDataProvider |
512 |
| - */ |
513 |
| - public function testClearCache( |
514 |
| - bool $isRenameSuccessful, |
515 |
| - string $expectedDirectoryToDelete |
516 |
| - ): void { |
517 |
| - $driver = $this->createMock(DriverInterface::class); |
518 |
| - $this->mediaDirectory->method('getAbsolutePath') |
519 |
| - ->willReturnCallback( |
520 |
| - function (string $path) { |
521 |
| - return 'path/to/media/' . $path; |
522 |
| - } |
523 |
| - ); |
524 |
| - $this->mediaDirectory->expects($this->exactly(2)) |
525 |
| - ->method('isDirectory') |
526 |
| - ->willReturnOnConsecutiveCalls(false, true); |
527 |
| - $this->mediaDirectory->expects($this->once()) |
528 |
| - ->method('getDriver') |
529 |
| - ->willReturn($driver); |
530 |
| - $driver->expects($this->once()) |
531 |
| - ->method('rename') |
532 |
| - ->with( |
533 |
| - 'path/to/media/catalog/product/cache', |
534 |
| - $this->matchesRegularExpression('/^path\/to\/media\/catalog\/product\/\.[0-9A-ZA-z-_]{3}$/') |
535 |
| - ) |
536 |
| - ->willReturn($isRenameSuccessful); |
537 |
| - $this->mediaDirectory->expects($this->once()) |
538 |
| - ->method('delete') |
539 |
| - ->with($this->matchesRegularExpression($expectedDirectoryToDelete)); |
540 |
| - |
| 508 | + * @throws FileSystemException |
| 509 | + */ |
| 510 | + public function testClearCache(): void |
| 511 | + { |
541 | 512 | $this->coreFileHelper->expects($this->once())->method('deleteFolder')->willReturn(true);
|
542 | 513 | $this->cacheManager->expects($this->once())->method('clean');
|
543 | 514 | $this->image->clearCache();
|
544 | 515 | }
|
545 | 516 |
|
546 | 517 | /**
|
547 |
| - * @return array |
| 518 | + * This test verifies that if the cache directory cannot be deleted because it is no longer empty (due to newly |
| 519 | + * cached files being created after the old ones were deleted), the cache clean method should handle the exception |
| 520 | + * and complete the clean successfully even if the directory cannot be deleted. |
| 521 | + * |
| 522 | + * @return void |
| 523 | + * @throws FileSystemException |
548 | 524 | */
|
549 |
| - public function clearCacheDataProvider(): array |
| 525 | + public function testClearCacheWithUnableToDeleteDirectory(): void |
550 | 526 | {
|
551 |
| - return [ |
552 |
| - [true, '/^catalog\/product\/\.[0-9A-ZA-z-_]{3}$/'], |
553 |
| - [false, '/^catalog\/product\/cache$/'], |
554 |
| - ]; |
| 527 | + $this->mediaDirectory->expects($this->once()) |
| 528 | + ->method('delete') |
| 529 | + ->willThrowException(new FileSystemException(__('Cannot delete non-empty dir.'))); |
| 530 | + |
| 531 | + // Image cache should complete successfully even if the directory cannot be deleted. |
| 532 | + $this->coreFileHelper->expects($this->once())->method('deleteFolder')->willReturn(true); |
| 533 | + $this->cacheManager->expects($this->once())->method('clean'); |
| 534 | + |
| 535 | + $this->image->clearCache(); |
555 | 536 | }
|
556 | 537 |
|
557 | 538 | /**
|
|
0 commit comments