Changeset 35179
- Timestamp:
- 10/15/2015 01:42:27 AM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/media.php
r35178 r35179 6 6 */ 7 7 class Tests_Media extends WP_UnitTestCase { 8 protected static $large_id; 9 10 public static function setUpBeforeClass() { 11 parent::setUpBeforeClass(); 12 13 $factory = new WP_UnitTest_Factory(); 14 15 $filename = DIR_TESTDATA . '/images/test-image-large.png'; 16 self::$large_id = $factory->attachment->create_upload_object( $filename ); 17 18 self::commit_transaction(); 19 } 20 21 public static function tearDownAfterClass() { 22 parent::tearDownAfterClass(); 23 24 wp_delete_attachment( self::$large_id ); 25 26 self::commit_transaction(); 27 } 8 28 9 29 function setUp() { … … 710 730 */ 711 731 function test_wp_get_attachment_image_srcset_array() { 712 $filename = DIR_TESTDATA . '/images/test-image-large.png';713 $id = $this->factory->attachment->create_upload_object( $filename );714 715 732 $year_month = date('Y/m'); 716 $image = wp_get_attachment_metadata( $id );733 $image = wp_get_attachment_metadata( self::$large_id ); 717 734 718 735 $expected = array( … … 738 755 739 756 foreach ( $sizes as $size ) { 740 $this->assertSame( $expected, wp_get_attachment_image_srcset_array( $id, $size ) );757 $this->assertSame( $expected, wp_get_attachment_image_srcset_array( self::$large_id, $size ) ); 741 758 } 742 759 } … … 791 808 */ 792 809 function test_wp_get_attachment_image_srcset_array_with_edits() { 793 // Make an image.794 $filename = DIR_TESTDATA . '/images/test-image-large.png';795 $id = $this->factory->attachment->create_upload_object( $filename );796 810 // For this test we're going to mock metadata changes from an edit. 797 811 // Start by getting the attachment metadata. 798 $meta = wp_get_attachment_metadata( $id );812 $meta = wp_get_attachment_metadata( self::$large_id ); 799 813 800 814 // Copy hash generation method used in wp_save_image(). … … 807 821 808 822 // Save edited metadata. 809 wp_update_attachment_metadata( $id, $meta ); 810 811 // Get the edited image and observe that a hash was created. 812 $img_url = wp_get_attachment_url( $id ); 823 wp_update_attachment_metadata( self::$large_id, $meta ); 813 824 814 825 // Calculate a srcset array. 815 $sizes = wp_get_attachment_image_srcset_array( $id, 'medium' );826 $sizes = wp_get_attachment_image_srcset_array( self::$large_id, 'medium' ); 816 827 817 828 // Test to confirm all sources in the array include the same edit hash. … … 825 836 */ 826 837 function test_wp_get_attachment_image_srcset_array_false() { 827 // Make an image.828 $filename = DIR_TESTDATA . '/images/test-image-large.png';829 $id = $this->factory->attachment->create_upload_object( $filename );830 838 $sizes = wp_get_attachment_image_srcset_array( 99999, 'foo' ); 831 839 … … 846 854 $srcset = wp_get_attachment_image_srcset_array( $id, 'medium' ); 847 855 856 // Remove filter. 857 remove_filter( 'wp_generate_attachment_metadata', array( $this, '_test_wp_get_attachment_image_srcset_array_no_width_filter' ) ); 858 848 859 // The srcset should be false. 849 860 $this->assertFalse( $srcset ); 850 851 // Remove filter.852 remove_filter( 'wp_generate_attachment_metadata', array( $this, '_test_wp_get_attachment_image_srcset_array_no_width_filter' ) );853 861 } 854 862 … … 866 874 */ 867 875 function test_wp_get_attachment_image_srcset() { 868 // Make an image. 869 $filename = DIR_TESTDATA . '/images/test-image-large.png'; 870 $id = $this->factory->attachment->create_upload_object( $filename ); 871 $sizes = wp_get_attachment_image_srcset( $id, 'full-size' ); 872 873 $image = wp_get_attachment_metadata( $id ); 876 $sizes = wp_get_attachment_image_srcset( self::$large_id, 'full-size' ); 877 878 $image = wp_get_attachment_metadata( self::$large_id ); 874 879 $year_month = date('Y/m'); 875 880 … … 887 892 */ 888 893 function test_wp_get_attachment_image_srcset_single_srcset() { 889 // Make an image.890 $filename = DIR_TESTDATA . '/images/test-image-large.png';891 $id = $this->factory->attachment->create_upload_object( $filename );892 894 /* 893 895 * In our tests, thumbnails will only return a single srcset candidate, 894 896 * so we shouldn't return a srcset value in order to avoid unneeded markup. 895 897 */ 896 $sizes = wp_get_attachment_image_srcset( $id, 'thumbnail' );898 $sizes = wp_get_attachment_image_srcset( self::$large_id, 'thumbnail' ); 897 899 898 900 $this->assertFalse( $sizes ); … … 903 905 */ 904 906 function test_wp_get_attachment_image_sizes() { 905 // Make an image.906 $filename = DIR_TESTDATA . '/images/test-image-large.png';907 $id = $this->factory->attachment->create_upload_object( $filename );908 909 910 907 global $content_width; 911 908 … … 922 919 923 920 $expected = '(max-width: ' . $width . 'px) 100vw, ' . $width . 'px'; 924 $sizes = wp_get_attachment_image_sizes( $id, $int );921 $sizes = wp_get_attachment_image_sizes( self::$large_id, $int ); 925 922 926 923 $this->assertSame($expected, $sizes); … … 932 929 */ 933 930 function test_wp_get_attachment_image_sizes_with_args() { 934 // Make an image.935 $filename = DIR_TESTDATA . '/images/test-image-large.png';936 $id = $this->factory->attachment->create_upload_object( $filename );937 938 939 931 $args = array( 940 932 'sizes' => array( … … 956 948 957 949 $expected = '(min-width: 60em) 10em, (min-width: 30em) 20em, calc(100vm - 30px)'; 958 $sizes = wp_get_attachment_image_sizes( $id, 'medium', $args );959 960 $this->assertSame( $expected, $sizes);950 $sizes = wp_get_attachment_image_sizes( self::$large_id, 'medium', $args ); 951 952 $this->assertSame( $expected, $sizes ); 961 953 } 962 954 … … 968 960 add_filter( 'wp_image_sizes_args', array( $this, '_test_wp_image_sizes_args' ) ); 969 961 970 // Make an image. 971 $filename = DIR_TESTDATA . '/images/test-image-large.png'; 972 $id = $this->factory->attachment->create_upload_object( $filename ); 973 974 $sizes = wp_get_attachment_image_sizes($id, 'medium'); 962 $sizes = wp_get_attachment_image_sizes( self::$large_id, 'medium' ); 963 964 remove_filter( 'wp_image_sizes_args', array( $this, '_test_wp_image_sizes_args' ) ); 975 965 976 966 // Evaluate that the sizes returned is what we expected. 977 967 $this->assertSame( $sizes, '100vm'); 978 979 remove_filter( 'wp_image_sizes_args', array( $this, '_test_wp_image_sizes_args' ) );980 968 } 981 969 … … 992 980 */ 993 981 function test_wp_make_content_images_responsive() { 994 // Make an image. 995 $filename = DIR_TESTDATA . '/images/test-image-large.png'; 996 $id = $this->factory->attachment->create_upload_object( $filename ); 997 998 $srcset = sprintf( 'srcset="%s"', wp_get_attachment_image_srcset( $id, 'medium' ) ); 999 $sizes = sprintf( 'sizes="%s"', wp_get_attachment_image_sizes( $id, 'medium' ) ); 982 $srcset = sprintf( 'srcset="%s"', wp_get_attachment_image_srcset( self::$large_id, 'medium' ) ); 983 $sizes = sprintf( 'sizes="%s"', wp_get_attachment_image_sizes( self::$large_id, 'medium' ) ); 1000 984 1001 985 // Function used to build HTML for the editor. 1002 $img = get_image_tag( $id, '', '', '', 'medium' );986 $img = get_image_tag( self::$large_id, '', '', '', 'medium' ); 1003 987 $img_no_size = str_replace( 'size-', '', $img ); 1004 988 $img_no_size_id = str_replace( 'wp-image-', 'id-', $img_no_size ); … … 1046 1030 */ 1047 1031 function test_wp_make_content_images_responsive_with_preexisting_srcset() { 1048 // Make an image.1049 $filename = DIR_TESTDATA . '/images/test-image-large.png';1050 $id = $this->factory->attachment->create_upload_object( $filename );1051 1052 1032 // Generate HTML and add a dummy srcset attribute. 1053 $image_html = get_image_tag( $id, '', '', '', 'medium' );1033 $image_html = get_image_tag( self::$large_id, '', '', '', 'medium' ); 1054 1034 $image_html = preg_replace('|<img ([^>]+) />|', '<img $1 ' . 'srcset="image2x.jpg 2x" />', $image_html ); 1055 1035
Note: See TracChangeset
for help on using the changeset viewer.