XmlSerializationTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /*
  3. * Copyright 2011 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\SerializerBundle\Tests\Serializer;
  18. use JMS\SerializerBundle\EventDispatcher\EventDispatcher;
  19. use Metadata\MetadataFactory;
  20. use Doctrine\Common\Annotations\AnnotationReader;
  21. use JMS\SerializerBundle\Tests\Fixtures\InvalidUsageOfXmlValue;
  22. use JMS\SerializerBundle\Serializer\Construction\UnserializeObjectConstructor;
  23. use JMS\SerializerBundle\Serializer\Naming\CamelCaseNamingStrategy;
  24. use JMS\SerializerBundle\Serializer\Naming\SerializedNameAnnotationStrategy;
  25. use JMS\SerializerBundle\Serializer\XmlDeserializationVisitor;
  26. use JMS\SerializerBundle\Metadata\Driver\AnnotationDriver;
  27. use JMS\SerializerBundle\Serializer\Serializer;
  28. use JMS\SerializerBundle\Exception\InvalidArgumentException;
  29. use JMS\SerializerBundle\Tests\Fixtures\PersonCollection;
  30. use JMS\SerializerBundle\Tests\Fixtures\PersonLocation;
  31. use JMS\SerializerBundle\Tests\Fixtures\Person;
  32. use JMS\SerializerBundle\Tests\Fixtures\ObjectWithVirtualXmlProperties;
  33. use JMS\SerializerBundle\Tests\Fixtures\ObjectWithXmlKeyValuePairs;
  34. class XmlSerializationTest extends BaseSerializationTest
  35. {
  36. /**
  37. * @expectedException \RuntimeException
  38. */
  39. public function testInvalidUsageOfXmlValue()
  40. {
  41. $obj = new InvalidUsageOfXmlValue();
  42. $this->serialize($obj);
  43. }
  44. public function testPropertyIsObjectWithAttributeAndValue()
  45. {
  46. $personCollection = new PersonLocation;
  47. $person = new Person;
  48. $person->name = 'Matthias Noback';
  49. $person->age = 28;
  50. $personCollection->person = $person;
  51. $personCollection->location = 'The Netherlands';
  52. $this->assertEquals($this->getContent('person_location'), $this->serialize($personCollection));
  53. }
  54. public function testPropertyIsCollectionOfObjectsWithAttributeAndValue()
  55. {
  56. $personCollection = new PersonCollection;
  57. $person = new Person;
  58. $person->name = 'Matthias Noback';
  59. $person->age = 28;
  60. $personCollection->persons->add($person);
  61. $personCollection->location = 'The Netherlands';
  62. $this->assertEquals($this->getContent('person_collection'), $this->serialize($personCollection));
  63. }
  64. /**
  65. * @expectedException \InvalidArgumentException
  66. * @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.
  67. */
  68. public function testExternalEntitiesAreDisabledByDefault()
  69. {
  70. $this->deserialize('<?xml version="1.0"?>
  71. <!DOCTYPE author [
  72. <!ENTITY foo SYSTEM "php://filter/read=convert.base64-encode/resource='.basename(__FILE__).'">
  73. ]>
  74. <result>
  75. &foo;
  76. </result>', 'stdClass');
  77. }
  78. /**
  79. * @expectedException \InvalidArgumentException
  80. * @expectedExceptionMessage The document type "<!DOCTYPE foo>" is not allowed. If it is safe, you may add it to the whitelist configuration.
  81. */
  82. public function testDocumentTypesAreNotAllowed()
  83. {
  84. $this->deserialize('<?xml version="1.0"?><!DOCTYPE foo><foo></foo>', 'stdClass');
  85. }
  86. public function testWhitelistedDocumentTypesAreAllowed()
  87. {
  88. $xmlVisitor = new XmlDeserializationVisitor(
  89. new SerializedNameAnnotationStrategy(new CamelCaseNamingStrategy()),
  90. $this->getDeserializationHandlers(),
  91. new UnserializeObjectConstructor()
  92. );
  93. $xmlVisitor->setDoctypeWhitelist(array(
  94. '<!DOCTYPE authorized SYSTEM "http://authorized_url.dtd">',
  95. '<!DOCTYPE author [<!ENTITY foo SYSTEM "php://filter/read=convert.base64-encode/resource='.basename(__FILE__).'">]>'));
  96. $serializer = new Serializer(new MetadataFactory(new AnnotationDriver(new AnnotationReader())), new EventDispatcher(), array(), array('xml' => $xmlVisitor));
  97. $serializer->deserialize('<?xml version="1.0"?>
  98. <!DOCTYPE authorized SYSTEM "http://authorized_url.dtd">
  99. <foo></foo>', 'stdClass', 'xml');
  100. $serializer->deserialize('<?xml version="1.0"?>
  101. <!DOCTYPE author [
  102. <!ENTITY foo SYSTEM "php://filter/read=convert.base64-encode/resource='.basename(__FILE__).'">
  103. ]>
  104. <foo></foo>', 'stdClass', 'xml');
  105. }
  106. public function testVirtualAttributes()
  107. {
  108. $serializer = $this->getSerializer();
  109. $serializer->setGroups(array('attributes'));
  110. $this->assertEquals($this->getContent('virtual_attributes'), $serializer->serialize(new ObjectWithVirtualXmlProperties(),'xml'));
  111. }
  112. public function testVirtualValues()
  113. {
  114. $serializer = $this->getSerializer();
  115. $serializer->setGroups(array('values'));
  116. $this->assertEquals($this->getContent('virtual_values'), $serializer->serialize(new ObjectWithVirtualXmlProperties(),'xml'));
  117. }
  118. public function testVirtualXmlList()
  119. {
  120. $serializer = $this->getSerializer();
  121. $serializer->setGroups(array('list'));
  122. $this->assertEquals($this->getContent('virtual_properties_list'), $serializer->serialize(new ObjectWithVirtualXmlProperties(),'xml'));
  123. }
  124. public function testVirtualXmlMap()
  125. {
  126. $serializer = $this->getSerializer();
  127. $serializer->setGroups(array('map'));
  128. $this->assertEquals($this->getContent('virtual_properties_map'), $serializer->serialize(new ObjectWithVirtualXmlProperties(),'xml'));
  129. }
  130. public function testArrayKeyValues()
  131. {
  132. $serializer = $this->getSerializer();
  133. $this->assertEquals($this->getContent('array_key_values'), $serializer->serialize(new ObjectWithXmlKeyValuePairs(), 'xml'));
  134. }
  135. /**
  136. * @param string $key
  137. */
  138. protected function getContent($key)
  139. {
  140. if (!file_exists($file = __DIR__.'/xml/'.$key.'.xml')) {
  141. throw new InvalidArgumentException(sprintf('The key "%s" is not supported.', $key));
  142. }
  143. return file_get_contents($file);
  144. }
  145. protected function getFormat()
  146. {
  147. return 'xml';
  148. }
  149. }