|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Tests for scoped module asset loading. |
| 4 | + * |
| 5 | + * @package Automattic\EditFlow\Tests\Integration |
| 6 | + * @see https://github.com/Automattic/edit-flow/issues/351 |
| 7 | + */ |
| 8 | + |
| 9 | +declare( strict_types=1 ); |
| 10 | + |
| 11 | +namespace Automattic\EditFlow\Tests\Integration; |
| 12 | + |
| 13 | +use EF_Module; |
| 14 | +use Yoast\WPTestUtils\WPIntegration\TestCase; |
| 15 | + |
| 16 | +/** |
| 17 | + * Test that is_post_management_page() properly scopes asset loading. |
| 18 | + * |
| 19 | + * Module assets should only load on pages where the module functionality is needed, |
| 20 | + * not on every admin page. |
| 21 | + */ |
| 22 | +class ModuleAssetScopingTest extends TestCase { |
| 23 | + |
| 24 | + protected static $admin_user_id; |
| 25 | + |
| 26 | + public static function wpSetUpBeforeClass( $factory ) { |
| 27 | + self::$admin_user_id = $factory->user->create( array( 'role' => 'administrator' ) ); |
| 28 | + } |
| 29 | + |
| 30 | + public static function wpTearDownAfterClass() { |
| 31 | + self::delete_user( self::$admin_user_id ); |
| 32 | + } |
| 33 | + |
| 34 | + protected function setUp(): void { |
| 35 | + parent::setUp(); |
| 36 | + wp_set_current_user( self::$admin_user_id ); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Get a module instance for testing. |
| 41 | + * |
| 42 | + * @return EF_Module |
| 43 | + */ |
| 44 | + protected function get_test_module() { |
| 45 | + global $edit_flow; |
| 46 | + // Use notifications module as test subject since it uses is_post_management_page(). |
| 47 | + return $edit_flow->notifications; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Test that is_post_management_page returns false on dashboard. |
| 52 | + */ |
| 53 | + public function test_is_post_management_page_returns_false_on_dashboard() { |
| 54 | + global $pagenow; |
| 55 | + $pagenow = 'index.php'; |
| 56 | + |
| 57 | + $module = $this->get_test_module(); |
| 58 | + $this->assertFalse( |
| 59 | + $module->is_post_management_page(), |
| 60 | + 'is_post_management_page() should return false on dashboard' |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Test that is_post_management_page returns false on options page. |
| 66 | + */ |
| 67 | + public function test_is_post_management_page_returns_false_on_options() { |
| 68 | + global $pagenow; |
| 69 | + $pagenow = 'options-general.php'; |
| 70 | + |
| 71 | + $module = $this->get_test_module(); |
| 72 | + $this->assertFalse( |
| 73 | + $module->is_post_management_page(), |
| 74 | + 'is_post_management_page() should return false on options page' |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Test that is_post_management_page returns false on plugins page. |
| 80 | + */ |
| 81 | + public function test_is_post_management_page_returns_false_on_plugins() { |
| 82 | + global $pagenow; |
| 83 | + $pagenow = 'plugins.php'; |
| 84 | + |
| 85 | + $module = $this->get_test_module(); |
| 86 | + $this->assertFalse( |
| 87 | + $module->is_post_management_page(), |
| 88 | + 'is_post_management_page() should return false on plugins page' |
| 89 | + ); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Test that is_post_management_page returns false on users page. |
| 94 | + */ |
| 95 | + public function test_is_post_management_page_returns_false_on_users() { |
| 96 | + global $pagenow; |
| 97 | + $pagenow = 'users.php'; |
| 98 | + |
| 99 | + $module = $this->get_test_module(); |
| 100 | + $this->assertFalse( |
| 101 | + $module->is_post_management_page(), |
| 102 | + 'is_post_management_page() should return false on users page' |
| 103 | + ); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Test that is_post_management_page returns true on post edit page. |
| 108 | + */ |
| 109 | + public function test_is_post_management_page_returns_true_on_post_edit() { |
| 110 | + global $pagenow, $typenow; |
| 111 | + $pagenow = 'post.php'; |
| 112 | + $typenow = 'post'; |
| 113 | + |
| 114 | + $module = $this->get_test_module(); |
| 115 | + $this->assertTrue( |
| 116 | + $module->is_post_management_page(), |
| 117 | + 'is_post_management_page() should return true on post.php' |
| 118 | + ); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Test that is_post_management_page returns true on new post page. |
| 123 | + */ |
| 124 | + public function test_is_post_management_page_returns_true_on_post_new() { |
| 125 | + global $pagenow, $typenow; |
| 126 | + $pagenow = 'post-new.php'; |
| 127 | + $typenow = 'post'; |
| 128 | + |
| 129 | + $module = $this->get_test_module(); |
| 130 | + $this->assertTrue( |
| 131 | + $module->is_post_management_page(), |
| 132 | + 'is_post_management_page() should return true on post-new.php' |
| 133 | + ); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Test that is_post_management_page returns true on posts list page. |
| 138 | + */ |
| 139 | + public function test_is_post_management_page_returns_true_on_edit() { |
| 140 | + global $pagenow, $typenow; |
| 141 | + $pagenow = 'edit.php'; |
| 142 | + $typenow = 'post'; |
| 143 | + |
| 144 | + $module = $this->get_test_module(); |
| 145 | + $this->assertTrue( |
| 146 | + $module->is_post_management_page(), |
| 147 | + 'is_post_management_page() should return true on edit.php' |
| 148 | + ); |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Test that is_post_management_page returns true on page edit page. |
| 153 | + */ |
| 154 | + public function test_is_post_management_page_returns_true_on_page_edit() { |
| 155 | + global $pagenow, $typenow; |
| 156 | + $pagenow = 'post.php'; |
| 157 | + $typenow = 'page'; |
| 158 | + |
| 159 | + $module = $this->get_test_module(); |
| 160 | + $this->assertTrue( |
| 161 | + $module->is_post_management_page(), |
| 162 | + 'is_post_management_page() should return true on post.php for pages' |
| 163 | + ); |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Test that is_post_management_page respects module-specific post types. |
| 168 | + */ |
| 169 | + public function test_is_post_management_page_respects_module_post_types() { |
| 170 | + global $pagenow, $typenow, $edit_flow; |
| 171 | + |
| 172 | + // Register a custom post type that's NOT supported by the module. |
| 173 | + register_post_type( 'unsupported_cpt', array( 'public' => true ) ); |
| 174 | + |
| 175 | + $pagenow = 'post.php'; |
| 176 | + $typenow = 'unsupported_cpt'; |
| 177 | + |
| 178 | + $module = $this->get_test_module(); |
| 179 | + |
| 180 | + // When passing module name, it should check if post type is supported. |
| 181 | + $result = $module->is_post_management_page( $module->module->name ); |
| 182 | + |
| 183 | + // Clean up. |
| 184 | + unregister_post_type( 'unsupported_cpt' ); |
| 185 | + |
| 186 | + $this->assertFalse( |
| 187 | + $result, |
| 188 | + 'is_post_management_page() should return false for unsupported post types when module name is passed' |
| 189 | + ); |
| 190 | + } |
| 191 | + |
| 192 | + /** |
| 193 | + * Test that is_post_management_page without module name still works on supported post types. |
| 194 | + */ |
| 195 | + public function test_is_post_management_page_without_module_name() { |
| 196 | + global $pagenow, $typenow; |
| 197 | + $pagenow = 'post.php'; |
| 198 | + $typenow = 'post'; |
| 199 | + |
| 200 | + $module = $this->get_test_module(); |
| 201 | + |
| 202 | + // Without module name, it should just check the page type. |
| 203 | + $this->assertTrue( |
| 204 | + $module->is_post_management_page(), |
| 205 | + 'is_post_management_page() without module name should return true on post edit pages' |
| 206 | + ); |
| 207 | + } |
| 208 | +} |
0 commit comments