editor.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * Test the WP_Image_Editor base class
  4. * @group image
  5. * @group media
  6. */
  7. class Tests_Image_Editor extends WP_Image_UnitTestCase {
  8. public $editor_engine = 'WP_Image_Editor_Mock';
  9. /**
  10. * Setup test fixture
  11. */
  12. public function setup() {
  13. require_once( ABSPATH . WPINC . '/class-wp-image-editor.php' );
  14. include_once( DIR_TESTDATA . '/../includes/mock-image-editor.php' );
  15. parent::setUp();
  16. }
  17. /**
  18. * Test wp_get_image_editor() where load returns true
  19. * @ticket 6821
  20. */
  21. public function test_get_editor_load_returns_true() {
  22. $editor = wp_get_image_editor( DIR_TESTDATA . '/images/canola.jpg' );
  23. $this->assertInstanceOf( 'WP_Image_Editor_Mock', $editor );
  24. }
  25. /**
  26. * Test wp_get_image_editor() where load returns false
  27. * @ticket 6821
  28. */
  29. public function test_get_editor_load_returns_false() {
  30. WP_Image_Editor_Mock::$load_return = new WP_Error();
  31. $editor = wp_get_image_editor( DIR_TESTDATA . '/images/canola.jpg' );
  32. $this->assertInstanceOf( 'WP_Error', $editor );
  33. WP_Image_Editor_Mock::$load_return = true;
  34. }
  35. /**
  36. * Test test_quality
  37. * @ticket 6821
  38. */
  39. public function test_set_quality() {
  40. // Get an editor
  41. $editor = wp_get_image_editor( DIR_TESTDATA . '/images/canola.jpg' );
  42. // Make quality readable
  43. $property = new ReflectionProperty( $editor, 'quality' );
  44. $property->setAccessible( true );
  45. // Ensure set_quality works
  46. $this->assertTrue( $editor->set_quality( 75 ) );
  47. $this->assertEquals( 75, $property->getValue( $editor ) );
  48. // Ensure the quality filter works
  49. $func = create_function( '', "return 100;");
  50. add_filter( 'wp_editor_set_quality', $func );
  51. $this->assertTrue( $editor->set_quality( 75 ) );
  52. $this->assertEquals( 100, $property->getValue( $editor ) );
  53. // Clean up
  54. remove_filter( 'wp_editor_set_quality', $func );
  55. }
  56. /**
  57. * Test generate_filename
  58. * @ticket 6821
  59. */
  60. public function test_generate_filename() {
  61. // Get an editor
  62. $editor = wp_get_image_editor( DIR_TESTDATA . '/images/canola.jpg' );
  63. $property = new ReflectionProperty( $editor, 'size' );
  64. $property->setAccessible( true );
  65. $property->setValue( $editor, array(
  66. 'height' => 50,
  67. 'width' => 100
  68. ));
  69. // Test with no parameters
  70. $this->assertEquals( 'canola-100x50.jpg', basename( $editor->generate_filename() ) );
  71. // Test with a suffix only
  72. $this->assertEquals( 'canola-new.jpg', basename( $editor->generate_filename( 'new' ) ) );
  73. // Test with a destination dir only
  74. $this->assertEquals(trailingslashit( realpath( get_temp_dir() ) ), trailingslashit( realpath( dirname( $editor->generate_filename( null, get_temp_dir() ) ) ) ) );
  75. // Test with a suffix only
  76. $this->assertEquals( 'canola-100x50.png', basename( $editor->generate_filename( null, null, 'png' ) ) );
  77. // Combo!
  78. $this->assertEquals( trailingslashit( realpath( get_temp_dir() ) ) . 'canola-new.png', $editor->generate_filename( 'new', realpath( get_temp_dir() ), 'png' ) );
  79. }
  80. /**
  81. * Test get_size
  82. * @ticket 6821
  83. */
  84. public function test_get_size() {
  85. $editor = wp_get_image_editor( DIR_TESTDATA . '/images/canola.jpg' );
  86. // Size should be false by default
  87. $this->assertNull( $editor->get_size() );
  88. // Set a size
  89. $size = array(
  90. 'height' => 50,
  91. 'width' => 100
  92. );
  93. $property = new ReflectionProperty( $editor, 'size' );
  94. $property->setAccessible( true );
  95. $property->setValue( $editor, $size );
  96. $this->assertEquals( $size, $editor->get_size() );
  97. }
  98. /**
  99. * Test get_suffix
  100. * @ticket 6821
  101. */
  102. public function test_get_suffix() {
  103. $editor = wp_get_image_editor( DIR_TESTDATA . '/images/canola.jpg' );
  104. // Size should be false by default
  105. $this->assertFalse( $editor->get_suffix() );
  106. // Set a size
  107. $size = array(
  108. 'height' => 50,
  109. 'width' => 100
  110. );
  111. $property = new ReflectionProperty( $editor, 'size' );
  112. $property->setAccessible( true );
  113. $property->setValue( $editor, $size );
  114. $this->assertEquals( '100x50', $editor->get_suffix() );
  115. }
  116. }