Make WordPress Core

Changeset 167 in tests


Ignore:
Timestamp:
03/10/2008 06:44:50 AM (17 years ago)
Author:
tellyworth
Message:

post and attachment tests

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wp-testcase/test_includes_post.php

    r125 r167  
    123123        $this->assertEquals($future_date_2, $this->_next_schedule_for_post('publish_future_post', $id));
    124124    }
    125    
     125
     126    function test_vb_insert_future_draft() {
     127        // insert a draft post with a future date, and make sure no cron schedule is set
     128
     129        $future_date = strtotime('+1 day');
     130
     131        $post = array(
     132            'post_author' => $this->author->ID,
     133            'post_status' => 'draft',
     134            'post_content' => rand_str(),
     135            'post_title' => rand_str(),
     136            'post_date'  => strftime("%Y-%m-%d %H:%M:%S", $future_date),
     137        );
     138
     139        // insert a post and make sure the ID is ok
     140        $id = $this->post_ids[] = wp_insert_post($post);
     141        #dmp(_get_cron_array());
     142        $this->assertTrue(is_numeric($id));
     143        $this->assertTrue($id > 0);
     144
     145        // fetch the post and make sure it matches
     146        $out = wp_get_single_post($id);
     147
     148        $this->assertEquals($post['post_content'], $out->post_content);
     149        $this->assertEquals($post['post_title'], $out->post_title);
     150        $this->assertEquals('draft', $out->post_status);
     151        $this->assertEquals($post['post_author'], $out->post_author);
     152        $this->assertEquals($post['post_date'], $out->post_date);
     153
     154        // there should be a publish_future_post hook scheduled on the future date
     155        $this->assertEquals(false, $this->_next_schedule_for_post('publish_future_post', $id));
     156       
     157    }
     158
     159    function test_vb_insert_future_change_to_draft() {
     160        // insert a future post, then edit and change it to draft, and make sure cron gets it right
     161        $future_date_1 = strtotime('+1 day');
     162
     163        $post = array(
     164            'post_author' => $this->author->ID,
     165            'post_status' => 'publish',
     166            'post_content' => rand_str(),
     167            'post_title' => rand_str(),
     168            'post_date'  => strftime("%Y-%m-%d %H:%M:%S", $future_date_1),
     169        );
     170
     171        // insert a post and make sure the ID is ok
     172        $id = $this->post_ids[] = wp_insert_post($post);
     173
     174        // fetch the post and make sure has the correct date and status
     175        $out = wp_get_single_post($id);
     176        $this->assertEquals('future', $out->post_status);
     177        $this->assertEquals($post['post_date'], $out->post_date);
     178
     179        // check that there's a publish_future_post job scheduled at the right time
     180        $this->assertEquals($future_date_1, $this->_next_schedule_for_post('publish_future_post', $id));
     181
     182        // now save it again with status set to draft
     183
     184        $post['ID'] = $id;
     185        $post['post_status'] = 'draft';
     186        wp_update_post($post);
     187
     188        // fetch the post again and make sure it has the new post_date
     189        $out = wp_get_single_post($id);
     190        $this->assertEquals('draft', $out->post_status);
     191        $this->assertEquals($post['post_date'], $out->post_date);
     192
     193        // and the correct date on the cron job
     194        $this->assertEquals(false, $this->_next_schedule_for_post('publish_future_post', $id));
     195    }
     196
     197    function test_vb_insert_future_change_status() {
     198        // insert a future post, then edit and change the status, and make sure cron gets it right
     199        $future_date_1 = strtotime('+1 day');
     200
     201        $statuses = array('draft', 'static', 'object', 'attachment', 'inherit', 'pending');
     202       
     203        foreach ($statuses as $status) {
     204            $post = array(
     205                'post_author' => $this->author->ID,
     206                'post_status' => 'publish',
     207                'post_content' => rand_str(),
     208                'post_title' => rand_str(),
     209                'post_date'  => strftime("%Y-%m-%d %H:%M:%S", $future_date_1),
     210            );
     211
     212            // insert a post and make sure the ID is ok
     213            $id = $this->post_ids[] = wp_insert_post($post);
     214
     215            // fetch the post and make sure has the correct date and status
     216            $out = wp_get_single_post($id);
     217            $this->assertEquals('future', $out->post_status);
     218            $this->assertEquals($post['post_date'], $out->post_date);
     219
     220            // check that there's a publish_future_post job scheduled at the right time
     221            $this->assertEquals($future_date_1, $this->_next_schedule_for_post('publish_future_post', $id));
     222            var_dump(_get_cron_array());
     223
     224            // now save it again with status changed
     225
     226            $post['ID'] = $id;
     227            $post['post_status'] = $status;
     228            wp_update_post($post);
     229
     230            // fetch the post again and make sure it has the new post_date
     231            $out = wp_get_single_post($id);
     232            $this->assertEquals($status, $out->post_status);
     233            $this->assertEquals($post['post_date'], $out->post_date);
     234
     235            // and the correct date on the cron job
     236            $this->assertEquals(false, $this->_next_schedule_for_post('publish_future_post', $id));
     237        }
     238    }
     239
     240    function test_vb_insert_future_private() {
     241        // insert a draft post with a future date, and make sure no cron schedule is set
     242
     243        $future_date = strtotime('+1 day');
     244
     245        $post = array(
     246            'post_author' => $this->author->ID,
     247            'post_status' => 'private',
     248            'post_content' => rand_str(),
     249            'post_title' => rand_str(),
     250            'post_date'  => strftime("%Y-%m-%d %H:%M:%S", $future_date),
     251        );
     252
     253        // insert a post and make sure the ID is ok
     254        $id = $this->post_ids[] = wp_insert_post($post);
     255        #dmp(_get_cron_array());
     256        $this->assertTrue(is_numeric($id));
     257        $this->assertTrue($id > 0);
     258
     259        // fetch the post and make sure it matches
     260        $out = wp_get_single_post($id);
     261
     262        $this->assertEquals($post['post_content'], $out->post_content);
     263        $this->assertEquals($post['post_title'], $out->post_title);
     264        $this->assertEquals('private', $out->post_status);
     265        $this->assertEquals($post['post_author'], $out->post_author);
     266        $this->assertEquals($post['post_date'], $out->post_date);
     267
     268        // there should be a publish_future_post hook scheduled on the future date
     269        $this->assertEquals(false, $this->_next_schedule_for_post('publish_future_post', $id));
     270    }
     271
     272    function test_vb_insert_future_change_to_private() {
     273        // insert a future post, then edit and change it to private, and make sure cron gets it right
     274        $future_date_1 = strtotime('+1 day');
     275
     276        $post = array(
     277            'post_author' => $this->author->ID,
     278            'post_status' => 'publish',
     279            'post_content' => rand_str(),
     280            'post_title' => rand_str(),
     281            'post_date'  => strftime("%Y-%m-%d %H:%M:%S", $future_date_1),
     282        );
     283
     284        // insert a post and make sure the ID is ok
     285        $id = $this->post_ids[] = wp_insert_post($post);
     286
     287        // fetch the post and make sure has the correct date and status
     288        $out = wp_get_single_post($id);
     289        $this->assertEquals('future', $out->post_status);
     290        $this->assertEquals($post['post_date'], $out->post_date);
     291
     292        // check that there's a publish_future_post job scheduled at the right time
     293        $this->assertEquals($future_date_1, $this->_next_schedule_for_post('publish_future_post', $id));
     294
     295        // now save it again with status set to draft
     296
     297        $post['ID'] = $id;
     298        $post['post_status'] = 'private';
     299        wp_update_post($post);
     300
     301        // fetch the post again and make sure it has the new post_date
     302        $out = wp_get_single_post($id);
     303        $this->assertEquals('private', $out->post_status);
     304        $this->assertEquals($post['post_date'], $out->post_date);
     305
     306        // and the correct date on the cron job
     307        $this->assertEquals(false, $this->_next_schedule_for_post('publish_future_post', $id));
     308    }
     309           
    126310    function test_delete_future_post_cron() {
    127311        // http://trac.wordpress.org/ticket/5364
    128312        // "When I delete a future post using wp_delete_post($post->ID) it does not update the cron correctly."
    129313       
    130         $this->knownWPBug(5364);
     314        #$this->knownWPBug(5364);
    131315       
    132316        $future_date = strtotime('+1 day');
     
    149333        wp_delete_post($id);
    150334        $this->assertFalse($this->_next_schedule_for_post('publish_future_post', $id));
    151        
    152335    }
    153336
     
    177360        $wp_rewrite->set_permalink_structure('');
    178361    }
     362   
     363    function test_attachment_url() {
     364    }
    179365}
    180366
     367class WPTestAttachments extends _WPEmptyBlog {
     368   
     369    function tearDown() {
     370        parent::tearDown();
     371        // restore system defaults
     372        update_option('medium_size_w', '');
     373        update_option('medium_size_h', '');
     374        update_option('thumbnail_size_w', 150);
     375        update_option('thumbnail_size_h', 150);
     376    }
     377   
     378    function _make_attachment($upload, $parent_post_id=-1) {
     379       
     380        $type = '';
     381        if ( !empty($upload['type']) )
     382            $type = $upload['type'];
     383        else {
     384            $mime = wp_check_filetype( $upload['file'] );
     385            if ($mime)
     386                $type = $mime['type'];
     387        }
     388       
     389        $attachment = array(
     390            'post_title' => basename( $upload['file'] ),
     391            'post_content' => '',
     392            'post_type' => 'attachment',
     393            'post_parent' => $parent_post_id,
     394            'post_mime_type' => $type,
     395            'guid' => $upload[ 'url' ],
     396        );
     397
     398        // Save the data
     399        $id = wp_insert_attachment( $attachment, $upload[ 'file' ], $parent_post_id );
     400        wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) );
     401       
     402        return $this->ids[] = $id;
     403       
     404    }
     405   
     406    function test_insert_bogus_image() {
     407        $filename = rand_str().'.jpg';
     408        $contents = rand_str();
     409       
     410        $upload = wp_upload_bits($filename, 'image/jpeg', $contents);
     411        $this->assertTrue( empty($upload['error']) );
     412       
     413        $id = $this->_make_attachment($upload);
     414    }
     415   
     416    function test_insert_image_no_thumb() {
     417       
     418        // this image is smaller than the thumbnail size so it won't have one
     419        $filename = ( DIR_TESTDATA.'/images/test-image.jpg' );
     420        $contents = file_get_contents($filename);
     421       
     422        $upload = wp_upload_bits($filename, 'image/jpeg', $contents);
     423        $this->assertTrue( empty($upload['error']) );
     424
     425        $id = $this->_make_attachment($upload);
     426
     427        // intermediate copies should not exist
     428        $this->assertFalse( image_get_intermediate_size($id, 'thumbnail') );
     429        $this->assertFalse( image_get_intermediate_size($id, 'medium') );
     430
     431        // the thumb url should point to the thumbnail intermediate
     432        $this->assertEquals( $thumb['url'], wp_get_attachment_thumb_url($id) );
     433       
     434        // image_downsize() should return the correct images and sizes
     435        $this->assertFalse( image_downsize($id, 'thumbnail') );
     436        $this->assertFalse( image_downsize($id, 'medium') );
     437
     438        var_dump(wp_get_attachment_metadata($id));
     439        $downsize = image_downsize($id, 'full');
     440        var_dump($downsize);
     441        $this->assertEquals( 'a2-small.jpg', basename($downsize[0]) );
     442        $this->assertEquals( 400, $downsize[1] );
     443        $this->assertEquals( 300, $downsize[2] );
     444    }
     445
     446    function test_insert_image_default_thumb() {
     447       
     448        // this image is smaller than the thumbnail size so it won't have one
     449        $filename = ( DIR_TESTDATA.'/images/a2-small.jpg' );
     450        $contents = file_get_contents($filename);
     451       
     452        $upload = wp_upload_bits($filename, 'image/jpeg', $contents);
     453        $this->assertTrue( empty($upload['error']) );
     454
     455        $id = $this->_make_attachment($upload);
     456
     457        // intermediate copies should exist: thumbnail only
     458        $thumb = image_get_intermediate_size($id, 'thumbnail');
     459        $this->assertEquals( 'a2-small-150x150.jpg', $thumb['file'] );
     460        $this->assertTrue( is_file($thumb['path']) );
     461       
     462        $this->assertFalse( image_get_intermediate_size($id, 'medium') );
     463
     464        // the thumb url should point to the thumbnail intermediate
     465        $this->assertEquals( $thumb['url'], wp_get_attachment_thumb_url($id) );
     466       
     467        // image_downsize() should return the correct images and sizes
     468        $downsize = image_downsize($id, 'thumbnail');
     469        $this->assertEquals( 'a2-small-150x150.jpg', basename($downsize[0]) );
     470        $this->assertEquals( 150, $downsize[1] );
     471        $this->assertEquals( 150, $downsize[2] );
     472
     473        $downsize = image_downsize($id, 'full');
     474        $this->assertEquals( 'a2-small.jpg', basename($downsize[0]) );
     475        $this->assertEquals( 400, $downsize[1] );
     476        $this->assertEquals( 300, $downsize[2] );
     477
     478    }
     479
     480    function test_insert_image_medium() {
     481
     482        update_option('medium_size_w', '400');
     483        update_option('medium_size_h', '');
     484       
     485        // this image is smaller than the thumbnail size so it won't have one
     486        $filename = ( DIR_TESTDATA.'/images/2007-06-17DSC_4173.JPG' );
     487        $contents = file_get_contents($filename);
     488       
     489        $upload = wp_upload_bits($filename, 'image/jpeg', $contents);
     490        $this->assertTrue( empty($upload['error']) );
     491
     492        $id = $this->_make_attachment($upload);
     493           
     494        // intermediate copies should exist: thumbnail and medium
     495        $thumb = image_get_intermediate_size($id, 'thumbnail');
     496        $this->assertEquals( '2007-06-17dsc_4173-150x150.jpg', $thumb['file'] );
     497        $this->assertTrue( is_file($thumb['path']) );
     498
     499        $medium = image_get_intermediate_size($id, 'medium');
     500        $this->assertEquals( '2007-06-17dsc_4173-400x602.jpg', $medium['file'] );
     501        $this->assertTrue( is_file($medium['path']) );
     502       
     503        // the thumb url should point to the thumbnail intermediate
     504        $this->assertEquals( $thumb['url'], wp_get_attachment_thumb_url($id) );
     505       
     506        // image_downsize() should return the correct images and sizes
     507        $downsize = image_downsize($id, 'thumbnail');
     508        $this->assertEquals( '2007-06-17dsc_4173-150x150.jpg', basename($downsize[0]) );
     509        $this->assertEquals( 150, $downsize[1] );
     510        $this->assertEquals( 150, $downsize[2] );
     511       
     512        $downsize = image_downsize($id, 'medium');
     513        $this->assertEquals( '2007-06-17dsc_4173-400x602.jpg', basename($downsize[0]) );
     514        $this->assertEquals( 400, $downsize[1] );
     515        $this->assertEquals( 602, $downsize[2] );
     516       
     517        $downsize = image_downsize($id, 'full');
     518        $this->assertEquals( '2007-06-17dsc_4173.jpg', basename($downsize[0]) );
     519        $this->assertEquals( 500, $downsize[1] );
     520        $this->assertEquals( 752, $downsize[2] );
     521    }
     522
     523
     524    function test_insert_image_delete() {
     525
     526        update_option('medium_size_w', '400');
     527        update_option('medium_size_h', '');
     528       
     529        // this image is smaller than the thumbnail size so it won't have one
     530        $filename = ( DIR_TESTDATA.'/images/2007-06-17DSC_4173.JPG' );
     531        $contents = file_get_contents($filename);
     532       
     533        $upload = wp_upload_bits($filename, 'image/jpeg', $contents);
     534        $this->assertTrue( empty($upload['error']) );
     535
     536        $id = $this->_make_attachment($upload);
     537       
     538        // check that the file and intermediates exist
     539        $thumb = image_get_intermediate_size($id, 'thumbnail');
     540        $this->assertEquals( '2007-06-17dsc_4173-150x150.jpg', $thumb['file'] );
     541        $this->assertTrue( is_file($thumb['path']) );
     542
     543        $medium = image_get_intermediate_size($id, 'medium');
     544        $this->assertEquals( '2007-06-17dsc_4173-400x602.jpg', $medium['file'] );
     545        $this->assertTrue( is_file($medium['path']) );
     546
     547        $meta = wp_get_attachment_metadata($id);
     548        $original = $meta['file'];
     549        $this->assertTrue( is_file($original) );
     550       
     551        // now delete the attachment and make sure all files are gone
     552        wp_delete_attachment($id);
     553
     554        $this->assertFalse( is_file($thumb['path']) );
     555        $this->assertFalse( is_file($medium['path']) );
     556        $this->assertFalse( is_file($original) );
     557    }
     558
     559}
     560
    181561?>
Note: See TracChangeset for help on using the changeset viewer.