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 SearchEngineIndexingTest 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 SearchEngineIndexingTest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class SearchEngineIndexingTest extends BaseTest |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * EZP-26186: Make sure index is NOT deleted on removal of version draft (affected Solr & content index on Elastic). |
||
| 30 | */ |
||
| 31 | public function testDeleteVersion() |
||
| 55 | |||
| 56 | /** |
||
| 57 | * EZP-26186: Make sure affected child locations are deleted on content deletion (affected Solr & Elastic). |
||
| 58 | */ |
||
| 59 | View Code Duplication | public function testDeleteContent() |
|
| 78 | |||
| 79 | /** |
||
| 80 | * EZP-26186: Make sure index is deleted on removal of Users (affected Solr & Elastic). |
||
| 81 | */ |
||
| 82 | View Code Duplication | public function testDeleteUser() |
|
| 101 | |||
| 102 | /** |
||
| 103 | * EZP-26186: Make sure index is deleted on removal of UserGroups (affected Solr & Elastic). |
||
| 104 | */ |
||
| 105 | View Code Duplication | public function testDeleteUserGroup() |
|
| 124 | |||
| 125 | /* |
||
| 126 | * Test that a newly created user is available for search. |
||
| 127 | */ |
||
| 128 | public function testCreateUser() |
||
| 129 | { |
||
| 130 | $repository = $this->getRepository(); |
||
| 131 | $userService = $repository->getUserService(); |
||
| 132 | $searchService = $repository->getSearchService(); |
||
| 133 | |||
| 134 | // ID of the "Editors" user group |
||
| 135 | $editorsGroupId = 13; |
||
| 136 | $userCreate = $userService->newUserCreateStruct( |
||
| 137 | 'user', |
||
| 138 | '[email protected]', |
||
| 139 | 'secret', |
||
| 140 | 'eng-US' |
||
| 141 | ); |
||
| 142 | $userCreate->enabled = true; |
||
| 143 | $userCreate->setField('first_name', 'Example'); |
||
| 144 | $userCreate->setField('last_name', 'User'); |
||
| 145 | |||
| 146 | // Load parent group for the user |
||
| 147 | $group = $userService->loadUserGroup($editorsGroupId); |
||
| 148 | |||
| 149 | // Create a new user instance. |
||
| 150 | $user = $userService->createUser($userCreate, array($group)); |
||
| 151 | |||
| 152 | $this->refreshSearch($repository); |
||
| 153 | |||
| 154 | // Should be found |
||
| 155 | $criterion = new Criterion\ContentId($user->id); |
||
| 156 | $query = new Query(array('filter' => $criterion)); |
||
| 157 | $result = $searchService->findContentInfo($query); |
||
| 158 | $this->assertEquals(1, $result->totalCount); |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Test that a newly created user group is available for search. |
||
| 163 | */ |
||
| 164 | public function testCreateUserGroup() |
||
| 165 | { |
||
| 166 | $repository = $this->getRepository(); |
||
| 167 | $userService = $repository->getUserService(); |
||
| 168 | $searchService = $repository->getSearchService(); |
||
| 169 | |||
| 170 | $mainGroupId = $this->generateId('group', 4); |
||
| 171 | |||
| 172 | $parentUserGroup = $userService->loadUserGroup($mainGroupId); |
||
| 173 | $userGroupCreateStruct = $userService->newUserGroupCreateStruct('eng-GB'); |
||
| 174 | $userGroupCreateStruct->setField('name', 'Example Group'); |
||
| 175 | |||
| 176 | // Create a new user group |
||
| 177 | $userGroup = $userService->createUserGroup( |
||
| 178 | $userGroupCreateStruct, |
||
| 179 | $parentUserGroup |
||
| 180 | ); |
||
| 181 | |||
| 182 | $this->refreshSearch($repository); |
||
| 183 | |||
| 184 | // Should be found |
||
| 185 | $criterion = new Criterion\ContentId($userGroup->id); |
||
| 186 | $query = new Query(array('filter' => $criterion)); |
||
| 187 | $result = $searchService->findContentInfo($query); |
||
| 188 | $this->assertEquals(1, $result->totalCount); |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Test that a newly created Location is available for search. |
||
| 193 | */ |
||
| 194 | public function testCreateLocation() |
||
| 195 | { |
||
| 196 | $repository = $this->getRepository(); |
||
| 197 | $searchService = $repository->getSearchService(); |
||
| 198 | $membersLocation = $this->createNewTestLocation(); |
||
| 199 | |||
| 200 | $this->refreshSearch($repository); |
||
| 201 | |||
| 202 | // Found |
||
| 203 | $criterion = new Criterion\LocationId($membersLocation->id); |
||
| 204 | $query = new LocationQuery(array('filter' => $criterion)); |
||
| 205 | $result = $searchService->findLocations($query); |
||
| 206 | $this->assertEquals(1, $result->totalCount); |
||
| 207 | $this->assertEquals( |
||
| 208 | $membersLocation->id, |
||
| 209 | $result->searchHits[0]->valueObject->id |
||
| 210 | ); |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Test that hiding a Location makes it unavailable for search. |
||
| 215 | */ |
||
| 216 | public function testHideSubtree() |
||
| 217 | { |
||
| 218 | $repository = $this->getRepository(); |
||
| 219 | $searchService = $repository->getSearchService(); |
||
| 220 | |||
| 221 | // 5 is the ID of an existing location |
||
| 222 | $locationId = $this->generateId('location', 5); |
||
| 223 | $locationService = $repository->getLocationService(); |
||
| 224 | $location = $locationService->loadLocation($locationId); |
||
| 225 | $locationService->hideLocation($location); |
||
| 226 | $this->refreshSearch($repository); |
||
| 227 | |||
| 228 | // Check if parent location is hidden |
||
| 229 | $criterion = new Criterion\LocationId($locationId); |
||
| 230 | $query = new LocationQuery(array('filter' => $criterion)); |
||
| 231 | $result = $searchService->findLocations($query); |
||
| 232 | $this->assertEquals(1, $result->totalCount); |
||
| 233 | $this->assertTrue($result->searchHits[0]->valueObject->hidden); |
||
| 234 | |||
| 235 | // Check if children locations are invisible |
||
| 236 | $this->assertSubtreeInvisibleProperty($searchService, $locationId, true); |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Test that hiding and revealing a Location makes it available for search. |
||
| 241 | */ |
||
| 242 | public function testRevealSubtree() |
||
| 243 | { |
||
| 244 | $repository = $this->getRepository(); |
||
| 245 | $searchService = $repository->getSearchService(); |
||
| 246 | |||
| 247 | // 5 is the ID of an existing location |
||
| 248 | $locationId = $this->generateId('location', 5); |
||
| 249 | $locationService = $repository->getLocationService(); |
||
| 250 | $location = $locationService->loadLocation($locationId); |
||
| 251 | $locationService->hideLocation($location); |
||
| 252 | $this->refreshSearch($repository); |
||
| 253 | $locationService->unhideLocation($location); |
||
| 254 | $this->refreshSearch($repository); |
||
| 255 | |||
| 256 | // Check if parent location is not hidden |
||
| 257 | $criterion = new Criterion\LocationId($locationId); |
||
| 258 | $query = new LocationQuery(array('filter' => $criterion)); |
||
| 259 | $result = $searchService->findLocations($query); |
||
| 260 | $this->assertEquals(1, $result->totalCount); |
||
| 261 | $this->assertFalse($result->searchHits[0]->valueObject->hidden); |
||
| 262 | |||
| 263 | // Check if children locations are not invisible |
||
| 264 | $this->assertSubtreeInvisibleProperty($searchService, $locationId, false); |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Test that a copied subtree is available for search. |
||
| 269 | */ |
||
| 270 | public function testCopySubtree() |
||
| 271 | { |
||
| 272 | $repository = $this->getRepository(); |
||
| 273 | $locationService = $repository->getLocationService(); |
||
| 274 | $contentService = $repository->getContentService(); |
||
| 275 | $searchService = $repository->getSearchService(); |
||
| 276 | |||
| 277 | $rootLocationId = 2; |
||
| 278 | $membersContentId = 11; |
||
| 279 | $adminsContentId = 12; |
||
| 280 | $editorsContentId = 13; |
||
| 281 | $membersContentInfo = $contentService->loadContentInfo($membersContentId); |
||
| 282 | $adminsContentInfo = $contentService->loadContentInfo($adminsContentId); |
||
| 283 | $editorsContentInfo = $contentService->loadContentInfo($editorsContentId); |
||
| 284 | |||
| 285 | $locationCreateStruct = $locationService->newLocationCreateStruct($rootLocationId); |
||
| 286 | $membersLocation = $locationService->createLocation($membersContentInfo, $locationCreateStruct); |
||
| 287 | $editorsLocation = $locationService->createLocation($editorsContentInfo, $locationCreateStruct); |
||
| 288 | $adminsLocation = $locationService->createLocation( |
||
| 289 | $adminsContentInfo, |
||
| 290 | $locationService->newLocationCreateStruct($membersLocation->id) |
||
| 291 | ); |
||
| 292 | |||
| 293 | $copiedLocation = $locationService->copySubtree($adminsLocation, $editorsLocation); |
||
| 294 | $this->refreshSearch($repository); |
||
| 295 | |||
| 296 | // Found under Members |
||
| 297 | $criterion = new Criterion\ParentLocationId($membersLocation->id); |
||
| 298 | $query = new LocationQuery(array('filter' => $criterion)); |
||
| 299 | $result = $searchService->findLocations($query); |
||
| 300 | $this->assertEquals(1, $result->totalCount); |
||
| 301 | $this->assertEquals( |
||
| 302 | $adminsLocation->id, |
||
| 303 | $result->searchHits[0]->valueObject->id |
||
| 304 | ); |
||
| 305 | |||
| 306 | // Found under Editors |
||
| 307 | $criterion = new Criterion\ParentLocationId($editorsLocation->id); |
||
| 308 | $query = new LocationQuery(array('filter' => $criterion)); |
||
| 309 | $result = $searchService->findLocations($query); |
||
| 310 | $this->assertEquals(1, $result->totalCount); |
||
| 311 | $this->assertEquals( |
||
| 312 | $copiedLocation->id, |
||
| 313 | $result->searchHits[0]->valueObject->id |
||
| 314 | ); |
||
| 315 | } |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Test that moved subtree is available for search and found only under a specific parent Location. |
||
| 319 | */ |
||
| 320 | public function testMoveSubtree() |
||
| 321 | { |
||
| 322 | $repository = $this->getRepository(); |
||
| 323 | $locationService = $repository->getLocationService(); |
||
| 324 | $contentService = $repository->getContentService(); |
||
| 325 | $searchService = $repository->getSearchService(); |
||
| 326 | |||
| 327 | $rootLocationId = 2; |
||
| 328 | $membersContentId = 11; |
||
| 329 | $adminsContentId = 12; |
||
| 330 | $editorsContentId = 13; |
||
| 331 | $membersContentInfo = $contentService->loadContentInfo($membersContentId); |
||
| 332 | $adminsContentInfo = $contentService->loadContentInfo($adminsContentId); |
||
| 333 | $editorsContentInfo = $contentService->loadContentInfo($editorsContentId); |
||
| 334 | |||
| 335 | $locationCreateStruct = $locationService->newLocationCreateStruct($rootLocationId); |
||
| 336 | $membersLocation = $locationService->createLocation($membersContentInfo, $locationCreateStruct); |
||
| 337 | $editorsLocation = $locationService->createLocation($editorsContentInfo, $locationCreateStruct); |
||
| 338 | $adminsLocation = $locationService->createLocation( |
||
| 339 | $adminsContentInfo, |
||
| 340 | $locationService->newLocationCreateStruct($membersLocation->id) |
||
| 341 | ); |
||
| 342 | |||
| 343 | $this->refreshSearch($repository); |
||
| 344 | |||
| 345 | // Not found under Editors |
||
| 346 | $criterion = new Criterion\ParentLocationId($editorsLocation->id); |
||
| 347 | $query = new LocationQuery(array('filter' => $criterion)); |
||
| 348 | $result = $searchService->findLocations($query); |
||
| 349 | $this->assertEquals(0, $result->totalCount); |
||
| 350 | |||
| 351 | // Found under Members |
||
| 352 | $criterion = new Criterion\ParentLocationId($membersLocation->id); |
||
| 353 | $query = new LocationQuery(array('filter' => $criterion)); |
||
| 354 | $result = $searchService->findLocations($query); |
||
| 355 | $this->assertEquals(1, $result->totalCount); |
||
| 356 | $this->assertEquals( |
||
| 357 | $adminsLocation->id, |
||
| 358 | $result->searchHits[0]->valueObject->id |
||
| 359 | ); |
||
| 360 | |||
| 361 | $locationService->moveSubtree($adminsLocation, $editorsLocation); |
||
| 362 | $this->refreshSearch($repository); |
||
| 363 | |||
| 364 | // Found under Editors |
||
| 365 | $criterion = new Criterion\ParentLocationId($editorsLocation->id); |
||
| 366 | $query = new LocationQuery(array('filter' => $criterion)); |
||
| 367 | $result = $searchService->findLocations($query); |
||
| 368 | $this->assertEquals(1, $result->totalCount); |
||
| 369 | $this->assertEquals( |
||
| 370 | $adminsLocation->id, |
||
| 371 | $result->searchHits[0]->valueObject->id |
||
| 372 | ); |
||
| 373 | |||
| 374 | // Not found under Members |
||
| 375 | $criterion = new Criterion\ParentLocationId($membersLocation->id); |
||
| 376 | $query = new LocationQuery(array('filter' => $criterion)); |
||
| 377 | $result = $searchService->findLocations($query); |
||
| 378 | $this->assertEquals(0, $result->totalCount); |
||
| 379 | } |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Testing that content is indexed even when containing only fields with values |
||
| 383 | * considered to be empty by the search engine. |
||
| 384 | */ |
||
| 385 | public function testIndexContentWithNullField() |
||
| 386 | { |
||
| 387 | $repository = $this->getRepository(); |
||
| 388 | $contentService = $repository->getContentService(); |
||
| 389 | $contentTypeService = $repository->getContentTypeService(); |
||
| 390 | $searchService = $repository->getSearchService(); |
||
| 391 | |||
| 392 | $createStruct = $contentTypeService->newContentTypeCreateStruct('test-type'); |
||
| 393 | $createStruct->mainLanguageCode = 'eng-GB'; |
||
| 394 | $createStruct->names = array('eng-GB' => 'Test type'); |
||
| 395 | $createStruct->creatorId = 14; |
||
| 396 | $createStruct->creationDate = new DateTime(); |
||
| 397 | |||
| 398 | $translatableFieldCreate = $contentTypeService->newFieldDefinitionCreateStruct( |
||
| 399 | 'integer', |
||
| 400 | 'ezinteger' |
||
| 401 | ); |
||
| 402 | $translatableFieldCreate->names = array('eng-GB' => 'Simple translatable integer field'); |
||
| 403 | $translatableFieldCreate->fieldGroup = 'main'; |
||
| 404 | $translatableFieldCreate->position = 1; |
||
| 405 | $translatableFieldCreate->isTranslatable = true; |
||
| 406 | $translatableFieldCreate->isSearchable = true; |
||
| 407 | |||
| 408 | $createStruct->addFieldDefinition($translatableFieldCreate); |
||
| 409 | |||
| 410 | $contentGroup = $contentTypeService->loadContentTypeGroupByIdentifier('Content'); |
||
| 411 | $contentTypeDraft = $contentTypeService->createContentType( |
||
| 412 | $createStruct, |
||
| 413 | array($contentGroup) |
||
| 414 | ); |
||
| 415 | $contentTypeService->publishContentTypeDraft($contentTypeDraft); |
||
| 416 | $contentType = $contentTypeService->loadContentType($contentTypeDraft->id); |
||
| 417 | |||
| 418 | $createStruct = $contentService->newContentCreateStruct($contentType, 'eng-GB'); |
||
| 419 | $createStruct->alwaysAvailable = false; |
||
| 420 | $createStruct->mainLanguageCode = 'eng-GB'; |
||
| 421 | |||
| 422 | $draft = $contentService->createContent($createStruct); |
||
| 423 | $content = $contentService->publishVersion($draft->getVersionInfo()); |
||
| 424 | |||
| 425 | $this->refreshSearch($repository); |
||
| 426 | |||
| 427 | // Found |
||
| 428 | $criterion = new Criterion\ContentId($content->id); |
||
| 429 | $query = new Query(array('filter' => $criterion)); |
||
| 430 | $result = $searchService->findContent($query); |
||
| 431 | $this->assertEquals(1, $result->totalCount); |
||
| 432 | $this->assertEquals( |
||
| 433 | $content->id, |
||
| 434 | $result->searchHits[0]->valueObject->id |
||
| 435 | ); |
||
| 436 | } |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Test that updated Location is available for search. |
||
| 440 | */ |
||
| 441 | public function testUpdateLocation() |
||
| 442 | { |
||
| 443 | $repository = $this->getRepository(); |
||
| 444 | $locationService = $repository->getLocationService(); |
||
| 445 | $searchService = $repository->getSearchService(); |
||
| 446 | |||
| 447 | $rootLocationId = 2; |
||
| 448 | $locationToUpdate = $locationService->loadLocation($rootLocationId); |
||
| 449 | |||
| 450 | $criterion = new Criterion\LogicalAnd([ |
||
| 451 | new Criterion\LocationId($rootLocationId), |
||
| 452 | new Criterion\Location\Priority(Criterion\Operator::GT, 0), |
||
| 453 | ]); |
||
| 454 | |||
| 455 | $query = new LocationQuery(array('filter' => $criterion)); |
||
| 456 | $result = $searchService->findLocations($query); |
||
| 457 | |||
| 458 | $this->assertEquals(0, $result->totalCount); |
||
| 459 | |||
| 460 | $locationUpdateStruct = $locationService->newLocationUpdateStruct(); |
||
| 461 | $locationUpdateStruct->priority = 4; |
||
| 462 | $locationService->updateLocation($locationToUpdate, $locationUpdateStruct); |
||
| 463 | |||
| 464 | $this->refreshSearch($repository); |
||
| 465 | |||
| 466 | $result = $searchService->findLocations($query); |
||
| 467 | |||
| 468 | $this->assertEquals(1, $result->totalCount); |
||
| 469 | $this->assertEquals( |
||
| 470 | $locationToUpdate->id, |
||
| 471 | $result->searchHits[0]->valueObject->id |
||
| 472 | ); |
||
| 473 | } |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Testing that content will be deleted with all of its subitems but subitems with additional location will stay as |
||
| 477 | * they are. |
||
| 478 | */ |
||
| 479 | public function testDeleteLocation() |
||
| 480 | { |
||
| 481 | $repository = $this->getRepository(); |
||
| 482 | $locationService = $repository->getLocationService(); |
||
| 483 | |||
| 484 | $treeContainerContent = $this->createContentWithName('Tree Container', [2]); |
||
| 485 | $supposeBeDeletedSubItem = $this->createContentWithName( |
||
| 486 | 'Suppose to be deleted sub-item', |
||
| 487 | [$treeContainerContent->contentInfo->mainLocationId] |
||
| 488 | ); |
||
| 489 | $supposeSurviveSubItem = $this->createContentWithName( |
||
| 490 | 'Suppose to Survive Item', |
||
| 491 | [2, $treeContainerContent->contentInfo->mainLocationId] |
||
| 492 | ); |
||
| 493 | |||
| 494 | $treeContainerLocation = $locationService->loadLocation($treeContainerContent->contentInfo->mainLocationId); |
||
| 495 | |||
| 496 | $this->refreshSearch($repository); |
||
| 497 | |||
| 498 | $this->assertContentIdSearch($treeContainerContent->id, 1); |
||
| 499 | $this->assertContentIdSearch($supposeSurviveSubItem->id, 1); |
||
| 500 | $this->assertContentIdSearch($supposeBeDeletedSubItem->id, 1); |
||
| 501 | |||
| 502 | $locationService->deleteLocation($treeContainerLocation); |
||
| 503 | |||
| 504 | $this->refreshSearch($repository); |
||
| 505 | |||
| 506 | $this->assertContentIdSearch($supposeSurviveSubItem->id, 1); |
||
| 507 | $this->assertContentIdSearch($treeContainerContent->id, 0); |
||
| 508 | $this->assertContentIdSearch($supposeBeDeletedSubItem->id, 0); |
||
| 509 | } |
||
| 510 | |||
| 511 | /** |
||
| 512 | * Test content is available for search after being published. |
||
| 513 | */ |
||
| 514 | public function testPublishVersion() |
||
| 515 | { |
||
| 516 | $repository = $this->getRepository(); |
||
| 517 | $searchService = $repository->getSearchService(); |
||
| 518 | |||
| 519 | $publishedContent = $this->createContentWithName('publishedContent', [2]); |
||
| 520 | $this->refreshSearch($repository); |
||
| 521 | |||
| 522 | $criterion = new Criterion\FullText('publishedContent'); |
||
| 523 | $query = new Query(['filter' => $criterion]); |
||
| 524 | $result = $searchService->findContent($query); |
||
| 525 | |||
| 526 | $this->assertCount(1, $result->searchHits); |
||
| 527 | $this->assertEquals($publishedContent->contentInfo->id, $result->searchHits[0]->valueObject->contentInfo->id); |
||
| 528 | |||
| 529 | // Searching for children of locationId=2 should also hit this content |
||
| 530 | $criterion = new Criterion\ParentLocationId(2); |
||
| 531 | $query = new LocationQuery(array('filter' => $criterion)); |
||
| 532 | $result = $searchService->findLocations($query); |
||
| 533 | |||
| 534 | foreach ($result->searchHits as $searchHit) { |
||
| 535 | if ($searchHit->valueObject->contentInfo->id === $publishedContent->contentInfo->id) { |
||
| 536 | return; |
||
| 537 | } |
||
| 538 | } |
||
| 539 | $this->fail('Parent location sub-items do not contain published content'); |
||
| 540 | } |
||
| 541 | |||
| 542 | /** |
||
| 543 | * Test recovered content is available for search. |
||
| 544 | */ |
||
| 545 | public function testRecoverLocation() |
||
| 546 | { |
||
| 547 | $repository = $this->getRepository(); |
||
| 548 | $locationService = $repository->getLocationService(); |
||
| 549 | $trashService = $repository->getTrashService(); |
||
| 550 | $searchService = $repository->getSearchService(); |
||
| 551 | |||
| 552 | $publishedContent = $this->createContentWithName('recovery-test', [2]); |
||
| 553 | $location = $locationService->loadLocation($publishedContent->contentInfo->mainLocationId); |
||
| 554 | |||
| 555 | $trashService->trash($location); |
||
| 556 | $this->refreshSearch($repository); |
||
| 557 | |||
| 558 | $criterion = new Criterion\LocationId($location->id); |
||
| 559 | $query = new LocationQuery(['filter' => $criterion]); |
||
| 560 | $locations = $searchService->findLocations($query); |
||
| 561 | $this->assertEquals(0, $locations->totalCount); |
||
| 562 | |||
| 563 | $trashItem = $trashService->loadTrashItem($location->id); |
||
| 564 | $trashService->recover($trashItem); |
||
| 565 | $this->refreshSearch($repository); |
||
| 566 | |||
| 567 | $locations = $searchService->findLocations($query); |
||
| 568 | $this->assertEquals(0, $locations->totalCount); |
||
| 569 | $this->assertContentIdSearch($publishedContent->contentInfo->id, 1); |
||
| 570 | } |
||
| 571 | |||
| 572 | /** |
||
| 573 | * Test copied content is available for search. |
||
| 574 | */ |
||
| 575 | public function testCopyContent() |
||
| 576 | { |
||
| 577 | $repository = $this->getRepository(); |
||
| 578 | $searchService = $repository->getSearchService(); |
||
| 579 | $contentService = $repository->getContentService(); |
||
| 580 | $locationService = $repository->getLocationService(); |
||
| 581 | |||
| 582 | $publishedContent = $this->createContentWithName('copyTest', [2]); |
||
| 583 | $this->refreshSearch($repository); |
||
| 584 | $criterion = new Criterion\FullText('copyTest'); |
||
| 585 | $query = new Query(['filter' => $criterion]); |
||
| 586 | $result = $searchService->findContent($query); |
||
| 587 | $this->assertCount(1, $result->searchHits); |
||
| 588 | |||
| 589 | $copiedContent = $contentService->copyContent($publishedContent->contentInfo, $locationService->newLocationCreateStruct(2)); |
||
| 590 | $this->refreshSearch($repository); |
||
| 591 | $result = $searchService->findContent($query); |
||
| 592 | $this->assertCount(2, $result->searchHits); |
||
| 593 | |||
| 594 | $this->assertContentIdSearch($publishedContent->contentInfo->id, 1); |
||
| 595 | $this->assertContentIdSearch($copiedContent->contentInfo->id, 1); |
||
| 596 | } |
||
| 597 | |||
| 598 | /** |
||
| 599 | * Test that setting object content state to locked and then unlocked does not affect search index. |
||
| 600 | */ |
||
| 601 | public function testSetContentState() |
||
| 602 | { |
||
| 603 | $repository = $this->getRepository(); |
||
| 604 | $objectStateService = $repository->getObjectStateService(); |
||
| 605 | |||
| 606 | // get Object States |
||
| 607 | $stateNotLocked = $objectStateService->loadObjectState(1); |
||
| 608 | $stateLocked = $objectStateService->loadObjectState(2); |
||
| 609 | |||
| 610 | $publishedContent = $this->createContentWithName('setContentStateTest', [2]); |
||
| 611 | $objectStateService->setContentState($publishedContent->contentInfo, $stateLocked->getObjectStateGroup(), $stateLocked); |
||
| 612 | $this->refreshSearch($repository); |
||
| 613 | |||
| 614 | // Setting Content State to "locked" should not affect search index |
||
| 615 | $this->assertContentIdSearch($publishedContent->contentInfo->id, 1); |
||
| 616 | |||
| 617 | $objectStateService->setContentState($publishedContent->contentInfo, $stateNotLocked->getObjectStateGroup(), $stateNotLocked); |
||
| 618 | $this->refreshSearch($repository); |
||
| 619 | |||
| 620 | // Setting Content State back to "not locked" should not affect search index |
||
| 621 | $this->assertContentIdSearch($publishedContent->contentInfo->id, 1); |
||
| 622 | } |
||
| 623 | |||
| 624 | /** |
||
| 625 | * Check if FullText indexing works for special cases of text. |
||
| 626 | * |
||
| 627 | * @param string $text Content Item field value text (to be indexed) |
||
| 628 | * @param string $searchForText text based on which Content Item should be found |
||
| 629 | * @param array $ignoreForSetupFactories list of SetupFactories to be ignored |
||
| 630 | * @dataProvider getSpecialFullTextCases |
||
| 631 | */ |
||
| 632 | public function testIndexingSpecialFullTextCases($text, $searchForText, array $ignoreForSetupFactories = []) |
||
| 633 | { |
||
| 634 | // check if provided data should be ignored for the current Search Engine (via SetupFactory) |
||
| 635 | if (!empty($ignoreForSetupFactories) && in_array(get_class($this->getSetupFactory()), $ignoreForSetupFactories)) { |
||
| 636 | $this->markTestIncomplete(sprintf( |
||
| 637 | 'Handling FullText Searching for the phrase {%s} is incomplete for %s', |
||
| 638 | $searchForText, |
||
| 639 | get_class($this->getSetupFactory()) |
||
| 640 | )); |
||
| 641 | } |
||
| 642 | |||
| 643 | $repository = $this->getRepository(); |
||
| 644 | $searchService = $repository->getSearchService(); |
||
| 645 | |||
| 646 | $content = $this->createContentWithName($text, [2]); |
||
| 647 | $this->refreshSearch($repository); |
||
| 648 | |||
| 649 | $criterion = new Criterion\FullText($searchForText); |
||
| 650 | $query = new Query(['filter' => $criterion]); |
||
| 651 | $result = $searchService->findContent($query); |
||
| 652 | |||
| 653 | // for some cases there might be more than one hit, so check if proper one was found |
||
| 654 | foreach ($result->searchHits as $searchHit) { |
||
| 655 | if ($content->contentInfo->id === $searchHit->valueObject->versionInfo->contentInfo->id) { |
||
| 656 | return; |
||
| 657 | } |
||
| 658 | } |
||
| 659 | $this->fail('Failed to find required Content in search results'); |
||
| 660 | } |
||
| 661 | |||
| 662 | /** |
||
| 663 | * Data Provider for {@see testIndexingSpecialFullTextCases()} method. |
||
| 664 | * |
||
| 665 | * @return array |
||
| 666 | */ |
||
| 667 | public function getSpecialFullTextCases() |
||
| 668 | { |
||
| 669 | return [ |
||
| 670 | ['UPPERCASE TEXT', 'uppercase text'], |
||
| 671 | ['lowercase text', 'LOWERCASE TEXT'], |
||
| 672 | ['text-with-hyphens', 'text-with-hyphens'], |
||
| 673 | ['text containing spaces', 'text containing spaces'], |
||
| 674 | ['"quoted text"', 'quoted text'], |
||
| 675 | ['ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝ', 'àáâãäåçèéêëìíîïðñòóôõöøùúûüý'], |
||
| 676 | ['with boundary.', 'with boundary'], |
||
| 677 | ['Folder1.', 'Folder1.'], |
||
| 678 | ['whitespaces', " whitespaces \n \t "], |
||
| 679 | // @todo: Remove as soon as elastic is updated to later version not affected |
||
| 680 | ["it's", "it's", [LegacyElasticsearch::class]], |
||
| 681 | ['with_underscore', 'with_underscore'], |
||
| 682 | ]; |
||
| 683 | } |
||
| 684 | |||
| 685 | /** |
||
| 686 | * Test updating Content field value with empty value removes it from search index. |
||
| 687 | */ |
||
| 688 | public function testRemovedContentFieldValueIsNotFound() |
||
| 715 | |||
| 716 | /** |
||
| 717 | * Check if children locations are/are not ivisible. |
||
| 718 | * |
||
| 719 | * @param \eZ\Publish\API\Repository\SearchService $searchService |
||
| 720 | * @param int $parentLocationId parent location Id |
||
| 721 | * @param bool $expected expected value of {invisible} property in subtree |
||
| 722 | */ |
||
| 723 | private function assertSubtreeInvisibleProperty(SearchService $searchService, $parentLocationId, $expected) |
||
| 734 | |||
| 735 | /** |
||
| 736 | * Test that swapping locations affects properly Search Engine Index. |
||
| 737 | */ |
||
| 738 | public function testSwapLocation() |
||
| 767 | |||
| 768 | /** |
||
| 769 | * Test that updating Content metadata affects properly Search Engine Index. |
||
| 770 | */ |
||
| 771 | public function testUpdateContentMetadata() |
||
| 818 | |||
| 819 | /** |
||
| 820 | * Test that updating Content Draft metadata does not affect Search Engine Index. |
||
| 821 | */ |
||
| 822 | public function testUpdateContentDraftMetadataIsNotIndexed() |
||
| 843 | |||
| 844 | /** |
||
| 845 | * Test that assigning section to content object properly affects Search Engine Index. |
||
| 846 | */ |
||
| 847 | public function testAssignSection() |
||
| 864 | |||
| 865 | /** |
||
| 866 | * Will create if not exists a simple content type for test purposes with just one required field name. |
||
| 867 | * |
||
| 868 | * @return \eZ\Publish\API\Repository\Values\ContentType\ContentType |
||
| 869 | */ |
||
| 870 | protected function createTestContentType() |
||
| 902 | |||
| 903 | /** |
||
| 904 | * Will create and publish an content with a filed with a given content name in location provided into |
||
| 905 | * $parentLocationIdList. |
||
| 906 | * |
||
| 907 | * @param string $contentName |
||
| 908 | * @param array $parentLocationIdList |
||
| 909 | * |
||
| 910 | * @return \eZ\Publish\API\Repository\Values\Content\Content |
||
| 911 | */ |
||
| 912 | protected function createContentWithName($contentName, array $parentLocationIdList = array()) |
||
| 932 | |||
| 933 | /** |
||
| 934 | * Create and publish a content with filled name and description fields in location provided into |
||
| 935 | * $parentLocationIdList. |
||
| 936 | * |
||
| 937 | * @param string $contentName |
||
| 938 | * @param $contentDescription |
||
| 939 | * @param array $parentLocationIdList |
||
| 940 | * |
||
| 941 | * @return \eZ\Publish\API\Repository\Values\Content\Content |
||
| 942 | */ |
||
| 943 | protected function createContentWithNameAndDescription($contentName, $contentDescription, array $parentLocationIdList = []) |
||
| 966 | |||
| 967 | /** |
||
| 968 | * Asserts an content id if it exists still in the solr core. |
||
| 969 | * |
||
| 970 | * @param int $contentId |
||
| 971 | * @param int $expectedCount |
||
| 972 | */ |
||
| 973 | protected function assertContentIdSearch($contentId, $expectedCount) |
||
| 991 | |||
| 992 | /** |
||
| 993 | * Create & get new Location for tests. |
||
| 994 | * |
||
| 995 | * @return \eZ\Publish\API\Repository\Values\Content\Location |
||
| 996 | */ |
||
| 997 | protected function createNewTestLocation() |
||
| 1011 | } |
||
| 1012 |
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.