Skip to content

Commit

Permalink
New test for cron deletion of sitemap with 0 posts
Browse files Browse the repository at this point in the history
  • Loading branch information
mjangda committed Mar 30, 2016
1 parent 941d33c commit 09cc015
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion tests/test_sitemap_creation.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ class WP_Test_Sitemap_Creation extends WP_UnitTestCase {
* Generate posts and build the sitemap
*/
function setup() {
if ( ! class_exists( 'MSM_Sitemap_Builder_Cron' ) ) {
require dirname( dirname( __FILE__ ) ) . '/includes/msm-sitemap-builder-cron.php';
MSM_Sitemap_Builder_Cron::setup();
}

$this->test_base = new MSM_SiteMap_Test();

// Create posts for the last num_days days
Expand Down Expand Up @@ -73,4 +78,40 @@ function test_sitemap_posts_were_created() {
wp_reset_postdata();
}
}
}

/**
* This is a long and convoluted test.
*
* Checks to see that a sitemap for a day is deleted when all posts for that day are deleted too.
*/
function test_delete_empty_sitemap() {
global $wpdb;

list( $sitemap ) = get_posts( array(
'post_type' => Metro_Sitemap::SITEMAP_CPT,
'posts_per_page' => 1,
) );

$sitemap_date = date( 'Y-m-d', strtotime( $sitemap->post_date ) );
list( $year, $month, $day ) = explode( '-', $sitemap_date );
$start_date = $sitemap_date . ' 00:00:00';
$end_date = $sitemap_date . ' 23:59:59';
$post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_status = 'publish' AND post_date >= %s AND post_date <= %s AND post_type = 'post' LIMIT 1", $start_date, $end_date ) );

$expected_total_urls = Metro_Sitemap::get_total_indexed_url_count() - count( $post_ids );

foreach ( $post_ids as $post_id ) {
wp_delete_post( $post_id, true );
}

MSM_Sitemap_Builder_Cron::generate_sitemap_for_year_month_day( array(
'year' => $year,
'month' => $month,
'day' => $day,
) );

$this->assertEmpty( get_post( $sitemap->ID ), 'Sitemap with no posts was not deleted' );
$this->assertEquals( $expected_total_urls, Metro_Sitemap::get_total_indexed_url_count(), 'Mismatch in total indexed URLs' );
$this->assertEquals( 1, did_action( 'msm_delete_sitemap_post' ), 'msm_delete_sitemap_post action did not fire' );
}
}

0 comments on commit 09cc015

Please sign in to comment.