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

FIX Make sure the href attribute is constructed correctly when trailing slash is used #139

Open
wants to merge 3 commits into
base: 3
Choose a base branch
from
Open
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
18 changes: 9 additions & 9 deletions src/DataFormatter/XMLDataFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ public function convertDataObjectWithoutHeader(DataObject $obj, $fields = null,
{
$className = $this->sanitiseClassName(get_class($obj));
$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) {
// Field filtering
if ($fields && !in_array($fieldName, $fields ?? [])) {
Expand Down Expand Up @@ -160,11 +160,11 @@ public function convertDataObjectWithoutHeader(DataObject $obj, $fields = null,

$fieldName = $relName . 'ID';
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 {
$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";
}

Expand All @@ -190,8 +190,8 @@ public function convertDataObjectWithoutHeader(DataObject $obj, $fields = null,
$items = $obj->$relName();
if ($items) {
foreach ($items as $item) {
$href = Director::absoluteURL($this->config()->api_base . "$relClass/$item->ID");
$xml .= "<$relClass href=\"$href.xml\" id=\"{$item->ID}\"></$relClass>\n";
$href = Director::absoluteURL($this->config()->api_base . "$relClass/$item->ID" . ".xml");
$xml .= "<$relClass href=\"$href\" id=\"{$item->ID}\"></$relClass>\n";
}
}
$xml .= "</$relName>\n";
Expand Down Expand Up @@ -221,8 +221,8 @@ public function convertDataObjectWithoutHeader(DataObject $obj, $fields = null,
$items = $obj->$relName();
if ($items) {
foreach ($items as $item) {
$href = Director::absoluteURL($this->config()->api_base . "$relClass/$item->ID");
$xml .= "<$relClass href=\"$href.xml\" id=\"{$item->ID}\"></$relClass>\n";
$href = Director::absoluteURL($this->config()->api_base . "$relClass/$item->ID" . ".xml");
$xml .= "<$relClass href=\"$href\" id=\"{$item->ID}\"></$relClass>\n";
}
}
$xml .= "</$relName>\n";
Expand Down
49 changes: 49 additions & 0 deletions tests/unit/XMLDataFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use SilverStripe\Dev\SapphireTest;
use SilverStripe\RestfulServer\DataFormatter\XMLDataFormatter;
use Exception;
use SilverStripe\Control\Controller;
use SilverStripe\ORM\DataObject;

class XMLDataFormatterTest extends SapphireTest
{
Expand Down Expand Up @@ -74,4 +76,51 @@ public function testConvertStringToArrayMultipleEntitiesException()
$formatter = new XMLDataFormatter();
$formatter->convertStringToArray($inputXML);
}

/**
* Data provider for trailing slash configuration
*/
public function trailingSlashProvider(): array
{
return [
'without trailing slash' => [false],
'with trailing slash' => [true],
];
}

/**
* Tests wrapper output of {@link XMLDataFormatter::convertDataObjectWithoutHeader()}
*
* @param bool $addTrailingSlash - Whether to add a trailing slash to the API endpoint
* @dataProvider trailingSlashProvider
*/
public function testConvertDataObjectWithoutHeaderClassNameAttribute(bool $addTrailingSlash): void
{
// Create a mock object
$mock = DataObject::create();
$mock->ID = 1;

// Set trailing slash configuration
Controller::config()->set('add_trailing_slash', $addTrailingSlash);

// 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);
}
}