wpGetUsersWithNoRole.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * @group user
  4. */
  5. class Tests_User_GetUsersWithNoRole extends WP_UnitTestCase {
  6. /**
  7. * @ticket 22993
  8. * @group ms-excluded
  9. */
  10. public function test_get_users_with_no_role_is_accurate() {
  11. // Setup users.
  12. $admin = self::factory()->user->create(
  13. array(
  14. 'role' => 'administrator',
  15. )
  16. );
  17. $editor = self::factory()->user->create(
  18. array(
  19. 'role' => 'editor',
  20. )
  21. );
  22. $nobody = self::factory()->user->create(
  23. array(
  24. 'role' => '',
  25. )
  26. );
  27. $nobody_else = self::factory()->user->create(
  28. array(
  29. 'role' => '',
  30. )
  31. );
  32. // Test users.
  33. $users = wp_get_users_with_no_role();
  34. $this->assertEquals(
  35. array(
  36. $nobody,
  37. $nobody_else,
  38. ),
  39. $users
  40. );
  41. }
  42. /**
  43. * @ticket 22993
  44. * @ticket 36196
  45. * @group multisite
  46. * @group ms-required
  47. */
  48. public function test_get_users_with_no_role_multisite_is_accurate() {
  49. // Setup users.
  50. $admin = self::factory()->user->create(
  51. array(
  52. 'role' => 'administrator',
  53. )
  54. );
  55. $editor = self::factory()->user->create(
  56. array(
  57. 'role' => 'editor',
  58. )
  59. );
  60. $nobody = self::factory()->user->create(
  61. array(
  62. 'role' => '',
  63. )
  64. );
  65. // Setup blogs.
  66. $blog_1 = (int) self::factory()->blog->create(
  67. array(
  68. 'user_id' => $editor,
  69. )
  70. );
  71. // Add editor to blog 1.
  72. add_user_to_blog( $blog_1, $editor, 'editor' );
  73. // Test users on root site.
  74. $users = wp_get_users_with_no_role();
  75. $this->assertSame(
  76. array(
  77. (string) $nobody,
  78. ),
  79. $users
  80. );
  81. // Test users counts on blog 1.
  82. $users = wp_get_users_with_no_role( $blog_1 );
  83. $this->assertSame( array(), $users );
  84. // Add admin to blog 1 with no role.
  85. add_user_to_blog( $blog_1, $admin, '' );
  86. // Re-test users counts on blog 1.
  87. $users = wp_get_users_with_no_role( $blog_1 );
  88. $this->assertSame(
  89. array(
  90. (string) $admin,
  91. ),
  92. $users
  93. );
  94. }
  95. /**
  96. * Role comparison must be done on role name, not role display name.
  97. *
  98. * @ticket 38234
  99. */
  100. public function test_get_users_with_no_role_matches_on_role_name() {
  101. // Create a role with a display name which would not match the role name
  102. // in a case-insentive SQL query.
  103. wp_roles()->add_role( 'somerole', 'Some role display name' );
  104. $someuser = self::factory()->user->create(
  105. array(
  106. 'role' => 'somerole',
  107. )
  108. );
  109. $users = wp_get_users_with_no_role();
  110. wp_roles()->remove_role( 'somerole' );
  111. $this->assertEmpty( $users );
  112. }
  113. /**
  114. * @ticket 42015
  115. * @group multisite
  116. * @group ms-required
  117. */
  118. public function test_get_users_with_no_role_matches_on_role_name_different_site() {
  119. $site_id = (int) self::factory()->blog->create();
  120. switch_to_blog( $site_id );
  121. wp_roles()->add_role( 'somerole', 'Some role display name' );
  122. $user_id = self::factory()->user->create(
  123. array(
  124. 'role' => 'somerole',
  125. )
  126. );
  127. restore_current_blog();
  128. $users = wp_get_users_with_no_role( $site_id );
  129. $this->assertEmpty( $users );
  130. }
  131. }