View source
<?php
namespace Drupal\Tests\url_embed\Functional;
use Drupal\editor\Entity\Editor;
use Drupal\filter\Entity\FilterFormat;
use Drupal\Tests\BrowserTestBase;
class ConvertUrlToEmbedFilterTest extends BrowserTestBase {
public static $modules = [
'url_embed',
'node',
'ckeditor',
];
public $defaultTheme = 'stark';
protected function setUp() {
parent::setUp();
$this
->drupalCreateContentType([
'type' => 'page',
'name' => 'Basic page',
]);
$format = FilterFormat::create([
'format' => 'custom_format',
'name' => 'Custom format',
'filters' => [
'url_embed_convert_links' => [
'status' => 1,
'settings' => [
'url_prefix' => '',
],
],
],
]);
$format
->save();
$editor_group = [
'name' => 'URL Embed',
'items' => [
'url',
],
];
$editor = Editor::create([
'format' => 'custom_format',
'editor' => 'ckeditor',
'settings' => [
'toolbar' => [
'rows' => [
[
$editor_group,
],
],
],
],
]);
$editor
->save();
$this->webUser = $this
->drupalCreateUser([
'access content',
'create page content',
'use text format custom_format',
]);
$this
->drupalLogin($this->webUser);
}
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);
$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>');
}
}