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

EZP-28118: Error in RelationConverter on content after editing class in legacy #2115

Merged
merged 3 commits into from
Oct 24, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,8 @@ public function toStorageFieldDefinition(FieldDefinition $fieldDef, StorageField
$root->appendChild($constraints);

$selectionType = $doc->createElement('selection_type');
if (isset($fieldSettings['selectionMethod'])) {
$selectionType->setAttribute('value', (int)$fieldSettings['selectionMethod']);
} else {
$selectionType->setAttribute('value', 0);
}
$selectionMethod = isset($fieldSettings['selectionMethod']) ? (int)$fieldSettings['selectionMethod'] : 0;
$selectionType->setAttribute('value', $selectionMethod);
$root->appendChild($selectionType);

$defaultLocation = $doc->createElement('contentobject-placement');
Expand All @@ -99,6 +96,13 @@ public function toStorageFieldDefinition(FieldDefinition $fieldDef, StorageField

$doc->appendChild($root);
$storageDef->dataText5 = $doc->saveXML();

// BC: For Backwards Compatibility for legacy and in case of downgrades or data sharing
// Selection method, 0 = browse, 1 = dropdown
$storageDef->dataInt1 = $selectionMethod;

// Selection root, location ID, or 0 if empty
$storageDef->dataInt2 = (int)$fieldSettings['selectionRoot'];
}

/**
Expand Down Expand Up @@ -146,7 +150,10 @@ public function toFieldDefinition(StorageFieldDefinition $storageDef, FieldDefin
return;
}

if ($selectionType = $dom->getElementsByTagName('selection_type')) {
if (
($selectionType = $dom->getElementsByTagName('selection_type')) &&
$selectionType->item(0)->hasAttribute('value')
) {
$fieldSettings['selectionMethod'] = (int)$selectionType->item(0)->getAttribute('value');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quick test showed me that getElementsByTagName never returns an empty value. It's always \DOMNodeList. It has property length. If length = 0, then $selectionType->item(0) will return null and calling hasAttribute will throw an exception.
So rather:
if (($selectionTypeFirstItem = $dom->getElementsByTagName('selection_type')->item(0)) && $selectionTypeFirstItem->hasAttribute('value'))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, so DomList never evaluates to empty here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Consider the following example (just re-pasting from elsewhere to keep discussion here consistent):

$xml = <<< XML
<?xml version="1.0" encoding="utf-8"?>
<para></para>
XML;

$dom = new DOMDocument;
$dom->loadXML($xml);
if (($elements = $dom->getElementsByTagName('non_existent_tag'))) {
	var_dump($elements->item(0));
}

will output NULL.

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,10 @@ public function toFieldDefinition(StorageFieldDefinition $storageDef, FieldDefin
return;
}

if ($selectionType = $dom->getElementsByTagName('selection_type')) {
if (
($selectionType = $dom->getElementsByTagName('selection_type')) &&
$selectionType->item(0)->hasAttribute('value')
) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same remark as above

$fieldSettings['selectionMethod'] = (int)$selectionType->item(0)->getAttribute('value');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ public function testToStorageFieldDefinition()
<related-objects><constraints><allowed-class contentclass-identifier="article"/><allowed-class contentclass-identifier="blog_post"/></constraints><selection_type value="0"/><contentobject-placement node-id="12345"/></related-objects>

EOT;
// For BC these are still set
$expectedStorageFieldDefinition->dataInt1 = 0;
$expectedStorageFieldDefinition->dataInt2 = 12345;

$actualStorageFieldDefinition = new StorageFieldDefinition();

Expand Down