[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/tests/phpunit/includes/ -> testcase.php (source)

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


Generated: Mon Jul 22 01:01:43 2019 Cross-referenced by PHPXref 0.7.1