Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like ContentTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ContentTest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
59 | class ContentTest extends BaseServiceMockTest |
||
60 | { |
||
61 | /** |
||
62 | * Represents empty Field Value. |
||
63 | */ |
||
64 | const EMPTY_FIELD_VALUE = 'empty'; |
||
65 | |||
66 | /** |
||
67 | * Test for the __construct() method. |
||
68 | * |
||
69 | * @covers \eZ\Publish\Core\Repository\ContentService::__construct |
||
70 | */ |
||
71 | public function testConstructor(): void |
||
72 | { |
||
73 | $repositoryMock = $this->getRepositoryMock(); |
||
74 | /** @var \eZ\Publish\SPI\Persistence\Handler $persistenceHandlerMock */ |
||
75 | $persistenceHandlerMock = $this->getPersistenceMockHandler('Handler'); |
||
76 | $domainMapperMock = $this->getDomainMapperMock(); |
||
77 | $relationProcessorMock = $this->getRelationProcessorMock(); |
||
78 | $nameSchemaServiceMock = $this->getNameSchemaServiceMock(); |
||
79 | $fieldTypeRegistryMock = $this->getFieldTypeRegistryMock(); |
||
80 | $permissionResolverMock = $this->getPermissionResolverMock(); |
||
81 | $settings = ['default_version_archive_limit' => 10]; |
||
82 | |||
83 | $service = new ContentService( |
||
|
|||
84 | $repositoryMock, |
||
85 | $persistenceHandlerMock, |
||
86 | $domainMapperMock, |
||
87 | $relationProcessorMock, |
||
88 | $nameSchemaServiceMock, |
||
89 | $fieldTypeRegistryMock, |
||
90 | $permissionResolverMock, |
||
91 | $settings |
||
92 | ); |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Test for the loadVersionInfo() method, of published version. |
||
97 | * |
||
98 | * @covers \eZ\Publish\Core\Repository\ContentService::loadVersionInfoById |
||
99 | */ |
||
100 | public function testLoadVersionInfoById() |
||
101 | { |
||
102 | $repository = $this->getRepositoryMock(); |
||
103 | $contentServiceMock = $this->getPartlyMockedContentService(['loadContentInfo']); |
||
104 | /** @var \PHPUnit\Framework\MockObject\MockObject $contentHandler */ |
||
105 | $contentHandler = $this->getPersistenceMock()->contentHandler(); |
||
106 | $domainMapperMock = $this->getDomainMapperMock(); |
||
107 | $versionInfoMock = $this->createMock(APIVersionInfo::class); |
||
108 | $permissionResolver = $this->getPermissionResolverMock(); |
||
109 | |||
110 | $versionInfoMock->expects($this->once()) |
||
111 | ->method('isPublished') |
||
112 | ->willReturn(true); |
||
113 | |||
114 | $contentServiceMock->expects($this->never()) |
||
115 | ->method('loadContentInfo'); |
||
116 | |||
117 | $contentHandler->expects($this->once()) |
||
118 | ->method('loadVersionInfo') |
||
119 | ->with( |
||
120 | $this->equalTo(42), |
||
121 | $this->equalTo(null) |
||
122 | )->will( |
||
123 | $this->returnValue(new SPIVersionInfo()) |
||
124 | ); |
||
125 | |||
126 | $domainMapperMock->expects($this->once()) |
||
127 | ->method('buildVersionInfoDomainObject') |
||
128 | ->with(new SPIVersionInfo()) |
||
129 | ->will($this->returnValue($versionInfoMock)); |
||
130 | |||
131 | $permissionResolver->expects($this->once()) |
||
132 | ->method('canUser') |
||
133 | ->with( |
||
134 | $this->equalTo('content'), |
||
135 | $this->equalTo('read'), |
||
136 | $this->equalTo($versionInfoMock) |
||
137 | )->will($this->returnValue(true)); |
||
138 | |||
139 | $result = $contentServiceMock->loadVersionInfoById(42); |
||
140 | |||
141 | $this->assertEquals($versionInfoMock, $result); |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * Test for the loadVersionInfo() method, of a draft. |
||
146 | * |
||
147 | * @depends testLoadVersionInfoById |
||
148 | * @covers \eZ\Publish\Core\Repository\ContentService::loadVersionInfoById |
||
149 | */ |
||
150 | public function testLoadVersionInfoByIdAndVersionNumber() |
||
151 | { |
||
152 | $repository = $this->getRepositoryMock(); |
||
153 | $contentServiceMock = $this->getPartlyMockedContentService(['loadContentInfo']); |
||
154 | /** @var \PHPUnit_Framework_MockObject_MockObject $contentHandler */ |
||
155 | $contentHandler = $this->getPersistenceMock()->contentHandler(); |
||
156 | $domainMapperMock = $this->getDomainMapperMock(); |
||
157 | $versionInfoMock = $this->createMock(APIVersionInfo::class); |
||
158 | |||
159 | $versionInfoMock->expects($this->any()) |
||
160 | ->method('__get') |
||
161 | ->with('status') |
||
162 | ->willReturn(APIVersionInfo::STATUS_DRAFT); |
||
163 | |||
164 | $contentServiceMock->expects($this->never()) |
||
165 | ->method('loadContentInfo'); |
||
166 | |||
167 | $contentHandler->expects($this->once()) |
||
168 | ->method('loadVersionInfo') |
||
169 | ->with( |
||
170 | $this->equalTo(42), |
||
171 | $this->equalTo(2) |
||
172 | )->willReturn(new SPIVersionInfo()); |
||
173 | |||
174 | $domainMapperMock->expects($this->once()) |
||
175 | ->method('buildVersionInfoDomainObject') |
||
176 | ->with(new SPIVersionInfo()) |
||
177 | ->willReturn($versionInfoMock); |
||
178 | |||
179 | $repository->expects($this->once()) |
||
180 | ->method('canUser') |
||
181 | ->with( |
||
182 | $this->equalTo('content'), |
||
183 | $this->equalTo('versionread'), |
||
184 | $this->equalTo($versionInfoMock) |
||
185 | )->willReturn(true); |
||
186 | |||
187 | $result = $contentServiceMock->loadVersionInfoById(42, 2); |
||
188 | |||
189 | $this->assertEquals($versionInfoMock, $result); |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * Test for the loadVersionInfo() method. |
||
194 | * |
||
195 | * @covers \eZ\Publish\Core\Repository\ContentService::loadVersionInfoById |
||
196 | */ |
||
197 | public function testLoadVersionInfoByIdThrowsNotFoundException() |
||
198 | { |
||
199 | $this->expectException(\eZ\Publish\Core\Base\Exceptions\NotFoundException::class); |
||
200 | |||
201 | $contentServiceMock = $this->getPartlyMockedContentService(['loadContentInfo']); |
||
202 | /** @var \PHPUnit\Framework\MockObject\MockObject $contentHandler */ |
||
203 | $contentHandler = $this->getPersistenceMock()->contentHandler(); |
||
204 | |||
205 | $contentHandler->expects($this->once()) |
||
206 | ->method('loadVersionInfo') |
||
207 | ->with( |
||
208 | $this->equalTo(42), |
||
209 | $this->equalTo(24) |
||
210 | )->will( |
||
211 | $this->throwException( |
||
212 | new NotFoundException( |
||
213 | 'Content', |
||
214 | [ |
||
215 | 'contentId' => 42, |
||
216 | 'versionNo' => 24, |
||
217 | ] |
||
218 | ) |
||
219 | ) |
||
220 | ); |
||
221 | |||
222 | $contentServiceMock->loadVersionInfoById(42, 24); |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * Test for the loadVersionInfo() method. |
||
227 | * |
||
228 | * @covers \eZ\Publish\Core\Repository\ContentService::loadVersionInfoById |
||
229 | */ |
||
230 | public function testLoadVersionInfoByIdThrowsUnauthorizedExceptionNonPublishedVersion() |
||
231 | { |
||
232 | $this->expectException(\eZ\Publish\Core\Base\Exceptions\UnauthorizedException::class); |
||
233 | |||
234 | $repository = $this->getRepositoryMock(); |
||
235 | $contentServiceMock = $this->getPartlyMockedContentService(); |
||
236 | /** @var \PHPUnit\Framework\MockObject\MockObject $contentHandler */ |
||
237 | $contentHandler = $this->getPersistenceMock()->contentHandler(); |
||
238 | $domainMapperMock = $this->getDomainMapperMock(); |
||
239 | $versionInfoMock = $this->createMock(APIVersionInfo::class); |
||
240 | $permissionResolver = $this->getPermissionResolverMock(); |
||
241 | |||
242 | $versionInfoMock->expects($this->any()) |
||
243 | ->method('isPublished') |
||
244 | ->willReturn(false); |
||
245 | |||
246 | $contentHandler->expects($this->once()) |
||
247 | ->method('loadVersionInfo') |
||
248 | ->with( |
||
249 | $this->equalTo(42), |
||
250 | $this->equalTo(24) |
||
251 | )->will( |
||
252 | $this->returnValue(new SPIVersionInfo()) |
||
253 | ); |
||
254 | |||
255 | $domainMapperMock->expects($this->once()) |
||
256 | ->method('buildVersionInfoDomainObject') |
||
257 | ->with(new SPIVersionInfo()) |
||
258 | ->will($this->returnValue($versionInfoMock)); |
||
259 | |||
260 | $permissionResolver->expects($this->once()) |
||
261 | ->method('canUser') |
||
262 | ->with( |
||
263 | $this->equalTo('content'), |
||
264 | $this->equalTo('versionread'), |
||
265 | $this->equalTo($versionInfoMock) |
||
266 | )->will($this->returnValue(false)); |
||
267 | |||
268 | $contentServiceMock->loadVersionInfoById(42, 24); |
||
269 | } |
||
270 | |||
271 | /** |
||
272 | * Test for the loadVersionInfo() method. |
||
273 | * |
||
274 | * @covers \eZ\Publish\Core\Repository\ContentService::loadVersionInfoById |
||
275 | */ |
||
276 | View Code Duplication | public function testLoadVersionInfoByIdPublishedVersion() |
|
277 | { |
||
278 | $repository = $this->getRepositoryMock(); |
||
279 | $contentServiceMock = $this->getPartlyMockedContentService(); |
||
280 | /** @var \PHPUnit\Framework\MockObject\MockObject $contentHandler */ |
||
281 | $contentHandler = $this->getPersistenceMock()->contentHandler(); |
||
282 | $domainMapperMock = $this->getDomainMapperMock(); |
||
283 | $versionInfoMock = $this->createMock(APIVersionInfo::class); |
||
284 | $permissionResolver = $this->getPermissionResolverMock(); |
||
285 | |||
286 | $versionInfoMock->expects($this->once()) |
||
287 | ->method('isPublished') |
||
288 | ->willReturn(true); |
||
289 | |||
290 | $contentHandler->expects($this->once()) |
||
291 | ->method('loadVersionInfo') |
||
292 | ->with( |
||
293 | $this->equalTo(42), |
||
294 | $this->equalTo(24) |
||
295 | )->will( |
||
296 | $this->returnValue(new SPIVersionInfo()) |
||
297 | ); |
||
298 | |||
299 | $domainMapperMock->expects($this->once()) |
||
300 | ->method('buildVersionInfoDomainObject') |
||
301 | ->with(new SPIVersionInfo()) |
||
302 | ->will($this->returnValue($versionInfoMock)); |
||
303 | |||
304 | $permissionResolver->expects($this->once()) |
||
305 | ->method('canUser') |
||
306 | ->with( |
||
307 | $this->equalTo('content'), |
||
308 | $this->equalTo('read'), |
||
309 | $this->equalTo($versionInfoMock) |
||
310 | )->will($this->returnValue(true)); |
||
311 | |||
312 | $result = $contentServiceMock->loadVersionInfoById(42, 24); |
||
313 | |||
314 | $this->assertEquals($versionInfoMock, $result); |
||
315 | } |
||
316 | |||
317 | /** |
||
318 | * Test for the loadVersionInfo() method. |
||
319 | * |
||
320 | * @covers \eZ\Publish\Core\Repository\ContentService::loadVersionInfoById |
||
321 | */ |
||
322 | View Code Duplication | public function testLoadVersionInfoByIdNonPublishedVersion() |
|
323 | { |
||
324 | $repository = $this->getRepositoryMock(); |
||
325 | $contentServiceMock = $this->getPartlyMockedContentService(); |
||
326 | /** @var \PHPUnit\Framework\MockObject\MockObject $contentHandler */ |
||
327 | $contentHandler = $this->getPersistenceMock()->contentHandler(); |
||
328 | $domainMapperMock = $this->getDomainMapperMock(); |
||
329 | $versionInfoMock = $this->createMock(APIVersionInfo::class); |
||
330 | $permissionResolver = $this->getPermissionResolverMock(); |
||
331 | |||
332 | $versionInfoMock->expects($this->once()) |
||
333 | ->method('isPublished') |
||
334 | ->willReturn(false); |
||
335 | |||
336 | $contentHandler->expects($this->once()) |
||
337 | ->method('loadVersionInfo') |
||
338 | ->with( |
||
339 | $this->equalTo(42), |
||
340 | $this->equalTo(24) |
||
341 | )->will( |
||
342 | $this->returnValue(new SPIVersionInfo()) |
||
343 | ); |
||
344 | |||
345 | $domainMapperMock->expects($this->once()) |
||
346 | ->method('buildVersionInfoDomainObject') |
||
347 | ->with(new SPIVersionInfo()) |
||
348 | ->will($this->returnValue($versionInfoMock)); |
||
349 | |||
350 | $permissionResolver->expects($this->once()) |
||
351 | ->method('canUser') |
||
352 | ->with( |
||
353 | $this->equalTo('content'), |
||
354 | $this->equalTo('versionread'), |
||
355 | $this->equalTo($versionInfoMock) |
||
356 | )->will($this->returnValue(true)); |
||
357 | |||
358 | $result = $contentServiceMock->loadVersionInfoById(42, 24); |
||
359 | |||
360 | $this->assertEquals($versionInfoMock, $result); |
||
361 | } |
||
362 | |||
363 | /** |
||
364 | * Test for the loadVersionInfo() method. |
||
365 | * |
||
366 | * @covers \eZ\Publish\Core\Repository\ContentService::loadVersionInfo |
||
367 | * @depends eZ\Publish\Core\Repository\Tests\Service\Mock\ContentTest::testLoadVersionInfoById |
||
368 | * @depends eZ\Publish\Core\Repository\Tests\Service\Mock\ContentTest::testLoadVersionInfoByIdThrowsNotFoundException |
||
369 | * @depends eZ\Publish\Core\Repository\Tests\Service\Mock\ContentTest::testLoadVersionInfoByIdThrowsUnauthorizedExceptionNonPublishedVersion |
||
370 | * @depends eZ\Publish\Core\Repository\Tests\Service\Mock\ContentTest::testLoadVersionInfoByIdPublishedVersion |
||
371 | * @depends eZ\Publish\Core\Repository\Tests\Service\Mock\ContentTest::testLoadVersionInfoByIdNonPublishedVersion |
||
372 | */ |
||
373 | public function testLoadVersionInfo() |
||
374 | { |
||
375 | $contentServiceMock = $this->getPartlyMockedContentService( |
||
376 | ['loadVersionInfoById'] |
||
377 | ); |
||
378 | $contentServiceMock->expects( |
||
379 | $this->once() |
||
380 | )->method( |
||
381 | 'loadVersionInfoById' |
||
382 | )->with( |
||
383 | $this->equalTo(42), |
||
384 | $this->equalTo(7) |
||
385 | )->will( |
||
386 | $this->returnValue('result') |
||
387 | ); |
||
388 | |||
389 | $result = $contentServiceMock->loadVersionInfo( |
||
390 | new ContentInfo(['id' => 42]), |
||
391 | 7 |
||
392 | ); |
||
393 | |||
394 | $this->assertEquals('result', $result); |
||
395 | } |
||
396 | |||
397 | public function testLoadContent() |
||
398 | { |
||
399 | $repository = $this->getRepositoryMock(); |
||
400 | $contentService = $this->getPartlyMockedContentService(['internalLoadContent']); |
||
401 | $content = $this->createMock(APIContent::class); |
||
402 | $versionInfo = $this->createMock(APIVersionInfo::class); |
||
403 | $permissionResolver = $this->getPermissionResolverMock(); |
||
404 | |||
405 | $content |
||
406 | ->expects($this->once()) |
||
407 | ->method('getVersionInfo') |
||
408 | ->will($this->returnValue($versionInfo)); |
||
409 | $versionInfo |
||
410 | ->expects($this->once()) |
||
411 | ->method('isPublished') |
||
412 | ->willReturn(true); |
||
413 | $contentId = 123; |
||
414 | $contentService |
||
415 | ->expects($this->once()) |
||
416 | ->method('internalLoadContent') |
||
417 | ->with($contentId) |
||
418 | ->will($this->returnValue($content)); |
||
419 | |||
420 | $permissionResolver |
||
421 | ->expects($this->once()) |
||
422 | ->method('canUser') |
||
423 | ->with('content', 'read', $content) |
||
424 | ->will($this->returnValue(true)); |
||
425 | |||
426 | $this->assertSame($content, $contentService->loadContent($contentId)); |
||
427 | } |
||
428 | |||
429 | View Code Duplication | public function testLoadContentNonPublished() |
|
430 | { |
||
431 | $repository = $this->getRepositoryMock(); |
||
432 | $contentService = $this->getPartlyMockedContentService(['internalLoadContent']); |
||
433 | $content = $this->createMock(APIContent::class); |
||
434 | $versionInfo = $this->createMock(APIVersionInfo::class); |
||
435 | $permissionResolver = $this->getPermissionResolverMock(); |
||
436 | |||
437 | $content |
||
438 | ->expects($this->once()) |
||
439 | ->method('getVersionInfo') |
||
440 | ->will($this->returnValue($versionInfo)); |
||
441 | $contentId = 123; |
||
442 | $contentService |
||
443 | ->expects($this->once()) |
||
444 | ->method('internalLoadContent') |
||
445 | ->with($contentId) |
||
446 | ->will($this->returnValue($content)); |
||
447 | |||
448 | $permissionResolver |
||
449 | ->expects($this->exactly(2)) |
||
450 | ->method('canUser') |
||
451 | ->will( |
||
452 | $this->returnValueMap( |
||
453 | [ |
||
454 | ['content', 'read', $content, [], true], |
||
455 | ['content', 'versionread', $content, [], true], |
||
456 | ] |
||
457 | ) |
||
458 | ); |
||
459 | |||
460 | $this->assertSame($content, $contentService->loadContent($contentId)); |
||
461 | } |
||
462 | |||
463 | public function testLoadContentUnauthorized() |
||
464 | { |
||
465 | $this->expectException(\eZ\Publish\Core\Base\Exceptions\UnauthorizedException::class); |
||
466 | |||
467 | $repository = $this->getRepositoryMock(); |
||
468 | $permissionResolver = $this->getPermissionResolverMock(); |
||
469 | |||
470 | $contentService = $this->getPartlyMockedContentService(['internalLoadContent']); |
||
471 | $content = $this->createMock(APIContent::class); |
||
472 | $contentId = 123; |
||
473 | $contentService |
||
474 | ->expects($this->once()) |
||
475 | ->method('internalLoadContent') |
||
476 | ->with($contentId) |
||
477 | ->will($this->returnValue($content)); |
||
478 | |||
479 | $permissionResolver |
||
480 | ->expects($this->once()) |
||
481 | ->method('canUser') |
||
482 | ->with('content', 'read', $content) |
||
483 | ->will($this->returnValue(false)); |
||
484 | |||
485 | $contentService->loadContent($contentId); |
||
486 | } |
||
487 | |||
488 | View Code Duplication | public function testLoadContentNotPublishedStatusUnauthorized() |
|
489 | { |
||
490 | $this->expectException(\eZ\Publish\Core\Base\Exceptions\UnauthorizedException::class); |
||
491 | |||
492 | $repository = $this->getRepositoryMock(); |
||
493 | $permissionResolver = $this->getPermissionResolverMock(); |
||
494 | $contentService = $this->getPartlyMockedContentService(['internalLoadContent']); |
||
495 | $content = $this->createMock(APIContent::class); |
||
496 | $versionInfo = $this |
||
497 | ->getMockBuilder(APIVersionInfo::class) |
||
498 | ->getMockForAbstractClass(); |
||
499 | $content |
||
500 | ->expects($this->once()) |
||
501 | ->method('getVersionInfo') |
||
502 | ->will($this->returnValue($versionInfo)); |
||
503 | $contentId = 123; |
||
504 | $contentService |
||
505 | ->expects($this->once()) |
||
506 | ->method('internalLoadContent') |
||
507 | ->with($contentId) |
||
508 | ->will($this->returnValue($content)); |
||
509 | |||
510 | $permissionResolver |
||
511 | ->expects($this->exactly(2)) |
||
512 | ->method('canUser') |
||
513 | ->will( |
||
514 | $this->returnValueMap( |
||
515 | [ |
||
516 | ['content', 'read', $content, [], true], |
||
517 | ['content', 'versionread', $content, [], false], |
||
518 | ] |
||
519 | ) |
||
520 | ); |
||
521 | |||
522 | $contentService->loadContent($contentId); |
||
523 | } |
||
524 | |||
525 | /** |
||
526 | * @dataProvider internalLoadContentProvider |
||
527 | */ |
||
528 | public function testInternalLoadContent($id, $languages, $versionNo, $isRemoteId, $useAlwaysAvailable) |
||
529 | { |
||
530 | $contentService = $this->getPartlyMockedContentService(); |
||
531 | /** @var \PHPUnit\Framework\MockObject\MockObject $contentHandler */ |
||
532 | $contentHandler = $this->getPersistenceMock()->contentHandler(); |
||
533 | $realId = $id; |
||
534 | |||
535 | if ($isRemoteId) { |
||
536 | $realId = 123; |
||
537 | $spiContentInfo = new SPIContentInfo(['currentVersionNo' => $versionNo ?: 7, 'id' => $realId]); |
||
538 | $contentHandler |
||
539 | ->expects($this->once()) |
||
540 | ->method('loadContentInfoByRemoteId') |
||
541 | ->with($id) |
||
542 | ->will($this->returnValue($spiContentInfo)); |
||
543 | } elseif (!empty($languages) && $useAlwaysAvailable) { |
||
544 | $spiContentInfo = new SPIContentInfo(['alwaysAvailable' => false]); |
||
545 | $contentHandler |
||
546 | ->expects($this->once()) |
||
547 | ->method('loadContentInfo') |
||
548 | ->with($id) |
||
549 | ->will($this->returnValue($spiContentInfo)); |
||
550 | } |
||
551 | |||
552 | $spiContent = new SPIContent([ |
||
553 | 'versionInfo' => new VersionInfo([ |
||
554 | 'contentInfo' => new ContentInfo(['id' => 42, 'contentTypeId' => 123]), |
||
555 | ]), |
||
556 | ]); |
||
557 | $contentHandler |
||
558 | ->expects($this->once()) |
||
559 | ->method('load') |
||
560 | ->with($realId, $versionNo, $languages) |
||
561 | ->willReturn($spiContent); |
||
562 | |||
563 | $content = $this->mockBuildContentDomainObject($spiContent, $languages); |
||
564 | |||
565 | $this->assertSame( |
||
566 | $content, |
||
567 | $contentService->internalLoadContent($id, $languages, $versionNo, $isRemoteId, $useAlwaysAvailable) |
||
568 | ); |
||
569 | } |
||
570 | |||
571 | public function internalLoadContentProvider() |
||
572 | { |
||
573 | return [ |
||
574 | [123, null, null, false, false], |
||
575 | [123, null, 456, false, false], |
||
576 | [456, null, 123, false, true], |
||
577 | [456, null, 2, false, false], |
||
578 | [456, ['eng-GB'], 2, false, true], |
||
579 | [456, ['eng-GB', 'fre-FR'], null, false, false], |
||
580 | [456, ['eng-GB', 'fre-FR', 'nor-NO'], 2, false, false], |
||
581 | // With remoteId |
||
582 | [123, null, null, true, false], |
||
583 | ['someRemoteId', null, 456, true, false], |
||
584 | [456, null, 123, true, false], |
||
585 | ['someRemoteId', null, 2, true, false], |
||
586 | ['someRemoteId', ['eng-GB'], 2, true, false], |
||
587 | [456, ['eng-GB', 'fre-FR'], null, true, false], |
||
588 | ['someRemoteId', ['eng-GB', 'fre-FR', 'nor-NO'], 2, true, false], |
||
589 | ]; |
||
590 | } |
||
591 | |||
592 | public function testInternalLoadContentNotFound() |
||
593 | { |
||
594 | $this->expectException(\eZ\Publish\Core\Base\Exceptions\NotFoundException::class); |
||
595 | |||
596 | $contentService = $this->getPartlyMockedContentService(); |
||
597 | /** @var \PHPUnit\Framework\MockObject\MockObject $contentHandler */ |
||
598 | $contentHandler = $this->getPersistenceMock()->contentHandler(); |
||
599 | $id = 123; |
||
600 | $versionNo = 7; |
||
601 | $languages = null; |
||
602 | $contentHandler |
||
603 | ->expects($this->once()) |
||
604 | ->method('load') |
||
605 | ->with($id, $versionNo, $languages) |
||
606 | ->will( |
||
607 | $this->throwException( |
||
608 | $this->createMock(APINotFoundException::class) |
||
609 | ) |
||
610 | ); |
||
611 | |||
612 | $contentService->internalLoadContent($id, $languages, $versionNo); |
||
613 | } |
||
614 | |||
615 | /** |
||
616 | * Test for the loadContentByContentInfo() method. |
||
617 | * |
||
618 | * @covers \eZ\Publish\Core\Repository\ContentService::loadContentByContentInfo |
||
619 | */ |
||
620 | public function testLoadContentByContentInfo() |
||
621 | { |
||
622 | $contentServiceMock = $this->getPartlyMockedContentService( |
||
623 | ['loadContent'] |
||
624 | ); |
||
625 | $contentServiceMock->expects( |
||
626 | $this->once() |
||
627 | )->method( |
||
628 | 'loadContent' |
||
629 | )->with( |
||
630 | $this->equalTo(42), |
||
631 | $this->equalTo(['cro-HR']), |
||
632 | $this->equalTo(7), |
||
633 | $this->equalTo(false) |
||
634 | )->will( |
||
635 | $this->returnValue('result') |
||
636 | ); |
||
637 | |||
638 | $result = $contentServiceMock->loadContentByContentInfo( |
||
639 | new ContentInfo(['id' => 42]), |
||
640 | ['cro-HR'], |
||
641 | 7 |
||
642 | ); |
||
643 | |||
644 | $this->assertEquals('result', $result); |
||
645 | } |
||
646 | |||
647 | /** |
||
648 | * Test for the loadContentByVersionInfo() method. |
||
649 | * |
||
650 | * @covers \eZ\Publish\Core\Repository\ContentService::loadContentByVersionInfo |
||
651 | */ |
||
652 | public function testLoadContentByVersionInfo() |
||
653 | { |
||
654 | $contentServiceMock = $this->getPartlyMockedContentService( |
||
655 | ['loadContent'] |
||
656 | ); |
||
657 | $contentServiceMock->expects( |
||
658 | $this->once() |
||
659 | )->method( |
||
660 | 'loadContent' |
||
661 | )->with( |
||
662 | $this->equalTo(42), |
||
663 | $this->equalTo(['cro-HR']), |
||
664 | $this->equalTo(7), |
||
665 | $this->equalTo(false) |
||
666 | )->will( |
||
667 | $this->returnValue('result') |
||
668 | ); |
||
669 | |||
670 | $result = $contentServiceMock->loadContentByVersionInfo( |
||
671 | new VersionInfo( |
||
672 | [ |
||
673 | 'contentInfo' => new ContentInfo(['id' => 42]), |
||
674 | 'versionNo' => 7, |
||
675 | ] |
||
676 | ), |
||
677 | ['cro-HR'] |
||
678 | ); |
||
679 | |||
680 | $this->assertEquals('result', $result); |
||
681 | } |
||
682 | |||
683 | /** |
||
684 | * Test for the deleteContent() method. |
||
685 | * |
||
686 | * @covers \eZ\Publish\Core\Repository\ContentService::deleteContent |
||
687 | */ |
||
688 | public function testDeleteContentThrowsUnauthorizedException() |
||
689 | { |
||
690 | $this->expectException(\eZ\Publish\Core\Base\Exceptions\UnauthorizedException::class); |
||
691 | |||
692 | $repository = $this->getRepositoryMock(); |
||
693 | $permissionResolver = $this->getPermissionResolverMock(); |
||
694 | $contentService = $this->getPartlyMockedContentService(['internalLoadContentInfo']); |
||
695 | $contentInfo = $this->createMock(APIContentInfo::class); |
||
696 | |||
697 | $contentInfo->expects($this->any()) |
||
698 | ->method('__get') |
||
699 | ->with('id') |
||
700 | ->will($this->returnValue(42)); |
||
701 | |||
702 | $contentService->expects($this->once()) |
||
703 | ->method('internalLoadContentInfo') |
||
704 | ->with(42) |
||
705 | ->will($this->returnValue($contentInfo)); |
||
706 | |||
707 | $permissionResolver->expects($this->once()) |
||
708 | ->method('canUser') |
||
709 | ->with('content', 'remove') |
||
710 | ->will($this->returnValue(false)); |
||
711 | |||
712 | /* @var \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo */ |
||
713 | $contentService->deleteContent($contentInfo); |
||
714 | } |
||
715 | |||
716 | /** |
||
717 | * Test for the deleteContent() method. |
||
718 | * |
||
719 | * @covers \eZ\Publish\Core\Repository\ContentService::deleteContent |
||
720 | */ |
||
721 | public function testDeleteContent() |
||
722 | { |
||
723 | $repository = $this->getRepositoryMock(); |
||
724 | $permissionResolver = $this->getPermissionResolverMock(); |
||
725 | |||
726 | $permissionResolver->expects($this->once()) |
||
727 | ->method('canUser') |
||
728 | ->with('content', 'remove') |
||
729 | ->will($this->returnValue(true)); |
||
730 | |||
731 | $contentService = $this->getPartlyMockedContentService(['internalLoadContentInfo']); |
||
732 | /** @var \PHPUnit\Framework\MockObject\MockObject $urlAliasHandler */ |
||
733 | $urlAliasHandler = $this->getPersistenceMock()->urlAliasHandler(); |
||
734 | /** @var \PHPUnit\Framework\MockObject\MockObject $locationHandler */ |
||
735 | $locationHandler = $this->getPersistenceMock()->locationHandler(); |
||
736 | /** @var \PHPUnit\Framework\MockObject\MockObject $contentHandler */ |
||
737 | $contentHandler = $this->getPersistenceMock()->contentHandler(); |
||
738 | |||
739 | $contentInfo = $this->createMock(APIContentInfo::class); |
||
740 | |||
741 | $contentService->expects($this->once()) |
||
742 | ->method('internalLoadContentInfo') |
||
743 | ->with(42) |
||
744 | ->will($this->returnValue($contentInfo)); |
||
745 | |||
746 | $contentInfo->expects($this->any()) |
||
747 | ->method('__get') |
||
748 | ->with('id') |
||
749 | ->will($this->returnValue(42)); |
||
750 | |||
751 | $repository->expects($this->once())->method('beginTransaction'); |
||
752 | |||
753 | $spiLocations = [ |
||
754 | new SPILocation(['id' => 1]), |
||
755 | new SPILocation(['id' => 2]), |
||
756 | ]; |
||
757 | $locationHandler->expects($this->once()) |
||
758 | ->method('loadLocationsByContent') |
||
759 | ->with(42) |
||
760 | ->will($this->returnValue($spiLocations)); |
||
761 | |||
762 | $contentHandler->expects($this->once()) |
||
763 | ->method('deleteContent') |
||
764 | ->with(42); |
||
765 | |||
766 | foreach ($spiLocations as $index => $spiLocation) { |
||
767 | $urlAliasHandler->expects($this->at($index)) |
||
768 | ->method('locationDeleted') |
||
769 | ->with($spiLocation->id); |
||
770 | } |
||
771 | |||
772 | $repository->expects($this->once())->method('commit'); |
||
773 | |||
774 | /* @var \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo */ |
||
775 | $contentService->deleteContent($contentInfo); |
||
776 | } |
||
777 | |||
778 | /** |
||
779 | * Test for the deleteContent() method. |
||
780 | * |
||
781 | * @covers \eZ\Publish\Core\Repository\ContentService::deleteContent |
||
782 | */ |
||
783 | public function testDeleteContentWithRollback() |
||
784 | { |
||
785 | $this->expectException(\Exception::class); |
||
786 | |||
787 | $repository = $this->getRepositoryMock(); |
||
788 | $permissionResolver = $this->getPermissionResolverMock(); |
||
789 | |||
790 | $permissionResolver->expects($this->once()) |
||
791 | ->method('canUser') |
||
792 | ->with('content', 'remove') |
||
793 | ->will($this->returnValue(true)); |
||
794 | |||
795 | $contentService = $this->getPartlyMockedContentService(['internalLoadContentInfo']); |
||
796 | /** @var \PHPUnit\Framework\MockObject\MockObject $locationHandler */ |
||
797 | $locationHandler = $this->getPersistenceMock()->locationHandler(); |
||
798 | |||
799 | $contentInfo = $this->createMock(APIContentInfo::class); |
||
800 | |||
801 | $contentService->expects($this->once()) |
||
802 | ->method('internalLoadContentInfo') |
||
803 | ->with(42) |
||
804 | ->will($this->returnValue($contentInfo)); |
||
805 | |||
806 | $contentInfo->expects($this->any()) |
||
807 | ->method('__get') |
||
808 | ->with('id') |
||
809 | ->will($this->returnValue(42)); |
||
810 | |||
811 | $repository->expects($this->once())->method('beginTransaction'); |
||
812 | |||
813 | $locationHandler->expects($this->once()) |
||
814 | ->method('loadLocationsByContent') |
||
815 | ->with(42) |
||
816 | ->will($this->throwException(new \Exception())); |
||
817 | |||
818 | $repository->expects($this->once())->method('rollback'); |
||
819 | |||
820 | /* @var \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo */ |
||
821 | $contentService->deleteContent($contentInfo); |
||
822 | } |
||
823 | |||
824 | /** |
||
825 | * Test for the deleteVersion() method. |
||
826 | * |
||
827 | * @covers \eZ\Publish\Core\Repository\ContentService::deleteVersion |
||
828 | */ |
||
829 | public function testDeleteVersionThrowsBadStateExceptionLastVersion() |
||
830 | { |
||
831 | $this->expectException(\eZ\Publish\API\Repository\Exceptions\BadStateException::class); |
||
832 | |||
833 | $repository = $this->getRepositoryMock(); |
||
834 | $permissionResolver = $this->getPermissionResolverMock(); |
||
835 | |||
836 | $permissionResolver |
||
837 | ->expects($this->once()) |
||
838 | ->method('canUser') |
||
839 | ->with('content', 'versionremove') |
||
840 | ->will($this->returnValue(true)); |
||
841 | $repository |
||
842 | ->expects($this->never()) |
||
843 | ->method('beginTransaction'); |
||
844 | |||
845 | $contentService = $this->getPartlyMockedContentService(); |
||
846 | /** @var \PHPUnit\Framework\MockObject\MockObject $contentHandler */ |
||
847 | $contentHandler = $this->getPersistenceMock()->contentHandler(); |
||
848 | $contentInfo = $this->createMock(APIContentInfo::class); |
||
849 | $versionInfo = $this->createMock(APIVersionInfo::class); |
||
850 | |||
851 | $contentInfo |
||
852 | ->expects($this->any()) |
||
853 | ->method('__get') |
||
854 | ->with('id') |
||
855 | ->will($this->returnValue(42)); |
||
856 | |||
857 | $versionInfo |
||
858 | ->expects($this->any()) |
||
859 | ->method('__get') |
||
860 | ->will( |
||
861 | $this->returnValueMap( |
||
862 | [ |
||
863 | ['versionNo', 123], |
||
864 | ['contentInfo', $contentInfo], |
||
865 | ] |
||
866 | ) |
||
867 | ); |
||
868 | $versionInfo |
||
869 | ->expects($this->once()) |
||
870 | ->method('isPublished') |
||
871 | ->willReturn(false); |
||
872 | |||
873 | $contentHandler |
||
874 | ->expects($this->once()) |
||
875 | ->method('listVersions') |
||
876 | ->with(42) |
||
877 | ->will($this->returnValue(['version'])); |
||
878 | |||
879 | /* @var \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo */ |
||
880 | $contentService->deleteVersion($versionInfo); |
||
881 | } |
||
882 | |||
883 | /** |
||
884 | * Test for the createContent() method. |
||
885 | * |
||
886 | * @covers \eZ\Publish\Core\Repository\ContentService::createContent |
||
887 | */ |
||
888 | View Code Duplication | public function testCreateContentThrowsInvalidArgumentExceptionMainLanguageCodeNotSet() |
|
889 | { |
||
890 | $this->expectException(\eZ\Publish\API\Repository\Exceptions\InvalidArgumentException::class); |
||
891 | $this->expectExceptionMessage('Argument \'$contentCreateStruct\' is invalid: \'mainLanguageCode\' property must be set'); |
||
892 | |||
893 | $mockedService = $this->getPartlyMockedContentService(); |
||
894 | $mockedService->createContent(new ContentCreateStruct(), []); |
||
895 | } |
||
896 | |||
897 | /** |
||
898 | * Test for the createContent() method. |
||
899 | * |
||
900 | * @covers \eZ\Publish\Core\Repository\ContentService::createContent |
||
901 | */ |
||
902 | View Code Duplication | public function testCreateContentThrowsInvalidArgumentExceptionContentTypeNotSet() |
|
903 | { |
||
904 | $this->expectException(\eZ\Publish\API\Repository\Exceptions\InvalidArgumentException::class); |
||
905 | $this->expectExceptionMessage('Argument \'$contentCreateStruct\' is invalid: \'contentType\' property must be set'); |
||
906 | |||
907 | $mockedService = $this->getPartlyMockedContentService(); |
||
908 | $mockedService->createContent( |
||
909 | new ContentCreateStruct(['mainLanguageCode' => 'eng-US']), |
||
910 | [] |
||
911 | ); |
||
912 | } |
||
913 | |||
914 | /** |
||
915 | * Test for the createContent() method. |
||
916 | * |
||
917 | * @covers \eZ\Publish\Core\Repository\ContentService::createContent |
||
918 | */ |
||
919 | public function testCreateContentThrowsUnauthorizedException() |
||
920 | { |
||
921 | $this->expectException(\eZ\Publish\API\Repository\Exceptions\UnauthorizedException::class); |
||
922 | |||
923 | $repositoryMock = $this->getRepositoryMock(); |
||
924 | |||
925 | $permissionResolver = $this->getPermissionResolverMock(); |
||
926 | $permissionResolver->expects($this->once()) |
||
927 | ->method('getCurrentUserReference') |
||
928 | ->will($this->returnValue(new UserReference(169))); |
||
929 | |||
930 | $mockedService = $this->getPartlyMockedContentService(); |
||
931 | $contentTypeServiceMock = $this->getContentTypeServiceMock(); |
||
932 | $contentType = new ContentType( |
||
933 | [ |
||
934 | 'id' => 123, |
||
935 | 'fieldDefinitions' => [], |
||
936 | ] |
||
937 | ); |
||
938 | $contentCreateStruct = new ContentCreateStruct( |
||
939 | [ |
||
940 | 'ownerId' => 169, |
||
941 | 'alwaysAvailable' => false, |
||
942 | 'mainLanguageCode' => 'eng-US', |
||
943 | 'contentType' => $contentType, |
||
944 | ] |
||
945 | ); |
||
946 | |||
947 | $contentTypeServiceMock->expects($this->once()) |
||
948 | ->method('loadContentType') |
||
949 | ->with($this->equalTo(123)) |
||
950 | ->will($this->returnValue($contentType)); |
||
951 | |||
952 | $repositoryMock->expects($this->once()) |
||
953 | ->method('getContentTypeService') |
||
954 | ->will($this->returnValue($contentTypeServiceMock)); |
||
955 | |||
956 | $permissionResolver->expects($this->once()) |
||
957 | ->method('canUser') |
||
958 | ->with( |
||
959 | $this->equalTo('content'), |
||
960 | $this->equalTo('create'), |
||
961 | $this->isInstanceOf(get_class($contentCreateStruct)), |
||
962 | $this->equalTo([]) |
||
963 | )->will($this->returnValue(false)); |
||
964 | |||
965 | $mockedService->createContent( |
||
966 | new ContentCreateStruct( |
||
967 | [ |
||
968 | 'mainLanguageCode' => 'eng-US', |
||
969 | 'contentType' => $contentType, |
||
970 | ] |
||
971 | ), |
||
972 | [] |
||
973 | ); |
||
974 | } |
||
975 | |||
976 | /** |
||
977 | * Test for the createContent() method. |
||
978 | * |
||
979 | * @covers \eZ\Publish\Core\Repository\ContentService::createContent |
||
980 | * @exceptionMessage Argument '$contentCreateStruct' is invalid: Another content with remoteId 'faraday' exists |
||
981 | */ |
||
982 | public function testCreateContentThrowsInvalidArgumentExceptionDuplicateRemoteId() |
||
983 | { |
||
984 | $this->expectException(\eZ\Publish\API\Repository\Exceptions\InvalidArgumentException::class); |
||
985 | |||
986 | $repositoryMock = $this->getRepositoryMock(); |
||
987 | $permissionResolverMock = $this->getPermissionResolverMock(); |
||
988 | $permissionResolverMock |
||
989 | ->expects($this->once()) |
||
990 | ->method('getCurrentUserReference') |
||
991 | ->willReturn($this->createMock(UserReference::class)); |
||
992 | |||
993 | $mockedService = $this->getPartlyMockedContentService(['loadContentByRemoteId']); |
||
994 | $contentTypeServiceMock = $this->getContentTypeServiceMock(); |
||
995 | $contentType = new ContentType( |
||
996 | [ |
||
997 | 'id' => 123, |
||
998 | 'fieldDefinitions' => [], |
||
999 | ] |
||
1000 | ); |
||
1001 | $contentCreateStruct = new ContentCreateStruct( |
||
1002 | [ |
||
1003 | 'ownerId' => 169, |
||
1004 | 'alwaysAvailable' => false, |
||
1005 | 'remoteId' => 'faraday', |
||
1006 | 'mainLanguageCode' => 'eng-US', |
||
1007 | 'contentType' => $contentType, |
||
1008 | ] |
||
1009 | ); |
||
1010 | |||
1011 | $contentTypeServiceMock->expects($this->once()) |
||
1012 | ->method('loadContentType') |
||
1013 | ->with($this->equalTo(123)) |
||
1014 | ->will($this->returnValue($contentType)); |
||
1015 | |||
1016 | $repositoryMock->expects($this->once()) |
||
1017 | ->method('getContentTypeService') |
||
1018 | ->will($this->returnValue($contentTypeServiceMock)); |
||
1019 | |||
1020 | $permissionResolverMock->expects($this->once()) |
||
1021 | ->method('canUser') |
||
1022 | ->with( |
||
1023 | $this->equalTo('content'), |
||
1024 | $this->equalTo('create'), |
||
1025 | $this->isInstanceOf(get_class($contentCreateStruct)), |
||
1026 | $this->equalTo([]) |
||
1027 | )->will($this->returnValue(true)); |
||
1028 | |||
1029 | $mockedService->expects($this->once()) |
||
1030 | ->method('loadContentByRemoteId') |
||
1031 | ->with($contentCreateStruct->remoteId) |
||
1032 | ->will($this->returnValue('Hello...')); |
||
1033 | |||
1034 | $mockedService->createContent( |
||
1035 | new ContentCreateStruct( |
||
1036 | [ |
||
1037 | 'remoteId' => 'faraday', |
||
1038 | 'mainLanguageCode' => 'eng-US', |
||
1039 | 'contentType' => $contentType, |
||
1040 | ] |
||
1041 | ), |
||
1042 | [] |
||
1043 | ); |
||
1044 | } |
||
1045 | |||
1046 | /** |
||
1047 | * @param string $mainLanguageCode |
||
1048 | * @param \eZ\Publish\API\Repository\Values\Content\Field[] $structFields |
||
1049 | * @param \eZ\Publish\API\Repository\Values\ContentType\FieldDefinition[] $fieldDefinitions |
||
1050 | * |
||
1051 | * @return array |
||
1052 | */ |
||
1053 | protected function mapStructFieldsForCreate($mainLanguageCode, $structFields, $fieldDefinitions) |
||
1054 | { |
||
1055 | $mappedFieldDefinitions = []; |
||
1056 | foreach ($fieldDefinitions as $fieldDefinition) { |
||
1057 | $mappedFieldDefinitions[$fieldDefinition->identifier] = $fieldDefinition; |
||
1058 | } |
||
1059 | |||
1060 | $mappedStructFields = []; |
||
1061 | foreach ($structFields as $structField) { |
||
1062 | if ($structField->languageCode === null) { |
||
1063 | $languageCode = $mainLanguageCode; |
||
1064 | } else { |
||
1065 | $languageCode = $structField->languageCode; |
||
1066 | } |
||
1067 | |||
1068 | $mappedStructFields[$structField->fieldDefIdentifier][$languageCode] = (string)$structField->value; |
||
1069 | } |
||
1070 | |||
1071 | return $mappedStructFields; |
||
1072 | } |
||
1073 | |||
1074 | /** |
||
1075 | * Returns full, possibly redundant array of field values, indexed by field definition |
||
1076 | * identifier and language code. |
||
1077 | * |
||
1078 | * @throws \RuntimeException Method is intended to be used only with consistent fixtures |
||
1079 | * |
||
1080 | * @param string $mainLanguageCode |
||
1081 | * @param \eZ\Publish\API\Repository\Values\Content\Field[] $structFields |
||
1082 | * @param \eZ\Publish\API\Repository\Values\ContentType\FieldDefinition[] $fieldDefinitions |
||
1083 | * @param array $languageCodes |
||
1084 | * |
||
1085 | * @return array |
||
1086 | */ |
||
1087 | protected function determineValuesForCreate( |
||
1088 | $mainLanguageCode, |
||
1089 | array $structFields, |
||
1090 | array $fieldDefinitions, |
||
1091 | array $languageCodes |
||
1092 | ) { |
||
1093 | $mappedStructFields = $this->mapStructFieldsForCreate( |
||
1094 | $mainLanguageCode, |
||
1095 | $structFields, |
||
1096 | $fieldDefinitions |
||
1097 | ); |
||
1098 | |||
1099 | $values = []; |
||
1100 | |||
1101 | foreach ($fieldDefinitions as $fieldDefinition) { |
||
1102 | $identifier = $fieldDefinition->identifier; |
||
1103 | foreach ($languageCodes as $languageCode) { |
||
1104 | View Code Duplication | if (!$fieldDefinition->isTranslatable) { |
|
1105 | if (isset($mappedStructFields[$identifier][$mainLanguageCode])) { |
||
1106 | $values[$identifier][$languageCode] = $mappedStructFields[$identifier][$mainLanguageCode]; |
||
1107 | } else { |
||
1108 | $values[$identifier][$languageCode] = (string)$fieldDefinition->defaultValue; |
||
1109 | } |
||
1110 | continue; |
||
1111 | } |
||
1112 | |||
1113 | View Code Duplication | if (isset($mappedStructFields[$identifier][$languageCode])) { |
|
1114 | $values[$identifier][$languageCode] = $mappedStructFields[$identifier][$languageCode]; |
||
1115 | continue; |
||
1116 | } |
||
1117 | |||
1118 | $values[$identifier][$languageCode] = (string)$fieldDefinition->defaultValue; |
||
1119 | } |
||
1120 | } |
||
1121 | |||
1122 | return $this->stubValues($values); |
||
1123 | } |
||
1124 | |||
1125 | /** |
||
1126 | * @param string $mainLanguageCode |
||
1127 | * @param \eZ\Publish\API\Repository\Values\Content\Field[] $structFields |
||
1128 | * |
||
1129 | * @return string[] |
||
1130 | */ |
||
1131 | protected function determineLanguageCodesForCreate($mainLanguageCode, array $structFields) |
||
1132 | { |
||
1133 | $languageCodes = []; |
||
1134 | |||
1135 | foreach ($structFields as $field) { |
||
1136 | if ($field->languageCode === null || isset($languageCodes[$field->languageCode])) { |
||
1137 | continue; |
||
1138 | } |
||
1139 | |||
1140 | $languageCodes[$field->languageCode] = true; |
||
1141 | } |
||
1142 | |||
1143 | $languageCodes[$mainLanguageCode] = true; |
||
1144 | |||
1145 | return array_keys($languageCodes); |
||
1146 | } |
||
1147 | |||
1148 | /** |
||
1149 | * Asserts that calling createContent() with given API field set causes calling |
||
1150 | * Handler::createContent() with given SPI field set. |
||
1151 | * |
||
1152 | * @param string $mainLanguageCode |
||
1153 | * @param \eZ\Publish\API\Repository\Values\Content\Field[] $structFields |
||
1154 | * @param \eZ\Publish\SPI\Persistence\Content\Field[] $spiFields |
||
1155 | * @param \eZ\Publish\API\Repository\Values\ContentType\FieldDefinition[] $fieldDefinitions |
||
1156 | * @param \eZ\Publish\API\Repository\Values\Content\LocationCreateStruct[] $locationCreateStructs |
||
1157 | * @param \eZ\Publish\SPI\Persistence\Content\ObjectState\Group[] $objectStateGroups |
||
1158 | * @param bool $execute |
||
1159 | * |
||
1160 | * @return mixed |
||
1161 | */ |
||
1162 | protected function assertForTestCreateContentNonRedundantFieldSet( |
||
1163 | $mainLanguageCode, |
||
1164 | array $structFields, |
||
1165 | array $spiFields, |
||
1166 | array $fieldDefinitions, |
||
1167 | array $locationCreateStructs = [], |
||
1168 | $withObjectStates = false, |
||
1169 | $execute = true |
||
1170 | ) { |
||
1171 | $repositoryMock = $this->getRepositoryMock(); |
||
1172 | $mockedService = $this->getPartlyMockedContentService(); |
||
1173 | /** @var \PHPUnit\Framework\MockObject\MockObject $contentHandlerMock */ |
||
1174 | $contentHandlerMock = $this->getPersistenceMock()->contentHandler(); |
||
1175 | /** @var \PHPUnit\Framework\MockObject\MockObject $languageHandlerMock */ |
||
1176 | $languageHandlerMock = $this->getPersistenceMock()->contentLanguageHandler(); |
||
1177 | /** @var \PHPUnit\Framework\MockObject\MockObject $objectStateHandlerMock */ |
||
1178 | $objectStateHandlerMock = $this->getPersistenceMock()->objectStateHandler(); |
||
1179 | $contentTypeServiceMock = $this->getContentTypeServiceMock(); |
||
1180 | $fieldTypeServiceMock = $this->getFieldTypeServiceMock(); |
||
1181 | $domainMapperMock = $this->getDomainMapperMock(); |
||
1182 | $relationProcessorMock = $this->getRelationProcessorMock(); |
||
1183 | $nameSchemaServiceMock = $this->getNameSchemaServiceMock(); |
||
1184 | $permissionResolverMock = $this->getPermissionResolverMock(); |
||
1185 | $fieldTypeMock = $this->createMock(SPIFieldType::class); |
||
1186 | $languageCodes = $this->determineLanguageCodesForCreate($mainLanguageCode, $structFields); |
||
1187 | $contentType = new ContentType( |
||
1188 | [ |
||
1189 | 'id' => 123, |
||
1190 | 'fieldDefinitions' => $fieldDefinitions, |
||
1191 | 'nameSchema' => '<nameSchema>', |
||
1192 | ] |
||
1193 | ); |
||
1194 | $contentCreateStruct = new ContentCreateStruct( |
||
1195 | [ |
||
1196 | 'fields' => $structFields, |
||
1197 | 'mainLanguageCode' => $mainLanguageCode, |
||
1198 | 'contentType' => $contentType, |
||
1199 | 'alwaysAvailable' => false, |
||
1200 | 'ownerId' => 169, |
||
1201 | 'sectionId' => 1, |
||
1202 | ] |
||
1203 | ); |
||
1204 | |||
1205 | $languageHandlerMock->expects($this->any()) |
||
1206 | ->method('loadByLanguageCode') |
||
1207 | ->with($this->isType('string')) |
||
1208 | ->will( |
||
1209 | $this->returnCallback( |
||
1210 | function () { |
||
1211 | return new Language(['id' => 4242]); |
||
1212 | } |
||
1213 | ) |
||
1214 | ); |
||
1215 | |||
1216 | $repositoryMock->expects($this->once())->method('beginTransaction'); |
||
1217 | |||
1218 | $contentTypeServiceMock->expects($this->once()) |
||
1219 | ->method('loadContentType') |
||
1220 | ->with($this->equalTo($contentType->id)) |
||
1221 | ->will($this->returnValue($contentType)); |
||
1222 | |||
1223 | $repositoryMock->expects($this->once()) |
||
1224 | ->method('getContentTypeService') |
||
1225 | ->will($this->returnValue($contentTypeServiceMock)); |
||
1226 | |||
1227 | $that = $this; |
||
1228 | $permissionResolverMock->expects($this->once()) |
||
1229 | ->method('canUser') |
||
1230 | ->with( |
||
1231 | $this->equalTo('content'), |
||
1232 | $this->equalTo('create'), |
||
1233 | $this->isInstanceOf(APIContentCreateStruct::class), |
||
1234 | $this->equalTo($locationCreateStructs) |
||
1235 | )->will( |
||
1236 | $this->returnCallback( |
||
1237 | function () use ($that, $contentCreateStruct) { |
||
1238 | $that->assertEquals($contentCreateStruct, func_get_arg(2)); |
||
1239 | |||
1240 | return true; |
||
1241 | } |
||
1242 | ) |
||
1243 | ); |
||
1244 | |||
1245 | $domainMapperMock->expects($this->once()) |
||
1246 | ->method('getUniqueHash') |
||
1247 | ->with($this->isInstanceOf(APIContentCreateStruct::class)) |
||
1248 | ->will( |
||
1249 | $this->returnCallback( |
||
1250 | function ($object) use ($that, $contentCreateStruct) { |
||
1251 | $that->assertEquals($contentCreateStruct, $object); |
||
1252 | |||
1253 | return 'hash'; |
||
1254 | } |
||
1255 | ) |
||
1256 | ); |
||
1257 | |||
1258 | $fieldTypeMock->expects($this->any()) |
||
1259 | ->method('acceptValue') |
||
1260 | ->will( |
||
1261 | $this->returnCallback( |
||
1262 | function ($valueString) { |
||
1263 | return new ValueStub($valueString); |
||
1264 | } |
||
1265 | ) |
||
1266 | ); |
||
1267 | |||
1268 | $fieldTypeMock->expects($this->any()) |
||
1269 | ->method('toPersistenceValue') |
||
1270 | ->will( |
||
1271 | $this->returnCallback( |
||
1272 | function (ValueStub $value) { |
||
1273 | return (string)$value; |
||
1274 | } |
||
1275 | ) |
||
1276 | ); |
||
1277 | |||
1278 | $emptyValue = self::EMPTY_FIELD_VALUE; |
||
1279 | $fieldTypeMock->expects($this->any()) |
||
1280 | ->method('isEmptyValue') |
||
1281 | ->will( |
||
1282 | $this->returnCallback( |
||
1283 | function (ValueStub $value) use ($emptyValue) { |
||
1284 | return $emptyValue === (string)$value; |
||
1285 | } |
||
1286 | ) |
||
1287 | ); |
||
1288 | |||
1289 | $fieldTypeMock->expects($this->any()) |
||
1290 | ->method('validate') |
||
1291 | ->will($this->returnValue([])); |
||
1292 | |||
1293 | $this->getFieldTypeRegistryMock()->expects($this->any()) |
||
1294 | ->method('getFieldType') |
||
1295 | ->will($this->returnValue($fieldTypeMock)); |
||
1296 | |||
1297 | $relationProcessorMock |
||
1298 | ->expects($this->exactly(count($fieldDefinitions) * count($languageCodes))) |
||
1299 | ->method('appendFieldRelations') |
||
1300 | ->with( |
||
1301 | $this->isType('array'), |
||
1302 | $this->isType('array'), |
||
1303 | $this->isInstanceOf(SPIFieldType::class), |
||
1304 | $this->isInstanceOf(Value::class), |
||
1305 | $this->anything() |
||
1306 | ); |
||
1307 | |||
1308 | $values = $this->determineValuesForCreate( |
||
1309 | $mainLanguageCode, |
||
1310 | $structFields, |
||
1311 | $fieldDefinitions, |
||
1312 | $languageCodes |
||
1313 | ); |
||
1314 | $nameSchemaServiceMock->expects($this->once()) |
||
1315 | ->method('resolve') |
||
1316 | ->with( |
||
1317 | $this->equalTo($contentType->nameSchema), |
||
1318 | $this->equalTo($contentType), |
||
1319 | $this->equalTo($values), |
||
1320 | $this->equalTo($languageCodes) |
||
1321 | )->will($this->returnValue([])); |
||
1322 | |||
1323 | $relationProcessorMock->expects($this->any()) |
||
1324 | ->method('processFieldRelations') |
||
1325 | ->with( |
||
1326 | $this->isType('array'), |
||
1327 | $this->equalTo(42), |
||
1328 | $this->isType('int'), |
||
1329 | $this->equalTo($contentType), |
||
1330 | $this->equalTo([]) |
||
1331 | ); |
||
1332 | |||
1333 | if (!$withObjectStates) { |
||
1334 | $objectStateHandlerMock->expects($this->once()) |
||
1335 | ->method('loadAllGroups') |
||
1336 | ->will($this->returnValue([])); |
||
1337 | } |
||
1338 | |||
1339 | if ($execute) { |
||
1340 | $spiContentCreateStruct = new SPIContentCreateStruct( |
||
1341 | [ |
||
1342 | 'name' => [], |
||
1343 | 'typeId' => 123, |
||
1344 | 'sectionId' => 1, |
||
1345 | 'ownerId' => 169, |
||
1346 | 'remoteId' => 'hash', |
||
1347 | 'fields' => $spiFields, |
||
1348 | 'modified' => time(), |
||
1349 | 'initialLanguageId' => 4242, |
||
1350 | ] |
||
1351 | ); |
||
1352 | $spiContentCreateStruct2 = clone $spiContentCreateStruct; |
||
1353 | ++$spiContentCreateStruct2->modified; |
||
1354 | |||
1355 | $spiContent = new SPIContent( |
||
1356 | [ |
||
1357 | 'versionInfo' => new SPIContent\VersionInfo( |
||
1358 | [ |
||
1359 | 'contentInfo' => new SPIContent\ContentInfo(['id' => 42]), |
||
1360 | 'versionNo' => 7, |
||
1361 | ] |
||
1362 | ), |
||
1363 | ] |
||
1364 | ); |
||
1365 | |||
1366 | $contentHandlerMock->expects($this->once()) |
||
1367 | ->method('create') |
||
1368 | ->with($this->logicalOr($spiContentCreateStruct, $spiContentCreateStruct2)) |
||
1369 | ->will($this->returnValue($spiContent)); |
||
1370 | |||
1371 | $repositoryMock->expects($this->once())->method('commit'); |
||
1372 | $domainMapperMock->expects($this->once()) |
||
1373 | ->method('buildContentDomainObject') |
||
1374 | ->with( |
||
1375 | $this->isInstanceOf(SPIContent::class), |
||
1376 | $this->equalTo($contentType) |
||
1377 | ); |
||
1378 | |||
1379 | $mockedService->createContent($contentCreateStruct, []); |
||
1380 | } |
||
1381 | |||
1382 | return $contentCreateStruct; |
||
1383 | } |
||
1384 | |||
1385 | public function providerForTestCreateContentNonRedundantFieldSet1() |
||
1386 | { |
||
1387 | $spiFields = [ |
||
1388 | new SPIField( |
||
1389 | [ |
||
1390 | 'fieldDefinitionId' => 'fieldDefinitionId', |
||
1391 | 'type' => 'fieldTypeIdentifier', |
||
1392 | 'value' => 'newValue', |
||
1393 | 'languageCode' => 'eng-US', |
||
1394 | ] |
||
1395 | ), |
||
1396 | ]; |
||
1397 | |||
1398 | return [ |
||
1399 | // 0. Without language set |
||
1400 | [ |
||
1401 | 'eng-US', |
||
1402 | [ |
||
1403 | new Field( |
||
1404 | [ |
||
1405 | 'fieldDefIdentifier' => 'identifier', |
||
1406 | 'value' => 'newValue', |
||
1407 | 'languageCode' => 'eng-US', |
||
1408 | ] |
||
1409 | ), |
||
1410 | ], |
||
1411 | $spiFields, |
||
1412 | ], |
||
1413 | // 1. Without language set |
||
1414 | [ |
||
1415 | 'eng-US', |
||
1416 | [ |
||
1417 | new Field( |
||
1418 | [ |
||
1419 | 'fieldDefIdentifier' => 'identifier', |
||
1420 | 'value' => 'newValue', |
||
1421 | 'languageCode' => null, |
||
1422 | ] |
||
1423 | ), |
||
1424 | ], |
||
1425 | $spiFields, |
||
1426 | ], |
||
1427 | ]; |
||
1428 | } |
||
1429 | |||
1430 | /** |
||
1431 | * Test for the createContent() method. |
||
1432 | * |
||
1433 | * Testing the simplest use case. |
||
1434 | * |
||
1435 | * @covers \eZ\Publish\Core\Repository\ContentService::getLanguageCodesForCreate |
||
1436 | * @covers \eZ\Publish\Core\Repository\ContentService::mapFieldsForCreate |
||
1437 | * @covers \eZ\Publish\Core\Repository\ContentService::cloneField |
||
1438 | * @covers \eZ\Publish\Core\Repository\ContentService::getDefaultObjectStates |
||
1439 | * @covers \eZ\Publish\Core\Repository\ContentService::createContent |
||
1440 | * @dataProvider providerForTestCreateContentNonRedundantFieldSet1 |
||
1441 | */ |
||
1442 | public function testCreateContentNonRedundantFieldSet1($mainLanguageCode, $structFields, $spiFields) |
||
1443 | { |
||
1444 | $fieldDefinitions = [ |
||
1445 | new FieldDefinition( |
||
1446 | [ |
||
1447 | 'id' => 'fieldDefinitionId', |
||
1448 | 'fieldTypeIdentifier' => 'fieldTypeIdentifier', |
||
1449 | 'isTranslatable' => false, |
||
1450 | 'identifier' => 'identifier', |
||
1451 | 'isRequired' => false, |
||
1452 | 'defaultValue' => 'defaultValue', |
||
1453 | ] |
||
1454 | ), |
||
1455 | ]; |
||
1456 | |||
1457 | $this->assertForTestCreateContentNonRedundantFieldSet( |
||
1458 | $mainLanguageCode, |
||
1459 | $structFields, |
||
1460 | $spiFields, |
||
1461 | $fieldDefinitions |
||
1462 | ); |
||
1463 | } |
||
1464 | |||
1465 | public function providerForTestCreateContentNonRedundantFieldSet2() |
||
1466 | { |
||
1467 | $spiFields = [ |
||
1468 | new SPIField( |
||
1469 | [ |
||
1470 | 'fieldDefinitionId' => 'fieldDefinitionId1', |
||
1471 | 'type' => 'fieldTypeIdentifier', |
||
1472 | 'value' => 'newValue1', |
||
1473 | 'languageCode' => 'eng-US', |
||
1474 | ] |
||
1475 | ), |
||
1476 | new SPIField( |
||
1477 | [ |
||
1478 | 'fieldDefinitionId' => 'fieldDefinitionId2', |
||
1479 | 'type' => 'fieldTypeIdentifier', |
||
1480 | 'value' => 'newValue2', |
||
1481 | 'languageCode' => 'ger-DE', |
||
1482 | ] |
||
1483 | ), |
||
1484 | ]; |
||
1485 | |||
1486 | return [ |
||
1487 | // 0. With language set |
||
1488 | [ |
||
1489 | 'eng-US', |
||
1490 | [ |
||
1491 | new Field( |
||
1492 | [ |
||
1493 | 'fieldDefIdentifier' => 'identifier1', |
||
1494 | 'value' => 'newValue1', |
||
1495 | 'languageCode' => 'eng-US', |
||
1496 | ] |
||
1497 | ), |
||
1498 | new Field( |
||
1499 | [ |
||
1500 | 'fieldDefIdentifier' => 'identifier2', |
||
1501 | 'value' => 'newValue2', |
||
1502 | 'languageCode' => 'ger-DE', |
||
1503 | ] |
||
1504 | ), |
||
1505 | ], |
||
1506 | $spiFields, |
||
1507 | ], |
||
1508 | // 1. Without language set |
||
1509 | [ |
||
1510 | 'eng-US', |
||
1511 | [ |
||
1512 | new Field( |
||
1513 | [ |
||
1514 | 'fieldDefIdentifier' => 'identifier1', |
||
1515 | 'value' => 'newValue1', |
||
1516 | 'languageCode' => null, |
||
1517 | ] |
||
1518 | ), |
||
1519 | new Field( |
||
1520 | [ |
||
1521 | 'fieldDefIdentifier' => 'identifier2', |
||
1522 | 'value' => 'newValue2', |
||
1523 | 'languageCode' => 'ger-DE', |
||
1524 | ] |
||
1525 | ), |
||
1526 | ], |
||
1527 | $spiFields, |
||
1528 | ], |
||
1529 | ]; |
||
1530 | } |
||
1531 | |||
1532 | /** |
||
1533 | * Test for the createContent() method. |
||
1534 | * |
||
1535 | * Testing multiple languages with multiple translatable fields with empty default value. |
||
1536 | * |
||
1537 | * @covers \eZ\Publish\Core\Repository\ContentService::getLanguageCodesForCreate |
||
1538 | * @covers \eZ\Publish\Core\Repository\ContentService::mapFieldsForCreate |
||
1539 | * @covers \eZ\Publish\Core\Repository\ContentService::cloneField |
||
1540 | * @covers \eZ\Publish\Core\Repository\ContentService::getDefaultObjectStates |
||
1541 | * @covers \eZ\Publish\Core\Repository\ContentService::createContent |
||
1542 | * @dataProvider providerForTestCreateContentNonRedundantFieldSet2 |
||
1543 | */ |
||
1544 | public function testCreateContentNonRedundantFieldSet2($mainLanguageCode, $structFields, $spiFields) |
||
1545 | { |
||
1546 | $fieldDefinitions = [ |
||
1547 | new FieldDefinition( |
||
1548 | [ |
||
1549 | 'id' => 'fieldDefinitionId1', |
||
1550 | 'fieldTypeIdentifier' => 'fieldTypeIdentifier', |
||
1551 | 'isTranslatable' => true, |
||
1552 | 'identifier' => 'identifier1', |
||
1553 | 'isRequired' => false, |
||
1554 | 'defaultValue' => self::EMPTY_FIELD_VALUE, |
||
1555 | ] |
||
1556 | ), |
||
1557 | new FieldDefinition( |
||
1558 | [ |
||
1559 | 'id' => 'fieldDefinitionId2', |
||
1560 | 'fieldTypeIdentifier' => 'fieldTypeIdentifier', |
||
1561 | 'isTranslatable' => true, |
||
1562 | 'identifier' => 'identifier2', |
||
1563 | 'isRequired' => false, |
||
1564 | 'defaultValue' => self::EMPTY_FIELD_VALUE, |
||
1565 | ] |
||
1566 | ), |
||
1567 | ]; |
||
1568 | |||
1569 | $this->assertForTestCreateContentNonRedundantFieldSet( |
||
1570 | $mainLanguageCode, |
||
1571 | $structFields, |
||
1572 | $spiFields, |
||
1573 | $fieldDefinitions |
||
1574 | ); |
||
1575 | } |
||
1576 | |||
1577 | public function providerForTestCreateContentNonRedundantFieldSetComplex() |
||
1578 | { |
||
1579 | $spiFields0 = [ |
||
1580 | new SPIField( |
||
1581 | [ |
||
1582 | 'fieldDefinitionId' => 'fieldDefinitionId2', |
||
1583 | 'type' => 'fieldTypeIdentifier', |
||
1584 | 'value' => 'defaultValue2', |
||
1585 | 'languageCode' => 'eng-US', |
||
1586 | ] |
||
1587 | ), |
||
1588 | new SPIField( |
||
1589 | [ |
||
1590 | 'fieldDefinitionId' => 'fieldDefinitionId4', |
||
1591 | 'type' => 'fieldTypeIdentifier', |
||
1592 | 'value' => 'defaultValue4', |
||
1593 | 'languageCode' => 'eng-US', |
||
1594 | ] |
||
1595 | ), |
||
1596 | ]; |
||
1597 | $spiFields1 = [ |
||
1598 | new SPIField( |
||
1599 | [ |
||
1600 | 'fieldDefinitionId' => 'fieldDefinitionId1', |
||
1601 | 'type' => 'fieldTypeIdentifier', |
||
1602 | 'value' => 'newValue1', |
||
1603 | 'languageCode' => 'ger-DE', |
||
1604 | ] |
||
1605 | ), |
||
1606 | new SPIField( |
||
1607 | [ |
||
1608 | 'fieldDefinitionId' => 'fieldDefinitionId2', |
||
1609 | 'type' => 'fieldTypeIdentifier', |
||
1610 | 'value' => 'defaultValue2', |
||
1611 | 'languageCode' => 'ger-DE', |
||
1612 | ] |
||
1613 | ), |
||
1614 | new SPIField( |
||
1615 | [ |
||
1616 | 'fieldDefinitionId' => 'fieldDefinitionId2', |
||
1617 | 'type' => 'fieldTypeIdentifier', |
||
1618 | 'value' => 'newValue2', |
||
1619 | 'languageCode' => 'eng-US', |
||
1620 | ] |
||
1621 | ), |
||
1622 | new SPIField( |
||
1623 | [ |
||
1624 | 'fieldDefinitionId' => 'fieldDefinitionId4', |
||
1625 | 'type' => 'fieldTypeIdentifier', |
||
1626 | 'value' => 'newValue4', |
||
1627 | 'languageCode' => 'eng-US', |
||
1628 | ] |
||
1629 | ), |
||
1630 | ]; |
||
1631 | |||
1632 | return [ |
||
1633 | // 0. Creating by default values only |
||
1634 | [ |
||
1635 | 'eng-US', |
||
1636 | [], |
||
1637 | $spiFields0, |
||
1638 | ], |
||
1639 | // 1. Multiple languages with language set |
||
1640 | [ |
||
1641 | 'eng-US', |
||
1642 | [ |
||
1643 | new Field( |
||
1644 | [ |
||
1645 | 'fieldDefIdentifier' => 'identifier1', |
||
1646 | 'value' => 'newValue1', |
||
1647 | 'languageCode' => 'ger-DE', |
||
1648 | ] |
||
1649 | ), |
||
1650 | new Field( |
||
1651 | [ |
||
1652 | 'fieldDefIdentifier' => 'identifier2', |
||
1653 | 'value' => 'newValue2', |
||
1654 | 'languageCode' => 'eng-US', |
||
1655 | ] |
||
1656 | ), |
||
1657 | new Field( |
||
1658 | [ |
||
1659 | 'fieldDefIdentifier' => 'identifier4', |
||
1660 | 'value' => 'newValue4', |
||
1661 | 'languageCode' => 'eng-US', |
||
1662 | ] |
||
1663 | ), |
||
1664 | ], |
||
1665 | $spiFields1, |
||
1666 | ], |
||
1667 | // 2. Multiple languages without language set |
||
1668 | [ |
||
1669 | 'eng-US', |
||
1670 | [ |
||
1671 | new Field( |
||
1672 | [ |
||
1673 | 'fieldDefIdentifier' => 'identifier1', |
||
1674 | 'value' => 'newValue1', |
||
1675 | 'languageCode' => 'ger-DE', |
||
1676 | ] |
||
1677 | ), |
||
1678 | new Field( |
||
1679 | [ |
||
1680 | 'fieldDefIdentifier' => 'identifier2', |
||
1681 | 'value' => 'newValue2', |
||
1682 | 'languageCode' => null, |
||
1683 | ] |
||
1684 | ), |
||
1685 | new Field( |
||
1686 | [ |
||
1687 | 'fieldDefIdentifier' => 'identifier4', |
||
1688 | 'value' => 'newValue4', |
||
1689 | 'languageCode' => null, |
||
1690 | ] |
||
1691 | ), |
||
1692 | ], |
||
1693 | $spiFields1, |
||
1694 | ], |
||
1695 | ]; |
||
1696 | } |
||
1697 | |||
1698 | protected function fixturesForTestCreateContentNonRedundantFieldSetComplex() |
||
1699 | { |
||
1700 | return [ |
||
1701 | new FieldDefinition( |
||
1702 | [ |
||
1703 | 'id' => 'fieldDefinitionId1', |
||
1704 | 'fieldTypeIdentifier' => 'fieldTypeIdentifier', |
||
1705 | 'isTranslatable' => true, |
||
1706 | 'identifier' => 'identifier1', |
||
1707 | 'isRequired' => false, |
||
1708 | 'defaultValue' => self::EMPTY_FIELD_VALUE, |
||
1709 | ] |
||
1710 | ), |
||
1711 | new FieldDefinition( |
||
1712 | [ |
||
1713 | 'id' => 'fieldDefinitionId2', |
||
1714 | 'fieldTypeIdentifier' => 'fieldTypeIdentifier', |
||
1715 | 'isTranslatable' => true, |
||
1716 | 'identifier' => 'identifier2', |
||
1717 | 'isRequired' => false, |
||
1718 | 'defaultValue' => 'defaultValue2', |
||
1719 | ] |
||
1720 | ), |
||
1721 | new FieldDefinition( |
||
1722 | [ |
||
1723 | 'id' => 'fieldDefinitionId3', |
||
1724 | 'fieldTypeIdentifier' => 'fieldTypeIdentifier', |
||
1725 | 'isTranslatable' => false, |
||
1726 | 'identifier' => 'identifier3', |
||
1727 | 'isRequired' => false, |
||
1728 | 'defaultValue' => self::EMPTY_FIELD_VALUE, |
||
1729 | ] |
||
1730 | ), |
||
1731 | new FieldDefinition( |
||
1732 | [ |
||
1733 | 'id' => 'fieldDefinitionId4', |
||
1734 | 'fieldTypeIdentifier' => 'fieldTypeIdentifier', |
||
1735 | 'isTranslatable' => false, |
||
1736 | 'identifier' => 'identifier4', |
||
1737 | 'isRequired' => false, |
||
1738 | 'defaultValue' => 'defaultValue4', |
||
1739 | ] |
||
1740 | ), |
||
1741 | ]; |
||
1742 | } |
||
1743 | |||
1744 | /** |
||
1745 | * Test for the createContent() method. |
||
1746 | * |
||
1747 | * Testing multiple languages with multiple translatable fields with empty default value. |
||
1748 | * |
||
1749 | * @covers \eZ\Publish\Core\Repository\ContentService::getLanguageCodesForCreate |
||
1750 | * @covers \eZ\Publish\Core\Repository\ContentService::mapFieldsForCreate |
||
1751 | * @covers \eZ\Publish\Core\Repository\ContentService::cloneField |
||
1752 | * @covers \eZ\Publish\Core\Repository\ContentService::getDefaultObjectStates |
||
1753 | * @covers \eZ\Publish\Core\Repository\ContentService::createContent |
||
1754 | * @dataProvider providerForTestCreateContentNonRedundantFieldSetComplex |
||
1755 | */ |
||
1756 | public function testCreateContentNonRedundantFieldSetComplex($mainLanguageCode, $structFields, $spiFields) |
||
1757 | { |
||
1758 | $fieldDefinitions = $this->fixturesForTestCreateContentNonRedundantFieldSetComplex(); |
||
1759 | |||
1760 | $this->assertForTestCreateContentNonRedundantFieldSet( |
||
1761 | $mainLanguageCode, |
||
1762 | $structFields, |
||
1763 | $spiFields, |
||
1764 | $fieldDefinitions |
||
1765 | ); |
||
1766 | } |
||
1767 | |||
1768 | View Code Duplication | public function providerForTestCreateContentWithInvalidLanguage() |
|
1769 | { |
||
1770 | return [ |
||
1771 | [ |
||
1772 | 'eng-GB', |
||
1773 | [ |
||
1774 | new Field( |
||
1775 | [ |
||
1776 | 'fieldDefIdentifier' => 'identifier', |
||
1777 | 'value' => 'newValue', |
||
1778 | 'languageCode' => 'Klingon', |
||
1779 | ] |
||
1780 | ), |
||
1781 | ], |
||
1782 | ], |
||
1783 | [ |
||
1784 | 'Klingon', |
||
1785 | [ |
||
1786 | new Field( |
||
1787 | [ |
||
1788 | 'fieldDefIdentifier' => 'identifier', |
||
1789 | 'value' => 'newValue', |
||
1790 | 'languageCode' => 'eng-GB', |
||
1791 | ] |
||
1792 | ), |
||
1793 | ], |
||
1794 | ], |
||
1795 | ]; |
||
1796 | } |
||
1797 | |||
1798 | /** |
||
1799 | * Test for the updateContent() method. |
||
1800 | * |
||
1801 | * @covers \eZ\Publish\Core\Repository\ContentService::getLanguageCodesForCreate |
||
1802 | * @covers \eZ\Publish\Core\Repository\ContentService::createContent |
||
1803 | * @dataProvider providerForTestCreateContentWithInvalidLanguage |
||
1804 | */ |
||
1805 | public function testCreateContentWithInvalidLanguage($mainLanguageCode, $structFields) |
||
1806 | { |
||
1807 | $this->expectException(\eZ\Publish\API\Repository\Exceptions\NotFoundException::class); |
||
1808 | $this->expectExceptionMessage('Could not find \'Language\' with identifier \'Klingon\''); |
||
1809 | |||
1810 | $repositoryMock = $this->getRepositoryMock(); |
||
1811 | $mockedService = $this->getPartlyMockedContentService(); |
||
1812 | /** @var \PHPUnit\Framework\MockObject\MockObject $languageHandlerMock */ |
||
1813 | $languageHandlerMock = $this->getPersistenceMock()->contentLanguageHandler(); |
||
1814 | $contentTypeServiceMock = $this->getContentTypeServiceMock(); |
||
1815 | $domainMapperMock = $this->getDomainMapperMock(); |
||
1816 | $permissionResolver = $this->getPermissionResolverMock(); |
||
1817 | |||
1818 | $contentType = new ContentType( |
||
1819 | [ |
||
1820 | 'id' => 123, |
||
1821 | 'fieldDefinitions' => [], |
||
1822 | ] |
||
1823 | ); |
||
1824 | $contentCreateStruct = new ContentCreateStruct( |
||
1825 | [ |
||
1826 | 'fields' => $structFields, |
||
1827 | 'mainLanguageCode' => $mainLanguageCode, |
||
1828 | 'contentType' => $contentType, |
||
1829 | 'alwaysAvailable' => false, |
||
1830 | 'ownerId' => 169, |
||
1831 | 'sectionId' => 1, |
||
1832 | ] |
||
1833 | ); |
||
1834 | |||
1835 | $languageHandlerMock->expects($this->any()) |
||
1836 | ->method('loadByLanguageCode') |
||
1837 | ->with($this->isType('string')) |
||
1838 | ->will( |
||
1839 | $this->returnCallback( |
||
1840 | View Code Duplication | function ($languageCode) { |
|
1841 | if ($languageCode === 'Klingon') { |
||
1842 | throw new NotFoundException('Language', 'Klingon'); |
||
1843 | } |
||
1844 | |||
1845 | return new Language(['id' => 4242]); |
||
1846 | } |
||
1847 | ) |
||
1848 | ); |
||
1849 | |||
1850 | $contentTypeServiceMock->expects($this->once()) |
||
1851 | ->method('loadContentType') |
||
1852 | ->with($this->equalTo($contentType->id)) |
||
1853 | ->will($this->returnValue($contentType)); |
||
1854 | |||
1855 | $repositoryMock->expects($this->once()) |
||
1856 | ->method('getContentTypeService') |
||
1857 | ->will($this->returnValue($contentTypeServiceMock)); |
||
1858 | |||
1859 | $that = $this; |
||
1860 | $permissionResolver->expects($this->once()) |
||
1861 | ->method('canUser') |
||
1862 | ->with( |
||
1863 | $this->equalTo('content'), |
||
1864 | $this->equalTo('create'), |
||
1865 | $this->isInstanceOf(APIContentCreateStruct::class), |
||
1866 | $this->equalTo([]) |
||
1867 | )->will( |
||
1868 | $this->returnCallback( |
||
1869 | function () use ($that, $contentCreateStruct) { |
||
1870 | $that->assertEquals($contentCreateStruct, func_get_arg(2)); |
||
1871 | |||
1872 | return true; |
||
1873 | } |
||
1874 | ) |
||
1875 | ); |
||
1876 | |||
1877 | $domainMapperMock->expects($this->once()) |
||
1878 | ->method('getUniqueHash') |
||
1879 | ->with($this->isInstanceOf(APIContentCreateStruct::class)) |
||
1880 | ->will( |
||
1881 | $this->returnCallback( |
||
1882 | function ($object) use ($that, $contentCreateStruct) { |
||
1883 | $that->assertEquals($contentCreateStruct, $object); |
||
1884 | |||
1885 | return 'hash'; |
||
1886 | } |
||
1887 | ) |
||
1888 | ); |
||
1889 | |||
1890 | $mockedService->createContent($contentCreateStruct, []); |
||
1891 | } |
||
1892 | |||
1893 | protected function assertForCreateContentContentValidationException( |
||
1894 | $mainLanguageCode, |
||
1895 | $structFields, |
||
1896 | $fieldDefinitions = [] |
||
1897 | ) { |
||
1898 | $repositoryMock = $this->getRepositoryMock(); |
||
1899 | $mockedService = $this->getPartlyMockedContentService(['loadContentByRemoteId']); |
||
1900 | $contentTypeServiceMock = $this->getContentTypeServiceMock(); |
||
1901 | $permissionResolver = $this->getPermissionResolverMock(); |
||
1902 | |||
1903 | $contentType = new ContentType( |
||
1904 | [ |
||
1905 | 'id' => 123, |
||
1906 | 'fieldDefinitions' => $fieldDefinitions, |
||
1907 | ] |
||
1908 | ); |
||
1909 | $contentCreateStruct = new ContentCreateStruct( |
||
1910 | [ |
||
1911 | 'ownerId' => 169, |
||
1912 | 'alwaysAvailable' => false, |
||
1913 | 'remoteId' => 'faraday', |
||
1914 | 'mainLanguageCode' => $mainLanguageCode, |
||
1915 | 'fields' => $structFields, |
||
1916 | 'contentType' => $contentType, |
||
1917 | ] |
||
1918 | ); |
||
1919 | |||
1920 | $contentTypeServiceMock->expects($this->once()) |
||
1921 | ->method('loadContentType') |
||
1922 | ->with($this->equalTo(123)) |
||
1923 | ->will($this->returnValue($contentType)); |
||
1924 | |||
1925 | $repositoryMock->expects($this->once()) |
||
1926 | ->method('getContentTypeService') |
||
1927 | ->will($this->returnValue($contentTypeServiceMock)); |
||
1928 | |||
1929 | $permissionResolver->expects($this->once()) |
||
1930 | ->method('canUser') |
||
1931 | ->with( |
||
1932 | $this->equalTo('content'), |
||
1933 | $this->equalTo('create'), |
||
1934 | $this->isInstanceOf(get_class($contentCreateStruct)), |
||
1935 | $this->equalTo([]) |
||
1936 | )->will($this->returnValue(true)); |
||
1937 | |||
1938 | $mockedService->expects($this->once()) |
||
1939 | ->method('loadContentByRemoteId') |
||
1940 | ->with($contentCreateStruct->remoteId) |
||
1941 | ->will( |
||
1942 | $this->throwException(new NotFoundException('Content', 'faraday')) |
||
1943 | ); |
||
1944 | |||
1945 | $mockedService->createContent($contentCreateStruct, []); |
||
1946 | } |
||
1947 | |||
1948 | View Code Duplication | public function providerForTestCreateContentThrowsContentValidationExceptionFieldDefinition() |
|
1965 | |||
1966 | /** |
||
1967 | * Test for the createContent() method. |
||
1968 | * |
||
1969 | * @covers \eZ\Publish\Core\Repository\ContentService::getLanguageCodesForCreate |
||
1970 | * @covers \eZ\Publish\Core\Repository\ContentService::mapFieldsForCreate |
||
1971 | * @covers \eZ\Publish\Core\Repository\ContentService::createContent |
||
1972 | * @dataProvider providerForTestCreateContentThrowsContentValidationExceptionFieldDefinition |
||
1973 | */ |
||
1974 | public function testCreateContentThrowsContentValidationExceptionFieldDefinition($mainLanguageCode, $structFields) |
||
1975 | { |
||
1976 | $this->expectException(\eZ\Publish\API\Repository\Exceptions\ContentValidationException::class); |
||
1977 | $this->expectExceptionMessage('Field definition \'identifier\' does not exist in given ContentType'); |
||
1978 | |||
1979 | $this->assertForCreateContentContentValidationException( |
||
1980 | $mainLanguageCode, |
||
1981 | $structFields, |
||
1982 | [] |
||
1983 | ); |
||
1984 | } |
||
1985 | |||
1986 | View Code Duplication | public function providerForTestCreateContentThrowsContentValidationExceptionTranslation() |
|
1987 | { |
||
1988 | return [ |
||
1989 | [ |
||
1990 | 'eng-GB', |
||
1991 | [ |
||
1992 | new Field( |
||
1993 | [ |
||
1994 | 'fieldDefIdentifier' => 'identifier', |
||
1995 | 'value' => 'newValue', |
||
1996 | 'languageCode' => 'eng-US', |
||
1997 | ] |
||
1998 | ), |
||
1999 | ], |
||
2000 | ], |
||
2001 | ]; |
||
2002 | } |
||
2003 | |||
2004 | /** |
||
2005 | * Test for the createContent() method. |
||
2006 | * |
||
2007 | * @covers \eZ\Publish\Core\Repository\ContentService::getLanguageCodesForCreate |
||
2008 | * @covers \eZ\Publish\Core\Repository\ContentService::mapFieldsForCreate |
||
2009 | * @covers \eZ\Publish\Core\Repository\ContentService::createContent |
||
2010 | * @dataProvider providerForTestCreateContentThrowsContentValidationExceptionTranslation |
||
2011 | */ |
||
2012 | View Code Duplication | public function testCreateContentThrowsContentValidationExceptionTranslation($mainLanguageCode, $structFields) |
|
2013 | { |
||
2014 | $this->expectException(\eZ\Publish\API\Repository\Exceptions\ContentValidationException::class); |
||
2015 | $this->expectExceptionMessage('A value is set for non translatable field definition \'identifier\' with language \'eng-US\''); |
||
2016 | |||
2017 | $fieldDefinitions = [ |
||
2018 | new FieldDefinition( |
||
2019 | [ |
||
2020 | 'id' => 'fieldDefinitionId1', |
||
2021 | 'fieldTypeIdentifier' => 'fieldTypeIdentifier', |
||
2022 | 'isTranslatable' => false, |
||
2023 | 'identifier' => 'identifier', |
||
2024 | 'isRequired' => false, |
||
2025 | 'defaultValue' => self::EMPTY_FIELD_VALUE, |
||
2026 | ] |
||
2027 | ), |
||
2028 | ]; |
||
2029 | |||
2030 | $this->assertForCreateContentContentValidationException( |
||
2031 | $mainLanguageCode, |
||
2032 | $structFields, |
||
2033 | $fieldDefinitions |
||
2034 | ); |
||
2035 | } |
||
2036 | |||
2037 | /** |
||
2038 | * Asserts behaviour necessary for testing ContentFieldValidationException because of required |
||
2039 | * field being empty. |
||
2040 | * |
||
2041 | * @param string $mainLanguageCode |
||
2042 | * @param \eZ\Publish\API\Repository\Values\Content\Field[] $structFields |
||
2043 | * @param \eZ\Publish\API\Repository\Values\ContentType\FieldDefinition[] $fieldDefinitions |
||
2044 | * |
||
2045 | * @return mixed |
||
2046 | */ |
||
2047 | protected function assertForTestCreateContentRequiredField( |
||
2048 | $mainLanguageCode, |
||
2049 | array $structFields, |
||
2050 | array $fieldDefinitions |
||
2051 | ) { |
||
2052 | $repositoryMock = $this->getRepositoryMock(); |
||
2053 | /** @var \PHPUnit\Framework\MockObject\MockObject $languageHandlerMock */ |
||
2054 | $languageHandlerMock = $this->getPersistenceMock()->contentLanguageHandler(); |
||
2055 | $contentTypeServiceMock = $this->getContentTypeServiceMock(); |
||
2056 | $fieldTypeServiceMock = $this->getFieldTypeServiceMock(); |
||
2057 | $domainMapperMock = $this->getDomainMapperMock(); |
||
2058 | $fieldTypeMock = $this->createMock(SPIFieldType::class); |
||
2059 | $permissionResolver = $this->getPermissionResolverMock(); |
||
2060 | |||
2061 | $contentType = new ContentType( |
||
2062 | [ |
||
2063 | 'id' => 123, |
||
2064 | 'fieldDefinitions' => $fieldDefinitions, |
||
2065 | 'nameSchema' => '<nameSchema>', |
||
2066 | ] |
||
2067 | ); |
||
2068 | $contentCreateStruct = new ContentCreateStruct( |
||
2069 | [ |
||
2070 | 'fields' => $structFields, |
||
2071 | 'mainLanguageCode' => $mainLanguageCode, |
||
2072 | 'contentType' => $contentType, |
||
2073 | 'alwaysAvailable' => false, |
||
2074 | 'ownerId' => 169, |
||
2075 | 'sectionId' => 1, |
||
2076 | ] |
||
2077 | ); |
||
2078 | |||
2079 | $languageHandlerMock->expects($this->any()) |
||
2080 | ->method('loadByLanguageCode') |
||
2081 | ->with($this->isType('string')) |
||
2082 | ->will( |
||
2083 | $this->returnCallback( |
||
2084 | function () { |
||
2085 | return new Language(['id' => 4242]); |
||
2086 | } |
||
2087 | ) |
||
2088 | ); |
||
2089 | |||
2090 | $contentTypeServiceMock->expects($this->once()) |
||
2091 | ->method('loadContentType') |
||
2092 | ->with($this->equalTo($contentType->id)) |
||
2093 | ->will($this->returnValue($contentType)); |
||
2094 | |||
2095 | $repositoryMock->expects($this->once()) |
||
2096 | ->method('getContentTypeService') |
||
2097 | ->will($this->returnValue($contentTypeServiceMock)); |
||
2098 | |||
2099 | $that = $this; |
||
2100 | $permissionResolver->expects($this->once()) |
||
2101 | ->method('canUser') |
||
2102 | ->with( |
||
2103 | $this->equalTo('content'), |
||
2104 | $this->equalTo('create'), |
||
2105 | $this->isInstanceOf(APIContentCreateStruct::class), |
||
2106 | $this->equalTo([]) |
||
2107 | )->will( |
||
2108 | $this->returnCallback( |
||
2109 | function () use ($that, $contentCreateStruct) { |
||
2110 | $that->assertEquals($contentCreateStruct, func_get_arg(2)); |
||
2111 | |||
2112 | return true; |
||
2113 | } |
||
2114 | ) |
||
2115 | ); |
||
2116 | |||
2117 | $domainMapperMock->expects($this->once()) |
||
2118 | ->method('getUniqueHash') |
||
2119 | ->with($this->isInstanceOf(APIContentCreateStruct::class)) |
||
2120 | ->will( |
||
2121 | $this->returnCallback( |
||
2122 | function ($object) use ($that, $contentCreateStruct) { |
||
2123 | $that->assertEquals($contentCreateStruct, $object); |
||
2124 | |||
2125 | return 'hash'; |
||
2126 | } |
||
2127 | ) |
||
2128 | ); |
||
2129 | |||
2130 | $fieldTypeMock->expects($this->any()) |
||
2131 | ->method('acceptValue') |
||
2132 | ->will( |
||
2133 | $this->returnCallback( |
||
2134 | function ($valueString) { |
||
2135 | return new ValueStub($valueString); |
||
2136 | } |
||
2137 | ) |
||
2138 | ); |
||
2139 | |||
2140 | $emptyValue = self::EMPTY_FIELD_VALUE; |
||
2141 | $fieldTypeMock->expects($this->any()) |
||
2142 | ->method('isEmptyValue') |
||
2143 | ->will( |
||
2144 | $this->returnCallback( |
||
2145 | function (ValueStub $value) use ($emptyValue) { |
||
2146 | return $emptyValue === (string)$value; |
||
2147 | } |
||
2148 | ) |
||
2149 | ); |
||
2150 | |||
2151 | $fieldTypeMock->expects($this->any()) |
||
2152 | ->method('validate') |
||
2153 | ->will($this->returnValue([])); |
||
2154 | |||
2155 | $this->getFieldTypeRegistryMock()->expects($this->any()) |
||
2156 | ->method('getFieldType') |
||
2157 | ->will($this->returnValue($fieldTypeMock)); |
||
2158 | |||
2159 | return $contentCreateStruct; |
||
2160 | } |
||
2161 | |||
2162 | View Code Duplication | public function providerForTestCreateContentThrowsContentValidationExceptionRequiredField() |
|
2163 | { |
||
2164 | return [ |
||
2165 | [ |
||
2166 | 'eng-US', |
||
2167 | [ |
||
2168 | new Field( |
||
2169 | [ |
||
2170 | 'fieldDefIdentifier' => 'identifier', |
||
2171 | 'value' => self::EMPTY_FIELD_VALUE, |
||
2172 | 'languageCode' => null, |
||
2173 | ] |
||
2174 | ), |
||
2175 | ], |
||
2176 | 'identifier', |
||
2177 | 'eng-US', |
||
2178 | ], |
||
2179 | ]; |
||
2180 | } |
||
2181 | |||
2182 | /** |
||
2183 | * Test for the createContent() method. |
||
2184 | * |
||
2185 | * @covers \eZ\Publish\Core\Repository\ContentService::getLanguageCodesForCreate |
||
2186 | * @covers \eZ\Publish\Core\Repository\ContentService::mapFieldsForCreate |
||
2187 | * @covers \eZ\Publish\Core\Repository\ContentService::createContent |
||
2188 | * @dataProvider providerForTestCreateContentThrowsContentValidationExceptionRequiredField |
||
2189 | */ |
||
2190 | public function testCreateContentRequiredField( |
||
2191 | $mainLanguageCode, |
||
2192 | $structFields, |
||
2193 | $identifier, |
||
2194 | $languageCode |
||
2195 | ) { |
||
2196 | $this->expectException(\eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException::class); |
||
2197 | |||
2198 | $fieldDefinitions = [ |
||
2199 | new FieldDefinition( |
||
2200 | [ |
||
2201 | 'id' => 'fieldDefinitionId', |
||
2202 | 'fieldTypeIdentifier' => 'fieldTypeIdentifier', |
||
2203 | 'isTranslatable' => true, |
||
2204 | 'identifier' => 'identifier', |
||
2205 | 'isRequired' => true, |
||
2206 | 'defaultValue' => 'defaultValue', |
||
2207 | ] |
||
2208 | ), |
||
2209 | ]; |
||
2210 | $contentCreateStruct = $this->assertForTestCreateContentRequiredField( |
||
2211 | $mainLanguageCode, |
||
2212 | $structFields, |
||
2213 | $fieldDefinitions |
||
2214 | ); |
||
2215 | |||
2216 | $mockedService = $this->getPartlyMockedContentService(); |
||
2217 | |||
2218 | try { |
||
2219 | $mockedService->createContent($contentCreateStruct, []); |
||
2220 | } catch (ContentValidationException $e) { |
||
2221 | $this->assertEquals( |
||
2222 | "Value for required field definition '{$identifier}' with language '{$languageCode}' is empty", |
||
2223 | $e->getMessage() |
||
2224 | ); |
||
2225 | |||
2226 | throw $e; |
||
2227 | } |
||
2228 | } |
||
2229 | |||
2230 | /** |
||
2231 | * Asserts behaviour necessary for testing ContentFieldValidationException because of |
||
2232 | * field not being valid. |
||
2233 | * |
||
2234 | * @param string $mainLanguageCode |
||
2235 | * @param \eZ\Publish\API\Repository\Values\Content\Field[] $structFields |
||
2236 | * @param \eZ\Publish\API\Repository\Values\ContentType\FieldDefinition[] $fieldDefinitions |
||
2237 | * |
||
2238 | * @return mixed |
||
2239 | */ |
||
2240 | protected function assertForTestCreateContentThrowsContentFieldValidationException( |
||
2241 | $mainLanguageCode, |
||
2242 | array $structFields, |
||
2243 | array $fieldDefinitions |
||
2244 | ) { |
||
2245 | $repositoryMock = $this->getRepositoryMock(); |
||
2246 | /** @var \PHPUnit\Framework\MockObject\MockObject $languageHandlerMock */ |
||
2247 | $languageHandlerMock = $this->getPersistenceMock()->contentLanguageHandler(); |
||
2248 | $contentTypeServiceMock = $this->getContentTypeServiceMock(); |
||
2249 | $fieldTypeServiceMock = $this->getFieldTypeServiceMock(); |
||
2250 | $domainMapperMock = $this->getDomainMapperMock(); |
||
2251 | $relationProcessorMock = $this->getRelationProcessorMock(); |
||
2252 | $fieldTypeMock = $this->createMock(SPIFieldType::class); |
||
2253 | $languageCodes = $this->determineLanguageCodesForCreate($mainLanguageCode, $structFields); |
||
2254 | $permissionResolver = $this->getPermissionResolverMock(); |
||
2255 | |||
2256 | $contentType = new ContentType( |
||
2257 | [ |
||
2258 | 'id' => 123, |
||
2259 | 'fieldDefinitions' => $fieldDefinitions, |
||
2260 | 'nameSchema' => '<nameSchema>', |
||
2261 | ] |
||
2262 | ); |
||
2263 | $contentCreateStruct = new ContentCreateStruct( |
||
2264 | [ |
||
2265 | 'fields' => $structFields, |
||
2266 | 'mainLanguageCode' => $mainLanguageCode, |
||
2267 | 'contentType' => $contentType, |
||
2268 | 'alwaysAvailable' => false, |
||
2269 | 'ownerId' => 169, |
||
2270 | 'sectionId' => 1, |
||
2271 | ] |
||
2272 | ); |
||
2273 | |||
2274 | $languageHandlerMock->expects($this->any()) |
||
2275 | ->method('loadByLanguageCode') |
||
2276 | ->with($this->isType('string')) |
||
2277 | ->will( |
||
2278 | $this->returnCallback( |
||
2279 | function () { |
||
2280 | return new Language(['id' => 4242]); |
||
2281 | } |
||
2282 | ) |
||
2283 | ); |
||
2284 | |||
2285 | $contentTypeServiceMock->expects($this->once()) |
||
2286 | ->method('loadContentType') |
||
2287 | ->with($this->equalTo($contentType->id)) |
||
2288 | ->will($this->returnValue($contentType)); |
||
2289 | |||
2290 | $repositoryMock->expects($this->once()) |
||
2291 | ->method('getContentTypeService') |
||
2292 | ->will($this->returnValue($contentTypeServiceMock)); |
||
2293 | |||
2294 | $that = $this; |
||
2295 | $permissionResolver->expects($this->once()) |
||
2296 | ->method('canUser') |
||
2297 | ->with( |
||
2298 | $this->equalTo('content'), |
||
2299 | $this->equalTo('create'), |
||
2300 | $this->isInstanceOf(APIContentCreateStruct::class), |
||
2301 | $this->equalTo([]) |
||
2302 | )->will( |
||
2303 | $this->returnCallback( |
||
2304 | function () use ($that, $contentCreateStruct) { |
||
2305 | $that->assertEquals($contentCreateStruct, func_get_arg(2)); |
||
2306 | |||
2307 | return true; |
||
2308 | } |
||
2309 | ) |
||
2310 | ); |
||
2311 | |||
2312 | $domainMapperMock->expects($this->once()) |
||
2313 | ->method('getUniqueHash') |
||
2314 | ->with($this->isInstanceOf(APIContentCreateStruct::class)) |
||
2315 | ->will( |
||
2316 | $this->returnCallback( |
||
2317 | function ($object) use ($that, $contentCreateStruct) { |
||
2318 | $that->assertEquals($contentCreateStruct, $object); |
||
2319 | |||
2320 | return 'hash'; |
||
2321 | } |
||
2322 | ) |
||
2323 | ); |
||
2324 | |||
2325 | $this->getFieldTypeRegistryMock()->expects($this->any()) |
||
2326 | ->method('getFieldType') |
||
2327 | ->will($this->returnValue($fieldTypeMock)); |
||
2328 | |||
2329 | $relationProcessorMock |
||
2330 | ->expects($this->any()) |
||
2331 | ->method('appendFieldRelations') |
||
2332 | ->with( |
||
2333 | $this->isType('array'), |
||
2334 | $this->isType('array'), |
||
2335 | $this->isInstanceOf(SPIFieldType::class), |
||
2336 | $this->isInstanceOf(Value::class), |
||
2337 | $this->anything() |
||
2338 | ); |
||
2339 | |||
2340 | $fieldValues = $this->determineValuesForCreate( |
||
2341 | $mainLanguageCode, |
||
2342 | $structFields, |
||
2343 | $fieldDefinitions, |
||
2344 | $languageCodes |
||
2345 | ); |
||
2346 | $allFieldErrors = []; |
||
2347 | $validateCount = 0; |
||
2348 | $emptyValue = self::EMPTY_FIELD_VALUE; |
||
2349 | foreach ($contentType->getFieldDefinitions() as $fieldDefinition) { |
||
2350 | foreach ($fieldValues[$fieldDefinition->identifier] as $languageCode => $value) { |
||
2351 | $fieldTypeMock->expects($this->at($validateCount++)) |
||
2352 | ->method('acceptValue') |
||
2353 | ->will( |
||
2354 | $this->returnCallback( |
||
2355 | function ($valueString) { |
||
2356 | return new ValueStub($valueString); |
||
2357 | } |
||
2358 | ) |
||
2359 | ); |
||
2360 | |||
2361 | $fieldTypeMock->expects($this->at($validateCount++)) |
||
2362 | ->method('isEmptyValue') |
||
2363 | ->will( |
||
2364 | $this->returnCallback( |
||
2365 | function (ValueStub $value) use ($emptyValue) { |
||
2366 | return $emptyValue === (string)$value; |
||
2367 | } |
||
2368 | ) |
||
2369 | ); |
||
2370 | |||
2371 | if (self::EMPTY_FIELD_VALUE === (string)$value) { |
||
2372 | continue; |
||
2373 | } |
||
2374 | |||
2375 | $fieldTypeMock->expects($this->at($validateCount++)) |
||
2376 | ->method('validate') |
||
2377 | ->with( |
||
2378 | $this->equalTo($fieldDefinition), |
||
2379 | $this->equalTo($value) |
||
2380 | )->will($this->returnArgument(1)); |
||
2381 | |||
2382 | $allFieldErrors[$fieldDefinition->id][$languageCode] = $value; |
||
2383 | } |
||
2384 | } |
||
2385 | |||
2386 | return [$contentCreateStruct, $allFieldErrors]; |
||
2387 | } |
||
2388 | |||
2389 | public function providerForTestCreateContentThrowsContentFieldValidationException() |
||
2390 | { |
||
2391 | return $this->providerForTestCreateContentNonRedundantFieldSetComplex(); |
||
2392 | } |
||
2393 | |||
2394 | /** |
||
2395 | * Test for the createContent() method. |
||
2396 | * |
||
2397 | * @covers \eZ\Publish\Core\Repository\ContentService::getLanguageCodesForCreate |
||
2398 | * @covers \eZ\Publish\Core\Repository\ContentService::mapFieldsForCreate |
||
2399 | * @covers \eZ\Publish\Core\Repository\ContentService::createContent |
||
2400 | * @dataProvider providerForTestCreateContentThrowsContentFieldValidationException |
||
2401 | */ |
||
2402 | View Code Duplication | public function testCreateContentThrowsContentFieldValidationException($mainLanguageCode, $structFields) |
|
2403 | { |
||
2404 | $this->expectException(\eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException::class); |
||
2405 | $this->expectExceptionMessage('Content fields did not validate'); |
||
2406 | |||
2407 | $fieldDefinitions = $this->fixturesForTestCreateContentNonRedundantFieldSetComplex(); |
||
2408 | list($contentCreateStruct, $allFieldErrors) = |
||
2409 | $this->assertForTestCreateContentThrowsContentFieldValidationException( |
||
2410 | $mainLanguageCode, |
||
2411 | $structFields, |
||
2412 | $fieldDefinitions |
||
2413 | ); |
||
2414 | |||
2415 | $mockedService = $this->getPartlyMockedContentService(); |
||
2416 | |||
2417 | try { |
||
2418 | $mockedService->createContent($contentCreateStruct); |
||
2419 | } catch (ContentFieldValidationException $e) { |
||
2420 | $this->assertEquals($allFieldErrors, $e->getFieldErrors()); |
||
2421 | throw $e; |
||
2422 | } |
||
2423 | } |
||
2424 | |||
2425 | /** |
||
2426 | * Test for the createContent() method. |
||
2427 | * |
||
2428 | * @covers \eZ\Publish\Core\Repository\ContentService::getLanguageCodesForCreate |
||
2429 | * @covers \eZ\Publish\Core\Repository\ContentService::mapFieldsForCreate |
||
2430 | * @covers \eZ\Publish\Core\Repository\ContentService::buildSPILocationCreateStructs |
||
2431 | * @covers \eZ\Publish\Core\Repository\ContentService::createContent |
||
2432 | */ |
||
2433 | public function testCreateContentWithLocations() |
||
2434 | { |
||
2435 | $spiFields = [ |
||
2436 | new SPIField( |
||
2437 | [ |
||
2438 | 'fieldDefinitionId' => 'fieldDefinitionId', |
||
2439 | 'type' => 'fieldTypeIdentifier', |
||
2440 | 'value' => 'defaultValue', |
||
2441 | 'languageCode' => 'eng-US', |
||
2442 | ] |
||
2443 | ), |
||
2444 | ]; |
||
2445 | $fieldDefinitions = [ |
||
2446 | new FieldDefinition( |
||
2447 | [ |
||
2448 | 'id' => 'fieldDefinitionId', |
||
2449 | 'fieldTypeIdentifier' => 'fieldTypeIdentifier', |
||
2450 | 'isTranslatable' => false, |
||
2451 | 'identifier' => 'identifier', |
||
2452 | 'isRequired' => false, |
||
2453 | 'defaultValue' => 'defaultValue', |
||
2454 | ] |
||
2455 | ), |
||
2456 | ]; |
||
2457 | |||
2458 | // Set up a simple case that will pass |
||
2459 | $locationCreateStruct1 = new LocationCreateStruct(['parentLocationId' => 321]); |
||
2460 | $locationCreateStruct2 = new LocationCreateStruct(['parentLocationId' => 654]); |
||
2461 | $locationCreateStructs = [$locationCreateStruct1, $locationCreateStruct2]; |
||
2462 | $contentCreateStruct = $this->assertForTestCreateContentNonRedundantFieldSet( |
||
2463 | 'eng-US', |
||
2464 | [], |
||
2465 | $spiFields, |
||
2466 | $fieldDefinitions, |
||
2467 | $locationCreateStructs, |
||
2468 | false, |
||
2469 | // Do not execute |
||
2470 | false |
||
2471 | ); |
||
2472 | |||
2473 | $repositoryMock = $this->getRepositoryMock(); |
||
2474 | $mockedService = $this->getPartlyMockedContentService(); |
||
2475 | $locationServiceMock = $this->getLocationServiceMock(); |
||
2476 | /** @var \PHPUnit\Framework\MockObject\MockObject $handlerMock */ |
||
2477 | $handlerMock = $this->getPersistenceMock()->contentHandler(); |
||
2478 | $domainMapperMock = $this->getDomainMapperMock(); |
||
2479 | $spiLocationCreateStruct = new SPILocation\CreateStruct(); |
||
2480 | $parentLocation = new Location(['contentInfo' => new ContentInfo(['sectionId' => 1])]); |
||
2481 | |||
2482 | $locationServiceMock->expects($this->at(0)) |
||
2483 | ->method('loadLocation') |
||
2484 | ->with($this->equalTo(321)) |
||
2485 | ->will($this->returnValue($parentLocation)); |
||
2486 | |||
2487 | $locationServiceMock->expects($this->at(1)) |
||
2488 | ->method('loadLocation') |
||
2489 | ->with($this->equalTo(654)) |
||
2490 | ->will($this->returnValue($parentLocation)); |
||
2491 | |||
2492 | $repositoryMock->expects($this->atLeastOnce()) |
||
2493 | ->method('getLocationService') |
||
2494 | ->will($this->returnValue($locationServiceMock)); |
||
2495 | |||
2496 | $domainMapperMock->expects($this->at(1)) |
||
2497 | ->method('buildSPILocationCreateStruct') |
||
2498 | ->with( |
||
2499 | $this->equalTo($locationCreateStruct1), |
||
2500 | $this->equalTo($parentLocation), |
||
2501 | $this->equalTo(true), |
||
2502 | $this->equalTo(null), |
||
2503 | $this->equalTo(null) |
||
2504 | )->will($this->returnValue($spiLocationCreateStruct)); |
||
2505 | |||
2506 | $domainMapperMock->expects($this->at(2)) |
||
2507 | ->method('buildSPILocationCreateStruct') |
||
2508 | ->with( |
||
2509 | $this->equalTo($locationCreateStruct2), |
||
2510 | $this->equalTo($parentLocation), |
||
2511 | $this->equalTo(false), |
||
2512 | $this->equalTo(null), |
||
2513 | $this->equalTo(null) |
||
2514 | )->will($this->returnValue($spiLocationCreateStruct)); |
||
2515 | |||
2516 | $spiContentCreateStruct = new SPIContentCreateStruct( |
||
2517 | [ |
||
2518 | 'name' => [], |
||
2519 | 'typeId' => 123, |
||
2520 | 'sectionId' => 1, |
||
2521 | 'ownerId' => 169, |
||
2522 | 'remoteId' => 'hash', |
||
2523 | 'fields' => $spiFields, |
||
2524 | 'modified' => time(), |
||
2525 | 'initialLanguageId' => 4242, |
||
2526 | 'locations' => [$spiLocationCreateStruct, $spiLocationCreateStruct], |
||
2527 | ] |
||
2528 | ); |
||
2529 | $spiContentCreateStruct2 = clone $spiContentCreateStruct; |
||
2530 | ++$spiContentCreateStruct2->modified; |
||
2531 | |||
2532 | $spiContent = new SPIContent( |
||
2533 | [ |
||
2534 | 'versionInfo' => new SPIContent\VersionInfo( |
||
2535 | [ |
||
2536 | 'contentInfo' => new SPIContent\ContentInfo(['id' => 42]), |
||
2537 | 'versionNo' => 7, |
||
2538 | ] |
||
2539 | ), |
||
2540 | ] |
||
2541 | ); |
||
2542 | |||
2543 | $handlerMock->expects($this->once()) |
||
2544 | ->method('create') |
||
2545 | ->with($this->logicalOr($spiContentCreateStruct, $spiContentCreateStruct2)) |
||
2546 | ->will($this->returnValue($spiContent)); |
||
2547 | |||
2548 | $domainMapperMock->expects($this->once()) |
||
2549 | ->method('buildContentDomainObject') |
||
2550 | ->with( |
||
2551 | $this->isInstanceOf(SPIContent::class), |
||
2552 | $this->isInstanceOf(APIContentType::class) |
||
2553 | ); |
||
2554 | |||
2555 | $repositoryMock->expects($this->once())->method('commit'); |
||
2556 | |||
2557 | // Execute |
||
2558 | $mockedService->createContent($contentCreateStruct, $locationCreateStructs); |
||
2559 | } |
||
2560 | |||
2561 | /** |
||
2562 | * Test for the createContent() method. |
||
2563 | * |
||
2564 | * @covers \eZ\Publish\Core\Repository\ContentService::getLanguageCodesForCreate |
||
2565 | * @covers \eZ\Publish\Core\Repository\ContentService::mapFieldsForCreate |
||
2566 | * @covers \eZ\Publish\Core\Repository\ContentService::buildSPILocationCreateStructs |
||
2567 | * @covers \eZ\Publish\Core\Repository\ContentService::createContent |
||
2568 | */ |
||
2569 | public function testCreateContentWithLocationsDuplicateUnderParent() |
||
2570 | { |
||
2571 | $this->expectException(\eZ\Publish\API\Repository\Exceptions\InvalidArgumentException::class); |
||
2572 | $this->expectExceptionMessage('Multiple LocationCreateStructs with the same parent Location \'321\' are given'); |
||
2573 | |||
2574 | $fieldDefinitions = [ |
||
2575 | new FieldDefinition( |
||
2576 | [ |
||
2577 | 'id' => 'fieldDefinitionId', |
||
2578 | 'fieldTypeIdentifier' => 'fieldTypeIdentifier', |
||
2579 | 'isTranslatable' => false, |
||
2580 | 'identifier' => 'identifier', |
||
2581 | 'isRequired' => false, |
||
2582 | 'defaultValue' => 'defaultValue', |
||
2583 | ] |
||
2584 | ), |
||
2585 | ]; |
||
2586 | |||
2587 | $repositoryMock = $this->getRepositoryMock(); |
||
2588 | $mockedService = $this->getPartlyMockedContentService(); |
||
2589 | $locationServiceMock = $this->getLocationServiceMock(); |
||
2590 | $contentTypeServiceMock = $this->getContentTypeServiceMock(); |
||
2591 | $domainMapperMock = $this->getDomainMapperMock(); |
||
2592 | $permissionResolver = $this->getPermissionResolverMock(); |
||
2593 | /** @var \PHPUnit\Framework\MockObject\MockObject $languageHandlerMock */ |
||
2594 | $languageHandlerMock = $this->getPersistenceMock()->contentLanguageHandler(); |
||
2595 | $spiLocationCreateStruct = new SPILocation\CreateStruct(); |
||
2596 | $parentLocation = new Location(['id' => 321]); |
||
2597 | $locationCreateStruct = new LocationCreateStruct(['parentLocationId' => 321]); |
||
2598 | $locationCreateStructs = [$locationCreateStruct, clone $locationCreateStruct]; |
||
2599 | $contentType = new ContentType( |
||
2600 | [ |
||
2601 | 'id' => 123, |
||
2602 | 'fieldDefinitions' => $fieldDefinitions, |
||
2603 | 'nameSchema' => '<nameSchema>', |
||
2604 | ] |
||
2605 | ); |
||
2606 | $contentCreateStruct = new ContentCreateStruct( |
||
2607 | [ |
||
2608 | 'fields' => [], |
||
2609 | 'mainLanguageCode' => 'eng-US', |
||
2610 | 'contentType' => $contentType, |
||
2611 | 'alwaysAvailable' => false, |
||
2612 | 'ownerId' => 169, |
||
2613 | 'sectionId' => 1, |
||
2614 | ] |
||
2615 | ); |
||
2616 | |||
2617 | $languageHandlerMock->expects($this->any()) |
||
2618 | ->method('loadByLanguageCode') |
||
2619 | ->with($this->isType('string')) |
||
2620 | ->will( |
||
2621 | $this->returnCallback( |
||
2622 | function () { |
||
2623 | return new Language(['id' => 4242]); |
||
2624 | } |
||
2625 | ) |
||
2626 | ); |
||
2627 | |||
2628 | $contentTypeServiceMock->expects($this->once()) |
||
2629 | ->method('loadContentType') |
||
2630 | ->with($this->equalTo($contentType->id)) |
||
2631 | ->will($this->returnValue($contentType)); |
||
2632 | |||
2633 | $repositoryMock->expects($this->once()) |
||
2634 | ->method('getContentTypeService') |
||
2635 | ->will($this->returnValue($contentTypeServiceMock)); |
||
2636 | |||
2637 | $that = $this; |
||
2638 | $permissionResolver->expects($this->once()) |
||
2639 | ->method('canUser') |
||
2640 | ->with( |
||
2641 | $this->equalTo('content'), |
||
2642 | $this->equalTo('create'), |
||
2643 | $this->isInstanceOf(APIContentCreateStruct::class), |
||
2644 | $this->equalTo($locationCreateStructs) |
||
2645 | )->will( |
||
2646 | $this->returnCallback( |
||
2647 | function () use ($that, $contentCreateStruct) { |
||
2648 | $that->assertEquals($contentCreateStruct, func_get_arg(2)); |
||
2649 | |||
2650 | return true; |
||
2651 | } |
||
2652 | ) |
||
2653 | ); |
||
2654 | |||
2655 | $domainMapperMock->expects($this->once()) |
||
2656 | ->method('getUniqueHash') |
||
2657 | ->with($this->isInstanceOf(APIContentCreateStruct::class)) |
||
2658 | ->will( |
||
2659 | $this->returnCallback( |
||
2660 | function ($object) use ($that, $contentCreateStruct) { |
||
2661 | $that->assertEquals($contentCreateStruct, $object); |
||
2662 | |||
2663 | return 'hash'; |
||
2664 | } |
||
2665 | ) |
||
2666 | ); |
||
2667 | |||
2668 | $locationServiceMock->expects($this->once()) |
||
2669 | ->method('loadLocation') |
||
2670 | ->with($this->equalTo(321)) |
||
2671 | ->will($this->returnValue($parentLocation)); |
||
2672 | |||
2673 | $repositoryMock->expects($this->any()) |
||
2674 | ->method('getLocationService') |
||
2675 | ->will($this->returnValue($locationServiceMock)); |
||
2676 | |||
2677 | $domainMapperMock->expects($this->any()) |
||
2678 | ->method('buildSPILocationCreateStruct') |
||
2679 | ->with( |
||
2680 | $this->equalTo($locationCreateStruct), |
||
2681 | $this->equalTo($parentLocation), |
||
2682 | $this->equalTo(true), |
||
2683 | $this->equalTo(null), |
||
2684 | $this->equalTo(null) |
||
2685 | )->will($this->returnValue($spiLocationCreateStruct)); |
||
2686 | |||
2687 | $mockedService->createContent( |
||
2688 | $contentCreateStruct, |
||
2689 | $locationCreateStructs |
||
2690 | ); |
||
2691 | } |
||
2692 | |||
2693 | /** |
||
2694 | * Test for the createContent() method. |
||
2695 | * |
||
2696 | * @covers \eZ\Publish\Core\Repository\ContentService::getLanguageCodesForCreate |
||
2697 | * @covers \eZ\Publish\Core\Repository\ContentService::mapFieldsForCreate |
||
2698 | * @covers \eZ\Publish\Core\Repository\ContentService::getDefaultObjectStates |
||
2699 | * @covers \eZ\Publish\Core\Repository\ContentService::createContent |
||
2700 | */ |
||
2701 | public function testCreateContentObjectStates() |
||
2797 | |||
2798 | /** |
||
2799 | * Test for the createContent() method. |
||
2800 | * |
||
2801 | * @covers \eZ\Publish\Core\Repository\ContentService::getLanguageCodesForCreate |
||
2802 | * @covers \eZ\Publish\Core\Repository\ContentService::mapFieldsForCreate |
||
2803 | * @covers \eZ\Publish\Core\Repository\ContentService::getDefaultObjectStates |
||
2804 | * @covers \eZ\Publish\Core\Repository\ContentService::createContent |
||
2805 | * @dataProvider providerForTestCreateContentThrowsContentValidationExceptionTranslation |
||
2806 | */ |
||
2807 | public function testCreateContentWithRollback() |
||
2808 | { |
||
2809 | $this->expectException(\Exception::class); |
||
2810 | $this->expectExceptionMessage('Store failed'); |
||
2811 | |||
2812 | $fieldDefinitions = [ |
||
2813 | new FieldDefinition( |
||
2814 | [ |
||
2815 | 'id' => 'fieldDefinitionId', |
||
2816 | 'fieldTypeIdentifier' => 'fieldTypeIdentifier', |
||
2817 | 'isTranslatable' => false, |
||
2818 | 'identifier' => 'identifier', |
||
2819 | 'isRequired' => false, |
||
2820 | 'defaultValue' => 'defaultValue', |
||
2821 | ] |
||
2822 | ), |
||
2823 | ]; |
||
2824 | |||
2825 | // Setup a simple case that will pass |
||
2826 | $contentCreateStruct = $this->assertForTestCreateContentNonRedundantFieldSet( |
||
2827 | 'eng-US', |
||
2828 | [], |
||
2829 | [], |
||
2830 | $fieldDefinitions, |
||
2831 | [], |
||
2832 | false, |
||
2833 | // Do not execute test |
||
2834 | false |
||
2835 | ); |
||
2836 | |||
2837 | $repositoryMock = $this->getRepositoryMock(); |
||
2838 | $repositoryMock->expects($this->never())->method('commit'); |
||
2839 | $repositoryMock->expects($this->once())->method('rollback'); |
||
2840 | |||
2841 | /** @var \PHPUnit\Framework\MockObject\MockObject $contentHandlerMock */ |
||
2842 | $contentHandlerMock = $this->getPersistenceMock()->contentHandler(); |
||
2843 | $contentHandlerMock->expects($this->once()) |
||
2844 | ->method('create') |
||
2845 | ->with($this->anything()) |
||
2846 | ->will($this->throwException(new \Exception('Store failed'))); |
||
2847 | |||
2851 | |||
2852 | public function providerForTestUpdateContentThrowsBadStateException() |
||
2859 | |||
2860 | /** |
||
2861 | * Test for the updateContent() method. |
||
2862 | * |
||
2863 | * @covers \eZ\Publish\Core\Repository\ContentService::updateContent |
||
2864 | * @dataProvider providerForTestUpdateContentThrowsBadStateException |
||
2865 | */ |
||
2866 | public function testUpdateContentThrowsBadStateException($status) |
||
2898 | |||
2899 | /** |
||
2900 | * Test for the updateContent() method. |
||
2901 | * |
||
2902 | * @covers \eZ\Publish\Core\Repository\ContentService::updateContent |
||
2903 | */ |
||
2904 | public function testUpdateContentThrowsUnauthorizedException() |
||
2946 | |||
2947 | /** |
||
2948 | * @param string $initialLanguageCode |
||
2949 | * @param \eZ\Publish\API\Repository\Values\Content\Field[] $structFields |
||
2950 | * @param string[] $existingLanguages |
||
2951 | * |
||
2952 | * @return string[] |
||
2953 | */ |
||
2954 | protected function determineLanguageCodesForUpdate($initialLanguageCode, array $structFields, $existingLanguages) |
||
2971 | |||
2972 | /** |
||
2973 | * @param string $initialLanguageCode |
||
2974 | * @param \eZ\Publish\API\Repository\Values\Content\Field[] $structFields |
||
2975 | * @param string $mainLanguageCode |
||
2976 | * @param \eZ\Publish\API\Repository\Values\ContentType\FieldDefinition[] $fieldDefinitions |
||
2977 | * |
||
2978 | * @return array |
||
2979 | */ |
||
2980 | protected function mapStructFieldsForUpdate($initialLanguageCode, $structFields, $mainLanguageCode, $fieldDefinitions) |
||
3006 | |||
3007 | /** |
||
3008 | * Returns full, possibly redundant array of field values, indexed by field definition |
||
3009 | * identifier and language code. |
||
3010 | * |
||
3011 | * @param string $initialLanguageCode |
||
3012 | * @param \eZ\Publish\API\Repository\Values\Content\Field[] $structFields |
||
3013 | * @param \eZ\Publish\Core\Repository\Values\Content\Content $content |
||
3014 | * @param \eZ\Publish\API\Repository\Values\ContentType\FieldDefinition[] $fieldDefinitions |
||
3015 | * @param array $languageCodes |
||
3016 | * |
||
3017 | * @return array |
||
3018 | */ |
||
3019 | protected function determineValuesForUpdate( |
||
3065 | |||
3066 | protected function stubValues(array $fieldValues) |
||
3076 | |||
3077 | /** |
||
3078 | * Asserts that calling updateContent() with given API field set causes calling |
||
3079 | * Handler::updateContent() with given SPI field set. |
||
3080 | * |
||
3081 | * @param string $initialLanguageCode |
||
3082 | * @param \eZ\Publish\API\Repository\Values\Content\Field[] $structFields |
||
3083 | * @param \eZ\Publish\SPI\Persistence\Content\Field[] $spiFields |
||
3084 | * @param \eZ\Publish\API\Repository\Values\Content\Field[] $existingFields |
||
3085 | * @param \eZ\Publish\API\Repository\Values\ContentType\FieldDefinition[] $fieldDefinitions |
||
3086 | * @param bool $execute |
||
3087 | * |
||
3088 | * @return mixed |
||
3089 | */ |
||
3090 | protected function assertForTestUpdateContentNonRedundantFieldSet( |
||
3330 | |||
3331 | public function providerForTestUpdateContentNonRedundantFieldSet1() |
||
3383 | |||
3384 | /** |
||
3385 | * Test for the updateContent() method. |
||
3386 | * |
||
3387 | * Testing the simplest use case. |
||
3388 | * |
||
3389 | * @covers \eZ\Publish\Core\Repository\ContentService::getLanguageCodesForUpdate |
||
3390 | * @covers \eZ\Publish\Core\Repository\ContentService::mapFieldsForUpdate |
||
3391 | * @covers \eZ\Publish\Core\Repository\ContentService::updateContent |
||
3392 | * @dataProvider providerForTestUpdateContentNonRedundantFieldSet1 |
||
3393 | */ |
||
3394 | View Code Duplication | public function testUpdateContentNonRedundantFieldSet1($initialLanguageCode, $structFields, $spiFields) |
|
3428 | |||
3429 | public function providerForTestUpdateContentNonRedundantFieldSet2() |
||
3596 | |||
3597 | /** |
||
3598 | * Test for the updateContent() method. |
||
3599 | * |
||
3600 | * Testing with translatable field. |
||
3601 | * |
||
3602 | * @covers \eZ\Publish\Core\Repository\ContentService::getLanguageCodesForUpdate |
||
3603 | * @covers \eZ\Publish\Core\Repository\ContentService::mapFieldsForUpdate |
||
3604 | * @covers \eZ\Publish\Core\Repository\ContentService::updateContent |
||
3605 | * @dataProvider providerForTestUpdateContentNonRedundantFieldSet2 |
||
3606 | */ |
||
3607 | View Code Duplication | public function testUpdateContentNonRedundantFieldSet2($initialLanguageCode, $structFields, $spiFields) |
|
3641 | |||
3642 | public function providerForTestUpdateContentNonRedundantFieldSet3() |
||
3858 | |||
3859 | /** |
||
3860 | * Test for the updateContent() method. |
||
3861 | * |
||
3862 | * Testing with new language and untranslatable field. |
||
3863 | * |
||
3864 | * @covers \eZ\Publish\Core\Repository\ContentService::getLanguageCodesForUpdate |
||
3865 | * @covers \eZ\Publish\Core\Repository\ContentService::mapFieldsForUpdate |
||
3866 | * @covers \eZ\Publish\Core\Repository\ContentService::updateContent |
||
3867 | * @dataProvider providerForTestUpdateContentNonRedundantFieldSet3 |
||
3868 | */ |
||
3869 | public function testUpdateContentNonRedundantFieldSet3($initialLanguageCode, $structFields, $spiFields) |
||
3921 | |||
3922 | public function providerForTestUpdateContentNonRedundantFieldSet4() |
||
4163 | |||
4164 | /** |
||
4165 | * Test for the updateContent() method. |
||
4166 | * |
||
4167 | * Testing with empty values. |
||
4168 | * |
||
4169 | * @covers \eZ\Publish\Core\Repository\ContentService::getLanguageCodesForUpdate |
||
4170 | * @covers \eZ\Publish\Core\Repository\ContentService::mapFieldsForUpdate |
||
4171 | * @covers \eZ\Publish\Core\Repository\ContentService::updateContent |
||
4172 | * @dataProvider providerForTestUpdateContentNonRedundantFieldSet4 |
||
4173 | */ |
||
4174 | public function testUpdateContentNonRedundantFieldSet4($initialLanguageCode, $structFields, $spiFields) |
||
4226 | |||
4227 | /** |
||
4228 | * @todo add first field empty |
||
4229 | * |
||
4230 | * @return array |
||
4231 | */ |
||
4232 | public function providerForTestUpdateContentNonRedundantFieldSetComplex() |
||
4460 | |||
4461 | protected function fixturesForTestUpdateContentNonRedundantFieldSetComplex() |
||
4543 | |||
4544 | /** |
||
4545 | * Test for the updateContent() method. |
||
4546 | * |
||
4547 | * Testing more complex cases. |
||
4548 | * |
||
4549 | * @covers \eZ\Publish\Core\Repository\ContentService::getLanguageCodesForUpdate |
||
4550 | * @covers \eZ\Publish\Core\Repository\ContentService::mapFieldsForUpdate |
||
4551 | * @covers \eZ\Publish\Core\Repository\ContentService::updateContent |
||
4552 | * @dataProvider providerForTestUpdateContentNonRedundantFieldSetComplex |
||
4553 | */ |
||
4554 | public function testUpdateContentNonRedundantFieldSetComplex($initialLanguageCode, $structFields, $spiFields) |
||
4566 | |||
4567 | View Code Duplication | public function providerForTestUpdateContentWithInvalidLanguage() |
|
4596 | |||
4597 | /** |
||
4598 | * Test for the updateContent() method. |
||
4599 | * |
||
4600 | * @covers \eZ\Publish\Core\Repository\ContentService::getLanguageCodesForUpdate |
||
4601 | * @covers \eZ\Publish\Core\Repository\ContentService::updateContent |
||
4602 | * @dataProvider providerForTestUpdateContentWithInvalidLanguage |
||
4603 | */ |
||
4604 | public function testUpdateContentWithInvalidLanguage($initialLanguageCode, $structFields) |
||
4678 | |||
4679 | protected function assertForUpdateContentContentValidationException( |
||
4764 | |||
4765 | View Code Duplication | public function providerForTestUpdateContentThrowsContentValidationExceptionFieldDefinition() |
|
4782 | |||
4783 | /** |
||
4784 | * Test for the updateContent() method. |
||
4785 | * |
||
4786 | * @covers \eZ\Publish\Core\Repository\ContentService::getLanguageCodesForUpdate |
||
4787 | * @covers \eZ\Publish\Core\Repository\ContentService::mapFieldsForUpdate |
||
4788 | * @covers \eZ\Publish\Core\Repository\ContentService::updateContent |
||
4789 | * @dataProvider providerForTestUpdateContentThrowsContentValidationExceptionFieldDefinition |
||
4790 | */ |
||
4791 | public function testUpdateContentThrowsContentValidationExceptionFieldDefinition($initialLanguageCode, $structFields) |
||
4802 | |||
4803 | View Code Duplication | public function providerForTestUpdateContentThrowsContentValidationExceptionTranslation() |
|
4820 | |||
4821 | /** |
||
4822 | * Test for the updateContent() method. |
||
4823 | * |
||
4824 | * @covers \eZ\Publish\Core\Repository\ContentService::getLanguageCodesForUpdate |
||
4825 | * @covers \eZ\Publish\Core\Repository\ContentService::mapFieldsForUpdate |
||
4826 | * @covers \eZ\Publish\Core\Repository\ContentService::updateContent |
||
4827 | * @dataProvider providerForTestUpdateContentThrowsContentValidationExceptionTranslation |
||
4828 | */ |
||
4829 | View Code Duplication | public function testUpdateContentThrowsContentValidationExceptionTranslation($initialLanguageCode, $structFields) |
|
4853 | |||
4854 | public function assertForTestUpdateContentRequiredField( |
||
4976 | |||
4977 | View Code Duplication | public function providerForTestUpdateContentRequiredField() |
|
4996 | |||
4997 | /** |
||
4998 | * Test for the updateContent() method. |
||
4999 | * |
||
5000 | * @covers \eZ\Publish\Core\Repository\ContentService::getLanguageCodesForUpdate |
||
5001 | * @covers \eZ\Publish\Core\Repository\ContentService::mapFieldsForUpdate |
||
5002 | * @covers \eZ\Publish\Core\Repository\ContentService::updateContent |
||
5003 | * @dataProvider providerForTestUpdateContentRequiredField |
||
5004 | */ |
||
5005 | public function testUpdateContentRequiredField( |
||
5054 | |||
5055 | public function assertForTestUpdateContentThrowsContentFieldValidationException( |
||
5188 | |||
5189 | public function providerForTestUpdateContentThrowsContentFieldValidationException() |
||
5310 | |||
5311 | /** |
||
5312 | * Test for the updateContent() method. |
||
5313 | * |
||
5314 | * @covers \eZ\Publish\Core\Repository\ContentService::getLanguageCodesForUpdate |
||
5315 | * @covers \eZ\Publish\Core\Repository\ContentService::mapFieldsForUpdate |
||
5316 | * @covers \eZ\Publish\Core\Repository\ContentService::updateContent |
||
5317 | * @dataProvider providerForTestUpdateContentThrowsContentFieldValidationException |
||
5318 | */ |
||
5319 | View Code Duplication | public function testUpdateContentThrowsContentFieldValidationException($initialLanguageCode, $structFields, $spiField, $allFieldErrors) |
|
5340 | |||
5341 | /** |
||
5342 | * Test for the updateContent() method. |
||
5343 | * |
||
5344 | * @covers \eZ\Publish\Core\Repository\ContentService::getLanguageCodesForUpdate |
||
5345 | * @covers \eZ\Publish\Core\Repository\ContentService::mapFieldsForUpdate |
||
5346 | * @covers \eZ\Publish\Core\Repository\ContentService::updateContent |
||
5347 | */ |
||
5348 | public function testUpdateContentTransactionRollback() |
||
5405 | |||
5406 | /** |
||
5407 | * Test for the copyContent() method. |
||
5408 | * |
||
5409 | * @covers \eZ\Publish\Core\Repository\ContentService::copyContent |
||
5410 | */ |
||
5411 | public function testCopyContentThrowsUnauthorizedException() |
||
5454 | |||
5455 | /** |
||
5456 | * Test for the copyContent() method. |
||
5457 | * |
||
5458 | * @covers \eZ\Publish\Core\Repository\ContentService::copyContent |
||
5459 | * @covers \eZ\Publish\Core\Repository\ContentService::getDefaultObjectStates |
||
5460 | * @covers \eZ\Publish\Core\Repository\ContentService::internalPublishVersion |
||
5461 | */ |
||
5462 | public function testCopyContent() |
||
5580 | |||
5581 | /** |
||
5582 | * Test for the copyContent() method. |
||
5583 | * |
||
5584 | * @covers \eZ\Publish\Core\Repository\ContentService::copyContent |
||
5585 | * @covers \eZ\Publish\Core\Repository\ContentService::getDefaultObjectStates |
||
5586 | * @covers \eZ\Publish\Core\Repository\ContentService::internalPublishVersion |
||
5587 | */ |
||
5588 | public function testCopyContentWithVersionInfo() |
||
5704 | |||
5705 | /** |
||
5706 | * Test for the copyContent() method. |
||
5707 | * |
||
5708 | * @covers \eZ\Publish\Core\Repository\ContentService::copyContent |
||
5709 | * @covers \eZ\Publish\Core\Repository\ContentService::getDefaultObjectStates |
||
5710 | * @covers \eZ\Publish\Core\Repository\ContentService::internalPublishVersion |
||
5711 | */ |
||
5712 | public function testCopyContentWithRollback() |
||
5775 | |||
5776 | /** |
||
5777 | * Reusable method for setting exceptions on buildContentDomainObject usage. |
||
5778 | * |
||
5779 | * Plain usage as in when content type is loaded directly. |
||
5780 | * |
||
5781 | * @param \eZ\Publish\SPI\Persistence\Content $spiContent |
||
5782 | * @param array $translations |
||
5783 | * @param bool $useAlwaysAvailable |
||
5784 | * |
||
5785 | * @return \PHPUnit\Framework\MockObject\MockObject|\eZ\Publish\API\Repository\Values\Content\Content |
||
5786 | */ |
||
5787 | private function mockBuildContentDomainObject(SPIContent $spiContent, array $translations = null, bool $useAlwaysAvailable = null) |
||
5817 | |||
5818 | protected function mockGetDefaultObjectStates() |
||
5857 | |||
5858 | protected function mockSetDefaultObjectStates() |
||
5877 | |||
5878 | /** |
||
5879 | * @param int|null $publicationDate |
||
5880 | * @param int|null $modificationDate |
||
5881 | * |
||
5882 | * @return \eZ\Publish\API\Repository\Values\Content\Content |
||
5883 | */ |
||
5884 | protected function mockPublishVersion($publicationDate = null, $modificationDate = null) |
||
5959 | |||
5960 | /** |
||
5961 | * @param \eZ\Publish\API\Repository\Values\Content\Content $content |
||
5962 | */ |
||
5963 | protected function mockPublishUrlAliasesForContent(APIContent $content) |
||
6009 | |||
6010 | protected $domainMapperMock; |
||
6011 | |||
6012 | /** |
||
6013 | * @return \PHPUnit\Framework\MockObject\MockObject|\eZ\Publish\Core\Repository\Helper\DomainMapper |
||
6014 | */ |
||
6015 | protected function getDomainMapperMock() |
||
6023 | |||
6024 | protected $relationProcessorMock; |
||
6025 | |||
6026 | /** |
||
6027 | * @return \PHPUnit\Framework\MockObject\MockObject|\eZ\Publish\Core\Repository\Helper\RelationProcessor |
||
6028 | */ |
||
6029 | protected function getRelationProcessorMock() |
||
6037 | |||
6038 | protected $nameSchemaServiceMock; |
||
6039 | |||
6040 | /** |
||
6041 | * @return \PHPUnit\Framework\MockObject\MockObject|\eZ\Publish\Core\Repository\Helper\NameSchemaService |
||
6042 | */ |
||
6043 | protected function getNameSchemaServiceMock() |
||
6051 | |||
6052 | protected $contentTypeServiceMock; |
||
6053 | |||
6054 | /** |
||
6055 | * @return \PHPUnit\Framework\MockObject\MockObject|\eZ\Publish\API\Repository\ContentTypeService |
||
6056 | */ |
||
6057 | protected function getContentTypeServiceMock() |
||
6065 | |||
6066 | protected $locationServiceMock; |
||
6067 | |||
6068 | /** |
||
6069 | * @return \PHPUnit\Framework\MockObject\MockObject|\eZ\Publish\API\Repository\LocationService |
||
6070 | */ |
||
6071 | protected function getLocationServiceMock() |
||
6079 | |||
6080 | /** @var \eZ\Publish\Core\Repository\ContentService */ |
||
6081 | protected $partlyMockedContentService; |
||
6082 | |||
6083 | /** |
||
6084 | * Returns the content service to test with $methods mocked. |
||
6085 | * |
||
6086 | * Injected Repository comes from {@see getRepositoryMock()} and persistence handler from {@see getPersistenceMock()} |
||
6087 | * |
||
6088 | * @param string[] $methods |
||
6089 | * |
||
6090 | * @return \eZ\Publish\Core\Repository\ContentService|\PHPUnit\Framework\MockObject\MockObject |
||
6091 | */ |
||
6092 | protected function getPartlyMockedContentService(array $methods = null) |
||
6114 | |||
6115 | /** |
||
6116 | * @return \eZ\Publish\API\Repository\Repository|\PHPUnit\Framework\MockObject\MockObject |
||
6117 | */ |
||
6118 | protected function getRepositoryMock(): Repository |
||
6128 | } |
||
6129 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.