Skip to content
Snippets Groups Projects
Commit 40fa14ba authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2557265 by claudiu.cristea, Wim Leers: Synchronize editor status with...

Issue #2557265 by claudiu.cristea, Wim Leers: Synchronize editor status with paired text format status
parent 4ea2fc71
No related branches found
No related tags found
No related merge requests found
<?php
/**
* @file
* Install, update and uninstall functions for the Editor module.
*/
/**
* Synchronizes the editor status with the paired text format status.
*/
function editor_update_8001() {
$config_factory = \Drupal::configFactory();
// Iterate on all text formats config entities.
foreach ($config_factory->listAll('filter.format.') as $name) {
list(,, $id) = explode('.', $name, 3);
$status = $config_factory->get($name)->get('status');
$editor = $config_factory->getEditable("editor.editor.$id");
if (!$editor->isNew() && $editor->get('status') !== $status) {
$editor->set('status', $status)->save();
}
}
}
......@@ -6,6 +6,7 @@
*/
use Drupal\Component\Utility\Html;
use Drupal\editor\Entity\Editor;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
......@@ -509,3 +510,30 @@ function _editor_parse_file_uuids($text) {
}
return $uuids;
}
/**
* Implements hook_ENTITY_TYPE_presave().
*
* Synchronizes the editor status to its paired text format status.
*/
function editor_filter_format_presave(FilterFormatInterface $format) {
// The text format being created cannot have a text editor yet.
if ($format->isNew()) {
return;
}
/** @var \Drupal\filter\FilterFormatInterface $original */
$original = \Drupal::entityManager()
->getStorage('filter_format')
->loadUnchanged($format->getOriginalId());
// If the text format status is the same, return early.
if (($status = $format->status()) === $original->status()) {
return;
}
/** @var \Drupal\editor\EditorInterface $editor */
if ($editor = Editor::load($format->id())) {
$editor->setStatus($status)->save();
}
}
<?php
/**
* @file
* Contains \Drupal\editor\Tests\Update\EditorUpdateTest.
*/
namespace Drupal\editor\Tests\Update;
use Drupal\system\Tests\Update\UpdatePathTestBase;
/**
* Tests Editor module database updates.
*
* @group editor
*/
class EditorUpdateTest extends UpdatePathTestBase {
/**
* {@inheritdoc}
*/
public function setDatabaseDumpFiles() {
$this->databaseDumpFiles = [
__DIR__ . '/../../../../system/tests/fixtures/update/drupal-8.bare.standard.php.gz',
// Simulate an un-synchronized environment.
__DIR__ . '/../../../../system/tests/fixtures/update/drupal-8.editor-editor_update_8001.php',
];
}
/**
* Tests editor_update_8001().
*
* @see editor_update_8001()
*/
public function testEditorUpdate8001() {
/** @var \Drupal\Core\Config\ConfigFactoryInterface $config_factory */
$config_factory = $this->container->get('config.factory');
$format_basic_html = $config_factory->get('filter.format.basic_html');
$editor_basic_html = $config_factory->get('editor.editor.basic_html');
$format_full_html = $config_factory->get('filter.format.full_html');
$editor_full_html = $config_factory->get('editor.editor.full_html');
// Checks if the 'basic_html' format and editor statuses differ.
$this->assertTrue($format_basic_html->get('status'));
$this->assertFalse($editor_basic_html->get('status'));
$this->assertNotIdentical($format_basic_html->get('status'), $editor_basic_html->get('status'));
// Checks if the 'full_html' format and editor statuses differ.
$this->assertFalse($format_full_html->get('status'));
$this->assertTrue($editor_full_html->get('status'));
$this->assertNotIdentical($format_full_html->get('status'), $editor_full_html->get('status'));
// Run updates.
$this->runUpdates();
// Reload text formats and editors.
$format_basic_html = $config_factory->get('filter.format.basic_html');
$editor_basic_html = $config_factory->get('editor.editor.basic_html');
$format_full_html = $config_factory->get('filter.format.full_html');
$editor_full_html = $config_factory->get('editor.editor.full_html');
// Checks if the 'basic_html' format and editor statuses are in sync.
$this->assertTrue($format_basic_html->get('status'));
$this->assertTrue($editor_basic_html->get('status'));
$this->assertIdentical($format_basic_html->get('status'), $editor_basic_html->get('status'));
// Checks if the 'full_html' format and editor statuses are in sync.
$this->assertFalse($format_full_html->get('status'));
$this->assertFalse($editor_full_html->get('status'));
$this->assertIdentical($format_full_html->get('status'), $editor_full_html->get('status'));
}
}
<?php
/**
* @file
* Contains \Drupal\Tests\editor\Kernel\EditorFilterIntegration.
*/
namespace Drupal\Tests\editor\Kernel;
use Drupal\Component\Utility\Unicode;
use Drupal\editor\Entity\Editor;
use Drupal\filter\Entity\FilterFormat;
use Drupal\KernelTests\KernelTestBase;
/**
* Tests integration with filter module.
*
* @group editor
*/
class EditorFilterIntegrationTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['filter', 'editor', 'editor_test'];
/**
* Tests text format removal or disabling.
*/
public function testTextFormatIntegration() {
// Create an arbitrary text format.
$format = FilterFormat::create([
'format' => Unicode::strtolower($this->randomMachineName()),
'name' => $this->randomString(),
]);
$format->save();
// Create a paired editor.
Editor::create(['format' => $format->id(), 'editor' => 'unicorn'])->save();
// Disable the text format.
$format->disable()->save();
// The paired editor should be disabled too.
$this->assertFalse(Editor::load($format->id())->status());
// Re-enable the text format.
$format->enable()->save();
// The paired editor should be enabled too.
$this->assertTrue(Editor::load($format->id())->status());
// Completely remove the text format. Usually this cannot occur via UI, but
// can be triggered from API.
$format->delete();
// The paired editor should be removed.
$this->assertNull(Editor::load($format->id()));
}
}
<?php
/**
* @file
* Contains database additions to drupal-8.bare.standard.php.gz for testing the
* upgrade path of editor_update_8001().
*/
use Drupal\Core\Database\Database;
$connection = Database::getConnection();
// Simulate an un-synchronized environment.
// Disable the 'basic_html' editor.
$data = $connection->select('config')
->fields('config', ['data'])
->condition('name', 'editor.editor.basic_html')
->execute()
->fetchField();
$data = unserialize($data);
$data['status'] = FALSE;
$connection->update('config')
->fields(['data' => serialize($data)])
->condition('name', 'editor.editor.basic_html')
->execute();
// Disable the 'full_html' text format.
$data = $connection->select('config')
->fields('config', ['data'])
->condition('name', 'filter.format.full_html')
->execute()
->fetchField();
$data = unserialize($data);
$data['status'] = FALSE;
$connection->update('config')
->fields(['data' => serialize($data)])
->condition('name', 'filter.format.full_html')
->execute();
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment