Commit bb393430 authored by alexpott's avatar alexpott

Issue #2556617 by claudiu.cristea, dawehner, geertvd, Wim Leers, tim.plunkett:...

Issue #2556617 by claudiu.cristea, dawehner, geertvd, Wim Leers, tim.plunkett: [regression] Disabled format still filters and displays the value
parent 97ea6b56
......@@ -78,10 +78,13 @@ public static function preRenderText($element) {
if (!isset($format_id)) {
$format_id = static::configFactory()->get('filter.settings')->get('fallback_format');
}
// If the requested text format does not exist, the text cannot be filtered.
/** @var \Drupal\filter\Entity\FilterFormat $format **/
if (!$format = FilterFormat::load($format_id)) {
static::logger('filter')->alert('Missing text format: %format.', array('%format' => $format_id));
$format = FilterFormat::load($format_id);
// If the requested text format doesn't exist or its disabled, the text
// cannot be filtered.
if (!$format || !$format->status()) {
$message = !$format ? 'Missing text format: %format.' : 'Disabled text format: %format.';
static::logger('filter')->alert($message, array('%format' => $format_id));
$element['#markup'] = '';
return $element;
}
......
......@@ -180,6 +180,10 @@ public function toArray() {
* {@inheritdoc}
*/
public function disable() {
if ($this->isFallbackFormat()) {
throw new \LogicException("The fallback text format '{$this->id()}' cannot be disabled.");
}
parent::disable();
// Allow modules to react on text format deletion.
......
......@@ -9,6 +9,9 @@
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\Unicode;
use Drupal\filter\Entity\FilterFormat;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\simpletest\WebTestBase;
use Drupal\user\RoleInterface;
......@@ -22,7 +25,7 @@ class FilterAdminTest extends WebTestBase {
/**
* {@inheritdoc}
*/
public static $modules = array('filter', 'node');
public static $modules = ['filter', 'node', 'filter_test_plugin', 'dblog'];
/**
* An user with administration permissions.
......@@ -99,6 +102,7 @@ protected function setUp() {
$basic_html_format->getPermissionName(),
$restricted_html_format->getPermissionName(),
$full_html_format->getPermissionName(),
'access site reports',
));
$this->webUser = $this->drupalCreateUser(array('create page content', 'edit own page content'));
......@@ -386,4 +390,69 @@ function testFilterTipHtmlEscape() {
$this->assertRaw('<td class="get">' . $ampersand . '</td>');
}
/**
* Tests whether a field using a disabled format is rendered.
*/
public function testDisabledFormat() {
// Create a node type and add a standard body field.
$node_type = NodeType::create(['type' => Unicode::strtolower($this->randomMachineName())]);
$node_type->save();
node_add_body_field($node_type, $this->randomString());
// Create a text format with a filter that returns a static string.
$format = FilterFormat::create([
'name' => $this->randomString(),
'format' => $format_id = Unicode::strtolower($this->randomMachineName()),
]);
$format->setFilterConfig('filter_static_text', ['status' => TRUE]);
$format->save();
// Create a new node of the new node type.
$node = Node::create([
'type' => $node_type->id(),
'title' => $this->randomString(),
]);
$body_value = $this->randomString();
$node->body->value = $body_value;
$node->body->format = $format_id;
$node->save();
// The format is used and we should see the static text instead of the body
// value.
$this->drupalGet($node->urlInfo());
$this->assertText('filtered text');
// Disable the format.
$format->disable()->save();
$this->drupalGet($node->urlInfo());
// The format is not used anymore.
$this->assertNoText('filtered text');
// The text is not displayed unfiltered or escaped.
$this->assertNoRaw($body_value);
$this->assertNoEscaped($body_value);
// Visit the dblog report page.
$this->drupalLogin($this->adminUser);
$this->drupalGet('admin/reports/dblog');
// The correct message has been logged.
$this->assertRaw(sprintf('Disabled text format: %s.', $format_id));
// Programmatically change the text format to something random so we trigger
// the missing text format message.
$format_id = $this->randomMachineName();
$node->body->format = $format_id;
$node->save();
$this->drupalGet($node->urlInfo());
// The text is not displayed unfiltered or escaped.
$this->assertNoRaw($body_value);
$this->assertNoEscaped($body_value);
// Visit the dblog report page.
$this->drupalGet('admin/reports/dblog');
// The missing text format message has been logged.
$this->assertRaw(sprintf('Missing text format: %s.', $format_id));
}
}
......@@ -7,6 +7,7 @@
namespace Drupal\filter\Tests;
use Drupal\filter\Entity\FilterFormat;
use Drupal\simpletest\KernelTestBase;
/**
......@@ -21,7 +22,7 @@ class FilterCrudTest extends KernelTestBase {
*
* @var array
*/
public static $modules = array('filter', 'filter_test');
public static $modules = ['filter', 'filter_test', 'system', 'user'];
/**
* Tests CRUD operations for text formats and filters.
......@@ -73,6 +74,21 @@ function testTextFormatCrud() {
$this->assertTrue(!isset($formats[$format->id()]), 'filter_formats: Disabled text format no longer exists.');
}
/**
* Tests disabling the fallback text format.
*/
public function testDisableFallbackFormat() {
$this->installConfig(['filter']);
$message = '\LogicException with message "The fallback text format \'plain_text\' cannot be disabled." was thrown.';
try {
FilterFormat::load('plain_text')->disable();
$this->fail($message);
}
catch (\LogicException $e) {
$this->assertIdentical($e->getMessage(), "The fallback text format 'plain_text' cannot be disabled.", $message);
}
}
/**
* Verifies that a text format is properly stored.
*/
......
<?php
/**
* @file
* Contains \Drupal\filter_test_plugin\Plugin\Filter\FilterTestStatic.
*/
namespace Drupal\filter_test_plugin\Plugin\Filter;
use Drupal\filter\FilterProcessResult;
use Drupal\filter\Plugin\FilterBase;
/**
* Provides a filter that returns the same static text.
*
* @Filter(
* id = "filter_static_text",
* title = @Translation("Static filter"),
* type = Drupal\filter\Plugin\FilterInterface::TYPE_HTML_RESTRICTOR,
* settings = {},
* )
*/
class FilterTestStatic extends FilterBase {
/**
* {@inheritdoc}
*/
public function process($text, $langcode) {
return new FilterProcessResult('filtered text');
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment