filtering.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. // save and fetch posts to make sure content is properly filtered.
  3. // these tests don't care what code is responsible for filtering or how it is called, just that it happens when a post is saved.
  4. /**
  5. * @group post
  6. * @group formatting
  7. */
  8. class Tests_Post_Filtering extends WP_UnitTestCase {
  9. function setUp() {
  10. parent::setUp();
  11. update_option('use_balanceTags', 1);
  12. kses_init_filters();
  13. }
  14. function tearDown() {
  15. kses_remove_filters();
  16. parent::tearDown();
  17. }
  18. // a simple test to make sure unclosed tags are fixed
  19. function test_post_content_unknown_tag() {
  20. $content = <<<EOF
  21. <foobar>no such tag</foobar>
  22. EOF;
  23. $expected = <<<EOF
  24. no such tag
  25. EOF;
  26. $id = $this->factory->post->create( array( 'post_content' => $content ) );
  27. $post = get_post($id);
  28. $this->assertEquals( $expected, $post->post_content );
  29. }
  30. // a simple test to make sure unbalanced tags are fixed
  31. function test_post_content_unbalanced_tag() {
  32. $content = <<<EOF
  33. <i>italics
  34. EOF;
  35. $expected = <<<EOF
  36. <i>italics</i>
  37. EOF;
  38. $id = $this->factory->post->create( array( 'post_content' => $content ) );
  39. $post = get_post($id);
  40. $this->assertEquals( $expected, $post->post_content );
  41. }
  42. // test kses filtering of disallowed attribute
  43. function test_post_content_disallowed_attr() {
  44. $content = <<<EOF
  45. <img src='foo' width='500' href='shlorp' />
  46. EOF;
  47. $expected = <<<EOF
  48. <img src='foo' width='500' />
  49. EOF;
  50. $id = $this->factory->post->create( array( 'post_content' => $content ) );
  51. $post = get_post($id);
  52. $this->assertEquals( $expected, $post->post_content );
  53. }
  54. /**
  55. * test kses bug. xhtml does not require space before closing empty element
  56. * @ticket 12394
  57. */
  58. function test_post_content_xhtml_empty_elem() {
  59. $content = <<<EOF
  60. <img src='foo' width='500' height='300'/>
  61. EOF;
  62. $expected = <<<EOF
  63. <img src='foo' width='500' height='300' />
  64. EOF;
  65. $id = $this->factory->post->create( array( 'post_content' => $content ) );
  66. $post = get_post($id);
  67. $this->assertEquals( $expected, $post->post_content );
  68. }
  69. /**
  70. * make sure unbalanced tags are fixed when they span a --more-- tag
  71. * @ticket 6297
  72. */
  73. function test_post_content_unbalanced_more() {
  74. $content = <<<EOF
  75. <em>some text<!--more-->
  76. that's continued after the jump</em>
  77. EOF;
  78. $expected = <<<EOF
  79. <em>some text</em><!--more-->
  80. that's continued after the jump
  81. EOF;
  82. $id = $this->factory->post->create( array( 'post_content' => $content ) );
  83. $post = get_post($id);
  84. $this->assertEquals( $expected, $post->post_content );
  85. }
  86. /**
  87. * make sure unbalanced tags are fixed when they span a --nextpage-- tag
  88. * @ticket 6297
  89. */
  90. function test_post_content_unbalanced_nextpage() {
  91. $content = <<<EOF
  92. <em>some text<!--nextpage-->
  93. that's continued after the jump</em>
  94. EOF;
  95. $expected = <<<EOF
  96. <em>some text</em><!--nextpage-->
  97. that's continued after the jump
  98. EOF;
  99. $id = $this->factory->post->create( array( 'post_content' => $content ) );
  100. $post = get_post($id);
  101. $this->assertEquals( $expected, $post->post_content );
  102. }
  103. /**
  104. * make sure unbalanced tags are fixed when they span both --more-- and --nextpage-- tags (in that order)
  105. * @ticket 6297
  106. */
  107. function test_post_content_unbalanced_more_nextpage() {
  108. $content = <<<EOF
  109. <em>some text<!--more-->
  110. that's continued after the jump</em>
  111. <!--nextpage-->
  112. <p>and the next page
  113. <!--nextpage-->
  114. breaks the graf</p>
  115. EOF;
  116. $expected = <<<EOF
  117. <em>some text</em><!--more-->
  118. that's continued after the jump
  119. <!--nextpage-->
  120. <p>and the next page
  121. </p><!--nextpage-->
  122. breaks the graf
  123. EOF;
  124. $id = $this->factory->post->create( array( 'post_content' => $content ) );
  125. $post = get_post($id);
  126. $this->assertEquals( $expected, $post->post_content );
  127. }
  128. /**
  129. * make sure unbalanced tags are fixed when they span both --nextpage-- and --more-- tags (in that order)
  130. * @ticket 6297
  131. */
  132. function test_post_content_unbalanced_nextpage_more() {
  133. $content = <<<EOF
  134. <em>some text<!--nextpage-->
  135. that's continued after the jump</em>
  136. <!--more-->
  137. <p>and the next page
  138. <!--nextpage-->
  139. breaks the graf</p>
  140. EOF;
  141. $expected = <<<EOF
  142. <em>some text</em><!--nextpage-->
  143. that's continued after the jump
  144. <!--more-->
  145. <p>and the next page
  146. </p><!--nextpage-->
  147. breaks the graf
  148. EOF;
  149. $id = $this->factory->post->create( array( 'post_content' => $content ) );
  150. $post = get_post($id);
  151. $this->assertEquals( $expected, $post->post_content );
  152. }
  153. // make sure unbalanced tags are untouched when the balance option is off
  154. function test_post_content_nobalance_nextpage_more() {
  155. update_option('use_balanceTags', 0);
  156. $content = <<<EOF
  157. <em>some text<!--nextpage-->
  158. that's continued after the jump</em>
  159. <!--more-->
  160. <p>and the next page
  161. <!--nextpage-->
  162. breaks the graf</p>
  163. EOF;
  164. $id = $this->factory->post->create( array( 'post_content' => $content ) );
  165. $post = get_post($id);
  166. $this->assertEquals( $content, $post->post_content );
  167. }
  168. }