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 2 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
69 changes: 69 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,71 @@ public function testConvertStringToArrayMultipleEntitiesException()
$formatter = new XMLDataFormatter();
$formatter->convertStringToArray($inputXML);
}

/**
* Tests wrapper output of {@link XMLDataFormatter::convertDataObjectWithoutHeader()}
*/
public function testConvertDataObjectWithoutHeaderClassNameAttribute(): void
Copy link
Member

@emteknetnz emteknetnz Oct 22, 2024

Choose a reason for hiding this comment

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

These two tests are basically identical, use a single test and use a @dataProvider and a bool $trailingSlash param.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, I was being lazy 😪

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