Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug in Magento\Framework\Simplexml\Element::appendChild #1035

Closed
tim-reynolds opened this issue Feb 11, 2015 · 6 comments
Closed

Bug in Magento\Framework\Simplexml\Element::appendChild #1035

tim-reynolds opened this issue Feb 11, 2015 · 6 comments
Labels
bug report Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development

Comments

@tim-reynolds
Copy link
Contributor

The bug is in the call to:
$source->children()

If an XML node has no child nodes and no attributes, this resolves to FALSE
However, if the XML node has no children and has one or more attributes it resolves to TRUE when clearly it should not.

See the line of code in question:
https://github.com/magento/magento2/blob/develop/lib/internal/Magento/Framework/Simplexml/Element.php#L329

This line needs to be changed to

if(count($source->children())) {

Please see this Gist
https://gist.github.com/tim-reynolds/ac20289acdcfcde3633f

The bug exists in Magento 1.X as well as 2.0.

I would really blame PHP SimpleXMLElement but we can't readily fix that.

@anupdugar anupdugar added the PS label Feb 11, 2015
@anupdugar
Copy link
Contributor

Thanks for submitting this pull request. We will process it internally through ticket MAGETWO-34069.
Also using \SimpleXMLElement::count is another option.
It counts the children of an element

if ($source->count()) {

ex:

$node = new \SimpleXMLElement('<sample testAttr="attrVal">test</sample>');
if ($node->count()) {
    echo 'has child';
} else {
    echo 'no child';
}

@tim-reynolds
Copy link
Contributor Author

Thanks for the quick response Anup!

I did not yet create a pull request proper as my local version of Magento2 is outdated and I was concerned about setup for unit tests.

Did you need a proper pull request for this?

@anupdugar
Copy link
Contributor

@tim-reynolds Pull request is highly encouraged. Here's our contribution guide:
https://github.com/magento/magento2/wiki/Contribution-Guide

Here's the Unit test in question : https://github.com/magento/magento2/blob/develop/dev/tests/unit/testsuite/Magento/Framework/Simplexml/ElementTest.php

For running this Unit test:

  • Navigate to m2_project_root/dev/tests/unit
  • ../../../vendor/phpunit/phpunit/phpunit testsuite/Magento/Framework/Simplexml/ElementTest.php

Feel free to check out our documentation regrading installation and updating your dependencies http://devdocs.magento.com/guides/v1.0/install-gde/install/prepare-install.html

@alankent
Copy link

One of the reasons for submitting a pull request is so your name is associated with the code as the contributor of the solution. So it's not mandatory to get it fixed (we can add to our bug backlog), it's just the best way to give you full credit for your contribution. You will have your name as a contributor in github for the world to see.

@tim-reynolds
Copy link
Contributor Author

@alankent - Yeah, hopefully my name is already in. I think I've sent at least one maybe two bug patches into the project, but that was early when they weren't giving attribution on fixes.

Working on a PR now.

tim-reynolds added a commit to tim-reynolds/magento2 that referenced this issue Feb 15, 2015
magento#1035

A bug in the appendNode method of Magento\Framework\Simplexml\Element.php causes values to be lost in very specific cases. This will fix it. The existing unit test for appendNode has been updated as well.

Bug: When appending a node that has an attribute and no child nodes, the code will take the code path for having children. This is because the result of $source->children() evaluates to TRUE when a node has attributes and no children.

Fix: Changing to call from [children()] to [count()] fixes the issue.
@ilol ilol added the TECH label Feb 17, 2015
@vpelipenko vpelipenko added Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development and removed TECH labels Feb 17, 2015
@anupdugar
Copy link
Contributor

The associated Pull request has been merged, closing the issue. Thanks again for your contribution!

magento-team pushed a commit that referenced this issue Apr 18, 2017
…ON_ERROR

MAGETWO-63116: [GitHub] Quotes in product name causes JSON error on product page MAP What's This Popup #8059
VitaliyBoyko pushed a commit to VitaliyBoyko/magento2 that referenced this issue Jun 22, 2018
MSI-1033: Saving configurable product automatically makes its children in stock
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug report Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development
Projects
None yet
Development

No branches or pull requests

5 participants