|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Tests for wp_get_active_and_valid_plugins(). |
| 5 | + * |
| 6 | + * @group load |
| 7 | + * @group plugins |
| 8 | + * |
| 9 | + * @covers ::wp_get_active_and_valid_plugins |
| 10 | + */ |
| 11 | +class Tests_Load_WpGetActiveAndValidPlugins extends WP_UnitTestCase { |
| 12 | + |
| 13 | + const TEST_SESSION_ID = 'test_active_valid_plugins_session'; |
| 14 | + |
| 15 | + /** |
| 16 | + * Created plugin files to clean up. |
| 17 | + * |
| 18 | + * @var string[] |
| 19 | + */ |
| 20 | + private $created_files = array(); |
| 21 | + |
| 22 | + /** |
| 23 | + * Created directories to clean up. |
| 24 | + * |
| 25 | + * @var string[] |
| 26 | + */ |
| 27 | + private $created_dirs = array(); |
| 28 | + |
| 29 | + /** |
| 30 | + * Original active_plugins option. |
| 31 | + * |
| 32 | + * @var mixed |
| 33 | + */ |
| 34 | + private $original_active_plugins; |
| 35 | + |
| 36 | + /** |
| 37 | + * Original installing status. |
| 38 | + * |
| 39 | + * @var bool |
| 40 | + */ |
| 41 | + private $original_installing; |
| 42 | + |
| 43 | + /** |
| 44 | + * Original recovery mode is_active property. |
| 45 | + * |
| 46 | + * @var bool |
| 47 | + */ |
| 48 | + private $original_is_active; |
| 49 | + |
| 50 | + /** |
| 51 | + * Original recovery mode session_id property. |
| 52 | + * |
| 53 | + * @var string |
| 54 | + */ |
| 55 | + private $original_session_id; |
| 56 | + |
| 57 | + /** |
| 58 | + * Original $_paused_plugins global. |
| 59 | + * |
| 60 | + * @var array|null |
| 61 | + */ |
| 62 | + private $original_paused_plugins; |
| 63 | + |
| 64 | + public function set_up() { |
| 65 | + parent::set_up(); |
| 66 | + |
| 67 | + $this->original_active_plugins = get_option( 'active_plugins' ); |
| 68 | + $this->original_installing = wp_installing(); |
| 69 | + $this->original_is_active = $this->get_recovery_mode_property( 'is_active' ); |
| 70 | + $this->original_session_id = $this->get_recovery_mode_property( 'session_id' ); |
| 71 | + $this->original_paused_plugins = $GLOBALS['_paused_plugins'] ?? null; |
| 72 | + |
| 73 | + unset( $GLOBALS['_paused_plugins'] ); |
| 74 | + wp_installing( false ); |
| 75 | + update_option( 'active_plugins', array() ); |
| 76 | + } |
| 77 | + |
| 78 | + public function tear_down() { |
| 79 | + foreach ( $this->created_files as $file ) { |
| 80 | + if ( file_exists( $file ) ) { |
| 81 | + unlink( $file ); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + foreach ( array_reverse( $this->created_dirs ) as $dir ) { |
| 86 | + if ( is_dir( $dir ) ) { |
| 87 | + rmdir( $dir ); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + $this->created_files = array(); |
| 92 | + $this->created_dirs = array(); |
| 93 | + |
| 94 | + update_option( 'active_plugins', $this->original_active_plugins ); |
| 95 | + wp_installing( $this->original_installing ); |
| 96 | + $this->set_recovery_mode_state( $this->original_is_active, $this->original_session_id ); |
| 97 | + delete_option( self::TEST_SESSION_ID . '_paused_extensions' ); |
| 98 | + |
| 99 | + if ( null !== $this->original_paused_plugins ) { |
| 100 | + $GLOBALS['_paused_plugins'] = $this->original_paused_plugins; |
| 101 | + } else { |
| 102 | + unset( $GLOBALS['_paused_plugins'] ); |
| 103 | + } |
| 104 | + |
| 105 | + parent::tear_down(); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Creates a plugin fixture under WP_PLUGIN_DIR. |
| 110 | + * |
| 111 | + * @param string $relative_path Path relative to WP_PLUGIN_DIR. |
| 112 | + * @return string Relative plugin path. |
| 113 | + */ |
| 114 | + private function create_plugin_file( $relative_path ) { |
| 115 | + $full_path = WP_PLUGIN_DIR . '/' . $relative_path; |
| 116 | + $dir = dirname( $full_path ); |
| 117 | + |
| 118 | + if ( ! is_dir( $dir ) && WP_PLUGIN_DIR !== $dir ) { |
| 119 | + $this->assertTrue( wp_mkdir_p( $dir ), "Failed to create directory: {$dir}" ); |
| 120 | + $this->created_dirs[] = $dir; |
| 121 | + } |
| 122 | + |
| 123 | + $this->assertNotFalse( |
| 124 | + file_put_contents( $full_path, "<?php\n/**\n * Plugin Name: Fixture\n */\n" ), |
| 125 | + "Failed to write plugin fixture: {$full_path}" |
| 126 | + ); |
| 127 | + $this->created_files[] = $full_path; |
| 128 | + |
| 129 | + return $relative_path; |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Gets a WP_Recovery_Mode property value. |
| 134 | + * |
| 135 | + * @param string $property Property name. |
| 136 | + * @return mixed Property value. |
| 137 | + */ |
| 138 | + private function get_recovery_mode_property( $property ) { |
| 139 | + $reflection = new ReflectionProperty( wp_recovery_mode(), $property ); |
| 140 | + if ( PHP_VERSION_ID < 80100 ) { |
| 141 | + $reflection->setAccessible( true ); |
| 142 | + } |
| 143 | + |
| 144 | + return $reflection->getValue( wp_recovery_mode() ); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Sets WP_Recovery_Mode protected state. |
| 149 | + * |
| 150 | + * @param bool $is_active Whether recovery mode is active. |
| 151 | + * @param string $session_id Recovery mode session ID. |
| 152 | + */ |
| 153 | + private function set_recovery_mode_state( $is_active, $session_id ) { |
| 154 | + $recovery_mode = wp_recovery_mode(); |
| 155 | + |
| 156 | + $is_active_property = new ReflectionProperty( $recovery_mode, 'is_active' ); |
| 157 | + if ( PHP_VERSION_ID < 80100 ) { |
| 158 | + $is_active_property->setAccessible( true ); |
| 159 | + } |
| 160 | + $is_active_property->setValue( $recovery_mode, $is_active ); |
| 161 | + |
| 162 | + $session_id_property = new ReflectionProperty( $recovery_mode, 'session_id' ); |
| 163 | + if ( PHP_VERSION_ID < 80100 ) { |
| 164 | + $session_id_property->setAccessible( true ); |
| 165 | + } |
| 166 | + $session_id_property->setValue( $recovery_mode, $session_id ); |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * @ticket 65819 |
| 171 | + */ |
| 172 | + public function test_returns_empty_array_when_no_active_plugins() { |
| 173 | + $this->assertSame( array(), wp_get_active_and_valid_plugins() ); |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * @ticket 65819 |
| 178 | + */ |
| 179 | + public function test_returns_empty_array_while_installing() { |
| 180 | + $plugin = $this->create_plugin_file( '65819-active-valid.php' ); |
| 181 | + update_option( 'active_plugins', array( $plugin ) ); |
| 182 | + wp_installing( true ); |
| 183 | + |
| 184 | + $this->assertSame( array(), wp_get_active_and_valid_plugins() ); |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * @ticket 65819 |
| 189 | + */ |
| 190 | + public function test_returns_absolute_paths_for_valid_active_plugins() { |
| 191 | + $single = $this->create_plugin_file( '65819-single-plugin.php' ); |
| 192 | + $nested = $this->create_plugin_file( '65819-nested-plugin/plugin.php' ); |
| 193 | + |
| 194 | + update_option( 'active_plugins', array( $single, $nested ) ); |
| 195 | + |
| 196 | + $this->assertSame( |
| 197 | + array( |
| 198 | + WP_PLUGIN_DIR . '/' . $single, |
| 199 | + WP_PLUGIN_DIR . '/' . $nested, |
| 200 | + ), |
| 201 | + wp_get_active_and_valid_plugins() |
| 202 | + ); |
| 203 | + } |
| 204 | + |
| 205 | + /** |
| 206 | + * @ticket 65819 |
| 207 | + */ |
| 208 | + public function test_skips_missing_and_invalid_plugin_entries() { |
| 209 | + $valid = $this->create_plugin_file( '65819-valid-plugin/plugin.php' ); |
| 210 | + $non_php = $this->create_plugin_file( '65819-not-a-php-file.txt' ); |
| 211 | + |
| 212 | + update_option( |
| 213 | + 'active_plugins', |
| 214 | + array( |
| 215 | + $valid, |
| 216 | + '65819-missing-plugin.php', |
| 217 | + $non_php, |
| 218 | + '65819-valid-plugin/../' . $valid, |
| 219 | + ) |
| 220 | + ); |
| 221 | + |
| 222 | + $this->assertSame( |
| 223 | + array( WP_PLUGIN_DIR . '/' . $valid ), |
| 224 | + wp_get_active_and_valid_plugins() |
| 225 | + ); |
| 226 | + } |
| 227 | + |
| 228 | + /** |
| 229 | + * @ticket 65819 |
| 230 | + */ |
| 231 | + public function test_skips_paused_plugins_in_recovery_mode() { |
| 232 | + $kept = $this->create_plugin_file( '65819-kept-plugin.php' ); |
| 233 | + $paused = $this->create_plugin_file( '65819-paused-plugin/plugin.php' ); |
| 234 | + $error = array( |
| 235 | + 'type' => E_ERROR, |
| 236 | + 'file' => WP_PLUGIN_DIR . '/65819-paused-plugin/plugin.php', |
| 237 | + 'line' => 1, |
| 238 | + 'message' => 'Fatal error', |
| 239 | + ); |
| 240 | + |
| 241 | + update_option( 'active_plugins', array( $kept, $paused ) ); |
| 242 | + |
| 243 | + $this->set_recovery_mode_state( true, self::TEST_SESSION_ID ); |
| 244 | + $this->assertTrue( wp_paused_plugins()->set( '65819-paused-plugin', $error ) ); |
| 245 | + |
| 246 | + $this->assertSame( |
| 247 | + array( WP_PLUGIN_DIR . '/' . $kept ), |
| 248 | + wp_get_active_and_valid_plugins() |
| 249 | + ); |
| 250 | + } |
| 251 | +} |
0 commit comments