rest-site-health-controller.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * Unit tests covering the site health controller.
  4. *
  5. * Also generates the fixture data used by the wp-api.js QUnit tests.
  6. *
  7. * @package WordPress
  8. * @subpackage REST API
  9. * @since 5.6.0
  10. */
  11. /**
  12. * @group restapi
  13. */
  14. class WP_Test_REST_Site_Health_Controller extends WP_Test_REST_TestCase {
  15. /**
  16. * Subscriber user ID.
  17. *
  18. * @since 5.6.0
  19. *
  20. * @var int
  21. */
  22. private static $subscriber;
  23. /**
  24. * Administrator user id.
  25. *
  26. * @since 5.6.0
  27. *
  28. * @var int
  29. */
  30. private static $admin;
  31. /**
  32. * Set up class test fixtures.
  33. *
  34. * @since 5.6.0
  35. *
  36. * @param WP_UnitTest_Factory $factory WordPress unit test factory.
  37. */
  38. public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
  39. self::$subscriber = $factory->user->create(
  40. array(
  41. 'role' => 'subscriber',
  42. )
  43. );
  44. self::$admin = $factory->user->create(
  45. array(
  46. 'role' => 'administrator',
  47. )
  48. );
  49. if ( is_multisite() ) {
  50. grant_super_admin( self::$admin );
  51. }
  52. }
  53. /**
  54. * Clean up test fixtures.
  55. *
  56. * @since 5.6.0
  57. */
  58. public static function wpTearDownAfterClass() {
  59. self::delete_user( self::$subscriber );
  60. self::delete_user( self::$admin );
  61. }
  62. public function test_logged_out() {
  63. $response = rest_do_request( '/wp-site-health/v1/tests/dotorg-communication' );
  64. $this->assertErrorResponse( 'rest_forbidden', $response, 401 );
  65. }
  66. public function test_insufficient_caps() {
  67. wp_set_current_user( self::$subscriber );
  68. $response = rest_do_request( '/wp-site-health/v1/tests/dotorg-communication' );
  69. $this->assertErrorResponse( 'rest_forbidden', $response, 403 );
  70. }
  71. /**
  72. * @group ms-excluded
  73. */
  74. public function test_custom_capability() {
  75. wp_set_current_user( self::$admin );
  76. add_filter(
  77. 'site_health_test_rest_capability_dotorg_communication',
  78. static function () {
  79. return 'a_custom_capability';
  80. }
  81. );
  82. $response = rest_do_request( '/wp-site-health/v1/tests/dotorg-communication' );
  83. $this->assertErrorResponse( 'rest_forbidden', $response, 403 );
  84. }
  85. public function test() {
  86. wp_set_current_user( self::$admin );
  87. $response = rest_do_request( '/wp-site-health/v1/tests/dotorg-communication' );
  88. $this->assertSame( 'dotorg_communication', $response->get_data()['test'] );
  89. }
  90. }