예제 #1
0
 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('<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>', $xml->Content->asXML());
     $this->assertEquals('<a href="http://mysite.com">mysite.com</a> is a link in this HTML content. <![CDATA[this is some nested CDATA]]>', (string) $xml->Content);
 }
 public function testShortcodesInDataObject()
 {
     $formatter = new XMLDataFormatter();
     $page = new XMLDataFormatterTest_DataObject();
     $page->Content = 'This is some test content [test_shortcode]test[/test_shortcode]';
     $xml = new SimpleXMLElement('<?xml version="1.0"?>' . $formatter->convertDataObjectWithoutHeader($page));
     $this->assertEquals('This is some test content test', (string) $xml->Content);
     $page->Content = '[test_shortcode,id=-1]';
     $xml = new SimpleXMLElement('<?xml version="1.0"?>' . $formatter->convertDataObjectWithoutHeader($page));
     $this->assertEmpty('', (string) $xml->Content);
     $page->Content = '[bad_code,id=1]';
     $xml = new SimpleXMLElement('<?xml version="1.0"?>' . $formatter->convertDataObjectWithoutHeader($page));
     $this->assertContains('[bad_code,id=1]', (string) $xml->Content);
 }
 function index()
 {
     XMLDataFormatter::$api_base = 'api/ecommerce/v1/';
     if (!isset($this->urlParams['ClassName'])) {
         return $this->notFound();
     }
     $className = $this->urlParams['ClassName'];
     $id = isset($this->urlParams['ID']) ? $this->urlParams['ID'] : null;
     $relation = isset($this->urlParams['Relation']) ? $this->urlParams['Relation'] : null;
     // Check input formats
     if (!class_exists($className)) {
         return $this->notFound();
     }
     if ($id && !is_numeric($id)) {
         return $this->notFound();
     }
     if ($relation && !preg_match('/^[a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*$/', $relation)) {
         return $this->notFound();
     }
     // fix
     if ($id) {
         $obj = DataObject::get_by_id($className, $id);
         if ($obj) {
             $className = $this->urlParams['ClassName'] = $obj->ClassName;
         } else {
             return $this->notFound();
         }
     }
     // if api access is disabled, don't proceed
     $apiAccess = singleton($className)->stat('api_access');
     if (!$apiAccess) {
         return $this->permissionFailure();
     }
     // authenticate through HTTP BasicAuth
     $this->member = $this->authenticate();
     // handle different HTTP verbs
     if ($this->request->isGET() || $this->request->isHEAD()) {
         return $this->getHandler($className, $id, $relation);
     }
     if ($this->request->isPOST()) {
         return $this->postHandler($className, $id, $relation);
     }
     if ($this->request->isPUT()) {
         return $this->putHandler($className, $id, $relation);
     }
     if ($this->request->isDELETE()) {
         return $this->deleteHandler($className, $id, $relation);
     }
     // if no HTTP verb matches, return error
     return $this->methodNotAllowed();
 }
 /**
  * Write back to the content source
  */
 public function remoteWrite($member = null)
 {
     foreach ($this->remoteProperties as $prop => $val) {
         $this->wrappedObject->{$prop} = $val;
     }
     $this->wrappedObject->ID = $this->getSS_ID();
     $xmlFormatter = new XMLDataFormatter();
     $xmlFormatter->relationDepth = 0;
     $xml = $xmlFormatter->convertDataObjectWithoutHeader($this->wrappedObject);
     $params = array('raw_body' => $xml, 'ClassName' => $this->getType(), 'ID' => $this->getSS_ID());
     $this->getSource()->getRemoteRepository()->saveObject($params);
 }