Problem/Motivation

Text editor select drop down has ajax error due to new DrupalMediaLibrary button's getConfig() code.

Proposed resolution

The getConfig() method should return an empty array in the context of this form, or basically if the prerequisites for the MediaLibraryState are missing.

Remaining tasks

Review / commit.

User interface changes

Fixes broken UI?

API changes

N/A

Data model changes

N/A

Release notes snippet

Comments

oknate created an issue. See original summary.

oknate’s picture

StatusFileSize
new872 bytes

Here's a fix, let me see if I can get a fail patch and test coverage.

oknate’s picture

StatusFileSize
new2.19 KB
new2.97 KB

What’s the best way to tell if a config entity hasn’t been saved yet? is it to check the ->id() method?

We could inject the current route match and test if we're on the filter format add form, but this seems unnecessary here.

oknate’s picture

Status: Active » Needs review
oknate’s picture

StatusFileSize
new2.97 KB

After discussing this with larowlan, I think isNew() is the right thing to use. Since Editor uses the ::isNew() method in ConfigEntityBase(), I'm pretty sure isNew() will only return TRUE before saving, although the documentation in ConfigEntityBase is a little unclear, I guess because it can be overridden.

Ah this confirms it:

    // Config is no longer new once saved.
    $this->config->save();
    $this->assertFalse($this->config->isNew());

(from core/tests/Drupal/Tests/Core/Config/ConfigTest.php)

The last submitted patch, 3: 3078161-3—-FAIL.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

oknate’s picture

Issue summary: View changes
oknate’s picture

StatusFileSize
new1.33 KB
new2.84 KB

Coding standard fixes.

pandaski’s picture

Do we need to check

$editor->get('status')

Otherwise looks good to me

phenaproxima’s picture

Status: Needs review » Needs work
Issue tags: +blocker

Only two things, then RTBC once I've manually tested it. This blocks #2994702: Allow editors to alter embed-specific metadata, as well as `data-align` and `data-caption`.

+++ b/core/modules/media_library/src/Plugin/CKEditorPlugin/DrupalMediaLibrary.php
@@ -108,6 +108,13 @@ public function getFile() {
+
+    // If the editor hasn't been saved, return an
+    // empty config.
+    if ($editor->isNew()) {
+      return [];
+    }

There's an empty line above this which shouldn't be here, and the we need to expand the comment because it doesn't currently explain why we do this. How about: "If the editor hasn't been saved, we will not be able to create a coherent MediaLibraryState instance, which is needed in order to generate the required configuration. However, if we're creating a new editor, we don't need to do that anyway, so just return an empty array instead."

+++ b/core/modules/media_library/tests/src/FunctionalJavascript/CKEditorIntegrationTest.php
@@ -205,4 +221,19 @@ public function testButton() {
+  /**
+   * Show visually hidden fields.
+   */
+  protected function showHiddenFields() {
+    $script = <<<JS
+      var hidden_fields = document.querySelectorAll(".visually-hidden");
+
+      [].forEach.call(hidden_fields, function(el) {
+        el.classList.remove("visually-hidden");
+      });
+JS;
+
+    $this->getSession()->executeScript($script);
+  }
+

In a JavaScript test, this feels like cheating. I would prefer if we entered the value in the 'name' field, then waited for the machine name to show up. (Or, alternately, we could wait for the machine name field to have the expected value.)

oknate’s picture

Status: Needs work » Needs review
StatusFileSize
new2.54 KB
new2.37 KB

Addressing feedback in #10.

-    $this->showHiddenFields();
-    $page->fillField('name', 'new_test_format');
-    $page->fillField('format', 'new_test_format');
+    $page->fillField('name', 'New test format');
+    $this->assertNotEmpty($assert_session->waitForText('new_test_format'));
oknate’s picture

StatusFileSize
new919 bytes
new2.59 KB

I forgot to update the comment.

phenaproxima’s picture

Status: Needs review » Reviewed & tested by the community

Thanks, @oknate. Looks good to me. RTBC when tests are green.

phenaproxima’s picture

Title: Regression: Text editor selector broken » DrupalMediaLibrary plugin breaks things when adding a new text editor
oknate’s picture

StatusFileSize
new2.67 KB
new3.77 KB
new2.82 KB

Since this regression was found while testing #2994702: Allow editors to alter embed-specific metadata, as well as `data-align` and `data-caption`, I wanted to update the test coverage to reflect changes made there.

I don't want to hold the regression fix back though. So if someone can commit #12, this update in #15 can wait.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 15: 3078161-15--FAIL.patch, failed testing. View results

oknate’s picture

Status: Needs work » Needs review
StatusFileSize
new3.77 KB

Same as #15, just reposting so the last patch isn't a FAIL patch. I should have posted the fail patch penultimately.

oknate’s picture

StatusFileSize
new1.02 KB
new2.67 KB
new3.77 KB

Fixing a capitalization inconsistency. I capitalized the label one place, but not both places it appears.

wim leers’s picture

Status: Needs review » Needs work
Issue tags: +Media Initiative

#15 strengthened the test coverage. It already was RTBC in #12.

I wanted to re-RTBC, but:

+++ b/core/modules/media_library/tests/src/FunctionalJavascript/CKEditorIntegrationTest.php
@@ -155,7 +155,20 @@ public function testConfigurationValidation() {
+    // Test that DrupalMediaLibrary button drupal-media adds allowed tags.
...
+    // Test that the saved filter format has the new allowed html tags on the
+    // <drupal-media> tag.

These comments need to be improved. The first sentence is very broken, the second sentence should have "html" capitalized to "HTML".

wim leers’s picture

Issue tags: +Needs reroll, +php-novice, +Novice
oknate’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll, -php-novice, -Novice
StatusFileSize
new1.51 KB
new3.77 KB

Fixing comments.

meenakshig’s picture

StatusFileSize
new3.78 KB
new1.53 KB

improved comments

oknate’s picture

I think it's better to just drop the word 'drupal-media', 'drupal-media' isn't the name of the button.

-    // Test that DrupalMediaLibrary button drupal-media adds allowed tags.
+    // Tests that DrupalMediaLibrary button <drupal-media> adds the allowed tags.

This is what I have in 21:

-    // Test that DrupalMediaLibrary button drupal-media adds allowed tags.
+    // Test that the DrupalMediaLibrary button adds allowed tags.

If we want to be really specific.
Test that when adding the DrupalMediaLibrary button to the editor the correct tags are added to the <drupal-media> tag in the Allowed HTML tags.

oknate’s picture

StatusFileSize
new1.04 KB
new3.85 KB

Updating the wording.

oknate’s picture

StatusFileSize
new1.05 KB
new3.86 KB

Fixing the wording, part two, line was longer than 80 characters.

oknate’s picture

StatusFileSize
new1.05 KB
new3.87 KB

Adding the word "when". It was a bit garbled again.

phenaproxima’s picture

Status: Needs review » Needs work

I really wanna re-RTBC, but first a few small things:

  1. +++ b/core/modules/media_library/tests/src/FunctionalJavascript/CKEditorIntegrationTest.php
    @@ -141,6 +140,37 @@ public function testConfigurationValidation() {
    +    $this->assertNotEmpty($buttonElement = $assert_session->elementExists('xpath', '//li[@data-drupal-ckeditor-button-name="DrupalMediaLibrary"]'));
    +    $buttonElement->dragTo($target);
    

    Nit: From what I hear, the coding standards don't want us to mix snake_case and camelCase in the same file. So, $buttonElement should be $button_element.

    Also, and this is no big deal at all: why are some of the element locators in this test CSS selectors while others are XPath queries?

  2. +++ b/core/modules/media_library/tests/src/FunctionalJavascript/CKEditorIntegrationTest.php
    @@ -141,6 +140,37 @@ public function testConfigurationValidation() {
    +    $assert_session->pageTextContains('Added text format Sulaco.');
    +    // Test that when adding the DrupalMediaLibrary button to the editor the
    

    The comment should have a blank line above it, I think; it's a new "section" of the test.

  3. +++ b/core/modules/media_library/tests/src/FunctionalJavascript/CKEditorIntegrationTest.php
    @@ -141,6 +140,37 @@ public function testConfigurationValidation() {
    +    // Test that the saved filter format has the new allowed HTML tags on the
    +    // <drupal-media> tag.
    +    $format = FilterFormat::load('sulaco');
    +    $allowed_html = $format->filters('filter_html')->settings['allowed_html'];
    +    $this->assertContains($expected, $allowed_html);
    

    I'm not sure what this is adding -- it's just proving that the filter form works (and saves the entity), which is surely tested elsewhere and is not really in scope for this test, IMHO.

oknate’s picture

Status: Needs work » Needs review
StatusFileSize
new2.09 KB
new3.57 KB

Addressing feedback in #27
1. Changed $buttonElement variable to $button.
2. Added blank line.
3. Dropped the end of the test where it checks that the attributes save properly. I guess you're right, we don't need test coverage for that.

meenakshig’s picture

StatusFileSize
new3.87 KB
new1.57 KB

1. Changed $buttonElement to $button_element
2. Added a blank line above comment

phenaproxima’s picture

Status: Needs review » Needs work

Regarding #28:

+++ b/core/modules/simpletest/simpletest.module
@@ -565,14 +565,8 @@ function simpletest_classloader_register() {
- *
- * @deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use
- *   \Drupal\Tests\TestFileCreationTrait::generateFile() instead.
- *
- * @see https://www.drupal.org/node/3077768

wat.

Looks like there are some unintended changes in here...that patch is RTBC otherwise, I think. Can you post a FAIL patch too, just for completeness' sake?

oknate’s picture

Status: Needs work » Needs review
StatusFileSize
new2.62 KB
new3.57 KB

Rerolling patch and adding fail patch.

phenaproxima’s picture

Status: Needs review » Reviewed & tested by the community

And, DONE. Let's get this bad boy in.

The last submitted patch, 31: 3078161-31--FAIL.patch, failed testing. View results

wim leers’s picture

+++ b/core/modules/media_library/tests/src/FunctionalJavascript/CKEditorIntegrationTest.php
@@ -141,6 +140,33 @@ public function testConfigurationValidation() {
+    // correct tags are added to the <drupal-media> tag in the Allowed HTML

Not "tags", but "attributes" … 😊

(Can be fixed on commit.)

oknate’s picture

Re #34: D'oh, yes attributes!, not tags! I won't update it as I don't want to trigger another test. Let's fix it on commit.

  • catch committed 9d9fd0a on 8.8.x
    Issue #3078161 by oknate, Meenakshi.g, phenaproxima, Wim Leers:...
catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to 8.8.x, thanks!

wim leers’s picture

Thanks @catch, and thanks for fixing #34 on commit :)

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.