https-migration.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /**
  3. * @group https-migration
  4. */
  5. class Tests_HTTPS_Migration extends WP_UnitTestCase {
  6. /**
  7. * @ticket 51437
  8. */
  9. public function test_wp_should_replace_insecure_home_url() {
  10. // Should return false because site is not using HTTPS.
  11. $this->force_wp_is_using_https( false );
  12. $this->assertFalse( wp_should_replace_insecure_home_url() );
  13. // Should still return false because HTTPS migration flag is not set.
  14. $this->force_wp_is_using_https( true );
  15. $this->assertFalse( wp_should_replace_insecure_home_url() );
  16. // Should return false because HTTPS migration flag is marked as not required.
  17. update_option( 'https_migration_required', '0' );
  18. $this->assertFalse( wp_should_replace_insecure_home_url() );
  19. // Should return true because HTTPS migration flag is marked as required.
  20. update_option( 'https_migration_required', '1' );
  21. $this->assertTrue( wp_should_replace_insecure_home_url() );
  22. // Should be overridable via filter.
  23. add_filter( 'wp_should_replace_insecure_home_url', '__return_false' );
  24. $this->assertFalse( wp_should_replace_insecure_home_url() );
  25. }
  26. /**
  27. * @ticket 51437
  28. */
  29. public function test_wp_replace_insecure_home_url() {
  30. $http_url = home_url( '', 'http' );
  31. $https_url = home_url( '', 'https' );
  32. $http_block_data = array(
  33. 'id' => 3,
  34. 'url' => $http_url . '/wp-content/uploads/2021/01/image.jpg',
  35. );
  36. $https_block_data = array(
  37. 'id' => 3,
  38. 'url' => $https_url . '/wp-content/uploads/2021/01/image.jpg',
  39. );
  40. $content = '
  41. <!-- wp:paragraph -->
  42. <p><a href="%1$s">This is a link.</a></p>
  43. <!-- /wp:paragraph -->
  44. <!-- wp:custom-media %2$s -->
  45. <img src="%3$s" alt="">
  46. <!-- /wp:custom-media -->
  47. ';
  48. $http_content = sprintf( $content, $http_url, wp_json_encode( $http_block_data ), $http_block_data['url'] );
  49. $https_content = sprintf( $content, $https_url, wp_json_encode( $https_block_data ), $https_block_data['url'] );
  50. // Replaces URLs, including its encoded variant.
  51. add_filter( 'wp_should_replace_insecure_home_url', '__return_true' );
  52. $this->assertSame( $https_content, wp_replace_insecure_home_url( $http_content ) );
  53. // Does not replace anything if determined as unnecessary.
  54. add_filter( 'wp_should_replace_insecure_home_url', '__return_false' );
  55. $this->assertSame( $http_content, wp_replace_insecure_home_url( $http_content ) );
  56. }
  57. /**
  58. * @ticket 51437
  59. */
  60. public function test_wp_update_urls_to_https() {
  61. remove_all_filters( 'option_home' );
  62. remove_all_filters( 'option_siteurl' );
  63. remove_all_filters( 'home_url' );
  64. remove_all_filters( 'site_url' );
  65. $http_url = 'http://example.org';
  66. $https_url = 'https://example.org';
  67. // Set up options to use HTTP URLs.
  68. update_option( 'home', $http_url );
  69. update_option( 'siteurl', $http_url );
  70. // Update URLs to HTTPS (successfully).
  71. $this->assertTrue( wp_update_urls_to_https() );
  72. $this->assertSame( $https_url, get_option( 'home' ) );
  73. $this->assertSame( $https_url, get_option( 'siteurl' ) );
  74. // Switch options back to use HTTP URLs, but now add filter to
  75. // force option value which will make the update irrelevant.
  76. update_option( 'home', $http_url );
  77. update_option( 'siteurl', $http_url );
  78. $this->force_option( 'home', $http_url );
  79. // Update URLs to HTTPS. While the update technically succeeds, it does not take effect due to the enforced
  80. // option. Therefore the change is expected to be reverted.
  81. $this->assertFalse( wp_update_urls_to_https() );
  82. $this->assertSame( $http_url, get_option( 'home' ) );
  83. $this->assertSame( $http_url, get_option( 'siteurl' ) );
  84. }
  85. /**
  86. * @ticket 51437
  87. */
  88. public function test_wp_update_https_migration_required() {
  89. // Changing HTTP to HTTPS on a site with content should result in flag being set, requiring migration.
  90. update_option( 'fresh_site', '0' );
  91. wp_update_https_migration_required( 'http://example.org', 'https://example.org' );
  92. $this->assertTrue( get_option( 'https_migration_required' ) );
  93. // Changing another part than the scheme should delete/reset the flag because changing those parts (e.g. the
  94. // domain) can have further implications.
  95. wp_update_https_migration_required( 'http://example.org', 'https://another-example.org' );
  96. $this->assertFalse( get_option( 'https_migration_required' ) );
  97. // Changing HTTP to HTTPS on a site without content should result in flag being set, but not requiring migration.
  98. update_option( 'fresh_site', '1' );
  99. wp_update_https_migration_required( 'http://example.org', 'https://example.org' );
  100. $this->assertFalse( get_option( 'https_migration_required' ) );
  101. // Changing (back) from HTTPS to HTTP should delete/reset the flag.
  102. wp_update_https_migration_required( 'https://example.org', 'http://example.org' );
  103. $this->assertFalse( get_option( 'https_migration_required' ) );
  104. }
  105. /**
  106. * @ticket 51437
  107. */
  108. public function test_wp_should_replace_insecure_home_url_integration() {
  109. // Setup (a site on HTTP, with existing content).
  110. remove_all_filters( 'option_home' );
  111. remove_all_filters( 'option_siteurl' );
  112. remove_all_filters( 'home_url' );
  113. remove_all_filters( 'site_url' );
  114. $http_url = 'http://example.org';
  115. $https_url = 'https://example.org';
  116. update_option( 'home', $http_url );
  117. update_option( 'siteurl', $http_url );
  118. update_option( 'fresh_site', '0' );
  119. // Should return false when URLs are HTTP.
  120. $this->assertFalse( wp_should_replace_insecure_home_url() );
  121. // Should still return false because only one of the two URLs was updated to its HTTPS counterpart.
  122. update_option( 'home', $https_url );
  123. $this->assertFalse( wp_should_replace_insecure_home_url() );
  124. // Should return true because now both URLs are updated to their HTTPS counterpart.
  125. update_option( 'siteurl', $https_url );
  126. $this->assertTrue( wp_should_replace_insecure_home_url() );
  127. // Should return false because the domains of 'home' and 'siteurl' do not match, and we shouldn't make any
  128. // assumptions about such special cases.
  129. update_option( 'siteurl', 'https://wp.example.org' );
  130. $this->assertFalse( wp_should_replace_insecure_home_url() );
  131. }
  132. private function force_wp_is_using_https( $enabled ) {
  133. $scheme = $enabled ? 'https' : 'http';
  134. $replace_scheme = function( $url ) use ( $scheme ) {
  135. return str_replace( array( 'http://', 'https://' ), $scheme . '://', $url );
  136. };
  137. add_filter( 'home_url', $replace_scheme, 99 );
  138. add_filter( 'site_url', $replace_scheme, 99 );
  139. }
  140. private function force_option( $option, $value ) {
  141. add_filter(
  142. "option_$option",
  143. function() use ( $value ) {
  144. return $value;
  145. }
  146. );
  147. }
  148. }