| 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 = 5; |
| 143 | update_option( 'site_icon', $attachment_id ); |
| 144 | |
| 145 | $this->site_icon->delete_attachment_data( $attachment_id ); |
| 146 | |
| 147 | $this->assertFalse( get_option( 'site_icon', false ) ); |
| 148 | } |
| 149 | } |