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

Add test and fix for API4 failing to decode strings stored as HTML #21549

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Civi/Api4/Generic/Traits/DAOActionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ protected function writeObjects(&$items) {
}

$result[] = $this->baoToArray($createResult, $item);
\CRM_Utils_API_HTMLInputCoder::singleton()->decodeRows($result);
}

// Use bulk `writeRecords` method if the BAO doesn't have a create or add method
Expand Down
33 changes: 33 additions & 0 deletions tests/phpunit/api/v4/Action/ResultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,37 @@ public function testJsonSerialize() {
$this->assertTrue(is_array(json_decode($json)));
}

/**
* Knowing that the db layer HTML-encodes strings, we want to test
* that this ugliness is hidden from us as users of the API.
*
* @see https://issues.civicrm.org/jira/browse/CRM-11532
* @see https://lab.civicrm.org/dev/core/-/issues/1328
*/
public function testNoDataCorruptionThroughEncoding() {

$original = 'hello < you';
$result = Contact::create(FALSE)
->setValues(['display_name' => $original])
->execute()->first();
$this->assertEquals($original, $result['display_name'],
"The value returned from Contact.create is different to the value sent."
);

$result = Contact::update(FALSE)
->addWhere('id', '=', $result['id'])
->setValues(['display_name' => $original])
->execute()->first();
$this->assertEquals($original, $result['display_name'],
"The value returned from Contact.update is different to the value sent."
);

$result = Contact::get(FALSE)
->addWhere('id', '=', $result['id'])
->execute()->first();
$this->assertEquals($original, $result['display_name'],
"The value returned from Contact.get is different to the value sent."
);
}

}