Ticket #16434: 16434.19.diff
File 16434.19.diff, 9.2 KB (added by , 9 years ago) |
---|
-
tests/phpunit/tests/image/site_icon.php
1 <?php 2 3 /** 4 * Tests for the WP_Site_Icon class. 5 * 6 * @group site_icon 7 */ 8 class Tests_WP_Site_Icon extends WP_UnitTestCase { 9 public $site_icon; 10 11 function setUp() { 12 parent::setUp(); 13 14 require_once ABSPATH . 'wp-admin/includes/class-wp-site-icon.php'; 15 $this->site_icon = $GLOBALS['wp_site_icon']; 16 } 17 18 function tearDown() { 19 $this->site_icon = null; 20 $this->remove_added_uploads(); 21 parent::tearDown(); 22 } 23 24 function test_intermediate_image_sizes() { 25 $image_sizes = $this->site_icon->intermediate_image_sizes( array() ); 26 27 $sizes = array(); 28 foreach ( $this->site_icon->site_icon_sizes as $size ) { 29 $sizes[] = 'site_icon-' . $size; 30 } 31 32 $this->assertEquals( $sizes, $image_sizes ); 33 } 34 35 function test_intermediate_image_sizes_with_filter() { 36 add_filter( 'site_icon_image_sizes', array( $this, 'custom_test_sizes' ) ); 37 $image_sizes = $this->site_icon->intermediate_image_sizes( array() ); 38 39 $sizes = array(); 40 foreach ( $this->site_icon->site_icon_sizes as $size ) { 41 $sizes[] = 'site_icon-' . $size; 42 } 43 44 // Is our custom icon size there? 45 $this->assertContains( 'site_icon-321', $image_sizes ); 46 47 // All icon sizes should be part of the array, including sizes added through the filter. 48 $this->assertEquals( $sizes, $image_sizes ); 49 50 // Remove custom size. 51 unset( $this->site_icon->site_icon_sizes[ array_search( 321, $this->site_icon->site_icon_sizes ) ] ); 52 } 53 54 function test_additional_sizes() { 55 $image_sizes = $this->site_icon->additional_sizes( array() ); 56 57 $sizes = array(); 58 foreach ( $this->site_icon->site_icon_sizes as $size ) { 59 $sizes[ 'site_icon-' . $size ] = array( 60 'width ' => $size, 61 'height' => $size, 62 'crop' => true, 63 ); 64 } 65 66 $this->assertEquals( $sizes, $image_sizes ); 67 } 68 69 function test_additional_sizes_with_filter() { 70 add_filter( 'site_icon_image_sizes', array( $this, 'custom_test_sizes' ) ); 71 $image_sizes = $this->site_icon->additional_sizes( array() ); 72 73 $sizes = array(); 74 foreach ( $this->site_icon->site_icon_sizes as $size ) { 75 $sizes[ 'site_icon-' . $size ] = array( 76 'width ' => $size, 77 'height' => $size, 78 'crop' => true, 79 ); 80 } 81 82 // Is our custom icon size there? 83 $this->assertArrayHasKey( 'site_icon-321', $image_sizes ); 84 85 // All icon sizes should be part of the array, including sizes added through the filter. 86 $this->assertEquals( $sizes, $image_sizes ); 87 88 // Remove custom size. 89 unset( $this->site_icon->site_icon_sizes[ array_search( 321, $this->site_icon->site_icon_sizes ) ] ); 90 } 91 92 function custom_test_sizes( $sizes ) { 93 $sizes[] = 321; 94 95 return $sizes; 96 } 97 98 function test_create_attachment_object() { 99 $cropped = 'http://localhost/foo-cropped.png'; 100 $attachment_id = wp_insert_attachment( array( 101 'post_status' => 'publish', 102 'post_title' => 'foo.png', 103 'post_type' => 'post', 104 'guid' => 'http://localhost/foo.png', 105 ) ); 106 107 $object = $this->site_icon->create_attachment_object( $cropped, $attachment_id ); 108 109 $this->assertEquals( $object['post_title'], 'foo-cropped.png' ); 110 $this->assertEquals( $object['context'], 'site-icon' ); 111 $this->assertEquals( $object['post_mime_type'], 'image/jpeg' ); 112 $this->assertEquals( $object['post_content'], $cropped ); 113 $this->assertEquals( $object['guid'], $cropped ); 114 } 115 116 function test_insert_cropped_attachment() { 117 $cropped = 'http://localhost/foo-cropped.png'; 118 $attachment_id = wp_insert_attachment( array( 119 'post_status' => 'publish', 120 'post_title' => 'foo.png', 121 'post_type' => 'post', 122 'guid' => 'http://localhost/foo.png', 123 ) ); 124 125 $object = $this->site_icon->create_attachment_object( $cropped, $attachment_id ); 126 $cropped_id = $this->site_icon->insert_attachment( $object, $cropped ); 127 128 $this->assertInternalType( 'int', $cropped_id ); 129 $this->assertGreaterThan( 0, $cropped_id ); 130 } 131 132 function test_delete_site_icon() { 133 $attachment_id = 5; 134 update_option( 'site_icon', $attachment_id ); 135 136 $this->site_icon->delete_site_icon(); 137 138 $this->assertFalse( get_option( 'site_icon', false ) ); 139 } 140 141 function test_delete_attachment_data() { 142 $attachment_id = wp_insert_attachment( array( 143 'post_status' => 'publish', 144 'post_title' => 'foo.png', 145 'post_type' => 'post', 146 'guid' => 'http://localhost/foo.png', 147 ) ); 148 update_option( 'site_icon', $attachment_id ); 149 150 wp_delete_attachment( $attachment_id, true ); 151 152 $this->assertFalse( get_option( 'site_icon', false ) ); 153 } 154 } -
tests/phpunit/tests/template/general.php
1 <?php 2 3 /** 4 * A set of unit tests for functions in wp-includes/general-template.php 5 * 6 * @group template 7 */ 8 class Tests_General_Template extends WP_UnitTestCase { 9 10 /** SITE ICON */ 11 12 public $wp_site_icon; 13 public $site_icon_id; 14 public $site_icon_url; 15 16 function setUp() { 17 parent::setUp(); 18 19 require_once ABSPATH . 'wp-admin/includes/class-wp-site-icon.php'; 20 $this->wp_site_icon = $GLOBALS['wp_site_icon']; 21 } 22 23 function test_get_site_icon_url() { 24 $this->assertEmpty( get_site_icon_url() ); 25 26 $this->_set_site_icon(); 27 $this->assertEquals( $this->site_icon_url, get_site_icon_url() ); 28 29 $this->_remove_site_icon(); 30 $this->assertEmpty( get_site_icon_url() ); 31 } 32 33 function test_site_icon_url() { 34 $this->expectOutputString( '' ); 35 site_icon_url(); 36 37 $this->_set_site_icon(); 38 $this->expectOutputString( $this->site_icon_url ); 39 site_icon_url(); 40 $this->_remove_site_icon(); 41 } 42 43 function test_has_site_icon() { 44 $this->assertFalse( has_site_icon() ); 45 46 $this->_set_site_icon(); 47 $this->assertTrue( has_site_icon() ); 48 49 $this->_remove_site_icon(); 50 $this->assertFalse( has_site_icon() ); 51 } 52 53 function test_wp_site_icon() { 54 $this->expectOutputString( '' ); 55 wp_site_icon(); 56 57 $this->_set_site_icon(); 58 $output = array( 59 sprintf( '<link rel="icon" href="%s" sizes="32x32" />', esc_url( get_site_icon_url( null, 32 ) ) ), 60 sprintf( '<link rel="apple-touch-icon-precomposed" href="%s">', esc_url( get_site_icon_url( null, 180 ) ) ), 61 sprintf( '<meta name="msapplication-TileImage" content="%s">', esc_url( get_site_icon_url( null, 270 ) ) ), 62 '', 63 ); 64 $output = implode( "\n", $output ); 65 66 $this->expectOutputString( $output ); 67 wp_site_icon(); 68 69 $this->_remove_site_icon(); 70 } 71 72 function test_wp_site_icon_with_filter() { 73 $this->expectOutputString( '' ); 74 wp_site_icon(); 75 76 $this->_set_site_icon(); 77 $output = array( 78 sprintf( '<link rel="icon" href="%s" sizes="32x32" />', esc_url( get_site_icon_url( null, 32 ) ) ), 79 sprintf( '<link rel="apple-touch-icon-precomposed" href="%s">', esc_url( get_site_icon_url( null, 180 ) ) ), 80 sprintf( '<meta name="msapplication-TileImage" content="%s">', esc_url( get_site_icon_url( null, 270 ) ) ), 81 sprintf( '<link rel="apple-touch-icon" sizes="150x150" href="%s">', esc_url( get_site_icon_url( null, 150 ) ) ), 82 '', 83 ); 84 $output = implode( "\n", $output ); 85 86 $this->expectOutputString( $output ); 87 add_filter( 'site_icon_meta_tags', array( $this, '_custom_site_icon_meta_tag' ) ); 88 wp_site_icon(); 89 remove_filter( 'site_icon_meta_tags', array( $this, '_custom_site_icon_meta_tag' ) ); 90 91 $this->_remove_site_icon(); 92 } 93 94 function _custom_site_icon_meta_tag( $meta_tags ) { 95 $meta_tags[] = sprintf( '<link rel="apple-touch-icon" sizes="150x150" href="%s">', esc_url( get_site_icon_url( null, 150 ) ) ); 96 97 return $meta_tags; 98 } 99 100 function _set_site_icon() { 101 if ( ! $this->site_icon_id ) { 102 add_filter( 'intermediate_image_sizes_advanced', array( $this->wp_site_icon, 'additional_sizes' ) ); 103 $this->_insert_attachment(); 104 remove_filter( 'intermediate_image_sizes_advanced', array( $this->wp_site_icon, 'additional_sizes' ) ); 105 } 106 107 update_option( 'site_icon', $this->site_icon_id ); 108 } 109 110 function _remove_site_icon() { 111 delete_option( 'site_icon' ); 112 } 113 114 function _insert_attachment() { 115 $filename = DIR_TESTDATA . '/images/test-image.jpg'; 116 $contents = file_get_contents( $filename ); 117 118 $upload = wp_upload_bits( basename( $filename ), null, $contents ); 119 $type = ''; 120 if ( ! empty( $upload['type'] ) ) { 121 $type = $upload['type']; 122 } else { 123 $mime = wp_check_filetype( $upload['file'] ); 124 if ( $mime ) { 125 $type = $mime['type']; 126 } 127 } 128 129 $attachment = array( 130 'post_title' => basename( $upload['file'] ), 131 'post_content' => $upload['url'], 132 'post_type' => 'attachment', 133 'post_mime_type' => $type, 134 'guid' => $upload['url'], 135 ); 136 137 // Save the data 138 $this->site_icon_url = $upload['url']; 139 $this->site_icon_id = wp_insert_attachment( $attachment, $upload['file'] ); 140 wp_update_attachment_metadata( $this->site_icon_id, wp_generate_attachment_metadata( $this->site_icon_id, $upload['file'] ) ); 141 } 142 }