Problem/Motivation

Basically, this is about improving error handling in our logic that auto-accepts translations if that is enabled.

Steps to reproduce

Proposed resolution

We want to add a try/catch around that, and if that somehow fails, add a message to the job with the error and then just save it as needs review.

Remaining tasks

User interface changes

API changes

Data model changes

Comments

johnchque created an issue. See original summary.

johnchque’s picture

Status: Active » Needs review
StatusFileSize
new3.22 KB

I added some simple tests that should check this. :)

Status: Needs review » Needs work

The last submitted patch, 2: 3193684-character-limit-2.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

johnchque’s picture

Status: Needs work » Needs review
StatusFileSize
new3.13 KB
new2.15 KB

Updating the tests. :)

Status: Needs review » Needs work

The last submitted patch, 4: 3193684-character-limit-4.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

berdir’s picture

You don't want to throw an exception if the key is not set. As you can see, there are a lot of tests that use different structures. Only do that if a very specific string is set but also don't cause a php notice if that key isn't set. So a combined isset && specific value.

berdir’s picture

And that value should IMHO be a proper structure, meaning an actual translation in a #translation key like other examples. You can use a self-explaining string like 'Invalid translation that will cause an exception' or something like that.

johnchque’s picture

Status: Needs work » Needs review
StatusFileSize
new3.3 KB
new2.31 KB

Ha, I was debugging my test so I negated the check to see if it was working fine but I forgot to revert, my bad. :)
This time should be better. :)
Tests passing locally.

berdir’s picture

Status: Needs review » Needs work
  1. +++ b/sources/content/tests/src/Kernel/ContentEntitySourceUnitTest.php
    @@ -440,6 +440,30 @@ class ContentEntitySourceUnitTest extends ContentEntityTestBase {
    +    $job_item->save();
    +
    +    // Request translation. Here it fails.
    +    $job->requestTranslation();
    +    $items = $job->getItems();
    +    /** @var \Drupal\tmgmt\Entity\JobItem $item */
    +    $item = reset($items);
    +    // If it was set to Auto Accept but there was an error, the Job Item should
    +    // be set as Needs Review.
    +    $this->assertEqual($item->getState(), JobItemInterface::STATE_REVIEW);
    

    Lets also assert that a message was created with state error that contains the error.

    we have some examples that do that in the test,

    \Drupal\Tests\tmgmt\Kernel\PluginsTest::testBasicWorkflow

    I also think that this isn't the right place for this test. This is testing the tmgmt_content source, but we're not doing that here, we're testing the core functionality of tmgmt, so it should go in the main module. \Drupal\Tests\tmgmt\Kernel\CrudTest for example.

  2. +++ b/src/Entity/JobItem.php
    @@ -472,7 +472,12 @@ class JobItem extends ContentEntityBase implements JobItemInterface {
    +        $this->getJob()->addMessage('Translation could not be accepted. Message error: @error', ['@error' => $e->getMessage()], 'error');
    

    Maybe this as message:

    "Failed to automatically accept translation, error: @error."

    Also, add the message on the job item, don't go through the job ($this->addMessage()) so that it is automatically referencing the affected job item. The message wi ll be displayed in the job and job item messages then.

johnchque’s picture

Status: Needs work » Needs review
StatusFileSize
new5.4 KB
new5.56 KB

Moving the tests and adding some assertions. I actually want to see if this is working here. I get some strange error when enabling the node type to be translated.

Status: Needs review » Needs work

The last submitted patch, 10: 3193684-character-limit-10.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

berdir’s picture

+++ b/tests/src/Kernel/CrudTest.php
@@ -231,6 +252,37 @@ class CrudTest extends TMGMTKernelTestBase {
+    $content_translation_manager = \Drupal::service('content_translation.manager');
+    $content_translation_manager->setEnabled('node', $type->id(), TRUE);
+    $new_node = Node::create([
+      'title' => 'Long text',
+      'type' => $type->id(),
+      'langcode' => 'en',
+    ]);
+    $new_node->save();
+    $job = tmgmt_job_create('en', 'de');
+    $job->translator = 'test_translator';
+    $job->save();

you're not installing the node schema.

But you don't need a node.

You're using a test source, that has absolutely nothing to do with nodes. just remove all node related code.

item type and id are just arbitrary strings, see \Drupal\tmgmt_test\Plugin\tmgmt\Source\TestSource::getData().

It just returns a fixed, predefined structure (unless you set something else in state). So you can just do $job->addItem('test_source', 'test', 1); or whatever.

And then for the translation, use the same dummy => deep_nesting structure for your translation, it's strange if you add translation data for something completely different.

Sorry, missed this before.

johnchque’s picture

Status: Needs work » Needs review
StatusFileSize
new3.4 KB
new3.21 KB

Alright! This should work. I was making this more complicated for no reason. :)

berdir’s picture

+++ b/tests/src/Kernel/CrudTest.php
@@ -231,6 +232,29 @@ class CrudTest extends TMGMTKernelTestBase {
+    $job_item->updateData('title', [0 => ['value' => ['#translation' => ['#text' => 'Invalid translation that will cause an exception']]]], TRUE);

you're still setting the translation here for the title, should update that to that dummy/deep nesting thing, so you set a translation for something actually exist. this doesn't fail, but maybe at some point we will validate it.

johnchque’s picture

StatusFileSize
new3.35 KB
new1.58 KB

Alright, sorry I missed that.

johnchque’s picture

Oh I made a mistake here. BTW right now I am checking the message of the job item I hope that is alright. :)

  • Berdir committed a7949c9 on 8.x-1.x authored by johnchque
    Issue #3193684 by johnchque, Berdir: Save as review when character limit...
berdir’s picture

Status: Needs review » Fixed

Changed it to assert the generated message with removed placeholders so we can assert this part works as well.

diff --git a/tests/src/Kernel/CrudTest.php b/tests/src/Kernel/CrudTest.php
index 5736fbfe..854a520f 100644
--- a/tests/src/Kernel/CrudTest.php
+++ b/tests/src/Kernel/CrudTest.php
@@ -246,11 +246,11 @@ class CrudTest extends TMGMTKernelTestBase {
     $job_item->addTranslatedData($translation);
     // If it was set to Auto Accept but there was an error, the Job Item should
     // be set as Needs Review.
-    $this->assertEqual($job_item->getState(), JobItemInterface::STATE_REVIEW);
+    $this->assertEquals(JobItemInterface::STATE_REVIEW, $job_item->getState());
     // There should be a message if auto accept has failed.
     $messages = $job->getMessages();
     $last_message = end($messages);
-    $this->assertEqual('Failed to automatically accept translation, error: @error', $last_message->message->value);
+    $this->assertEquals('Failed to automatically accept translation, error: The translation cannot be saved.', $last_message->getMessage());
   }
 
   /**

Status: Fixed » Closed (fixed)

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