addFilter.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. /**
  3. * Test the add_filter method of WP_Hook
  4. *
  5. * @group hooks
  6. * @covers WP_Hook::add_filter
  7. */
  8. class Tests_Hooks_AddFilter extends WP_UnitTestCase {
  9. public $hook;
  10. public function test_add_filter_with_function() {
  11. $callback = '__return_null';
  12. $hook = new WP_Hook();
  13. $tag = __FUNCTION__;
  14. $priority = rand( 1, 100 );
  15. $accepted_args = rand( 1, 100 );
  16. $hook->add_filter( $tag, $callback, $priority, $accepted_args );
  17. $function_index = _wp_filter_build_unique_id( $tag, $callback, $priority );
  18. $this->assertSame( $callback, $hook->callbacks[ $priority ][ $function_index ]['function'] );
  19. $this->assertSame( $accepted_args, $hook->callbacks[ $priority ][ $function_index ]['accepted_args'] );
  20. }
  21. public function test_add_filter_with_object() {
  22. $a = new MockAction();
  23. $callback = array( $a, 'action' );
  24. $hook = new WP_Hook();
  25. $tag = __FUNCTION__;
  26. $priority = rand( 1, 100 );
  27. $accepted_args = rand( 1, 100 );
  28. $hook->add_filter( $tag, $callback, $priority, $accepted_args );
  29. $function_index = _wp_filter_build_unique_id( $tag, $callback, $priority );
  30. $this->assertSame( $callback, $hook->callbacks[ $priority ][ $function_index ]['function'] );
  31. $this->assertSame( $accepted_args, $hook->callbacks[ $priority ][ $function_index ]['accepted_args'] );
  32. }
  33. public function test_add_filter_with_static_method() {
  34. $callback = array( 'MockAction', 'action' );
  35. $hook = new WP_Hook();
  36. $tag = __FUNCTION__;
  37. $priority = rand( 1, 100 );
  38. $accepted_args = rand( 1, 100 );
  39. $hook->add_filter( $tag, $callback, $priority, $accepted_args );
  40. $function_index = _wp_filter_build_unique_id( $tag, $callback, $priority );
  41. $this->assertSame( $callback, $hook->callbacks[ $priority ][ $function_index ]['function'] );
  42. $this->assertSame( $accepted_args, $hook->callbacks[ $priority ][ $function_index ]['accepted_args'] );
  43. }
  44. public function test_add_two_filters_with_same_priority() {
  45. $callback_one = '__return_null';
  46. $callback_two = '__return_false';
  47. $hook = new WP_Hook();
  48. $tag = __FUNCTION__;
  49. $priority = rand( 1, 100 );
  50. $accepted_args = rand( 1, 100 );
  51. $hook->add_filter( $tag, $callback_one, $priority, $accepted_args );
  52. $this->assertCount( 1, $hook->callbacks[ $priority ] );
  53. $hook->add_filter( $tag, $callback_two, $priority, $accepted_args );
  54. $this->assertCount( 2, $hook->callbacks[ $priority ] );
  55. }
  56. public function test_add_two_filters_with_different_priority() {
  57. $callback_one = '__return_null';
  58. $callback_two = '__return_false';
  59. $hook = new WP_Hook();
  60. $tag = __FUNCTION__;
  61. $priority = rand( 1, 100 );
  62. $accepted_args = rand( 1, 100 );
  63. $hook->add_filter( $tag, $callback_one, $priority, $accepted_args );
  64. $this->assertCount( 1, $hook->callbacks[ $priority ] );
  65. $hook->add_filter( $tag, $callback_two, $priority + 1, $accepted_args );
  66. $this->assertCount( 1, $hook->callbacks[ $priority ] );
  67. $this->assertCount( 1, $hook->callbacks[ $priority + 1 ] );
  68. }
  69. public function test_readd_filter() {
  70. $callback = '__return_null';
  71. $hook = new WP_Hook();
  72. $tag = __FUNCTION__;
  73. $priority = rand( 1, 100 );
  74. $accepted_args = rand( 1, 100 );
  75. $hook->add_filter( $tag, $callback, $priority, $accepted_args );
  76. $this->assertCount( 1, $hook->callbacks[ $priority ] );
  77. $hook->add_filter( $tag, $callback, $priority, $accepted_args );
  78. $this->assertCount( 1, $hook->callbacks[ $priority ] );
  79. }
  80. public function test_readd_filter_with_different_priority() {
  81. $callback = '__return_null';
  82. $hook = new WP_Hook();
  83. $tag = __FUNCTION__;
  84. $priority = rand( 1, 100 );
  85. $accepted_args = rand( 1, 100 );
  86. $hook->add_filter( $tag, $callback, $priority, $accepted_args );
  87. $this->assertCount( 1, $hook->callbacks[ $priority ] );
  88. $hook->add_filter( $tag, $callback, $priority + 1, $accepted_args );
  89. $this->assertCount( 1, $hook->callbacks[ $priority ] );
  90. $this->assertCount( 1, $hook->callbacks[ $priority + 1 ] );
  91. }
  92. public function test_sort_after_add_filter() {
  93. $a = new MockAction();
  94. $b = new MockAction();
  95. $c = new MockAction();
  96. $hook = new WP_Hook();
  97. $tag = __FUNCTION__;
  98. $hook->add_filter( $tag, array( $a, 'action' ), 10, 1 );
  99. $hook->add_filter( $tag, array( $b, 'action' ), 5, 1 );
  100. $hook->add_filter( $tag, array( $c, 'action' ), 8, 1 );
  101. $this->assertSame( array( 5, 8, 10 ), array_keys( $hook->callbacks ) );
  102. }
  103. public function test_remove_and_add() {
  104. $this->hook = new Wp_Hook();
  105. $this->hook->add_filter( 'remove_and_add', '__return_empty_string', 10, 0 );
  106. $this->hook->add_filter( 'remove_and_add', array( $this, '_filter_remove_and_add2' ), 11, 1 );
  107. $this->hook->add_filter( 'remove_and_add', array( $this, '_filter_remove_and_add4' ), 12, 1 );
  108. $value = $this->hook->apply_filters( '', array() );
  109. $this->assertSame( '24', $value );
  110. }
  111. public function test_remove_and_add_last_filter() {
  112. $this->hook = new Wp_Hook();
  113. $this->hook->add_filter( 'remove_and_add', '__return_empty_string', 10, 0 );
  114. $this->hook->add_filter( 'remove_and_add', array( $this, '_filter_remove_and_add1' ), 11, 1 );
  115. $this->hook->add_filter( 'remove_and_add', array( $this, '_filter_remove_and_add2' ), 12, 1 );
  116. $value = $this->hook->apply_filters( '', array() );
  117. $this->assertSame( '12', $value );
  118. }
  119. public function test_remove_and_recurse_and_add() {
  120. $this->hook = new Wp_Hook();
  121. $this->hook->add_filter( 'remove_and_add', '__return_empty_string', 10, 0 );
  122. $this->hook->add_filter( 'remove_and_add', array( $this, '_filter_remove_and_add1' ), 11, 1 );
  123. $this->hook->add_filter( 'remove_and_add', array( $this, '_filter_remove_and_recurse_and_add2' ), 11, 1 );
  124. $this->hook->add_filter( 'remove_and_add', array( $this, '_filter_remove_and_add3' ), 11, 1 );
  125. $this->hook->add_filter( 'remove_and_add', array( $this, '_filter_remove_and_add4' ), 12, 1 );
  126. $value = $this->hook->apply_filters( '', array() );
  127. $this->assertSame( '1-134-234', $value );
  128. }
  129. public function _filter_remove_and_add1( $string ) {
  130. return $string . '1';
  131. }
  132. public function _filter_remove_and_add2( $string ) {
  133. $this->hook->remove_filter( 'remove_and_add', array( $this, '_filter_remove_and_add2' ), 11 );
  134. $this->hook->add_filter( 'remove_and_add', array( $this, '_filter_remove_and_add2' ), 11, 1 );
  135. return $string . '2';
  136. }
  137. public function _filter_remove_and_recurse_and_add2( $string ) {
  138. $this->hook->remove_filter( 'remove_and_add', array( $this, '_filter_remove_and_recurse_and_add2' ), 11 );
  139. $string .= '-' . $this->hook->apply_filters( '', array() ) . '-';
  140. $this->hook->add_filter( 'remove_and_add', array( $this, '_filter_remove_and_recurse_and_add2' ), 11, 1 );
  141. return $string . '2';
  142. }
  143. public function _filter_remove_and_add3( $string ) {
  144. return $string . '3';
  145. }
  146. public function _filter_remove_and_add4( $string ) {
  147. return $string . '4';
  148. }
  149. public function test_remove_and_add_action() {
  150. $this->hook = new Wp_Hook();
  151. $this->action_output = '';
  152. $this->hook->add_filter( 'remove_and_add_action', '__return_empty_string', 10, 0 );
  153. $this->hook->add_filter( 'remove_and_add_action', array( $this, '_action_remove_and_add2' ), 11, 0 );
  154. $this->hook->add_filter( 'remove_and_add_action', array( $this, '_action_remove_and_add4' ), 12, 0 );
  155. $this->hook->do_action( array() );
  156. $this->assertSame( '24', $this->action_output );
  157. }
  158. public function test_remove_and_add_last_action() {
  159. $this->hook = new Wp_Hook();
  160. $this->action_output = '';
  161. $this->hook->add_filter( 'remove_and_add_action', '__return_empty_string', 10, 0 );
  162. $this->hook->add_filter( 'remove_and_add_action', array( $this, '_action_remove_and_add1' ), 11, 0 );
  163. $this->hook->add_filter( 'remove_and_add_action', array( $this, '_action_remove_and_add2' ), 12, 0 );
  164. $this->hook->do_action( array() );
  165. $this->assertSame( '12', $this->action_output );
  166. }
  167. public function test_remove_and_recurse_and_add_action() {
  168. $this->hook = new Wp_Hook();
  169. $this->action_output = '';
  170. $this->hook->add_filter( 'remove_and_add_action', '__return_empty_string', 10, 0 );
  171. $this->hook->add_filter( 'remove_and_add_action', array( $this, '_action_remove_and_add1' ), 11, 0 );
  172. $this->hook->add_filter( 'remove_and_add_action', array( $this, '_action_remove_and_recurse_and_add2' ), 11, 0 );
  173. $this->hook->add_filter( 'remove_and_add_action', array( $this, '_action_remove_and_add3' ), 11, 0 );
  174. $this->hook->add_filter( 'remove_and_add_action', array( $this, '_action_remove_and_add4' ), 12, 0 );
  175. $this->hook->do_action( array() );
  176. $this->assertSame( '1-134-234', $this->action_output );
  177. }
  178. public function _action_remove_and_add1() {
  179. $this->action_output .= 1;
  180. }
  181. public function _action_remove_and_add2() {
  182. $this->hook->remove_filter( 'remove_and_add_action', array( $this, '_action_remove_and_add2' ), 11 );
  183. $this->hook->add_filter( 'remove_and_add_action', array( $this, '_action_remove_and_add2' ), 11, 0 );
  184. $this->action_output .= '2';
  185. }
  186. public function _action_remove_and_recurse_and_add2() {
  187. $this->hook->remove_filter( 'remove_and_add_action', array( $this, '_action_remove_and_recurse_and_add2' ), 11 );
  188. $this->action_output .= '-';
  189. $this->hook->do_action( array() );
  190. $this->action_output .= '-';
  191. $this->hook->add_filter( 'remove_and_add_action', array( $this, '_action_remove_and_recurse_and_add2' ), 11, 0 );
  192. $this->action_output .= '2';
  193. }
  194. public function _action_remove_and_add3() {
  195. $this->action_output .= '3';
  196. }
  197. public function _action_remove_and_add4() {
  198. $this->action_output .= '4';
  199. }
  200. }