[wp-trac] [WordPress Trac] #60018: PHPUnit test for force_ssl_admin

WordPress Trac noreply at wordpress.org
Tue Feb 13 11:37:42 UTC 2024


#60018: PHPUnit test for force_ssl_admin
--------------------------------------+------------------------------
 Reporter:  pbearne                   |       Owner:  (none)
     Type:  defect (bug)              |      Status:  new
 Priority:  normal                    |   Milestone:  Awaiting Review
Component:  Build/Test Tools          |     Version:
 Severity:  normal                    |  Resolution:
 Keywords:  has-patch has-unit-tests  |     Focuses:
--------------------------------------+------------------------------

Comment (by flihovituhe1982):

 Sure, here's an example PHPUnit test for the `force_ssl_admin`
 functionality in WordPress:

 ```php
 <?php
 class ForceSslAdminTest extends WP_UnitTestCase {

     // Test that force_ssl_admin is enabled
     function test_force_ssl_admin_enabled() {
         $this->assertTrue(force_ssl_admin());
     }

     // Test that force_ssl_admin is disabled when turned off
     function test_force_ssl_admin_disabled() {
         update_option('force_ssl_admin', 0);
         $this->assertFalse(force_ssl_admin());
     }

     // Test that force_ssl_admin redirects admin urls to https when
 enabled

     function test_force_ssl_admin_redirect() {
         update_option('force_ssl_admin', 1);
         $admin_url = admin_url();
         $this->assertStringStartsWith('https://', $admin_url);
     }
 }
 ```

 This PHPUnit test class includes three test methods:

 1. `test_force_ssl_admin_enabled()`: Checks if `force_ssl_admin()` returns
 `true` when the option is enabled.
 2. `test_force_ssl_admin_disabled()`: Checks if `force_ssl_admin()`
 returns `false` when the option is disabled.
 3. `test_force_ssl_admin_redirect()`: Checks if the admin URL starts with
 'https://' after enabling `force_ssl_admin`.

 Make sure you have the necessary setup for running PHPUnit tests in your
 WordPress environment. This includes having PHPUnit installed and
 configuring `wp-tests-config.php` properly.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/60018#comment:3>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list