appConfig = $this->createMock(AppConfig::class); $this->preview = new Preview( "onlyoffice", $this->createMock(IRootFolder::class), $this->createMock(LoggerInterface::class), $this->appConfig, $this->createMock(IURLGenerator::class), $this->createMock(Crypt::class), $this->createMock(FileUtility::class), $this->createMock(DocumentService::class), null, ); } /** * Produces a syntactically valid PCRE regex that matches at least one known supported mime type. */ public function testGetMimeTypeRegexReturnsValidRegex(): void { $regex = Preview::getMimeTypeRegex(); $this->assertSame(1, preg_match($regex, "application/pdf")); } /** * Matches every mime type declared in the capabilities list. */ public function testGetMimeTypeRegexMatchesAllCapabilities(): void { $regex = Preview::getMimeTypeRegex(); foreach (Preview::$capabilities as $mime) { $this->assertSame(1, preg_match($regex, $mime), "Expected regex to match: $mime"); } } /** * Does not match mime types outside the capabilities list, acting as an allowlist. */ public function testGetMimeTypeRegexDoesNotMatchUnknownMime(): void { $regex = Preview::getMimeTypeRegex(); $this->assertSame(0, preg_match($regex, "image/png")); } private function makeFile( int $size, string $mime, bool $isExternalStorage = false ): FileInfo&MockObject { $storage = $this->createStub(IStorage::class); $storage->method("instanceOfStorage") ->willReturnCallback(fn($class) => $isExternalStorage && $class === SharingExternalStorage::class); $file = $this->createMock(FileInfo::class); $file->method("getSize")->willReturn($size); $file->method("getMimetype")->willReturn($mime); $file->method("getStorage")->willReturn($storage); return $file; } /** * Returns false when the preview feature is disabled in the app configuration, regardless of the file. */ public function testIsAvailableReturnsFalseWhenPreviewIsDisabled(): void { $this->appConfig->method("getPreview")->willReturn(false); $file = $this->makeFile(1024, "application/pdf"); $this->assertFalse($this->preview->isAvailable($file)); } /** * Rejects empty files as there is nothing to generate a thumbnail from. */ public function testIsAvailableReturnsFalseWhenFileSizeIsZero(): void { $this->appConfig->method("getPreview")->willReturn(true); $this->appConfig->method("getLimitThumbSize")->willReturn(10 * 1024 * 1024); $file = $this->makeFile(0, "application/pdf"); $this->assertFalse($this->preview->isAvailable($file)); } /** * Rejects files larger than the configured thumbnail size limit to avoid excessive overhead. */ public function testIsAvailableReturnsFalseWhenFileSizeExceedsLimit(): void { $this->appConfig->method("getPreview")->willReturn(true); $this->appConfig->method("getLimitThumbSize")->willReturn(1024); $file = $this->makeFile(2048, "application/pdf"); $this->assertFalse($this->preview->isAvailable($file)); } /** * Rejects files with a mime type not in the capabilities list, as they cannot be converted to a thumbnail. */ public function testIsAvailableReturnsFalseWhenMimeTypeNotSupported(): void { $this->appConfig->method("getPreview")->willReturn(true); $this->appConfig->method("getLimitThumbSize")->willReturn(10 * 1024 * 1024); $file = $this->makeFile(1024, "image/png"); $this->assertFalse($this->preview->isAvailable($file)); } /** * Rejects files on external sharing storage, as they cannot be reached through the internal storage path. */ public function testIsAvailableReturnsFalseForExternalSharingStorage(): void { $this->appConfig->method("getPreview")->willReturn(true); $this->appConfig->method("getLimitThumbSize")->willReturn(10 * 1024 * 1024); $file = $this->makeFile(1024, "application/pdf", true); $this->assertFalse($this->preview->isAvailable($file)); } /** * Accepts a non-empty, supported, local file within the size limit when preview generation is enabled. */ public function testIsAvailableReturnsTrueForSupportedLocalFile(): void { $this->appConfig->method("getPreview")->willReturn(true); $this->appConfig->method("getLimitThumbSize")->willReturn(10 * 1024 * 1024); $file = $this->makeFile(1024, "application/pdf"); $this->assertTrue($this->preview->isAvailable($file)); } }