1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* File contains: eZ\Publish\Core\Repository\Tests\Service\Mock\DomainMapperTest class. |
5
|
|
|
* |
6
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
7
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
namespace eZ\Publish\Core\Repository\Tests\Service\Mock; |
10
|
|
|
|
11
|
|
|
use DateTime; |
12
|
|
|
use eZ\Publish\API\Repository\Exceptions\InvalidArgumentException; |
13
|
|
|
use eZ\Publish\API\Repository\Values\Content\Search\SearchHit; |
14
|
|
|
use eZ\Publish\API\Repository\Values\Content\Search\SearchResult; |
15
|
|
|
use eZ\Publish\API\Repository\Values\Content\VersionInfo as APIVersionInfo; |
16
|
|
|
use eZ\Publish\Core\Repository\ProxyFactory\ProxyDomainMapperInterface; |
17
|
|
|
use eZ\Publish\Core\Repository\Tests\Service\Mock\Base as BaseServiceMockTest; |
18
|
|
|
use eZ\Publish\Core\Repository\Mapper\ContentDomainMapper; |
19
|
|
|
use eZ\Publish\Core\Repository\Values\Content\Content; |
20
|
|
|
use eZ\Publish\Core\Repository\Values\Content\VersionInfo; |
21
|
|
|
use eZ\Publish\SPI\Persistence\Content\ContentInfo; |
22
|
|
|
use eZ\Publish\SPI\Persistence\Content\ContentInfo as SPIContentInfo; |
23
|
|
|
use eZ\Publish\SPI\Persistence\Content\Location; |
24
|
|
|
use eZ\Publish\API\Repository\Values\Content\Location as APILocation; |
25
|
|
|
use eZ\Publish\SPI\Persistence\Content\VersionInfo as SPIVersionInfo; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Mock test case for internal ContentDomainMapper. |
29
|
|
|
*/ |
30
|
|
|
class DomainMapperTest extends BaseServiceMockTest |
31
|
|
|
{ |
32
|
|
|
private const EXAMPLE_CONTENT_TYPE_ID = 1; |
33
|
|
|
private const EXAMPLE_SECTION_ID = 1; |
34
|
|
|
private const EXAMPLE_MAIN_LOCATION_ID = 1; |
35
|
|
|
private const EXAMPLE_MAIN_LANGUAGE_CODE = 'ger-DE'; |
36
|
|
|
private const EXAMPLE_OWNER_ID = 1; |
37
|
|
|
private const EXAMPLE_INITIAL_LANGUAGE_CODE = 'eng-GB'; |
38
|
|
|
private const EXAMPLE_CREATOR_ID = 23; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @covers \eZ\Publish\Core\Repository\Mapper\ContentDomainMapper::buildVersionInfoDomainObject |
42
|
|
|
* @dataProvider providerForBuildVersionInfo |
43
|
|
|
*/ |
44
|
|
|
public function testBuildVersionInfo(SPIVersionInfo $spiVersionInfo) |
45
|
|
|
{ |
46
|
|
|
$languageHandlerMock = $this->getLanguageHandlerMock(); |
47
|
|
|
$languageHandlerMock->expects($this->never())->method('load'); |
48
|
|
|
|
49
|
|
|
$versionInfo = $this->getContentDomainMapper()->buildVersionInfoDomainObject($spiVersionInfo); |
50
|
|
|
|
51
|
|
|
$this->assertInstanceOf(APIVersionInfo::class, $versionInfo); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @covers \eZ\Publish\Core\Repository\Mapper\ContentDomainMapper::buildLocationWithContent |
56
|
|
|
*/ |
57
|
|
|
public function testBuildLocationWithContentForRootLocation() |
58
|
|
|
{ |
59
|
|
|
$spiRootLocation = new Location(['id' => 1, 'parentId' => 1]); |
60
|
|
|
$apiRootLocation = $this->getContentDomainMapper()->buildLocationWithContent($spiRootLocation, null); |
61
|
|
|
|
62
|
|
|
$legacyDateTime = new DateTime(); |
63
|
|
|
$legacyDateTime->setTimestamp(1030968000); |
64
|
|
|
|
65
|
|
|
$expectedContentInfo = new \eZ\Publish\API\Repository\Values\Content\ContentInfo([ |
66
|
|
|
'id' => 0, |
67
|
|
|
'name' => 'Top Level Nodes', |
68
|
|
|
'sectionId' => 1, |
69
|
|
|
'mainLocationId' => 1, |
70
|
|
|
'contentTypeId' => 1, |
71
|
|
|
'currentVersionNo' => 1, |
72
|
|
|
'published' => 1, |
73
|
|
|
'ownerId' => 14, |
74
|
|
|
'modificationDate' => $legacyDateTime, |
75
|
|
|
'publishedDate' => $legacyDateTime, |
76
|
|
|
'alwaysAvailable' => 1, |
77
|
|
|
'remoteId' => null, |
78
|
|
|
'mainLanguageCode' => 'eng-GB', |
79
|
|
|
]); |
80
|
|
|
|
81
|
|
|
$expectedContent = new Content([ |
82
|
|
|
'versionInfo' => new VersionInfo([ |
83
|
|
|
'names' => [ |
84
|
|
|
$expectedContentInfo->mainLanguageCode => $expectedContentInfo->name, |
85
|
|
|
], |
86
|
|
|
'contentInfo' => $expectedContentInfo, |
87
|
|
|
'versionNo' => $expectedContentInfo->currentVersionNo, |
88
|
|
|
'modificationDate' => $expectedContentInfo->modificationDate, |
89
|
|
|
'creationDate' => $expectedContentInfo->modificationDate, |
90
|
|
|
'creatorId' => $expectedContentInfo->ownerId, |
91
|
|
|
]), |
92
|
|
|
]); |
93
|
|
|
|
94
|
|
|
$this->assertInstanceOf(APILocation::class, $apiRootLocation); |
95
|
|
|
$this->assertEquals($spiRootLocation->id, $apiRootLocation->id); |
96
|
|
|
$this->assertEquals($expectedContentInfo->id, $apiRootLocation->getContentInfo()->id); |
97
|
|
|
$this->assertEquals($expectedContent, $apiRootLocation->getContent()); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @covers \eZ\Publish\Core\Repository\Mapper\ContentDomainMapper::buildLocationWithContent |
102
|
|
|
*/ |
103
|
|
|
public function testBuildLocationWithContentThrowsInvalidArgumentException() |
104
|
|
|
{ |
105
|
|
|
$this->expectException(InvalidArgumentException::class); |
106
|
|
|
$this->expectExceptionMessage('Argument \'$content\' is invalid: Location 2 has missing Content'); |
107
|
|
|
|
108
|
|
|
$nonRootLocation = new Location(['id' => 2, 'parentId' => 1]); |
109
|
|
|
|
110
|
|
|
$this->getContentDomainMapper()->buildLocationWithContent($nonRootLocation, null); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function testBuildLocationWithContentIsAlignedWithBuildLocation() |
114
|
|
|
{ |
115
|
|
|
$spiRootLocation = new Location(['id' => 1, 'parentId' => 1]); |
116
|
|
|
|
117
|
|
|
$this->assertEquals( |
118
|
|
|
$this->getContentDomainMapper()->buildLocationWithContent($spiRootLocation, null), |
119
|
|
|
$this->getContentDomainMapper()->buildLocation($spiRootLocation) |
120
|
|
|
); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function providerForBuildVersionInfo() |
124
|
|
|
{ |
125
|
|
|
$properties = [ |
126
|
|
|
'contentInfo' => new SPIContentInfo([ |
127
|
|
|
'contentTypeId' => self::EXAMPLE_CONTENT_TYPE_ID, |
128
|
|
|
'sectionId' => self::EXAMPLE_SECTION_ID, |
129
|
|
|
'mainLocationId' => self::EXAMPLE_MAIN_LOCATION_ID, |
130
|
|
|
'mainLanguageCode' => self::EXAMPLE_MAIN_LANGUAGE_CODE, |
131
|
|
|
'ownerId' => self::EXAMPLE_OWNER_ID, |
132
|
|
|
]), |
133
|
|
|
'creatorId' => self::EXAMPLE_CREATOR_ID, |
134
|
|
|
'initialLanguageCode' => self::EXAMPLE_INITIAL_LANGUAGE_CODE, |
135
|
|
|
]; |
136
|
|
|
|
137
|
|
|
return [ |
138
|
|
|
[ |
139
|
|
|
new SPIVersionInfo( |
140
|
|
|
$properties + [ |
141
|
|
|
'status' => 44, |
142
|
|
|
]), |
143
|
|
|
], |
144
|
|
|
[ |
145
|
|
|
new SPIVersionInfo( |
146
|
|
|
$properties + [ |
147
|
|
|
'status' => SPIVersionInfo::STATUS_DRAFT, |
148
|
|
|
] |
149
|
|
|
), |
150
|
|
|
], |
151
|
|
|
[ |
152
|
|
|
new SPIVersionInfo( |
153
|
|
|
$properties + [ |
154
|
|
|
'status' => SPIVersionInfo::STATUS_PENDING, |
155
|
|
|
] |
156
|
|
|
), |
157
|
|
|
], |
158
|
|
|
[ |
159
|
|
|
new SPIVersionInfo( |
160
|
|
|
$properties + [ |
161
|
|
|
'status' => SPIVersionInfo::STATUS_ARCHIVED, |
162
|
|
|
'languageCodes' => ['eng-GB', 'nor-NB', 'fre-FR'], |
163
|
|
|
] |
164
|
|
|
), |
165
|
|
|
], |
166
|
|
|
[ |
167
|
|
|
new SPIVersionInfo( |
168
|
|
|
$properties + [ |
169
|
|
|
'status' => SPIVersionInfo::STATUS_PUBLISHED, |
170
|
|
|
] |
171
|
|
|
), |
172
|
|
|
], |
173
|
|
|
]; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public function providerForBuildLocationDomainObjectsOnSearchResult() |
177
|
|
|
{ |
178
|
|
|
$properties = [ |
179
|
|
|
'contentTypeId' => self::EXAMPLE_CONTENT_TYPE_ID, |
180
|
|
|
'sectionId' => self::EXAMPLE_SECTION_ID, |
181
|
|
|
'mainLocationId' => self::EXAMPLE_MAIN_LOCATION_ID, |
182
|
|
|
'mainLanguageCode' => self::EXAMPLE_MAIN_LANGUAGE_CODE, |
183
|
|
|
'ownerId' => self::EXAMPLE_OWNER_ID, |
184
|
|
|
]; |
185
|
|
|
|
186
|
|
|
$locationHits = [ |
187
|
|
|
new Location(['id' => 21, 'contentId' => 32, 'parentId' => 1]), |
188
|
|
|
new Location(['id' => 22, 'contentId' => 33, 'parentId' => 1]), |
189
|
|
|
]; |
190
|
|
|
|
191
|
|
|
return [ |
192
|
|
|
[ |
193
|
|
|
$locationHits, |
194
|
|
|
[32, 33], |
195
|
|
|
[], |
196
|
|
|
[ |
197
|
|
|
32 => new ContentInfo($properties + ['id' => 32]), |
198
|
|
|
33 => new ContentInfo($properties + ['id' => 33]), |
199
|
|
|
], |
200
|
|
|
0, |
201
|
|
|
], |
202
|
|
|
[ |
203
|
|
|
$locationHits, |
204
|
|
|
[32, 33], |
205
|
|
|
['languages' => ['eng-GB']], |
206
|
|
|
[ |
207
|
|
|
32 => new ContentInfo($properties + ['id' => 32]), |
208
|
|
|
], |
209
|
|
|
1, |
210
|
|
|
], |
211
|
|
|
[ |
212
|
|
|
$locationHits, |
213
|
|
|
[32, 33], |
214
|
|
|
['languages' => ['eng-GB']], |
215
|
|
|
[], |
216
|
|
|
2, |
217
|
|
|
], |
218
|
|
|
]; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @covers \eZ\Publish\Core\Repository\Mapper\ContentDomainMapper::buildLocationDomainObjectsOnSearchResult |
223
|
|
|
* @dataProvider providerForBuildLocationDomainObjectsOnSearchResult |
224
|
|
|
* |
225
|
|
|
* @param array $locationHits |
226
|
|
|
* @param array $contentIds |
227
|
|
|
* @param array $languageFilter |
228
|
|
|
* @param array $contentInfoList |
229
|
|
|
* @param int $missing |
230
|
|
|
*/ |
231
|
|
|
public function testBuildLocationDomainObjectsOnSearchResult( |
232
|
|
|
array $locationHits, |
233
|
|
|
array $contentIds, |
234
|
|
|
array $languageFilter, |
235
|
|
|
array $contentInfoList, |
236
|
|
|
int $missing |
237
|
|
|
) { |
238
|
|
|
$contentHandlerMock = $this->getContentHandlerMock(); |
239
|
|
|
$contentHandlerMock |
240
|
|
|
->expects($this->once()) |
241
|
|
|
->method('loadContentInfoList') |
242
|
|
|
->with($contentIds) |
243
|
|
|
->willReturn($contentInfoList); |
244
|
|
|
|
245
|
|
|
$result = new SearchResult(['totalCount' => 10]); |
246
|
|
|
foreach ($locationHits as $locationHit) { |
247
|
|
|
$result->searchHits[] = new SearchHit(['valueObject' => $locationHit]); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
$spiResult = clone $result; |
251
|
|
|
$missingLocations = $this->getContentDomainMapper()->buildLocationDomainObjectsOnSearchResult($result, $languageFilter); |
252
|
|
|
$this->assertIsArray($missingLocations); |
253
|
|
|
|
254
|
|
|
if (!$missing) { |
255
|
|
|
$this->assertEmpty($missingLocations); |
256
|
|
|
} else { |
257
|
|
|
$this->assertNotEmpty($missingLocations); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
$this->assertCount($missing, $missingLocations); |
261
|
|
|
$this->assertEquals($spiResult->totalCount - $missing, $result->totalCount); |
262
|
|
|
$this->assertCount(count($spiResult->searchHits) - $missing, $result->searchHits); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Returns ContentDomainMapper. |
267
|
|
|
* |
268
|
|
|
* @return \eZ\Publish\Core\Repository\Mapper\ContentDomainMapper |
269
|
|
|
*/ |
270
|
|
|
protected function getContentDomainMapper(): ContentDomainMapper |
271
|
|
|
{ |
272
|
|
|
return new ContentDomainMapper( |
273
|
|
|
$this->getContentHandlerMock(), |
274
|
|
|
$this->getPersistenceMockHandler('Content\\Location\\Handler'), |
275
|
|
|
$this->getTypeHandlerMock(), |
276
|
|
|
$this->getContentTypeDomainMapperMock(), |
277
|
|
|
$this->getLanguageHandlerMock(), |
278
|
|
|
$this->getFieldTypeRegistryMock(), |
279
|
|
|
$this->getThumbnailStrategy(), |
280
|
|
|
$this->getProxyFactoryMock() |
281
|
|
|
); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* @return \eZ\Publish\SPI\Persistence\Content\Handler|\PHPUnit\Framework\MockObject\MockObject |
286
|
|
|
*/ |
287
|
|
|
protected function getContentHandlerMock() |
288
|
|
|
{ |
289
|
|
|
return $this->getPersistenceMockHandler('Content\\Handler'); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* @return \eZ\Publish\SPI\Persistence\Content\Language\Handler|\PHPUnit\Framework\MockObject\MockObject |
294
|
|
|
*/ |
295
|
|
|
protected function getLanguageHandlerMock() |
296
|
|
|
{ |
297
|
|
|
return $this->getPersistenceMockHandler('Content\\Language\\Handler'); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* @return \eZ\Publish\SPI\Persistence\Content\Type\Handler|\PHPUnit\Framework\MockObject\MockObject |
302
|
|
|
*/ |
303
|
|
|
protected function getTypeHandlerMock() |
304
|
|
|
{ |
305
|
|
|
return $this->getPersistenceMockHandler('Content\\Type\\Handler'); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
protected function getProxyFactoryMock(): ProxyDomainMapperInterface |
309
|
|
|
{ |
310
|
|
|
return $this->createMock(ProxyDomainMapperInterface::class); |
311
|
|
|
} |
312
|
|
|
} |
313
|
|
|
|