2010-12-20 05:51:24 +01:00
|
|
|
<?php
|
|
|
|
class XMLDataFormatterTest extends SapphireTest {
|
2012-09-18 06:21:35 +02:00
|
|
|
protected $arguments, $contents, $tagName;
|
2010-12-20 05:51:24 +01:00
|
|
|
|
2011-03-30 08:49:11 +02:00
|
|
|
public static $fixture_file = 'XMLDataFormatterTest.yml';
|
2010-12-20 05:51:24 +01:00
|
|
|
|
2012-09-18 06:21:35 +02:00
|
|
|
public function setUp() {
|
|
|
|
ShortcodeParser::get_active()->register('test_shortcode', array($this, 'shortcodeSaver'));
|
|
|
|
|
|
|
|
parent::setUp();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown() {
|
|
|
|
ShortcodeParser::get_active()->unregister('test_shortcode');
|
|
|
|
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
2010-12-20 05:51:24 +01:00
|
|
|
protected $extraDataObjects = array(
|
|
|
|
'XMLDataFormatterTest_DataObject'
|
|
|
|
);
|
|
|
|
|
|
|
|
public function testConvertDataObjectWithoutHeader() {
|
|
|
|
$formatter = new XMLDataFormatter();
|
|
|
|
$obj = $this->objFromFixture('XMLDataFormatterTest_DataObject', 'test-do');
|
|
|
|
$xml = new SimpleXMLElement('<?xml version="1.0"?>' . $formatter->convertDataObjectWithoutHeader($obj));
|
|
|
|
$this->assertEquals(
|
|
|
|
Director::absoluteBaseURL() . sprintf('api/v1/XMLDataFormatterTest_DataObject/%d.xml', $obj->ID),
|
|
|
|
(string) $xml['href']
|
|
|
|
);
|
|
|
|
$this->assertEquals('Test DataObject', (string) $xml->Name);
|
|
|
|
$this->assertEquals('Test Company', (string) $xml->Company);
|
|
|
|
$this->assertEquals($obj->ID, (int) $xml->ID);
|
|
|
|
$this->assertEquals(
|
2012-09-26 23:34:00 +02:00
|
|
|
'<Content><![CDATA[<a href="http://mysite.com">mysite.com</a> is a link in this HTML content.'
|
|
|
|
. ' <![CDATA[this is some nested CDATA]]]]><![CDATA[>]]></Content>',
|
2010-12-20 05:51:24 +01:00
|
|
|
$xml->Content->asXML()
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2012-09-26 23:34:00 +02:00
|
|
|
'<a href="http://mysite.com">mysite.com</a> is a link in this HTML content.'
|
|
|
|
. ' <![CDATA[this is some nested CDATA]]>',
|
2010-12-20 05:51:24 +01:00
|
|
|
(string) $xml->Content
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-09-17 03:17:54 +02:00
|
|
|
public function testShortcodesInDataObject() {
|
|
|
|
$formatter = new XMLDataFormatter();
|
|
|
|
|
2012-09-18 06:21:35 +02:00
|
|
|
$page = new XMLDataFormatterTest_DataObject();
|
|
|
|
$page->Content = 'This is some test content [test_shortcode]test[/test_shortcode]';
|
2012-09-17 03:17:54 +02:00
|
|
|
|
2012-09-18 06:21:35 +02:00
|
|
|
$xml = new SimpleXMLElement('<?xml version="1.0"?>' . $formatter->convertDataObjectWithoutHeader($page));
|
|
|
|
$this->assertEquals('This is some test content test', $xml->Content);
|
2012-09-17 03:17:54 +02:00
|
|
|
|
2012-09-18 06:21:35 +02:00
|
|
|
$page->Content = '[test_shortcode,id=-1]';
|
|
|
|
$xml = new SimpleXMLElement('<?xml version="1.0"?>' . $formatter->convertDataObjectWithoutHeader($page));
|
2012-09-17 03:17:54 +02:00
|
|
|
$this->assertEmpty('', $xml->Content);
|
|
|
|
|
2012-09-18 06:21:35 +02:00
|
|
|
$page->Content = '[bad_code,id=1]';
|
|
|
|
|
|
|
|
$xml = new SimpleXMLElement('<?xml version="1.0"?>' . $formatter->convertDataObjectWithoutHeader($page));
|
2012-09-17 03:17:54 +02:00
|
|
|
$this->assertContains('[bad_code,id=1]', $xml->Content);
|
2012-09-18 06:21:35 +02:00
|
|
|
}
|
2012-09-17 03:17:54 +02:00
|
|
|
|
2012-09-18 06:21:35 +02:00
|
|
|
/**
|
|
|
|
* Stores the result of a shortcode parse in object properties for easy testing access.
|
|
|
|
*/
|
|
|
|
public function shortcodeSaver($arguments, $content = null, $parser, $tagName = null) {
|
|
|
|
$this->arguments = $arguments;
|
|
|
|
$this->contents = $content;
|
|
|
|
$this->tagName = $tagName;
|
|
|
|
|
|
|
|
return $content;
|
2012-09-17 03:17:54 +02:00
|
|
|
}
|
|
|
|
|
2010-12-20 05:51:24 +01:00
|
|
|
}
|
|
|
|
class XMLDataFormatterTest_DataObject extends DataObject implements TestOnly {
|
|
|
|
|
|
|
|
public static $db = array(
|
|
|
|
'Name' => 'Varchar(50)',
|
|
|
|
'Company' => 'Varchar(50)',
|
|
|
|
'Content' => 'HTMLText'
|
|
|
|
);
|
|
|
|
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|