|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Unit tests covering post mime types. |
| 4 | + * |
| 5 | + * @ticket 59195 |
| 6 | + * |
| 7 | + * @group post |
| 8 | + * |
| 9 | + * @covers ::get_available_post_mime_types |
| 10 | + */ |
| 11 | + |
| 12 | +class Tests_Post_GetAvailablePostMimeTypes extends WP_UnitTestCase { |
| 13 | + |
| 14 | + public function tear_down() { |
| 15 | + // Remove all uploads. |
| 16 | + $this->remove_added_uploads(); |
| 17 | + remove_filter( 'pre_get_available_post_mime_types', array( $this, 'filter_add_null_to_post_mime_types' ) ); |
| 18 | + parent::tear_down(); |
| 19 | + } |
| 20 | + |
| 21 | + public function test_should_return_expected_post_mime_types() { |
| 22 | + // Upload a JPEG image. |
| 23 | + $filename = DIR_TESTDATA . '/images/test-image.jpg'; |
| 24 | + $contents = file_get_contents( $filename ); |
| 25 | + $upload = wp_upload_bits( wp_basename( $filename ), null, $contents ); |
| 26 | + $this->assertEmpty( $upload['error'], 'Uploading a JPEG file should not result in an error.' ); |
| 27 | + $this->_make_attachment( $upload ); |
| 28 | + |
| 29 | + // Upload a PDF file. |
| 30 | + $filename = DIR_TESTDATA . '/images/test-alpha.pdf'; |
| 31 | + $contents = file_get_contents( $filename ); |
| 32 | + $upload = wp_upload_bits( wp_basename( $filename ), null, $contents ); |
| 33 | + $this->assertEmpty( $upload['error'], 'Uploading a PDF file should not result in an error.' ); |
| 34 | + $this->_make_attachment( $upload ); |
| 35 | + |
| 36 | + $mime_types = get_available_post_mime_types(); |
| 37 | + |
| 38 | + $this->assertSame( array( 'image/jpeg', 'application/pdf' ), $mime_types, 'The MIME types returned should match the uploaded file MIME types.' ); |
| 39 | + } |
| 40 | + |
| 41 | + public function test_should_remove_null() { |
| 42 | + // Add filter to inject null into the mime types array. |
| 43 | + add_filter( 'pre_get_available_post_mime_types', array( $this, 'filter_add_null_to_post_mime_types' ) ); |
| 44 | + |
| 45 | + $mime_types = get_available_post_mime_types(); |
| 46 | + $this->assertEqualsCanonicalizing( array( 'image/jpeg', 'image/png' ), $mime_types ); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Filter to inject null into the mime types array. |
| 51 | + * |
| 52 | + * @param string $type Post type. |
| 53 | + * @return array |
| 54 | + */ |
| 55 | + public function filter_add_null_to_post_mime_types( $type ) { |
| 56 | + return array( 'image/jpeg', null, 'image/png' ); |
| 57 | + } |
| 58 | +} |
0 commit comments