When using a Predicate with the XML Data Parser the final item in the XML is loaded even if it doesn't match the Predicate given. This is causing incorrect items to end up in Migrations.

In an xml import from Wordpress I have 2 pages and 3 posts. The final item in the xml is an image. The parser is saying that there are 3 pages and 4 posts.
failing screenshot

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

segovia94 created an issue. See original summary.

segovia94’s picture

StatusFileSize
new631 bytes
new125.85 KB

The problem is that line 237 in src/Plugin/migrate_plus/data_parser/Xml.php still has a valid target_element even though the predicate did not match earlier since as the final element it exited the loop instead of moving on to a new element.

2 Pages and 3 Posts are now correctly detected.
correct screenshot

segovia94’s picture

Status: Active » Needs review
heddn’s picture

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

I hate to do this, but needs work because needs tests.

jcalais’s picture

@heddn What tests are you specifically talking about? I just ran into this bug and spent a few hours solving it, only to end up here when I wanted to patch it and I need to up my test writing game, so maybe I could help out.

On a different note, which is preferred? unset($target_element) or $target_element = NULL? I would've gone with the first one out of habit mostly.

heddn’s picture

Here's some guidance on test writing. https://www.drupal.org/docs/8/phpunit Look around for existing tests, but I doubt there are any for this code so this would be starting from scratch. I'd probably use unset() myself.

jocowood’s picture

@jcalais: I added a duplicate bug report to this one. (see https://www.drupal.org/project/migrate_plus/issues/3004394). In this issue, the user somatick proved, you should prefer $target_element = NULL instead of unset($target_element), because it avoids the notice "$target_element is undefined".

codebymikey made their first commit to this issue’s fork.

codebymikey’s picture

Version: 8.x-4.x-dev » 6.0.x-dev
Status: Needs work » Needs review
Issue tags: -Needs tests

  • heddn committed 38d1939e on 6.0.x authored by codebymikey
    Issue #2891964 by codebymikey, segovia94, heddn: Using the XML Data...
heddn’s picture

Status: Needs review » Fixed

Thanks for writing tests in this space.

Status: Fixed » Closed (fixed)

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