‪TYPO3CMS  10.4
MetaDataAspectTest.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 Prophecy\Argument;
21 use Psr\EventDispatcher\EventDispatcherInterface;
30 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
31 
35 class ‪MetaDataAspectTest extends UnitTestCase
36 {
40  protected ‪$storageMock;
41 
45  protected function ‪setUp(): void
46  {
47  parent::setUp();
48  $eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
49  $eventDispatcher->dispatch(Argument::cetera())->willReturnArgument(0);
50  $metaDataRepository = new ‪MetaDataRepository($eventDispatcher->reveal());
51  GeneralUtility::setSingletonInstance(MetaDataRepository::class, $metaDataRepository);
52  $this->storageMock = $this->createMock(ResourceStorage::class);
53  $this->storageMock->expects(self::any())->method('getUid')->willReturn(12);
54  }
55 
59  protected function ‪tearDown(): void
60  {
61  $this->resetSingletonInstances = true;
62 
63  GeneralUtility::purgeInstances();
64  parent::tearDown();
65  }
66 
70  public function ‪knownMetaDataIsAdded(): void
71  {
72  $metaData = [
73  'width' => 4711,
74  'title' => 'Lorem ipsum meta sit amet',
75  ];
76  $file = new ‪File([], $this->storageMock, $metaData);
77 
78  self::assertSame($metaData, $file->getMetaData()->get());
79  }
80 
84  public function ‪manuallyAddedMetaDataIsMerged(): void
85  {
86  $metaData = [
87  'width' => 4711,
88  'title' => 'Lorem ipsum meta sit amet',
89  ];
90  $file = new ‪File([], $this->storageMock, $metaData);
91  $file->getMetaData()->add([
92  'height' => 900,
93  'description' => 'This file is presented by TYPO3',
94  ]);
95 
96  $expected = [
97  'width' => 4711,
98  'title' => 'Lorem ipsum meta sit amet',
99  'height' => 900,
100  'description' => 'This file is presented by TYPO3',
101  ];
102 
103  self::assertSame($expected, $file->getMetaData()->get());
104  }
105 
109  public function ‪metaDataGetsRemoved(): void
110  {
111  $metaData = ['foo' => 'bar'];
112 
113  $file = new ‪File(['uid' => 12], $this->storageMock);
114 
116  $metaDataAspectMock = $this->getMockBuilder(MetaDataAspect::class)
117  ->setConstructorArgs([$file])
118  ->setMethods(['getMetaDataRepository'])
119  ->getMock();
120 
121  $metaDataAspectMock->add($metaData);
122  $metaDataAspectMock->remove();
123 
124  self::assertEmpty($metaDataAspectMock->get());
125  }
126 
130  public function ‪positiveUidOfFileIsExpectedToLoadMetaData(): void
131  {
132  $this->expectException(InvalidUidException::class);
133  $this->expectExceptionCode(1381590731);
134 
135  $file = new ‪File(['uid' => -3], $this->storageMock);
136  $file->getMetaData()->get();
137  }
138 
142  public function ‪newMetaDataIsCreated(): void
143  {
144  ‪$GLOBALS['EXEC_TIME'] = 1534530781;
145  $metaData = [
146  'title' => 'Hooray',
147  // This value is ignored on purpose, we simulate the non-existence of the field "description"
148  'description' => 'Yipp yipp yipp',
149  ];
150 
151  $file = new ‪File(['uid' => 12], $this->storageMock);
152 
153  $connectionProphecy = $this->prophesize(Connection::class);
154  $connectionProphecy->insert(Argument::cetera())->willReturn(1);
155  $connectionProphecy->lastInsertId(Argument::cetera())->willReturn(5);
156  $connectionPoolProphecy = $this->prophesize(ConnectionPool::class);
157  $connectionPoolProphecy->getConnectionForTable(Argument::cetera())->willReturn($connectionProphecy->reveal());
158  GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolProphecy->reveal());
159 
160  $eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
161  $eventDispatcher->dispatch(Argument::cetera())->willReturnArgument(0);
162  $metaDataRepositoryMock = $this->getMockBuilder(MetaDataRepository::class)
163  ->setMethods(['findByFileUid', 'getTableFields', 'update'])
164  ->setConstructorArgs([$eventDispatcher->reveal()])
165  ->getMock();
166  $metaDataRepositoryMock->expects(self::any())->method('findByFileUid')->willReturn([]);
167  $metaDataRepositoryMock->expects(self::any())->method('getTableFields')->willReturn(['title' => 'sometype']);
168  $metaDataRepositoryMock->expects(self::never())->method('update');
169  GeneralUtility::setSingletonInstance(MetaDataRepository::class, $metaDataRepositoryMock);
170 
171  $file->getMetaData()->add($metaData)->save();
172 
173  $expected = [
174  'file' => $file->getUid(),
175  'pid' => 0,
176  'crdate' => 1534530781,
177  'tstamp' => 1534530781,
178  'cruser_id' => 0,
179  'l10n_diffsource' => '',
180  'title' => 'Hooray',
181  'uid' => '5',
182  ];
183 
184  self::assertSame($expected, $file->getMetaData()->get());
185  }
186 
190  public function ‪existingMetaDataGetsUpdated(): void
191  {
192  $metaData = ['foo' => 'bar'];
193 
194  $file = new ‪File(['uid' => 12], $this->storageMock);
195 
196  $metaDataRepositoryMock = $this->getMockBuilder(MetaDataRepository::class)
197  ->setMethods(['loadFromRepository', 'createMetaDataRecord', 'update'])
198  ->disableOriginalConstructor()
199  ->getMock();
200 
201  $metaDataRepositoryMock->expects(self::any())->method('createMetaDataRecord')->willReturn($metaData);
202  GeneralUtility::setSingletonInstance(MetaDataRepository::class, $metaDataRepositoryMock);
203 
204  $metaDataAspectMock = $this->getMockBuilder(MetaDataAspect::class)
205  ->setConstructorArgs([$file])
206  ->setMethods(['loadFromRepository'])
207  ->getMock();
208 
209  $metaDataAspectMock->expects(self::any())->method('loadFromRepository')->will(self::onConsecutiveCalls([], $metaData));
210  $metaDataAspectMock->add($metaData)->save();
211  $metaDataAspectMock->add(['testproperty' => 'testvalue'])->save();
212 
213  self::assertSame(['foo' => 'bar', 'testproperty' => 'testvalue'], $metaDataAspectMock->get());
214  }
215 
219  public function ‪propertyDataProvider(): array
220  {
221  return [
222  [
223  [
224  'width' => 4711,
225  'title' => 'Lorem ipsum meta sit amet',
226  ],
227  [
228  'property' => 'width',
229  'expected' => true,
230  ],
231  [
232  'property' => 'width',
233  'expected' => 4711,
234  ],
235  ],
236  [
237  [
238  'foo' => 'bar',
239  ],
240  [
241  'property' => 'husel',
242  'expected' => false,
243  ],
244  [
245  'property' => 'husel',
246  'expected' => null,
247  ],
248  ],
249  ];
250  }
251 
259  public function ‪propertyIsFetchedProperly($metaData, $has, $get): void
260  {
261  $file = new ‪File([], $this->storageMock, $metaData);
262 
263  self::assertSame($has['expected'], isset($file->getMetaData()[$has['property']]));
264  self::assertSame($get['expected'], $file->getMetaData()[$get['property']] ?? null);
265  }
266 }
‪TYPO3\CMS\Core\Resource\Index\MetaDataRepository
Definition: MetaDataRepository.php:40
‪TYPO3\CMS\Core\Tests\Unit\Resource\MetaDataAspectTest\$storageMock
‪ResourceStorage PHPUnit Framework MockObject MockObject $storageMock
Definition: MetaDataAspectTest.php:39
‪TYPO3\CMS\Core\Tests\Unit\Resource\MetaDataAspectTest\manuallyAddedMetaDataIsMerged
‪manuallyAddedMetaDataIsMerged()
Definition: MetaDataAspectTest.php:83
‪TYPO3\CMS\Core\Tests\Unit\Resource
Definition: AbstractFileTest.php:18
‪TYPO3\CMS\Core\Resource\Exception\InvalidUidException
Definition: InvalidUidException.php:24
‪TYPO3\CMS\Core\Resource\MetaDataAspect
Definition: MetaDataAspect.php:27
‪TYPO3\CMS\Core\Tests\Unit\Resource\MetaDataAspectTest\existingMetaDataGetsUpdated
‪existingMetaDataGetsUpdated()
Definition: MetaDataAspectTest.php:189
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:24
‪TYPO3\CMS\Core\Tests\Unit\Resource\MetaDataAspectTest\knownMetaDataIsAdded
‪knownMetaDataIsAdded()
Definition: MetaDataAspectTest.php:69
‪TYPO3\CMS\Core\Tests\Unit\Resource\MetaDataAspectTest\propertyDataProvider
‪array propertyDataProvider()
Definition: MetaDataAspectTest.php:218
‪TYPO3\CMS\Core\Tests\Unit\Resource\MetaDataAspectTest\metaDataGetsRemoved
‪metaDataGetsRemoved()
Definition: MetaDataAspectTest.php:108
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:36
‪TYPO3\CMS\Core\Resource\ResourceStorage
Definition: ResourceStorage.php:122
‪TYPO3\CMS\Core\Tests\Unit\Resource\MetaDataAspectTest
Definition: MetaDataAspectTest.php:36
‪TYPO3\CMS\Core\Tests\Unit\Resource\MetaDataAspectTest\newMetaDataIsCreated
‪newMetaDataIsCreated()
Definition: MetaDataAspectTest.php:141
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Tests\Unit\Resource\MetaDataAspectTest\positiveUidOfFileIsExpectedToLoadMetaData
‪positiveUidOfFileIsExpectedToLoadMetaData()
Definition: MetaDataAspectTest.php:129
‪TYPO3\CMS\Core\Tests\Unit\Resource\MetaDataAspectTest\setUp
‪setUp()
Definition: MetaDataAspectTest.php:44
‪TYPO3\CMS\Core\Tests\Unit\Resource\MetaDataAspectTest\tearDown
‪tearDown()
Definition: MetaDataAspectTest.php:58
‪TYPO3\CMS\Core\Tests\Unit\Resource\MetaDataAspectTest\propertyIsFetchedProperly
‪propertyIsFetchedProperly($metaData, $has, $get)
Definition: MetaDataAspectTest.php:258
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46