wordpress-develop/tests/phpunit/tests/general/archives.php
Gary Pendergast 8f95800d52 Code is Poetry.
WordPress' code just... wasn't.
This is now dealt with.

Props jrf, pento, netweb, GaryJ, jdgrimes, westonruter, Greg Sherwood from PHPCS, and everyone who's ever contributed to WPCS and PHPCS.
Fixes #41057.



git-svn-id: https://develop.svn.wordpress.org/trunk@42343 602fd350-edb4-49c9-b593-d223f7449a82
2017-11-30 23:09:33 +00:00

174 lines
4.7 KiB
PHP

<?php
/**
* @group general
*/
class Tests_General_Archives extends WP_UnitTestCase {
function setUp() {
parent::setUp();
wp_cache_delete( 'last_changed', 'posts' );
}
/**
* @ticket 23206
*/
function test_get_archives_cache() {
global $wpdb;
self::factory()->post->create_many( 3, array( 'post_type' => 'post' ) );
wp_cache_delete( 'last_changed', 'posts' );
$this->assertFalse( wp_cache_get( 'last_changed', 'posts' ) );
$num_queries = $wpdb->num_queries;
// Cache is not primed, expect 1 query.
$result = wp_get_archives(
array(
'type' => 'monthly',
'echo' => false,
)
);
$this->assertInternalType( 'string', $result );
$this->assertNotEmpty( $time1 = wp_cache_get( 'last_changed', 'posts' ) );
$this->assertEquals( $num_queries + 1, $wpdb->num_queries );
$num_queries = $wpdb->num_queries;
// Cache is primed, expect no queries.
$result = wp_get_archives(
array(
'type' => 'monthly',
'echo' => false,
)
);
$this->assertInternalType( 'string', $result );
$this->assertEquals( $time1, wp_cache_get( 'last_changed', 'posts' ) );
$this->assertEquals( $num_queries, $wpdb->num_queries );
// Change args, resulting in a different query string. Cache is not primed, expect 1 query.
$result = wp_get_archives(
array(
'type' => 'monthly',
'echo' => false,
'order' => 'ASC',
)
);
$this->assertInternalType( 'string', $result );
$this->assertEquals( $time1, wp_cache_get( 'last_changed', 'posts' ) );
$this->assertEquals( $num_queries + 1, $wpdb->num_queries );
$num_queries = $wpdb->num_queries;
// Cache is primed, expect no queries.
$result = wp_get_archives(
array(
'type' => 'monthly',
'echo' => false,
'order' => 'ASC',
)
);
$this->assertInternalType( 'string', $result );
$this->assertEquals( $time1, wp_cache_get( 'last_changed', 'posts' ) );
$this->assertEquals( $num_queries, $wpdb->num_queries );
$num_queries = $wpdb->num_queries;
// Change type. Cache is not primed, expect 1 query.
$result = wp_get_archives(
array(
'type' => 'yearly',
'echo' => false,
)
);
$this->assertInternalType( 'string', $result );
$this->assertEquals( $time1, wp_cache_get( 'last_changed', 'posts' ) );
$this->assertEquals( $num_queries + 1, $wpdb->num_queries );
$num_queries = $wpdb->num_queries;
// Cache is primed, expect no queries.
$result = wp_get_archives(
array(
'type' => 'yearly',
'echo' => false,
)
);
$this->assertInternalType( 'string', $result );
$this->assertEquals( $time1, wp_cache_get( 'last_changed', 'posts' ) );
$this->assertEquals( $num_queries, $wpdb->num_queries );
// Change type. Cache is not primed, expect 1 query.
$result = wp_get_archives(
array(
'type' => 'daily',
'echo' => false,
)
);
$this->assertInternalType( 'string', $result );
$this->assertEquals( $time1, wp_cache_get( 'last_changed', 'posts' ) );
$this->assertEquals( $num_queries + 1, $wpdb->num_queries );
$num_queries = $wpdb->num_queries;
// Cache is primed, expect no queries.
$result = wp_get_archives(
array(
'type' => 'daily',
'echo' => false,
)
);
$this->assertInternalType( 'string', $result );
$this->assertEquals( $time1, wp_cache_get( 'last_changed', 'posts' ) );
$this->assertEquals( $num_queries, $wpdb->num_queries );
// Change type. Cache is not primed, expect 1 query.
$result = wp_get_archives(
array(
'type' => 'weekly',
'echo' => false,
)
);
$this->assertInternalType( 'string', $result );
$this->assertEquals( $time1, wp_cache_get( 'last_changed', 'posts' ) );
$this->assertEquals( $num_queries + 1, $wpdb->num_queries );
$num_queries = $wpdb->num_queries;
// Cache is primed, expect no queries.
$result = wp_get_archives(
array(
'type' => 'weekly',
'echo' => false,
)
);
$this->assertInternalType( 'string', $result );
$this->assertEquals( $time1, wp_cache_get( 'last_changed', 'posts' ) );
$this->assertEquals( $num_queries, $wpdb->num_queries );
// Change type. Cache is not primed, expect 1 query.
$result = wp_get_archives(
array(
'type' => 'postbypost',
'echo' => false,
)
);
$this->assertInternalType( 'string', $result );
$this->assertEquals( $time1, wp_cache_get( 'last_changed', 'posts' ) );
$this->assertEquals( $num_queries + 1, $wpdb->num_queries );
$num_queries = $wpdb->num_queries;
// Cache is primed, expect no queries.
$result = wp_get_archives(
array(
'type' => 'postbypost',
'echo' => false,
)
);
$this->assertInternalType( 'string', $result );
$this->assertEquals( $time1, wp_cache_get( 'last_changed', 'posts' ) );
$this->assertEquals( $num_queries, $wpdb->num_queries );
}
}