PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/formatting/balanceTags.php

https://gitlab.com/Blueprint-Marketing/wordpress-unit-tests
PHP | 234 lines | 166 code | 30 blank | 38 comment | 0 complexity | 82cd88d3673eaf85be95aeb59d0f944b MD5 | raw file
  1. <?php
  2. /**
  3. * @group formatting
  4. */
  5. class Tests_Formatting_BalanceTags extends WP_UnitTestCase {
  6. function nestable_tags() {
  7. return array(
  8. array( 'blockquote', 'div', 'object', 'q', 'span' )
  9. );
  10. }
  11. // This is a complete(?) listing of valid single/self-closing tags.
  12. function single_tags() {
  13. return array(
  14. array( 'area' ), array( 'base' ), array( 'basefont' ), array( 'br' ), array( 'col' ), array( 'command' ),
  15. array( 'embed' ), array( 'frame' ), array( 'hr' ), array( 'img' ), array( 'input' ), array( 'isindex' ),
  16. array( 'link' ), array( 'meta' ), array( 'param' ), array( 'source' ),
  17. );
  18. }
  19. // These are single/self-closing tags that WP has traditionally recognized.
  20. function basic_single_tags() {
  21. return array(
  22. array( 'br' ), array( 'hr' ), array( 'img' ), array( 'input' )
  23. );
  24. }
  25. /**
  26. * These are single tags WP has traditionally properly handled
  27. * This test can be removed if #1597 is fixed and the next test passes, as
  28. * it supercedes this test.
  29. *
  30. * @dataProvider basic_single_tags
  31. */
  32. function test_selfcloses_unclosed_basic_known_single_tags( $tag ) {
  33. $this->assertEquals( "<$tag />", balanceTags( "<$tag>", true ) );
  34. }
  35. /**
  36. * If a recognized valid single tag appears unclosed, it should get self-closed
  37. *
  38. * @ticket 1597
  39. * @dataProvider single_tags
  40. */
  41. function test_selfcloses_unclosed_known_single_tags( $tag ) {
  42. $this->assertEquals( "<$tag />", balanceTags( "<$tag>", true ) );
  43. }
  44. /**
  45. * These are single tags WP has traditionally properly handled
  46. * This test can be removed if #1597 is fixed and the next test passes, as
  47. * it supercedes this test.
  48. *
  49. * @dataProvider basic_single_tags
  50. */
  51. function test_selfcloses_basic_known_single_tags_having_closing_tag( $tag ) {
  52. $this->assertEquals( "<$tag />", balanceTags( "<$tag></$tag>", true ) );
  53. }
  54. /**
  55. * If a recognized valid single tag is given a closing tag, the closing tag
  56. * should get removed and tag should be self-closed.
  57. *
  58. * @ticket 1597
  59. * @dataProvider single_tags
  60. */
  61. function test_selfcloses_known_single_tags_having_closing_tag( $tag ) {
  62. $this->assertEquals( "<$tag />", balanceTags( "<$tag></$tag>", true ) );
  63. }
  64. /**
  65. * @ticket 1597
  66. */
  67. function test_closes_unknown_single_tags_with_closing_tag() {
  68. $inputs = array(
  69. '<strong/>',
  70. '<em />',
  71. '<p class="main1"/>',
  72. '<p class="main2" />',
  73. );
  74. $expected = array(
  75. '<strong></strong>',
  76. '<em></em>',
  77. '<p class="main1"></p>',
  78. '<p class="main2"></p>',
  79. );
  80. foreach ( $inputs as $key => $input ) {
  81. $this->assertEquals( $expected[$key], balanceTags( $inputs[$key], true ) );
  82. }
  83. }
  84. function test_closes_unclosed_single_tags_having_attributes() {
  85. $inputs = array(
  86. '<img src="/images/example.png">',
  87. '<input type="text" name="example">'
  88. );
  89. $expected = array(
  90. '<img src="/images/example.png"/>',
  91. '<input type="text" name="example"/>'
  92. );
  93. foreach ( $inputs as $key => $input ) {
  94. $this->assertEquals( $expected[$key], balanceTags( $inputs[$key], true ) );
  95. }
  96. }
  97. function test_allows_validly_closed_single_tags() {
  98. $inputs = array(
  99. '<br />',
  100. '<hr />',
  101. '<img src="/images/example.png" />',
  102. '<input type="text" name="example" />'
  103. );
  104. foreach ( $inputs as $key => $input ) {
  105. $this->assertEquals( $inputs[$key], balanceTags( $inputs[$key], true ) );
  106. }
  107. }
  108. function test_balances_nestable_tags() {
  109. $inputs = array(
  110. '<q>Test<q>Test</q>',
  111. '<div><div>Test',
  112. '<div>Test</div></div>',
  113. );
  114. $expected = array(
  115. '<q>Test<q>Test</q></q>',
  116. '<div><div>Test</div></div>',
  117. '<div>Test</div>',
  118. );
  119. foreach ( $inputs as $key => $input ) {
  120. $this->assertEquals( $expected[$key], balanceTags( $inputs[$key], true ) );
  121. }
  122. }
  123. function test_allows_adjacent_nestable_tags() {
  124. $inputs = array(
  125. '<blockquote><blockquote>Example quote</blockquote></blockquote>',
  126. '<div class="container"><div>This is allowed></div></div>',
  127. '<span><span><span>Example in spans</span></span></span>',
  128. '<blockquote>Main quote<blockquote>Example quote</blockquote> more text</blockquote>',
  129. '<q><q class="inner-q">Inline quote</q></q>',
  130. );
  131. foreach ( $inputs as $key => $input ) {
  132. $this->assertEquals( $inputs[$key], balanceTags( $inputs[$key], true ) );
  133. }
  134. }
  135. /**
  136. * @ticket 20401
  137. */
  138. function test_allows_immediately_nested_object_tags() {
  139. $object = '<object id="obj1"><param name="param1"/><object id="obj2"><param name="param2"/></object></object>';
  140. $this->assertEquals( $object, balanceTags( $object, true ) );
  141. }
  142. function test_balances_nested_non_nestable_tags() {
  143. $inputs = array(
  144. '<b><b>This is bold</b></b>',
  145. '<b>Some text here <b>This is bold</b></b>',
  146. );
  147. $expected = array(
  148. '<b></b><b>This is bold</b>',
  149. '<b>Some text here </b><b>This is bold</b>',
  150. );
  151. foreach ( $inputs as $key => $input ) {
  152. $this->assertEquals( $expected[$key], balanceTags( $inputs[$key], true ) );
  153. }
  154. }
  155. function test_fixes_improper_closing_tag_sequence() {
  156. $inputs = array(
  157. '<p>Here is a <strong class="part">bold <em>and emphasis</p></em></strong>',
  158. '<ul><li>Aaa</li><li>Bbb</ul></li>',
  159. );
  160. $expected = array(
  161. '<p>Here is a <strong class="part">bold <em>and emphasis</em></strong></p>',
  162. '<ul><li>Aaa</li><li>Bbb</li></ul>',
  163. );
  164. foreach ($inputs as $key => $input) {
  165. $this->assertEquals( $expected[$key], balanceTags( $inputs[$key], true ) );
  166. }
  167. }
  168. function test_adds_missing_closing_tags() {
  169. $inputs = array(
  170. '<b><i>Test</b>',
  171. '<p>Test',
  172. '<p>Test test</em> test</p>',
  173. '</p>Test',
  174. '<p>Here is a <strong class="part">Test</p>',
  175. );
  176. $expected = array(
  177. '<b><i>Test</i></b>',
  178. '<p>Test</p>',
  179. '<p>Test test test</p>',
  180. 'Test',
  181. '<p>Here is a <strong class="part">Test</strong></p>',
  182. );
  183. foreach ( $inputs as $key => $input ) {
  184. $this->assertEquals( $expected[$key], balanceTags( $inputs[$key], true ) );
  185. }
  186. }
  187. function test_removes_extraneous_closing_tags() {
  188. $inputs = array(
  189. '<b>Test</b></b>',
  190. '<div>Test</div></div><div>Test',
  191. '<p>Test test</em> test</p>',
  192. '</p>Test',
  193. );
  194. $expected = array(
  195. '<b>Test</b>',
  196. '<div>Test</div><div>Test</div>',
  197. '<p>Test test test</p>',
  198. 'Test',
  199. );
  200. foreach ( $inputs as $key => $input ) {
  201. $this->assertEquals( $expected[$key], balanceTags( $inputs[$key], true ) );
  202. }
  203. }
  204. }