‪TYPO3CMS  ‪main
FileControllerTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use PHPUnit\Framework\Attributes\Test;
21 use PHPUnit\Framework\MockObject\MockObject;
34 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
35 
36 final class ‪FileControllerTest extends UnitTestCase
37 {
38  protected ‪File&MockObject ‪$fileResourceMock;
39 
40  protected function ‪setUp(): void
41  {
42  parent::setUp();
43 
44  $parentFolderMock = $this->createMock(FolderInterface::class);
45  $this->fileResourceMock = $this->getMockBuilder(File::class)
46  ->onlyMethods(['toArray', 'getModificationTime', 'getExtension', 'getParentFolder'])
47  ->disableOriginalConstructor()
48  ->getMock();
49  $this->fileResourceMock->method('toArray')->willReturn(['id' => 'foo']);
50  $this->fileResourceMock->method('getModificationTime')->willReturn(123456789);
51  $this->fileResourceMock->method('getExtension')->willReturn('html');
52  $this->fileResourceMock->method('getParentFolder')->willReturn($parentFolderMock);
53  }
54 
55  #[Test]
57  {
58  $subject = $this->getAccessibleMock(FileController::class, ['init', 'main'], [], '', false);
59  self::assertTrue($subject->_call('flattenResultDataValue', true));
60  self::assertSame([], $subject->_call('flattenResultDataValue', []));
61  }
62 
63  #[Test]
64  public function ‪flattenResultDataValueFlattensFile(): void
65  {
66  $iconFactoryMock = $this->createMock(IconFactory::class);
67  $icon = $this->createMock(Icon::class);
68  $icon->expects(self::once())->method('render')->willReturn('');
69  $iconFactoryMock->method('getIconForFileExtension')->willReturn($icon);
70  $subject = $this->getAccessibleMock(
71  FileController::class,
72  ['init', 'main'],
73  [
74  $this->createMock(ResourceFactory::class),
76  $iconFactoryMock,
77  $this->createMock(UriBuilder::class),
79  ],
80  );
81 
82  $result = $subject->_call('flattenResultDataValue', $this->fileResourceMock);
83  self::assertSame(
84  [
85  'id' => 'foo',
86  'date' => '1973-11-29',
87  'icon' => '',
88  'thumbUrl' => '',
89  'path' => '',
90  ],
91  $result
92  );
93  }
94 
95  #[Test]
97  {
98  $subject = $this->getAccessibleMock(
99  FileController::class,
100  ['init', 'main'],
101  [
102  $this->createMock(ResourceFactory::class),
104  $this->createMock(IconFactory::class),
105  $this->createMock(UriBuilder::class),
107  ],
108  );
109  $subject->_set('fileData', ['delete' => [true]]);
110  $subject->_set('redirect', false);
111  $subject->expects(self::once())->method('main');
112  $subject->processAjaxRequest(new ‪ServerRequest());
113  }
114 
115  #[Test]
117  {
118  $subject = $this->getAccessibleMock(
119  FileController::class,
120  ['init', 'main'],
121  [
122  $this->createMock(ResourceFactory::class),
124  $this->createMock(IconFactory::class),
125  $this->createMock(UriBuilder::class),
127  ],
128  );
129  $subject->_set('fileData', ['editfile' => [true]]);
130  $subject->_set('redirect', false);
131  $subject->expects(self::once())->method('main');
132  $subject->processAjaxRequest(new ‪ServerRequest());
133  }
134 
135  #[Test]
137  {
138  $subject = $this->getAccessibleMock(
139  FileController::class,
140  ['init', 'main'],
141  [
142  $this->createMock(ResourceFactory::class),
144  $this->createMock(IconFactory::class),
145  $this->createMock(UriBuilder::class),
147  ],
148  );
149  $subject->_set('fileData', ['editfile' => [true]]);
150  $subject->_set('redirect', false);
151  $response = $subject->processAjaxRequest(new ‪ServerRequest());
152  self::assertEquals(200, $response->getStatusCode());
153  }
154 
155  #[Test]
157  {
158  $flashMessageService = new ‪FlashMessageService();
159  $messageQueue = $flashMessageService->getMessageQueueByIdentifier();
160  $messageQueue->addMessage(new ‪FlashMessage('Error occurred', 'Error occurred', ContextualFeedbackSeverity::ERROR));
161  $subject = $this->getAccessibleMock(
162  FileController::class,
163  ['init', 'main'],
164  [
165  $this->createMock(ResourceFactory::class),
167  $this->createMock(IconFactory::class),
168  $this->createMock(UriBuilder::class),
169  $flashMessageService,
170  ],
171  );
172  $subject->_set('fileData', []);
173  $response = $subject->processAjaxRequest(new ‪ServerRequest());
174  self::assertEquals(500, $response->getStatusCode());
175  }
176 }
‪TYPO3\CMS\Backend\Tests\Unit\Controller\File
Definition: FileControllerTest.php:18
‪TYPO3\CMS\Core\Imaging\Icon
Definition: Icon.php:27
‪TYPO3\CMS\Backend\Tests\Unit\Controller\File\FileControllerTest\setUp
‪setUp()
Definition: FileControllerTest.php:40
‪TYPO3\CMS\Core\Imaging\IconFactory
Definition: IconFactory.php:34
‪TYPO3\CMS\Backend\Tests\Unit\Controller\File\FileControllerTest\processAjaxRequestReturnsStatus200IfNoErrorOccurs
‪processAjaxRequestReturnsStatus200IfNoErrorOccurs()
Definition: FileControllerTest.php:136
‪TYPO3\CMS\Backend\Tests\Unit\Controller\File\FileControllerTest\flattenResultDataValueFlattensFile
‪flattenResultDataValueFlattensFile()
Definition: FileControllerTest.php:64
‪TYPO3\CMS\Backend\Tests\Unit\Controller\File\FileControllerTest\processAjaxRequestReturnsStatus500IfErrorOccurs
‪processAjaxRequestReturnsStatus500IfErrorOccurs()
Definition: FileControllerTest.php:156
‪TYPO3\CMS\Core\Type\ContextualFeedbackSeverity
‪ContextualFeedbackSeverity
Definition: ContextualFeedbackSeverity.php:25
‪TYPO3\CMS\Core\Utility\File\ExtendedFileUtility
Definition: ExtendedFileUtility.php:76
‪TYPO3\CMS\Backend\Routing\UriBuilder
Definition: UriBuilder.php:44
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:42
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:26
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:39
‪TYPO3\CMS\Backend\Tests\Unit\Controller\File\FileControllerTest\flattenResultDataValueReturnsAnythingElseAsIs
‪flattenResultDataValueReturnsAnythingElseAsIs()
Definition: FileControllerTest.php:56
‪TYPO3\CMS\Core\Messaging\FlashMessage
Definition: FlashMessage.php:27
‪TYPO3\CMS\Backend\Controller\File\FileController
Definition: FileController.php:50
‪TYPO3\CMS\Backend\Tests\Unit\Controller\File\FileControllerTest\processAjaxRequestDeleteProcessActuallyDoesNotChangeFileData
‪processAjaxRequestDeleteProcessActuallyDoesNotChangeFileData()
Definition: FileControllerTest.php:96
‪TYPO3\CMS\Core\Resource\FolderInterface
Definition: FolderInterface.php:24
‪TYPO3\CMS\Backend\Tests\Unit\Controller\File\FileControllerTest
Definition: FileControllerTest.php:37
‪TYPO3\CMS\Backend\Tests\Unit\Controller\File\FileControllerTest\$fileResourceMock
‪File &MockObject $fileResourceMock
Definition: FileControllerTest.php:38
‪TYPO3\CMS\Backend\Tests\Unit\Controller\File\FileControllerTest\processAjaxRequestEditFileProcessActuallyDoesNotChangeFileData
‪processAjaxRequestEditFileProcessActuallyDoesNotChangeFileData()
Definition: FileControllerTest.php:116
‪TYPO3\CMS\Core\Messaging\FlashMessageService
Definition: FlashMessageService.php:27