wpdb = $this->useWpdbSpy();
// Pin the year so the functions do not go looking for the newest comic.
$_GET['archive_year'] = '2020';
}
protected function tearDown(): void {
unset( $_GET['archive_year'] );
parent::tearDown();
}
public function testByYearWithoutChapterQueriesPublishedComics() {
ceo_archive_list_by_year( false, 'ASC', 0 );
$sql = $this->wpdb->last();
$this->assertStringContainsString( 'DISTINCT YEAR(post_date)', $sql );
$this->assertStringContainsString( "post_type='comic'", $sql );
$this->assertStringContainsString( "post_status = 'publish'", $sql );
}
public function testByYearWithChapterJoinsTheChaptersTaxonomy() {
ceo_archive_list_by_year( false, 'ASC', 5 );
$sql = $this->wpdb->last();
$this->assertStringContainsString( 'term_relationships', $sql );
$this->assertStringContainsString( 'term_taxonomy', $sql );
$this->assertStringContainsString( "taxonomy = 'chapters'", $sql );
}
public function testByAllYearsWithChapterJoinsTheChaptersTaxonomy() {
ceo_archive_list_by_all_years( false, 'ASC', 5 );
$this->assertStringContainsString( "taxonomy = 'chapters'", $this->wpdb->last() );
}
/**
* Table names must come from $wpdb rather than being hardcoded, or the query silently
* returns nothing on any site whose prefix is not the default.
*/
public function testArchiveQueriesUseTheConfiguredTablePrefix() {
$this->wpdb->posts = 'xyz_posts';
ceo_archive_list_by_year( false, 'ASC', 0 );
$sql = $this->wpdb->last();
$this->assertStringContainsString( 'xyz_posts', $sql );
$this->assertStringNotContainsString( 'wp_posts', $sql );
}
public function testByYearRendersTheSelectedYearHeading() {
$out = ceo_archive_list_by_year( false, 'ASC', 0 );
$this->assertStringContainsString( '2020', $out );
$this->assertStringContainsString( 'archive-yearlist', $out );
}
/**
* archive_year comes from the query string and is cast to int before use, so a
* non-numeric value must not reach the output.
*/
public function testArchiveYearFromQueryStringIsCastToInteger() {
$_GET['archive_year'] = '2020';
$out = ceo_archive_list_by_year( false, 'ASC', 0 );
$this->assertStringNotContainsString( '