‪TYPO3CMS  ‪main
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 PHPUnit\Framework\MockObject\MockObject;
21 use Psr\EventDispatcher\EventDispatcherInterface;
31 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
32 
33 final class ‪MetaDataAspectTest extends UnitTestCase
34 {
35  protected bool ‪$resetSingletonInstances = true;
36  protected ‪ResourceStorage&MockObject ‪$storageMock;
37 
38  protected function ‪setUp(): void
39  {
40  parent::setUp();
41  $metaDataRepository = new ‪MetaDataRepository(new ‪NoopEventDispatcher());
42  GeneralUtility::setSingletonInstance(MetaDataRepository::class, $metaDataRepository);
43  $this->storageMock = $this->createMock(ResourceStorage::class);
44  $this->storageMock->method('getUid')->willReturn(12);
45  }
46 
47  protected function ‪tearDown(): void
48  {
49  GeneralUtility::purgeInstances();
50  parent::tearDown();
51  }
52 
56  public function ‪knownMetaDataIsAdded(): void
57  {
58  $metaData = [
59  'width' => 4711,
60  'title' => 'Lorem ipsum meta sit amet',
61  ];
62  $file = new ‪File([], $this->storageMock, $metaData);
63 
64  self::assertSame($metaData, $file->getMetaData()->get());
65  }
66 
70  public function ‪manuallyAddedMetaDataIsMerged(): void
71  {
72  $metaData = [
73  'width' => 4711,
74  'title' => 'Lorem ipsum meta sit amet',
75  ];
76  $file = new ‪File([], $this->storageMock, $metaData);
77  $file->getMetaData()->add([
78  'height' => 900,
79  'description' => 'This file is presented by TYPO3',
80  ]);
81 
82  $expected = [
83  'width' => 4711,
84  'title' => 'Lorem ipsum meta sit amet',
85  'height' => 900,
86  'description' => 'This file is presented by TYPO3',
87  ];
88 
89  self::assertSame($expected, $file->getMetaData()->get());
90  }
91 
95  public function ‪metaDataGetsRemoved(): void
96  {
97  $metaData = ['foo' => 'bar'];
98 
99  $file = new ‪File(['uid' => 12], $this->storageMock);
100 
101  $metaDataAspectMock = $this->getMockBuilder(MetaDataAspect::class)
102  ->setConstructorArgs([$file])
103  ->onlyMethods(['getMetaDataRepository'])
104  ->getMock();
105 
106  $metaDataAspectMock->add($metaData);
107  $metaDataAspectMock->remove();
108 
109  self::assertEmpty($metaDataAspectMock->get());
110  }
111 
116  {
117  $this->expectException(InvalidUidException::class);
118  $this->expectExceptionCode(1381590731);
119 
120  $file = new ‪File(['uid' => -3], $this->storageMock);
121  $file->getMetaData()->get();
122  }
123 
127  public function ‪newMetaDataIsCreated(): void
128  {
129  ‪$GLOBALS['EXEC_TIME'] = 1534530781;
130  $metaData = [
131  'title' => 'Hooray',
132  // This value is ignored on purpose, we simulate the non-existence of the field "description"
133  'description' => 'Yipp yipp yipp',
134  ];
135 
136  $file = new ‪File(['uid' => 12], $this->storageMock);
137 
138  $connectionMock = $this->createMock(Connection::class);
139  $connectionMock->method('insert')->with(self::anything())->willReturn(1);
140  $connectionMock->method('lastInsertId')->willReturn('5');
141  $connectionPoolMock = $this->createMock(ConnectionPool::class);
142  $connectionPoolMock->method('getConnectionForTable')->with(self::anything())->willReturn($connectionMock);
143  GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolMock);
144 
145  $metaDataRepositoryMock = $this->getMockBuilder(MetaDataRepository::class)
146  ->onlyMethods(['findByFileUid', 'getTableFields', 'update'])
147  ->setConstructorArgs([new ‪NoopEventDispatcher()])
148  ->getMock();
149  $metaDataRepositoryMock->method('findByFileUid')->willReturn([]);
150  $metaDataRepositoryMock->method('getTableFields')->willReturn(['title' => 'sometype']);
151  $metaDataRepositoryMock->expects(self::never())->method('update');
152  GeneralUtility::setSingletonInstance(MetaDataRepository::class, $metaDataRepositoryMock);
153 
154  $file->getMetaData()->add($metaData)->save();
155 
156  $expected = [
157  'file' => $file->getUid(),
158  'pid' => 0,
159  'crdate' => 1534530781,
160  'tstamp' => 1534530781,
161  'l10n_diffsource' => '',
162  'title' => 'Hooray',
163  'uid' => '5',
164  ];
165 
166  self::assertSame($expected, $file->getMetaData()->get());
167  }
168 
172  public function ‪existingMetaDataGetsUpdated(): void
173  {
174  $metaData = ['uid' => 12, 'foo' => 'bar'];
175  $updatedMetadata = array_merge($metaData, ['testproperty' => 'testvalue']);
176 
177  $file = new ‪File(['uid' => 12], $this->storageMock);
178 
179  $eventDispatcherMock = $this->getMockBuilder(EventDispatcherInterface::class)->getMock();
180  $eventDispatcherMock->expects(self::atLeastOnce())->method('dispatch')->with(self::anything())->willReturnArgument(0);
181 
182  $metaDataRepositoryMock = $this->getMockBuilder(MetaDataRepository::class)
183  ->onlyMethods(['createMetaDataRecord', 'getTableFields'])
184  ->addMethods(['loadFromRepository'])
185  ->setConstructorArgs([$eventDispatcherMock])
186  ->getMock();
187 
188  $connectionMock = $this->createMock(Connection::class);
189  $connectionMock->method('update')->with('sys_file_metadata', self::anything())->willReturn(1);
190  $connectionPoolMock = $this->createMock(ConnectionPool::class);
191  $connectionPoolMock->method('getConnectionForTable')->with(self::anything())->willReturn($connectionMock);
192  GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolMock);
193 
194  $metaDataRepositoryMock->method('createMetaDataRecord')->willReturn($metaData);
195  $metaDataRepositoryMock->method('getTableFields')->willReturn(array_flip(['foo', 'testproperty']));
196  GeneralUtility::setSingletonInstance(MetaDataRepository::class, $metaDataRepositoryMock);
197 
198  $metaDataAspectMock = $this->getMockBuilder(MetaDataAspect::class)
199  ->setConstructorArgs([$file])
200  ->onlyMethods(['loadFromRepository'])
201  ->getMock();
202 
203  $metaDataAspectMock->method('loadFromRepository')->will(self::onConsecutiveCalls([], $metaData, $updatedMetadata));
204  $metaDataAspectMock->add($metaData)->save();
205  $metaDataAspectMock->add(['testproperty' => 'testvalue'])->save();
206 
207  self::assertSame('bar', $metaDataAspectMock->offsetGet('foo'));
208  self::assertSame('testvalue', $metaDataAspectMock->offsetGet('testproperty'));
209 
210  $metaDataAspectMock->add($updatedMetadata)->save();
211  self::assertFalse($metaDataAspectMock->offsetExists('tstamp'));
212  }
213 
214  public static function ‪propertyDataProvider(): array
215  {
216  return [
217  [
218  [
219  'width' => 4711,
220  'title' => 'Lorem ipsum meta sit amet',
221  ],
222  [
223  'property' => 'width',
224  'expected' => true,
225  ],
226  [
227  'property' => 'width',
228  'expected' => 4711,
229  ],
230  ],
231  [
232  [
233  'foo' => 'bar',
234  ],
235  [
236  'property' => 'husel',
237  'expected' => false,
238  ],
239  [
240  'property' => 'husel',
241  'expected' => null,
242  ],
243  ],
244  ];
245  }
246 
251  public function ‪propertyIsFetchedProperly(array $metaData, array $has, array $get): void
252  {
253  $file = new ‪File([], $this->storageMock, $metaData);
254 
255  self::assertSame($has['expected'], isset($file->getMetaData()[$has['property']]));
256  self::assertSame($get['expected'], $file->getMetaData()[$get['property']] ?? null);
257  }
258 }
‪TYPO3\CMS\Core\Resource\Index\MetaDataRepository
Definition: MetaDataRepository.php:40
‪TYPO3\CMS\Core\Tests\Unit\Resource\MetaDataAspectTest\manuallyAddedMetaDataIsMerged
‪manuallyAddedMetaDataIsMerged()
Definition: MetaDataAspectTest.php:70
‪TYPO3\CMS\Core\Tests\Unit\Resource
Definition: AbstractFileTest.php:18
‪TYPO3\CMS\Core\Resource\Exception\InvalidUidException
Definition: InvalidUidException.php:23
‪TYPO3\CMS\Core\Resource\MetaDataAspect
Definition: MetaDataAspect.php:27
‪TYPO3\CMS\Core\Tests\Unit\Resource\MetaDataAspectTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: MetaDataAspectTest.php:35
‪TYPO3\CMS\Core\Tests\Unit\Resource\MetaDataAspectTest\existingMetaDataGetsUpdated
‪existingMetaDataGetsUpdated()
Definition: MetaDataAspectTest.php:172
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:26
‪TYPO3\CMS\Core\Tests\Unit\Resource\MetaDataAspectTest\knownMetaDataIsAdded
‪knownMetaDataIsAdded()
Definition: MetaDataAspectTest.php:56
‪TYPO3\CMS\Core\Tests\Unit\Resource\MetaDataAspectTest\metaDataGetsRemoved
‪metaDataGetsRemoved()
Definition: MetaDataAspectTest.php:95
‪TYPO3\CMS\Core\Tests\Unit\Resource\MetaDataAspectTest\propertyIsFetchedProperly
‪propertyIsFetchedProperly(array $metaData, array $has, array $get)
Definition: MetaDataAspectTest.php:251
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:41
‪TYPO3\CMS\Core\Resource\ResourceStorage
Definition: ResourceStorage.php:128
‪TYPO3\CMS\Core\EventDispatcher\NoopEventDispatcher
Definition: NoopEventDispatcher.php:29
‪TYPO3\CMS\Core\Tests\Unit\Resource\MetaDataAspectTest
Definition: MetaDataAspectTest.php:34
‪TYPO3\CMS\Core\Tests\Unit\Resource\MetaDataAspectTest\newMetaDataIsCreated
‪newMetaDataIsCreated()
Definition: MetaDataAspectTest.php:127
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Tests\Unit\Resource\MetaDataAspectTest\positiveUidOfFileIsExpectedToLoadMetaData
‪positiveUidOfFileIsExpectedToLoadMetaData()
Definition: MetaDataAspectTest.php:115
‪TYPO3\CMS\Core\Tests\Unit\Resource\MetaDataAspectTest\setUp
‪setUp()
Definition: MetaDataAspectTest.php:38
‪TYPO3\CMS\Core\Tests\Unit\Resource\MetaDataAspectTest\tearDown
‪tearDown()
Definition: MetaDataAspectTest.php:47
‪TYPO3\CMS\Core\Tests\Unit\Resource\MetaDataAspectTest\propertyDataProvider
‪static propertyDataProvider()
Definition: MetaDataAspectTest.php:214
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Tests\Unit\Resource\MetaDataAspectTest\$storageMock
‪ResourceStorage &MockObject $storageMock
Definition: MetaDataAspectTest.php:36