From 9682e0881368260130d5008486800cd019891b48 Mon Sep 17 00:00:00 2001 From: Takashi Kitajima Date: Fri, 18 Nov 2016 00:40:09 +0900 Subject: [PATCH] Fix static view template bug --- app/views/view.php | 31 +++++--- tests/test-breadcrumbs.php | 11 ++- tests/test-view.php | 156 +++++++++++++++++++++++++++++++++++++ 3 files changed, 183 insertions(+), 15 deletions(-) create mode 100644 tests/test-view.php diff --git a/app/views/view.php b/app/views/view.php index 094c4aaa..7910efad 100644 --- a/app/views/view.php +++ b/app/views/view.php @@ -107,8 +107,7 @@ protected function _render() { public function view() { $view = $this->_get_view_args(); $view = apply_filters( 'mimizuku_view', $view ); - $slug = \Mimizuku\App\Models\Config::get( 'app/config/directory', 'views' ); - get_template_part( $slug . '/' . $view['slug'], $view['name'] ); + get_template_part( $view['slug'], $view['name'] ); } /** @@ -117,8 +116,9 @@ public function view() { * @return array */ protected function _get_view_args() { + $slug = \Mimizuku\App\Models\Config::get( 'app/config/directory', 'views' ); $view = [ - 'slug' => $this->view, + 'slug' => $slug . '/' . $this->view, 'name' => $this->view_suffix, ]; @@ -126,24 +126,33 @@ protected function _get_view_args() { return $view; } - $request_uri = $_SERVER['REQUEST_URI']; - $request_uri = $this->_get_relative_path( $request_uri ); - $path = $this->_remove_http_query( $request_uri ); - $path = $this->_remove_paged_slug( $path ); - $path = trim( $path, '/' ); - $slug = \Mimizuku\App\Models\Config::get( 'app/config/directory', 'static' ); - $template_name = $slug . '/' . $path; + $template_name = $this->get_static_view_template_name(); if ( ! locate_template( $template_name . '.php', false ) ) { return $view; } return [ - 'slug' => $path, + 'slug' => $template_name, 'name' => '', ]; } + /** + * Returns static view template name + * + * @return string + */ + public function get_static_view_template_name() { + $request_uri = $_SERVER['REQUEST_URI']; + $request_uri = $this->_get_relative_path( $request_uri ); + $path = $this->_remove_http_query( $request_uri ); + $path = $this->_remove_paged_slug( $path ); + $path = trim( $path, '/' ); + $slug = \Mimizuku\App\Models\Config::get( 'app/config/directory', 'static' ); + return $slug . '/' . $path; + } + protected function _get_relative_path( $uri ) { return str_replace( home_url(), '', $uri ); } diff --git a/tests/test-breadcrumbs.php b/tests/test-breadcrumbs.php index 4284cf5c..73befe12 100644 --- a/tests/test-breadcrumbs.php +++ b/tests/test-breadcrumbs.php @@ -2,8 +2,12 @@ class BreadcrumbsTest extends WP_UnitTestCase { public function setup() { + global $wp_rewrite; parent::setup(); + $wp_rewrite->init(); + $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); + $this->author_id = $this->factory->user->create(); $this->post_ids = $this->factory->post->create_many( 20, [ 'post_author' => $this->author_id ] ); $this->front_page_id = $this->factory->post->create( [ 'post_type' => 'page', 'post_title' => 'HOME' ] ); @@ -33,8 +37,8 @@ public function setup() { wp_set_object_terms( $post_id, get_term( $this->tag_id, 'post_tag' )->slug, 'post_tag' ); } - global $wp_rewrite; - $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); + create_initial_taxonomies(); + $wp_rewrite->flush_rules(); } public function tearDown() { @@ -202,7 +206,6 @@ public function test_post_type_archive() { $this->assertFalse( get_post_type() ); $breadcrumbs = new \Mimizuku\App\Models\Breadcrumbs\Breadcrumbs(); $post_type_object = get_post_type_object( $this->post_type ); - $this->assertFalse( get_post_type() ); $this->assertEquals( [ [ 'title' => 'Home', 'link' => 'http://example.org' ], @@ -214,9 +217,9 @@ public function test_post_type_archive() { // Has posts $custom_post_type_id = $this->factory->post->create( [ 'post_type' => $this->post_type ] ); $this->go_to( get_post_type_archive_link( $this->post_type ) ); + $this->assertNotFalse( get_post_type() ); $breadcrumbs = new \Mimizuku\App\Models\Breadcrumbs\Breadcrumbs(); $post_type_object = get_post_type_object( $this->post_type ); - $this->assertNotFalse( get_post_type() ); $this->assertEquals( [ [ 'title' => 'Home', 'link' => 'http://example.org' ], diff --git a/tests/test-view.php b/tests/test-view.php new file mode 100644 index 00000000..42007d19 --- /dev/null +++ b/tests/test-view.php @@ -0,0 +1,156 @@ +init(); + $wp_rewrite->set_permalink_structure( '/%post_id%/' ); + + $this->view = new \Mimizuku\App\Views\View(); + + $this->author_id = $this->factory->user->create(); + $this->post_ids = $this->factory->post->create_many( 20, [ 'post_author' => $this->author_id ] ); + $this->front_page_id = $this->factory->post->create( [ 'post_type' => 'page', 'post_title' => 'HOME' ] ); + $this->blog_page_id = $this->factory->post->create( [ 'post_type' => 'page', 'post_title' => 'BLOG' ] ); + $this->tag_id = $this->factory->term->create( array( 'taxonomy' => 'post_tag' ) ); + $this->post_type = rand_str( 12 ); + $this->taxonomy = rand_str( 12 ); + + register_post_type( + $this->post_type, + [ + 'public' => true , + 'taxonomies' => ['category'], + 'has_archive' => true + ] + ); + + register_taxonomy( + $this->taxonomy, + $this->post_type, + [ + 'public' => true, + ] + ); + + foreach( $this->post_ids as $post_id ) { + wp_set_object_terms( $post_id, get_term( $this->tag_id, 'post_tag' )->slug, 'post_tag' ); + } + + create_initial_taxonomies(); + $wp_rewrite->flush_rules(); + } + + public function tearDown() { + parent::tearDown(); + + _unregister_post_type( $this->post_type ); + _unregister_taxonomy( $this->taxonomy, $this->post_type ); + } + + public function test_get_static_view_template_name__category() { + $category = get_terms( 'post_tag' )[0]; + $this->go_to( get_term_link( $category ) ); + $this->assertEquals( + 'views/static/tag/' . $category->slug, + $this->view->get_static_view_template_name() + ); + } + + public function test_get_static_view_template_name__post_tag() { + $post_tag = get_terms( 'post_tag' )[0]; + $this->go_to( get_term_link( $post_tag ) ); + $this->assertEquals( + 'views/static/tag/' . $post_tag->slug, + $this->view->get_static_view_template_name() + ); + } + + public function test_get_static_view_template_name__year() { + $newest_post = get_post( $this->post_ids[0] ); + $year = date( 'Y', strtotime( $newest_post->post_date ) ); + $this->go_to( get_year_link( $year ) ); + $this->assertEquals( + 'views/static/date/' . $year, + $this->view->get_static_view_template_name() + ); + } + + public function test_get_static_view_template_name__month() { + $newest_post = get_post( $this->post_ids[0] ); + $year = date( 'Y', strtotime( $newest_post->post_date ) ); + $month = date( 'm', strtotime( $newest_post->post_date ) ); + $this->go_to( get_month_link( $year, $month ) ); + $this->assertEquals( + 'views/static/date/' . $year . '/' . $month, + $this->view->get_static_view_template_name() + ); + } + + public function test_get_static_view_template_name__day() { + $newest_post = get_post( $this->post_ids[0] ); + $year = date( 'Y', strtotime( $newest_post->post_date ) ); + $month = date( 'm', strtotime( $newest_post->post_date ) ); + $day = date( 'd', strtotime( $newest_post->post_date ) ); + $this->go_to( get_day_link( $year, $month, $day ) ); + $this->assertEquals( + 'views/static/date/' . $year . '/' . $month . '/' . $day, + $this->view->get_static_view_template_name() + ); + } + + public function test_get_static_view_template_name__author() { + $newest_post = get_post( $this->post_ids[0] ); + $author = $newest_post->post_author; + $this->go_to( get_author_posts_url( $author ) ); + $this->assertEquals( + 'views/static/author/' . get_the_author_meta( 'user_nicename', $author ), + $this->view->get_static_view_template_name() + ); + } + + public function test_get_static_view_template_name__single() { + // Post + $newest_post = get_post( $this->post_ids[0] ); + $categories = get_the_category( $newest_post ); + $this->go_to( get_permalink( $newest_post ) ); + $this->assertEquals( + 'views/static/' . $this->post_ids[0], + $this->view->get_static_view_template_name() + ); + + // Custom post + $custom_post_type_id = $this->factory->post->create( [ 'post_type' => $this->post_type ] ); + $custom_post = get_post( $custom_post_type_id ); + $this->go_to( get_permalink( $custom_post_type_id ) ); + $post_type_object = get_post_type_object( $custom_post->post_type ); + $this->assertEquals( + 'views/static/' . $post_type_object->name . '/' . $custom_post->post_name, + $this->view->get_static_view_template_name() + ); + } + + public function test_get_static_view_template_name__post_type_archive() { + // No posts + $this->go_to( get_post_type_archive_link( $this->post_type ) ); + $this->assertFalse( get_post_type() ); + $post_type_object = get_post_type_object( $this->post_type ); + $this->assertEquals( + 'views/static/' . $post_type_object->name, + $this->view->get_static_view_template_name() + ); + + // Has posts + $custom_post_type_id = $this->factory->post->create( [ 'post_type' => $this->post_type ] ); + $this->go_to( get_post_type_archive_link( $this->post_type ) ); + $breadcrumbs = new \Mimizuku\App\Models\Breadcrumbs\Breadcrumbs(); + $post_type_object = get_post_type_object( $this->post_type ); + $this->assertNotFalse( get_post_type() ); + $this->assertEquals( + 'views/static/' . $post_type_object->name, + $this->view->get_static_view_template_name() + ); + } +}