From 40fa14ba9a51b09fd4d99d5a2710bae023db75fd Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Tue, 6 Oct 2015 03:19:33 +0100 Subject: [PATCH] Issue #2557265 by claudiu.cristea, Wim Leers: Synchronize editor status with paired text format status --- core/modules/editor/editor.install | 22 ++++++ core/modules/editor/editor.module | 28 +++++++ .../src/Tests/Update/EditorUpdateTest.php | 75 +++++++++++++++++++ .../Kernel/EditorFilterIntegrationTest.php | 61 +++++++++++++++ .../drupal-8.editor-editor_update_8001.php | 39 ++++++++++ 5 files changed, 225 insertions(+) create mode 100644 core/modules/editor/editor.install create mode 100644 core/modules/editor/src/Tests/Update/EditorUpdateTest.php create mode 100644 core/modules/editor/tests/src/Kernel/EditorFilterIntegrationTest.php create mode 100644 core/modules/system/tests/fixtures/update/drupal-8.editor-editor_update_8001.php diff --git a/core/modules/editor/editor.install b/core/modules/editor/editor.install new file mode 100644 index 0000000000..a07bd80560 --- /dev/null +++ b/core/modules/editor/editor.install @@ -0,0 +1,22 @@ +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(); + } + } +} diff --git a/core/modules/editor/editor.module b/core/modules/editor/editor.module index 16e0520f7f..e83d49a972 100644 --- a/core/modules/editor/editor.module +++ b/core/modules/editor/editor.module @@ -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(); + } +} diff --git a/core/modules/editor/src/Tests/Update/EditorUpdateTest.php b/core/modules/editor/src/Tests/Update/EditorUpdateTest.php new file mode 100644 index 0000000000..beefcfdfba --- /dev/null +++ b/core/modules/editor/src/Tests/Update/EditorUpdateTest.php @@ -0,0 +1,75 @@ +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')); + } + +} diff --git a/core/modules/editor/tests/src/Kernel/EditorFilterIntegrationTest.php b/core/modules/editor/tests/src/Kernel/EditorFilterIntegrationTest.php new file mode 100644 index 0000000000..625fb90011 --- /dev/null +++ b/core/modules/editor/tests/src/Kernel/EditorFilterIntegrationTest.php @@ -0,0 +1,61 @@ + 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())); + } + +} diff --git a/core/modules/system/tests/fixtures/update/drupal-8.editor-editor_update_8001.php b/core/modules/system/tests/fixtures/update/drupal-8.editor-editor_update_8001.php new file mode 100644 index 0000000000..c2e4cab1e9 --- /dev/null +++ b/core/modules/system/tests/fixtures/update/drupal-8.editor-editor_update_8001.php @@ -0,0 +1,39 @@ +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(); -- GitLab