|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * PHPUnit tests for the Activation_Redirect class. |
| 4 | + * |
| 5 | + * @package Accessibility_Checker\Tests |
| 6 | + */ |
| 7 | + |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | +use EDAC\Admin\Activation_Redirect; |
| 10 | + |
| 11 | +/** |
| 12 | + * Class Activation_Redirect_Test |
| 13 | + * |
| 14 | + * @covers \EDAC\Admin\Activation_Redirect |
| 15 | + */ |
| 16 | +class ActivationRedirectTest extends WP_UnitTestCase { |
| 17 | + |
| 18 | + /** |
| 19 | + * Instance of the Activation_Redirect class. |
| 20 | + * |
| 21 | + * @var Activation_Redirect $activation_redirect. |
| 22 | + */ |
| 23 | + private $activation_redirect; |
| 24 | + |
| 25 | + /** |
| 26 | + * Set up the test fixture. |
| 27 | + */ |
| 28 | + protected function setUp(): void { |
| 29 | + parent::setUp(); |
| 30 | + $this->activation_redirect = new Activation_Redirect(); |
| 31 | + |
| 32 | + // Clear any existing transients. |
| 33 | + delete_transient( 'edac_activation_redirect' ); |
| 34 | + |
| 35 | + // Clear any $_GET parameters. |
| 36 | + unset( $_GET['activate-multi'] ); |
| 37 | + |
| 38 | + // Intercept redirects to prevent headers being sent during tests. |
| 39 | + add_filter( |
| 40 | + 'wp_redirect', |
| 41 | + static function ( $location ) { |
| 42 | + // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- Test exception message. |
| 43 | + throw new Exception( 'Redirect to: ' . $location ); |
| 44 | + } |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Clean up after each test. |
| 50 | + */ |
| 51 | + protected function tearDown(): void { |
| 52 | + // Clean up transient and global state. |
| 53 | + delete_transient( 'edac_activation_redirect' ); |
| 54 | + unset( $_GET['activate-multi'] ); |
| 55 | + remove_all_filters( 'wp_doing_ajax' ); |
| 56 | + remove_all_filters( 'is_network_admin' ); |
| 57 | + remove_all_filters( 'wp_redirect' ); |
| 58 | + parent::tearDown(); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Test instantiation of Activation_Redirect class. |
| 63 | + */ |
| 64 | + public function test_can_instantiate_class() { |
| 65 | + $this->assertInstanceOf( Activation_Redirect::class, $this->activation_redirect ); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Test that init() adds the admin_init action. |
| 70 | + */ |
| 71 | + public function test_init_adds_action() { |
| 72 | + $this->activation_redirect->init(); |
| 73 | + $this->assertNotFalse( has_action( 'admin_init', [ $this->activation_redirect, 'maybe_redirect_to_welcome' ] ) ); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Test that redirect does not happen when transient is not set. |
| 78 | + */ |
| 79 | + public function test_no_redirect_without_transient() { |
| 80 | + // Ensure transient doesn't exist. |
| 81 | + delete_transient( 'edac_activation_redirect' ); |
| 82 | + |
| 83 | + // Set up admin user. |
| 84 | + $admin_user = $this->factory->user->create( [ 'role' => 'administrator' ] ); |
| 85 | + wp_set_current_user( $admin_user ); |
| 86 | + |
| 87 | + // Call the method - it should return early. |
| 88 | + $this->activation_redirect->maybe_redirect_to_welcome(); |
| 89 | + |
| 90 | + // If we get here without a redirect and no transient, the test passes. |
| 91 | + $this->assertFalse( get_transient( 'edac_activation_redirect' ) ); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Test that transient is deleted only when redirect happens. |
| 96 | + * |
| 97 | + * Note: This test expects a redirect exception to be thrown because |
| 98 | + * WP_TESTS_DOMAIN is defined in the test environment, preventing the |
| 99 | + * actual redirect. In a real scenario without WP_TESTS_DOMAIN, the |
| 100 | + * redirect would occur and the transient would be deleted. |
| 101 | + */ |
| 102 | + public function test_transient_is_deleted_only_on_redirect() { |
| 103 | + // Set the transient. |
| 104 | + set_transient( 'edac_activation_redirect', true, 60 ); |
| 105 | + |
| 106 | + // Set up admin user. |
| 107 | + $admin_user = $this->factory->user->create( [ 'role' => 'administrator' ] ); |
| 108 | + wp_set_current_user( $admin_user ); |
| 109 | + |
| 110 | + // Call the method - it will return early due to WP_TESTS_DOMAIN. |
| 111 | + $this->activation_redirect->maybe_redirect_to_welcome(); |
| 112 | + |
| 113 | + // Transient should still exist because redirect didn't happen. |
| 114 | + $this->assertTrue( (bool) get_transient( 'edac_activation_redirect' ) ); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Test that redirect does not happen during AJAX requests. |
| 119 | + */ |
| 120 | + public function test_no_redirect_during_ajax() { |
| 121 | + // Set the transient. |
| 122 | + set_transient( 'edac_activation_redirect', true, 60 ); |
| 123 | + |
| 124 | + // Set up admin user. |
| 125 | + $admin_user = $this->factory->user->create( [ 'role' => 'administrator' ] ); |
| 126 | + wp_set_current_user( $admin_user ); |
| 127 | + |
| 128 | + // Simulate AJAX request. |
| 129 | + add_filter( |
| 130 | + 'wp_doing_ajax', |
| 131 | + function () { |
| 132 | + return true; |
| 133 | + } |
| 134 | + ); |
| 135 | + |
| 136 | + // Call the method - it should return early. |
| 137 | + $this->activation_redirect->maybe_redirect_to_welcome(); |
| 138 | + |
| 139 | + // Transient should still exist because no redirect happened. |
| 140 | + $this->assertTrue( (bool) get_transient( 'edac_activation_redirect' ) ); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Test that redirect does not happen during bulk plugin activation. |
| 145 | + */ |
| 146 | + public function test_no_redirect_during_bulk_activation() { |
| 147 | + // Set the transient. |
| 148 | + set_transient( 'edac_activation_redirect', true, 60 ); |
| 149 | + |
| 150 | + // Set up admin user. |
| 151 | + $admin_user = $this->factory->user->create( [ 'role' => 'administrator' ] ); |
| 152 | + wp_set_current_user( $admin_user ); |
| 153 | + |
| 154 | + // Simulate bulk activation. |
| 155 | + $_GET['activate-multi'] = 'true'; |
| 156 | + |
| 157 | + // Call the method - it should return early. |
| 158 | + $this->activation_redirect->maybe_redirect_to_welcome(); |
| 159 | + |
| 160 | + // Transient should still exist because no redirect happened. |
| 161 | + $this->assertTrue( (bool) get_transient( 'edac_activation_redirect' ) ); |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Test that redirect does not happen for users without proper capabilities. |
| 166 | + */ |
| 167 | + public function test_no_redirect_without_proper_capability() { |
| 168 | + // Set the transient. |
| 169 | + set_transient( 'edac_activation_redirect', true, 60 ); |
| 170 | + |
| 171 | + // Set up subscriber user (no edit_posts capability). |
| 172 | + $subscriber = $this->factory->user->create( [ 'role' => 'subscriber' ] ); |
| 173 | + wp_set_current_user( $subscriber ); |
| 174 | + |
| 175 | + // Call the method - it should return early. |
| 176 | + $this->activation_redirect->maybe_redirect_to_welcome(); |
| 177 | + |
| 178 | + // Transient should still exist because no redirect happened. |
| 179 | + $this->assertTrue( (bool) get_transient( 'edac_activation_redirect' ) ); |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * Test that redirect does not happen in network admin (multisite). |
| 184 | + * |
| 185 | + * Note: This test is skipped because is_network_admin() checks the WP_NETWORK_ADMIN |
| 186 | + * constant which cannot be easily mocked in unit tests. The network admin check is |
| 187 | + * covered by integration testing in actual multisite environments. |
| 188 | + */ |
| 189 | + public function test_no_redirect_in_network_admin() { |
| 190 | + $this->markTestSkipped( |
| 191 | + 'Cannot reliably test network admin context in unit tests. ' . |
| 192 | + 'The is_network_admin() function checks WP_NETWORK_ADMIN constant ' . |
| 193 | + 'which cannot be mocked. This is covered by integration tests.' |
| 194 | + ); |
| 195 | + } |
| 196 | + |
| 197 | + /** |
| 198 | + * Test activation function sets the transient. |
| 199 | + */ |
| 200 | + public function test_activation_sets_transient() { |
| 201 | + // Ensure transient doesn't exist initially. |
| 202 | + delete_transient( 'edac_activation_redirect' ); |
| 203 | + |
| 204 | + // Mock the Accessibility_Statement class since it's required by activation. |
| 205 | + if ( ! class_exists( 'EDAC\Admin\Accessibility_Statement' ) ) { |
| 206 | + require_once EDAC_PLUGIN_DIR . 'admin/class-accessibility-statement.php'; |
| 207 | + } |
| 208 | + |
| 209 | + // Run the activation function. |
| 210 | + edac_activation(); |
| 211 | + |
| 212 | + // Check that the transient was set. |
| 213 | + $this->assertTrue( (bool) get_transient( 'edac_activation_redirect' ) ); |
| 214 | + |
| 215 | + // Clean up. |
| 216 | + delete_transient( 'edac_activation_redirect' ); |
| 217 | + } |
| 218 | + |
| 219 | + /** |
| 220 | + * Test that the correct redirect URL is returned. |
| 221 | + */ |
| 222 | + public function test_get_welcome_page_url_returns_correct_url() { |
| 223 | + $url = $this->activation_redirect->get_welcome_page_url(); |
| 224 | + |
| 225 | + // Verify the URL is properly formed and points to the welcome page. |
| 226 | + $this->assertStringContainsString( 'admin.php?page=accessibility_checker', $url ); |
| 227 | + $this->assertStringContainsString( 'wp-admin', $url ); |
| 228 | + |
| 229 | + // Verify it's a valid admin URL. |
| 230 | + $expected_url = admin_url( 'admin.php?page=accessibility_checker' ); |
| 231 | + $this->assertEquals( $expected_url, $url ); |
| 232 | + } |
| 233 | + |
| 234 | + /** |
| 235 | + * Test that redirect does not happen in test environment. |
| 236 | + */ |
| 237 | + public function test_no_redirect_in_test_environment() { |
| 238 | + // Set the transient. |
| 239 | + set_transient( 'edac_activation_redirect', true, 60 ); |
| 240 | + |
| 241 | + // Set up admin user. |
| 242 | + $admin_user = $this->factory->user->create( [ 'role' => 'administrator' ] ); |
| 243 | + wp_set_current_user( $admin_user ); |
| 244 | + |
| 245 | + // WP_TESTS_DOMAIN should be defined in test environment. |
| 246 | + $this->assertTrue( defined( 'WP_TESTS_DOMAIN' ), 'WP_TESTS_DOMAIN should be defined in test environment' ); |
| 247 | + |
| 248 | + // Call the method - it should return early without redirecting. |
| 249 | + $this->activation_redirect->maybe_redirect_to_welcome(); |
| 250 | + |
| 251 | + // Transient should still exist because no redirect happened. |
| 252 | + $this->assertTrue( (bool) get_transient( 'edac_activation_redirect' ) ); |
| 253 | + } |
| 254 | +} |
0 commit comments