-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAdminScreenTest.php
More file actions
259 lines (196 loc) · 8.63 KB
/
Copy pathAdminScreenTest.php
File metadata and controls
259 lines (196 loc) · 8.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<?php
declare(strict_types=1);
namespace n5s\PageForCustomPostType\Tests\Integration;
use Mantle\Testing\Concerns\Admin_Screen;
use n5s\PageForCustomPostType\Admin\Admin;
use n5s\PageForCustomPostType\Plugin;
use n5s\PageForCustomPostType\Tests\Fixtures\TestCase;
class AdminScreenTest extends TestCase
{
use Admin_Screen;
private Admin $admin;
protected function setUp(): void
{
parent::setUp();
$this->createFixtures();
// Load admin includes needed for settings API
if (!\function_exists('add_settings_section')) {
require_once \ABSPATH . 'wp-admin/includes/template.php';
}
if (!\function_exists('add_submenu_page')) {
require_once \ABSPATH . 'wp-admin/includes/plugin.php';
}
$container = Plugin::getInstance()->getContainer();
$this->admin = $container->get(Admin::class);
}
public function testAddReadingSettingsRegistersSettingsSection(): void
{
$this->admin->addReadingSettings();
$this->assertArrayHasKey(
'page_for_custom_post_type',
$GLOBALS['wp_settings_sections']['reading'],
);
}
public function testAddReadingSettingsRegistersFieldsForEachPostType(): void
{
$this->admin->addReadingSettings();
$fields = $GLOBALS['wp_settings_fields']['reading']['page_for_custom_post_type'] ?? [];
$this->assertArrayHasKey('page_for_' . self::BOOK_POST_TYPE, $fields);
$this->assertArrayHasKey('page_for_' . self::BIKE_POST_TYPE, $fields);
}
public function testAddReadingSettingsRegistersSettingsForEachPostType(): void
{
$this->admin->addReadingSettings();
$registeredSettings = get_registered_settings();
$this->assertArrayHasKey('page_for_' . self::BOOK_POST_TYPE, $registeredSettings);
$this->assertArrayHasKey('page_for_' . self::BIKE_POST_TYPE, $registeredSettings);
}
public function testRenderPageDropdownOutputsSelectElement(): void
{
$this->acting_as('administrator');
$this->admin->addReadingSettings();
ob_start();
do_settings_fields('reading', 'page_for_custom_post_type');
$output = ob_get_clean();
$this->assertStringContainsString('<select', $output);
$this->assertStringContainsString('page_for_' . self::BOOK_POST_TYPE, $output);
$this->assertStringContainsString('_use_slug', $output);
}
public function testAddPostSubmenusAddsSubmenus(): void
{
$postsPageId = static::factory()->post->create(['post_type' => 'page', 'post_status' => 'publish']);
update_option('page_for_posts', $postsPageId);
$this->acting_as('administrator');
$this->admin->addPostTypeSubmenus();
// Check submenus was added for posts
$postMenuKey = 'edit.php';
$this->assertArrayHasKey($postMenuKey, $GLOBALS['submenu'] ?? []);
}
public function testAddCustomPostTypeSubmenusAddsSubmenus(): void
{
$this->acting_as('administrator');
$this->admin->addPostTypeSubmenus();
// Check submenus were added for at least one custom post type
$bookMenuKey = 'edit.php?post_type=' . self::BOOK_POST_TYPE;
$this->assertArrayHasKey($bookMenuKey, $GLOBALS['submenu'] ?? []);
}
public function testGetExcludedPageIdsExcludesFrontPageAndPostsPage(): void
{
$frontPageId = static::factory()->post->create(['post_type' => 'page', 'post_status' => 'publish']);
$postsPageId = static::factory()->post->create(['post_type' => 'page', 'post_status' => 'publish']);
update_option('page_on_front', $frontPageId);
update_option('page_for_posts', $postsPageId);
$method = new \ReflectionMethod(Admin::class, 'getExcludedPageIds');
$result = $method->invoke($this->admin);
$this->assertContains($frontPageId, $result);
$this->assertContains($postsPageId, $result);
}
public function testGetExcludedPageIdsReturnsEmptyWhenNoPagesSet(): void
{
delete_option('page_on_front');
delete_option('page_for_posts');
$method = new \ReflectionMethod(Admin::class, 'getExcludedPageIds');
$result = $method->invoke($this->admin);
$this->assertEmpty($result);
}
public function testGetDefaultLabelWithArchive(): void
{
// Need to ensure the PostType service has stored original args.
// Re-register to trigger the filter which stores them.
$this->reRegisterPostType(self::BOOK_POST_TYPE);
$method = new \ReflectionMethod(Admin::class, 'getDefaultLabel');
$result = $method->invoke($this->admin, self::BOOK_POST_TYPE);
$this->assertIsString($result);
$this->assertStringContainsString('Default', $result);
}
public function testGetDefaultLabelWithoutArchive(): void
{
register_post_type('noarchive_cpt', [
'public' => true,
'publicly_queryable' => true,
'has_archive' => false,
'label' => 'No Archive CPT',
]);
$method = new \ReflectionMethod(Admin::class, 'getDefaultLabel');
$result = $method->invoke($this->admin, 'noarchive_cpt');
$this->assertIsString($result);
$this->assertStringContainsString('No archive', $result);
unregister_post_type('noarchive_cpt');
}
public function testGetDefaultLabelReturnsNullWhenNoOriginalArgs(): void
{
$method = new \ReflectionMethod(Admin::class, 'getDefaultLabel');
$result = $method->invoke($this->admin, 'nonexistent_cpt');
$this->assertNull($result);
}
public function testAddAdminBarArchiveLinkReturnsEarlyWithNoScreen(): void
{
$this->acting_as('administrator');
// WP_Admin_Bar requires admin includes
if (!\class_exists('WP_Admin_Bar')) {
require_once \ABSPATH . \WPINC . '/class-wp-admin-bar.php';
}
$adminBar = new \WP_Admin_Bar();
// No screen set → should return early
$this->admin->addAdminBarArchiveLink($adminBar);
$this->assertNull($adminBar->get_node('archive'));
}
public function testAddAdminBarArchiveLinkAddsMenuOnEditScreen(): void
{
$this->acting_as('administrator');
if (!\class_exists('WP_Admin_Bar')) {
require_once \ABSPATH . \WPINC . '/class-wp-admin-bar.php';
}
if (!\class_exists('WP_Screen')) {
require_once \ABSPATH . 'wp-admin/includes/class-wp-screen.php';
}
if (!\function_exists('set_current_screen')) {
require_once \ABSPATH . 'wp-admin/includes/screen.php';
}
set_current_screen('edit-' . self::BOOK_POST_TYPE);
$adminBar = new \WP_Admin_Bar();
$adminBar->initialize();
$this->admin->addAdminBarArchiveLink($adminBar);
$node = $adminBar->get_node('archive');
$this->assertNotNull($node, 'Archive link should be added to admin bar');
$this->assertStringContainsString('home-for-books', $node->href);
}
public function testAddAdminBarArchiveLinkSkipsNonEditScreen(): void
{
$this->acting_as('administrator');
if (!\class_exists('WP_Admin_Bar')) {
require_once \ABSPATH . \WPINC . '/class-wp-admin-bar.php';
}
if (!\function_exists('set_current_screen')) {
require_once \ABSPATH . 'wp-admin/includes/screen.php';
}
// Set to a non-edit screen (dashboard)
set_current_screen('dashboard');
$adminBar = new \WP_Admin_Bar();
$this->admin->addAdminBarArchiveLink($adminBar);
$this->assertNull($adminBar->get_node('archive'));
}
public function testPluginAdminHooksAreRegisteredInAdminContext(): void
{
$plugin = \n5s\PageForCustomPostType\Plugin::getInstance();
// Reset the initialized flag so init() re-runs in admin context
$ref = new \ReflectionProperty(\n5s\PageForCustomPostType\Plugin::class, 'initialized');
$ref->setValue($plugin, false);
$plugin->init();
// In admin context, admin hooks should be registered
$this->assertNotFalse(has_action('admin_menu'));
$this->assertNotFalse(has_action('admin_init'));
$this->assertNotFalse(has_action('admin_bar_menu'));
$this->assertNotFalse(has_filter('display_post_states'));
}
private function reRegisterPostType(string $postType): void
{
$postTypeObject = get_post_type_object($postType);
if (!$postTypeObject instanceof \WP_Post_Type) {
return;
}
$args = get_object_vars($postTypeObject);
unregister_post_type($postType);
register_post_type($postType, $args);
}
}