Skip to content
Snippets Groups Projects
Commit 1334b254 authored by Mark Fullmer's avatar Mark Fullmer Committed by Derek Wright
Browse files

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

- Use assertSame() not assertEquals()
- Lots of code style cleanup
- Move some procedural helper functions to protected methods of the test

(cherry picked from commit df6a3017)
parent a5978d5e
Branches
No related tags found
No related merge requests found
......@@ -3,11 +3,9 @@
namespace Drupal\Tests\pathologic\Kernel;
use Drupal\Component\Utility\Html;
use Drupal\Core\Language\Language;
use Drupal\Core\Url;
use Drupal\KernelTests\KernelTestBase;
use Drupal\filter\Entity\FilterFormat;
use Drupal\pathologic\Plugin\Filter\FilterPathologic;
/**
* Tests Pathologic functionality.
......@@ -19,13 +17,25 @@ class PathologicTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['system', 'filter', 'pathologic', 'pathologic_test'];
protected static $modules = [
'system',
'filter',
'pathologic',
'pathologic_test',
];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* The ID of a text format to be used in a test.
*
* @var formatId
*/
protected $formatId = '';
/**
* {@inheritdoc}
*/
......@@ -38,39 +48,41 @@ class PathologicTest extends KernelTestBase {
$this->installConfig(['pathologic']);
}
/**
* Test all variations of URLs.
*/
public function testPathologic() {
global $script_path;
// Start by testing our function to build protocol-relative URLs
$this->assertEquals(
// Start by testing our function to build protocol-relative URLs.
$this->assertSame(
'//example.com/foo/bar',
_pathologic_url_to_protocol_relative('http://example.com/foo/bar'),
t('Protocol-relative URL creation with http:// URL')
);
$this->assertEquals(
$this->assertSame(
'//example.org/baz',
_pathologic_url_to_protocol_relative('https://example.org/baz'),
t('Protocol-relative URL creation with https:// URL')
);
// Build some paths to check against
// Build some paths to check against.
$test_paths = [
'foo' => [
'path' => 'foo',
'opts' => []
'opts' => [],
],
'foo/bar' => [
'path' => 'foo/bar',
'opts' => []
'opts' => [],
],
'foo/bar?baz' => [
'path' => 'foo/bar',
'opts' => ['query' => ['baz' => NULL]]
'opts' => ['query' => ['baz' => NULL]],
],
'foo/bar?baz=qux' => [
'path' => 'foo/bar',
'opts' => ['query' => ['baz' => 'qux']]
'opts' => ['query' => ['baz' => 'qux']],
],
'foo/bar#baz' => [
'path' => 'foo/bar',
......@@ -96,11 +108,16 @@ class PathologicTest extends KernelTestBase {
];
foreach (['full', 'proto-rel', 'path'] as $protocol_style) {
$format_id = $this->buildFormat(['settings_source' => 'local', 'local_settings' => ['protocol_style' => $protocol_style]]);
$this->formatId = $this->buildFormat([
'settings_source' => 'local',
'local_settings' => [
'protocol_style' => $protocol_style,
],
]);
$paths = [];
foreach ($test_paths as $path => $args) {
$args['opts']['absolute'] = $protocol_style !== 'path';
$paths[$path] = _pathologic_content_url($args['path'], $args['opts']);
$paths[$path] = $this->pathologicContentUrl($args['path'], $args['opts']);
if ($protocol_style === 'proto-rel') {
$paths[$path] = _pathologic_url_to_protocol_relative($paths[$path]);
}
......@@ -110,81 +127,82 @@ class PathologicTest extends KernelTestBase {
'@ps' => $protocol_style,
];
$this->assertEquals(
$this->assertSame(
'<a href="' . $paths['foo'] . '"><img src="' . $paths['foo/bar'] . '" /></a>',
check_markup('<a href="foo"><img src="foo/bar" /></a>', $format_id),
$this->runFilter('<a href="foo"><img src="foo/bar" /></a>'),
t('Simple paths. Clean URLs: @clean; protocol style: @ps.', $t10ns)
);
$this->assertEquals(
$this->assertSame(
'<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),
$this->runFilter('<a href="index.php?q=foo"></a><a href="index.php?q=foo/bar&baz=qux"></a>'),
t('D7 and earlier-style non-clean URLs. Clean URLs: @clean; protocol style: @ps.', $t10ns)
);
$this->assertEquals(
$this->assertSame(
'<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),
$this->runFilter('<a href="index.php/foo"></a><a href="index.php/foo/bar?baz=qux"></a>'),
t('D8-style non-clean URLs. Clean URLs: @clean; protocol style: @ps.', $t10ns)
);
$this->assertEquals(
$this->assertSame(
'<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),
$this->runFilter('<form action="foo/bar?baz"><IMG LONGDESC="foo/bar?baz=qux" /></a>'),
t('Paths with query string. Clean URLs: @clean; protocol style: @ps.', $t10ns)
);
$this->assertEquals(
$this->assertSame(
'<a href="' . $paths['foo/bar#baz'] . '">',
check_markup('<a href="foo/bar#baz">', $format_id),
$this->runFilter('<a href="foo/bar#baz">'),
t('Path with fragment. Clean URLs: @clean; protocol style: @ps.', $t10ns)
);
$this->assertEquals(
$this->assertSame(
'<a href="#foo">',
check_markup('<a href="#foo">', $format_id),
$this->runFilter('<a href="#foo">'),
t('Fragment-only href. Clean URLs: @clean; protocol style: @ps.', $t10ns)
);
// @see https://drupal.org/node/2208223
$this->assertEquals(
$this->assertSame(
'<a href="#">',
check_markup('<a href="#">', $format_id),
$this->runFilter('<a href="#">'),
t('Hash-only href. Clean URLs: @clean; protocol style: @ps.', $t10ns)
);
$this->assertEquals(
$this->assertSame(
'<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),
$this->runFilter('<a href="foo/bar?baz=qux&amp;quux=quuux#quuuux">'),
t('Path with query string and fragment. Clean URLs: @clean; protocol style: @ps.', $t10ns)
);
$this->assertEquals(
$this->assertSame(
'<a href="' . $paths['foo%20bar?baz=qux%26quux'] . '">',
check_markup('<a href="foo%20bar?baz=qux%26quux">', $format_id),
$this->runFilter('<a href="foo%20bar?baz=qux%26quux">'),
t('Path with URL encoded parts. Clean URLs: @clean; protocol style: @ps.', $t10ns)
);
$this->assertEquals(
$this->assertSame(
'<a href="' . $paths['/'] . '"></a>',
check_markup('<a href="/"></a>', $format_id),
$this->runFilter('<a href="/"></a>'),
t('Path with just slash. Clean URLs: @clean; protocol style: @ps', $t10ns)
);
}
global $base_path;
$this->assertEquals(
'<a href="' . _pathologic_content_url('foo', ['absolute' => FALSE]) .'">bar</a>',
check_markup('<a href="' . $base_path . 'foo">bar</a>', $format_id),
$this->assertSame(
'<a href="' . $this->pathologicContentUrl('foo', ['absolute' => FALSE]) . '">bar</a>',
$this->runFilter('<a href="' . $base_path . 'foo">bar</a>'),
t('Paths beginning with $base_path (like WYSIWYG editors like to make)')
);
global $base_url;
$this->assertEquals(
'<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')
$this->assertSame(
'<a href="' . $this->pathologicContentUrl('foo', ['absolute' => FALSE]) . '">bar</a>',
$this->runFilter('<a href="' . $base_url . '/foo">bar</a>'),
t('Paths beginning with $base_url using @format_id', ['@format_id' => $this->formatId])
);
// @see http://drupal.org/node/1617944
$this->assertEquals(
$this->assertSame(
'<a href="//example.com/foo">bar</a>',
check_markup('<a href="//example.com/foo">bar</a>', $format_id),
$this->runFilter('<a href="//example.com/foo">bar</a>'),
t('Off-site schemeless URLs (//example.com/foo) ignored')
);
// Test internal: and all base paths
$format_id = $this->buildFormat([
// Test internal: and all base paths.
$this->formatId = $this->buildFormat([
'settings_source' => 'local',
'local_settings' => [
'local_paths' => "http://example.com/qux\nhttp://example.org\n/bananas",
......@@ -193,57 +211,63 @@ class PathologicTest extends KernelTestBase {
]);
// @see https://drupal.org/node/2030789
$this->assertEquals(
'<a href="' . _pathologic_content_url('foo', ['absolute' => TRUE]) . '">bar</a>',
check_markup('<a href="//example.org/foo">bar</a>', $format_id),
$this->assertSame(
'<a href="' . $this->pathologicContentUrl('foo', ['absolute' => TRUE]) . '">bar</a>',
$this->runFilter('<a href="//example.org/foo">bar</a>'),
t('On-site schemeless URLs processed')
);
$this->assertEquals(
'<a href="' . _pathologic_content_url('foo', ['absolute' => TRUE]) . '">',
check_markup('<a href="internal:foo">', $format_id),
$this->assertSame(
'<a href="' . $this->pathologicContentUrl('foo', ['absolute' => TRUE]) . '">',
$this->runFilter('<a href="internal:foo">'),
t('Path Filter compatibility (internal:)')
);
$this->assertEquals(
'<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),
$this->assertSame(
'<a href="' . $this->pathologicContentUrl(\Drupal::service('file_url_generator')->generateAbsoluteString(\Drupal::config('system.file')->get('default_scheme') . '://image.jpeg'), []) . '">look</a>',
urldecode($this->runFilter('<a href="files:image.jpeg">look</a>')),
t('Path Filter compatibility (files:)')
);
$this->assertEquals(
'<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),
$this->assertSame(
'<a href="' . $this->pathologicContentUrl('foo', ['absolute' => TRUE]) . '"><picture><source srcset="' . $this->pathologicContentUrl('bar.jpeg', ['absolute' => TRUE]) . '" /><img src="' . $this->pathologicContentUrl('bar.jpeg', ['absolute' => TRUE]) . '" longdesc="' . $this->pathologicContentUrl('baz', ['absolute' => TRUE]) . '" /></picture></a>',
$this->runFilter('<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>'),
t('"All base paths for this site" functionality')
);
$this->assertEquals(
$this->assertSame(
'<a href="webcal:foo">bar</a>',
check_markup('<a href="webcal:foo">bar</a>', $format_id),
$this->runFilter('<a href="webcal:foo">bar</a>'),
t('URLs with likely protocols are ignored')
);
// Test hook_pathologic_alter() implementation.
$this->assertEquals(
'<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),
$this->assertSame(
'<a href="' . $this->pathologicContentUrl('foo', [
'absolute' => TRUE,
'query' => [
'test' => 'add_foo_qpart',
'foo' => 'bar',
],
]) . '">',
$this->runFilter('<a href="foo?test=add_foo_qpart">'),
t('hook_pathologic_alter(): Alter $url_params')
);
$this->assertEquals(
$this->assertSame(
'<a href="bar?test=use_original">',
check_markup('<a href="bar?test=use_original">', $format_id),
$this->runFilter('<a href="bar?test=use_original">'),
t('hook_pathologic_alter(): Passthrough with use_original option')
);
$this->assertEquals(
$this->assertSame(
'<a href="http://cdn.example.com/bar?test=external">',
check_markup('<a href="bar?test=external">', $format_id),
$this->runFilter('<a href="bar?test=external">'),
);
// Test paths to existing files when clean URLs are disabled.
// @see http://drupal.org/node/1672430
$script_path = '';
$filtered_tag = check_markup('<img src="misc/druplicon.png" />', $format_id);
$filtered_tag = $this->runFilter('<img src="misc/druplicon.png" />');
$this->assertTrue(
strpos($filtered_tag, 'q=') === FALSE,
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 = $this->buildFormat([
$this->formatId = $this->buildFormat([
'settings_source' => 'global',
'local_settings' => [
'protocol_style' => 'rel',
......@@ -253,9 +277,9 @@ class PathologicTest extends KernelTestBase {
->set('protocol_style', 'proto-rel')
->set('local_paths', 'http://example.com/')
->save();
$this->assertEquals(
'<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),
$this->assertSame(
'<img src="' . _pathologic_url_to_protocol_relative($this->pathologicContentUrl('foo.jpeg', ['absolute' => TRUE])) . '" />',
$this->runFilter('<img src="http://example.com/foo.jpeg" />'),
t('Use global settings when so configured on the format')
);
......@@ -263,7 +287,7 @@ class PathologicTest extends KernelTestBase {
// @see https://www.drupal.org/node/2602312
$original = '<a href="/Epic:failure">foo</a>';
try {
$filtered = check_markup($original, $format_id);
$this->runFilter($original);
}
catch (\Exception $e) {
$this->fail('Fails miserably when \Drupal\Core\Url::fromUri() throws exception');
......@@ -276,6 +300,7 @@ class PathologicTest extends KernelTestBase {
*
* @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.
*/
......@@ -293,12 +318,23 @@ class PathologicTest extends KernelTestBase {
return $format_id;
}
/**
* Cleaner syntax for text format filter running.
*
* @param string $markup
* Raw markup to be processed.
*
* @return string
* A string of text-format-filtered markup.
*/
protected function runFilter($markup) {
return check_markup($markup, $this->formatId)->__toString();
}
/**
* Wrapper around url() which does HTML entity decoding and encoding.
*
* Since Pathologic works with paths in content, it needs to decode paths which
* Since Pathologic works with paths in content, it needs to decode paths that
* have been HTML-encoded, and re-encode them when done. This is a wrapper
* around url() which does the same thing so that we can expect the results
* from it and from Pathologic to still match in our tests.
......@@ -307,15 +343,14 @@ class PathologicTest extends KernelTestBase {
* @see http://drupal.org/node/1672932
* @see http://www.w3.org/TR/xhtml1/guidelines.html#C_12
*/
function _pathologic_content_url($path, $options) {
// If we should pretend this is a path to a file, make url() behave like clean
protected function pathologicContentUrl($path, $options) {
// If we pretend this is a path to a file, make url() behave like clean
// URLs are enabled.
// @see _pathologic_replace()
// @see http://drupal.org/node/1672430
if (!empty($options['is_file'])) {
$options['script_path'] = '';
}
if (parse_url($path, PHP_URL_SCHEME) === NULL) {
if ($path == '<front>') {
return Html::escape(Url::fromRoute('<front>', [], $options)->toString());
......@@ -324,3 +359,5 @@ function _pathologic_content_url($path, $options) {
}
return Html::escape(Url::fromUri(htmlspecialchars_decode($path), $options)->toString());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment