deprecated.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /**
  3. * Test cases for deprecated functions, arguments, and files
  4. *
  5. * @package WordPress
  6. * @subpackage Unit Tests
  7. * @since 3.5
  8. * @group deprecated
  9. */
  10. class Test_Functions_Deprecated extends WP_UnitTestCase {
  11. /**
  12. * List of functions that have been passed through _deprecated_function()
  13. * @var string[]
  14. */
  15. protected $_deprecated_functions = array();
  16. /**
  17. * List of arguments that have been passed through _deprecated_argument()
  18. * @var string[]
  19. */
  20. protected $_deprecated_arguments = array();
  21. /**
  22. * List of files that have been passed through _deprecated_file()
  23. * @var string[]
  24. */
  25. protected $_deprecated_files = array();
  26. /**
  27. * Set up the test fixture
  28. * @return void
  29. */
  30. public function setUp() {
  31. parent::setUp();
  32. $this->_deprecated_functions = array();
  33. $this->_deprecated_arguments = array();
  34. $this->_deprecated_files = array();
  35. add_action( 'deprecated_function_run' , array( $this, 'deprecated_function' ), 10, 3 );
  36. add_action( 'deprecated_function_trigger_error', '__return_false' );
  37. add_action( 'deprecated_argument_run' , array( $this, 'deprecated_argument' ), 10, 3 );
  38. add_action( 'deprecated_argument_trigger_error', '__return_false' );
  39. add_action( 'deprecated_file_included' , array( $this, 'deprecated_file' ), 10, 4 );
  40. add_action( 'deprecated_file_trigger_error', '__return_false' );
  41. }
  42. /**
  43. * Tear down the test fixture
  44. * @return void
  45. */
  46. public function teardown() {
  47. remove_action( 'deprecated_function_run' , array( $this, 'deprecated_function' ), 10, 3 );
  48. remove_action( 'deprecated_function_trigger_error', '__return_false' );
  49. remove_action( 'deprecated_argument_run' , array( $this, 'deprecated_argument' ), 10, 3 );
  50. remove_action( 'deprecated_argument_trigger_error', '__return_false' );
  51. remove_action( 'deprecated_file_included' , array( $this, 'deprecated_argument' ), 10, 4 );
  52. remove_action( 'deprecated_file_trigger_error', '__return_false' );
  53. parent::tearDown();
  54. }
  55. /**
  56. * Catch functions that have passed through _deprecated_function
  57. * @param string $function
  58. * @param string $replacement
  59. * @param float $version
  60. * @return void
  61. */
  62. public function deprecated_function( $function, $replacement, $version ) {
  63. $this->_deprecated_functions[] = array(
  64. 'function' => $function,
  65. 'replacement' => $replacement,
  66. 'version' => $version
  67. );
  68. }
  69. /**
  70. * Catch arguments that have passed through _deprecated_argument
  71. * @param string $argument
  72. * @param string $message
  73. * @param float $version
  74. * @return void
  75. */
  76. public function deprecated_argument( $argument, $message, $version ) {
  77. $this->_deprecated_arguments[] = array(
  78. 'argument' => $argument,
  79. 'message' => $message,
  80. 'version' => $version
  81. );
  82. }
  83. /**
  84. * Catch arguments that have passed through _deprecated_argument
  85. * @param string $argument
  86. * @param string $message
  87. * @param float $version
  88. * @return void
  89. */
  90. public function deprecated_file( $file, $version, $replacement, $message ) {
  91. $this->_deprecated_files[] = array(
  92. 'file' => $file,
  93. 'version' => $version,
  94. 'replacement' => $replacement,
  95. 'message' => $message
  96. );
  97. }
  98. /**
  99. * Check if something was deprecated
  100. * @param string $type argument|function|file
  101. * @param string $name
  102. * @return array|false
  103. */
  104. protected function was_deprecated( $type, $name ) {
  105. switch ( $type ) {
  106. case 'argument' :
  107. $search = $this->_deprecated_arguments;
  108. $key = 'argument';
  109. break;
  110. case 'function' :
  111. $search = $this->_deprecated_functions;
  112. $key = 'function';
  113. break;
  114. default :
  115. $search = $this->_deprecated_files;
  116. $key = 'file';
  117. }
  118. foreach ( $search as $v ) {
  119. if ( $name == $v[$key] ) {
  120. return $v;
  121. }
  122. }
  123. return false;
  124. }
  125. /**
  126. * Test that wp_save_image_file has a deprecated argument when passed a GD resource
  127. * @ticket 6821
  128. * @expectedDeprecated wp_save_image_file
  129. */
  130. public function test_wp_save_image_file_deprecated_with_gd_resource() {
  131. if ( !function_exists( 'imagejpeg' ) )
  132. $this->markTestSkipped( 'jpeg support unavailable' );
  133. // Call wp_save_image_file
  134. include_once( ABSPATH . 'wp-admin/includes/image-edit.php' );
  135. $file = wp_tempnam();
  136. $img = imagecreatefromjpeg( DIR_TESTDATA . '/images/canola.jpg' );
  137. wp_save_image_file( $file, $img, 'image/jpeg', 1 );
  138. imagedestroy( $img );
  139. @unlink($file);
  140. // Check if the arg was deprecated
  141. $check = $this->was_deprecated( 'argument', 'wp_save_image_file' );
  142. $this->assertNotEmpty( $check );
  143. }
  144. /**
  145. * Test that wp_save_image_file doesn't have a deprecated argument when passed a WP_Image_Editor
  146. * @ticket 6821
  147. */
  148. public function test_wp_save_image_file_not_deprecated_with_wp_image_editor() {
  149. if ( !function_exists( 'imagejpeg' ) )
  150. $this->markTestSkipped( 'jpeg support unavailable' );
  151. // Call wp_save_image_file
  152. include_once( ABSPATH . 'wp-admin/includes/image-edit.php' );
  153. $file = wp_tempnam();
  154. $img = wp_get_image_editor( DIR_TESTDATA . '/images/canola.jpg' );
  155. wp_save_image_file( $file, $img, 'image/jpeg', 1 );
  156. unset( $img );
  157. @unlink($file);
  158. // Check if the arg was deprecated
  159. $check = $this->was_deprecated( 'argument', 'wp_save_image_file' );
  160. $this->assertFalse( $check );
  161. }
  162. }