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
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
protected 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(
|
2013-02-18 05:02:11 +01:00
|
|
|
'<Content><![CDATA[<a href="http://mysite.com">mysite.com</a> is a link in this HTML content.]]>'
|
|
|
|
. '</Content>',
|
2010-12-20 05:51:24 +01:00
|
|
|
$xml->Content->asXML()
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2013-02-18 05:02:11 +01:00
|
|
|
'<a href="http://mysite.com">mysite.com</a> is a link in this HTML content.',
|
2012-12-17 15:52:01 +01:00
|
|
|
(string)$xml->Content
|
2010-12-20 05:51:24 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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));
|
2012-12-17 15:52:01 +01:00
|
|
|
$this->assertEquals('This is some test content test', (string)$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-12-17 15:52:01 +01:00
|
|
|
$this->assertEmpty('', (string)$xml->Content);
|
2012-09-17 03:17:54 +02:00
|
|
|
|
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-12-17 15:52:01 +01:00
|
|
|
$this->assertContains('[bad_code,id=1]', (string)$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;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-18 06:21:35 +02:00
|
|
|
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 {
|
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2010-12-20 05:51:24 +01:00
|
|
|
'Name' => 'Varchar(50)',
|
|
|
|
'Company' => 'Varchar(50)',
|
|
|
|
'Content' => 'HTMLText'
|
|
|
|
);
|
|
|
|
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|