Skip to content

Commit

Permalink
Fix static view template bug
Browse files Browse the repository at this point in the history
  • Loading branch information
inc2734 committed Nov 17, 2016
1 parent 48f7b7a commit 9682e08
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 15 deletions.
31 changes: 20 additions & 11 deletions app/views/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] );
}

/**
Expand All @@ -117,33 +116,43 @@ 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,
];

if ( is_404() || is_front_page() || is_search() ) {
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 );
}
Expand Down
11 changes: 7 additions & 4 deletions tests/test-breadcrumbs.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ] );
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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' ],
Expand All @@ -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' ],
Expand Down
156 changes: 156 additions & 0 deletions tests/test-view.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
<?php
class ViewTest extends WP_UnitTestCase {

public function setup() {
global $wp_rewrite;
parent::setup();

$wp_rewrite->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()
);
}
}

0 comments on commit 9682e08

Please sign in to comment.