Skip to content

Instantly share code, notes, and snippets.

@s-hiroshi
Created May 5, 2016 07:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save s-hiroshi/ce91f11a9d4be7a389da224deb2f3a39 to your computer and use it in GitHub Desktop.
Save s-hiroshi/ce91f11a9d4be7a389da224deb2f3a39 to your computer and use it in GitHub Desktop.
WordPressの固定ページ単体テストのサンプルです。
/**
* WP_UnitTestCaseのサンプル
*
* @package InfoTown
* @author Hiroshi Sawai <info@info-town.jp>
* @copyright Hiroshi Sawai
*/
class Page_Test extends WP_UnitTestCase {
/**
* @group page
*
* @link http://qiita.com/miya0001/items/8f1a777b455a8bdcbd62
*/
public function test_page() {
$post = $this->factory->post->create_and_get( [
'post_title' => 'Sample Page!',
'post_type' => 'page',
] );
$this->go_to( '/?page_id=' . $post->ID );
$this->assertTrue( is_page() );
$this->expectOutputString( "Sample Page!" );
the_title();
}
/**
* @group page
*
* @link http://qiita.com/miya0001/items/8f1a777b455a8bdcbd62
*/
public function test_page_many() {
$post = $this->factory->post->create_and_get( [
'post_title' => 'Sample Page',
'post_type' => 'page',
] );
$this->factory->post->create_many( 5 );
$this->go_to( '/?page_id=' . $post->ID );
$this->assertTrue( is_page() );
$this->expectOutputString( "Sample Page" );
the_title();
}
/**
* 孫ページ
*
* @group page
*
* @link http://qiita.com/miya0001/items/8f1a777b455a8bdcbd62
*/
public function test_grandson_page() {
$post_parent = $this->factory->post->create_and_get( [
'slug' => 'parent',
'post_title' => 'Parent Page',
'post_type' => 'page',
] );
$post_child1 = $this->factory->post->create_and_get( [
'slug' => 'child1',
'post_title' => 'Child1 Page',
'post_type' => 'page',
'post_parent' => $post_parent->ID,
] );
$post_grandson1 = $this->factory->post->create_and_get( [
'slug' => 'grandson1',
'post_title' => 'Grandson1 Page',
'post_type' => 'page',
'post_parent' => $post_child1->ID,
] );
$this->go_to( '/?page_id=' . $post_grandson1->ID );
$this->assertTrue( is_page() );
$this->expectOutputString( "Grandson1 Page" );
the_title();
}
/**
* wp_list_page
*
* @group page
*/
public function test_wp_list_page() {
$post_parent = $this->factory->post->create_and_get( [
'slug' => 'parent',
'post_title' => 'Parent Page',
'post_type' => 'page',
] );
$post_child1 = $this->factory->post->create_and_get( [
'slug' => 'child1',
'post_title' => 'Child1 Page',
'post_type' => 'page',
'post_parent' => $post_parent->ID,
] );
$this->factory->post->create_and_get( [
'slug' => 'grandson1',
'post_title' => 'Grandson1 Page',
'post_type' => 'page',
'post_parent' => $post_child1->ID,
] );
$this->expectOutputRegex( '/(?=.*Parent Page)(?=.*Child1 Page)(?=.*Grandson1 Page)/s' );
wp_list_pages();
}
/**
* wp_list_page引数あり
*
* @group page
*
* @link https://developer.wordpress.org/reference/classes/walker_page/start_el/
*/
public function test_wp_list_page_custom() {
$post_parent = $this->factory->post->create_and_get( [
'slug' => 'parent',
'post_title' => 'Parent Page',
'post_type' => 'page',
] );
$post_child1 = $this->factory->post->create_and_get( [
'slug' => 'child1',
'post_title' => 'Child1 Page',
'post_type' => 'page',
'post_parent' => $post_parent->ID,
] );
$post_grandson1 = $this->factory->post->create_and_get( [
'slug' => 'grandson1',
'post_title' => 'Grandson1 Page',
'post_type' => 'page',
'post_parent' => $post_child1->ID,
] );
// Parent Page, Child1 Page, Grandson1 Pageの順でマッチします。
$this->expectOutputRegex( '/(?=.*\[Parent Page\])(?=.*\[Child1 Page\])(?=.*\[Grandson1 Page\])/s' );
wp_list_pages( [
'title_li' => 'Test',
'link_after' => '] ',
'link_before' => '[',
] );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment