Last active
May 17, 2016 00:03
-
-
Save s-hiroshi/e778b832f194472eb84ee1ef843360e1 to your computer and use it in GitHub Desktop.
WordPressでWP_Rewriteの単体テストのサンプルです。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* WP_Rewriteテスト | |
* | |
* @package InfoTown | |
* @author Hiroshi Sawai <info@info-town.jp> | |
* @copyright Hiroshi Sawai | |
*/ | |
class InfoTown_Rewrite_Test extends WP_UnitTestCase { | |
/** | |
* @group rewrite | |
*/ | |
public function test_wp_rewrite_empty_rule() { | |
global $wp_rewrite; | |
$rules = $wp_rewrite->rewrite_rules(); | |
$this->assertEmpty( $rules ); | |
} | |
/** | |
* Test permalink. | |
* | |
* @group rewrite | |
*/ | |
public function test_wp_rewrite_permalink() { | |
/** @var WP_Rewrite $wp_rewrite */ | |
global $wp_rewrite; | |
// Set permalink. | |
$wp_rewrite->set_permalink_structure( '/%category%/%post_id%.html' ); | |
// Test rewrite rule is not empty | |
$this->assertNotEmpty( $wp_rewrite->rewrite_rules() ); | |
// Test permalink rule. | |
$this->assertEquals( '/%category%/%post_id%.html', $wp_rewrite->permalink_structure ); | |
$cat = $this->factory->category->create_and_get( | |
[ | |
'name' => 'Sample category', | |
'slug' => 'sample' | |
] | |
); | |
$post_id = $this->factory->post->create( [ 'post_category' => [ $cat->term_id ] ] ); | |
$actual = get_permalink( $post_id ); | |
// Test permalink. | |
$this->assertEquals( | |
'http://example.org/' . $cat->slug . '/' . $post_id . '.html', | |
$actual | |
); | |
} | |
/** | |
* Test extra permastruct. | |
* | |
* @group rewrite | |
*/ | |
public function test_wp_rewrite_extra_permastruct_category() { | |
/** @var WP_Rewrite $wp_rewrite */ | |
global $wp_rewrite; | |
// Set extra rule(category rule). | |
$wp_rewrite->add_rewrite_tag( '%category%', '(.+?)', 'category_name=' ); | |
$wp_rewrite->add_permastruct( 'category', '/category/%category%', false ); | |
$wp_rewrite->flush_rules(); | |
// Set post. | |
$cat = $this->factory->category->create_and_get( | |
[ | |
'name' => 'Test category', | |
'slug' => 'test' | |
] | |
); | |
$this->factory->post->create( [ 'post_category' => [ $cat->term_id ] ] ); | |
$this->go_to( '/category/' . $cat->slug ); | |
// Test rule. | |
$this->assertTrue( is_category() ); | |
$this->assertEquals( 'http://example.org/category/test', get_category_link( $cat->term_id ) ); | |
} | |
/** | |
* @group rewrite | |
*/ | |
public function test_wp_rewrite_extra_permastruct_year() { | |
/** @var WP_Rewrite $wp_rewrite */ | |
global $wp_rewrite; | |
$wp_rewrite->add_rewrite_tag( '%year%', '([0-9]{4})', 'year=' ); | |
$wp_rewrite->add_permastruct( 'year', '/year/%year%', false ); | |
$wp_rewrite->flush_rules(); | |
$date = new \DateTime(); | |
$year = $date->format( 'Y' ); | |
$this->factory->post->create(); | |
$this->go_to( '/year/' . $year ); | |
$this->assertTrue( is_year() ); | |
} | |
/** | |
* @group rewrite | |
*/ | |
public function test_wp_rewrite_extra_permastruct_date() { | |
/** @var WP_Rewrite $wp_rewrite */ | |
global $wp_rewrite; | |
$wp_rewrite->add_permastruct( 'date', '%year%/%monthnum%/%day%', false ); | |
$wp_rewrite->flush_rules(); | |
$date = new \DateTime(); | |
$this->factory->post->create(); | |
$this->go_to( '/date/' . $date->format( 'Y' ) . '/' . $date->format( 'm' ) . '/' . $date->format( 'd' ) ); | |
$this->assertTrue( is_date() ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment