upload.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * @group upload
  4. * @group media
  5. */
  6. class Tests_Upload extends WP_UnitTestCase {
  7. public $siteurl;
  8. function setUp() {
  9. parent::setUp();
  10. $this->_reset_options();
  11. }
  12. function _reset_options() {
  13. // System defaults.
  14. update_option( 'upload_path', 'wp-content/uploads' );
  15. update_option( 'upload_url_path', '' );
  16. update_option( 'uploads_use_yearmonth_folders', 1 );
  17. }
  18. function test_upload_dir_default() {
  19. // wp_upload_dir() with default parameters.
  20. $info = wp_upload_dir();
  21. $subdir = gmstrftime( '/%Y/%m' );
  22. $this->assertSame( get_option( 'siteurl' ) . '/wp-content/uploads' . $subdir, $info['url'] );
  23. $this->assertSame( ABSPATH . 'wp-content/uploads' . $subdir, $info['path'] );
  24. $this->assertSame( $subdir, $info['subdir'] );
  25. $this->assertFalse( $info['error'] );
  26. }
  27. function test_upload_dir_relative() {
  28. // wp_upload_dir() with a relative upload path that is not 'wp-content/uploads'.
  29. update_option( 'upload_path', 'foo/bar' );
  30. $info = _wp_upload_dir();
  31. $subdir = gmstrftime( '/%Y/%m' );
  32. $this->assertSame( get_option( 'siteurl' ) . '/foo/bar' . $subdir, $info['url'] );
  33. $this->assertSame( ABSPATH . 'foo/bar' . $subdir, $info['path'] );
  34. $this->assertSame( $subdir, $info['subdir'] );
  35. $this->assertFalse( $info['error'] );
  36. }
  37. /**
  38. * @ticket 5953
  39. */
  40. function test_upload_dir_absolute() {
  41. $path = get_temp_dir() . 'wp-unit-test';
  42. // wp_upload_dir() with an absolute upload path.
  43. update_option( 'upload_path', $path );
  44. // Doesn't make sense to use an absolute file path without setting the url path.
  45. update_option( 'upload_url_path', '/baz' );
  46. // Use `_wp_upload_dir()` directly to bypass caching and work with the changed options.
  47. // It doesn't create the /year/month directories.
  48. $info = _wp_upload_dir();
  49. $subdir = gmstrftime( '/%Y/%m' );
  50. $this->assertSame( '/baz' . $subdir, $info['url'] );
  51. $this->assertSame( $path . $subdir, $info['path'] );
  52. $this->assertSame( $subdir, $info['subdir'] );
  53. $this->assertFalse( $info['error'] );
  54. }
  55. function test_upload_dir_no_yearnum() {
  56. update_option( 'uploads_use_yearmonth_folders', 0 );
  57. // Use `_wp_upload_dir()` directly to bypass caching and work with the changed options.
  58. $info = _wp_upload_dir();
  59. $this->assertSame( get_option( 'siteurl' ) . '/wp-content/uploads', $info['url'] );
  60. $this->assertSame( ABSPATH . 'wp-content/uploads', $info['path'] );
  61. $this->assertSame( '', $info['subdir'] );
  62. $this->assertFalse( $info['error'] );
  63. }
  64. function test_upload_path_absolute() {
  65. update_option( 'upload_url_path', 'http://' . WP_TESTS_DOMAIN . '/asdf' );
  66. // Use `_wp_upload_dir()` directly to bypass caching and work with the changed options.
  67. // It doesn't create the /year/month directories.
  68. $info = _wp_upload_dir();
  69. $subdir = gmstrftime( '/%Y/%m' );
  70. $this->assertSame( 'http://' . WP_TESTS_DOMAIN . '/asdf' . $subdir, $info['url'] );
  71. $this->assertSame( ABSPATH . 'wp-content/uploads' . $subdir, $info['path'] );
  72. $this->assertSame( $subdir, $info['subdir'] );
  73. $this->assertFalse( $info['error'] );
  74. }
  75. function test_upload_dir_empty() {
  76. // Upload path setting is empty - it should default to 'wp-content/uploads'.
  77. update_option( 'upload_path', '' );
  78. // Use `_wp_upload_dir()` directly to bypass caching and work with the changed options.
  79. // It doesn't create the /year/month directories.
  80. $info = _wp_upload_dir();
  81. $subdir = gmstrftime( '/%Y/%m' );
  82. $this->assertSame( get_option( 'siteurl' ) . '/wp-content/uploads' . $subdir, $info['url'] );
  83. $this->assertSame( ABSPATH . 'wp-content/uploads' . $subdir, $info['path'] );
  84. $this->assertSame( $subdir, $info['subdir'] );
  85. $this->assertFalse( $info['error'] );
  86. }
  87. }