Make WordPress Core

Ticket #16434: 16434.21.diff

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