Saving a node with an invalid data-editor-file-uuid attribute gives a fatal error.

Comments

The last submitted patch, editor_file_usage-fail.patch, failed testing.

olli’s picture

StatusFileSize
new2.29 KB
+++ b/core/modules/editor/editor.module
@@ -411,13 +411,14 @@ function editor_entity_revision_delete(EntityInterface $entity) {
-  foreach ($uuids as $uuid) {
-    $file = entity_load_by_uuid('file', $uuid);
...
+    foreach (entity_load_multiple_by_properties('file', ['uuid' => $uuids]) as $file) {

I guess these are not the same thing as it wouldn't record each usage but only one per entity.

dawehner’s picture

+++ b/core/modules/editor/src/Tests/EditorFileUsageTest.php
@@ -68,7 +68,7 @@ public function testEditorEntityHooks() {
       'title' => 'test',
       'body' => array(
-        'value' => '<p>Hello, world!</p><img src="awesome-llama.jpg" data-editor-file-uuid="' . $image->uuid() . '" />',
+        'value' => '<p>Hello, world!</p><img src="awesome-llama.jpg" data-editor-file-uuid="' . $image->uuid() . '" /><img src="awesome-llama.jpg" data-editor-file-uuid="invalid-editor-file-uuid-value" />',
         'format' => 'filtered_html',
       ),
       'uid' => 1,

Some comment above would be great to explain what we are testing here.

olli’s picture

StatusFileSize
new1.07 KB
new2.58 KB

Thanks for the review! Something like this?

The last submitted patch, 4: 2326537-fail.patch, failed testing.

dawehner’s picture

Great!

+++ b/core/modules/editor/src/Tests/EditorFileUsageTest.php
@@ -103,6 +103,11 @@ public function testEditorEntityHooks() {
+    // Test hook_entity_update() by adding an invalid data- attribute.
+    $node->get('body')->first()->get('value')->setValue($original_value . '<img src="awesome-llama.jpg" data-editor-file-uuid="invalid-file-uuid" />');
+    $node->save();
+    $this->assertIdentical(array('editor' => array('node' => array(1 => '2'))), $file_usage->listUsage($image), 'The image still has 2 usages.');

I wonder whether we could also test a non-existing UUID

wim leers’s picture

Status: Needs review » Needs work

So very close! I'm sure the next one will be RTBC :)


There should also be test coverage for hook_entity_insert() and hook_entity_delete(). Finally, ideally this would test both an invalid UUID (like you're doing now) and a non-existing UUID.

+++ b/core/modules/editor/src/Tests/EditorFileUsageTest.php
@@ -103,6 +103,11 @@ public function testEditorEntityHooks() {
+    // Test hook_entity_update() by adding an invalid data- attribute.

This should be Test hook_entity_update()'s handling of an invalid data- attribute.

dawehner’s picture

This should be Test hook_entity_update()'s handling of an invalid data- attribute.

What about directly pointing to the code handling that?

olli’s picture

Status: Needs work » Needs review
StatusFileSize
new1.44 KB
new2.39 KB
new2.95 KB

Thanks!
Re #6: added a random uuid.
Re #7/#8: modified the comment pointing to functions we're fixing. These functions are called from hook_insert/update/revision_delete/delete.

The last submitted patch, 9: 2326537-fail1.patch, failed testing.

The last submitted patch, 9: 2326537-fail2.patch, failed testing.

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

Awesome!

wim leers’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/modules/editor/src/Tests/EditorFileUsageTest.php
@@ -103,6 +103,16 @@ public function testEditorEntityHooks() {
+    // Test _editor_delete_file_usage()'s handling of invalid data- attributes.
+    $node->get('body')->first()->get('value')->setValue($original_value);
+    $node->save();
+    $this->assertIdentical(array('editor' => array('node' => array(1 => '2'))), $file_usage->listUsage($image), 'The image has 2 usages.');

$original_value contains a valid file entity UUID. So this doesn't test the deletion hook in any way, it tests the insertion hook for a valid UUID, which is a case already covered by existing tests.

Your assertion also proves this: the assertion verifies that after this code has run, there are two usages, versus one before. If this was the deletion of an invalid file UUID, then the number of usages would have to remain constant.


Therefore, I'm afraid that #9 is wrong. It claims to test hook_entity_delete(), but it doesn't, as I've explained above. It also doesn't yet test hook_entity_insert(). And it also doesn't do this yet:

Finally, ideally this would test both an invalid UUID (like you're doing now) and a non-existing UUID.

So… I'm afraid I have to unRTBC this :(

olli’s picture

Re #13: Yes, you are right, this doesn't test those hooks at all. This (cl)aims to test _editor_record_file_usage() and _editor_delete_file_usage() by first replacing the valid value with an invalid value (_editor_record_file_usage for invalid value fails, fail1 patch above) which decreases usage from 2 to 1, and then replacing the invalid value with the original valid value (_editor_record_delete_usage for invalid value fails, fail2 patch) which restores usage from 1 to 2.

Hooks (editor_entity_insert, editor_entity_update, editor_entity_revision_delete and editor_entity_delete) are already covered by existing tests. Do you think it is necessary to run those again with invalid values? If so, maybe just append them to original value like in #0 editor_file_usage-fail.patch rather than what I've done in #9?

About the UUIDs, the latter is just a random one and the other value is what you get if you disable file uploads. Which values would you prefer?

Thank you again and sorry for not providing interdiffs!

wim leers’s picture

Do you think it is necessary to run those again with invalid values? If so, maybe just append them to original value like in #0 editor_file_usage-fail.patch […]

Yes and yes :)

About the UUIDs, the latter is just a random one and the other value is what you get if you disable file uploads. Which values would you prefer?

Both! A valid, but non-existing UUID and something that doesn't even resemble a UUID.

Thank you again and sorry for not providing interdiffs!

Thank you, and no worries! :)

olli’s picture

Status: Needs work » Needs review
StatusFileSize
new2.83 KB
new2.49 KB

Here's #15.

wim leers’s picture

Title: Call to undefined method stdClass::save() in _editor_record_file_usage() » editor.module causes fatal error for images with invalid or non-existing file UUIDs
Status: Needs review » Reviewed & tested by the community

Perfect — thank you :)

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed 7e2ab24 and pushed to 8.0.x. Thanks!

  • alexpott committed 7e2ab24 on 8.0.x
    Issue #2326537 by olli: Fixed editor.module causes fatal error for...

Status: Fixed » Closed (fixed)

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