/** * Set up * * @return void */ protected function setUp() { $this->iconRegistryMock = $this->prophesize(\TYPO3\CMS\Core\Imaging\IconRegistry::class); $this->subject = new IconFactory($this->iconRegistryMock->reveal()); $this->iconRegistryMock->isRegistered('tcarecords--default')->willReturn(false); $this->iconRegistryMock->isRegistered(Argument::any())->willReturn(true); $this->iconRegistryMock->isDeprecated(Argument::any())->willReturn(false); $this->iconRegistryMock->getDefaultIconIdentifier(Argument::any())->willReturn('default-not-found'); $this->iconRegistryMock->getIconIdentifierForMimeType('application/pdf')->willReturn('mimetypes-pdf'); $this->iconRegistryMock->getIconIdentifierForMimeType('image/*')->willReturn('mimetypes-media-image'); $this->iconRegistryMock->getIconIdentifierForMimeType(Argument::any())->willReturn(null); $this->iconRegistryMock->getIconIdentifierForFileExtension(Argument::any())->willReturn('mimetypes-other-other'); $this->iconRegistryMock->getIconIdentifierForFileExtension('foo')->willReturn('mimetypes-other-other'); $this->iconRegistryMock->getIconIdentifierForFileExtension('pdf')->willReturn('mimetypes-pdf'); $this->iconRegistryMock->getIconIdentifierForFileExtension('png')->willReturn('mimetypes-media-image'); $this->iconRegistryMock->getIconConfigurationByIdentifier(Argument::any())->willReturn(['provider' => FontawesomeIconProvider::class, 'options' => array('name' => 'times', 'additionalClasses' => 'fa-fw')]); }
/** * @test */ public function registerFileExtensionOverwriteAnExistingIcon() { $this->subject->registerFileExtension('jpg', 'xyz'); $result = $this->subject->getIconIdentifierForFileExtension('jpg'); $this->assertEquals('xyz', $result); }
/** * This method is used throughout the TYPO3 Backend to show icons for files and folders * * The method takes care of the translation of file extension to proper icon and for folders * it will return the icon depending on the role of the folder. * * If the given resource is a folder there are some additional options that can be used: * - mount-root => TRUE (to indicate this is the root of a mount) * - folder-open => TRUE (to indicate that the folder is opened in the file tree) * * There is a hook in place to manipulate the icon name and overlays. * * @param ResourceInterface $resource * @param string $size "large" "small" or "default", see the constants of the Icon class * @param string $overlayIdentifier * @param array $options An associative array with additional options. * @return Icon */ public function getIconForResource(ResourceInterface $resource, $size = Icon::SIZE_DEFAULT, $overlayIdentifier = null, array $options = array()) { $iconIdentifier = null; // Folder if ($resource instanceof FolderInterface) { // non browsable storage if ($resource->getStorage()->isBrowsable() === false && !empty($options['mount-root'])) { $iconIdentifier = 'apps-filetree-folder-locked'; } else { // storage root if ($resource->getStorage()->getRootLevelFolder()->getIdentifier() === $resource->getIdentifier()) { $iconIdentifier = 'apps-filetree-root'; } $role = is_callable([$resource, 'getRole']) ? $resource->getRole() : ''; // user/group mount root if (!empty($options['mount-root'])) { $iconIdentifier = 'apps-filetree-mount'; if ($role === FolderInterface::ROLE_READONLY_MOUNT) { $overlayIdentifier = 'overlay-locked'; } elseif ($role === FolderInterface::ROLE_USER_MOUNT) { $overlayIdentifier = 'overlay-restricted'; } } if ($iconIdentifier === null) { // in folder tree view $options['folder-open'] can define an open folder icon if (!empty($options['folder-open'])) { $iconIdentifier = 'apps-filetree-folder-opened'; } else { $iconIdentifier = 'apps-filetree-folder-default'; } if ($role === FolderInterface::ROLE_TEMPORARY) { $iconIdentifier = 'apps-filetree-folder-temp'; } elseif ($role === FolderInterface::ROLE_RECYCLER) { $iconIdentifier = 'apps-filetree-folder-recycler'; } } // if locked add overlay if ($resource instanceof InaccessibleFolder || !$resource->getStorage()->isBrowsable() || !$resource->getStorage()->checkFolderActionPermission('add', $resource)) { $overlayIdentifier = 'overlay-locked'; } } // File } elseif ($resource instanceof File) { $mimeTypeIcon = $this->iconRegistry->getIconIdentifierForMimeType($resource->getMimeType()); // Check if we find a exact matching mime type if ($mimeTypeIcon !== null) { $iconIdentifier = $mimeTypeIcon; } else { $fileExtensionIcon = $this->iconRegistry->getIconIdentifierForFileExtension($resource->getExtension()); if ($fileExtensionIcon !== 'mimetypes-other-other') { // Fallback 1: icon by file extension $iconIdentifier = $fileExtensionIcon; } else { // Fallback 2: icon by mime type with subtype replaced by * $mimeTypeParts = explode('/', $resource->getMimeType()); $mimeTypeIcon = $this->iconRegistry->getIconIdentifierForMimeType($mimeTypeParts[0] . '/*'); if ($mimeTypeIcon !== null) { $iconIdentifier = $mimeTypeIcon; } else { // Fallback 3: use 'mimetypes-other-other' $iconIdentifier = $fileExtensionIcon; } } } if ($resource->isMissing()) { $overlayIdentifier = 'overlay-missing'; } } unset($options['mount-root']); unset($options['folder-open']); list($iconIdentifier, $overlayIdentifier) = $this->emitBuildIconForResourceSignal($resource, $size, $options, $iconIdentifier, $overlayIdentifier); return $this->getIcon($iconIdentifier, $size, $overlayIdentifier); }