upload.php 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * @group upload
  4. * @group media
  5. */
  6. class Tests_Upload extends WP_UnitTestCase {
  7. var $siteurl;
  8. function setUp() {
  9. if ( is_multisite() )
  10. $this->knownUTBug( 35 );
  11. parent::setUp();
  12. return;
  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 tearDown() {
  19. parent::tearDown();
  20. // Remove year/month folders created by wp_upload_dir().
  21. $uploads = wp_upload_dir();
  22. foreach ( scandir( $uploads['basedir'] ) as $file )
  23. _rmdir( $uploads['basedir'] . '/' . $file );
  24. _rmdir( ABSPATH . 'foo/' );
  25. }
  26. function test_upload_dir_default() {
  27. // wp_upload_dir() with default parameters
  28. $info = wp_upload_dir();
  29. $this->assertEquals( get_option( 'siteurl' ) . '/wp-content/uploads/' . gmstrftime('%Y/%m'), $info['url'] );
  30. $this->assertEquals( ABSPATH . 'wp-content/uploads/' . gmstrftime('%Y/%m'), $info['path'] );
  31. $this->assertEquals( gmstrftime('/%Y/%m'), $info['subdir'] );
  32. $this->assertEquals( '', $info['error'] );
  33. }
  34. function test_upload_dir_relative() {
  35. // wp_upload_dir() with a relative upload path that is not 'wp-content/uploads'
  36. update_option( 'upload_path', 'foo/bar' );
  37. $info = wp_upload_dir();
  38. $this->assertEquals( get_option( 'siteurl' ) . '/foo/bar/' . gmstrftime('%Y/%m'), $info['url'] );
  39. $this->assertEquals( ABSPATH . 'foo/bar/' . gmstrftime('%Y/%m'), $info['path'] );
  40. $this->assertEquals( gmstrftime('/%Y/%m'), $info['subdir'] );
  41. $this->assertEquals( '', $info['error'] );
  42. }
  43. /**
  44. * @ticket 5953
  45. */
  46. function test_upload_dir_absolute() {
  47. $path = '/tmp/wp-unit-test';
  48. // wp_upload_dir() with an absolute upload path
  49. update_option( 'upload_path', $path );
  50. // doesn't make sense to use an absolute file path without setting the url path
  51. update_option( 'upload_url_path', '/baz' );
  52. $info = wp_upload_dir();
  53. $this->assertEquals( '/baz/' . gmstrftime('%Y/%m'), $info['url'] );
  54. $this->assertEquals( "$path/" . gmstrftime('%Y/%m'), $info['path'] );
  55. $this->assertEquals( gmstrftime('/%Y/%m'), $info['subdir'] );
  56. $this->assertEquals( '', $info['error'] );
  57. }
  58. function test_upload_dir_no_yearnum() {
  59. update_option( 'uploads_use_yearmonth_folders', 0 );
  60. $info = wp_upload_dir();
  61. $this->assertEquals( get_option( 'siteurl' ) . '/wp-content/uploads', $info['url'] );
  62. $this->assertEquals( ABSPATH . 'wp-content/uploads', $info['path'] );
  63. $this->assertEquals( '', $info['subdir'] );
  64. $this->assertEquals( '', $info['error'] );
  65. }
  66. function test_upload_path_absolute() {
  67. update_option( 'upload_url_path', 'http://example.org/asdf' );
  68. $info = wp_upload_dir();
  69. $this->assertEquals( 'http://example.org/asdf/' . gmstrftime('%Y/%m'), $info['url'] );
  70. $this->assertEquals( ABSPATH . 'wp-content/uploads/' . gmstrftime('%Y/%m'), $info['path'] );
  71. $this->assertEquals( gmstrftime('/%Y/%m'), $info['subdir'] );
  72. $this->assertEquals( '', $info['error'] );
  73. }
  74. function test_upload_dir_empty() {
  75. // upload path setting is empty - it should default to 'wp-content/uploads'
  76. update_option('upload_path', '');
  77. $info = wp_upload_dir();
  78. $this->assertEquals( get_option( 'siteurl' ) . '/wp-content/uploads/' . gmstrftime('%Y/%m'), $info['url'] );
  79. $this->assertEquals( ABSPATH . 'wp-content/uploads/' . gmstrftime('%Y/%m'), $info['path'] );
  80. $this->assertEquals( gmstrftime('/%Y/%m'), $info['subdir'] );
  81. $this->assertEquals( '', $info['error'] );
  82. }
  83. }