Skip to content

Commit 5a08925

Browse files
Tsvetan StoychevTsvetan Stoychev
authored andcommitted
Fix page type detector test coverage
1 parent 16e3dac commit 5a08925

3 files changed

Lines changed: 161 additions & 15 deletions

File tree

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<?php
2+
/**
3+
* Integration tests for page type detection against real WordPress queries.
4+
*
5+
* @package Basicrum\Tests\Integration
6+
*/
7+
8+
namespace Basicrum\WP\Tests\Integration;
9+
10+
use Basicrum\WP\PageTypeDetector;
11+
12+
/**
13+
* Verifies custom content is classified using real WordPress conditionals.
14+
*/
15+
class PageTypeDetectorIntegrationTest extends \WP_UnitTestCase {
16+
17+
/**
18+
* Custom post type used by the test fixture.
19+
*
20+
* @var string
21+
*/
22+
const POST_TYPE = 'basicrum_test_item';
23+
24+
/**
25+
* Custom taxonomy used by the test fixture.
26+
*
27+
* @var string
28+
*/
29+
const TAXONOMY = 'basicrum_genre';
30+
31+
/**
32+
* Register custom content types before each test.
33+
*
34+
* @return void
35+
*/
36+
public function set_up() {
37+
parent::set_up();
38+
39+
register_post_type(
40+
self::POST_TYPE,
41+
array(
42+
'public' => true,
43+
'query_var' => true,
44+
'rewrite' => false,
45+
)
46+
);
47+
48+
register_taxonomy(
49+
self::TAXONOMY,
50+
'post',
51+
array(
52+
'public' => true,
53+
'query_var' => true,
54+
'rewrite' => false,
55+
)
56+
);
57+
}
58+
59+
/**
60+
* Unregister custom content types after each test.
61+
*
62+
* @return void
63+
*/
64+
public function tear_down() {
65+
unregister_post_type( self::POST_TYPE );
66+
unregister_taxonomy( self::TAXONOMY );
67+
68+
parent::tear_down();
69+
}
70+
71+
/**
72+
* A single custom post must not be classified as a standard post.
73+
*
74+
* @return void
75+
*/
76+
public function test_detects_custom_post_from_main_query() {
77+
$post_id = self::factory()->post->create(
78+
array(
79+
'post_name' => 'basicrum-integration-item',
80+
'post_type' => self::POST_TYPE,
81+
)
82+
);
83+
84+
$this->go_to(
85+
add_query_arg(
86+
self::POST_TYPE,
87+
get_post_field( 'post_name', $post_id ),
88+
home_url( '/' )
89+
)
90+
);
91+
92+
$this->assertTrue( is_singular( self::POST_TYPE ) );
93+
$this->assertFalse( is_singular( 'post' ) );
94+
$this->assertSame( 'custom_post', ( new PageTypeDetector() )->detect() );
95+
}
96+
97+
/**
98+
* A custom taxonomy query must use the custom taxonomy page type.
99+
*
100+
* @return void
101+
*/
102+
public function test_detects_custom_taxonomy_from_main_query() {
103+
$post_id = self::factory()->post->create();
104+
$term = wp_insert_term( 'Performance', self::TAXONOMY );
105+
106+
$this->assertIsArray( $term );
107+
wp_set_object_terms( $post_id, (int) $term['term_id'], self::TAXONOMY );
108+
109+
$this->go_to(
110+
add_query_arg(
111+
self::TAXONOMY,
112+
'performance',
113+
home_url( '/' )
114+
)
115+
);
116+
117+
$this->assertTrue( is_tax( self::TAXONOMY ) );
118+
$this->assertSame( 'taxonomy_archive', ( new PageTypeDetector() )->detect() );
119+
}
120+
}

‎plugins/basicrum/tests/unit/AssetsTest.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ protected function set_up() {
4646

4747
// Stub page type conditionals (default: unknown).
4848
$conditionals = array(
49-
'is_front_page', 'is_single', 'is_page', 'is_category',
50-
'is_tag', 'is_author', 'is_date', 'is_archive', 'is_search', 'is_404',
49+
'is_front_page', 'is_singular', 'is_page', 'is_category',
50+
'is_tag', 'is_tax', 'is_author', 'is_date', 'is_archive', 'is_search', 'is_404',
5151
);
5252
foreach ( $conditionals as $func ) {
5353
Functions\when( $func )->justReturn( false );

‎plugins/basicrum/tests/unit/PageTypeDetectorTest.php‎

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ protected function set_up() {
3939
private function stub_conditionals( $true_conditionals = array() ) {
4040
$wp_conditionals = array(
4141
'is_front_page',
42-
'is_single',
4342
'is_page',
4443
'is_category',
4544
'is_tag',
45+
'is_tax',
4646
'is_author',
4747
'is_date',
4848
'is_archive',
@@ -55,6 +55,16 @@ private function stub_conditionals( $true_conditionals = array() ) {
5555
Functions\when( $func )->justReturn( $return );
5656
}
5757

58+
Functions\when( 'is_singular' )->alias(
59+
function( $post_types = '' ) use ( $true_conditionals ) {
60+
if ( 'post' === $post_types ) {
61+
return in_array( 'is_singular_post', $true_conditionals, true );
62+
}
63+
64+
return in_array( 'is_singular', $true_conditionals, true );
65+
}
66+
);
67+
5868
Functions\when( 'apply_filters' )->alias( function() {
5969
$args = func_get_args();
6070
return $args[1];
@@ -73,40 +83,56 @@ public function test_detects_home_page() {
7383
* Test detection of single post.
7484
*/
7585
public function test_detects_single_post() {
76-
$this->stub_conditionals( array( 'is_single' ) );
86+
$this->stub_conditionals( array( 'is_singular_post', 'is_singular' ) );
7787
$this->assertSame( 'post', $this->detector->detect() );
7888
}
7989

8090
/**
8191
* Test detection of page.
8292
*/
8393
public function test_detects_page() {
84-
$this->stub_conditionals( array( 'is_page' ) );
94+
$this->stub_conditionals( array( 'is_page', 'is_singular' ) );
8595
$this->assertSame( 'page', $this->detector->detect() );
8696
}
8797

98+
/**
99+
* Test detection of a custom post type.
100+
*/
101+
public function test_detects_custom_post() {
102+
$this->stub_conditionals( array( 'is_singular' ) );
103+
$this->assertSame( 'custom_post', $this->detector->detect() );
104+
}
105+
88106
/**
89107
* Test detection of category archive.
90108
*/
91109
public function test_detects_category() {
92110
$this->stub_conditionals( array( 'is_category' ) );
93-
$this->assertSame( 'category', $this->detector->detect() );
111+
$this->assertSame( 'category_archive', $this->detector->detect() );
94112
}
95113

96114
/**
97115
* Test detection of tag archive.
98116
*/
99117
public function test_detects_tag() {
100118
$this->stub_conditionals( array( 'is_tag' ) );
101-
$this->assertSame( 'tag', $this->detector->detect() );
119+
$this->assertSame( 'tag_archive', $this->detector->detect() );
120+
}
121+
122+
/**
123+
* Test detection of a custom taxonomy archive.
124+
*/
125+
public function test_detects_taxonomy_archive() {
126+
$this->stub_conditionals( array( 'is_tax' ) );
127+
$this->assertSame( 'taxonomy_archive', $this->detector->detect() );
102128
}
103129

104130
/**
105131
* Test detection of author archive.
106132
*/
107133
public function test_detects_author() {
108134
$this->stub_conditionals( array( 'is_author' ) );
109-
$this->assertSame( 'author', $this->detector->detect() );
135+
$this->assertSame( 'author_archive', $this->detector->detect() );
110136
}
111137

112138
/**
@@ -170,8 +196,8 @@ public function test_returns_unknown_when_nothing_matches() {
170196
*/
171197
public function test_page_type_filter_is_applied() {
172198
$wp_conditionals = array(
173-
'is_front_page', 'is_single', 'is_page', 'is_category',
174-
'is_tag', 'is_author', 'is_date', 'is_archive', 'is_search', 'is_404',
199+
'is_front_page', 'is_singular', 'is_page', 'is_category',
200+
'is_tag', 'is_tax', 'is_author', 'is_date', 'is_archive', 'is_search', 'is_404',
175201
);
176202
foreach ( $wp_conditionals as $func ) {
177203
Functions\when( $func )->justReturn( false );
@@ -197,8 +223,8 @@ public function test_page_type_filter_is_applied() {
197223
private function stub_woocommerce_conditionals( $true_conditionals = array() ) {
198224
// Standard WP conditionals - all false.
199225
$wp_conditionals = array(
200-
'is_front_page', 'is_single', 'is_page', 'is_category',
201-
'is_tag', 'is_author', 'is_date', 'is_archive', 'is_search', 'is_404',
226+
'is_front_page', 'is_singular', 'is_page', 'is_category',
227+
'is_tag', 'is_tax', 'is_author', 'is_date', 'is_archive', 'is_search', 'is_404',
202228
);
203229
foreach ( $wp_conditionals as $func ) {
204230
Functions\when( $func )->justReturn( false );
@@ -293,9 +319,9 @@ public function test_detects_woocommerce_account() {
293319
* Test WooCommerce types take priority over WP types.
294320
*/
295321
public function test_woocommerce_takes_priority_over_wordpress() {
296-
$this->stub_woocommerce_conditionals( array( 'is_product', 'is_single' ) );
297-
// is_single would normally return 'post', but WooCommerce product should win.
298-
Functions\when( 'is_single' )->justReturn( true );
322+
$this->stub_woocommerce_conditionals( array( 'is_product' ) );
323+
// A singular post would normally return 'post', but WooCommerce should win.
324+
Functions\when( 'is_singular' )->justReturn( true );
299325
$this->assertSame( 'product', $this->detector->detect() );
300326
}
301327
}

0 commit comments

Comments
 (0)