Problem/Motivation

An exception message in Row.php should include the name of the property not just $id.

Original report:
The invalid exception for multi language, throw a non standard translation strings:
"$id is defined as a source ID but has no value." in core/modules/migrate/src/Row.php

       if (!$this->hasSourceProperty($id)) {
         throw new \InvalidArgumentException("$id is defined as a source ID but has no value.");
       }

Proposed resolution

Add the name of the id to the exception message.

Translation is not added as per the coding standards, specifically item 3 in Basic conventions.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

John.nie created an issue. See original summary.

John.nie’s picture

John.nie’s picture

Status: Active » Needs review
John.nie’s picture

FileSize
656 bytes

Status: Needs review » Needs work

The last submitted patch, 4: migrate-3160323-2.patch, failed testing. View results

quietone’s picture

Issue summary: View changes

@John.nie, thanks for the patch. Adding the name of $id will be helpful.

+++ b/core/modules/migrate/src/Row.php
@@ -104,7 +104,7 @@ public function __construct(array $values = [], array $source_ids = [], $is_stub
+        throw new \InvalidArgumentException(t("@key is defined as a source ID but has no value.", ['@key' => $id]));

The @key needs single quotes, "'$key' is defined as a source ID but has no value". Exceptions messages are not translated so the use of t() can be removed. See Php exceptions.

ravi.shankar’s picture

Assigned: Unassigned » ravi.shankar
ravi.shankar’s picture

Assigned: ravi.shankar » Unassigned
Status: Needs work » Needs review
FileSize
676 bytes
655 bytes

This patch might fix comment #6.

quietone’s picture

Status: Needs review » Needs work

@ravi.shankar, thanks for the patch.

The unit test still needs to be fixed.

jungle’s picture

+++ b/core/modules/migrate/src/Row.php
@@ -104,7 +104,7 @@ public function __construct(array $values = [], array $source_ids = [], $is_stub
-        throw new \InvalidArgumentException("$id is defined as a source ID but has no value.");
+        throw new \InvalidArgumentException("'@key' is defined as a source ID but has no value.", ['@key' => $id]);

IMO, should be just throw new \InvalidArgumentException("'$id' is defined as a source ID but has no value.");, no need to use spritf()

jungle’s picture

Version: 8.8.x-dev » 9.1.x-dev
Issue tags: +Needs issue summary update

It's unclear what to do here, besides adding single quotes around $id, so adding a "Needs issue summary update" tag

quietone’s picture

Version: 9.1.x-dev » 8.8.x-dev
Issue summary: View changes
Issue tags: -Needs issue summary update
quietone’s picture

Version: 8.8.x-dev » 9.1.x-dev
jian he’s picture

Title: Translation: "$id is defined as a source ID but has no value." » String "$id is defined as a source ID but has no value." need support translation
quietone’s picture

Title: String "$id is defined as a source ID but has no value." need support translation » Add property name to exception message in Row.php
Issue summary: View changes

@jian he, thanks for updating the title. I think you are asking for the exception message that is thrown in Row.php to be translated. However, Exception messages in Drupal are not translated. This is in the coding standards for exceptions, it is the 3rd item in Basic conventions. I don't know if there is an issue for adding translation for exception messages.

I'm changing the title based on my understanding. If I am wrong, please say so.

Needs work for fixing the two failing tests.

ravi.shankar’s picture

Status: Needs work » Needs review
FileSize
664 bytes
684 bytes

Here I have tried to fix failed tests of patch #8.

Status: Needs review » Needs work

The last submitted patch, 16: 3160323-16.patch, failed testing. View results

quietone’s picture

+++ b/core/modules/migrate/src/Row.php
@@ -104,7 +104,7 @@ public function __construct(array $values = [], array $source_ids = [], $is_stub
-        throw new \InvalidArgumentException("$id is defined as a source ID but has no value.");

The format string for sprintf looks more like a format string for the t() function. Sprintf doesn't use an '@' symbol for conversion specifications. But there is no need for sprintf anyway. This just needs single quotes around the string $id so that it becomes '$id'.

Then the exception message in testInvalidSourceIdKeys needs updating. And I think we should an a test of the exception message in testRowWithInvalidData() as well. That needs $this->expectExceptionMessage("'nid' is defined as a source ID but has no value.");

@ravi.shankar, I am not aware of your experience or your knowledge with Drupal so please ignore if I am repeating things you already know. Before uploading a patch to let the testbot test it is better to test the patch locally in your own dev environment. That way you can test your ideas on just the one file you are working on and make multiple iterations easily. Setting up for testing is worth the time and effort. There is a handbook section that covers all aspects of PHPUnit in Drupal. Cheers.

ravi.shankar’s picture

@quietone Thanks

Added a patch that might fix the failed tests.

ravi.shankar’s picture

Status: Needs work » Needs review
jungle’s picture

  public function __construct(array $values = [], array $source_ids = [], $is_stub = FALSE) {
    $this->source = $values;
    $this->sourceIds = $source_ids;
    $this->isStub = $is_stub;
    foreach (array_keys($source_ids) as $id) {
      if (!$this->hasSourceProperty($id)) {
        throw new \InvalidArgumentException("'$id' is defined as a source ID but has no value.");
      }
    }
  }

The title is "Add property name to exception message in Row.php", but it's impossible to get the source property per !$this->hasSourceProperty($id), so how to add the property name to the exception message?

From my understanding, this issue is just adding single quotes, nothing more.

quietone’s picture

Title: Add property name to exception message in Row.php » Add quotes around property name in exception message in Row.php
Status: Needs review » Reviewed & tested by the community

@jungle, yes it is about adding single quotes and I have finally updated the title.

The patch in #19 adds the missing single quotes around the name of the source id and the test is updated correctly.

The last submitted patch, 16: 3160323-16.patch, failed testing. View results

alexpott’s picture

Category: Bug report » Task
Status: Reviewed & tested by the community » Fixed

Committed 33a6866 and pushed to 9.1.x. Thanks!

As the ID is already included I think this is only a task.

  • alexpott committed 33a6866 on 9.1.x
    Issue #3160323 by ravi.shankar, John.nie, quietone, jungle: Add quotes...

Status: Fixed » Closed (fixed)

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