[ Index ] |
PHP Cross Reference of BuddyPress |
[Summary view] [Print] [Text view]
1 <?php 2 3 require_once dirname( __FILE__ ) . '/factory.php'; 4 5 class BP_UnitTestCase extends WP_UnitTestCase { 6 7 protected $temp_has_bp_moderate = array(); 8 protected static $cached_SERVER_NAME = null; 9 10 /** 11 * A flag indicating whether an autocommit has been detected inside of a test. 12 * 13 * @since 2.4.0 14 * 15 * @var bool 16 */ 17 protected $autocommitted = false; 18 19 /** 20 * A list of components that have been deactivated during a test. 21 * 22 * @since 2.4.0 23 * 24 * @var array 25 */ 26 protected $deactivated_components = array(); 27 28 /** 29 * Cribbed from WP so that the self::factory() call comes from this class. 30 * 31 * @since 3.0.0 32 */ 33 public static function setUpBeforeClass() { 34 global $wpdb; 35 36 // Fake WP mail globals, to avoid errors 37 add_filter( 'wp_mail', array( 'BP_UnitTestCase', 'setUp_wp_mail' ) ); 38 add_filter( 'wp_mail_from', array( 'BP_UnitTestCase', 'tearDown_wp_mail' ) ); 39 40 $c = self::get_called_class(); 41 if ( ! method_exists( $c, 'wpSetUpBeforeClass' ) ) { 42 self::commit_transaction(); 43 return; 44 } 45 46 call_user_func( array( $c, 'wpSetUpBeforeClass' ), self::factory() ); 47 48 self::commit_transaction(); 49 } 50 51 public function setUp() { 52 parent::setUp(); 53 54 /* 55 * WP's test suite wipes out BP's directory page mappings with `_delete_all_posts()`. 56 * We must reestablish them before our tests can be successfully run. 57 */ 58 bp_core_add_page_mappings( bp_get_option( 'bp-active-components' ), 'delete' ); 59 60 61 $this->factory = new BP_UnitTest_Factory; 62 63 // Fixes warnings in multisite functions 64 $_SERVER['REMOTE_ADDR'] = ''; 65 global $wpdb; 66 67 // Clean up after autocommits. 68 add_action( 'bp_blogs_recorded_existing_blogs', array( $this, 'set_autocommit_flag' ) ); 69 70 // Make sure Activity actions are reset before each test 71 $this->reset_bp_activity_actions(); 72 73 // Make sure all Post types activities globals are reset before each test 74 $this->reset_bp_activity_post_types_globals(); 75 } 76 77 public function tearDown() { 78 global $wpdb; 79 80 remove_action( 'bp_blogs_recorded_existing_blogs', array( $this, 'set_autocommit_flag' ) ); 81 82 parent::tearDown(); 83 84 // If we detect that a COMMIT has been triggered during the test, clean up blog and user fixtures. 85 if ( $this->autocommitted ) { 86 if ( is_multisite() ) { 87 foreach ( $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs WHERE blog_id != 1" ) as $blog_id ) { 88 wp_uninitialize_site( $blog_id ); 89 } 90 } 91 92 foreach ( $wpdb->get_col( "SELECT ID FROM $wpdb->users WHERE ID != 1" ) as $user_id ) { 93 if ( is_multisite() ) { 94 wpmu_delete_user( $user_id ); 95 } else { 96 wp_delete_user( $user_id ); 97 } 98 } 99 } 100 101 $this->commit_transaction(); 102 103 // Reactivate any components that have been deactivated. 104 foreach ( $this->deactivated_components as $component ) { 105 buddypress()->active_components[ $component ] = 1; 106 } 107 $this->deactivated_components = array(); 108 } 109 110 /** 111 * Multisite-agnostic way to delete a user from the database. 112 * 113 * @since 3.0.0 114 */ 115 public static function delete_user( $user_id ) { 116 $deleted = parent::delete_user( $user_id ); 117 118 // When called in tearDownAfterClass(), BP's cleanup functions may no longer be hooked. 119 if ( bp_is_active( 'activity' ) ) { 120 bp_activity_remove_all_user_data( $user_id ); 121 } 122 123 bp_core_remove_data( $user_id ); 124 125 return $deleted; 126 } 127 128 function clean_up_global_scope() { 129 buddypress()->bp_nav = buddypress()->bp_options_nav = buddypress()->action_variables = buddypress()->canonical_stack = buddypress()->unfiltered_uri = $GLOBALS['bp_unfiltered_uri'] = array(); 130 buddypress()->current_component = buddypress()->current_item = buddypress()->current_action = buddypress()->current_member_type = ''; 131 buddypress()->unfiltered_uri_offset = 0; 132 buddypress()->is_single_item = false; 133 buddypress()->current_user = new stdClass(); 134 buddypress()->displayed_user = new stdClass(); 135 buddypress()->loggedin_user = new stdClass(); 136 buddypress()->pages = array(); 137 138 parent::clean_up_global_scope(); 139 } 140 141 /** 142 * Returns a factory that can be used across tests, even in static methods. 143 * 144 * @since 3.0 145 * 146 * @return BP_UnitTest_Factory 147 */ 148 protected static function factory() { 149 static $factory = null; 150 if ( ! $factory ) { 151 $factory = new BP_UnitTest_Factory(); 152 } 153 return $factory; 154 } 155 156 protected function reset_bp_activity_actions() { 157 buddypress()->activity->actions = new stdClass(); 158 159 /** 160 * Populate the global with default activity actions only 161 * before each test. 162 */ 163 do_action( 'bp_register_activity_actions' ); 164 } 165 166 protected function reset_bp_activity_post_types_globals() { 167 global $wp_post_types; 168 169 // Remove all remaining tracking arguments to each post type 170 foreach ( $wp_post_types as $post_type => $post_type_arg ) { 171 if ( post_type_supports( $post_type, 'buddypress-activity' ) ) { 172 remove_post_type_support( $post_type, 'buddypress-activity' ); 173 } 174 175 if ( isset( $post_type_arg->bp_activity ) ) { 176 unset( $post_type_arg->bp_activity ); 177 } 178 } 179 180 buddypress()->activity->track = array(); 181 } 182 183 function assertPreConditions() { 184 parent::assertPreConditions(); 185 186 // Reinit some of the globals that might have been cleared by BP_UnitTestCase::clean_up_global_scope(). 187 // This is here because it didn't work in clean_up_global_scope(); I don't know why. 188 do_action( 'bp_setup_globals' ); 189 } 190 191 function go_to( $url ) { 192 $GLOBALS['bp']->loggedin_user = NULL; 193 $GLOBALS['bp']->pages = bp_core_get_directory_pages(); 194 195 parent::go_to( $url ); 196 197 do_action( 'bp_init' ); 198 do_action( 'bp_template_redirect' ); 199 } 200 201 /** 202 * WP's core tests use wp_set_current_user() to change the current 203 * user during tests. BP caches the current user differently, so we 204 * have to do a bit more work to change it 205 */ 206 public static function set_current_user( $user_id ) { 207 $bp = buddypress(); 208 209 $bp->loggedin_user->id = $user_id; 210 $bp->loggedin_user->fullname = bp_core_get_user_displayname( $user_id ); 211 $bp->loggedin_user->is_super_admin = $bp->loggedin_user->is_site_admin = is_super_admin( $user_id ); 212 $bp->loggedin_user->domain = bp_core_get_user_domain( $user_id ); 213 $bp->loggedin_user->userdata = bp_core_get_core_userdata( $user_id ); 214 215 wp_set_current_user( $user_id ); 216 } 217 218 public static function add_user_to_group( $user_id, $group_id, $args = array() ) { 219 $r = bp_parse_args( $args, array( 220 'date_modified' => bp_core_current_time(), 221 'is_confirmed' => 1, 222 'is_admin' => 0, 223 'is_mod' => 0, 224 'invite_sent' => 0, 225 'inviter_id' => 0, 226 ) ); 227 228 $new_member = new BP_Groups_Member; 229 $new_member->group_id = $group_id; 230 $new_member->user_id = $user_id; 231 $new_member->inviter_id = 0; 232 $new_member->is_admin = $r['is_admin']; 233 $new_member->is_mod = $r['is_mod']; 234 $new_member->user_title = ''; 235 $new_member->date_modified = $r['date_modified']; 236 $new_member->is_confirmed = $r['is_confirmed']; 237 $new_member->invite_sent = $r['invite_sent']; 238 $new_member->inviter_id = $r['inviter_id']; 239 240 $new_member->save(); 241 return $new_member->id; 242 } 243 244 /** 245 * We can't use grant_super_admin() because we will need to modify 246 * the list more than once, and grant_super_admin() can only be run 247 * once because of its global check 248 */ 249 public function grant_super_admin( $user_id ) { 250 global $super_admins; 251 if ( ! is_multisite() ) { 252 return; 253 } 254 255 $user = get_userdata( $user_id ); 256 $super_admins[] = $user->user_login; 257 } 258 259 public function restore_admins() { 260 // We assume that the global can be wiped out 261 // @see grant_super_admin() 262 unset( $GLOBALS['super_admins'] ); 263 } 264 265 public function grant_bp_moderate( $user_id ) { 266 if ( ! isset( $this->temp_has_bp_moderate[ $user_id ] ) ) { 267 $this->temp_has_bp_moderate[ $user_id ] = 1; 268 } 269 add_filter( 'bp_current_user_can', array( $this, 'grant_bp_moderate_cb' ), 10, 2 ); 270 } 271 272 public function revoke_bp_moderate( $user_id ) { 273 if ( isset( $this->temp_has_bp_moderate[ $user_id ] ) ) { 274 unset( $this->temp_has_bp_moderate[ $user_id ] ); 275 } 276 remove_filter( 'bp_current_user_can', array( $this, 'grant_bp_moderate_cb' ), 10 ); 277 } 278 279 public function grant_bp_moderate_cb( $retval, $capability ) { 280 $current_user = bp_loggedin_user_id(); 281 if ( ! isset( $this->temp_has_bp_moderate[ $current_user ] ) ) { 282 return $retval; 283 } 284 285 if ( 'bp_moderate' == $capability ) { 286 $retval = true; 287 } 288 289 return $retval; 290 } 291 292 /** 293 * Go to the root blog. This helps reset globals after moving between 294 * blogs. 295 */ 296 public function go_to_root() { 297 $blog_1_url = get_blog_option( 1, 'home' ); 298 $this->go_to( str_replace( $blog_1_url, '', trailingslashit( bp_get_root_domain() ) ) ); 299 } 300 301 /** 302 * Set up globals necessary to avoid errors when using wp_mail() 303 */ 304 public static function setUp_wp_mail( $args ) { 305 if ( isset( $_SERVER['SERVER_NAME'] ) ) { 306 self::$cached_SERVER_NAME = $_SERVER['SERVER_NAME']; 307 } 308 309 $_SERVER['SERVER_NAME'] = 'example.com'; 310 311 // passthrough 312 return $args; 313 } 314 315 /** 316 * Tear down globals set up in setUp_wp_mail() 317 */ 318 public static function tearDown_wp_mail( $args ) { 319 if ( ! empty( self::$cached_SERVER_NAME ) ) { 320 $_SERVER['SERVER_NAME'] = self::$cached_SERVER_NAME; 321 self::$cached_SERVER_NAME = ''; 322 } else { 323 $_SERVER['SERVER_NAME'] = WP_TESTS_DOMAIN; 324 } 325 326 // passthrough 327 return $args; 328 } 329 330 /** 331 * Commit a MySQL transaction. 332 */ 333 public static function commit_transaction() { 334 global $wpdb; 335 $wpdb->query( 'COMMIT;' ); 336 } 337 338 /** 339 * Clean up created directories/files 340 */ 341 public function rrmdir( $dir ) { 342 // Make sure we are only removing files/dir from uploads 343 if ( 0 !== strpos( $dir, bp_core_avatar_upload_path() ) ) { 344 return; 345 } 346 347 $d = glob( $dir . '/*' ); 348 349 if ( ! empty( $d ) ) { 350 foreach ( $d as $file ) { 351 if ( is_dir( $file ) ) { 352 $this->rrmdir( $file ); 353 } else { 354 @unlink( $file ); 355 } 356 } 357 } 358 359 @rmdir( $dir ); 360 } 361 362 /** 363 * Set a flag that an autocommit has taken place inside of a test method. 364 * 365 * @since 2.4.0 366 */ 367 public function set_autocommit_flag() { 368 $this->autocommitted = true; 369 } 370 371 /** 372 * Deactivate a component for the duration of a test. 373 * 374 * @since 2.4.0 375 * 376 * @param string $component Component name. 377 */ 378 public function deactivate_component( $component ) { 379 $is_active = isset( buddypress()->active_components[ $component ] ); 380 381 if ( ! isset( $component ) ) { 382 return false; 383 } 384 385 unset( buddypress()->active_components[ $component ] ); 386 $this->deactivated_components[] = $component; 387 } 388 389 /** 390 * Fake an attachment upload (doesn't actually upload a file). 391 * 392 * @param string $file Absolute path to valid file. 393 * @param int $parent Optional. Post ID to attach the new post to. 394 * @return int Attachment post ID. 395 */ 396 public function fake_attachment_upload( $file, $parent = 0 ) { 397 $mime = wp_check_filetype( $file ); 398 if ( $mime ) { 399 $type = $mime['type']; 400 } else { 401 $type = ''; 402 } 403 404 $url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . basename( $file ); 405 $attachment = array( 406 'guid' => 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $url, 407 'post_content' => '', 408 'post_mime_type' => $type, 409 'post_parent' => $parent, 410 'post_title' => basename( $file ), 411 'post_type' => 'attachment', 412 ); 413 414 $id = wp_insert_attachment( $attachment, $url, $parent ); 415 wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $url ) ); 416 417 return $id; 418 } 419 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Nov 6 01:00:56 2024 | Cross-referenced by PHPXref 0.7.1 |