| | 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 $wp_site_icon; |
| | 10 | public $attachment_id = 0; |
| | 11 | |
| | 12 | function setUp() { |
| | 13 | parent::setUp(); |
| | 14 | |
| | 15 | require_once ABSPATH . 'wp-admin/includes/class-wp-site-icon.php'; |
| | 16 | $this->wp_site_icon = $GLOBALS['wp_site_icon']; |
| | 17 | } |
| | 18 | |
| | 19 | function tearDown() { |
| | 20 | $this->site_icon = null; |
| | 21 | $this->remove_added_uploads(); |
| | 22 | parent::tearDown(); |
| | 23 | } |
| | 24 | |
| | 25 | function test_intermediate_image_sizes() { |
| | 26 | $image_sizes = $this->wp_site_icon->intermediate_image_sizes( array() ); |
| | 27 | |
| | 28 | $sizes = array(); |
| | 29 | foreach ( $this->wp_site_icon->site_icon_sizes as $size ) { |
| | 30 | $sizes[] = 'site_icon-' . $size; |
| | 31 | } |
| | 32 | |
| | 33 | $this->assertEquals( $sizes, $image_sizes ); |
| | 34 | } |
| | 35 | |
| | 36 | function test_intermediate_image_sizes_with_filter() { |
| | 37 | add_filter( 'site_icon_image_sizes', array( $this, '_custom_test_sizes' ) ); |
| | 38 | $image_sizes = $this->wp_site_icon->intermediate_image_sizes( array() ); |
| | 39 | |
| | 40 | $sizes = array(); |
| | 41 | foreach ( $this->wp_site_icon->site_icon_sizes as $size ) { |
| | 42 | $sizes[] = 'site_icon-' . $size; |
| | 43 | } |
| | 44 | |
| | 45 | // Is our custom icon size there? |
| | 46 | $this->assertContains( 'site_icon-321', $image_sizes ); |
| | 47 | |
| | 48 | // All icon sizes should be part of the array, including sizes added through the filter. |
| | 49 | $this->assertEquals( $sizes, $image_sizes ); |
| | 50 | |
| | 51 | // Remove custom size. |
| | 52 | unset( $this->wp_site_icon->site_icon_sizes[ array_search( 321, $this->wp_site_icon->site_icon_sizes ) ] ); |
| | 53 | } |
| | 54 | |
| | 55 | function test_additional_sizes() { |
| | 56 | $image_sizes = $this->wp_site_icon->additional_sizes( array() ); |
| | 57 | |
| | 58 | $sizes = array(); |
| | 59 | foreach ( $this->wp_site_icon->site_icon_sizes as $size ) { |
| | 60 | $sizes[ 'site_icon-' . $size ] = array( |
| | 61 | 'width ' => $size, |
| | 62 | 'height' => $size, |
| | 63 | 'crop' => true, |
| | 64 | ); |
| | 65 | } |
| | 66 | |
| | 67 | $this->assertEquals( $sizes, $image_sizes ); |
| | 68 | } |
| | 69 | |
| | 70 | function test_additional_sizes_with_filter() { |
| | 71 | add_filter( 'site_icon_image_sizes', array( $this, '_custom_test_sizes' ) ); |
| | 72 | $image_sizes = $this->wp_site_icon->additional_sizes( array() ); |
| | 73 | |
| | 74 | $sizes = array(); |
| | 75 | foreach ( $this->wp_site_icon->site_icon_sizes as $size ) { |
| | 76 | $sizes[ 'site_icon-' . $size ] = array( |
| | 77 | 'width ' => $size, |
| | 78 | 'height' => $size, |
| | 79 | 'crop' => true, |
| | 80 | ); |
| | 81 | } |
| | 82 | |
| | 83 | // Is our custom icon size there? |
| | 84 | $this->assertArrayHasKey( 'site_icon-321', $image_sizes ); |
| | 85 | |
| | 86 | // All icon sizes should be part of the array, including sizes added through the filter. |
| | 87 | $this->assertEquals( $sizes, $image_sizes ); |
| | 88 | |
| | 89 | // Remove custom size. |
| | 90 | unset( $this->wp_site_icon->site_icon_sizes[ array_search( 321, $this->wp_site_icon->site_icon_sizes ) ] ); |
| | 91 | } |
| | 92 | |
| | 93 | function test_create_attachment_object() { |
| | 94 | $attachment_id = $this->_insert_attachment(); |
| | 95 | $parent_url = get_post( $attachment_id )->guid; |
| | 96 | $cropped = str_replace( basename( $parent_url ), 'cropped-test-image.jpg', $parent_url ); |
| | 97 | |
| | 98 | $object = $this->wp_site_icon->create_attachment_object( $cropped, $attachment_id ); |
| | 99 | |
| | 100 | $this->assertEquals( $object['post_title'], 'cropped-test-image.jpg' ); |
| | 101 | $this->assertEquals( $object['context'], 'site-icon' ); |
| | 102 | $this->assertEquals( $object['post_mime_type'], 'image/jpeg' ); |
| | 103 | $this->assertEquals( $object['post_content'], $cropped ); |
| | 104 | $this->assertEquals( $object['guid'], $cropped ); |
| | 105 | } |
| | 106 | |
| | 107 | function test_insert_cropped_attachment() { |
| | 108 | $attachment_id = $this->_insert_attachment(); |
| | 109 | $parent_url = get_post( $attachment_id )->guid; |
| | 110 | $cropped = str_replace( basename( $parent_url ), 'cropped-test-image.jpg', $parent_url ); |
| | 111 | |
| | 112 | $object = $this->wp_site_icon->create_attachment_object( $cropped, $attachment_id ); |
| | 113 | $cropped_id = $this->wp_site_icon->insert_attachment( $object, $cropped ); |
| | 114 | |
| | 115 | $this->assertInternalType( 'int', $cropped_id ); |
| | 116 | $this->assertGreaterThan( 0, $cropped_id ); |
| | 117 | } |
| | 118 | |
| | 119 | function test_delete_site_icon() { |
| | 120 | $attachment_id = $this->_insert_attachment(); |
| | 121 | update_option( 'site_icon', $attachment_id ); |
| | 122 | |
| | 123 | $this->wp_site_icon->delete_site_icon(); |
| | 124 | |
| | 125 | $this->assertFalse( get_option( 'site_icon', false ) ); |
| | 126 | } |
| | 127 | |
| | 128 | function test_delete_attachment_data() { |
| | 129 | $attachment_id = $this->_insert_attachment(); |
| | 130 | update_option( 'site_icon', $attachment_id ); |
| | 131 | |
| | 132 | wp_delete_attachment( $attachment_id, true ); |
| | 133 | |
| | 134 | $this->assertFalse( get_option( 'site_icon', false ) ); |
| | 135 | } |
| | 136 | |
| | 137 | function _custom_test_sizes( $sizes ) { |
| | 138 | $sizes[] = 321; |
| | 139 | |
| | 140 | return $sizes; |
| | 141 | } |
| | 142 | |
| | 143 | function _insert_attachment() { |
| | 144 | if ( $this->attachment_id ) { |
| | 145 | return $this->attachment_id; |
| | 146 | } |
| | 147 | |
| | 148 | $filename = DIR_TESTDATA . '/images/test-image.jpg'; |
| | 149 | $contents = file_get_contents( $filename ); |
| | 150 | |
| | 151 | $upload = wp_upload_bits( basename( $filename ), null, $contents ); |
| | 152 | $type = ''; |
| | 153 | if ( ! empty( $upload['type'] ) ) { |
| | 154 | $type = $upload['type']; |
| | 155 | } else { |
| | 156 | $mime = wp_check_filetype( $upload['file'] ); |
| | 157 | if ( $mime ) { |
| | 158 | $type = $mime['type']; |
| | 159 | } |
| | 160 | } |
| | 161 | |
| | 162 | $attachment = array( |
| | 163 | 'post_title' => basename( $upload['file'] ), |
| | 164 | 'post_content' => $upload['url'], |
| | 165 | 'post_type' => 'attachment', |
| | 166 | 'post_mime_type' => $type, |
| | 167 | 'guid' => $upload['url'], |
| | 168 | ); |
| | 169 | |
| | 170 | // Save the data |
| | 171 | $this->attachment_id = wp_insert_attachment( $attachment, $upload['file'] ); |
| | 172 | wp_update_attachment_metadata( $this->attachment_id, wp_generate_attachment_metadata( $this->attachment_id, $upload['file'] ) ); |
| | 173 | |
| | 174 | return $this->attachment_id; |
| | 175 | } |
| | 176 | } |