hasFilter.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * Test the has_filter method of WP_Hook
  4. *
  5. * @group hooks
  6. * @covers WP_Hook::has_filter
  7. */
  8. class Tests_Hooks_HasFilter extends WP_UnitTestCase {
  9. public function test_has_filter_with_function() {
  10. $callback = '__return_null';
  11. $hook = new WP_Hook();
  12. $tag = __FUNCTION__;
  13. $priority = rand( 1, 100 );
  14. $accepted_args = rand( 1, 100 );
  15. $hook->add_filter( $tag, $callback, $priority, $accepted_args );
  16. $this->assertSame( $priority, $hook->has_filter( $tag, $callback ) );
  17. }
  18. public function test_has_filter_with_object() {
  19. $a = new MockAction();
  20. $callback = array( $a, 'action' );
  21. $hook = new WP_Hook();
  22. $tag = __FUNCTION__;
  23. $priority = rand( 1, 100 );
  24. $accepted_args = rand( 1, 100 );
  25. $hook->add_filter( $tag, $callback, $priority, $accepted_args );
  26. $this->assertSame( $priority, $hook->has_filter( $tag, $callback ) );
  27. }
  28. public function test_has_filter_with_static_method() {
  29. $callback = array( 'MockAction', 'action' );
  30. $hook = new WP_Hook();
  31. $tag = __FUNCTION__;
  32. $priority = rand( 1, 100 );
  33. $accepted_args = rand( 1, 100 );
  34. $hook->add_filter( $tag, $callback, $priority, $accepted_args );
  35. $this->assertSame( $priority, $hook->has_filter( $tag, $callback ) );
  36. }
  37. public function test_has_filter_without_callback() {
  38. $callback = '__return_null';
  39. $hook = new WP_Hook();
  40. $tag = __FUNCTION__;
  41. $priority = rand( 1, 100 );
  42. $accepted_args = rand( 1, 100 );
  43. $hook->add_filter( $tag, $callback, $priority, $accepted_args );
  44. $this->assertTrue( $hook->has_filter() );
  45. }
  46. public function test_not_has_filter_without_callback() {
  47. $hook = new WP_Hook();
  48. $this->assertFalse( $hook->has_filter() );
  49. }
  50. public function test_not_has_filter_with_callback() {
  51. $callback = '__return_null';
  52. $hook = new WP_Hook();
  53. $tag = __FUNCTION__;
  54. $this->assertFalse( $hook->has_filter( $tag, $callback ) );
  55. }
  56. public function test_has_filter_with_wrong_callback() {
  57. $callback = '__return_null';
  58. $hook = new WP_Hook();
  59. $tag = __FUNCTION__;
  60. $priority = rand( 1, 100 );
  61. $accepted_args = rand( 1, 100 );
  62. $hook->add_filter( $tag, $callback, $priority, $accepted_args );
  63. $this->assertFalse( $hook->has_filter( $tag, '__return_false' ) );
  64. }
  65. }