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. |
||
| 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->once()) |
||
| 115 | ->method('loadContentInfo') |
||
| 116 | ->with($this->equalTo(42)) |
||
| 117 | ->will( |
||
| 118 | $this->returnValue( |
||
| 119 | new ContentInfo(['currentVersionNo' => 24]) |
||
| 120 | ) |
||
| 121 | ); |
||
| 122 | |||
| 123 | $contentHandler->expects($this->once()) |
||
| 124 | ->method('loadVersionInfo') |
||
| 125 | ->with( |
||
| 126 | $this->equalTo(42), |
||
| 127 | $this->equalTo(24) |
||
| 128 | )->will( |
||
| 129 | $this->returnValue(new SPIVersionInfo()) |
||
| 130 | ); |
||
| 131 | |||
| 132 | $domainMapperMock->expects($this->once()) |
||
| 133 | ->method('buildVersionInfoDomainObject') |
||
| 134 | ->with(new SPIVersionInfo()) |
||
| 135 | ->will($this->returnValue($versionInfoMock)); |
||
| 136 | |||
| 137 | $permissionResolver->expects($this->once()) |
||
| 138 | ->method('canUser') |
||
| 139 | ->with( |
||
| 140 | $this->equalTo('content'), |
||
| 141 | $this->equalTo('read'), |
||
| 142 | $this->equalTo($versionInfoMock) |
||
| 143 | )->will($this->returnValue(true)); |
||
| 144 | |||
| 145 | $result = $contentServiceMock->loadVersionInfoById(42); |
||
| 146 | |||
| 147 | $this->assertEquals($versionInfoMock, $result); |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Test for the loadVersionInfo() method. |
||
| 152 | * |
||
| 153 | * @covers \eZ\Publish\Core\Repository\ContentService::loadVersionInfoById |
||
| 154 | */ |
||
| 155 | public function testLoadVersionInfoByIdThrowsNotFoundException() |
||
| 156 | { |
||
| 157 | $this->expectException(\eZ\Publish\Core\Base\Exceptions\NotFoundException::class); |
||
| 158 | |||
| 159 | $contentServiceMock = $this->getPartlyMockedContentService(['loadContentInfo']); |
||
| 160 | /** @var \PHPUnit\Framework\MockObject\MockObject $contentHandler */ |
||
| 161 | $contentHandler = $this->getPersistenceMock()->contentHandler(); |
||
| 162 | |||
| 163 | $contentHandler->expects($this->once()) |
||
| 164 | ->method('loadVersionInfo') |
||
| 165 | ->with( |
||
| 166 | $this->equalTo(42), |
||
| 167 | $this->equalTo(24) |
||
| 168 | )->will( |
||
| 169 | $this->throwException( |
||
| 170 | new NotFoundException( |
||
| 171 | 'Content', |
||
| 172 | [ |
||
| 173 | 'contentId' => 42, |
||
| 174 | 'versionNo' => 24, |
||
| 175 | ] |
||
| 176 | ) |
||
| 177 | ) |
||
| 178 | ); |
||
| 179 | |||
| 180 | $contentServiceMock->loadVersionInfoById(42, 24); |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Test for the loadVersionInfo() method. |
||
| 185 | * |
||
| 186 | * @covers \eZ\Publish\Core\Repository\ContentService::loadVersionInfoById |
||
| 187 | */ |
||
| 188 | public function testLoadVersionInfoByIdThrowsUnauthorizedExceptionNonPublishedVersion() |
||
| 189 | { |
||
| 190 | $this->expectException(\eZ\Publish\Core\Base\Exceptions\UnauthorizedException::class); |
||
| 191 | |||
| 192 | $repository = $this->getRepositoryMock(); |
||
| 193 | $contentServiceMock = $this->getPartlyMockedContentService(); |
||
| 194 | /** @var \PHPUnit\Framework\MockObject\MockObject $contentHandler */ |
||
| 195 | $contentHandler = $this->getPersistenceMock()->contentHandler(); |
||
| 196 | $domainMapperMock = $this->getDomainMapperMock(); |
||
| 197 | $versionInfoMock = $this->createMock(APIVersionInfo::class); |
||
| 198 | $permissionResolver = $this->getPermissionResolverMock(); |
||
| 199 | |||
| 200 | $versionInfoMock->expects($this->any()) |
||
| 201 | ->method('isPublished') |
||
| 202 | ->willReturn(false); |
||
| 203 | |||
| 204 | $contentHandler->expects($this->once()) |
||
| 205 | ->method('loadVersionInfo') |
||
| 206 | ->with( |
||
| 207 | $this->equalTo(42), |
||
| 208 | $this->equalTo(24) |
||
| 209 | )->will( |
||
| 210 | $this->returnValue(new SPIVersionInfo()) |
||
| 211 | ); |
||
| 212 | |||
| 213 | $domainMapperMock->expects($this->once()) |
||
| 214 | ->method('buildVersionInfoDomainObject') |
||
| 215 | ->with(new SPIVersionInfo()) |
||
| 216 | ->will($this->returnValue($versionInfoMock)); |
||
| 217 | |||
| 218 | $permissionResolver->expects($this->once()) |
||
| 219 | ->method('canUser') |
||
| 220 | ->with( |
||
| 221 | $this->equalTo('content'), |
||
| 222 | $this->equalTo('versionread'), |
||
| 223 | $this->equalTo($versionInfoMock) |
||
| 224 | )->will($this->returnValue(false)); |
||
| 225 | |||
| 226 | $contentServiceMock->loadVersionInfoById(42, 24); |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Test for the loadVersionInfo() method. |
||
| 231 | * |
||
| 232 | * @covers \eZ\Publish\Core\Repository\ContentService::loadVersionInfoById |
||
| 233 | */ |
||
| 234 | View Code Duplication | public function testLoadVersionInfoByIdPublishedVersion() |
|
| 235 | { |
||
| 236 | $repository = $this->getRepositoryMock(); |
||
| 237 | $contentServiceMock = $this->getPartlyMockedContentService(); |
||
| 238 | /** @var \PHPUnit\Framework\MockObject\MockObject $contentHandler */ |
||
| 239 | $contentHandler = $this->getPersistenceMock()->contentHandler(); |
||
| 240 | $domainMapperMock = $this->getDomainMapperMock(); |
||
| 241 | $versionInfoMock = $this->createMock(APIVersionInfo::class); |
||
| 242 | $permissionResolver = $this->getPermissionResolverMock(); |
||
| 243 | |||
| 244 | $versionInfoMock->expects($this->once()) |
||
| 245 | ->method('isPublished') |
||
| 246 | ->willReturn(true); |
||
| 247 | |||
| 248 | $contentHandler->expects($this->once()) |
||
| 249 | ->method('loadVersionInfo') |
||
| 250 | ->with( |
||
| 251 | $this->equalTo(42), |
||
| 252 | $this->equalTo(24) |
||
| 253 | )->will( |
||
| 254 | $this->returnValue(new SPIVersionInfo()) |
||
| 255 | ); |
||
| 256 | |||
| 257 | $domainMapperMock->expects($this->once()) |
||
| 258 | ->method('buildVersionInfoDomainObject') |
||
| 259 | ->with(new SPIVersionInfo()) |
||
| 260 | ->will($this->returnValue($versionInfoMock)); |
||
| 261 | |||
| 262 | $permissionResolver->expects($this->once()) |
||
| 263 | ->method('canUser') |
||
| 264 | ->with( |
||
| 265 | $this->equalTo('content'), |
||
| 266 | $this->equalTo('read'), |
||
| 267 | $this->equalTo($versionInfoMock) |
||
| 268 | )->will($this->returnValue(true)); |
||
| 269 | |||
| 270 | $result = $contentServiceMock->loadVersionInfoById(42, 24); |
||
| 271 | |||
| 272 | $this->assertEquals($versionInfoMock, $result); |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Test for the loadVersionInfo() method. |
||
| 277 | * |
||
| 278 | * @covers \eZ\Publish\Core\Repository\ContentService::loadVersionInfoById |
||
| 279 | */ |
||
| 280 | View Code Duplication | public function testLoadVersionInfoByIdNonPublishedVersion() |
|
| 281 | { |
||
| 282 | $repository = $this->getRepositoryMock(); |
||
| 283 | $contentServiceMock = $this->getPartlyMockedContentService(); |
||
| 284 | /** @var \PHPUnit\Framework\MockObject\MockObject $contentHandler */ |
||
| 285 | $contentHandler = $this->getPersistenceMock()->contentHandler(); |
||
| 286 | $domainMapperMock = $this->getDomainMapperMock(); |
||
| 287 | $versionInfoMock = $this->createMock(APIVersionInfo::class); |
||
| 288 | $permissionResolver = $this->getPermissionResolverMock(); |
||
| 289 | |||
| 290 | $versionInfoMock->expects($this->once()) |
||
| 291 | ->method('isPublished') |
||
| 292 | ->willReturn(false); |
||
| 293 | |||
| 294 | $contentHandler->expects($this->once()) |
||
| 295 | ->method('loadVersionInfo') |
||
| 296 | ->with( |
||
| 297 | $this->equalTo(42), |
||
| 298 | $this->equalTo(24) |
||
| 299 | )->will( |
||
| 300 | $this->returnValue(new SPIVersionInfo()) |
||
| 301 | ); |
||
| 302 | |||
| 303 | $domainMapperMock->expects($this->once()) |
||
| 304 | ->method('buildVersionInfoDomainObject') |
||
| 305 | ->with(new SPIVersionInfo()) |
||
| 306 | ->will($this->returnValue($versionInfoMock)); |
||
| 307 | |||
| 308 | $permissionResolver->expects($this->once()) |
||
| 309 | ->method('canUser') |
||
| 310 | ->with( |
||
| 311 | $this->equalTo('content'), |
||
| 312 | $this->equalTo('versionread'), |
||
| 313 | $this->equalTo($versionInfoMock) |
||
| 314 | )->will($this->returnValue(true)); |
||
| 315 | |||
| 316 | $result = $contentServiceMock->loadVersionInfoById(42, 24); |
||
| 317 | |||
| 318 | $this->assertEquals($versionInfoMock, $result); |
||
| 319 | } |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Test for the loadVersionInfo() method. |
||
| 323 | * |
||
| 324 | * @covers \eZ\Publish\Core\Repository\ContentService::loadVersionInfo |
||
| 325 | * @depends eZ\Publish\Core\Repository\Tests\Service\Mock\ContentTest::testLoadVersionInfoById |
||
| 326 | * @depends eZ\Publish\Core\Repository\Tests\Service\Mock\ContentTest::testLoadVersionInfoByIdThrowsNotFoundException |
||
| 327 | * @depends eZ\Publish\Core\Repository\Tests\Service\Mock\ContentTest::testLoadVersionInfoByIdThrowsUnauthorizedExceptionNonPublishedVersion |
||
| 328 | * @depends eZ\Publish\Core\Repository\Tests\Service\Mock\ContentTest::testLoadVersionInfoByIdPublishedVersion |
||
| 329 | * @depends eZ\Publish\Core\Repository\Tests\Service\Mock\ContentTest::testLoadVersionInfoByIdNonPublishedVersion |
||
| 330 | */ |
||
| 331 | public function testLoadVersionInfo() |
||
| 332 | { |
||
| 333 | $contentServiceMock = $this->getPartlyMockedContentService( |
||
| 334 | ['loadVersionInfoById'] |
||
| 335 | ); |
||
| 336 | $contentServiceMock->expects( |
||
| 337 | $this->once() |
||
| 338 | )->method( |
||
| 339 | 'loadVersionInfoById' |
||
| 340 | )->with( |
||
| 341 | $this->equalTo(42), |
||
| 342 | $this->equalTo(7) |
||
| 343 | )->will( |
||
| 344 | $this->returnValue('result') |
||
| 345 | ); |
||
| 346 | |||
| 347 | $result = $contentServiceMock->loadVersionInfo( |
||
| 348 | new ContentInfo(['id' => 42]), |
||
| 349 | 7 |
||
| 350 | ); |
||
| 351 | |||
| 352 | $this->assertEquals('result', $result); |
||
| 353 | } |
||
| 354 | |||
| 355 | public function testLoadContent() |
||
| 356 | { |
||
| 357 | $repository = $this->getRepositoryMock(); |
||
| 358 | $contentService = $this->getPartlyMockedContentService(['internalLoadContent']); |
||
| 359 | $content = $this->createMock(APIContent::class); |
||
| 360 | $versionInfo = $this->createMock(APIVersionInfo::class); |
||
| 361 | $permissionResolver = $this->getPermissionResolverMock(); |
||
| 362 | |||
| 363 | $content |
||
| 364 | ->expects($this->once()) |
||
| 365 | ->method('getVersionInfo') |
||
| 366 | ->will($this->returnValue($versionInfo)); |
||
| 367 | $versionInfo |
||
| 368 | ->expects($this->once()) |
||
| 369 | ->method('isPublished') |
||
| 370 | ->willReturn(true); |
||
| 371 | $contentId = 123; |
||
| 372 | $contentService |
||
| 373 | ->expects($this->once()) |
||
| 374 | ->method('internalLoadContent') |
||
| 375 | ->with($contentId) |
||
| 376 | ->will($this->returnValue($content)); |
||
| 377 | |||
| 378 | $permissionResolver |
||
| 379 | ->expects($this->once()) |
||
| 380 | ->method('canUser') |
||
| 381 | ->with('content', 'read', $content) |
||
| 382 | ->will($this->returnValue(true)); |
||
| 383 | |||
| 384 | $this->assertSame($content, $contentService->loadContent($contentId)); |
||
| 385 | } |
||
| 386 | |||
| 387 | View Code Duplication | public function testLoadContentNonPublished() |
|
| 388 | { |
||
| 389 | $repository = $this->getRepositoryMock(); |
||
| 390 | $contentService = $this->getPartlyMockedContentService(['internalLoadContent']); |
||
| 391 | $content = $this->createMock(APIContent::class); |
||
| 392 | $versionInfo = $this->createMock(APIVersionInfo::class); |
||
| 393 | $permissionResolver = $this->getPermissionResolverMock(); |
||
| 394 | |||
| 395 | $content |
||
| 396 | ->expects($this->once()) |
||
| 397 | ->method('getVersionInfo') |
||
| 398 | ->will($this->returnValue($versionInfo)); |
||
| 399 | $contentId = 123; |
||
| 400 | $contentService |
||
| 401 | ->expects($this->once()) |
||
| 402 | ->method('internalLoadContent') |
||
| 403 | ->with($contentId) |
||
| 404 | ->will($this->returnValue($content)); |
||
| 405 | |||
| 406 | $permissionResolver |
||
| 407 | ->expects($this->exactly(2)) |
||
| 408 | ->method('canUser') |
||
| 409 | ->will( |
||
| 410 | $this->returnValueMap( |
||
| 411 | [ |
||
| 412 | ['content', 'read', $content, [], true], |
||
| 413 | ['content', 'versionread', $content, [], true], |
||
| 414 | ] |
||
| 415 | ) |
||
| 416 | ); |
||
| 417 | |||
| 418 | $this->assertSame($content, $contentService->loadContent($contentId)); |
||
| 419 | } |
||
| 420 | |||
| 421 | public function testLoadContentUnauthorized() |
||
| 422 | { |
||
| 423 | $this->expectException(\eZ\Publish\Core\Base\Exceptions\UnauthorizedException::class); |
||
| 424 | |||
| 425 | $repository = $this->getRepositoryMock(); |
||
| 426 | $permissionResolver = $this->getPermissionResolverMock(); |
||
| 427 | |||
| 428 | $contentService = $this->getPartlyMockedContentService(['internalLoadContent']); |
||
| 429 | $content = $this->createMock(APIContent::class); |
||
| 430 | $contentId = 123; |
||
| 431 | $contentService |
||
| 432 | ->expects($this->once()) |
||
| 433 | ->method('internalLoadContent') |
||
| 434 | ->with($contentId) |
||
| 435 | ->will($this->returnValue($content)); |
||
| 436 | |||
| 437 | $permissionResolver |
||
| 438 | ->expects($this->once()) |
||
| 439 | ->method('canUser') |
||
| 440 | ->with('content', 'read', $content) |
||
| 441 | ->will($this->returnValue(false)); |
||
| 442 | |||
| 443 | $contentService->loadContent($contentId); |
||
| 444 | } |
||
| 445 | |||
| 446 | View Code Duplication | public function testLoadContentNotPublishedStatusUnauthorized() |
|
| 447 | { |
||
| 448 | $this->expectException(\eZ\Publish\Core\Base\Exceptions\UnauthorizedException::class); |
||
| 449 | |||
| 450 | $repository = $this->getRepositoryMock(); |
||
| 451 | $permissionResolver = $this->getPermissionResolverMock(); |
||
| 452 | $contentService = $this->getPartlyMockedContentService(['internalLoadContent']); |
||
| 453 | $content = $this->createMock(APIContent::class); |
||
| 454 | $versionInfo = $this |
||
| 455 | ->getMockBuilder(APIVersionInfo::class) |
||
| 456 | ->getMockForAbstractClass(); |
||
| 457 | $content |
||
| 458 | ->expects($this->once()) |
||
| 459 | ->method('getVersionInfo') |
||
| 460 | ->will($this->returnValue($versionInfo)); |
||
| 461 | $contentId = 123; |
||
| 462 | $contentService |
||
| 463 | ->expects($this->once()) |
||
| 464 | ->method('internalLoadContent') |
||
| 465 | ->with($contentId) |
||
| 466 | ->will($this->returnValue($content)); |
||
| 467 | |||
| 468 | $permissionResolver |
||
| 469 | ->expects($this->exactly(2)) |
||
| 470 | ->method('canUser') |
||
| 471 | ->will( |
||
| 472 | $this->returnValueMap( |
||
| 473 | [ |
||
| 474 | ['content', 'read', $content, [], true], |
||
| 475 | ['content', 'versionread', $content, [], false], |
||
| 476 | ] |
||
| 477 | ) |
||
| 478 | ); |
||
| 479 | |||
| 480 | $contentService->loadContent($contentId); |
||
| 481 | } |
||
| 482 | |||
| 483 | /** |
||
| 484 | * @dataProvider internalLoadContentProvider |
||
| 485 | */ |
||
| 486 | public function testInternalLoadContent($id, $languages, $versionNo, $isRemoteId, $useAlwaysAvailable) |
||
| 487 | { |
||
| 488 | $contentService = $this->getPartlyMockedContentService(); |
||
| 489 | /** @var \PHPUnit\Framework\MockObject\MockObject $contentHandler */ |
||
| 490 | $contentHandler = $this->getPersistenceMock()->contentHandler(); |
||
| 491 | $realId = $id; |
||
| 492 | |||
| 493 | if ($isRemoteId) { |
||
| 494 | $realId = 123; |
||
| 495 | $spiContentInfo = new SPIContentInfo(['currentVersionNo' => $versionNo ?: 7, 'id' => $realId]); |
||
| 496 | $contentHandler |
||
| 497 | ->expects($this->once()) |
||
| 498 | ->method('loadContentInfoByRemoteId') |
||
| 499 | ->with($id) |
||
| 500 | ->will($this->returnValue($spiContentInfo)); |
||
| 501 | } elseif (!empty($languages) && $useAlwaysAvailable) { |
||
| 502 | $spiContentInfo = new SPIContentInfo(['alwaysAvailable' => false]); |
||
| 503 | $contentHandler |
||
| 504 | ->expects($this->once()) |
||
| 505 | ->method('loadContentInfo') |
||
| 506 | ->with($id) |
||
| 507 | ->will($this->returnValue($spiContentInfo)); |
||
| 508 | } |
||
| 509 | |||
| 510 | $spiContent = new SPIContent([ |
||
| 511 | 'versionInfo' => new VersionInfo([ |
||
| 512 | 'contentInfo' => new ContentInfo(['id' => 42, 'contentTypeId' => 123]), |
||
| 513 | ]), |
||
| 514 | ]); |
||
| 515 | $contentHandler |
||
| 516 | ->expects($this->once()) |
||
| 517 | ->method('load') |
||
| 518 | ->with($realId, $versionNo, $languages) |
||
| 519 | ->willReturn($spiContent); |
||
| 520 | |||
| 521 | $content = $this->mockBuildContentDomainObject($spiContent, $languages); |
||
| 522 | |||
| 523 | $this->assertSame( |
||
| 524 | $content, |
||
| 525 | $contentService->internalLoadContent($id, $languages, $versionNo, $isRemoteId, $useAlwaysAvailable) |
||
| 526 | ); |
||
| 527 | } |
||
| 528 | |||
| 529 | public function internalLoadContentProvider() |
||
| 530 | { |
||
| 531 | return [ |
||
| 532 | [123, null, null, false, false], |
||
| 533 | [123, null, 456, false, false], |
||
| 534 | [456, null, 123, false, true], |
||
| 535 | [456, null, 2, false, false], |
||
| 536 | [456, ['eng-GB'], 2, false, true], |
||
| 537 | [456, ['eng-GB', 'fre-FR'], null, false, false], |
||
| 538 | [456, ['eng-GB', 'fre-FR', 'nor-NO'], 2, false, false], |
||
| 539 | // With remoteId |
||
| 540 | [123, null, null, true, false], |
||
| 541 | ['someRemoteId', null, 456, true, false], |
||
| 542 | [456, null, 123, true, false], |
||
| 543 | ['someRemoteId', null, 2, true, false], |
||
| 544 | ['someRemoteId', ['eng-GB'], 2, true, false], |
||
| 545 | [456, ['eng-GB', 'fre-FR'], null, true, false], |
||
| 546 | ['someRemoteId', ['eng-GB', 'fre-FR', 'nor-NO'], 2, true, false], |
||
| 547 | ]; |
||
| 548 | } |
||
| 549 | |||
| 550 | public function testInternalLoadContentNotFound() |
||
| 551 | { |
||
| 552 | $this->expectException(\eZ\Publish\Core\Base\Exceptions\NotFoundException::class); |
||
| 553 | |||
| 554 | $contentService = $this->getPartlyMockedContentService(); |
||
| 555 | /** @var \PHPUnit\Framework\MockObject\MockObject $contentHandler */ |
||
| 556 | $contentHandler = $this->getPersistenceMock()->contentHandler(); |
||
| 557 | $id = 123; |
||
| 558 | $versionNo = 7; |
||
| 559 | $languages = null; |
||
| 560 | $contentHandler |
||
| 561 | ->expects($this->once()) |
||
| 562 | ->method('load') |
||
| 563 | ->with($id, $versionNo, $languages) |
||
| 564 | ->will( |
||
| 565 | $this->throwException( |
||
| 566 | $this->createMock(APINotFoundException::class) |
||
| 567 | ) |
||
| 568 | ); |
||
| 569 | |||
| 570 | $contentService->internalLoadContent($id, $languages, $versionNo); |
||
| 571 | } |
||
| 572 | |||
| 573 | /** |
||
| 574 | * Test for the loadContentByContentInfo() method. |
||
| 575 | * |
||
| 576 | * @covers \eZ\Publish\Core\Repository\ContentService::loadContentByContentInfo |
||
| 577 | */ |
||
| 578 | public function testLoadContentByContentInfo() |
||
| 579 | { |
||
| 580 | $contentServiceMock = $this->getPartlyMockedContentService( |
||
| 581 | ['loadContent'] |
||
| 582 | ); |
||
| 583 | $contentServiceMock->expects( |
||
| 584 | $this->once() |
||
| 585 | )->method( |
||
| 586 | 'loadContent' |
||
| 587 | )->with( |
||
| 588 | $this->equalTo(42), |
||
| 589 | $this->equalTo(['cro-HR']), |
||
| 590 | $this->equalTo(7), |
||
| 591 | $this->equalTo(false) |
||
| 592 | )->will( |
||
| 593 | $this->returnValue('result') |
||
| 594 | ); |
||
| 595 | |||
| 596 | $result = $contentServiceMock->loadContentByContentInfo( |
||
| 597 | new ContentInfo(['id' => 42]), |
||
| 598 | ['cro-HR'], |
||
| 599 | 7 |
||
| 600 | ); |
||
| 601 | |||
| 602 | $this->assertEquals('result', $result); |
||
| 603 | } |
||
| 604 | |||
| 605 | /** |
||
| 606 | * Test for the loadContentByVersionInfo() method. |
||
| 607 | * |
||
| 608 | * @covers \eZ\Publish\Core\Repository\ContentService::loadContentByVersionInfo |
||
| 609 | */ |
||
| 610 | public function testLoadContentByVersionInfo() |
||
| 611 | { |
||
| 612 | $contentServiceMock = $this->getPartlyMockedContentService( |
||
| 613 | ['loadContent'] |
||
| 614 | ); |
||
| 615 | $contentServiceMock->expects( |
||
| 616 | $this->once() |
||
| 617 | )->method( |
||
| 618 | 'loadContent' |
||
| 619 | )->with( |
||
| 620 | $this->equalTo(42), |
||
| 621 | $this->equalTo(['cro-HR']), |
||
| 622 | $this->equalTo(7), |
||
| 623 | $this->equalTo(false) |
||
| 624 | )->will( |
||
| 625 | $this->returnValue('result') |
||
| 626 | ); |
||
| 627 | |||
| 628 | $result = $contentServiceMock->loadContentByVersionInfo( |
||
| 629 | new VersionInfo( |
||
| 630 | [ |
||
| 631 | 'contentInfo' => new ContentInfo(['id' => 42]), |
||
| 632 | 'versionNo' => 7, |
||
| 633 | ] |
||
| 634 | ), |
||
| 635 | ['cro-HR'] |
||
| 636 | ); |
||
| 637 | |||
| 638 | $this->assertEquals('result', $result); |
||
| 639 | } |
||
| 640 | |||
| 641 | /** |
||
| 642 | * Test for the deleteContent() method. |
||
| 643 | * |
||
| 644 | * @covers \eZ\Publish\Core\Repository\ContentService::deleteContent |
||
| 645 | */ |
||
| 646 | public function testDeleteContentThrowsUnauthorizedException() |
||
| 647 | { |
||
| 648 | $this->expectException(\eZ\Publish\Core\Base\Exceptions\UnauthorizedException::class); |
||
| 649 | |||
| 650 | $repository = $this->getRepositoryMock(); |
||
| 651 | $permissionResolver = $this->getPermissionResolverMock(); |
||
| 652 | $contentService = $this->getPartlyMockedContentService(['internalLoadContentInfo']); |
||
| 653 | $contentInfo = $this->createMock(APIContentInfo::class); |
||
| 654 | |||
| 655 | $contentInfo->expects($this->any()) |
||
| 656 | ->method('__get') |
||
| 657 | ->with('id') |
||
| 658 | ->will($this->returnValue(42)); |
||
| 659 | |||
| 660 | $contentService->expects($this->once()) |
||
| 661 | ->method('internalLoadContentInfo') |
||
| 662 | ->with(42) |
||
| 663 | ->will($this->returnValue($contentInfo)); |
||
| 664 | |||
| 665 | $permissionResolver->expects($this->once()) |
||
| 666 | ->method('canUser') |
||
| 667 | ->with('content', 'remove') |
||
| 668 | ->will($this->returnValue(false)); |
||
| 669 | |||
| 670 | /* @var \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo */ |
||
| 671 | $contentService->deleteContent($contentInfo); |
||
| 672 | } |
||
| 673 | |||
| 674 | /** |
||
| 675 | * Test for the deleteContent() method. |
||
| 676 | * |
||
| 677 | * @covers \eZ\Publish\Core\Repository\ContentService::deleteContent |
||
| 678 | */ |
||
| 679 | public function testDeleteContent() |
||
| 680 | { |
||
| 681 | $repository = $this->getRepositoryMock(); |
||
| 682 | $permissionResolver = $this->getPermissionResolverMock(); |
||
| 683 | |||
| 684 | $permissionResolver->expects($this->once()) |
||
| 685 | ->method('canUser') |
||
| 686 | ->with('content', 'remove') |
||
| 687 | ->will($this->returnValue(true)); |
||
| 688 | |||
| 689 | $contentService = $this->getPartlyMockedContentService(['internalLoadContentInfo']); |
||
| 690 | /** @var \PHPUnit\Framework\MockObject\MockObject $urlAliasHandler */ |
||
| 691 | $urlAliasHandler = $this->getPersistenceMock()->urlAliasHandler(); |
||
| 692 | /** @var \PHPUnit\Framework\MockObject\MockObject $locationHandler */ |
||
| 693 | $locationHandler = $this->getPersistenceMock()->locationHandler(); |
||
| 694 | /** @var \PHPUnit\Framework\MockObject\MockObject $contentHandler */ |
||
| 695 | $contentHandler = $this->getPersistenceMock()->contentHandler(); |
||
| 696 | |||
| 697 | $contentInfo = $this->createMock(APIContentInfo::class); |
||
| 698 | |||
| 699 | $contentService->expects($this->once()) |
||
| 700 | ->method('internalLoadContentInfo') |
||
| 701 | ->with(42) |
||
| 702 | ->will($this->returnValue($contentInfo)); |
||
| 703 | |||
| 704 | $contentInfo->expects($this->any()) |
||
| 705 | ->method('__get') |
||
| 706 | ->with('id') |
||
| 707 | ->will($this->returnValue(42)); |
||
| 708 | |||
| 709 | $repository->expects($this->once())->method('beginTransaction'); |
||
| 710 | |||
| 711 | $spiLocations = [ |
||
| 712 | new SPILocation(['id' => 1]), |
||
| 713 | new SPILocation(['id' => 2]), |
||
| 714 | ]; |
||
| 715 | $locationHandler->expects($this->once()) |
||
| 716 | ->method('loadLocationsByContent') |
||
| 717 | ->with(42) |
||
| 718 | ->will($this->returnValue($spiLocations)); |
||
| 719 | |||
| 720 | $contentHandler->expects($this->once()) |
||
| 721 | ->method('deleteContent') |
||
| 722 | ->with(42); |
||
| 723 | |||
| 724 | foreach ($spiLocations as $index => $spiLocation) { |
||
| 725 | $urlAliasHandler->expects($this->at($index)) |
||
| 726 | ->method('locationDeleted') |
||
| 727 | ->with($spiLocation->id); |
||
| 728 | } |
||
| 729 | |||
| 730 | $repository->expects($this->once())->method('commit'); |
||
| 731 | |||
| 732 | /* @var \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo */ |
||
| 733 | $contentService->deleteContent($contentInfo); |
||
| 734 | } |
||
| 735 | |||
| 736 | /** |
||
| 737 | * Test for the deleteContent() method. |
||
| 738 | * |
||
| 739 | * @covers \eZ\Publish\Core\Repository\ContentService::deleteContent |
||
| 740 | */ |
||
| 741 | public function testDeleteContentWithRollback() |
||
| 742 | { |
||
| 743 | $this->expectException(\Exception::class); |
||
| 744 | |||
| 745 | $repository = $this->getRepositoryMock(); |
||
| 746 | $permissionResolver = $this->getPermissionResolverMock(); |
||
| 747 | |||
| 748 | $permissionResolver->expects($this->once()) |
||
| 749 | ->method('canUser') |
||
| 750 | ->with('content', 'remove') |
||
| 751 | ->will($this->returnValue(true)); |
||
| 752 | |||
| 753 | $contentService = $this->getPartlyMockedContentService(['internalLoadContentInfo']); |
||
| 754 | /** @var \PHPUnit\Framework\MockObject\MockObject $locationHandler */ |
||
| 755 | $locationHandler = $this->getPersistenceMock()->locationHandler(); |
||
| 756 | |||
| 757 | $contentInfo = $this->createMock(APIContentInfo::class); |
||
| 758 | |||
| 759 | $contentService->expects($this->once()) |
||
| 760 | ->method('internalLoadContentInfo') |
||
| 761 | ->with(42) |
||
| 762 | ->will($this->returnValue($contentInfo)); |
||
| 763 | |||
| 764 | $contentInfo->expects($this->any()) |
||
| 765 | ->method('__get') |
||
| 766 | ->with('id') |
||
| 767 | ->will($this->returnValue(42)); |
||
| 768 | |||
| 769 | $repository->expects($this->once())->method('beginTransaction'); |
||
| 770 | |||
| 771 | $locationHandler->expects($this->once()) |
||
| 772 | ->method('loadLocationsByContent') |
||
| 773 | ->with(42) |
||
| 774 | ->will($this->throwException(new \Exception())); |
||
| 775 | |||
| 776 | $repository->expects($this->once())->method('rollback'); |
||
| 777 | |||
| 778 | /* @var \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo */ |
||
| 779 | $contentService->deleteContent($contentInfo); |
||
| 780 | } |
||
| 781 | |||
| 782 | /** |
||
| 783 | * Test for the deleteVersion() method. |
||
| 784 | * |
||
| 785 | * @covers \eZ\Publish\Core\Repository\ContentService::deleteVersion |
||
| 786 | */ |
||
| 787 | public function testDeleteVersionThrowsBadStateExceptionLastVersion() |
||
| 788 | { |
||
| 789 | $this->expectException(\eZ\Publish\API\Repository\Exceptions\BadStateException::class); |
||
| 790 | |||
| 791 | $repository = $this->getRepositoryMock(); |
||
| 792 | $permissionResolver = $this->getPermissionResolverMock(); |
||
| 793 | |||
| 794 | $permissionResolver |
||
| 795 | ->expects($this->once()) |
||
| 796 | ->method('canUser') |
||
| 797 | ->with('content', 'versionremove') |
||
| 798 | ->will($this->returnValue(true)); |
||
| 799 | $repository |
||
| 800 | ->expects($this->never()) |
||
| 801 | ->method('beginTransaction'); |
||
| 802 | |||
| 803 | $contentService = $this->getPartlyMockedContentService(); |
||
| 804 | /** @var \PHPUnit\Framework\MockObject\MockObject $contentHandler */ |
||
| 805 | $contentHandler = $this->getPersistenceMock()->contentHandler(); |
||
| 806 | $contentInfo = $this->createMock(APIContentInfo::class); |
||
| 807 | $versionInfo = $this->createMock(APIVersionInfo::class); |
||
| 808 | |||
| 809 | $contentInfo |
||
| 810 | ->expects($this->any()) |
||
| 811 | ->method('__get') |
||
| 812 | ->with('id') |
||
| 813 | ->will($this->returnValue(42)); |
||
| 814 | |||
| 815 | $versionInfo |
||
| 816 | ->expects($this->any()) |
||
| 817 | ->method('__get') |
||
| 818 | ->will( |
||
| 819 | $this->returnValueMap( |
||
| 820 | [ |
||
| 821 | ['versionNo', 123], |
||
| 822 | ['contentInfo', $contentInfo], |
||
| 823 | ] |
||
| 824 | ) |
||
| 825 | ); |
||
| 826 | $versionInfo |
||
| 827 | ->expects($this->once()) |
||
| 828 | ->method('isPublished') |
||
| 829 | ->willReturn(false); |
||
| 830 | |||
| 831 | $contentHandler |
||
| 832 | ->expects($this->once()) |
||
| 833 | ->method('listVersions') |
||
| 834 | ->with(42) |
||
| 835 | ->will($this->returnValue(['version'])); |
||
| 836 | |||
| 837 | /* @var \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo */ |
||
| 838 | $contentService->deleteVersion($versionInfo); |
||
| 839 | } |
||
| 840 | |||
| 841 | /** |
||
| 842 | * Test for the createContent() method. |
||
| 843 | * |
||
| 844 | * @covers \eZ\Publish\Core\Repository\ContentService::createContent |
||
| 845 | */ |
||
| 846 | View Code Duplication | public function testCreateContentThrowsInvalidArgumentExceptionMainLanguageCodeNotSet() |
|
| 847 | { |
||
| 848 | $this->expectException(\eZ\Publish\API\Repository\Exceptions\InvalidArgumentException::class); |
||
| 849 | $this->expectExceptionMessage('Argument \'$contentCreateStruct\' is invalid: \'mainLanguageCode\' property must be set'); |
||
| 850 | |||
| 851 | $mockedService = $this->getPartlyMockedContentService(); |
||
| 852 | $mockedService->createContent(new ContentCreateStruct(), []); |
||
| 853 | } |
||
| 854 | |||
| 855 | /** |
||
| 856 | * Test for the createContent() method. |
||
| 857 | * |
||
| 858 | * @covers \eZ\Publish\Core\Repository\ContentService::createContent |
||
| 859 | */ |
||
| 860 | View Code Duplication | public function testCreateContentThrowsInvalidArgumentExceptionContentTypeNotSet() |
|
| 861 | { |
||
| 862 | $this->expectException(\eZ\Publish\API\Repository\Exceptions\InvalidArgumentException::class); |
||
| 863 | $this->expectExceptionMessage('Argument \'$contentCreateStruct\' is invalid: \'contentType\' property must be set'); |
||
| 864 | |||
| 865 | $mockedService = $this->getPartlyMockedContentService(); |
||
| 866 | $mockedService->createContent( |
||
| 867 | new ContentCreateStruct(['mainLanguageCode' => 'eng-US']), |
||
| 868 | [] |
||
| 869 | ); |
||
| 870 | } |
||
| 871 | |||
| 872 | /** |
||
| 873 | * Test for the createContent() method. |
||
| 874 | * |
||
| 875 | * @covers \eZ\Publish\Core\Repository\ContentService::createContent |
||
| 876 | */ |
||
| 877 | public function testCreateContentThrowsUnauthorizedException() |
||
| 878 | { |
||
| 879 | $this->expectException(\eZ\Publish\API\Repository\Exceptions\UnauthorizedException::class); |
||
| 880 | |||
| 881 | $repositoryMock = $this->getRepositoryMock(); |
||
| 882 | |||
| 883 | $permissionResolver = $this->getPermissionResolverMock(); |
||
| 884 | $permissionResolver->expects($this->once()) |
||
| 885 | ->method('getCurrentUserReference') |
||
| 886 | ->will($this->returnValue(new UserReference(169))); |
||
| 887 | |||
| 888 | $mockedService = $this->getPartlyMockedContentService(); |
||
| 889 | $contentTypeServiceMock = $this->getContentTypeServiceMock(); |
||
| 890 | $contentType = new ContentType( |
||
| 891 | [ |
||
| 892 | 'id' => 123, |
||
| 893 | 'fieldDefinitions' => [], |
||
| 894 | ] |
||
| 895 | ); |
||
| 896 | $contentCreateStruct = new ContentCreateStruct( |
||
| 897 | [ |
||
| 898 | 'ownerId' => 169, |
||
| 899 | 'alwaysAvailable' => false, |
||
| 900 | 'mainLanguageCode' => 'eng-US', |
||
| 901 | 'contentType' => $contentType, |
||
| 902 | ] |
||
| 903 | ); |
||
| 904 | |||
| 905 | $contentTypeServiceMock->expects($this->once()) |
||
| 906 | ->method('loadContentType') |
||
| 907 | ->with($this->equalTo(123)) |
||
| 908 | ->will($this->returnValue($contentType)); |
||
| 909 | |||
| 910 | $repositoryMock->expects($this->once()) |
||
| 911 | ->method('getContentTypeService') |
||
| 912 | ->will($this->returnValue($contentTypeServiceMock)); |
||
| 913 | |||
| 914 | $permissionResolver->expects($this->once()) |
||
| 915 | ->method('canUser') |
||
| 916 | ->with( |
||
| 917 | $this->equalTo('content'), |
||
| 918 | $this->equalTo('create'), |
||
| 919 | $this->isInstanceOf(get_class($contentCreateStruct)), |
||
| 920 | $this->equalTo([]) |
||
| 921 | )->will($this->returnValue(false)); |
||
| 922 | |||
| 923 | $mockedService->createContent( |
||
| 924 | new ContentCreateStruct( |
||
| 925 | [ |
||
| 926 | 'mainLanguageCode' => 'eng-US', |
||
| 927 | 'contentType' => $contentType, |
||
| 928 | ] |
||
| 929 | ), |
||
| 930 | [] |
||
| 931 | ); |
||
| 932 | } |
||
| 933 | |||
| 934 | /** |
||
| 935 | * Test for the createContent() method. |
||
| 936 | * |
||
| 937 | * @covers \eZ\Publish\Core\Repository\ContentService::createContent |
||
| 938 | * @exceptionMessage Argument '$contentCreateStruct' is invalid: Another content with remoteId 'faraday' exists |
||
| 939 | */ |
||
| 940 | public function testCreateContentThrowsInvalidArgumentExceptionDuplicateRemoteId() |
||
| 941 | { |
||
| 942 | $this->expectException(\eZ\Publish\API\Repository\Exceptions\InvalidArgumentException::class); |
||
| 943 | |||
| 944 | $repositoryMock = $this->getRepositoryMock(); |
||
| 945 | $permissionResolverMock = $this->getPermissionResolverMock(); |
||
| 946 | $permissionResolverMock |
||
| 947 | ->expects($this->once()) |
||
| 948 | ->method('getCurrentUserReference') |
||
| 949 | ->willReturn($this->createMock(UserReference::class)); |
||
| 950 | |||
| 951 | $mockedService = $this->getPartlyMockedContentService(['loadContentByRemoteId']); |
||
| 952 | $contentTypeServiceMock = $this->getContentTypeServiceMock(); |
||
| 953 | $contentType = new ContentType( |
||
| 954 | [ |
||
| 955 | 'id' => 123, |
||
| 956 | 'fieldDefinitions' => [], |
||
| 957 | ] |
||
| 958 | ); |
||
| 959 | $contentCreateStruct = new ContentCreateStruct( |
||
| 960 | [ |
||
| 961 | 'ownerId' => 169, |
||
| 962 | 'alwaysAvailable' => false, |
||
| 963 | 'remoteId' => 'faraday', |
||
| 964 | 'mainLanguageCode' => 'eng-US', |
||
| 965 | 'contentType' => $contentType, |
||
| 966 | ] |
||
| 967 | ); |
||
| 968 | |||
| 969 | $contentTypeServiceMock->expects($this->once()) |
||
| 970 | ->method('loadContentType') |
||
| 971 | ->with($this->equalTo(123)) |
||
| 972 | ->will($this->returnValue($contentType)); |
||
| 973 | |||
| 974 | $repositoryMock->expects($this->once()) |
||
| 975 | ->method('getContentTypeService') |
||
| 976 | ->will($this->returnValue($contentTypeServiceMock)); |
||
| 977 | |||
| 978 | $permissionResolverMock->expects($this->once()) |
||
| 979 | ->method('canUser') |
||
| 980 | ->with( |
||
| 981 | $this->equalTo('content'), |
||
| 982 | $this->equalTo('create'), |
||
| 983 | $this->isInstanceOf(get_class($contentCreateStruct)), |
||
| 984 | $this->equalTo([]) |
||
| 985 | )->will($this->returnValue(true)); |
||
| 986 | |||
| 987 | $mockedService->expects($this->once()) |
||
| 988 | ->method('loadContentByRemoteId') |
||
| 989 | ->with($contentCreateStruct->remoteId) |
||
| 990 | ->will($this->returnValue('Hello...')); |
||
| 991 | |||
| 992 | $mockedService->createContent( |
||
| 993 | new ContentCreateStruct( |
||
| 994 | [ |
||
| 995 | 'remoteId' => 'faraday', |
||
| 996 | 'mainLanguageCode' => 'eng-US', |
||
| 997 | 'contentType' => $contentType, |
||
| 998 | ] |
||
| 999 | ), |
||
| 1000 | [] |
||
| 1001 | ); |
||
| 1002 | } |
||
| 1003 | |||
| 1004 | /** |
||
| 1005 | * @param string $mainLanguageCode |
||
| 1006 | * @param \eZ\Publish\API\Repository\Values\Content\Field[] $structFields |
||
| 1007 | * @param \eZ\Publish\API\Repository\Values\ContentType\FieldDefinition[] $fieldDefinitions |
||
| 1008 | * |
||
| 1009 | * @return array |
||
| 1010 | */ |
||
| 1011 | protected function mapStructFieldsForCreate($mainLanguageCode, $structFields, $fieldDefinitions) |
||
| 1012 | { |
||
| 1013 | $mappedFieldDefinitions = []; |
||
| 1014 | foreach ($fieldDefinitions as $fieldDefinition) { |
||
| 1015 | $mappedFieldDefinitions[$fieldDefinition->identifier] = $fieldDefinition; |
||
| 1016 | } |
||
| 1017 | |||
| 1018 | $mappedStructFields = []; |
||
| 1019 | foreach ($structFields as $structField) { |
||
| 1020 | if ($structField->languageCode === null) { |
||
| 1021 | $languageCode = $mainLanguageCode; |
||
| 1022 | } else { |
||
| 1023 | $languageCode = $structField->languageCode; |
||
| 1024 | } |
||
| 1025 | |||
| 1026 | $mappedStructFields[$structField->fieldDefIdentifier][$languageCode] = (string)$structField->value; |
||
| 1027 | } |
||
| 1028 | |||
| 1029 | return $mappedStructFields; |
||
| 1030 | } |
||
| 1031 | |||
| 1032 | /** |
||
| 1033 | * Returns full, possibly redundant array of field values, indexed by field definition |
||
| 1034 | * identifier and language code. |
||
| 1035 | * |
||
| 1036 | * @throws \RuntimeException Method is intended to be used only with consistent fixtures |
||
| 1037 | * |
||
| 1038 | * @param string $mainLanguageCode |
||
| 1039 | * @param \eZ\Publish\API\Repository\Values\Content\Field[] $structFields |
||
| 1040 | * @param \eZ\Publish\API\Repository\Values\ContentType\FieldDefinition[] $fieldDefinitions |
||
| 1041 | * @param array $languageCodes |
||
| 1042 | * |
||
| 1043 | * @return array |
||
| 1044 | */ |
||
| 1045 | protected function determineValuesForCreate( |
||
| 1046 | $mainLanguageCode, |
||
| 1047 | array $structFields, |
||
| 1048 | array $fieldDefinitions, |
||
| 1049 | array $languageCodes |
||
| 1050 | ) { |
||
| 1051 | $mappedStructFields = $this->mapStructFieldsForCreate( |
||
| 1052 | $mainLanguageCode, |
||
| 1053 | $structFields, |
||
| 1054 | $fieldDefinitions |
||
| 1055 | ); |
||
| 1056 | |||
| 1057 | $values = []; |
||
| 1058 | |||
| 1059 | foreach ($fieldDefinitions as $fieldDefinition) { |
||
| 1060 | $identifier = $fieldDefinition->identifier; |
||
| 1061 | foreach ($languageCodes as $languageCode) { |
||
| 1062 | View Code Duplication | if (!$fieldDefinition->isTranslatable) { |
|
| 1063 | if (isset($mappedStructFields[$identifier][$mainLanguageCode])) { |
||
| 1064 | $values[$identifier][$languageCode] = $mappedStructFields[$identifier][$mainLanguageCode]; |
||
| 1065 | } else { |
||
| 1066 | $values[$identifier][$languageCode] = (string)$fieldDefinition->defaultValue; |
||
| 1067 | } |
||
| 1068 | continue; |
||
| 1069 | } |
||
| 1070 | |||
| 1071 | View Code Duplication | if (isset($mappedStructFields[$identifier][$languageCode])) { |
|
| 1072 | $values[$identifier][$languageCode] = $mappedStructFields[$identifier][$languageCode]; |
||
| 1073 | continue; |
||
| 1074 | } |
||
| 1075 | |||
| 1076 | $values[$identifier][$languageCode] = (string)$fieldDefinition->defaultValue; |
||
| 1077 | } |
||
| 1078 | } |
||
| 1079 | |||
| 1080 | return $this->stubValues($values); |
||
| 1081 | } |
||
| 1082 | |||
| 1083 | /** |
||
| 1084 | * @param string $mainLanguageCode |
||
| 1085 | * @param \eZ\Publish\API\Repository\Values\Content\Field[] $structFields |
||
| 1086 | * |
||
| 1087 | * @return string[] |
||
| 1088 | */ |
||
| 1089 | protected function determineLanguageCodesForCreate($mainLanguageCode, array $structFields) |
||
| 1090 | { |
||
| 1091 | $languageCodes = []; |
||
| 1092 | |||
| 1093 | foreach ($structFields as $field) { |
||
| 1094 | if ($field->languageCode === null || isset($languageCodes[$field->languageCode])) { |
||
| 1095 | continue; |
||
| 1096 | } |
||
| 1097 | |||
| 1098 | $languageCodes[$field->languageCode] = true; |
||
| 1099 | } |
||
| 1100 | |||
| 1101 | $languageCodes[$mainLanguageCode] = true; |
||
| 1102 | |||
| 1103 | return array_keys($languageCodes); |
||
| 1104 | } |
||
| 1105 | |||
| 1106 | /** |
||
| 1107 | * Asserts that calling createContent() with given API field set causes calling |
||
| 1108 | * Handler::createContent() with given SPI field set. |
||
| 1109 | * |
||
| 1110 | * @param string $mainLanguageCode |
||
| 1111 | * @param \eZ\Publish\API\Repository\Values\Content\Field[] $structFields |
||
| 1112 | * @param \eZ\Publish\SPI\Persistence\Content\Field[] $spiFields |
||
| 1113 | * @param \eZ\Publish\API\Repository\Values\ContentType\FieldDefinition[] $fieldDefinitions |
||
| 1114 | * @param \eZ\Publish\API\Repository\Values\Content\LocationCreateStruct[] $locationCreateStructs |
||
| 1115 | * @param \eZ\Publish\SPI\Persistence\Content\ObjectState\Group[] $objectStateGroups |
||
| 1116 | * @param bool $execute |
||
| 1117 | * |
||
| 1118 | * @return mixed |
||
| 1119 | */ |
||
| 1120 | protected function assertForTestCreateContentNonRedundantFieldSet( |
||
| 1121 | $mainLanguageCode, |
||
| 1122 | array $structFields, |
||
| 1123 | array $spiFields, |
||
| 1124 | array $fieldDefinitions, |
||
| 1125 | array $locationCreateStructs = [], |
||
| 1126 | $withObjectStates = false, |
||
| 1127 | $execute = true |
||
| 1128 | ) { |
||
| 1129 | $repositoryMock = $this->getRepositoryMock(); |
||
| 1130 | $mockedService = $this->getPartlyMockedContentService(); |
||
| 1131 | /** @var \PHPUnit\Framework\MockObject\MockObject $contentHandlerMock */ |
||
| 1132 | $contentHandlerMock = $this->getPersistenceMock()->contentHandler(); |
||
| 1133 | /** @var \PHPUnit\Framework\MockObject\MockObject $languageHandlerMock */ |
||
| 1134 | $languageHandlerMock = $this->getPersistenceMock()->contentLanguageHandler(); |
||
| 1135 | /** @var \PHPUnit\Framework\MockObject\MockObject $objectStateHandlerMock */ |
||
| 1136 | $objectStateHandlerMock = $this->getPersistenceMock()->objectStateHandler(); |
||
| 1137 | $contentTypeServiceMock = $this->getContentTypeServiceMock(); |
||
| 1138 | $fieldTypeServiceMock = $this->getFieldTypeServiceMock(); |
||
| 1139 | $domainMapperMock = $this->getDomainMapperMock(); |
||
| 1140 | $relationProcessorMock = $this->getRelationProcessorMock(); |
||
| 1141 | $nameSchemaServiceMock = $this->getNameSchemaServiceMock(); |
||
| 1142 | $permissionResolverMock = $this->getPermissionResolverMock(); |
||
| 1143 | $fieldTypeMock = $this->createMock(SPIFieldType::class); |
||
| 1144 | $languageCodes = $this->determineLanguageCodesForCreate($mainLanguageCode, $structFields); |
||
| 1145 | $contentType = new ContentType( |
||
| 1146 | [ |
||
| 1147 | 'id' => 123, |
||
| 1148 | 'fieldDefinitions' => $fieldDefinitions, |
||
| 1149 | 'nameSchema' => '<nameSchema>', |
||
| 1150 | ] |
||
| 1151 | ); |
||
| 1152 | $contentCreateStruct = new ContentCreateStruct( |
||
| 1153 | [ |
||
| 1154 | 'fields' => $structFields, |
||
| 1155 | 'mainLanguageCode' => $mainLanguageCode, |
||
| 1156 | 'contentType' => $contentType, |
||
| 1157 | 'alwaysAvailable' => false, |
||
| 1158 | 'ownerId' => 169, |
||
| 1159 | 'sectionId' => 1, |
||
| 1160 | ] |
||
| 1161 | ); |
||
| 1162 | |||
| 1163 | $languageHandlerMock->expects($this->any()) |
||
| 1164 | ->method('loadByLanguageCode') |
||
| 1165 | ->with($this->isType('string')) |
||
| 1166 | ->will( |
||
| 1167 | $this->returnCallback( |
||
| 1168 | function () { |
||
| 1169 | return new Language(['id' => 4242]); |
||
| 1170 | } |
||
| 1171 | ) |
||
| 1172 | ); |
||
| 1173 | |||
| 1174 | $repositoryMock->expects($this->once())->method('beginTransaction'); |
||
| 1175 | |||
| 1176 | $contentTypeServiceMock->expects($this->once()) |
||
| 1177 | ->method('loadContentType') |
||
| 1178 | ->with($this->equalTo($contentType->id)) |
||
| 1179 | ->will($this->returnValue($contentType)); |
||
| 1180 | |||
| 1181 | $repositoryMock->expects($this->once()) |
||
| 1182 | ->method('getContentTypeService') |
||
| 1183 | ->will($this->returnValue($contentTypeServiceMock)); |
||
| 1184 | |||
| 1185 | $that = $this; |
||
| 1186 | $permissionResolverMock->expects($this->once()) |
||
| 1187 | ->method('canUser') |
||
| 1188 | ->with( |
||
| 1189 | $this->equalTo('content'), |
||
| 1190 | $this->equalTo('create'), |
||
| 1191 | $this->isInstanceOf(APIContentCreateStruct::class), |
||
| 1192 | $this->equalTo($locationCreateStructs) |
||
| 1193 | )->will( |
||
| 1194 | $this->returnCallback( |
||
| 1195 | function () use ($that, $contentCreateStruct) { |
||
| 1196 | $that->assertEquals($contentCreateStruct, func_get_arg(2)); |
||
| 1197 | |||
| 1198 | return true; |
||
| 1199 | } |
||
| 1200 | ) |
||
| 1201 | ); |
||
| 1202 | |||
| 1203 | $domainMapperMock->expects($this->once()) |
||
| 1204 | ->method('getUniqueHash') |
||
| 1205 | ->with($this->isInstanceOf(APIContentCreateStruct::class)) |
||
| 1206 | ->will( |
||
| 1207 | $this->returnCallback( |
||
| 1208 | function ($object) use ($that, $contentCreateStruct) { |
||
| 1209 | $that->assertEquals($contentCreateStruct, $object); |
||
| 1210 | |||
| 1211 | return 'hash'; |
||
| 1212 | } |
||
| 1213 | ) |
||
| 1214 | ); |
||
| 1215 | |||
| 1216 | $fieldTypeMock->expects($this->any()) |
||
| 1217 | ->method('acceptValue') |
||
| 1218 | ->will( |
||
| 1219 | $this->returnCallback( |
||
| 1220 | function ($valueString) { |
||
| 1221 | return new ValueStub($valueString); |
||
| 1222 | } |
||
| 1223 | ) |
||
| 1224 | ); |
||
| 1225 | |||
| 1226 | $fieldTypeMock->expects($this->any()) |
||
| 1227 | ->method('toPersistenceValue') |
||
| 1228 | ->will( |
||
| 1229 | $this->returnCallback( |
||
| 1230 | function (ValueStub $value) { |
||
| 1231 | return (string)$value; |
||
| 1232 | } |
||
| 1233 | ) |
||
| 1234 | ); |
||
| 1235 | |||
| 1236 | $emptyValue = self::EMPTY_FIELD_VALUE; |
||
| 1237 | $fieldTypeMock->expects($this->any()) |
||
| 1238 | ->method('isEmptyValue') |
||
| 1239 | ->will( |
||
| 1240 | $this->returnCallback( |
||
| 1241 | function (ValueStub $value) use ($emptyValue) { |
||
| 1242 | return $emptyValue === (string)$value; |
||
| 1243 | } |
||
| 1244 | ) |
||
| 1245 | ); |
||
| 1246 | |||
| 1247 | $fieldTypeMock->expects($this->any()) |
||
| 1248 | ->method('validate') |
||
| 1249 | ->will($this->returnValue([])); |
||
| 1250 | |||
| 1251 | $this->getFieldTypeRegistryMock()->expects($this->any()) |
||
| 1252 | ->method('getFieldType') |
||
| 1253 | ->will($this->returnValue($fieldTypeMock)); |
||
| 1254 | |||
| 1255 | $relationProcessorMock |
||
| 1256 | ->expects($this->exactly(count($fieldDefinitions) * count($languageCodes))) |
||
| 1257 | ->method('appendFieldRelations') |
||
| 1258 | ->with( |
||
| 1259 | $this->isType('array'), |
||
| 1260 | $this->isType('array'), |
||
| 1261 | $this->isInstanceOf(SPIFieldType::class), |
||
| 1262 | $this->isInstanceOf(Value::class), |
||
| 1263 | $this->anything() |
||
| 1264 | ); |
||
| 1265 | |||
| 1266 | $values = $this->determineValuesForCreate( |
||
| 1267 | $mainLanguageCode, |
||
| 1268 | $structFields, |
||
| 1269 | $fieldDefinitions, |
||
| 1270 | $languageCodes |
||
| 1271 | ); |
||
| 1272 | $nameSchemaServiceMock->expects($this->once()) |
||
| 1273 | ->method('resolve') |
||
| 1274 | ->with( |
||
| 1275 | $this->equalTo($contentType->nameSchema), |
||
| 1276 | $this->equalTo($contentType), |
||
| 1277 | $this->equalTo($values), |
||
| 1278 | $this->equalTo($languageCodes) |
||
| 1279 | )->will($this->returnValue([])); |
||
| 1280 | |||
| 1281 | $relationProcessorMock->expects($this->any()) |
||
| 1282 | ->method('processFieldRelations') |
||
| 1283 | ->with( |
||
| 1284 | $this->isType('array'), |
||
| 1285 | $this->equalTo(42), |
||
| 1286 | $this->isType('int'), |
||
| 1287 | $this->equalTo($contentType), |
||
| 1288 | $this->equalTo([]) |
||
| 1289 | ); |
||
| 1290 | |||
| 1291 | if (!$withObjectStates) { |
||
| 1292 | $objectStateHandlerMock->expects($this->once()) |
||
| 1293 | ->method('loadAllGroups') |
||
| 1294 | ->will($this->returnValue([])); |
||
| 1295 | } |
||
| 1296 | |||
| 1297 | if ($execute) { |
||
| 1298 | $spiContentCreateStruct = new SPIContentCreateStruct( |
||
| 1299 | [ |
||
| 1300 | 'name' => [], |
||
| 1301 | 'typeId' => 123, |
||
| 1302 | 'sectionId' => 1, |
||
| 1303 | 'ownerId' => 169, |
||
| 1304 | 'remoteId' => 'hash', |
||
| 1305 | 'fields' => $spiFields, |
||
| 1306 | 'modified' => time(), |
||
| 1307 | 'initialLanguageId' => 4242, |
||
| 1308 | ] |
||
| 1309 | ); |
||
| 1310 | $spiContentCreateStruct2 = clone $spiContentCreateStruct; |
||
| 1311 | ++$spiContentCreateStruct2->modified; |
||
| 1312 | |||
| 1313 | $spiContent = new SPIContent( |
||
| 1314 | [ |
||
| 1315 | 'versionInfo' => new SPIContent\VersionInfo( |
||
| 1316 | [ |
||
| 1317 | 'contentInfo' => new SPIContent\ContentInfo(['id' => 42]), |
||
| 1318 | 'versionNo' => 7, |
||
| 1319 | ] |
||
| 1320 | ), |
||
| 1321 | ] |
||
| 1322 | ); |
||
| 1323 | |||
| 1324 | $contentHandlerMock->expects($this->once()) |
||
| 1325 | ->method('create') |
||
| 1326 | ->with($this->logicalOr($spiContentCreateStruct, $spiContentCreateStruct2)) |
||
| 1327 | ->will($this->returnValue($spiContent)); |
||
| 1328 | |||
| 1329 | $repositoryMock->expects($this->once())->method('commit'); |
||
| 1330 | $domainMapperMock->expects($this->once()) |
||
| 1331 | ->method('buildContentDomainObject') |
||
| 1332 | ->with( |
||
| 1333 | $this->isInstanceOf(SPIContent::class), |
||
| 1334 | $this->equalTo($contentType) |
||
| 1335 | ); |
||
| 1336 | |||
| 1337 | $mockedService->createContent($contentCreateStruct, []); |
||
| 1338 | } |
||
| 1339 | |||
| 1340 | return $contentCreateStruct; |
||
| 1341 | } |
||
| 1342 | |||
| 1343 | public function providerForTestCreateContentNonRedundantFieldSet1() |
||
| 1344 | { |
||
| 1345 | $spiFields = [ |
||
| 1346 | new SPIField( |
||
| 1347 | [ |
||
| 1348 | 'fieldDefinitionId' => 'fieldDefinitionId', |
||
| 1349 | 'type' => 'fieldTypeIdentifier', |
||
| 1350 | 'value' => 'newValue', |
||
| 1351 | 'languageCode' => 'eng-US', |
||
| 1352 | ] |
||
| 1353 | ), |
||
| 1354 | ]; |
||
| 1355 | |||
| 1356 | return [ |
||
| 1357 | // 0. Without language set |
||
| 1358 | [ |
||
| 1359 | 'eng-US', |
||
| 1360 | [ |
||
| 1361 | new Field( |
||
| 1362 | [ |
||
| 1363 | 'fieldDefIdentifier' => 'identifier', |
||
| 1364 | 'value' => 'newValue', |
||
| 1365 | 'languageCode' => 'eng-US', |
||
| 1366 | ] |
||
| 1367 | ), |
||
| 1368 | ], |
||
| 1369 | $spiFields, |
||
| 1370 | ], |
||
| 1371 | // 1. Without language set |
||
| 1372 | [ |
||
| 1373 | 'eng-US', |
||
| 1374 | [ |
||
| 1375 | new Field( |
||
| 1376 | [ |
||
| 1377 | 'fieldDefIdentifier' => 'identifier', |
||
| 1378 | 'value' => 'newValue', |
||
| 1379 | 'languageCode' => null, |
||
| 1380 | ] |
||
| 1381 | ), |
||
| 1382 | ], |
||
| 1383 | $spiFields, |
||
| 1384 | ], |
||
| 1385 | ]; |
||
| 1386 | } |
||
| 1387 | |||
| 1388 | /** |
||
| 1389 | * Test for the createContent() method. |
||
| 1390 | * |
||
| 1391 | * Testing the simplest use case. |
||
| 1392 | * |
||
| 1393 | * @covers \eZ\Publish\Core\Repository\ContentService::getLanguageCodesForCreate |
||
| 1394 | * @covers \eZ\Publish\Core\Repository\ContentService::mapFieldsForCreate |
||
| 1395 | * @covers \eZ\Publish\Core\Repository\ContentService::cloneField |
||
| 1396 | * @covers \eZ\Publish\Core\Repository\ContentService::getDefaultObjectStates |
||
| 1397 | * @covers \eZ\Publish\Core\Repository\ContentService::createContent |
||
| 1398 | * @dataProvider providerForTestCreateContentNonRedundantFieldSet1 |
||
| 1399 | */ |
||
| 1400 | public function testCreateContentNonRedundantFieldSet1($mainLanguageCode, $structFields, $spiFields) |
||
| 1401 | { |
||
| 1402 | $fieldDefinitions = [ |
||
| 1403 | new FieldDefinition( |
||
| 1404 | [ |
||
| 1405 | 'id' => 'fieldDefinitionId', |
||
| 1406 | 'fieldTypeIdentifier' => 'fieldTypeIdentifier', |
||
| 1407 | 'isTranslatable' => false, |
||
| 1408 | 'identifier' => 'identifier', |
||
| 1409 | 'isRequired' => false, |
||
| 1410 | 'defaultValue' => 'defaultValue', |
||
| 1411 | ] |
||
| 1412 | ), |
||
| 1413 | ]; |
||
| 1414 | |||
| 1415 | $this->assertForTestCreateContentNonRedundantFieldSet( |
||
| 1416 | $mainLanguageCode, |
||
| 1417 | $structFields, |
||
| 1418 | $spiFields, |
||
| 1419 | $fieldDefinitions |
||
| 1420 | ); |
||
| 1421 | } |
||
| 1422 | |||
| 1423 | public function providerForTestCreateContentNonRedundantFieldSet2() |
||
| 1424 | { |
||
| 1425 | $spiFields = [ |
||
| 1426 | new SPIField( |
||
| 1427 | [ |
||
| 1428 | 'fieldDefinitionId' => 'fieldDefinitionId1', |
||
| 1429 | 'type' => 'fieldTypeIdentifier', |
||
| 1430 | 'value' => 'newValue1', |
||
| 1431 | 'languageCode' => 'eng-US', |
||
| 1432 | ] |
||
| 1433 | ), |
||
| 1434 | new SPIField( |
||
| 1435 | [ |
||
| 1436 | 'fieldDefinitionId' => 'fieldDefinitionId2', |
||
| 1437 | 'type' => 'fieldTypeIdentifier', |
||
| 1438 | 'value' => 'newValue2', |
||
| 1439 | 'languageCode' => 'ger-DE', |
||
| 1440 | ] |
||
| 1441 | ), |
||
| 1442 | ]; |
||
| 1443 | |||
| 1444 | return [ |
||
| 1445 | // 0. With language set |
||
| 1446 | [ |
||
| 1447 | 'eng-US', |
||
| 1448 | [ |
||
| 1449 | new Field( |
||
| 1450 | [ |
||
| 1451 | 'fieldDefIdentifier' => 'identifier1', |
||
| 1452 | 'value' => 'newValue1', |
||
| 1453 | 'languageCode' => 'eng-US', |
||
| 1454 | ] |
||
| 1455 | ), |
||
| 1456 | new Field( |
||
| 1457 | [ |
||
| 1458 | 'fieldDefIdentifier' => 'identifier2', |
||
| 1459 | 'value' => 'newValue2', |
||
| 1460 | 'languageCode' => 'ger-DE', |
||
| 1461 | ] |
||
| 1462 | ), |
||
| 1463 | ], |
||
| 1464 | $spiFields, |
||
| 1465 | ], |
||
| 1466 | // 1. Without language set |
||
| 1467 | [ |
||
| 1468 | 'eng-US', |
||
| 1469 | [ |
||
| 1470 | new Field( |
||
| 1471 | [ |
||
| 1472 | 'fieldDefIdentifier' => 'identifier1', |
||
| 1473 | 'value' => 'newValue1', |
||
| 1474 | 'languageCode' => null, |
||
| 1475 | ] |
||
| 1476 | ), |
||
| 1477 | new Field( |
||
| 1478 | [ |
||
| 1479 | 'fieldDefIdentifier' => 'identifier2', |
||
| 1480 | 'value' => 'newValue2', |
||
| 1481 | 'languageCode' => 'ger-DE', |
||
| 1482 | ] |
||
| 1483 | ), |
||
| 1484 | ], |
||
| 1485 | $spiFields, |
||
| 1486 | ], |
||
| 1487 | ]; |
||
| 1488 | } |
||
| 1489 | |||
| 1490 | /** |
||
| 1491 | * Test for the createContent() method. |
||
| 1492 | * |
||
| 1493 | * Testing multiple languages with multiple translatable fields with empty default value. |
||
| 1494 | * |
||
| 1495 | * @covers \eZ\Publish\Core\Repository\ContentService::getLanguageCodesForCreate |
||
| 1496 | * @covers \eZ\Publish\Core\Repository\ContentService::mapFieldsForCreate |
||
| 1497 | * @covers \eZ\Publish\Core\Repository\ContentService::cloneField |
||
| 1498 | * @covers \eZ\Publish\Core\Repository\ContentService::getDefaultObjectStates |
||
| 1499 | * @covers \eZ\Publish\Core\Repository\ContentService::createContent |
||
| 1500 | * @dataProvider providerForTestCreateContentNonRedundantFieldSet2 |
||
| 1501 | */ |
||
| 1502 | public function testCreateContentNonRedundantFieldSet2($mainLanguageCode, $structFields, $spiFields) |
||
| 1503 | { |
||
| 1504 | $fieldDefinitions = [ |
||
| 1505 | new FieldDefinition( |
||
| 1506 | [ |
||
| 1507 | 'id' => 'fieldDefinitionId1', |
||
| 1508 | 'fieldTypeIdentifier' => 'fieldTypeIdentifier', |
||
| 1509 | 'isTranslatable' => true, |
||
| 1510 | 'identifier' => 'identifier1', |
||
| 1511 | 'isRequired' => false, |
||
| 1512 | 'defaultValue' => self::EMPTY_FIELD_VALUE, |
||
| 1513 | ] |
||
| 1514 | ), |
||
| 1515 | new FieldDefinition( |
||
| 1516 | [ |
||
| 1517 | 'id' => 'fieldDefinitionId2', |
||
| 1518 | 'fieldTypeIdentifier' => 'fieldTypeIdentifier', |
||
| 1519 | 'isTranslatable' => true, |
||
| 1520 | 'identifier' => 'identifier2', |
||
| 1521 | 'isRequired' => false, |
||
| 1522 | 'defaultValue' => self::EMPTY_FIELD_VALUE, |
||
| 1523 | ] |
||
| 1524 | ), |
||
| 1525 | ]; |
||
| 1526 | |||
| 1527 | $this->assertForTestCreateContentNonRedundantFieldSet( |
||
| 1528 | $mainLanguageCode, |
||
| 1529 | $structFields, |
||
| 1530 | $spiFields, |
||
| 1531 | $fieldDefinitions |
||
| 1532 | ); |
||
| 1533 | } |
||
| 1534 | |||
| 1535 | public function providerForTestCreateContentNonRedundantFieldSetComplex() |
||
| 1536 | { |
||
| 1537 | $spiFields0 = [ |
||
| 1538 | new SPIField( |
||
| 1539 | [ |
||
| 1540 | 'fieldDefinitionId' => 'fieldDefinitionId2', |
||
| 1541 | 'type' => 'fieldTypeIdentifier', |
||
| 1542 | 'value' => 'defaultValue2', |
||
| 1543 | 'languageCode' => 'eng-US', |
||
| 1544 | ] |
||
| 1545 | ), |
||
| 1546 | new SPIField( |
||
| 1547 | [ |
||
| 1548 | 'fieldDefinitionId' => 'fieldDefinitionId4', |
||
| 1549 | 'type' => 'fieldTypeIdentifier', |
||
| 1550 | 'value' => 'defaultValue4', |
||
| 1551 | 'languageCode' => 'eng-US', |
||
| 1552 | ] |
||
| 1553 | ), |
||
| 1554 | ]; |
||
| 1555 | $spiFields1 = [ |
||
| 1556 | new SPIField( |
||
| 1557 | [ |
||
| 1558 | 'fieldDefinitionId' => 'fieldDefinitionId1', |
||
| 1559 | 'type' => 'fieldTypeIdentifier', |
||
| 1560 | 'value' => 'newValue1', |
||
| 1561 | 'languageCode' => 'ger-DE', |
||
| 1562 | ] |
||
| 1563 | ), |
||
| 1564 | new SPIField( |
||
| 1565 | [ |
||
| 1566 | 'fieldDefinitionId' => 'fieldDefinitionId2', |
||
| 1567 | 'type' => 'fieldTypeIdentifier', |
||
| 1568 | 'value' => 'defaultValue2', |
||
| 1569 | 'languageCode' => 'ger-DE', |
||
| 1570 | ] |
||
| 1571 | ), |
||
| 1572 | new SPIField( |
||
| 1573 | [ |
||
| 1574 | 'fieldDefinitionId' => 'fieldDefinitionId2', |
||
| 1575 | 'type' => 'fieldTypeIdentifier', |
||
| 1576 | 'value' => 'newValue2', |
||
| 1577 | 'languageCode' => 'eng-US', |
||
| 1578 | ] |
||
| 1579 | ), |
||
| 1580 | new SPIField( |
||
| 1581 | [ |
||
| 1582 | 'fieldDefinitionId' => 'fieldDefinitionId4', |
||
| 1583 | 'type' => 'fieldTypeIdentifier', |
||
| 1584 | 'value' => 'newValue4', |
||
| 1585 | 'languageCode' => 'eng-US', |
||
| 1586 | ] |
||
| 1587 | ), |
||
| 1588 | ]; |
||
| 1589 | |||
| 1590 | return [ |
||
| 1591 | // 0. Creating by default values only |
||
| 1592 | [ |
||
| 1593 | 'eng-US', |
||
| 1594 | [], |
||
| 1595 | $spiFields0, |
||
| 1596 | ], |
||
| 1597 | // 1. Multiple languages with language set |
||
| 1598 | [ |
||
| 1599 | 'eng-US', |
||
| 1600 | [ |
||
| 1601 | new Field( |
||
| 1602 | [ |
||
| 1603 | 'fieldDefIdentifier' => 'identifier1', |
||
| 1604 | 'value' => 'newValue1', |
||
| 1605 | 'languageCode' => 'ger-DE', |
||
| 1606 | ] |
||
| 1607 | ), |
||
| 1608 | new Field( |
||
| 1609 | [ |
||
| 1610 | 'fieldDefIdentifier' => 'identifier2', |
||
| 1611 | 'value' => 'newValue2', |
||
| 1612 | 'languageCode' => 'eng-US', |
||
| 1613 | ] |
||
| 1614 | ), |
||
| 1615 | new Field( |
||
| 1616 | [ |
||
| 1617 | 'fieldDefIdentifier' => 'identifier4', |
||
| 1618 | 'value' => 'newValue4', |
||
| 1619 | 'languageCode' => 'eng-US', |
||
| 1620 | ] |
||
| 1621 | ), |
||
| 1622 | ], |
||
| 1623 | $spiFields1, |
||
| 1624 | ], |
||
| 1625 | // 2. Multiple languages without language set |
||
| 1626 | [ |
||
| 1627 | 'eng-US', |
||
| 1628 | [ |
||
| 1629 | new Field( |
||
| 1630 | [ |
||
| 1631 | 'fieldDefIdentifier' => 'identifier1', |
||
| 1632 | 'value' => 'newValue1', |
||
| 1633 | 'languageCode' => 'ger-DE', |
||
| 1634 | ] |
||
| 1635 | ), |
||
| 1636 | new Field( |
||
| 1637 | [ |
||
| 1638 | 'fieldDefIdentifier' => 'identifier2', |
||
| 1639 | 'value' => 'newValue2', |
||
| 1640 | 'languageCode' => null, |
||
| 1641 | ] |
||
| 1642 | ), |
||
| 1643 | new Field( |
||
| 1644 | [ |
||
| 1645 | 'fieldDefIdentifier' => 'identifier4', |
||
| 1646 | 'value' => 'newValue4', |
||
| 1647 | 'languageCode' => null, |
||
| 1648 | ] |
||
| 1649 | ), |
||
| 1650 | ], |
||
| 1651 | $spiFields1, |
||
| 1652 | ], |
||
| 1653 | ]; |
||
| 1654 | } |
||
| 1655 | |||
| 1656 | protected function fixturesForTestCreateContentNonRedundantFieldSetComplex() |
||
| 1657 | { |
||
| 1658 | return [ |
||
| 1659 | new FieldDefinition( |
||
| 1660 | [ |
||
| 1661 | 'id' => 'fieldDefinitionId1', |
||
| 1662 | 'fieldTypeIdentifier' => 'fieldTypeIdentifier', |
||
| 1663 | 'isTranslatable' => true, |
||
| 1664 | 'identifier' => 'identifier1', |
||
| 1665 | 'isRequired' => false, |
||
| 1666 | 'defaultValue' => self::EMPTY_FIELD_VALUE, |
||
| 1667 | ] |
||
| 1668 | ), |
||
| 1669 | new FieldDefinition( |
||
| 1670 | [ |
||
| 1671 | 'id' => 'fieldDefinitionId2', |
||
| 1672 | 'fieldTypeIdentifier' => 'fieldTypeIdentifier', |
||
| 1673 | 'isTranslatable' => true, |
||
| 1674 | 'identifier' => 'identifier2', |
||
| 1675 | 'isRequired' => false, |
||
| 1676 | 'defaultValue' => 'defaultValue2', |
||
| 1677 | ] |
||
| 1678 | ), |
||
| 1679 | new FieldDefinition( |
||
| 1680 | [ |
||
| 1681 | 'id' => 'fieldDefinitionId3', |
||
| 1682 | 'fieldTypeIdentifier' => 'fieldTypeIdentifier', |
||
| 1683 | 'isTranslatable' => false, |
||
| 1684 | 'identifier' => 'identifier3', |
||
| 1685 | 'isRequired' => false, |
||
| 1686 | 'defaultValue' => self::EMPTY_FIELD_VALUE, |
||
| 1687 | ] |
||
| 1688 | ), |
||
| 1689 | new FieldDefinition( |
||
| 1690 | [ |
||
| 1691 | 'id' => 'fieldDefinitionId4', |
||
| 1692 | 'fieldTypeIdentifier' => 'fieldTypeIdentifier', |
||
| 1693 | 'isTranslatable' => false, |
||
| 1694 | 'identifier' => 'identifier4', |
||
| 1695 | 'isRequired' => false, |
||
| 1696 | 'defaultValue' => 'defaultValue4', |
||
| 1697 | ] |
||
| 1698 | ), |
||
| 1699 | ]; |
||
| 1700 | } |
||
| 1701 | |||
| 1702 | /** |
||
| 1703 | * Test for the createContent() method. |
||
| 1704 | * |
||
| 1705 | * Testing multiple languages with multiple translatable fields with empty default value. |
||
| 1706 | * |
||
| 1707 | * @covers \eZ\Publish\Core\Repository\ContentService::getLanguageCodesForCreate |
||
| 1708 | * @covers \eZ\Publish\Core\Repository\ContentService::mapFieldsForCreate |
||
| 1709 | * @covers \eZ\Publish\Core\Repository\ContentService::cloneField |
||
| 1710 | * @covers \eZ\Publish\Core\Repository\ContentService::getDefaultObjectStates |
||
| 1711 | * @covers \eZ\Publish\Core\Repository\ContentService::createContent |
||
| 1712 | * @dataProvider providerForTestCreateContentNonRedundantFieldSetComplex |
||
| 1713 | */ |
||
| 1714 | public function testCreateContentNonRedundantFieldSetComplex($mainLanguageCode, $structFields, $spiFields) |
||
| 1715 | { |
||
| 1716 | $fieldDefinitions = $this->fixturesForTestCreateContentNonRedundantFieldSetComplex(); |
||
| 1717 | |||
| 1718 | $this->assertForTestCreateContentNonRedundantFieldSet( |
||
| 1719 | $mainLanguageCode, |
||
| 1720 | $structFields, |
||
| 1721 | $spiFields, |
||
| 1722 | $fieldDefinitions |
||
| 1723 | ); |
||
| 1724 | } |
||
| 1725 | |||
| 1726 | View Code Duplication | public function providerForTestCreateContentWithInvalidLanguage() |
|
| 1727 | { |
||
| 1728 | return [ |
||
| 1729 | [ |
||
| 1730 | 'eng-GB', |
||
| 1731 | [ |
||
| 1732 | new Field( |
||
| 1733 | [ |
||
| 1734 | 'fieldDefIdentifier' => 'identifier', |
||
| 1735 | 'value' => 'newValue', |
||
| 1736 | 'languageCode' => 'Klingon', |
||
| 1737 | ] |
||
| 1738 | ), |
||
| 1739 | ], |
||
| 1740 | ], |
||
| 1741 | [ |
||
| 1742 | 'Klingon', |
||
| 1743 | [ |
||
| 1744 | new Field( |
||
| 1745 | [ |
||
| 1746 | 'fieldDefIdentifier' => 'identifier', |
||
| 1747 | 'value' => 'newValue', |
||
| 1748 | 'languageCode' => 'eng-GB', |
||
| 1749 | ] |
||
| 1750 | ), |
||
| 1751 | ], |
||
| 1752 | ], |
||
| 1753 | ]; |
||
| 1754 | } |
||
| 1755 | |||
| 1756 | /** |
||
| 1757 | * Test for the updateContent() method. |
||
| 1758 | * |
||
| 1759 | * @covers \eZ\Publish\Core\Repository\ContentService::getLanguageCodesForCreate |
||
| 1760 | * @covers \eZ\Publish\Core\Repository\ContentService::createContent |
||
| 1761 | * @dataProvider providerForTestCreateContentWithInvalidLanguage |
||
| 1762 | */ |
||
| 1763 | public function testCreateContentWithInvalidLanguage($mainLanguageCode, $structFields) |
||
| 1764 | { |
||
| 1765 | $this->expectException(\eZ\Publish\API\Repository\Exceptions\NotFoundException::class); |
||
| 1766 | $this->expectExceptionMessage('Could not find \'Language\' with identifier \'Klingon\''); |
||
| 1767 | |||
| 1768 | $repositoryMock = $this->getRepositoryMock(); |
||
| 1769 | $mockedService = $this->getPartlyMockedContentService(); |
||
| 1770 | /** @var \PHPUnit\Framework\MockObject\MockObject $languageHandlerMock */ |
||
| 1771 | $languageHandlerMock = $this->getPersistenceMock()->contentLanguageHandler(); |
||
| 1772 | $contentTypeServiceMock = $this->getContentTypeServiceMock(); |
||
| 1773 | $domainMapperMock = $this->getDomainMapperMock(); |
||
| 1774 | $permissionResolver = $this->getPermissionResolverMock(); |
||
| 1775 | |||
| 1776 | $contentType = new ContentType( |
||
| 1777 | [ |
||
| 1778 | 'id' => 123, |
||
| 1779 | 'fieldDefinitions' => [], |
||
| 1780 | ] |
||
| 1781 | ); |
||
| 1782 | $contentCreateStruct = new ContentCreateStruct( |
||
| 1783 | [ |
||
| 1784 | 'fields' => $structFields, |
||
| 1785 | 'mainLanguageCode' => $mainLanguageCode, |
||
| 1786 | 'contentType' => $contentType, |
||
| 1787 | 'alwaysAvailable' => false, |
||
| 1788 | 'ownerId' => 169, |
||
| 1789 | 'sectionId' => 1, |
||
| 1790 | ] |
||
| 1791 | ); |
||
| 1792 | |||
| 1793 | $languageHandlerMock->expects($this->any()) |
||
| 1794 | ->method('loadByLanguageCode') |
||
| 1795 | ->with($this->isType('string')) |
||
| 1796 | ->will( |
||
| 1797 | $this->returnCallback( |
||
| 1798 | View Code Duplication | function ($languageCode) { |
|
| 1799 | if ($languageCode === 'Klingon') { |
||
| 1800 | throw new NotFoundException('Language', 'Klingon'); |
||
| 1801 | } |
||
| 1802 | |||
| 1803 | return new Language(['id' => 4242]); |
||
| 1804 | } |
||
| 1805 | ) |
||
| 1806 | ); |
||
| 1807 | |||
| 1808 | $contentTypeServiceMock->expects($this->once()) |
||
| 1809 | ->method('loadContentType') |
||
| 1810 | ->with($this->equalTo($contentType->id)) |
||
| 1811 | ->will($this->returnValue($contentType)); |
||
| 1812 | |||
| 1813 | $repositoryMock->expects($this->once()) |
||
| 1814 | ->method('getContentTypeService') |
||
| 1815 | ->will($this->returnValue($contentTypeServiceMock)); |
||
| 1816 | |||
| 1817 | $that = $this; |
||
| 1818 | $permissionResolver->expects($this->once()) |
||
| 1819 | ->method('canUser') |
||
| 1820 | ->with( |
||
| 1821 | $this->equalTo('content'), |
||
| 1822 | $this->equalTo('create'), |
||
| 1823 | $this->isInstanceOf(APIContentCreateStruct::class), |
||
| 1824 | $this->equalTo([]) |
||
| 1825 | )->will( |
||
| 1826 | $this->returnCallback( |
||
| 1827 | function () use ($that, $contentCreateStruct) { |
||
| 1828 | $that->assertEquals($contentCreateStruct, func_get_arg(2)); |
||
| 1829 | |||
| 1830 | return true; |
||
| 1831 | } |
||
| 1832 | ) |
||
| 1833 | ); |
||
| 1834 | |||
| 1835 | $domainMapperMock->expects($this->once()) |
||
| 1836 | ->method('getUniqueHash') |
||
| 1837 | ->with($this->isInstanceOf(APIContentCreateStruct::class)) |
||
| 1838 | ->will( |
||
| 1839 | $this->returnCallback( |
||
| 1840 | function ($object) use ($that, $contentCreateStruct) { |
||
| 1841 | $that->assertEquals($contentCreateStruct, $object); |
||
| 1842 | |||
| 1843 | return 'hash'; |
||
| 1844 | } |
||
| 1845 | ) |
||
| 1846 | ); |
||
| 1847 | |||
| 1848 | $mockedService->createContent($contentCreateStruct, []); |
||
| 1849 | } |
||
| 1850 | |||
| 1851 | protected function assertForCreateContentContentValidationException( |
||
| 1852 | $mainLanguageCode, |
||
| 1853 | $structFields, |
||
| 1854 | $fieldDefinitions = [] |
||
| 1855 | ) { |
||
| 1856 | $repositoryMock = $this->getRepositoryMock(); |
||
| 1857 | $mockedService = $this->getPartlyMockedContentService(['loadContentByRemoteId']); |
||
| 1858 | $contentTypeServiceMock = $this->getContentTypeServiceMock(); |
||
| 1859 | $permissionResolver = $this->getPermissionResolverMock(); |
||
| 1860 | |||
| 1861 | $contentType = new ContentType( |
||
| 1862 | [ |
||
| 1863 | 'id' => 123, |
||
| 1864 | 'fieldDefinitions' => $fieldDefinitions, |
||
| 1865 | ] |
||
| 1866 | ); |
||
| 1867 | $contentCreateStruct = new ContentCreateStruct( |
||
| 1868 | [ |
||
| 1869 | 'ownerId' => 169, |
||
| 1870 | 'alwaysAvailable' => false, |
||
| 1871 | 'remoteId' => 'faraday', |
||
| 1872 | 'mainLanguageCode' => $mainLanguageCode, |
||
| 1873 | 'fields' => $structFields, |
||
| 1874 | 'contentType' => $contentType, |
||
| 1875 | ] |
||
| 1876 | ); |
||
| 1877 | |||
| 1878 | $contentTypeServiceMock->expects($this->once()) |
||
| 1879 | ->method('loadContentType') |
||
| 1880 | ->with($this->equalTo(123)) |
||
| 1881 | ->will($this->returnValue($contentType)); |
||
| 1882 | |||
| 1883 | $repositoryMock->expects($this->once()) |
||
| 1884 | ->method('getContentTypeService') |
||
| 1885 | ->will($this->returnValue($contentTypeServiceMock)); |
||
| 1886 | |||
| 1887 | $permissionResolver->expects($this->once()) |
||
| 1888 | ->method('canUser') |
||
| 1889 | ->with( |
||
| 1890 | $this->equalTo('content'), |
||
| 1891 | $this->equalTo('create'), |
||
| 1892 | $this->isInstanceOf(get_class($contentCreateStruct)), |
||
| 1893 | $this->equalTo([]) |
||
| 1894 | )->will($this->returnValue(true)); |
||
| 1895 | |||
| 1896 | $mockedService->expects($this->once()) |
||
| 1897 | ->method('loadContentByRemoteId') |
||
| 1898 | ->with($contentCreateStruct->remoteId) |
||
| 1899 | ->will( |
||
| 1900 | $this->throwException(new NotFoundException('Content', 'faraday')) |
||
| 1901 | ); |
||
| 1902 | |||
| 1903 | $mockedService->createContent($contentCreateStruct, []); |
||
| 1904 | } |
||
| 1905 | |||
| 1906 | View Code Duplication | public function providerForTestCreateContentThrowsContentValidationExceptionFieldDefinition() |
|
| 1907 | { |
||
| 1908 | return [ |
||
| 1909 | [ |
||
| 1910 | 'eng-GB', |
||
| 1911 | [ |
||
| 1912 | new Field( |
||
| 1913 | [ |
||
| 1914 | 'fieldDefIdentifier' => 'identifier', |
||
| 1915 | 'value' => 'newValue', |
||
| 1916 | 'languageCode' => 'eng-GB', |
||
| 1917 | ] |
||
| 1918 | ), |
||
| 1919 | ], |
||
| 1920 | ], |
||
| 1921 | ]; |
||
| 1922 | } |
||
| 1923 | |||
| 1924 | /** |
||
| 1925 | * Test for the createContent() method. |
||
| 1926 | * |
||
| 1927 | * @covers \eZ\Publish\Core\Repository\ContentService::getLanguageCodesForCreate |
||
| 1928 | * @covers \eZ\Publish\Core\Repository\ContentService::mapFieldsForCreate |
||
| 1929 | * @covers \eZ\Publish\Core\Repository\ContentService::createContent |
||
| 1930 | * @dataProvider providerForTestCreateContentThrowsContentValidationExceptionFieldDefinition |
||
| 1931 | */ |
||
| 1932 | public function testCreateContentThrowsContentValidationExceptionFieldDefinition($mainLanguageCode, $structFields) |
||
| 1933 | { |
||
| 1934 | $this->expectException(\eZ\Publish\API\Repository\Exceptions\ContentValidationException::class); |
||
| 1935 | $this->expectExceptionMessage('Field definition \'identifier\' does not exist in given ContentType'); |
||
| 1936 | |||
| 1937 | $this->assertForCreateContentContentValidationException( |
||
| 1938 | $mainLanguageCode, |
||
| 1939 | $structFields, |
||
| 1940 | [] |
||
| 1941 | ); |
||
| 1942 | } |
||
| 1943 | |||
| 1944 | View Code Duplication | public function providerForTestCreateContentThrowsContentValidationExceptionTranslation() |
|
| 1945 | { |
||
| 1946 | return [ |
||
| 1947 | [ |
||
| 1948 | 'eng-GB', |
||
| 1949 | [ |
||
| 1950 | new Field( |
||
| 1951 | [ |
||
| 1952 | 'fieldDefIdentifier' => 'identifier', |
||
| 1953 | 'value' => 'newValue', |
||
| 1954 | 'languageCode' => 'eng-US', |
||
| 1955 | ] |
||
| 1956 | ), |
||
| 1957 | ], |
||
| 1958 | ], |
||
| 1959 | ]; |
||
| 1960 | } |
||
| 1961 | |||
| 1962 | /** |
||
| 1963 | * Test for the createContent() method. |
||
| 1964 | * |
||
| 1965 | * @covers \eZ\Publish\Core\Repository\ContentService::getLanguageCodesForCreate |
||
| 1966 | * @covers \eZ\Publish\Core\Repository\ContentService::mapFieldsForCreate |
||
| 1967 | * @covers \eZ\Publish\Core\Repository\ContentService::createContent |
||
| 1968 | * @dataProvider providerForTestCreateContentThrowsContentValidationExceptionTranslation |
||
| 1969 | */ |
||
| 1970 | View Code Duplication | public function testCreateContentThrowsContentValidationExceptionTranslation($mainLanguageCode, $structFields) |
|
| 1971 | { |
||
| 1972 | $this->expectException(\eZ\Publish\API\Repository\Exceptions\ContentValidationException::class); |
||
| 1973 | $this->expectExceptionMessage('A value is set for non translatable field definition \'identifier\' with language \'eng-US\''); |
||
| 1974 | |||
| 1975 | $fieldDefinitions = [ |
||
| 1976 | new FieldDefinition( |
||
| 1977 | [ |
||
| 1978 | 'id' => 'fieldDefinitionId1', |
||
| 1979 | 'fieldTypeIdentifier' => 'fieldTypeIdentifier', |
||
| 1980 | 'isTranslatable' => false, |
||
| 1981 | 'identifier' => 'identifier', |
||
| 1982 | 'isRequired' => false, |
||
| 1983 | 'defaultValue' => self::EMPTY_FIELD_VALUE, |
||
| 1984 | ] |
||
| 1985 | ), |
||
| 1986 | ]; |
||
| 1987 | |||
| 1988 | $this->assertForCreateContentContentValidationException( |
||
| 1989 | $mainLanguageCode, |
||
| 1990 | $structFields, |
||
| 1991 | $fieldDefinitions |
||
| 1992 | ); |
||
| 1993 | } |
||
| 1994 | |||
| 1995 | /** |
||
| 1996 | * Asserts behaviour necessary for testing ContentFieldValidationException because of required |
||
| 1997 | * field being empty. |
||
| 1998 | * |
||
| 1999 | * @param string $mainLanguageCode |
||
| 2000 | * @param \eZ\Publish\API\Repository\Values\Content\Field[] $structFields |
||
| 2001 | * @param \eZ\Publish\API\Repository\Values\ContentType\FieldDefinition[] $fieldDefinitions |
||
| 2002 | * |
||
| 2003 | * @return mixed |
||
| 2004 | */ |
||
| 2005 | protected function assertForTestCreateContentRequiredField( |
||
| 2006 | $mainLanguageCode, |
||
| 2007 | array $structFields, |
||
| 2008 | array $fieldDefinitions |
||
| 2009 | ) { |
||
| 2010 | $repositoryMock = $this->getRepositoryMock(); |
||
| 2011 | /** @var \PHPUnit\Framework\MockObject\MockObject $languageHandlerMock */ |
||
| 2012 | $languageHandlerMock = $this->getPersistenceMock()->contentLanguageHandler(); |
||
| 2013 | $contentTypeServiceMock = $this->getContentTypeServiceMock(); |
||
| 2014 | $fieldTypeServiceMock = $this->getFieldTypeServiceMock(); |
||
| 2015 | $domainMapperMock = $this->getDomainMapperMock(); |
||
| 2016 | $fieldTypeMock = $this->createMock(SPIFieldType::class); |
||
| 2017 | $permissionResolver = $this->getPermissionResolverMock(); |
||
| 2018 | |||
| 2019 | $contentType = new ContentType( |
||
| 2020 | [ |
||
| 2021 | 'id' => 123, |
||
| 2022 | 'fieldDefinitions' => $fieldDefinitions, |
||
| 2023 | 'nameSchema' => '<nameSchema>', |
||
| 2024 | ] |
||
| 2025 | ); |
||
| 2026 | $contentCreateStruct = new ContentCreateStruct( |
||
| 2027 | [ |
||
| 2028 | 'fields' => $structFields, |
||
| 2029 | 'mainLanguageCode' => $mainLanguageCode, |
||
| 2030 | 'contentType' => $contentType, |
||
| 2031 | 'alwaysAvailable' => false, |
||
| 2032 | 'ownerId' => 169, |
||
| 2033 | 'sectionId' => 1, |
||
| 2034 | ] |
||
| 2035 | ); |
||
| 2036 | |||
| 2037 | $languageHandlerMock->expects($this->any()) |
||
| 2038 | ->method('loadByLanguageCode') |
||
| 2039 | ->with($this->isType('string')) |
||
| 2040 | ->will( |
||
| 2041 | $this->returnCallback( |
||
| 2042 | function () { |
||
| 2043 | return new Language(['id' => 4242]); |
||
| 2044 | } |
||
| 2045 | ) |
||
| 2046 | ); |
||
| 2047 | |||
| 2048 | $contentTypeServiceMock->expects($this->once()) |
||
| 2049 | ->method('loadContentType') |
||
| 2050 | ->with($this->equalTo($contentType->id)) |
||
| 2051 | ->will($this->returnValue($contentType)); |
||
| 2052 | |||
| 2053 | $repositoryMock->expects($this->once()) |
||
| 2054 | ->method('getContentTypeService') |
||
| 2055 | ->will($this->returnValue($contentTypeServiceMock)); |
||
| 2056 | |||
| 2057 | $that = $this; |
||
| 2058 | $permissionResolver->expects($this->once()) |
||
| 2059 | ->method('canUser') |
||
| 2060 | ->with( |
||
| 2061 | $this->equalTo('content'), |
||
| 2062 | $this->equalTo('create'), |
||
| 2063 | $this->isInstanceOf(APIContentCreateStruct::class), |
||
| 2064 | $this->equalTo([]) |
||
| 2065 | )->will( |
||
| 2066 | $this->returnCallback( |
||
| 2067 | function () use ($that, $contentCreateStruct) { |
||
| 2068 | $that->assertEquals($contentCreateStruct, func_get_arg(2)); |
||
| 2069 | |||
| 2070 | return true; |
||
| 2071 | } |
||
| 2072 | ) |
||
| 2073 | ); |
||
| 2074 | |||
| 2075 | $domainMapperMock->expects($this->once()) |
||
| 2076 | ->method('getUniqueHash') |
||
| 2077 | ->with($this->isInstanceOf(APIContentCreateStruct::class)) |
||
| 2078 | ->will( |
||
| 2079 | $this->returnCallback( |
||
| 2080 | function ($object) use ($that, $contentCreateStruct) { |
||
| 2081 | $that->assertEquals($contentCreateStruct, $object); |
||
| 2082 | |||
| 2083 | return 'hash'; |
||
| 2084 | } |
||
| 2085 | ) |
||
| 2086 | ); |
||
| 2087 | |||
| 2088 | $fieldTypeMock->expects($this->any()) |
||
| 2089 | ->method('acceptValue') |
||
| 2090 | ->will( |
||
| 2091 | $this->returnCallback( |
||
| 2092 | function ($valueString) { |
||
| 2093 | return new ValueStub($valueString); |
||
| 2094 | } |
||
| 2095 | ) |
||
| 2096 | ); |
||
| 2097 | |||
| 2098 | $emptyValue = self::EMPTY_FIELD_VALUE; |
||
| 2099 | $fieldTypeMock->expects($this->any()) |
||
| 2100 | ->method('isEmptyValue') |
||
| 2101 | ->will( |
||
| 2102 | $this->returnCallback( |
||
| 2103 | function (ValueStub $value) use ($emptyValue) { |
||
| 2104 | return $emptyValue === (string)$value; |
||
| 2105 | } |
||
| 2106 | ) |
||
| 2107 | ); |
||
| 2108 | |||
| 2109 | $fieldTypeMock->expects($this->any()) |
||
| 2110 | ->method('validate') |
||
| 2111 | ->will($this->returnValue([])); |
||
| 2112 | |||
| 2113 | $this->getFieldTypeRegistryMock()->expects($this->any()) |
||
| 2114 | ->method('getFieldType') |
||
| 2115 | ->will($this->returnValue($fieldTypeMock)); |
||
| 2116 | |||
| 2117 | return $contentCreateStruct; |
||
| 2118 | } |
||
| 2119 | |||
| 2120 | View Code Duplication | public function providerForTestCreateContentThrowsContentValidationExceptionRequiredField() |
|
| 2121 | { |
||
| 2122 | return [ |
||
| 2123 | [ |
||
| 2124 | 'eng-US', |
||
| 2125 | [ |
||
| 2126 | new Field( |
||
| 2127 | [ |
||
| 2128 | 'fieldDefIdentifier' => 'identifier', |
||
| 2129 | 'value' => self::EMPTY_FIELD_VALUE, |
||
| 2130 | 'languageCode' => null, |
||
| 2131 | ] |
||
| 2132 | ), |
||
| 2133 | ], |
||
| 2134 | 'identifier', |
||
| 2135 | 'eng-US', |
||
| 2136 | ], |
||
| 2137 | ]; |
||
| 2138 | } |
||
| 2139 | |||
| 2140 | /** |
||
| 2141 | * Test for the createContent() method. |
||
| 2142 | * |
||
| 2143 | * @covers \eZ\Publish\Core\Repository\ContentService::getLanguageCodesForCreate |
||
| 2144 | * @covers \eZ\Publish\Core\Repository\ContentService::mapFieldsForCreate |
||
| 2145 | * @covers \eZ\Publish\Core\Repository\ContentService::createContent |
||
| 2146 | * @dataProvider providerForTestCreateContentThrowsContentValidationExceptionRequiredField |
||
| 2147 | */ |
||
| 2148 | public function testCreateContentRequiredField( |
||
| 2149 | $mainLanguageCode, |
||
| 2150 | $structFields, |
||
| 2151 | $identifier, |
||
| 2152 | $languageCode |
||
| 2153 | ) { |
||
| 2154 | $this->expectException(\eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException::class); |
||
| 2155 | |||
| 2156 | $fieldDefinitions = [ |
||
| 2157 | new FieldDefinition( |
||
| 2158 | [ |
||
| 2159 | 'id' => 'fieldDefinitionId', |
||
| 2160 | 'fieldTypeIdentifier' => 'fieldTypeIdentifier', |
||
| 2161 | 'isTranslatable' => true, |
||
| 2162 | 'identifier' => 'identifier', |
||
| 2163 | 'isRequired' => true, |
||
| 2164 | 'defaultValue' => 'defaultValue', |
||
| 2165 | ] |
||
| 2166 | ), |
||
| 2167 | ]; |
||
| 2168 | $contentCreateStruct = $this->assertForTestCreateContentRequiredField( |
||
| 2169 | $mainLanguageCode, |
||
| 2170 | $structFields, |
||
| 2171 | $fieldDefinitions |
||
| 2172 | ); |
||
| 2173 | |||
| 2174 | $mockedService = $this->getPartlyMockedContentService(); |
||
| 2175 | |||
| 2176 | try { |
||
| 2177 | $mockedService->createContent($contentCreateStruct, []); |
||
| 2178 | } catch (ContentValidationException $e) { |
||
| 2179 | $this->assertEquals( |
||
| 2180 | "Value for required field definition '{$identifier}' with language '{$languageCode}' is empty", |
||
| 2181 | $e->getMessage() |
||
| 2182 | ); |
||
| 2183 | |||
| 2184 | throw $e; |
||
| 2185 | } |
||
| 2186 | } |
||
| 2187 | |||
| 2188 | /** |
||
| 2189 | * Asserts behaviour necessary for testing ContentFieldValidationException because of |
||
| 2190 | * field not being valid. |
||
| 2191 | * |
||
| 2192 | * @param string $mainLanguageCode |
||
| 2193 | * @param \eZ\Publish\API\Repository\Values\Content\Field[] $structFields |
||
| 2194 | * @param \eZ\Publish\API\Repository\Values\ContentType\FieldDefinition[] $fieldDefinitions |
||
| 2195 | * |
||
| 2196 | * @return mixed |
||
| 2197 | */ |
||
| 2198 | protected function assertForTestCreateContentThrowsContentFieldValidationException( |
||
| 2199 | $mainLanguageCode, |
||
| 2200 | array $structFields, |
||
| 2201 | array $fieldDefinitions |
||
| 2202 | ) { |
||
| 2203 | $repositoryMock = $this->getRepositoryMock(); |
||
| 2204 | /** @var \PHPUnit\Framework\MockObject\MockObject $languageHandlerMock */ |
||
| 2205 | $languageHandlerMock = $this->getPersistenceMock()->contentLanguageHandler(); |
||
| 2206 | $contentTypeServiceMock = $this->getContentTypeServiceMock(); |
||
| 2207 | $fieldTypeServiceMock = $this->getFieldTypeServiceMock(); |
||
| 2208 | $domainMapperMock = $this->getDomainMapperMock(); |
||
| 2209 | $relationProcessorMock = $this->getRelationProcessorMock(); |
||
| 2210 | $fieldTypeMock = $this->createMock(SPIFieldType::class); |
||
| 2211 | $languageCodes = $this->determineLanguageCodesForCreate($mainLanguageCode, $structFields); |
||
| 2212 | $permissionResolver = $this->getPermissionResolverMock(); |
||
| 2213 | |||
| 2214 | $contentType = new ContentType( |
||
| 2215 | [ |
||
| 2216 | 'id' => 123, |
||
| 2217 | 'fieldDefinitions' => $fieldDefinitions, |
||
| 2218 | 'nameSchema' => '<nameSchema>', |
||
| 2219 | ] |
||
| 2220 | ); |
||
| 2221 | $contentCreateStruct = new ContentCreateStruct( |
||
| 2222 | [ |
||
| 2223 | 'fields' => $structFields, |
||
| 2224 | 'mainLanguageCode' => $mainLanguageCode, |
||
| 2225 | 'contentType' => $contentType, |
||
| 2226 | 'alwaysAvailable' => false, |
||
| 2227 | 'ownerId' => 169, |
||
| 2228 | 'sectionId' => 1, |
||
| 2229 | ] |
||
| 2230 | ); |
||
| 2231 | |||
| 2232 | $languageHandlerMock->expects($this->any()) |
||
| 2233 | ->method('loadByLanguageCode') |
||
| 2234 | ->with($this->isType('string')) |
||
| 2235 | ->will( |
||
| 2236 | $this->returnCallback( |
||
| 2237 | function () { |
||
| 2238 | return new Language(['id' => 4242]); |
||
| 2239 | } |
||
| 2240 | ) |
||
| 2241 | ); |
||
| 2242 | |||
| 2243 | $contentTypeServiceMock->expects($this->once()) |
||
| 2244 | ->method('loadContentType') |
||
| 2245 | ->with($this->equalTo($contentType->id)) |
||
| 2246 | ->will($this->returnValue($contentType)); |
||
| 2247 | |||
| 2248 | $repositoryMock->expects($this->once()) |
||
| 2249 | ->method('getContentTypeService') |
||
| 2250 | ->will($this->returnValue($contentTypeServiceMock)); |
||
| 2251 | |||
| 2252 | $that = $this; |
||
| 2253 | $permissionResolver->expects($this->once()) |
||
| 2254 | ->method('canUser') |
||
| 2255 | ->with( |
||
| 2256 | $this->equalTo('content'), |
||
| 2257 | $this->equalTo('create'), |
||
| 2258 | $this->isInstanceOf(APIContentCreateStruct::class), |
||
| 2259 | $this->equalTo([]) |
||
| 2260 | )->will( |
||
| 2261 | $this->returnCallback( |
||
| 2262 | function () use ($that, $contentCreateStruct) { |
||
| 2263 | $that->assertEquals($contentCreateStruct, func_get_arg(2)); |
||
| 2264 | |||
| 2265 | return true; |
||
| 2266 | } |
||
| 2267 | ) |
||
| 2268 | ); |
||
| 2269 | |||
| 2270 | $domainMapperMock->expects($this->once()) |
||
| 2271 | ->method('getUniqueHash') |
||
| 2272 | ->with($this->isInstanceOf(APIContentCreateStruct::class)) |
||
| 2273 | ->will( |
||
| 2274 | $this->returnCallback( |
||
| 2275 | function ($object) use ($that, $contentCreateStruct) { |
||
| 2276 | $that->assertEquals($contentCreateStruct, $object); |
||
| 2277 | |||
| 2278 | return 'hash'; |
||
| 2279 | } |
||
| 2280 | ) |
||
| 2281 | ); |
||
| 2282 | |||
| 2283 | $this->getFieldTypeRegistryMock()->expects($this->any()) |
||
| 2284 | ->method('getFieldType') |
||
| 2285 | ->will($this->returnValue($fieldTypeMock)); |
||
| 2286 | |||
| 2287 | $relationProcessorMock |
||
| 2288 | ->expects($this->any()) |
||
| 2289 | ->method('appendFieldRelations') |
||
| 2290 | ->with( |
||
| 2291 | $this->isType('array'), |
||
| 2292 | $this->isType('array'), |
||
| 2293 | $this->isInstanceOf(SPIFieldType::class), |
||
| 2294 | $this->isInstanceOf(Value::class), |
||
| 2295 | $this->anything() |
||
| 2296 | ); |
||
| 2297 | |||
| 2298 | $fieldValues = $this->determineValuesForCreate( |
||
| 2299 | $mainLanguageCode, |
||
| 2300 | $structFields, |
||
| 2301 | $fieldDefinitions, |
||
| 2302 | $languageCodes |
||
| 2303 | ); |
||
| 2304 | $allFieldErrors = []; |
||
| 2305 | $validateCount = 0; |
||
| 2306 | $emptyValue = self::EMPTY_FIELD_VALUE; |
||
| 2307 | foreach ($contentType->getFieldDefinitions() as $fieldDefinition) { |
||
| 2308 | foreach ($fieldValues[$fieldDefinition->identifier] as $languageCode => $value) { |
||
| 2309 | $fieldTypeMock->expects($this->at($validateCount++)) |
||
| 2310 | ->method('acceptValue') |
||
| 2311 | ->will( |
||
| 2312 | $this->returnCallback( |
||
| 2313 | function ($valueString) { |
||
| 2314 | return new ValueStub($valueString); |
||
| 2315 | } |
||
| 2316 | ) |
||
| 2317 | ); |
||
| 2318 | |||
| 2319 | $fieldTypeMock->expects($this->at($validateCount++)) |
||
| 2320 | ->method('isEmptyValue') |
||
| 2321 | ->will( |
||
| 2322 | $this->returnCallback( |
||
| 2323 | function (ValueStub $value) use ($emptyValue) { |
||
| 2324 | return $emptyValue === (string)$value; |
||
| 2325 | } |
||
| 2326 | ) |
||
| 2327 | ); |
||
| 2328 | |||
| 2329 | if (self::EMPTY_FIELD_VALUE === (string)$value) { |
||
| 2330 | continue; |
||
| 2331 | } |
||
| 2332 | |||
| 2333 | $fieldTypeMock->expects($this->at($validateCount++)) |
||
| 2334 | ->method('validate') |
||
| 2335 | ->with( |
||
| 2336 | $this->equalTo($fieldDefinition), |
||
| 2337 | $this->equalTo($value) |
||
| 2338 | )->will($this->returnArgument(1)); |
||
| 2339 | |||
| 2340 | $allFieldErrors[$fieldDefinition->id][$languageCode] = $value; |
||
| 2341 | } |
||
| 2342 | } |
||
| 2343 | |||
| 2344 | return [$contentCreateStruct, $allFieldErrors]; |
||
| 2345 | } |
||
| 2346 | |||
| 2347 | public function providerForTestCreateContentThrowsContentFieldValidationException() |
||
| 2348 | { |
||
| 2349 | return $this->providerForTestCreateContentNonRedundantFieldSetComplex(); |
||
| 2350 | } |
||
| 2351 | |||
| 2352 | /** |
||
| 2353 | * Test for the createContent() method. |
||
| 2354 | * |
||
| 2355 | * @covers \eZ\Publish\Core\Repository\ContentService::getLanguageCodesForCreate |
||
| 2356 | * @covers \eZ\Publish\Core\Repository\ContentService::mapFieldsForCreate |
||
| 2357 | * @covers \eZ\Publish\Core\Repository\ContentService::createContent |
||
| 2358 | * @dataProvider providerForTestCreateContentThrowsContentFieldValidationException |
||
| 2359 | */ |
||
| 2360 | View Code Duplication | public function testCreateContentThrowsContentFieldValidationException($mainLanguageCode, $structFields) |
|
| 2361 | { |
||
| 2362 | $this->expectException(\eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException::class); |
||
| 2363 | $this->expectExceptionMessage('Content fields did not validate'); |
||
| 2364 | |||
| 2365 | $fieldDefinitions = $this->fixturesForTestCreateContentNonRedundantFieldSetComplex(); |
||
| 2366 | list($contentCreateStruct, $allFieldErrors) = |
||
| 2367 | $this->assertForTestCreateContentThrowsContentFieldValidationException( |
||
| 2368 | $mainLanguageCode, |
||
| 2369 | $structFields, |
||
| 2370 | $fieldDefinitions |
||
| 2371 | ); |
||
| 2372 | |||
| 2373 | $mockedService = $this->getPartlyMockedContentService(); |
||
| 2374 | |||
| 2375 | try { |
||
| 2376 | $mockedService->createContent($contentCreateStruct); |
||
| 2377 | } catch (ContentFieldValidationException $e) { |
||
| 2378 | $this->assertEquals($allFieldErrors, $e->getFieldErrors()); |
||
| 2379 | throw $e; |
||
| 2380 | } |
||
| 2381 | } |
||
| 2382 | |||
| 2383 | /** |
||
| 2384 | * Test for the createContent() method. |
||
| 2385 | * |
||
| 2386 | * @covers \eZ\Publish\Core\Repository\ContentService::getLanguageCodesForCreate |
||
| 2387 | * @covers \eZ\Publish\Core\Repository\ContentService::mapFieldsForCreate |
||
| 2388 | * @covers \eZ\Publish\Core\Repository\ContentService::buildSPILocationCreateStructs |
||
| 2389 | * @covers \eZ\Publish\Core\Repository\ContentService::createContent |
||
| 2390 | */ |
||
| 2391 | public function testCreateContentWithLocations() |
||
| 2518 | |||
| 2519 | /** |
||
| 2520 | * Test for the createContent() method. |
||
| 2521 | * |
||
| 2522 | * @covers \eZ\Publish\Core\Repository\ContentService::getLanguageCodesForCreate |
||
| 2523 | * @covers \eZ\Publish\Core\Repository\ContentService::mapFieldsForCreate |
||
| 2524 | * @covers \eZ\Publish\Core\Repository\ContentService::buildSPILocationCreateStructs |
||
| 2525 | * @covers \eZ\Publish\Core\Repository\ContentService::createContent |
||
| 2526 | */ |
||
| 2527 | public function testCreateContentWithLocationsDuplicateUnderParent() |
||
| 2528 | { |
||
| 2529 | $this->expectException(\eZ\Publish\API\Repository\Exceptions\InvalidArgumentException::class); |
||
| 2530 | $this->expectExceptionMessage('Multiple LocationCreateStructs with the same parent Location \'321\' are given'); |
||
| 2531 | |||
| 2532 | $fieldDefinitions = [ |
||
| 2533 | new FieldDefinition( |
||
| 2534 | [ |
||
| 2535 | 'id' => 'fieldDefinitionId', |
||
| 2536 | 'fieldTypeIdentifier' => 'fieldTypeIdentifier', |
||
| 2537 | 'isTranslatable' => false, |
||
| 2538 | 'identifier' => 'identifier', |
||
| 2539 | 'isRequired' => false, |
||
| 2540 | 'defaultValue' => 'defaultValue', |
||
| 2541 | ] |
||
| 2542 | ), |
||
| 2543 | ]; |
||
| 2544 | |||
| 2545 | $repositoryMock = $this->getRepositoryMock(); |
||
| 2546 | $mockedService = $this->getPartlyMockedContentService(); |
||
| 2547 | $locationServiceMock = $this->getLocationServiceMock(); |
||
| 2548 | $contentTypeServiceMock = $this->getContentTypeServiceMock(); |
||
| 2549 | $domainMapperMock = $this->getDomainMapperMock(); |
||
| 2550 | $permissionResolver = $this->getPermissionResolverMock(); |
||
| 2551 | /** @var \PHPUnit\Framework\MockObject\MockObject $languageHandlerMock */ |
||
| 2552 | $languageHandlerMock = $this->getPersistenceMock()->contentLanguageHandler(); |
||
| 2553 | $spiLocationCreateStruct = new SPILocation\CreateStruct(); |
||
| 2554 | $parentLocation = new Location(['id' => 321]); |
||
| 2555 | $locationCreateStruct = new LocationCreateStruct(['parentLocationId' => 321]); |
||
| 2556 | $locationCreateStructs = [$locationCreateStruct, clone $locationCreateStruct]; |
||
| 2557 | $contentType = new ContentType( |
||
| 2558 | [ |
||
| 2559 | 'id' => 123, |
||
| 2560 | 'fieldDefinitions' => $fieldDefinitions, |
||
| 2561 | 'nameSchema' => '<nameSchema>', |
||
| 2562 | ] |
||
| 2563 | ); |
||
| 2564 | $contentCreateStruct = new ContentCreateStruct( |
||
| 2565 | [ |
||
| 2566 | 'fields' => [], |
||
| 2567 | 'mainLanguageCode' => 'eng-US', |
||
| 2568 | 'contentType' => $contentType, |
||
| 2569 | 'alwaysAvailable' => false, |
||
| 2570 | 'ownerId' => 169, |
||
| 2571 | 'sectionId' => 1, |
||
| 2572 | ] |
||
| 2573 | ); |
||
| 2574 | |||
| 2575 | $languageHandlerMock->expects($this->any()) |
||
| 2576 | ->method('loadByLanguageCode') |
||
| 2577 | ->with($this->isType('string')) |
||
| 2578 | ->will( |
||
| 2579 | $this->returnCallback( |
||
| 2580 | function () { |
||
| 2581 | return new Language(['id' => 4242]); |
||
| 2582 | } |
||
| 2583 | ) |
||
| 2584 | ); |
||
| 2585 | |||
| 2586 | $contentTypeServiceMock->expects($this->once()) |
||
| 2587 | ->method('loadContentType') |
||
| 2588 | ->with($this->equalTo($contentType->id)) |
||
| 2589 | ->will($this->returnValue($contentType)); |
||
| 2590 | |||
| 2591 | $repositoryMock->expects($this->once()) |
||
| 2592 | ->method('getContentTypeService') |
||
| 2593 | ->will($this->returnValue($contentTypeServiceMock)); |
||
| 2594 | |||
| 2595 | $that = $this; |
||
| 2596 | $permissionResolver->expects($this->once()) |
||
| 2597 | ->method('canUser') |
||
| 2598 | ->with( |
||
| 2599 | $this->equalTo('content'), |
||
| 2600 | $this->equalTo('create'), |
||
| 2601 | $this->isInstanceOf(APIContentCreateStruct::class), |
||
| 2602 | $this->equalTo($locationCreateStructs) |
||
| 2603 | )->will( |
||
| 2604 | $this->returnCallback( |
||
| 2605 | function () use ($that, $contentCreateStruct) { |
||
| 2606 | $that->assertEquals($contentCreateStruct, func_get_arg(2)); |
||
| 2607 | |||
| 2608 | return true; |
||
| 2609 | } |
||
| 2610 | ) |
||
| 2611 | ); |
||
| 2612 | |||
| 2613 | $domainMapperMock->expects($this->once()) |
||
| 2614 | ->method('getUniqueHash') |
||
| 2615 | ->with($this->isInstanceOf(APIContentCreateStruct::class)) |
||
| 2616 | ->will( |
||
| 2617 | $this->returnCallback( |
||
| 2618 | function ($object) use ($that, $contentCreateStruct) { |
||
| 2619 | $that->assertEquals($contentCreateStruct, $object); |
||
| 2620 | |||
| 2621 | return 'hash'; |
||
| 2622 | } |
||
| 2623 | ) |
||
| 2624 | ); |
||
| 2625 | |||
| 2626 | $locationServiceMock->expects($this->once()) |
||
| 2627 | ->method('loadLocation') |
||
| 2628 | ->with($this->equalTo(321)) |
||
| 2629 | ->will($this->returnValue($parentLocation)); |
||
| 2630 | |||
| 2631 | $repositoryMock->expects($this->any()) |
||
| 2632 | ->method('getLocationService') |
||
| 2633 | ->will($this->returnValue($locationServiceMock)); |
||
| 2634 | |||
| 2635 | $domainMapperMock->expects($this->any()) |
||
| 2636 | ->method('buildSPILocationCreateStruct') |
||
| 2637 | ->with( |
||
| 2638 | $this->equalTo($locationCreateStruct), |
||
| 2639 | $this->equalTo($parentLocation), |
||
| 2640 | $this->equalTo(true), |
||
| 2641 | $this->equalTo(null), |
||
| 2642 | $this->equalTo(null) |
||
| 2643 | )->will($this->returnValue($spiLocationCreateStruct)); |
||
| 2644 | |||
| 2645 | $mockedService->createContent( |
||
| 2646 | $contentCreateStruct, |
||
| 2647 | $locationCreateStructs |
||
| 2648 | ); |
||
| 2649 | } |
||
| 2650 | |||
| 2651 | /** |
||
| 2652 | * Test for the createContent() method. |
||
| 2653 | * |
||
| 2654 | * @covers \eZ\Publish\Core\Repository\ContentService::getLanguageCodesForCreate |
||
| 2655 | * @covers \eZ\Publish\Core\Repository\ContentService::mapFieldsForCreate |
||
| 2656 | * @covers \eZ\Publish\Core\Repository\ContentService::getDefaultObjectStates |
||
| 2657 | * @covers \eZ\Publish\Core\Repository\ContentService::createContent |
||
| 2658 | */ |
||
| 2659 | public function testCreateContentObjectStates() |
||
| 2755 | |||
| 2756 | /** |
||
| 2757 | * Test for the createContent() method. |
||
| 2758 | * |
||
| 2759 | * @covers \eZ\Publish\Core\Repository\ContentService::getLanguageCodesForCreate |
||
| 2760 | * @covers \eZ\Publish\Core\Repository\ContentService::mapFieldsForCreate |
||
| 2761 | * @covers \eZ\Publish\Core\Repository\ContentService::getDefaultObjectStates |
||
| 2762 | * @covers \eZ\Publish\Core\Repository\ContentService::createContent |
||
| 2763 | * @dataProvider providerForTestCreateContentThrowsContentValidationExceptionTranslation |
||
| 2764 | */ |
||
| 2765 | public function testCreateContentWithRollback() |
||
| 2766 | { |
||
| 2767 | $this->expectException(\Exception::class); |
||
| 2768 | $this->expectExceptionMessage('Store failed'); |
||
| 2769 | |||
| 2770 | $fieldDefinitions = [ |
||
| 2771 | new FieldDefinition( |
||
| 2772 | [ |
||
| 2773 | 'id' => 'fieldDefinitionId', |
||
| 2774 | 'fieldTypeIdentifier' => 'fieldTypeIdentifier', |
||
| 2775 | 'isTranslatable' => false, |
||
| 2776 | 'identifier' => 'identifier', |
||
| 2777 | 'isRequired' => false, |
||
| 2778 | 'defaultValue' => 'defaultValue', |
||
| 2779 | ] |
||
| 2780 | ), |
||
| 2781 | ]; |
||
| 2782 | |||
| 2783 | // Setup a simple case that will pass |
||
| 2784 | $contentCreateStruct = $this->assertForTestCreateContentNonRedundantFieldSet( |
||
| 2785 | 'eng-US', |
||
| 2786 | [], |
||
| 2787 | [], |
||
| 2788 | $fieldDefinitions, |
||
| 2789 | [], |
||
| 2790 | false, |
||
| 2791 | // Do not execute test |
||
| 2792 | false |
||
| 2793 | ); |
||
| 2794 | |||
| 2795 | $repositoryMock = $this->getRepositoryMock(); |
||
| 2796 | $repositoryMock->expects($this->never())->method('commit'); |
||
| 2797 | $repositoryMock->expects($this->once())->method('rollback'); |
||
| 2798 | |||
| 2799 | /** @var \PHPUnit\Framework\MockObject\MockObject $contentHandlerMock */ |
||
| 2800 | $contentHandlerMock = $this->getPersistenceMock()->contentHandler(); |
||
| 2801 | $contentHandlerMock->expects($this->once()) |
||
| 2802 | ->method('create') |
||
| 2803 | ->with($this->anything()) |
||
| 2804 | ->will($this->throwException(new \Exception('Store failed'))); |
||
| 2809 | |||
| 2810 | public function providerForTestUpdateContentThrowsBadStateException() |
||
| 2817 | |||
| 2818 | /** |
||
| 2819 | * Test for the updateContent() method. |
||
| 2820 | * |
||
| 2821 | * @covers \eZ\Publish\Core\Repository\ContentService::updateContent |
||
| 2822 | * @dataProvider providerForTestUpdateContentThrowsBadStateException |
||
| 2823 | */ |
||
| 2824 | public function testUpdateContentThrowsBadStateException($status) |
||
| 2856 | |||
| 2857 | /** |
||
| 2858 | * Test for the updateContent() method. |
||
| 2859 | * |
||
| 2860 | * @covers \eZ\Publish\Core\Repository\ContentService::updateContent |
||
| 2861 | */ |
||
| 2862 | public function testUpdateContentThrowsUnauthorizedException() |
||
| 2904 | |||
| 2905 | /** |
||
| 2906 | * @param string $initialLanguageCode |
||
| 2907 | * @param \eZ\Publish\API\Repository\Values\Content\Field[] $structFields |
||
| 2908 | * @param string[] $existingLanguages |
||
| 2909 | * |
||
| 2910 | * @return string[] |
||
| 2911 | */ |
||
| 2912 | protected function determineLanguageCodesForUpdate($initialLanguageCode, array $structFields, $existingLanguages) |
||
| 2929 | |||
| 2930 | /** |
||
| 2931 | * @param string $initialLanguageCode |
||
| 2932 | * @param \eZ\Publish\API\Repository\Values\Content\Field[] $structFields |
||
| 2933 | * @param string $mainLanguageCode |
||
| 2934 | * @param \eZ\Publish\API\Repository\Values\ContentType\FieldDefinition[] $fieldDefinitions |
||
| 2935 | * |
||
| 2936 | * @return array |
||
| 2937 | */ |
||
| 2938 | protected function mapStructFieldsForUpdate($initialLanguageCode, $structFields, $mainLanguageCode, $fieldDefinitions) |
||
| 2964 | |||
| 2965 | /** |
||
| 2966 | * Returns full, possibly redundant array of field values, indexed by field definition |
||
| 2967 | * identifier and language code. |
||
| 2968 | * |
||
| 2969 | * @param string $initialLanguageCode |
||
| 2970 | * @param \eZ\Publish\API\Repository\Values\Content\Field[] $structFields |
||
| 2971 | * @param \eZ\Publish\Core\Repository\Values\Content\Content $content |
||
| 2972 | * @param \eZ\Publish\API\Repository\Values\ContentType\FieldDefinition[] $fieldDefinitions |
||
| 2973 | * @param array $languageCodes |
||
| 2974 | * |
||
| 2975 | * @return array |
||
| 2976 | */ |
||
| 2977 | protected function determineValuesForUpdate( |
||
| 3023 | |||
| 3024 | protected function stubValues(array $fieldValues) |
||
| 3034 | |||
| 3035 | /** |
||
| 3036 | * Asserts that calling updateContent() with given API field set causes calling |
||
| 3037 | * Handler::updateContent() with given SPI field set. |
||
| 3038 | * |
||
| 3039 | * @param string $initialLanguageCode |
||
| 3040 | * @param \eZ\Publish\API\Repository\Values\Content\Field[] $structFields |
||
| 3041 | * @param \eZ\Publish\SPI\Persistence\Content\Field[] $spiFields |
||
| 3042 | * @param \eZ\Publish\API\Repository\Values\Content\Field[] $existingFields |
||
| 3043 | * @param \eZ\Publish\API\Repository\Values\ContentType\FieldDefinition[] $fieldDefinitions |
||
| 3044 | * @param bool $execute |
||
| 3045 | * |
||
| 3046 | * @return mixed |
||
| 3047 | */ |
||
| 3048 | protected function assertForTestUpdateContentNonRedundantFieldSet( |
||
| 3288 | |||
| 3289 | public function providerForTestUpdateContentNonRedundantFieldSet1() |
||
| 3341 | |||
| 3342 | /** |
||
| 3343 | * Test for the updateContent() method. |
||
| 3344 | * |
||
| 3345 | * Testing the simplest use case. |
||
| 3346 | * |
||
| 3347 | * @covers \eZ\Publish\Core\Repository\ContentService::getLanguageCodesForUpdate |
||
| 3348 | * @covers \eZ\Publish\Core\Repository\ContentService::mapFieldsForUpdate |
||
| 3349 | * @covers \eZ\Publish\Core\Repository\ContentService::updateContent |
||
| 3350 | * @dataProvider providerForTestUpdateContentNonRedundantFieldSet1 |
||
| 3351 | */ |
||
| 3352 | View Code Duplication | public function testUpdateContentNonRedundantFieldSet1($initialLanguageCode, $structFields, $spiFields) |
|
| 3386 | |||
| 3387 | public function providerForTestUpdateContentNonRedundantFieldSet2() |
||
| 3554 | |||
| 3555 | /** |
||
| 3556 | * Test for the updateContent() method. |
||
| 3557 | * |
||
| 3558 | * Testing with translatable field. |
||
| 3559 | * |
||
| 3560 | * @covers \eZ\Publish\Core\Repository\ContentService::getLanguageCodesForUpdate |
||
| 3561 | * @covers \eZ\Publish\Core\Repository\ContentService::mapFieldsForUpdate |
||
| 3562 | * @covers \eZ\Publish\Core\Repository\ContentService::updateContent |
||
| 3563 | * @dataProvider providerForTestUpdateContentNonRedundantFieldSet2 |
||
| 3564 | */ |
||
| 3565 | View Code Duplication | public function testUpdateContentNonRedundantFieldSet2($initialLanguageCode, $structFields, $spiFields) |
|
| 3599 | |||
| 3600 | public function providerForTestUpdateContentNonRedundantFieldSet3() |
||
| 3816 | |||
| 3817 | /** |
||
| 3818 | * Test for the updateContent() method. |
||
| 3819 | * |
||
| 3820 | * Testing with new language and untranslatable field. |
||
| 3821 | * |
||
| 3822 | * @covers \eZ\Publish\Core\Repository\ContentService::getLanguageCodesForUpdate |
||
| 3823 | * @covers \eZ\Publish\Core\Repository\ContentService::mapFieldsForUpdate |
||
| 3824 | * @covers \eZ\Publish\Core\Repository\ContentService::updateContent |
||
| 3825 | * @dataProvider providerForTestUpdateContentNonRedundantFieldSet3 |
||
| 3826 | */ |
||
| 3827 | public function testUpdateContentNonRedundantFieldSet3($initialLanguageCode, $structFields, $spiFields) |
||
| 3879 | |||
| 3880 | public function providerForTestUpdateContentNonRedundantFieldSet4() |
||
| 4121 | |||
| 4122 | /** |
||
| 4123 | * Test for the updateContent() method. |
||
| 4124 | * |
||
| 4125 | * Testing with empty values. |
||
| 4126 | * |
||
| 4127 | * @covers \eZ\Publish\Core\Repository\ContentService::getLanguageCodesForUpdate |
||
| 4128 | * @covers \eZ\Publish\Core\Repository\ContentService::mapFieldsForUpdate |
||
| 4129 | * @covers \eZ\Publish\Core\Repository\ContentService::updateContent |
||
| 4130 | * @dataProvider providerForTestUpdateContentNonRedundantFieldSet4 |
||
| 4131 | */ |
||
| 4132 | public function testUpdateContentNonRedundantFieldSet4($initialLanguageCode, $structFields, $spiFields) |
||
| 4184 | |||
| 4185 | /** |
||
| 4186 | * @todo add first field empty |
||
| 4187 | * |
||
| 4188 | * @return array |
||
| 4189 | */ |
||
| 4190 | public function providerForTestUpdateContentNonRedundantFieldSetComplex() |
||
| 4418 | |||
| 4419 | protected function fixturesForTestUpdateContentNonRedundantFieldSetComplex() |
||
| 4501 | |||
| 4502 | /** |
||
| 4503 | * Test for the updateContent() method. |
||
| 4504 | * |
||
| 4505 | * Testing more complex cases. |
||
| 4506 | * |
||
| 4507 | * @covers \eZ\Publish\Core\Repository\ContentService::getLanguageCodesForUpdate |
||
| 4508 | * @covers \eZ\Publish\Core\Repository\ContentService::mapFieldsForUpdate |
||
| 4509 | * @covers \eZ\Publish\Core\Repository\ContentService::updateContent |
||
| 4510 | * @dataProvider providerForTestUpdateContentNonRedundantFieldSetComplex |
||
| 4511 | */ |
||
| 4512 | public function testUpdateContentNonRedundantFieldSetComplex($initialLanguageCode, $structFields, $spiFields) |
||
| 4524 | |||
| 4525 | View Code Duplication | public function providerForTestUpdateContentWithInvalidLanguage() |
|
| 4554 | |||
| 4555 | /** |
||
| 4556 | * Test for the updateContent() method. |
||
| 4557 | * |
||
| 4558 | * @covers \eZ\Publish\Core\Repository\ContentService::getLanguageCodesForUpdate |
||
| 4559 | * @covers \eZ\Publish\Core\Repository\ContentService::updateContent |
||
| 4560 | * @dataProvider providerForTestUpdateContentWithInvalidLanguage |
||
| 4561 | */ |
||
| 4562 | public function testUpdateContentWithInvalidLanguage($initialLanguageCode, $structFields) |
||
| 4636 | |||
| 4637 | protected function assertForUpdateContentContentValidationException( |
||
| 4722 | |||
| 4723 | View Code Duplication | public function providerForTestUpdateContentThrowsContentValidationExceptionFieldDefinition() |
|
| 4740 | |||
| 4741 | /** |
||
| 4742 | * Test for the updateContent() method. |
||
| 4743 | * |
||
| 4744 | * @covers \eZ\Publish\Core\Repository\ContentService::getLanguageCodesForUpdate |
||
| 4745 | * @covers \eZ\Publish\Core\Repository\ContentService::mapFieldsForUpdate |
||
| 4746 | * @covers \eZ\Publish\Core\Repository\ContentService::updateContent |
||
| 4747 | * @dataProvider providerForTestUpdateContentThrowsContentValidationExceptionFieldDefinition |
||
| 4748 | */ |
||
| 4749 | public function testUpdateContentThrowsContentValidationExceptionFieldDefinition($initialLanguageCode, $structFields) |
||
| 4760 | |||
| 4761 | View Code Duplication | public function providerForTestUpdateContentThrowsContentValidationExceptionTranslation() |
|
| 4778 | |||
| 4779 | /** |
||
| 4780 | * Test for the updateContent() method. |
||
| 4781 | * |
||
| 4782 | * @covers \eZ\Publish\Core\Repository\ContentService::getLanguageCodesForUpdate |
||
| 4783 | * @covers \eZ\Publish\Core\Repository\ContentService::mapFieldsForUpdate |
||
| 4784 | * @covers \eZ\Publish\Core\Repository\ContentService::updateContent |
||
| 4785 | * @dataProvider providerForTestUpdateContentThrowsContentValidationExceptionTranslation |
||
| 4786 | */ |
||
| 4787 | View Code Duplication | public function testUpdateContentThrowsContentValidationExceptionTranslation($initialLanguageCode, $structFields) |
|
| 4811 | |||
| 4812 | public function assertForTestUpdateContentRequiredField( |
||
| 4934 | |||
| 4935 | View Code Duplication | public function providerForTestUpdateContentRequiredField() |
|
| 4954 | |||
| 4955 | /** |
||
| 4956 | * Test for the updateContent() method. |
||
| 4957 | * |
||
| 4958 | * @covers \eZ\Publish\Core\Repository\ContentService::getLanguageCodesForUpdate |
||
| 4959 | * @covers \eZ\Publish\Core\Repository\ContentService::mapFieldsForUpdate |
||
| 4960 | * @covers \eZ\Publish\Core\Repository\ContentService::updateContent |
||
| 4961 | * @dataProvider providerForTestUpdateContentRequiredField |
||
| 4962 | */ |
||
| 4963 | public function testUpdateContentRequiredField( |
||
| 5012 | |||
| 5013 | public function assertForTestUpdateContentThrowsContentFieldValidationException( |
||
| 5146 | |||
| 5147 | public function providerForTestUpdateContentThrowsContentFieldValidationException() |
||
| 5268 | |||
| 5269 | /** |
||
| 5270 | * Test for the updateContent() method. |
||
| 5271 | * |
||
| 5272 | * @covers \eZ\Publish\Core\Repository\ContentService::getLanguageCodesForUpdate |
||
| 5273 | * @covers \eZ\Publish\Core\Repository\ContentService::mapFieldsForUpdate |
||
| 5274 | * @covers \eZ\Publish\Core\Repository\ContentService::updateContent |
||
| 5275 | * @dataProvider providerForTestUpdateContentThrowsContentFieldValidationException |
||
| 5276 | */ |
||
| 5277 | View Code Duplication | public function testUpdateContentThrowsContentFieldValidationException($initialLanguageCode, $structFields, $spiField, $allFieldErrors) |
|
| 5298 | |||
| 5299 | /** |
||
| 5300 | * Test for the updateContent() method. |
||
| 5301 | * |
||
| 5302 | * @covers \eZ\Publish\Core\Repository\ContentService::getLanguageCodesForUpdate |
||
| 5303 | * @covers \eZ\Publish\Core\Repository\ContentService::mapFieldsForUpdate |
||
| 5304 | * @covers \eZ\Publish\Core\Repository\ContentService::updateContent |
||
| 5305 | */ |
||
| 5306 | public function testUpdateContentTransactionRollback() |
||
| 5363 | |||
| 5364 | /** |
||
| 5365 | * Test for the copyContent() method. |
||
| 5366 | * |
||
| 5367 | * @covers \eZ\Publish\Core\Repository\ContentService::copyContent |
||
| 5368 | */ |
||
| 5369 | public function testCopyContentThrowsUnauthorizedException() |
||
| 5412 | |||
| 5413 | /** |
||
| 5414 | * Test for the copyContent() method. |
||
| 5415 | * |
||
| 5416 | * @covers \eZ\Publish\Core\Repository\ContentService::copyContent |
||
| 5417 | * @covers \eZ\Publish\Core\Repository\ContentService::getDefaultObjectStates |
||
| 5418 | * @covers \eZ\Publish\Core\Repository\ContentService::internalPublishVersion |
||
| 5419 | */ |
||
| 5420 | public function testCopyContent() |
||
| 5538 | |||
| 5539 | /** |
||
| 5540 | * Test for the copyContent() method. |
||
| 5541 | * |
||
| 5542 | * @covers \eZ\Publish\Core\Repository\ContentService::copyContent |
||
| 5543 | * @covers \eZ\Publish\Core\Repository\ContentService::getDefaultObjectStates |
||
| 5544 | * @covers \eZ\Publish\Core\Repository\ContentService::internalPublishVersion |
||
| 5545 | */ |
||
| 5546 | public function testCopyContentWithVersionInfo() |
||
| 5662 | |||
| 5663 | /** |
||
| 5664 | * Test for the copyContent() method. |
||
| 5665 | * |
||
| 5666 | * @covers \eZ\Publish\Core\Repository\ContentService::copyContent |
||
| 5667 | * @covers \eZ\Publish\Core\Repository\ContentService::getDefaultObjectStates |
||
| 5668 | * @covers \eZ\Publish\Core\Repository\ContentService::internalPublishVersion |
||
| 5669 | */ |
||
| 5670 | public function testCopyContentWithRollback() |
||
| 5733 | |||
| 5734 | /** |
||
| 5735 | * Reusable method for setting exceptions on buildContentDomainObject usage. |
||
| 5736 | * |
||
| 5737 | * Plain usage as in when content type is loaded directly. |
||
| 5738 | * |
||
| 5739 | * @param \eZ\Publish\SPI\Persistence\Content $spiContent |
||
| 5740 | * @param array $translations |
||
| 5741 | * @param bool $useAlwaysAvailable |
||
| 5742 | * |
||
| 5743 | * @return \PHPUnit\Framework\MockObject\MockObject|\eZ\Publish\API\Repository\Values\Content\Content |
||
| 5744 | */ |
||
| 5745 | private function mockBuildContentDomainObject(SPIContent $spiContent, array $translations = null, bool $useAlwaysAvailable = null) |
||
| 5775 | |||
| 5776 | protected function mockGetDefaultObjectStates() |
||
| 5815 | |||
| 5816 | protected function mockSetDefaultObjectStates() |
||
| 5835 | |||
| 5836 | /** |
||
| 5837 | * @param int|null $publicationDate |
||
| 5838 | * @param int|null $modificationDate |
||
| 5839 | * |
||
| 5840 | * @return \eZ\Publish\API\Repository\Values\Content\Content |
||
| 5841 | */ |
||
| 5842 | protected function mockPublishVersion($publicationDate = null, $modificationDate = null) |
||
| 5917 | |||
| 5918 | /** |
||
| 5919 | * @param \eZ\Publish\API\Repository\Values\Content\Content $content |
||
| 5920 | */ |
||
| 5921 | protected function mockPublishUrlAliasesForContent(APIContent $content) |
||
| 5967 | |||
| 5968 | protected $domainMapperMock; |
||
| 5969 | |||
| 5970 | /** |
||
| 5971 | * @return \PHPUnit\Framework\MockObject\MockObject|\eZ\Publish\Core\Repository\Helper\DomainMapper |
||
| 5972 | */ |
||
| 5973 | protected function getDomainMapperMock() |
||
| 5981 | |||
| 5982 | protected $relationProcessorMock; |
||
| 5983 | |||
| 5984 | /** |
||
| 5985 | * @return \PHPUnit\Framework\MockObject\MockObject|\eZ\Publish\Core\Repository\Helper\RelationProcessor |
||
| 5986 | */ |
||
| 5987 | protected function getRelationProcessorMock() |
||
| 5995 | |||
| 5996 | protected $nameSchemaServiceMock; |
||
| 5997 | |||
| 5998 | /** |
||
| 5999 | * @return \PHPUnit\Framework\MockObject\MockObject|\eZ\Publish\Core\Repository\Helper\NameSchemaService |
||
| 6000 | */ |
||
| 6001 | protected function getNameSchemaServiceMock() |
||
| 6009 | |||
| 6010 | protected $contentTypeServiceMock; |
||
| 6011 | |||
| 6012 | /** |
||
| 6013 | * @return \PHPUnit\Framework\MockObject\MockObject|\eZ\Publish\API\Repository\ContentTypeService |
||
| 6014 | */ |
||
| 6015 | protected function getContentTypeServiceMock() |
||
| 6023 | |||
| 6024 | protected $locationServiceMock; |
||
| 6025 | |||
| 6026 | /** |
||
| 6027 | * @return \PHPUnit\Framework\MockObject\MockObject|\eZ\Publish\API\Repository\LocationService |
||
| 6028 | */ |
||
| 6029 | protected function getLocationServiceMock() |
||
| 6037 | |||
| 6038 | /** @var \eZ\Publish\Core\Repository\ContentService */ |
||
| 6039 | protected $partlyMockedContentService; |
||
| 6040 | |||
| 6041 | /** |
||
| 6042 | * Returns the content service to test with $methods mocked. |
||
| 6043 | * |
||
| 6044 | * Injected Repository comes from {@see getRepositoryMock()} and persistence handler from {@see getPersistenceMock()} |
||
| 6045 | * |
||
| 6046 | * @param string[] $methods |
||
| 6047 | * |
||
| 6048 | * @return \eZ\Publish\Core\Repository\ContentService|\PHPUnit\Framework\MockObject\MockObject |
||
| 6049 | */ |
||
| 6050 | protected function getPartlyMockedContentService(array $methods = null) |
||
| 6072 | |||
| 6073 | /** |
||
| 6074 | * @return \eZ\Publish\API\Repository\Repository|\PHPUnit\Framework\MockObject\MockObject |
||
| 6075 | */ |
||
| 6076 | protected function getRepositoryMock(): Repository |
||
| 6086 | } |
||
| 6087 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.