You are here

public function ConvertUrlToEmbedFilterTest::testFilter in URL Embed 8

Tests the url_embed_convert_links filter.

Ensures that iframes are getting rendered when valid urls are passed. Also tests situations when embed fails.

File

tests/src/Functional/ConvertUrlToEmbedFilterTest.php, line 81

Class

ConvertUrlToEmbedFilterTest
Tests the url_embed_convert_links filter.

Namespace

Drupal\Tests\url_embed\Functional

Code

public function testFilter() {
  $content = 'before https://twitter.com/drupal/status/735873777683320832 after';
  $settings = [];
  $settings['type'] = 'page';
  $settings['title'] = 'Test convert url to embed with sample Twitter url';
  $settings['body'] = [
    [
      'value' => $content,
      'format' => 'custom_format',
    ],
  ];
  $node = $this
    ->drupalCreateNode($settings);
  $this
    ->drupalGet('node/' . $node
    ->id());
  $this
    ->assertSession()
    ->responseContains('<drupal-url data-embed-url="https://twitter.com/drupal/status/735873777683320832"></drupal-url>');
  $this
    ->assertNoText(strip_tags($content), 'URL does not appear in the output when embed is successful.');
  $content = 'before /not-valid/url after';
  $settings = [];
  $settings['type'] = 'page';
  $settings['title'] = 'Test convert url to embed with non valid URL';
  $settings['body'] = [
    [
      'value' => $content,
      'format' => 'custom_format',
    ],
  ];
  $node = $this
    ->drupalCreateNode($settings);
  $this
    ->drupalGet('node/' . $node
    ->id());
  $this
    ->assertSession()
    ->responseContains($content);

  /** @var \Drupal\filter\FilterFormatInterface $format */
  $format = FilterFormat::load('custom_format');
  $configuration = $format
    ->filters('url_embed_convert_links')
    ->getConfiguration();
  $configuration['settings']['url_prefix'] = 'EMBED ';
  $format
    ->setFilterConfig('url_embed_convert_links', $configuration);
  $format
    ->save();
  $content = 'before https://twitter.com/drupal/status/735873777683320832 after';
  $settings = [];
  $settings['type'] = 'page';
  $settings['title'] = 'Test convert url to embed with sample Twitter url and no prefix';
  $settings['body'] = [
    [
      'value' => $content,
      'format' => 'custom_format',
    ],
  ];
  $node = $this
    ->drupalCreateNode($settings);
  $this
    ->drupalGet('node/' . $node
    ->id());
  $this
    ->assertSession()
    ->responseContains(strip_tags($content));
  $this
    ->assertSession()
    ->responseNotContains('<drupal-url data-embed-url="https://twitter.com/drupal/status/735873777683320832"></drupal-url>');
  $content = 'before EMBED https://twitter.com/drupal/status/735873777683320832 after';
  $settings = [];
  $settings['type'] = 'page';
  $settings['title'] = 'Test convert url to embed with sample Twitter url with the prefix';
  $settings['body'] = [
    [
      'value' => $content,
      'format' => 'custom_format',
    ],
  ];
  $node = $this
    ->drupalCreateNode($settings);
  $this
    ->drupalGet('node/' . $node
    ->id());
  $this
    ->assertSession()
    ->responseContains('<drupal-url data-embed-url="https://twitter.com/drupal/status/735873777683320832"></drupal-url>');
  $this
    ->assertNoText(strip_tags($content), 'URL does not appear in the output when embed is successful.');
  $content = 'before Embed https://twitter.com/drupal/status/735873777683320832 after';
  $settings = [];
  $settings['type'] = 'page';
  $settings['title'] = 'Test convert url to embed with sample Twitter url with wrong prefix';
  $settings['body'] = [
    [
      'value' => $content,
      'format' => 'custom_format',
    ],
  ];
  $node = $this
    ->drupalCreateNode($settings);
  $this
    ->drupalGet('node/' . $node
    ->id());
  $this
    ->assertSession()
    ->responseContains(strip_tags($content));
  $this
    ->assertSession()
    ->responseNotContains('<drupal-url data-embed-url="https://twitter.com/drupal/status/735873777683320832"></drupal-url>');
}