public function FieldLinkDetectorTest::testRunExtract in Lingotek Translation 3.8.x
Same name and namespace in other branches
- 4.0.x tests/src/Unit/Plugin/RelatedEntitiesDetector/FieldLinkDetectorTest.php \Drupal\Tests\lingotek\Unit\Plugin\RelatedEntitiesDetector\FieldLinkDetectorTest::testRunExtract()
- 3.5.x tests/src/Unit/Plugin/RelatedEntitiesDetector/FieldLinkDetectorTest.php \Drupal\Tests\lingotek\Unit\Plugin\RelatedEntitiesDetector\FieldLinkDetectorTest::testRunExtract()
- 3.6.x tests/src/Unit/Plugin/RelatedEntitiesDetector/FieldLinkDetectorTest.php \Drupal\Tests\lingotek\Unit\Plugin\RelatedEntitiesDetector\FieldLinkDetectorTest::testRunExtract()
- 3.7.x tests/src/Unit/Plugin/RelatedEntitiesDetector/FieldLinkDetectorTest.php \Drupal\Tests\lingotek\Unit\Plugin\RelatedEntitiesDetector\FieldLinkDetectorTest::testRunExtract()
@covers ::extract
File
- tests/
src/ Unit/ Plugin/ RelatedEntitiesDetector/ FieldLinkDetectorTest.php, line 156
Class
- FieldLinkDetectorTest
- Unit test for the html links entity detector plugin.
Namespace
Drupal\Tests\lingotek\Unit\Plugin\RelatedEntitiesDetectorCode
public function testRunExtract() {
$this->lingotekConfiguration
->expects($this
->exactly(3))
->method('isEnabled')
->withConsecutive([
'the_first_type',
'first_bundle',
], [
'entity_id',
'second_bundle',
], [
'entity_id',
'third_bundle',
])
->willReturnOnConsecutiveCalls(TRUE, TRUE, FALSE);
$linkFieldDefinition = $this
->createMock(BaseFieldDefinition::class);
$linkFieldDefinition
->expects($this
->once())
->method('getType')
->willReturn('link');
$linkFieldDefinition
->expects($this
->any())
->method('getName')
->willReturn('Link Field');
$target_entity_type = $this
->createMock(ContentEntityType::class);
$this->entityTypeManager
->expects($this
->exactly(3))
->method('getDefinition')
->withConsecutive([
'the_first_type',
], [
'entity_id',
])
->willReturn($target_entity_type);
$this->entityFieldManager
->expects($this
->any())
->method('getFieldDefinitions')
->willReturn([
'field_link' => $linkFieldDefinition,
]);
$entity = $this
->createMock(ContentEntityInterface::class);
$entity
->expects($this
->any())
->method('getEntityTypeId')
->willReturn($this->entityType
->getBundleEntityType());
$entity
->expects($this
->any())
->method('id')
->willReturn(1);
$entity
->expects($this
->any())
->method('uuid')
->willReturn('this-is-my-uuid');
$entity
->expects($this
->any())
->method('bundle')
->willReturn($this->entityType
->id());
$entity
->expects($this
->once())
->method('getUntranslated')
->willReturnSelf();
$firstEntity = $this
->createMock(ContentEntityInterface::class);
$firstEntity
->expects($this
->any())
->method('getEntityTypeId')
->willReturn('the_first_type');
$firstEntity
->expects($this
->any())
->method('id')
->willReturn(8);
$firstEntity
->expects($this
->any())
->method('uuid')
->willReturn('the-first-entity-uuid');
$firstEntity
->expects($this
->any())
->method('bundle')
->willReturn('first_bundle');
$firstEntity
->expects($this
->once())
->method('isTranslatable')
->willReturn(TRUE);
$firstEntity
->expects($this
->once())
->method('getUntranslated')
->willReturnSelf();
$secondEntity = $this
->createMock(ContentEntityInterface::class);
$secondEntity
->expects($this
->any())
->method('getEntityTypeId')
->willReturn('entity_id');
$secondEntity
->expects($this
->any())
->method('id')
->willReturn(2);
$secondEntity
->expects($this
->any())
->method('uuid')
->willReturn('the-second-entity-uuid');
$secondEntity
->expects($this
->any())
->method('bundle')
->willReturn('second_bundle');
$secondEntity
->expects($this
->once())
->method('isTranslatable')
->willReturn(TRUE);
$secondEntity
->expects($this
->once())
->method('getUntranslated')
->willReturnSelf();
$thirdEntity = $this
->createMock(ContentEntityInterface::class);
$thirdEntity
->expects($this
->any())
->method('getEntityTypeId')
->willReturn('entity_id');
$thirdEntity
->expects($this
->any())
->method('id')
->willReturn(5);
$thirdEntity
->expects($this
->any())
->method('uuid')
->willReturn('the-third-entity-uuid');
$thirdEntity
->expects($this
->any())
->method('bundle')
->willReturn('third_bundle');
$thirdEntity
->expects($this
->once())
->method('isTranslatable')
->willReturn(TRUE);
$thirdEntity
->expects($this
->never())
->method('getUntranslated')
->willReturnSelf();
$entityStorage = $this
->createMock(EntityStorageInterface::class);
$entityStorage
->expects($this
->exactly(6))
->method('load')
->withConsecutive([
8,
], [
8,
], [
2,
], [
2,
], [
5,
], [
5,
])
->willReturnOnConsecutiveCalls($firstEntity, $firstEntity, $secondEntity, $secondEntity, $thirdEntity, $thirdEntity);
$this->entityTypeManager
->expects($this
->any())
->method('getStorage')
->willReturn($entityStorage);
$linkItem = $this
->createMock(LinkItemInterface::class);
$linkItem
->expects($this
->exactly(4))
->method('getUrl')
->willReturnOnConsecutiveCalls(Url::fromRoute("entity.the_first_type.canonical", [
"the_first_type" => 8,
]), Url::fromUri('http://example.com'), Url::fromRoute("entity.entity_id.canonical", [
"entity_id" => 2,
]), Url::fromRoute("entity.entity_id.canonical", [
"entity_id" => 5,
]));
$data = [
$linkItem,
$linkItem,
$linkItem,
$linkItem,
];
$entity
->expects($this
->once())
->method('get')
->with('field_link')
->willReturn($data);
$entities = [];
$related = [];
$visited = [];
$this
->assertEmpty($entities);
$this->detector
->extract($entity, $entities, $related, 1, $visited);
// Entities from 2 different entity types.
$this
->assertCount(2, $entities);
// Total of three entities.
$this
->assertCount(2, $entities['entity_id']);
$this
->assertCount(1, $entities['the_first_type']);
$this
->assertEquals($entities['entity_id'][1], $entity);
$this
->assertEquals($entities['entity_id'][2], $secondEntity);
$this
->assertEquals($entities['the_first_type'][8], $firstEntity);
}