| | 1 | <?php |
| | 2 | /** |
| | 3 | * Tests for wp_get_active_and_valid_plugins(). |
| | 4 | * |
| | 5 | * @group load |
| | 6 | * @covers ::wp_get_active_and_valid_plugins |
| | 7 | */ |
| | 8 | class Tests_Load_WpGetActiveAndValidPlugins extends WP_UnitTestCase { |
| | 9 | |
| | 10 | /** |
| | 11 | * Tests that Hello Dolly plugin migration logic is present in the function. |
| | 12 | * |
| | 13 | * This test verifies that the migration code exists by checking that |
| | 14 | * hello-dolly/hello.php is handled correctly when it exists. |
| | 15 | * |
| | 16 | * @ticket 53323 |
| | 17 | */ |
| | 18 | public function test_hello_dolly_migration_logic_exists() { |
| | 19 | // Ensure we have the hello-dolly plugin available for testing. |
| | 20 | $hello_dolly_file = WP_PLUGIN_DIR . '/hello-dolly/hello.php'; |
| | 21 | |
| | 22 | if ( ! file_exists( $hello_dolly_file ) ) { |
| | 23 | $this->markTestSkipped( 'Hello Dolly plugin file not found for testing.' ); |
| | 24 | } |
| | 25 | |
| | 26 | // Store original active plugins. |
| | 27 | $original_active_plugins = get_option( 'active_plugins', array() ); |
| | 28 | |
| | 29 | // Set hello-dolly/hello.php as active (the new format). |
| | 30 | $test_active_plugins = array( 'hello-dolly/hello.php' ); |
| | 31 | update_option( 'active_plugins', $test_active_plugins ); |
| | 32 | |
| | 33 | // Call the function - it should handle the hello-dolly plugin correctly. |
| | 34 | $valid_plugins = wp_get_active_and_valid_plugins(); |
| | 35 | |
| | 36 | // Verify hello-dolly plugin is included when it exists. |
| | 37 | $this->assertContains( $hello_dolly_file, $valid_plugins, 'Hello Dolly plugin should be loaded when active and file exists.' ); |
| | 38 | |
| | 39 | // Restore original state. |
| | 40 | update_option( 'active_plugins', $original_active_plugins ); |
| | 41 | } |
| | 42 | |
| | 43 | /** |
| | 44 | * Tests that function handles empty active plugins gracefully. |
| | 45 | * |
| | 46 | * @ticket 53323 |
| | 47 | */ |
| | 48 | public function test_handles_empty_active_plugins() { |
| | 49 | // Store original active plugins. |
| | 50 | $original_active_plugins = get_option( 'active_plugins', array() ); |
| | 51 | |
| | 52 | // Set empty active plugins. |
| | 53 | update_option( 'active_plugins', array() ); |
| | 54 | |
| | 55 | // Call the function. |
| | 56 | $valid_plugins = wp_get_active_and_valid_plugins(); |
| | 57 | |
| | 58 | // Should return array (might contain hacks file, but no plugins). |
| | 59 | $this->assertIsArray( $valid_plugins ); |
| | 60 | |
| | 61 | // Restore original state. |
| | 62 | update_option( 'active_plugins', $original_active_plugins ); |
| | 63 | } |
| | 64 | |
| | 65 | /** |
| | 66 | * Tests Hello Dolly migration from hello.php to hello-dolly/hello.php. |
| | 67 | * |
| | 68 | * This test simulates the scenario where: |
| | 69 | * 1. hello.php is active in the database |
| | 70 | * 2. The old hello.php file doesn't exist (removed during update) |
| | 71 | * 3. The new hello-dolly/hello.php file exists |
| | 72 | * 4. The migration should automatically update active_plugins option |
| | 73 | * |
| | 74 | * @ticket 53323 |
| | 75 | */ |
| | 76 | public function test_hello_dolly_migration_from_file_to_directory() { |
| | 77 | // Store original active plugins. |
| | 78 | $original_active_plugins = get_option( 'active_plugins', array() ); |
| | 79 | |
| | 80 | // Set hello.php as active (simulating pre-migration state). |
| | 81 | $test_active_plugins = array( 'hello.php' ); |
| | 82 | update_option( 'active_plugins', $test_active_plugins ); |
| | 83 | |
| | 84 | // Use filter to simulate migration conditions: old file gone, new file exists. |
| | 85 | add_filter( 'wp_should_migrate_hello_dolly', array( $this, 'simulate_hello_dolly_migration_needed' ) ); |
| | 86 | |
| | 87 | try { |
| | 88 | // Call the function that should trigger migration. |
| | 89 | wp_get_active_and_valid_plugins(); |
| | 90 | |
| | 91 | // Check that migration occurred. |
| | 92 | $updated_active_plugins = get_option( 'active_plugins', array() ); |
| | 93 | $this->assertNotContains( 'hello.php', $updated_active_plugins, 'Old hello.php should be removed from active plugins after migration' ); |
| | 94 | $this->assertContains( 'hello-dolly/hello.php', $updated_active_plugins, 'New hello-dolly/hello.php should be added to active plugins after migration' ); |
| | 95 | |
| | 96 | } finally { |
| | 97 | // Always restore original state. |
| | 98 | remove_filter( 'wp_should_migrate_hello_dolly', array( $this, 'simulate_hello_dolly_migration_needed' ) ); |
| | 99 | update_option( 'active_plugins', $original_active_plugins ); |
| | 100 | } |
| | 101 | } |
| | 102 | |
| | 103 | /** |
| | 104 | * Tests that migration does NOT occur when old file still exists. |
| | 105 | * |
| | 106 | * This ensures the migration only happens in the correct scenario. |
| | 107 | * |
| | 108 | * @ticket 53323 |
| | 109 | */ |
| | 110 | public function test_hello_dolly_no_migration_when_old_file_exists() { |
| | 111 | // Store original active plugins. |
| | 112 | $original_active_plugins = get_option( 'active_plugins', array() ); |
| | 113 | |
| | 114 | // Set hello.php as active. |
| | 115 | $test_active_plugins = array( 'hello.php' ); |
| | 116 | update_option( 'active_plugins', $test_active_plugins ); |
| | 117 | |
| | 118 | // Use filter to simulate both files existing (no migration needed). |
| | 119 | add_filter( 'wp_should_migrate_hello_dolly', array( $this, 'simulate_hello_dolly_migration_not_needed' ) ); |
| | 120 | |
| | 121 | try { |
| | 122 | // Call the function - migration should NOT occur. |
| | 123 | wp_get_active_and_valid_plugins(); |
| | 124 | |
| | 125 | // Check that NO migration occurred. |
| | 126 | $updated_active_plugins = get_option( 'active_plugins', array() ); |
| | 127 | $this->assertContains( 'hello.php', $updated_active_plugins, 'hello.php should remain when old file still exists' ); |
| | 128 | $this->assertNotContains( 'hello-dolly/hello.php', $updated_active_plugins, 'Migration should not occur when old file exists' ); |
| | 129 | |
| | 130 | } finally { |
| | 131 | // Always restore original state. |
| | 132 | remove_filter( 'wp_should_migrate_hello_dolly', array( $this, 'simulate_hello_dolly_migration_not_needed' ) ); |
| | 133 | update_option( 'active_plugins', $original_active_plugins ); |
| | 134 | } |
| | 135 | } |
| | 136 | |
| | 137 | /** |
| | 138 | * Filter callback to simulate Hello Dolly migration being needed. |
| | 139 | * |
| | 140 | * @param bool $should_migrate Default migration decision. |
| | 141 | * @return bool Always true to simulate migration conditions are met. |
| | 142 | */ |
| | 143 | public function simulate_hello_dolly_migration_needed( $should_migrate ) { |
| | 144 | return true; |
| | 145 | } |
| | 146 | |
| | 147 | /** |
| | 148 | * Filter callback to simulate Hello Dolly migration NOT being needed. |
| | 149 | * |
| | 150 | * @param bool $should_migrate Default migration decision. |
| | 151 | * @return bool Always false to simulate no migration needed. |
| | 152 | */ |
| | 153 | public function simulate_hello_dolly_migration_not_needed( $should_migrate ) { |
| | 154 | return false; |
| | 155 | } |
| | 156 | } |
| | 157 | No newline at end of file |