XmlSerializationTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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\Serializer\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. $this->deserializationVisitors['xml']->setDoctypeWhitelist(array(
  89. '<!DOCTYPE authorized SYSTEM "http://authorized_url.dtd">',
  90. '<!DOCTYPE author [<!ENTITY foo SYSTEM "php://filter/read=convert.base64-encode/resource='.basename(__FILE__).'">]>'));
  91. $this->serializer->deserialize('<?xml version="1.0"?>
  92. <!DOCTYPE authorized SYSTEM "http://authorized_url.dtd">
  93. <foo></foo>', 'stdClass', 'xml');
  94. $this->serializer->deserialize('<?xml version="1.0"?>
  95. <!DOCTYPE author [
  96. <!ENTITY foo SYSTEM "php://filter/read=convert.base64-encode/resource='.basename(__FILE__).'">
  97. ]>
  98. <foo></foo>', 'stdClass', 'xml');
  99. }
  100. public function testVirtualAttributes()
  101. {
  102. $this->serializer->setGroups(array('attributes'));
  103. $this->assertEquals($this->getContent('virtual_attributes'), $this->serializer->serialize(new ObjectWithVirtualXmlProperties(),'xml'));
  104. }
  105. public function testVirtualValues()
  106. {
  107. $this->serializer->setGroups(array('values'));
  108. $this->assertEquals($this->getContent('virtual_values'), $this->serializer->serialize(new ObjectWithVirtualXmlProperties(),'xml'));
  109. }
  110. public function testVirtualXmlList()
  111. {
  112. $this->serializer->setGroups(array('list'));
  113. $this->assertEquals($this->getContent('virtual_properties_list'), $this->serializer->serialize(new ObjectWithVirtualXmlProperties(),'xml'));
  114. }
  115. public function testVirtualXmlMap()
  116. {
  117. $this->serializer->setGroups(array('map'));
  118. $this->assertEquals($this->getContent('virtual_properties_map'), $this->serializer->serialize(new ObjectWithVirtualXmlProperties(),'xml'));
  119. }
  120. public function testArrayKeyValues()
  121. {
  122. $this->assertEquals($this->getContent('array_key_values'), $this->serializer->serialize(new ObjectWithXmlKeyValuePairs(), 'xml'));
  123. }
  124. /**
  125. * @param string $key
  126. */
  127. protected function getContent($key)
  128. {
  129. if (!file_exists($file = __DIR__.'/xml/'.$key.'.xml')) {
  130. throw new InvalidArgumentException(sprintf('The key "%s" is not supported.', $key));
  131. }
  132. return file_get_contents($file);
  133. }
  134. protected function getFormat()
  135. {
  136. return 'xml';
  137. }
  138. }