This commit is contained in:
Ed Wilde 2024-10-22 17:58:18 +13:00 committed by GitHub
commit 0f3acf2034
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 78 additions and 9 deletions

View File

@ -117,9 +117,9 @@ class XMLDataFormatter extends DataFormatter
{ {
$className = $this->sanitiseClassName(get_class($obj)); $className = $this->sanitiseClassName(get_class($obj));
$id = $obj->ID; $id = $obj->ID;
$objHref = Director::absoluteURL($this->config()->api_base . "$className/$obj->ID"); $objHref = Director::absoluteURL($this->config()->api_base . "$className/$obj->ID" . ".xml");
$xml = "<$className href=\"$objHref.xml\">\n"; $xml = "<$className href=\"$objHref\">\n";
foreach ($this->getFieldsForObj($obj) as $fieldName => $fieldType) { foreach ($this->getFieldsForObj($obj) as $fieldName => $fieldType) {
// Field filtering // Field filtering
if ($fields && !in_array($fieldName, $fields ?? [])) { if ($fields && !in_array($fieldName, $fields ?? [])) {
@ -160,11 +160,11 @@ class XMLDataFormatter extends DataFormatter
$fieldName = $relName . 'ID'; $fieldName = $relName . 'ID';
if ($obj->$fieldName) { if ($obj->$fieldName) {
$href = Director::absoluteURL($this->config()->api_base . "$relClass/" . $obj->$fieldName); $href = Director::absoluteURL($this->config()->api_base . "$relClass/" . $obj->$fieldName . ".xml");
} else { } else {
$href = Director::absoluteURL($this->config()->api_base . "$className/$id/$relName"); $href = Director::absoluteURL($this->config()->api_base . "$className/$id/$relName" . ".xml");
} }
$xml .= "<$relName linktype=\"has_one\" href=\"$href.xml\" id=\"" . $obj->$fieldName $xml .= "<$relName linktype=\"has_one\" href=\"$href\" id=\"" . $obj->$fieldName
. "\"></$relName>\n"; . "\"></$relName>\n";
} }
@ -190,8 +190,8 @@ class XMLDataFormatter extends DataFormatter
$items = $obj->$relName(); $items = $obj->$relName();
if ($items) { if ($items) {
foreach ($items as $item) { foreach ($items as $item) {
$href = Director::absoluteURL($this->config()->api_base . "$relClass/$item->ID"); $href = Director::absoluteURL($this->config()->api_base . "$relClass/$item->ID" . ".xml");
$xml .= "<$relClass href=\"$href.xml\" id=\"{$item->ID}\"></$relClass>\n"; $xml .= "<$relClass href=\"$href\" id=\"{$item->ID}\"></$relClass>\n";
} }
} }
$xml .= "</$relName>\n"; $xml .= "</$relName>\n";
@ -221,8 +221,8 @@ class XMLDataFormatter extends DataFormatter
$items = $obj->$relName(); $items = $obj->$relName();
if ($items) { if ($items) {
foreach ($items as $item) { foreach ($items as $item) {
$href = Director::absoluteURL($this->config()->api_base . "$relClass/$item->ID"); $href = Director::absoluteURL($this->config()->api_base . "$relClass/$item->ID" . ".xml");
$xml .= "<$relClass href=\"$href.xml\" id=\"{$item->ID}\"></$relClass>\n"; $xml .= "<$relClass href=\"$href\" id=\"{$item->ID}\"></$relClass>\n";
} }
} }
$xml .= "</$relName>\n"; $xml .= "</$relName>\n";

View File

@ -5,6 +5,8 @@ namespace SilverStripe\RestfulServer\Tests;
use SilverStripe\Dev\SapphireTest; use SilverStripe\Dev\SapphireTest;
use SilverStripe\RestfulServer\DataFormatter\XMLDataFormatter; use SilverStripe\RestfulServer\DataFormatter\XMLDataFormatter;
use Exception; use Exception;
use SilverStripe\Control\Controller;
use SilverStripe\ORM\DataObject;
class XMLDataFormatterTest extends SapphireTest class XMLDataFormatterTest extends SapphireTest
{ {
@ -74,4 +76,71 @@ XML
$formatter = new XMLDataFormatter(); $formatter = new XMLDataFormatter();
$formatter->convertStringToArray($inputXML); $formatter->convertStringToArray($inputXML);
} }
/**
* Tests wrapper output of {@link XMLDataFormatter::convertDataObjectWithoutHeader()}
*/
public function testConvertDataObjectWithoutHeaderClassNameAttribute(): void
{
// Create a mock object
$mock = DataObject::create();
$mock->ID = 1;
// Disable trailing slash by default
Controller::config()->set('add_trailing_slash', false);
// Create a formatter
$formatter = new XMLDataFormatter();
// Test the output
$expectedClass = 'SilverStripe-ORM-DataObject';
$expectedHref = sprintf('http://localhost/api/v1/%s/%d.xml', $expectedClass, $mock->ID);
$expectedOutput = sprintf(
'<%s href="%s"><ID>%d</ID></%s>',
$expectedClass,
$expectedHref,
$mock->ID,
$expectedClass
);
$actualOutput = $formatter->convertDataObjectWithoutHeader($mock);
// remove line breaks and compare
$actualOutput = str_replace(["\n", "\r"], '', $actualOutput);
$this->assertEquals($expectedOutput, $actualOutput);
}
/**
* Tests wrapper output of {@link XMLDataFormatter::convertDataObjectWithoutHeader()} when
* used with a forced trailing slash
*/
public function testConvertDataObjectWithoutHeaderClassNameAttributeWithTrailingSlash(): void
{
// Create a mock object
$mock = DataObject::create();
$mock->ID = 1;
// Enable trailing slash by default
Controller::config()->set('add_trailing_slash', true);
// Create a formatter
$formatter = new XMLDataFormatter();
// Test the output
$expectedClass = 'SilverStripe-ORM-DataObject';
$expectedHref = sprintf('http://localhost/api/v1/%s/%d.xml', $expectedClass, $mock->ID);
$expectedOutput = sprintf(
'<%s href="%s"><ID>%d</ID></%s>',
$expectedClass,
$expectedHref,
$mock->ID,
$expectedClass
);
$actualOutput = $formatter->convertDataObjectWithoutHeader($mock);
// remove line breaks and compare
$actualOutput = str_replace(["\n", "\r"], '', $actualOutput);
$this->assertEquals($expectedOutput, $actualOutput);
}
} }