Skip to content
Snippets Groups Projects
Commit be8a63cd authored by Derek Wright's avatar Derek Wright
Browse files

Issue #3157085 by dww: Convert Functional/PathologicTest.php to a Kernel test

parent 3b9701e4
No related branches found
No related tags found
No related merge requests found
<?php <?php
namespace Drupal\Tests\pathologic\Functional; namespace Drupal\Tests\pathologic\Kernel;
use Drupal\Component\Utility\Html; use Drupal\Component\Utility\Html;
use Drupal\Core\Language\Language; use Drupal\Core\Language\Language;
use Drupal\Core\Url; use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase; use Drupal\KernelTests\KernelTestBase;
use Drupal\filter\Entity\FilterFormat; use Drupal\filter\Entity\FilterFormat;
use Drupal\pathologic\Plugin\Filter\FilterPathologic; use Drupal\pathologic\Plugin\Filter\FilterPathologic;
...@@ -14,31 +14,43 @@ use Drupal\pathologic\Plugin\Filter\FilterPathologic; ...@@ -14,31 +14,43 @@ use Drupal\pathologic\Plugin\Filter\FilterPathologic;
* *
* @group filter * @group filter
*/ */
class PathologicTest extends BrowserTestBase { class PathologicTest extends KernelTestBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected static $modules = ['filter', 'pathologic', 'pathologic_test']; protected static $modules = ['system', 'filter', 'pathologic', 'pathologic_test'];
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected $defaultTheme = 'stark'; protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
public function setUp(): void {
parent::setUp();
$this->installConfig(['system']);
// Install the global config so that the filter works when it's checking for
// a valid scheme, etc.
$this->installConfig(['pathologic']);
}
function testPathologic() { public function testPathologic() {
global $script_path; global $script_path;
// Start by testing our function to build protocol-relative URLs // Start by testing our function to build protocol-relative URLs
$this->assertEquals( $this->assertEquals(
_pathologic_url_to_protocol_relative('http://example.com/foo/bar'),
'//example.com/foo/bar', '//example.com/foo/bar',
_pathologic_url_to_protocol_relative('http://example.com/foo/bar'),
t('Protocol-relative URL creation with http:// URL') t('Protocol-relative URL creation with http:// URL')
); );
$this->assertEquals( $this->assertEquals(
_pathologic_url_to_protocol_relative('https://example.org/baz'),
'//example.org/baz', '//example.org/baz',
_pathologic_url_to_protocol_relative('https://example.org/baz'),
t('Protocol-relative URL creation with https:// URL') t('Protocol-relative URL creation with https:// URL')
); );
...@@ -84,7 +96,7 @@ class PathologicTest extends BrowserTestBase { ...@@ -84,7 +96,7 @@ class PathologicTest extends BrowserTestBase {
]; ];
foreach (['full', 'proto-rel', 'path'] as $protocol_style) { foreach (['full', 'proto-rel', 'path'] as $protocol_style) {
$format_id = _pathologic_build_format(['settings_source' => 'local', 'local_settings' => ['protocol_style' => $protocol_style]]); $format_id = $this->buildFormat(['settings_source' => 'local', 'local_settings' => ['protocol_style' => $protocol_style]]);
$paths = []; $paths = [];
foreach ($test_paths as $path => $args) { foreach ($test_paths as $path => $args) {
$args['opts']['absolute'] = $protocol_style !== 'path'; $args['opts']['absolute'] = $protocol_style !== 'path';
...@@ -99,80 +111,80 @@ class PathologicTest extends BrowserTestBase { ...@@ -99,80 +111,80 @@ class PathologicTest extends BrowserTestBase {
]; ];
$this->assertEquals( $this->assertEquals(
check_markup('<a href="foo"><img src="foo/bar" /></a>', $format_id),
'<a href="' . $paths['foo'] . '"><img src="' . $paths['foo/bar'] . '" /></a>', '<a href="' . $paths['foo'] . '"><img src="' . $paths['foo/bar'] . '" /></a>',
check_markup('<a href="foo"><img src="foo/bar" /></a>', $format_id),
t('Simple paths. Clean URLs: @clean; protocol style: @ps.', $t10ns) t('Simple paths. Clean URLs: @clean; protocol style: @ps.', $t10ns)
); );
$this->assertEquals( $this->assertEquals(
check_markup('<a href="index.php?q=foo"></a><a href="index.php?q=foo/bar&baz=qux"></a>', $format_id),
'<a href="' . $paths['foo'] . '"></a><a href="' . $paths['foo/bar?baz=qux'] . '"></a>', '<a href="' . $paths['foo'] . '"></a><a href="' . $paths['foo/bar?baz=qux'] . '"></a>',
check_markup('<a href="index.php?q=foo"></a><a href="index.php?q=foo/bar&baz=qux"></a>', $format_id),
t('D7 and earlier-style non-clean URLs. Clean URLs: @clean; protocol style: @ps.', $t10ns) t('D7 and earlier-style non-clean URLs. Clean URLs: @clean; protocol style: @ps.', $t10ns)
); );
$this->assertEquals( $this->assertEquals(
check_markup('<a href="index.php/foo"></a><a href="index.php/foo/bar?baz=qux"></a>', $format_id),
'<a href="' . $paths['foo'] . '"></a><a href="' . $paths['foo/bar?baz=qux'] . '"></a>', '<a href="' . $paths['foo'] . '"></a><a href="' . $paths['foo/bar?baz=qux'] . '"></a>',
check_markup('<a href="index.php/foo"></a><a href="index.php/foo/bar?baz=qux"></a>', $format_id),
t('D8-style non-clean URLs. Clean URLs: @clean; protocol style: @ps.', $t10ns) t('D8-style non-clean URLs. Clean URLs: @clean; protocol style: @ps.', $t10ns)
); );
$this->assertEquals( $this->assertEquals(
check_markup('<form action="foo/bar?baz"><IMG LONGDESC="foo/bar?baz=qux" /></a>', $format_id),
'<form action="' . $paths['foo/bar?baz'] . '"><IMG LONGDESC="' . $paths['foo/bar?baz=qux'] . '" /></a>', '<form action="' . $paths['foo/bar?baz'] . '"><IMG LONGDESC="' . $paths['foo/bar?baz=qux'] . '" /></a>',
check_markup('<form action="foo/bar?baz"><IMG LONGDESC="foo/bar?baz=qux" /></a>', $format_id),
t('Paths with query string. Clean URLs: @clean; protocol style: @ps.', $t10ns) t('Paths with query string. Clean URLs: @clean; protocol style: @ps.', $t10ns)
); );
$this->assertEquals( $this->assertEquals(
check_markup('<a href="foo/bar#baz">', $format_id),
'<a href="' . $paths['foo/bar#baz'] . '">', '<a href="' . $paths['foo/bar#baz'] . '">',
check_markup('<a href="foo/bar#baz">', $format_id),
t('Path with fragment. Clean URLs: @clean; protocol style: @ps.', $t10ns) t('Path with fragment. Clean URLs: @clean; protocol style: @ps.', $t10ns)
); );
$this->assertEquals( $this->assertEquals(
check_markup('<a href="#foo">', $format_id),
'<a href="#foo">', '<a href="#foo">',
check_markup('<a href="#foo">', $format_id),
t('Fragment-only href. Clean URLs: @clean; protocol style: @ps.', $t10ns) t('Fragment-only href. Clean URLs: @clean; protocol style: @ps.', $t10ns)
); );
// @see https://drupal.org/node/2208223 // @see https://drupal.org/node/2208223
$this->assertEquals( $this->assertEquals(
check_markup('<a href="#">', $format_id),
'<a href="#">', '<a href="#">',
check_markup('<a href="#">', $format_id),
t('Hash-only href. Clean URLs: @clean; protocol style: @ps.', $t10ns) t('Hash-only href. Clean URLs: @clean; protocol style: @ps.', $t10ns)
); );
$this->assertEquals( $this->assertEquals(
check_markup('<a href="foo/bar?baz=qux&amp;quux=quuux#quuuux">', $format_id),
'<a href="' . $paths['foo/bar?baz=qux&amp;quux=quuux#quuuux'] . '">', '<a href="' . $paths['foo/bar?baz=qux&amp;quux=quuux#quuuux'] . '">',
check_markup('<a href="foo/bar?baz=qux&amp;quux=quuux#quuuux">', $format_id),
t('Path with query string and fragment. Clean URLs: @clean; protocol style: @ps.', $t10ns) t('Path with query string and fragment. Clean URLs: @clean; protocol style: @ps.', $t10ns)
); );
$this->assertEquals( $this->assertEquals(
check_markup('<a href="foo%20bar?baz=qux%26quux">', $format_id),
'<a href="' . $paths['foo%20bar?baz=qux%26quux'] . '">', '<a href="' . $paths['foo%20bar?baz=qux%26quux'] . '">',
check_markup('<a href="foo%20bar?baz=qux%26quux">', $format_id),
t('Path with URL encoded parts. Clean URLs: @clean; protocol style: @ps.', $t10ns) t('Path with URL encoded parts. Clean URLs: @clean; protocol style: @ps.', $t10ns)
); );
$this->assertEquals( $this->assertEquals(
check_markup('<a href="/"></a>', $format_id),
'<a href="' . $paths['/'] . '"></a>', '<a href="' . $paths['/'] . '"></a>',
check_markup('<a href="/"></a>', $format_id),
t('Path with just slash. Clean URLs: @clean; protocol style: @ps', $t10ns) t('Path with just slash. Clean URLs: @clean; protocol style: @ps', $t10ns)
); );
} }
global $base_path; global $base_path;
$this->assertEquals( $this->assertEquals(
check_markup('<a href="' . $base_path . 'foo">bar</a>', $format_id),
'<a href="' . _pathologic_content_url('foo', ['absolute' => FALSE]) .'">bar</a>', '<a href="' . _pathologic_content_url('foo', ['absolute' => FALSE]) .'">bar</a>',
check_markup('<a href="' . $base_path . 'foo">bar</a>', $format_id),
t('Paths beginning with $base_path (like WYSIWYG editors like to make)') t('Paths beginning with $base_path (like WYSIWYG editors like to make)')
); );
global $base_url; global $base_url;
$this->assertEquals( $this->assertEquals(
check_markup('<a href="' . $base_url . '/foo">bar</a>', $format_id),
'<a href="' . _pathologic_content_url('foo', ['absolute' => FALSE]) .'">bar</a>', '<a href="' . _pathologic_content_url('foo', ['absolute' => FALSE]) .'">bar</a>',
check_markup('<a href="' . $base_url . '/foo">bar</a>', $format_id),
t('Paths beginning with $base_url') t('Paths beginning with $base_url')
); );
// @see http://drupal.org/node/1617944 // @see http://drupal.org/node/1617944
$this->assertEquals( $this->assertEquals(
check_markup('<a href="//example.com/foo">bar</a>', $format_id),
'<a href="//example.com/foo">bar</a>', '<a href="//example.com/foo">bar</a>',
check_markup('<a href="//example.com/foo">bar</a>', $format_id),
t('Off-site schemeless URLs (//example.com/foo) ignored') t('Off-site schemeless URLs (//example.com/foo) ignored')
); );
// Test internal: and all base paths // Test internal: and all base paths
$format_id = _pathologic_build_format([ $format_id = $this->buildFormat([
'settings_source' => 'local', 'settings_source' => 'local',
'local_settings' => [ 'local_settings' => [
'local_paths' => "http://example.com/qux\nhttp://example.org\n/bananas", 'local_paths' => "http://example.com/qux\nhttp://example.org\n/bananas",
...@@ -182,39 +194,39 @@ class PathologicTest extends BrowserTestBase { ...@@ -182,39 +194,39 @@ class PathologicTest extends BrowserTestBase {
// @see https://drupal.org/node/2030789 // @see https://drupal.org/node/2030789
$this->assertEquals( $this->assertEquals(
check_markup('<a href="//example.org/foo">bar</a>', $format_id),
'<a href="' . _pathologic_content_url('foo', ['absolute' => TRUE]) . '">bar</a>', '<a href="' . _pathologic_content_url('foo', ['absolute' => TRUE]) . '">bar</a>',
check_markup('<a href="//example.org/foo">bar</a>', $format_id),
t('On-site schemeless URLs processed') t('On-site schemeless URLs processed')
); );
$this->assertEquals( $this->assertEquals(
check_markup('<a href="internal:foo">', $format_id),
'<a href="' . _pathologic_content_url('foo', ['absolute' => TRUE]) . '">', '<a href="' . _pathologic_content_url('foo', ['absolute' => TRUE]) . '">',
check_markup('<a href="internal:foo">', $format_id),
t('Path Filter compatibility (internal:)') t('Path Filter compatibility (internal:)')
); );
$this->assertEquals( $this->assertEquals(
check_markup('<a href="files:image.jpeg">look</a>', $format_id),
'<a href="' . _pathologic_content_url(\Drupal::service('file_url_generator')->generateAbsoluteString( \Drupal::config('system.file')->get('default_scheme') . '://image.jpeg'), ['absolute' => TRUE]) . '">look</a>', '<a href="' . _pathologic_content_url(\Drupal::service('file_url_generator')->generateAbsoluteString( \Drupal::config('system.file')->get('default_scheme') . '://image.jpeg'), ['absolute' => TRUE]) . '">look</a>',
check_markup('<a href="files:image.jpeg">look</a>', $format_id),
t('Path Filter compatibility (files:)') t('Path Filter compatibility (files:)')
); );
$this->assertEquals( $this->assertEquals(
check_markup('<a href="http://example.com/qux/foo"><picture><source srcset="http://example.org/bar.jpeg" /><img src="http://example.org/bar.jpeg" longdesc="/bananas/baz" /></picture></a>', $format_id),
'<a href="' . _pathologic_content_url('foo', ['absolute' => TRUE]) . '"><picture><source srcset="' . _pathologic_content_url('bar.jpeg', ['absolute' => TRUE]) . '" /><img src="' . _pathologic_content_url('bar.jpeg', ['absolute' => TRUE]) . '" longdesc="' . _pathologic_content_url('baz', ['absolute' => TRUE]) . '" /></picture></a>', '<a href="' . _pathologic_content_url('foo', ['absolute' => TRUE]) . '"><picture><source srcset="' . _pathologic_content_url('bar.jpeg', ['absolute' => TRUE]) . '" /><img src="' . _pathologic_content_url('bar.jpeg', ['absolute' => TRUE]) . '" longdesc="' . _pathologic_content_url('baz', ['absolute' => TRUE]) . '" /></picture></a>',
check_markup('<a href="http://example.com/qux/foo"><picture><source srcset="http://example.org/bar.jpeg" /><img src="http://example.org/bar.jpeg" longdesc="/bananas/baz" /></picture></a>', $format_id),
t('"All base paths for this site" functionality') t('"All base paths for this site" functionality')
); );
$this->assertEquals( $this->assertEquals(
check_markup('<a href="webcal:foo">bar</a>', $format_id),
'<a href="webcal:foo">bar</a>', '<a href="webcal:foo">bar</a>',
check_markup('<a href="webcal:foo">bar</a>', $format_id),
t('URLs with likely protocols are ignored') t('URLs with likely protocols are ignored')
); );
// Test hook_pathologic_alter() implementation. // Test hook_pathologic_alter() implementation.
$this->assertEquals( $this->assertEquals(
check_markup('<a href="foo?test=add_foo_qpart">', $format_id),
'<a href="' . _pathologic_content_url('foo', ['absolute' => TRUE, 'query' => ['test' => 'add_foo_qpart', 'foo' => 'bar']]) . '">', '<a href="' . _pathologic_content_url('foo', ['absolute' => TRUE, 'query' => ['test' => 'add_foo_qpart', 'foo' => 'bar']]) . '">',
check_markup('<a href="foo?test=add_foo_qpart">', $format_id),
t('hook_pathologic_alter(): Alter $url_params') t('hook_pathologic_alter(): Alter $url_params')
); );
$this->assertEquals( $this->assertEquals(
check_markup('<a href="bar?test=use_original">', $format_id),
'<a href="bar?test=use_original">', '<a href="bar?test=use_original">',
check_markup('<a href="bar?test=use_original">', $format_id),
t('hook_pathologic_alter(): Passthrough with use_original option') t('hook_pathologic_alter(): Passthrough with use_original option')
); );
$this->assertEquals( $this->assertEquals(
...@@ -231,7 +243,7 @@ class PathologicTest extends BrowserTestBase { ...@@ -231,7 +243,7 @@ class PathologicTest extends BrowserTestBase {
t('Paths to files don\'t have ?q= when clean URLs are off') t('Paths to files don\'t have ?q= when clean URLs are off')
); );
$format_id = _pathologic_build_format([ $format_id = $this->buildFormat([
'settings_source' => 'global', 'settings_source' => 'global',
'local_settings' => [ 'local_settings' => [
'protocol_style' => 'rel', 'protocol_style' => 'rel',
...@@ -242,8 +254,8 @@ class PathologicTest extends BrowserTestBase { ...@@ -242,8 +254,8 @@ class PathologicTest extends BrowserTestBase {
->set('local_paths', 'http://example.com/') ->set('local_paths', 'http://example.com/')
->save(); ->save();
$this->assertEquals( $this->assertEquals(
check_markup('<img src="http://example.com/foo.jpeg" />', $format_id),
'<img src="' . _pathologic_url_to_protocol_relative(_pathologic_content_url('foo.jpeg', ['absolute' => TRUE])) . '" />', '<img src="' . _pathologic_url_to_protocol_relative(_pathologic_content_url('foo.jpeg', ['absolute' => TRUE])) . '" />',
check_markup('<img src="http://example.com/foo.jpeg" />', $format_id),
t('Use global settings when so configured on the format') t('Use global settings when so configured on the format')
); );
...@@ -259,6 +271,28 @@ class PathologicTest extends BrowserTestBase { ...@@ -259,6 +271,28 @@ class PathologicTest extends BrowserTestBase {
} }
/**
* Build a text format with Pathologic configured a certain way.
*
* @param array $settings
* An array of settings for the Pathologic instance on the format.
* @return string
* The randomly generated format machine name for the new format.
*/
protected function buildFormat(array $settings) {
$format_id = ($settings['local_settings']['protocol_style'] ?? 'unknown') . '_' . $this->randomMachineName(8);
$format = FilterFormat::create([
'format' => $format_id,
'name' => $format_id,
]);
$format->setFilterConfig('filter_pathologic', [
'status' => 1,
'settings' => $settings,
]);
$format->save();
return $format_id;
}
} }
/** /**
...@@ -290,26 +324,3 @@ function _pathologic_content_url($path, $options) { ...@@ -290,26 +324,3 @@ function _pathologic_content_url($path, $options) {
} }
return Html::escape(Url::fromUri(htmlspecialchars_decode($path), $options)->toString()); return Html::escape(Url::fromUri(htmlspecialchars_decode($path), $options)->toString());
} }
/**
* Build a text format with Pathologic configured a certain way.
*
* @param $settings
* An array of settings for the Pathologic instance on the format.
* @return string
* A format machine name (consisting of random characters) for the format.
*/
function _pathologic_build_format($settings) {
$format_id = \Drupal::service('password_generator')->generate();
$format = FilterFormat::create([
'format' => $format_id,
'name' => $format_id,
]);
$format->setFilterConfig('filter_pathologic', [
'status' => 1,
'settings' => $settings,
]);
$format->save();
return $format_id;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment