XmlSerializationTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <?php
  2. /*
  3. * Copyright 2013 Johannes M. Schmitt <schmittjoh@gmail.com>
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. namespace JMS\Serializer\Tests\Serializer;
  18. use JMS\Serializer\Construction\UnserializeObjectConstructor;
  19. use JMS\Serializer\Handler\DateHandler;
  20. use JMS\Serializer\Handler\HandlerRegistry;
  21. use JMS\Serializer\SerializationContext;
  22. use JMS\Serializer\Serializer;
  23. use JMS\Serializer\Tests\Fixtures\InvalidUsageOfXmlValue;
  24. use JMS\Serializer\Exception\InvalidArgumentException;
  25. use JMS\Serializer\Tests\Fixtures\PersonCollection;
  26. use JMS\Serializer\Tests\Fixtures\PersonLocation;
  27. use JMS\Serializer\Tests\Fixtures\Person;
  28. use JMS\Serializer\Tests\Fixtures\ObjectWithVirtualXmlProperties;
  29. use JMS\Serializer\Tests\Fixtures\ObjectWithXmlKeyValuePairs;
  30. use JMS\Serializer\Tests\Fixtures\ObjectWithXmlNamespaces;
  31. use JMS\Serializer\Tests\Fixtures\ObjectWithXmlRootNamespace;
  32. use JMS\Serializer\Tests\Fixtures\Input;
  33. use JMS\Serializer\Tests\Fixtures\SimpleClassObject;
  34. use JMS\Serializer\Tests\Fixtures\SimpleSubClassObject;
  35. class XmlSerializationTest extends BaseSerializationTest
  36. {
  37. /**
  38. * @expectedException JMS\Serializer\Exception\RuntimeException
  39. */
  40. public function testInvalidUsageOfXmlValue()
  41. {
  42. $obj = new InvalidUsageOfXmlValue();
  43. $this->serialize($obj);
  44. }
  45. /**
  46. * @dataProvider getXMLBooleans
  47. */
  48. public function testXMLBooleans($xmlBoolean, $boolean)
  49. {
  50. if ($this->hasDeserializer()) {
  51. $this->assertSame($boolean, $this->deserialize('<result>'.$xmlBoolean.'</result>', 'boolean'));
  52. }
  53. }
  54. public function getXMLBooleans()
  55. {
  56. return array(array('true', true), array('false', false), array('1', true), array('0', false));
  57. }
  58. public function testPropertyIsObjectWithAttributeAndValue()
  59. {
  60. $personCollection = new PersonLocation;
  61. $person = new Person;
  62. $person->name = 'Matthias Noback';
  63. $person->age = 28;
  64. $personCollection->person = $person;
  65. $personCollection->location = 'The Netherlands';
  66. $this->assertEquals($this->getContent('person_location'), $this->serialize($personCollection));
  67. }
  68. public function testPropertyIsCollectionOfObjectsWithAttributeAndValue()
  69. {
  70. $personCollection = new PersonCollection;
  71. $person = new Person;
  72. $person->name = 'Matthias Noback';
  73. $person->age = 28;
  74. $personCollection->persons->add($person);
  75. $personCollection->location = 'The Netherlands';
  76. $this->assertEquals($this->getContent('person_collection'), $this->serialize($personCollection));
  77. }
  78. /**
  79. * @expectedException JMS\Serializer\Exception\InvalidArgumentException
  80. * @expectedExceptionMessage The document type "<!DOCTYPE author [<!ENTITY foo SYSTEM "php://filter/read=convert.base64-encode/resource=XmlSerializationTest.php">]>" is not allowed. If it is safe, you may add it to the whitelist configuration.
  81. */
  82. public function testExternalEntitiesAreDisabledByDefault()
  83. {
  84. if ($this->isBugFixedPhpVersion()){
  85. $this->setExpectedException(
  86. 'JMS\Serializer\Exception\InvalidArgumentException',
  87. 'The document type "<!ENTITY foo SYSTEM "php://filter/read=convert.base64-encode/resource=XmlSerializationTest.php">" is not allowed. If it is safe, you may add it to the whitelist configuration.'
  88. );
  89. }
  90. $this->deserialize('<?xml version="1.0"?>
  91. <!DOCTYPE author [
  92. <!ENTITY foo SYSTEM "php://filter/read=convert.base64-encode/resource='.basename(__FILE__).'">
  93. ]>
  94. <result>
  95. &foo;
  96. </result>', 'stdClass');
  97. }
  98. /**
  99. * @expectedException JMS\Serializer\Exception\InvalidArgumentException
  100. * @expectedExceptionMessage The document type "<!DOCTYPE foo>" is not allowed. If it is safe, you may add it to the whitelist configuration.
  101. */
  102. public function testDocumentTypesAreNotAllowed()
  103. {
  104. if ($this->isBugFixedPhpVersion()) {
  105. $this->setExpectedException(
  106. 'JMS\Serializer\Exception\InvalidArgumentException',
  107. 'The document type "" is not allowed. If it is safe, you may add it to the whitelist configuration.');
  108. }
  109. $this->deserialize('<?xml version="1.0"?><!DOCTYPE foo><foo></foo>', 'stdClass');
  110. }
  111. public function testWhitelistedDocumentTypesAreAllowed()
  112. {
  113. if ($this->isBugFixedPhpVersion()) {
  114. $this->markTestSkipped(sprintf('PHP version %s does not support this behavior', phpversion()));
  115. }
  116. $this->deserializationVisitors->get('xml')->get()->setDoctypeWhitelist(array(
  117. '<!DOCTYPE authorized SYSTEM "http://authorized_url.dtd">',
  118. '<!DOCTYPE author [<!ENTITY foo SYSTEM "php://filter/read=convert.base64-encode/resource='.basename(__FILE__).'">]>'));
  119. $this->serializer->deserialize('<?xml version="1.0"?>
  120. <!DOCTYPE authorized SYSTEM "http://authorized_url.dtd">
  121. <foo></foo>', 'stdClass', 'xml');
  122. $this->serializer->deserialize('<?xml version="1.0"?>
  123. <!DOCTYPE author [
  124. <!ENTITY foo SYSTEM "php://filter/read=convert.base64-encode/resource='.basename(__FILE__).'">
  125. ]>
  126. <foo></foo>', 'stdClass', 'xml');
  127. }
  128. public function testVirtualAttributes()
  129. {
  130. $this->assertEquals(
  131. $this->getContent('virtual_attributes'),
  132. $this->serialize(new ObjectWithVirtualXmlProperties(), SerializationContext::create()->setGroups(array('attributes')))
  133. );
  134. }
  135. public function testVirtualValues()
  136. {
  137. $this->assertEquals(
  138. $this->getContent('virtual_values'),
  139. $this->serialize(new ObjectWithVirtualXmlProperties(), SerializationContext::create()->setGroups(array('values')))
  140. );
  141. }
  142. public function testVirtualXmlList()
  143. {
  144. $this->assertEquals(
  145. $this->getContent('virtual_properties_list'),
  146. $this->serialize(new ObjectWithVirtualXmlProperties(), SerializationContext::create()->setGroups(array('list')))
  147. );
  148. }
  149. public function testVirtualXmlMap()
  150. {
  151. $this->assertEquals(
  152. $this->getContent('virtual_properties_map'),
  153. $this->serialize(new ObjectWithVirtualXmlProperties(), SerializationContext::create()->setGroups(array('map')))
  154. );
  155. }
  156. public function testArrayKeyValues()
  157. {
  158. $this->assertEquals($this->getContent('array_key_values'), $this->serializer->serialize(new ObjectWithXmlKeyValuePairs(), 'xml'));
  159. }
  160. /**
  161. * @dataProvider getDateTime
  162. * @group datetime
  163. */
  164. public function testDateTimeNoCData($key, $value, $type)
  165. {
  166. $handlerRegistry = new HandlerRegistry();
  167. $handlerRegistry->registerSubscribingHandler(new DateHandler(\DateTime::ISO8601, 'UTC', false));
  168. $objectConstructor = new UnserializeObjectConstructor();
  169. $serializer = new Serializer($this->factory, $handlerRegistry, $objectConstructor, $this->serializationVisitors, $this->deserializationVisitors);
  170. $this->assertEquals($this->getContent($key . '_no_cdata'), $serializer->serialize($value, $this->getFormat()));
  171. }
  172. /**
  173. * @expectedException JMS\Serializer\Exception\RuntimeException
  174. * @expectedExceptionMessage Unsupported value type for XML attribute map. Expected array but got object
  175. */
  176. public function testXmlAttributeMapWithoutArray()
  177. {
  178. $attributes = new \ArrayObject(array(
  179. 'type' => 'text',
  180. ));
  181. $this->serializer->serialize(new Input($attributes), $this->getFormat());
  182. }
  183. public function testDeserializingNull()
  184. {
  185. $this->markTestSkipped('Not supported in XML.');
  186. }
  187. public function testObjectWithXmlNamespaces()
  188. {
  189. $object = new ObjectWithXmlNamespaces('This is a nice title.', 'Foo Bar', new \DateTime('2011-07-30 00:00', new \DateTimeZone('UTC')), 'en');
  190. $serialized = $this->serialize($object);
  191. $this->assertEquals($this->getContent('object_with_xml_namespaces'), $this->serialize($object));
  192. $xml = simplexml_load_string($this->serialize($object));
  193. $xml->registerXPathNamespace('ns1', "http://purl.org/dc/elements/1.1/");
  194. $xml->registerXPathNamespace('ns2', "http://schemas.google.com/g/2005");
  195. $xml->registerXPathNamespace('ns3', "http://www.w3.org/2005/Atom");
  196. $this->assertEquals('2011-07-30T00:00:00+0000', $this->xpathFirstToString($xml, './@created_at'));
  197. $this->assertEquals('1edf9bf60a32d89afbb85b2be849e3ceed5f5b10', $this->xpathFirstToString($xml, './@ns2:etag'));
  198. $this->assertEquals('en', $this->xpathFirstToString($xml, './@ns1:language'));
  199. $this->assertEquals('This is a nice title.', $this->xpathFirstToString($xml, './ns1:title'));
  200. $this->assertEquals('Foo Bar', $this->xpathFirstToString($xml, './ns3:author'));
  201. $deserialized = $this->deserialize($this->getContent('object_with_xml_namespacesalias'), get_class($object));
  202. $this->assertEquals('2011-07-30T00:00:00+0000', $this->getField($deserialized, 'createdAt')->format(\DateTime::ISO8601));
  203. $this->assertAttributeEquals('This is a nice title.', 'title', $deserialized);
  204. $this->assertAttributeSame('1edf9bf60a32d89afbb85b2be849e3ceed5f5b10', 'etag', $deserialized);
  205. $this->assertAttributeSame('en', 'language', $deserialized);
  206. $this->assertAttributeEquals('Foo Bar', 'author', $deserialized);
  207. }
  208. public function testObjectWithXmlRootNamespace()
  209. {
  210. $object = new ObjectWithXmlRootNamespace('This is a nice title.', 'Foo Bar', new \DateTime('2011-07-30 00:00', new \DateTimeZone('UTC')), 'en');
  211. $this->assertEquals($this->getContent('object_with_xml_root_namespace'), $this->serialize($object));
  212. }
  213. public function testXmlNamespacesInheritance()
  214. {
  215. $object = new SimpleClassObject();
  216. $object->foo = 'foo';
  217. $object->bar = 'bar';
  218. $object->moo = 'moo';
  219. $this->assertEquals($this->getContent('simple_class_object'), $this->serialize($object));
  220. $childObject = new SimpleSubClassObject();
  221. $childObject->foo = 'foo';
  222. $childObject->bar = 'bar';
  223. $childObject->moo = 'moo';
  224. $childObject->baz = 'baz';
  225. $childObject->qux = 'qux';
  226. $this->assertEquals($this->getContent('simple_subclass_object'), $this->serialize($childObject));
  227. }
  228. private function xpathFirstToString(\SimpleXMLElement $xml, $xpath)
  229. {
  230. $nodes = $xml->xpath($xpath);
  231. return (string) reset($nodes);
  232. }
  233. private function isBugFixedPhpVersion()
  234. {
  235. return (PHP_VERSION_ID >= 50513) || (PHP_VERSION_ID >= 50429 && PHP_VERSION_ID < 50500);
  236. }
  237. /**
  238. * @param string $key
  239. */
  240. protected function getContent($key)
  241. {
  242. if (!file_exists($file = __DIR__.'/xml/'.$key.'.xml')) {
  243. throw new InvalidArgumentException(sprintf('The key "%s" is not supported.', $key));
  244. }
  245. return file_get_contents($file);
  246. }
  247. protected function getFormat()
  248. {
  249. return 'xml';
  250. }
  251. }