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.

| Comment | File | Size | Author |
|---|---|---|---|
| #2 | fix-ignored-predicate-2891964-2.patch | 631 bytes | segovia94 |
Issue fork migrate_plus-2891964
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
Comment #2
segovia94 commentedThe 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.

Comment #3
segovia94 commentedComment #4
heddnI hate to do this, but needs work because needs tests.
Comment #5
jcalais commented@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.
Comment #6
heddnHere'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.
Comment #7
jocowood@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".
Comment #10
codebymikey commentedComment #12
heddnThanks for writing tests in this space.