Make WordPress Core

Changeset 1260 in tests


Ignore:
Timestamp:
04/19/2013 06:32:45 PM (13 years ago)
Author:
wonderboymusic
Message:

Add initial Unit Tests for images, galleries. See #22960

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/media.php

    r1226 r1260  
    1616<img src="pic.jpg" id='anId' alt="pic"/>
    1717CAP;
     18        $this->img_name = 'image.jpg';
     19        $this->img_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $this->img_name;
     20        $this->img_html = '<img src="' . $this->img_url . '"/>';
     21        $this->img_dimensions = array( 'width' => 100, 'height' => 100 );
    1822  }
    1923
     
    170174                $this->assertEquals( '0B', wp_convert_bytes_to_hr( 0 ) );
    171175        }
     176
     177        /**
     178         * @ticket 22960
     179         */
     180        function test_get_attached_images() {
     181                $post_id = $this->factory->post->create();
     182                $attachment_id = $this->factory->attachment->create_object( $this->img_name, $post_id, array(
     183                        'post_mime_type' => 'image/jpeg',
     184                        'post_type' => 'attachment'
     185                ) );
     186
     187                $images = get_attached_images( $post_id );
     188                $this->assertEquals( $images, array( $attachment_id => get_post( $attachment_id ) ) );
     189        }
     190
     191        /**
     192         * @ticket 22960
     193         */
     194        function test_get_attached_image_srcs() {
     195                $post_id = $this->factory->post->create();
     196                $this->factory->attachment->create_object( $this->img_name, $post_id, array(
     197                        'post_mime_type' => 'image/jpeg',
     198                        'post_type' => 'attachment'
     199                ) );
     200
     201                $images = get_attached_image_srcs( $post_id );
     202                $this->assertEquals( $images, array( $this->img_url ) );
     203        }
     204
     205        /**
     206         * @ticket 22960
     207         */
     208        function test_content_images() {
     209                $src1 = $this->img_url;
     210                $html1 = $this->img_html;
     211                $src2 = str_replace( '.jpg', '2.jpg', $this->img_url );
     212                $html2 = str_replace( $src1, $src2, $this->img_html );
     213                $src3 = str_replace( '.jpg', '3.jpg', $this->img_url );
     214                $html3 = str_replace( $src1, $src3, $this->img_html );
     215
     216                $blob =<<<BLOB
     217This is a sentence that will all of a sudden contain {$html1} and then {$html2} and why not {$html3}
     218BLOB;
     219
     220                $imgless = "This is a sentence that will all of a sudden contain  and then  and why not ";
     221                $imgless1 = "This is a sentence that will all of a sudden contain  and then {$html2} and why not {$html3}";
     222
     223                $images = get_content_images( $blob );
     224                $this->assertEquals( $images, array( $html1, $html2, $html3 ) );
     225
     226                $images = get_content_images( $blob, false );
     227                $this->assertEquals( $images, array( $src1, $src2, $src3 ) );
     228
     229                $copyblob = $blob;
     230                $images = get_content_images( $blob, true, true );
     231                $this->assertEquals( $images, array( $html1, $html2, $html3 ) );
     232                $this->assertEquals( $blob, $imgless );
     233                $blob = $copyblob;
     234
     235                $copyblob = $blob;
     236                $images = get_content_images( $blob, false, true );
     237                $this->assertEquals( $images, array( $src1, $src2, $src3 ) );
     238                $this->assertEquals( $blob, $imgless );
     239                $blob = $copyblob;
     240
     241                $copyblob = $blob;
     242                $images = get_content_images( $blob, true, true, 1 );
     243                $this->assertEquals( $images, array( $html1 ) );
     244                $this->assertEquals( $blob, $imgless1 );
     245                $blob = $copyblob;
     246
     247                $copyblob = $blob;
     248                $images = get_content_images( $blob, false, true, 1 );
     249                $this->assertEquals( $images, array( $src1 ) );
     250                $this->assertEquals( $blob, $imgless1 );
     251                $blob = $copyblob;
     252        }
     253
     254        /**
     255         * @ticket 22960
     256         */
     257        function test_content_image() {
     258                $blob =<<<BLOB
     259This is a sentence that will all of a sudden contain {$this->img_html}
     260BLOB;
     261                $imgless = "This is a sentence that will all of a sudden contain ";
     262
     263                $image = get_content_image( $blob );
     264                $this->assertEquals( $image, $this->img_html );
     265
     266                $image = get_content_image( $blob, false );
     267                $this->assertEquals( $image, $this->img_url );
     268
     269                $image = get_content_image( $blob, false, true );
     270                $this->assertEquals( $image, $this->img_url );
     271                $this->assertEquals( $blob, $imgless );
     272        }
     273
     274        /**
     275         * @ticket 22960
     276         */
     277        function test_content_galleries() {
     278                $ids1 = array();
     279                $ids1_srcs = array();
     280                foreach ( range( 1, 3 ) as $i ) {
     281                        $attachment_id = $this->factory->attachment->create_object( "image$i.jpg", 0, array(
     282                                'post_mime_type' => 'image/jpeg',
     283                                'post_type' => 'attachment'
     284                        ) );
     285                        wp_update_attachment_metadata( $attachment_id, $this->img_dimensions );
     286                        $ids1[] = $attachment_id;
     287                        $ids1_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
     288                }
     289
     290                $ids2 = array();
     291                $ids2_srcs = array();
     292                foreach ( range( 4, 6 ) as $i ) {
     293                        $attachment_id = $this->factory->attachment->create_object( "image$i.jpg", 0, array(
     294                                'post_mime_type' => 'image/jpeg',
     295                                'post_type' => 'attachment'
     296                        ) );
     297                        wp_update_attachment_metadata( $attachment_id, $this->img_dimensions );
     298                        $ids2[] = $attachment_id;
     299                        $ids2_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
     300                }
     301
     302                $ids1_joined = join( ',', $ids1 );
     303                $ids2_joined = join( ',', $ids2 );
     304
     305                $blob =<<<BLOB
     306[gallery ids="$ids1_joined"]
     307
     308[gallery ids="$ids2_joined"]
     309BLOB;
     310
     311                $post_id = $this->factory->post->create( array( 'post_content' => $blob ) );
     312                $galleries = get_content_galleries( get_post_field( 'post_content', $post_id ) );
     313                $expected = array(
     314                        array( 'ids' => $ids1_joined, 'src' => $ids1_srcs ),
     315                        array( 'ids' => $ids2_joined, 'src' => $ids2_srcs )
     316                );
     317                $this->assertEquals( $galleries, $expected );
     318        }
     319
     320        /**
     321         * @ticket 22960
     322         */
     323        function test_post_galleries_images() {
     324                $ids1 = array();
     325                $ids1_srcs = array();
     326                foreach ( range( 1, 3 ) as $i ) {
     327                        $attachment_id = $this->factory->attachment->create_object( "image$i.jpg", 0, array(
     328                                'post_mime_type' => 'image/jpeg',
     329                                'post_type' => 'attachment'
     330                        ) );
     331                        wp_update_attachment_metadata( $attachment_id, $this->img_dimensions );
     332                        $ids1[] = $attachment_id;
     333                        $ids1_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
     334                }
     335
     336                $ids2 = array();
     337                $ids2_srcs = array();
     338                foreach ( range( 4, 6 ) as $i ) {
     339                        $attachment_id = $this->factory->attachment->create_object( "image$i.jpg", 0, array(
     340                                'post_mime_type' => 'image/jpeg',
     341                                'post_type' => 'attachment'
     342                        ) );
     343                        wp_update_attachment_metadata( $attachment_id, $this->img_dimensions );
     344                        $ids2[] = $attachment_id;
     345                        $ids2_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
     346                }
     347
     348                $ids1_joined = join( ',', $ids1 );
     349                $ids2_joined = join( ',', $ids2 );
     350
     351                $blob =<<<BLOB
     352[gallery ids="$ids1_joined"]
     353
     354[gallery ids="$ids2_joined"]
     355BLOB;
     356                $post_id = $this->factory->post->create( array( 'post_content' => $blob ) );
     357                $srcs = get_post_galleries_images( $post_id );
     358                $this->assertEquals( $srcs, array( $ids1_srcs, $ids2_srcs ) );
     359        }
     360
     361        /**
     362         * @ticket 22960
     363         */
     364        function test_post_gallery_images() {
     365                $ids1 = array();
     366                $ids1_srcs = array();
     367                foreach ( range( 1, 3 ) as $i ) {
     368                        $attachment_id = $this->factory->attachment->create_object( "image$i.jpg", 0, array(
     369                                'post_mime_type' => 'image/jpeg',
     370                                'post_type' => 'attachment'
     371                        ) );
     372                        wp_update_attachment_metadata( $attachment_id, $this->img_dimensions );
     373                        $ids1[] = $attachment_id;
     374                        $ids1_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
     375                }
     376
     377                $ids2 = array();
     378                $ids2_srcs = array();
     379                foreach ( range( 4, 6 ) as $i ) {
     380                        $attachment_id = $this->factory->attachment->create_object( "image$i.jpg", 0, array(
     381                                'post_mime_type' => 'image/jpeg',
     382                                'post_type' => 'attachment'
     383                        ) );
     384                        wp_update_attachment_metadata( $attachment_id, $this->img_dimensions );
     385                        $ids2[] = $attachment_id;
     386                        $ids2_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
     387                }
     388
     389                $ids1_joined = join( ',', $ids1 );
     390                $ids2_joined = join( ',', $ids2 );
     391
     392                $blob =<<<BLOB
     393[gallery ids="$ids1_joined"]
     394
     395[gallery ids="$ids2_joined"]
     396BLOB;
     397                $post_id = $this->factory->post->create( array( 'post_content' => $blob ) );
     398                $srcs = get_post_gallery_images( $post_id );
     399                $this->assertEquals( $srcs, $ids1_srcs );
     400        }
    172401}
Note: See TracChangeset for help on using the changeset viewer.