diff --git a/tests/phpunit/tests/functions/wpuploadbits.php b/tests/phpunit/tests/functions/wpuploadbits.php new file mode 100644 index 0000000000000..dbcb944d624ae --- /dev/null +++ b/tests/phpunit/tests/functions/wpuploadbits.php @@ -0,0 +1,88 @@ +assertSameSets( array( 'error' => __( 'Empty filename' ) ), wp_upload_bits( '', '', 'bits' ) ); + } + + /** + * Tests that wp_upload_bits() returns an array with an error message if the file type is not allowed. + * + * @ticket 57130 + **/ + public function test_wp_upload_bits_should_return_an_array_with_error_message_if_filename_without_an_extension() { + $this->assertSameSets( array( 'error' => __( 'Sorry, you are not allowed to upload this file type.' ) ), wp_upload_bits( 'filename', '', 'bits' ) ); + } + + /** + * @ticket 57130 + * + * + **/ + public function test_should_return_error_if_bad_time_path_is_passed() { + $upload_dir = wp_upload_dir(); + $this->assertSameSets( + array( + 'path' => ABSPATH . 'wp-content/uploads/.././/1', + 'url' => $upload_dir['baseurl'] . '/.././/1', + 'subdir' => '/.././/1', + 'basedir' => ABSPATH . 'wp-content/uploads', + 'baseurl' => $upload_dir['baseurl'], + 'error' => 'Unable to create directory wp-content/uploads/.././/1. Is its parent directory writable by the server?', + ), + wp_upload_bits( 'filename.jpg', null, 'bits', '../../12' ) + ); + } + + /** + * Tests that wp_upload_bits() creates a file in the upload folder with the given content. + * + * @ticket 57130 + */ + public function test_wp_upload_bits_should_create_file_in_upload_folder_with_given_content() { + $upload_dir = wp_upload_dir(); + $filename = ABSPATH . 'wp-content/uploads/9999/12/filename.txt'; + $content = 'file content'; + + $this->assertSameSets( + array( + 'error' => false, + 'path' => $filename, + 'url' => $upload_dir['baseurl'] . '/9999/12/filename.txt', + 'type' => 'text/plain', + + ), + wp_upload_bits( 'filename.txt', null, $content, '9999/12' ), + 'wp_upload_bits() did not return the expected result.' + ); + $file = fopen( $filename, 'rb' ); + $file_contents = fread( $file, filesize( $filename ) ); + fclose( $file ); + $this->unlink( $filename ); + + $this->assertSame( $content, $file_contents, 'The content of the file does not match the expected value.' ); + } +}