only in patch2: unchanged: --- /dev/null +++ b/tests/src/Functional/Plugin/Field/FieldFormatter/FeedsItemTargetEntityFormatterTest.php @@ -0,0 +1,74 @@ +setComponent('feeds_item', [ + 'type' => 'feeds_item_target_entity_view', + 'weight' => 1, + ]) + ->save(); + } + + /** + * Tests the feeds target entity view formatter. + * + * @covers \Drupal\feeds\Plugin\Field\FieldFormatter\FeedsItemTargetEntityFormatter::viewElements + */ + public function testFeedsItemTargetEntityFormatter($input, $expected) { + $feed = $this->getFeed(); + + // :TODO: We need to set predictable values for items imported, last import, + // next import on the feed. We used /csv/content.csv for source as + // FeedsItemFormatterTestBase sets that up but maybe we should define that + // separately here too to make this test more clear. Also I think we need to + // set a display mode on the feed item itself for this to work well. + + // Setup the article with test guid. + $settings['title'] = "Teenage Mutant Ninja Turtles"; + $article = $this->createNodeWithFeedsItem($feed, $settings); + $article->feeds_item = $feed; + + // Display the article and test we are getting correct output for target id. + $display = entity_get_display($article->getEntityTypeId(), $article->bundle(), 'default'); + $content = $display->build($article); + $rendered_content = \Drupal::service('renderer')->renderRoot($content); + + // Make sure all fields of the target feed item are rendering as expected. + $this->assertContains('
' . $feed->source . '
', (string) $rendered_content); + // :TODO: Figure out how to test the following fields displaying for the + // target feed. + // 1) Items imported: 10 + // 2) Last import: 3 months ago + // 3) Next import: Wed, 12/31/1969 - 15:59 + + } + + /** + * Provides the feed ids to check and expected results. + */ + public function providerTargetIds() { + return [ + 'empty target id' => ['', NULL], + 'existing target id' => ['1', '
1
'], + 'non existing target id' => ['123', NULL], + 'weird html string target id' => ['Skeletor!!!!', NULL], + ]; + } + +} only in patch2: unchanged: --- /dev/null +++ b/tests/src/Functional/Plugin/Field/FieldFormatter/FeedsItemTargetLabelFormatterTest.php @@ -0,0 +1,68 @@ +setComponent('feeds_item', [ + 'type' => 'feeds_item_target_id', + 'weight' => 1, + ]) + ->save(); + } + + /** + * Tests the feeds target id formatter. + * + * @covers \Drupal\feeds\Plugin\Field\FieldFormatter\FeedsItemTargetIdFormatter::viewElements + * + * @dataProvider providerTargetIds + */ + public function testFeedsItemTargetIdFormatter($input, $expected) { + $feed = $this->getFeed(); + + // Setup the article with test guid. + $settings['title'] = "He-Man and the Masters of the Universe"; + $article = $this->createNodeWithFeedsItem($feed, $settings); + $article->feeds_item->target_id = $input; + + // Display the article and test we are getting correct output for target id. + $display = entity_get_display($article->getEntityTypeId(), $article->bundle(), 'default'); + $content = $display->build($article); + $rendered_content = \Drupal::service('renderer')->renderRoot($content); + if ($expected) { + $this->assertContains($expected, (string) $rendered_content); + } + else { + // Make sure no field item is rendered with empty input. + $this->assertFeedsItemFieldNotDisplayed($rendered_content, $input); + } + } + + /** + * Provides the feed ids to check and expected results. + */ + public function providerTargetIds() { + return [ + 'empty target id' => ['', NULL], + 'existing target id' => ['1', '
1
'], + 'non existing target id' => ['123', NULL], + 'weird html string target id' => ['Skeletor!!!!', NULL], + ]; + } + +}