Skip to content

Commit 9061105

Browse files
committed
test: re-register fixture post types between tests
EdgeCaseTest::testUnregisteredCptDoesNotHijackQuery calls unregister_post_type('book') without re-registering. Mantle's Preserves_Globals backs up $wp_post_types but not $_wp_post_type_features, so post_type_supports('book', 'title') stayed false for subsequent tests. TSF's get_post_title then short-circuited and the breadcrumb fell back to "Untitled", flaking AutodescriptionTest::testBreadcrumbsOnSinglePostIncludesPfcptPage depending on test order. Extracts the post type registration into TestCase::registerFixturePostTypes() and calls it from both bootstrap.php and createFixtures() so the features global is rebuilt before any test that needs it.
1 parent 52cc94a commit 9061105

2 files changed

Lines changed: 43 additions & 18 deletions

File tree

tests/Fixtures/TestCase.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ protected function setUp(): void
5858
*/
5959
protected function createFixtures(): void
6060
{
61+
// Mantle's Preserves_Globals trait backs up $wp_post_types between tests
62+
// but not $_wp_post_type_features. When a test calls unregister_post_type()
63+
// without re-registering (e.g. EdgeCaseTest::testUnregisteredCptDoesNotHijackQuery),
64+
// post_type_supports() returns false for subsequent tests and downstream
65+
// code (TSF's get_post_title, etc.) falls back to defaults like "Untitled".
66+
self::registerFixturePostTypes();
67+
6168
$this->createBooks();
6269
$this->createBikes();
6370
$this->createHomePages();
@@ -79,6 +86,39 @@ protected function createFixtures(): void
7986
flush_rewrite_rules();
8087
}
8188

89+
/**
90+
* Register the fixture post types (book, bike).
91+
*
92+
* Called from bootstrap.php on suite startup and from createFixtures()
93+
* to guard against tests that leave post types unregistered.
94+
*/
95+
public static function registerFixturePostTypes(): void
96+
{
97+
$postTypes = [
98+
self::BIKE_POST_TYPE => [
99+
'public' => true,
100+
'publicly_queryable' => true,
101+
'label' => 'Bikes',
102+
'has_archive' => true,
103+
'rewrite' => ['slug' => 'bikes'],
104+
],
105+
self::BOOK_POST_TYPE => [
106+
'public' => true,
107+
'publicly_queryable' => true,
108+
'label' => 'Books',
109+
'has_archive' => true,
110+
'rewrite' => ['slug' => 'books'],
111+
],
112+
];
113+
114+
foreach ($postTypes as $postType => $args) {
115+
if (post_type_exists($postType)) {
116+
unregister_post_type($postType);
117+
}
118+
register_post_type($postType, $args);
119+
}
120+
}
121+
82122
/**
83123
* Create book posts with a genre taxonomy term.
84124
*

tests/bootstrap.php

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
declare(strict_types=1);
44

5+
use n5s\PageForCustomPostType\Tests\Fixtures\TestCase;
6+
57
use function Mantle\Testing\manager;
68

79
require_once __DIR__ . '/../vendor/autoload.php';
@@ -35,24 +37,7 @@
3537
}
3638
})
3739
->init(static function (): void {
38-
register_post_type('bike', [
39-
'public' => true,
40-
'publicly_queryable' => true,
41-
'label' => 'Bikes',
42-
'has_archive' => true,
43-
'rewrite' => [
44-
'slug' => 'bikes',
45-
],
46-
]);
47-
register_post_type('book', [
48-
'public' => true,
49-
'publicly_queryable' => true,
50-
'label' => 'Books',
51-
'has_archive' => true,
52-
'rewrite' => [
53-
'slug' => 'books',
54-
],
55-
]);
40+
TestCase::registerFixturePostTypes();
5641
register_taxonomy('genre', 'book', [
5742
'public' => true,
5843
'label' => 'Genres',

0 commit comments

Comments
 (0)