Skip to:
Content

bbPress.org

Ticket #2542: 2542.1.diff

File 2542.1.diff, 65.4 KB (added by netweb, 10 years ago)
  • tests/bootstrap.php

     
     1<?php
     2
     3require( dirname( __FILE__ ) . '/includes/define-constants.php' );
     4
     5if ( ! file_exists( WP_TESTS_DIR . '/includes/functions.php' ) ) {
     6        die( "The WordPress PHPUnit test suite could not be found.\n" );
     7}
     8
     9require_once WP_TESTS_DIR . '/includes/functions.php';
     10
     11function _install_and_load_bbpress() {
     12        require BBP_TESTS_DIR . '/includes/loader.php';
     13}
     14tests_add_filter( 'muplugins_loaded', '_install_and_load_bbpress' );
     15
     16require WP_TESTS_DIR . '/includes/bootstrap.php';
     17
     18// Load the BP-specific testing tools
     19require BBP_TESTS_DIR . '/includes/testcase.php';
  • tests/includes/define-constants.php

     
     1<?php
     2
     3/**
     4 * Define constants needed by test suite.
     5 */
     6
     7define( 'BBP_PLUGIN_DIR', dirname( dirname( dirname( __FILE__ ) ) ) . '/' );
     8
     9if ( ! defined( 'BBP_TESTS_DIR' ) ) {
     10        define( 'BBP_TESTS_DIR', dirname( dirname( __FILE__ ) ) . '/' );
     11}
     12
     13/**
     14 * In the pre-develop.svn WP development environment, an environmental bash
     15 * variable would be set to run PHP Unit tests. However, this has been done
     16 * away with in a post-develop.svn world. We'll still check if this variable
     17 * is set for backwards compat.
     18 */
     19if ( getenv( 'WP_TESTS_DIR' ) ) {
     20        define( 'WP_TESTS_DIR', getenv( 'WP_TESTS_DIR' ) );
     21        define( 'WP_ROOT_DIR', WP_TESTS_DIR );
     22} else {
     23        define( 'WP_ROOT_DIR', dirname( dirname( dirname( dirname( dirname( dirname( __DIR__ ) ) ) ) ) ) );
     24        define( 'WP_TESTS_DIR', WP_ROOT_DIR . '/tests/phpunit' );
     25}
     26
     27// Based on the tests directory, look for a config file
     28if ( file_exists( WP_ROOT_DIR . '/wp-tests-config.php' ) ) {
     29        // Standard develop.svn.wordpress.org setup
     30        define( 'WP_TESTS_CONFIG_PATH', WP_ROOT_DIR . '/wp-tests-config.php' );
     31
     32} else if ( file_exists( WP_TESTS_DIR . '/wp-tests-config.php' ) ) {
     33        // Legacy unit-test.svn.wordpress.org setup
     34        define( 'WP_TESTS_CONFIG_PATH', WP_TESTS_DIR . '/wp-tests-config.php' );
     35
     36} else if ( file_exists( dirname( dirname( WP_TESTS_DIR ) ) . '/wp-tests-config.php' ) ) {
     37        // Environment variable exists and points to tests/phpunit of
     38        // develop.svn.wordpress.org setup
     39        define( 'WP_TESTS_CONFIG_PATH', dirname( dirname( WP_TESTS_DIR ) ) . '/wp-tests-config.php' );
     40
     41} else {
     42        die( "wp-tests-config.php could not be found.\n" );
     43}
  • tests/includes/factory.php

     
     1<?php
     2class BBP_UnitTest_Factory extends WP_UnitTest_Factory {
     3        public $activity = null;
     4
     5        function __construct() {
     6                parent::__construct();
     7
     8                $this->activity = new BBP_UnitTest_Factory_For_Activity( $this );
     9                $this->group = new BBP_UnitTest_Factory_For_Group( $this );
     10                $this->xprofile_group = new BBP_UnitTest_Factory_For_XProfileGroup( $this );
     11                $this->xprofile_field = new BBP_UnitTest_Factory_For_XProfileField( $this );
     12                $this->notification = new BBP_UnitTest_Factory_For_Notification( $this );
     13        }
     14}
     15
     16class BBP_UnitTest_Factory_For_Activity extends WP_UnitTest_Factory_For_Thing {
     17
     18        function __construct( $factory = null ) {
     19                parent::__construct( $factory );
     20
     21                $this->default_generation_definitions = array(
     22                        'action'       => new WP_UnitTest_Generator_Sequence( 'Activity action %s' ),
     23                        'component'    => buddypress()->activity->id,
     24                        'content'      => new WP_UnitTest_Generator_Sequence( 'Activity content %s' ),
     25                        'primary_link' => 'http://example.com',
     26                        'type'         => 'activity_update',
     27                        'recorded_time' => bbp_core_current_time(),
     28                );
     29        }
     30
     31        function create_object( $args ) {
     32                if ( ! isset( $args['user_id'] ) )
     33                        $args['user_id'] = get_current_user_id();
     34
     35                return bbp_activity_add( $args );
     36        }
     37
     38        function update_object( $activity_id, $fields ) {
     39                $activity = new BBP_Activity_Activity( $activity_id );
     40
     41                foreach ( $fields as $field_name => $value ) {
     42                        if ( isset( $activity->$field_name ) )
     43                                $activity->$field_name = $value;
     44                }
     45
     46                $activity->save();
     47                return $activity;
     48        }
     49
     50        function get_object_by_id( $user_id ) {
     51                return new BBP_Activity_Activity( $user_id );
     52        }
     53}
     54
     55class BBP_UnitTest_Factory_For_Group extends WP_UnitTest_Factory_For_Thing {
     56
     57        function __construct( $factory = null ) {
     58                parent::__construct( $factory );
     59
     60                $this->default_generation_definitions = array(
     61                        'name'         => new WP_UnitTest_Generator_Sequence( 'Group %s' ),
     62                        'description'  => new WP_UnitTest_Generator_Sequence( 'Group description %s' ),
     63                        'slug'         => new WP_UnitTest_Generator_Sequence( 'group-slug-%s' ),
     64                        'status'       => 'public',
     65                        'enable_forum' => true,
     66                        'date_created' => bbp_core_current_time(),
     67                );
     68        }
     69
     70        function create_object( $args ) {
     71                if ( ! isset( $args['creator_id'] ) ) {
     72                        $args['creator_id'] = get_current_user_id();
     73                }
     74
     75                $group_id = groups_create_group( $args );
     76
     77                groups_update_groupmeta( $group_id, 'total_member_count', 1 );
     78
     79                $last_activity = isset( $args['last_activity'] ) ? $args['last_activity'] : bbp_core_current_time();
     80                groups_update_groupmeta( $group_id, 'last_activity', $last_activity );
     81
     82                return $group_id;
     83        }
     84
     85        function update_object( $group_id, $fields ) {
     86                $group = new BBP_Groups_Group( $group_id );
     87
     88                foreach ( $fields as $field_name => $value ) {
     89                        if ( isset( $group->field_name ) )
     90                                $group->field_name = $value;
     91                }
     92
     93                $group->save();
     94                return $group;
     95        }
     96
     97        function get_object_by_id( $group_id ) {
     98                return new BBP_Groups_Group( $group_id );
     99        }
     100}
     101
     102class BBP_UnitTest_Factory_For_XProfileGroup extends WP_UnitTest_Factory_For_Thing {
     103
     104        function __construct( $factory = null ) {
     105                parent::__construct( $factory );
     106
     107                $this->default_generation_definitions = array(
     108                        'name'         => new WP_UnitTest_Generator_Sequence( 'XProfile group %s' ),
     109                        'description'  => new WP_UnitTest_Generator_Sequence( 'XProfile group description %s' ),
     110                        'slug'         => new WP_UnitTest_Generator_Sequence( 'xprofile-group-slug-%s' ),
     111                );
     112        }
     113
     114        function create_object( $args ) {
     115                $group_id = xprofile_insert_field_group( $args );
     116                return $this->get_object_by_id( $group_id );
     117        }
     118
     119        function update_object( $group_id, $fields ) {
     120        }
     121
     122        function get_object_by_id( $group_id ) {
     123                return new BBP_XProfile_Group( $group_id );
     124        }
     125}
     126
     127class BBP_UnitTest_Factory_For_XProfileField extends WP_UnitTest_Factory_For_Thing {
     128
     129        function __construct( $factory = null ) {
     130                parent::__construct( $factory );
     131
     132                $this->default_generation_definitions = array(
     133                        'name'         => new WP_UnitTest_Generator_Sequence( 'XProfile field %s' ),
     134                        'description'  => new WP_UnitTest_Generator_Sequence( 'XProfile field description %s' ),
     135                );
     136        }
     137
     138        function create_object( $args ) {
     139                $field_id = xprofile_insert_field( $args );
     140                return $this->get_object_by_id( $field_id );
     141        }
     142
     143        function update_object( $field_id, $fields ) {
     144        }
     145
     146        function get_object_by_id( $field_id ) {
     147                return new BBP_XProfile_Field( $field_id );
     148        }
     149}
     150
     151class BBP_UnitTest_Factory_For_Notification extends WP_UnitTest_Factory_For_Thing {
     152        public function __construct( $factory = null ) {
     153                parent::__construct( $factory );
     154        }
     155
     156        public function create_object( $args ) {
     157                return bbp_notifications_add_notification( $args );
     158        }
     159
     160        public function update_object( $id, $fields ) {}
     161
     162        public function get_object_by_id( $id ) {
     163                return new BBP_Notifications_Notification( $id );
     164        }
     165}
  • tests/includes/install.php

     
     1<?php
     2/**
     3 * Installs bbPress for the purpose of the unit-tests
     4 *
     5 * @todo Reuse the init/load code in init.php
     6 * @todo Support MULTIBLOG
     7 */
     8error_reporting( E_ALL & ~E_DEPRECATED & ~E_STRICT );
     9
     10$config_file_path = $argv[1];
     11$tests_dir_path = $argv[2];
     12$multisite = ! empty( $argv[3] );
     13
     14require_once $config_file_path;
     15require_once $tests_dir_path . '/includes/functions.php';
     16
     17function _load_bbpress() {
     18        require dirname( dirname( dirname( __FILE__ ) ) ) . '/bbpress.php';
     19}
     20tests_add_filter( 'muplugins_loaded', '_load_bbpress' );
     21
     22define( 'BBP_PLUGIN_DIR', dirname( dirname( dirname( __FILE__ ) ) ) . '/' );
     23define( 'BBP_ROOT_BLOG', 1 );
     24
     25// Always load admin bar
     26tests_add_filter( 'show_admin_bar', '__return_true' );
     27
     28$_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';
     29$_SERVER['HTTP_HOST'] = WP_TESTS_DOMAIN;
     30$PHP_SELF = $GLOBALS['PHP_SELF'] = $_SERVER['PHP_SELF'] = '/index.php';
     31
     32require_once ABSPATH . '/wp-settings.php';
     33
     34echo "Installing bbPress...\n";
     35
     36// Make sure that bbPress has been cleaned from all blogs before reinstalling
     37$blogs = is_multisite() ? $wpdb->get_col( "SELECT blog_id FROM {$wpdb->blogs}" ) : array( 1 );
     38foreach ( $blogs as $blog ) {
     39        if ( is_multisite() ) {
     40                switch_to_blog( $blog );
     41        }
     42
     43        $wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE '%bbp%'" );
     44
     45        if ( is_multisite() ) {
     46                restore_current_blog();
     47        }
     48}
     49
     50$wpdb->query( 'SET storage_engine = INNODB' );
     51$wpdb->select( DB_NAME, $wpdb->dbh );
     52
     53// Install BuddyPress
     54// bbp_version_updater();
  • tests/includes/loader.php

     
     1<?php
     2
     3require_once( dirname( __FILE__ ) . '/define-constants.php' );
     4
     5$multisite = (int) ( defined( 'WP_TESTS_MULTISITE') && WP_TESTS_MULTISITE );
     6system( WP_PHP_BINARY . ' ' . escapeshellarg( dirname( __FILE__ ) . '/install.php' ) . ' ' . escapeshellarg( WP_TESTS_CONFIG_PATH ) . ' ' . escapeshellarg( WP_TESTS_DIR ) . ' ' . $multisite );
     7
     8// Bootstrap BBP
     9require dirname( __FILE__ ) . '/../../bbpress.php';
  • tests/includes/testcase.php

     
     1<?php
     2
     3/**
     4 * WP's test suite wipes out BBP's directory page mappings with _delete_all_posts()
     5 * We must reestablish them before our tests can be successfully run
     6 */
     7
     8require_once dirname( __FILE__ ) . '/factory.php';
     9
     10class BBP_UnitTestCase extends WP_UnitTestCase {
     11
     12        protected $temp_has_bbp_moderate = array();
     13        protected $cached_SERVER_NAME = null;
     14
     15        public function setUp() {
     16                parent::setUp();
     17
     18                // Make sure all users are deleted
     19                // There's a bug in the multisite tests that causes the
     20                // transaction rollback to fail for the first user created,
     21                // which busts every other attempt to create users. This is a
     22                // hack workaround
     23                global $wpdb;
     24                $wpdb->query( "TRUNCATE TABLE {$wpdb->users}" );
     25
     26                // Fake WP mail globals, to avoid errors
     27                add_filter( 'wp_mail', array( $this, 'setUp_wp_mail' ) );
     28                add_filter( 'wp_mail_from', array( $this, 'tearDown_wp_mail' ) );
     29
     30                $this->factory = new BBP_UnitTest_Factory;
     31        }
     32
     33        function clean_up_global_scope() {
     34                buddypress()->bbp_nav                = buddypress()->bbp_options_nav = buddypress()->action_variables = buddypress()->canonical_stack = buddypress()->unfiltered_uri = $GLOBALS['bbp_unfiltered_uri'] = array();
     35                buddypress()->current_component     = buddypress()->current_item = buddypress()->current_action = '';
     36                buddypress()->unfiltered_uri_offset = 0;
     37                buddypress()->is_single_item        = false;
     38                buddypress()->current_user          = new stdClass();
     39                buddypress()->displayed_user        = new stdClass();
     40                buddypress()->loggedin_user         = new stdClass();
     41                buddypress()->avatar                = new stdClass();
     42
     43                parent::clean_up_global_scope();
     44        }
     45
     46        function assertPreConditions() {
     47                parent::assertPreConditions();
     48
     49                // Reinit some of the globals that might have been cleared by BBP_UnitTestCase::clean_up_global_scope().
     50                // This is here because it didn't work in clean_up_global_scope(); I don't know why.
     51                do_action( 'bbp_setup_globals' );
     52        }
     53
     54        function go_to( $url ) {
     55                // note: the WP and WP_Query classes like to silently fetch parameters
     56                // from all over the place (globals, GET, etc), which makes it tricky
     57                // to run them more than once without very carefully clearing everything
     58                $_GET = $_POST = array();
     59                foreach (array('query_string', 'id', 'postdata', 'authordata', 'day', 'currentmonth', 'page', 'pages', 'multipage', 'more', 'numpages', 'pagenow') as $v) {
     60                        if ( isset( $GLOBALS[$v] ) ) unset( $GLOBALS[$v] );
     61                }
     62                $parts = parse_url($url);
     63                if (isset($parts['scheme'])) {
     64                        // set the HTTP_HOST
     65                        $GLOBALS['_SERVER']['HTTP_HOST'] = $parts['host'];
     66
     67                        $req = $parts['path'];
     68                        if (isset($parts['query'])) {
     69                                $req .= '?' . $parts['query'];
     70                                // parse the url query vars into $_GET
     71                                parse_str($parts['query'], $_GET);
     72                        }
     73                } else {
     74                        $req = $url;
     75                }
     76                if ( ! isset( $parts['query'] ) ) {
     77                        $parts['query'] = '';
     78                }
     79
     80                // Scheme
     81                if ( 0 === strpos( $req, '/wp-admin' ) && force_ssl_admin() ) {
     82                        $_SERVER['HTTPS'] = 'on';
     83                } else {
     84                        unset( $_SERVER['HTTPS'] );
     85                }
     86
     87                // Set this for bbp_core_set_uri_globals()
     88                $GLOBALS['_SERVER']['REQUEST_URI'] = $req;
     89                unset($_SERVER['PATH_INFO']);
     90
     91                // setup $current_site and $current_blog globals for multisite based on
     92                // REQUEST_URI; mostly copied from /wp-includes/ms-settings.php
     93                if ( is_multisite() ) {
     94                        $domain = addslashes( $_SERVER['HTTP_HOST'] );
     95                        if ( false !== strpos( $domain, ':' ) ) {
     96                                if ( substr( $domain, -3 ) == ':80' ) {
     97                                        $domain = substr( $domain, 0, -3 );
     98                                        $_SERVER['HTTP_HOST'] = substr( $_SERVER['HTTP_HOST'], 0, -3 );
     99                                } elseif ( substr( $domain, -4 ) == ':443' ) {
     100                                        $domain = substr( $domain, 0, -4 );
     101                                        $_SERVER['HTTP_HOST'] = substr( $_SERVER['HTTP_HOST'], 0, -4 );
     102                                }
     103                        }
     104
     105                        $domain = rtrim( $domain, '.' );
     106                        $cookie_domain = $domain;
     107                        if ( substr( $cookie_domain, 0, 4 ) == 'www.' )
     108                                $cookie_domain = substr( $cookie_domain, 4 );
     109
     110                        $path = preg_replace( '|([a-z0-9-]+.php.*)|', '', $GLOBALS['_SERVER']['REQUEST_URI'] );
     111                        $path = str_replace ( '/wp-admin/', '/', $path );
     112                        $path = preg_replace( '|(/[a-z0-9-]+?/).*|', '$1', $path );
     113
     114                        $GLOBALS['current_site'] = wpmu_current_site();
     115                        if ( ! isset( $GLOBALS['current_site']->blog_id ) )
     116                                $GLOBALS['current_site']->blog_id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE domain = %s AND path = %s", $GLOBALS['current_site']->domain, $GLOBALS['current_site']->path ) );
     117
     118                        // unit tests only support subdirectory install at the moment
     119                        // removed object cache references
     120                        if ( ! is_subdomain_install() ) {
     121                                $blogname = htmlspecialchars( substr( $GLOBALS['_SERVER']['REQUEST_URI'], strlen( $path ) ) );
     122                                if ( false !== strpos( $blogname, '/' ) )
     123                                        $blogname = substr( $blogname, 0, strpos( $blogname, '/' ) );
     124                                if ( false !== strpos( $blogname, '?' ) )
     125                                        $blogname = substr( $blogname, 0, strpos( $blogname, '?' ) );
     126                                $reserved_blognames = array( 'page', 'comments', 'blog', 'wp-admin', 'wp-includes', 'wp-content', 'files', 'feed' );
     127                                if ( $blogname != '' && ! in_array( $blogname, $reserved_blognames ) && ! is_file( $blogname ) )
     128                                        $path .= $blogname . '/';
     129
     130                                $GLOBALS['current_blog'] = get_blog_details( array( 'domain' => $domain, 'path' => $path ), false );
     131
     132                                unset($reserved_blognames);
     133                        }
     134
     135                        $GLOBALS['blog_id'] = $GLOBALS['current_blog']->blog_id;
     136                }
     137
     138                unset($GLOBALS['wp_query'], $GLOBALS['wp_the_query']);
     139                $GLOBALS['wp_the_query'] = new WP_Query();
     140                $GLOBALS['wp_query'] = $GLOBALS['wp_the_query'];
     141                $GLOBALS['wp'] = new WP();
     142
     143                // clean out globals to stop them polluting wp and wp_query
     144                foreach ($GLOBALS['wp']->public_query_vars as $v) {
     145                        unset($GLOBALS[$v]);
     146                }
     147                foreach ($GLOBALS['wp']->private_query_vars as $v) {
     148                        unset($GLOBALS[$v]);
     149                }
     150
     151                $GLOBALS['wp']->main($parts['query']);
     152
     153                // For bbPress, James.
     154                $GLOBALS['bbp']->loggedin_user = NULL;
     155                do_action( 'bbp_init' );
     156        }
     157
     158        protected function checkRequirements() {
     159                if ( WP_TESTS_FORCE_KNOWN_BUGS )
     160                        return;
     161
     162                parent::checkRequirements();
     163
     164                $tickets = PHPUnit_Util_Test::getTickets( get_class( $this ), $this->getName( false ) );
     165                foreach ( $tickets as $ticket ) {
     166                        if ( 'BBP' == substr( $ticket, 0, 2 ) ) {
     167                                $ticket = substr( $ticket, 2 );
     168                                if ( $ticket && is_numeric( $ticket ) )
     169                                        $this->knownBBPBug( $ticket );
     170                        }
     171                }
     172        }
     173
     174        /**
     175         * Skips the current test if there is an open bbPress ticket with id $ticket_id
     176         */
     177        function knownBBPBug( $ticket_id ) {
     178                if ( WP_TESTS_FORCE_KNOWN_BUGS || in_array( $ticket_id, self::$forced_tickets ) )
     179                        return;
     180
     181                if ( ! TracTickets::isTracTicketClosed( 'http://buddypress.trac.wordpress.org', $ticket_id ) )
     182                        $this->markTestSkipped( sprintf( 'BuddyPress Ticket #%d is not fixed', $ticket_id ) );
     183        }
     184
     185        /**
     186         * WP's core tests use wp_set_current_user() to change the current
     187         * user during tests. BBP caches the current user differently, so we
     188         * have to do a bit more work to change it
     189         *
     190         * @global bbPres $bbp
     191         */
     192        function set_current_user( $user_id ) {
     193                global $bbp;
     194                $bbp->loggedin_user->id = $user_id;
     195                $bbp->loggedin_user->fullname       = bbp_core_get_user_displayname( $user_id );
     196                $bbp->loggedin_user->is_super_admin = $bbp->loggedin_user->is_site_admin = is_super_admin( $user_id );
     197                $bbp->loggedin_user->domain         = bbp_core_get_user_domain( $user_id );
     198                $bbp->loggedin_user->userdata       = bbp_core_get_core_userdata( $user_id );
     199
     200                wp_set_current_user( $user_id );
     201        }
     202
     203        /**
     204         * When creating a new user, it's almost always necessary to have the
     205         * last_activity usermeta set right away, so that the user shows up in
     206         * directory queries. This is a shorthand wrapper for the user factory
     207         * create() method.
     208         *
     209         * Also set a display name
     210         */
     211        function create_user( $args = array() ) {
     212                $r = wp_parse_args( $args, array(
     213                        'role' => 'subscriber',
     214                        'last_activity' => bbp_core_current_time() - 60*60*24*365,
     215                ) );
     216
     217                $last_activity = $r['last_activity'];
     218                unset( $r['last_activity'] );
     219
     220                $user_id = $this->factory->user->create( $args );
     221
     222                bbp_update_user_last_activity( $user_id, $last_activity );
     223
     224                if ( bbp_is_active( 'xprofile' ) ) {
     225                        $user = new WP_User( $user_id );
     226                        xprofile_set_field_data( 1, $user_id, $user->display_name );
     227                }
     228
     229                return $user_id;
     230        }
     231
     232        public static function add_user_to_group( $user_id, $group_id, $args = array() ) {
     233                $r = wp_parse_args( $args, array(
     234                        'date_modified' => bbp_core_current_time(),
     235                        'is_confirmed' => 1,
     236                ) );
     237
     238                $new_member                = new BBP_Groups_Member;
     239                $new_member->group_id      = $group_id;
     240                $new_member->user_id       = $user_id;
     241                $new_member->inviter_id    = 0;
     242                $new_member->is_admin      = 0;
     243                $new_member->user_title    = '';
     244                $new_member->date_modified = $r['date_modified'];
     245                $new_member->is_confirmed  = $r['is_confirmed'];
     246
     247                $new_member->save();
     248                return $new_member->id;
     249        }
     250
     251        /**
     252         * We can't use grant_super_admin() because we will need to modify
     253         * the list more than once, and grant_super_admin() can only be run
     254         * once because of its global check
     255         */
     256        public function grant_super_admin( $user_id ) {
     257                global $super_admins;
     258                if ( ! is_multisite() ) {
     259                        return;
     260                }
     261
     262                $user = get_userdata( $user_id );
     263                $super_admins[] = $user->user_login;
     264        }
     265
     266        public function restore_admins() {
     267                // We assume that the global can be wiped out
     268                // @see grant_super_admin()
     269                unset( $GLOBALS['super_admins'] );
     270        }
     271
     272        public function grant_bbp_moderate( $user_id ) {
     273                if ( ! isset( $this->temp_has_bbp_moderate[ $user_id ] ) ) {
     274                        $this->temp_has_bbp_moderate[ $user_id ] = 1;
     275                }
     276                add_filter( 'bbp_current_user_can', array( $this, 'grant_bbp_moderate_cb' ), 10, 2 );
     277        }
     278
     279        public function revoke_bbp_moderate( $user_id ) {
     280                if ( isset( $this->temp_has_bbp_moderate[ $user_id ] ) ) {
     281                        unset( $this->temp_has_bbp_moderate[ $user_id ] );
     282                }
     283                remove_filter( 'bbp_current_user_can', array( $this, 'grant_bbp_moderate_cb' ), 10, 2 );
     284        }
     285
     286        public function grant_bbp_moderate_cb( $retval, $capability ) {
     287                $current_user = bbp_loggedin_user_id();
     288                if ( ! isset( $this->temp_has_bbp_moderate[ $current_user ] ) ) {
     289                        return $retval;
     290                }
     291
     292                if ( 'bbp_moderate' == $capability ) {
     293                        $retval = true;
     294                }
     295
     296                return $retval;
     297        }
     298
     299        /**
     300         * Go to the root blog. This helps reset globals after moving between
     301         * blogs.
     302         */
     303        public function go_to_root() {
     304                $blog_1_url = get_blog_option( 1, 'home' );
     305                $this->go_to( str_replace( $blog_1_url, '', trailingslashit( bbp_get_root_domain() ) ) );
     306        }
     307
     308        /**
     309         * Set up globals necessary to avoid errors when using wp_mail()
     310         */
     311        public function setUp_wp_mail( $args ) {
     312                if ( isset( $_SERVER['SERVER_NAME'] ) ) {
     313                        $this->cached_SERVER_NAME = $_SERVER['SERVER_NAME'];
     314                }
     315
     316                $_SERVER['SERVER_NAME'] = 'example.com';
     317
     318                // passthrough
     319                return $args;
     320        }
     321
     322        /**
     323         * Tear down globals set up in setUp_wp_mail()
     324         */
     325        public function tearDown_wp_mail( $args ) {
     326                if ( ! empty( $this->cached_SERVER_NAME ) ) {
     327                        $_SERVER['SERVER_NAME'] = $this->cached_SERVER_NAME;
     328                        unset( $this->cached_SERVER_NAME );
     329                } else {
     330                        unset( $_SERVER['SERVER_NAME'] );
     331                }
     332
     333                // passthrough
     334                return $args;
     335        }
     336}
  • tests/multisite.xml

     
     1<phpunit
     2        bootstrap="bootstrap.php"
     3        backupGlobals="false"
     4        colors="true"
     5        convertErrorsToExceptions="true"
     6        convertNoticesToExceptions="true"
     7        convertWarningsToExceptions="true"
     8        >
     9        <php>
     10                <const name="WP_TESTS_MULTISITE" value="1" />
     11        </php>
     12        <testsuites>
     13                <testsuite>
     14                        <directory suffix=".php">./testcases/</directory>
     15                </testsuite>
     16        </testsuites>
     17</phpunit>
  • tests/phpunit.xml

     
     1<phpunit
     2        bootstrap="bootstrap.php"
     3        backupGlobals="false"
     4        colors="true"
     5        convertErrorsToExceptions="true"
     6        convertNoticesToExceptions="true"
     7        convertWarningsToExceptions="true"
     8        >
     9        <testsuites>
     10                <testsuite>
     11                        <directory suffix=".php">./testcases/</directory>
     12                </testsuite>
     13        </testsuites>
     14</phpunit>
  • tests/bootstrap.php

     
     1<?php
     2
     3require( dirname( __FILE__ ) . '/includes/define-constants.php' );
     4
     5if ( ! file_exists( WP_TESTS_DIR . '/includes/functions.php' ) ) {
     6        die( "The WordPress PHPUnit test suite could not be found.\n" );
     7}
     8
     9require_once WP_TESTS_DIR . '/includes/functions.php';
     10
     11function _install_and_load_bbpress() {
     12        require BBP_TESTS_DIR . '/includes/loader.php';
     13}
     14tests_add_filter( 'muplugins_loaded', '_install_and_load_bbpress' );
     15
     16require WP_TESTS_DIR . '/includes/bootstrap.php';
     17
     18// Load the BP-specific testing tools
     19require BBP_TESTS_DIR . '/includes/testcase.php';
  • tests/includes/define-constants.php

     
     1<?php
     2
     3/**
     4 * Define constants needed by test suite.
     5 */
     6
     7define( 'BBP_PLUGIN_DIR', dirname( dirname( dirname( __FILE__ ) ) ) . '/' );
     8
     9if ( ! defined( 'BBP_TESTS_DIR' ) ) {
     10        define( 'BBP_TESTS_DIR', dirname( dirname( __FILE__ ) ) . '/' );
     11}
     12
     13/**
     14 * In the pre-develop.svn WP development environment, an environmental bash
     15 * variable would be set to run PHP Unit tests. However, this has been done
     16 * away with in a post-develop.svn world. We'll still check if this variable
     17 * is set for backwards compat.
     18 */
     19if ( getenv( 'WP_TESTS_DIR' ) ) {
     20        define( 'WP_TESTS_DIR', getenv( 'WP_TESTS_DIR' ) );
     21        define( 'WP_ROOT_DIR', WP_TESTS_DIR );
     22} else {
     23        define( 'WP_ROOT_DIR', dirname( dirname( dirname( dirname( dirname( dirname( __DIR__ ) ) ) ) ) ) );
     24        define( 'WP_TESTS_DIR', WP_ROOT_DIR . '/tests/phpunit' );
     25}
     26
     27// Based on the tests directory, look for a config file
     28if ( file_exists( WP_ROOT_DIR . '/wp-tests-config.php' ) ) {
     29        // Standard develop.svn.wordpress.org setup
     30        define( 'WP_TESTS_CONFIG_PATH', WP_ROOT_DIR . '/wp-tests-config.php' );
     31
     32} else if ( file_exists( WP_TESTS_DIR . '/wp-tests-config.php' ) ) {
     33        // Legacy unit-test.svn.wordpress.org setup
     34        define( 'WP_TESTS_CONFIG_PATH', WP_TESTS_DIR . '/wp-tests-config.php' );
     35
     36} else if ( file_exists( dirname( dirname( WP_TESTS_DIR ) ) . '/wp-tests-config.php' ) ) {
     37        // Environment variable exists and points to tests/phpunit of
     38        // develop.svn.wordpress.org setup
     39        define( 'WP_TESTS_CONFIG_PATH', dirname( dirname( WP_TESTS_DIR ) ) . '/wp-tests-config.php' );
     40
     41} else {
     42        die( "wp-tests-config.php could not be found.\n" );
     43}
  • tests/includes/factory.php

     
     1<?php
     2class BBP_UnitTest_Factory extends WP_UnitTest_Factory {
     3        public $activity = null;
     4
     5        function __construct() {
     6                parent::__construct();
     7
     8                $this->activity = new BBP_UnitTest_Factory_For_Activity( $this );
     9                $this->group = new BBP_UnitTest_Factory_For_Group( $this );
     10                $this->xprofile_group = new BBP_UnitTest_Factory_For_XProfileGroup( $this );
     11                $this->xprofile_field = new BBP_UnitTest_Factory_For_XProfileField( $this );
     12                $this->notification = new BBP_UnitTest_Factory_For_Notification( $this );
     13        }
     14}
     15
     16class BBP_UnitTest_Factory_For_Activity extends WP_UnitTest_Factory_For_Thing {
     17
     18        function __construct( $factory = null ) {
     19                parent::__construct( $factory );
     20
     21                $this->default_generation_definitions = array(
     22                        'action'       => new WP_UnitTest_Generator_Sequence( 'Activity action %s' ),
     23                        'component'    => buddypress()->activity->id,
     24                        'content'      => new WP_UnitTest_Generator_Sequence( 'Activity content %s' ),
     25                        'primary_link' => 'http://example.com',
     26                        'type'         => 'activity_update',
     27                        'recorded_time' => bbp_core_current_time(),
     28                );
     29        }
     30
     31        function create_object( $args ) {
     32                if ( ! isset( $args['user_id'] ) )
     33                        $args['user_id'] = get_current_user_id();
     34
     35                return bbp_activity_add( $args );
     36        }
     37
     38        function update_object( $activity_id, $fields ) {
     39                $activity = new BBP_Activity_Activity( $activity_id );
     40
     41                foreach ( $fields as $field_name => $value ) {
     42                        if ( isset( $activity->$field_name ) )
     43                                $activity->$field_name = $value;
     44                }
     45
     46                $activity->save();
     47                return $activity;
     48        }
     49
     50        function get_object_by_id( $user_id ) {
     51                return new BBP_Activity_Activity( $user_id );
     52        }
     53}
     54
     55class BBP_UnitTest_Factory_For_Group extends WP_UnitTest_Factory_For_Thing {
     56
     57        function __construct( $factory = null ) {
     58                parent::__construct( $factory );
     59
     60                $this->default_generation_definitions = array(
     61                        'name'         => new WP_UnitTest_Generator_Sequence( 'Group %s' ),
     62                        'description'  => new WP_UnitTest_Generator_Sequence( 'Group description %s' ),
     63                        'slug'         => new WP_UnitTest_Generator_Sequence( 'group-slug-%s' ),
     64                        'status'       => 'public',
     65                        'enable_forum' => true,
     66                        'date_created' => bbp_core_current_time(),
     67                );
     68        }
     69
     70        function create_object( $args ) {
     71                if ( ! isset( $args['creator_id'] ) ) {
     72                        $args['creator_id'] = get_current_user_id();
     73                }
     74
     75                $group_id = groups_create_group( $args );
     76
     77                groups_update_groupmeta( $group_id, 'total_member_count', 1 );
     78
     79                $last_activity = isset( $args['last_activity'] ) ? $args['last_activity'] : bbp_core_current_time();
     80                groups_update_groupmeta( $group_id, 'last_activity', $last_activity );
     81
     82                return $group_id;
     83        }
     84
     85        function update_object( $group_id, $fields ) {
     86                $group = new BBP_Groups_Group( $group_id );
     87
     88                foreach ( $fields as $field_name => $value ) {
     89                        if ( isset( $group->field_name ) )
     90                                $group->field_name = $value;
     91                }
     92
     93                $group->save();
     94                return $group;
     95        }
     96
     97        function get_object_by_id( $group_id ) {
     98                return new BBP_Groups_Group( $group_id );
     99        }
     100}
     101
     102class BBP_UnitTest_Factory_For_XProfileGroup extends WP_UnitTest_Factory_For_Thing {
     103
     104        function __construct( $factory = null ) {
     105                parent::__construct( $factory );
     106
     107                $this->default_generation_definitions = array(
     108                        'name'         => new WP_UnitTest_Generator_Sequence( 'XProfile group %s' ),
     109                        'description'  => new WP_UnitTest_Generator_Sequence( 'XProfile group description %s' ),
     110                        'slug'         => new WP_UnitTest_Generator_Sequence( 'xprofile-group-slug-%s' ),
     111                );
     112        }
     113
     114        function create_object( $args ) {
     115                $group_id = xprofile_insert_field_group( $args );
     116                return $this->get_object_by_id( $group_id );
     117        }
     118
     119        function update_object( $group_id, $fields ) {
     120        }
     121
     122        function get_object_by_id( $group_id ) {
     123                return new BBP_XProfile_Group( $group_id );
     124        }
     125}
     126
     127class BBP_UnitTest_Factory_For_XProfileField extends WP_UnitTest_Factory_For_Thing {
     128
     129        function __construct( $factory = null ) {
     130                parent::__construct( $factory );
     131
     132                $this->default_generation_definitions = array(
     133                        'name'         => new WP_UnitTest_Generator_Sequence( 'XProfile field %s' ),
     134                        'description'  => new WP_UnitTest_Generator_Sequence( 'XProfile field description %s' ),
     135                );
     136        }
     137
     138        function create_object( $args ) {
     139                $field_id = xprofile_insert_field( $args );
     140                return $this->get_object_by_id( $field_id );
     141        }
     142
     143        function update_object( $field_id, $fields ) {
     144        }
     145
     146        function get_object_by_id( $field_id ) {
     147                return new BBP_XProfile_Field( $field_id );
     148        }
     149}
     150
     151class BBP_UnitTest_Factory_For_Notification extends WP_UnitTest_Factory_For_Thing {
     152        public function __construct( $factory = null ) {
     153                parent::__construct( $factory );
     154        }
     155
     156        public function create_object( $args ) {
     157                return bbp_notifications_add_notification( $args );
     158        }
     159
     160        public function update_object( $id, $fields ) {}
     161
     162        public function get_object_by_id( $id ) {
     163                return new BBP_Notifications_Notification( $id );
     164        }
     165}
  • tests/includes/install.php

     
     1<?php
     2/**
     3 * Installs bbPress for the purpose of the unit-tests
     4 *
     5 * @todo Reuse the init/load code in init.php
     6 * @todo Support MULTIBLOG
     7 */
     8error_reporting( E_ALL & ~E_DEPRECATED & ~E_STRICT );
     9
     10$config_file_path = $argv[1];
     11$tests_dir_path = $argv[2];
     12$multisite = ! empty( $argv[3] );
     13
     14require_once $config_file_path;
     15require_once $tests_dir_path . '/includes/functions.php';
     16
     17function _load_bbpress() {
     18        require dirname( dirname( dirname( __FILE__ ) ) ) . '/bbpress.php';
     19}
     20tests_add_filter( 'muplugins_loaded', '_load_bbpress' );
     21
     22define( 'BBP_PLUGIN_DIR', dirname( dirname( dirname( __FILE__ ) ) ) . '/' );
     23define( 'BBP_ROOT_BLOG', 1 );
     24
     25// Always load admin bar
     26tests_add_filter( 'show_admin_bar', '__return_true' );
     27
     28$_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';
     29$_SERVER['HTTP_HOST'] = WP_TESTS_DOMAIN;
     30$PHP_SELF = $GLOBALS['PHP_SELF'] = $_SERVER['PHP_SELF'] = '/index.php';
     31
     32require_once ABSPATH . '/wp-settings.php';
     33
     34echo "Installing bbPress...\n";
     35
     36// Make sure that bbPress has been cleaned from all blogs before reinstalling
     37$blogs = is_multisite() ? $wpdb->get_col( "SELECT blog_id FROM {$wpdb->blogs}" ) : array( 1 );
     38foreach ( $blogs as $blog ) {
     39        if ( is_multisite() ) {
     40                switch_to_blog( $blog );
     41        }
     42
     43        $wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE '%bbp%'" );
     44
     45        if ( is_multisite() ) {
     46                restore_current_blog();
     47        }
     48}
     49
     50$wpdb->query( 'SET storage_engine = INNODB' );
     51$wpdb->select( DB_NAME, $wpdb->dbh );
     52
     53// Install BuddyPress
     54// bbp_version_updater();
  • tests/includes/loader.php

     
     1<?php
     2
     3require_once( dirname( __FILE__ ) . '/define-constants.php' );
     4
     5$multisite = (int) ( defined( 'WP_TESTS_MULTISITE') && WP_TESTS_MULTISITE );
     6system( WP_PHP_BINARY . ' ' . escapeshellarg( dirname( __FILE__ ) . '/install.php' ) . ' ' . escapeshellarg( WP_TESTS_CONFIG_PATH ) . ' ' . escapeshellarg( WP_TESTS_DIR ) . ' ' . $multisite );
     7
     8// Bootstrap BBP
     9require dirname( __FILE__ ) . '/../../bbpress.php';
  • tests/includes/testcase.php

     
     1<?php
     2
     3/**
     4 * WP's test suite wipes out BBP's directory page mappings with _delete_all_posts()
     5 * We must reestablish them before our tests can be successfully run
     6 */
     7
     8require_once dirname( __FILE__ ) . '/factory.php';
     9
     10class BBP_UnitTestCase extends WP_UnitTestCase {
     11
     12        protected $temp_has_bbp_moderate = array();
     13        protected $cached_SERVER_NAME = null;
     14
     15        public function setUp() {
     16                parent::setUp();
     17
     18                // Make sure all users are deleted
     19                // There's a bug in the multisite tests that causes the
     20                // transaction rollback to fail for the first user created,
     21                // which busts every other attempt to create users. This is a
     22                // hack workaround
     23                global $wpdb;
     24                $wpdb->query( "TRUNCATE TABLE {$wpdb->users}" );
     25
     26                // Fake WP mail globals, to avoid errors
     27                add_filter( 'wp_mail', array( $this, 'setUp_wp_mail' ) );
     28                add_filter( 'wp_mail_from', array( $this, 'tearDown_wp_mail' ) );
     29
     30                $this->factory = new BBP_UnitTest_Factory;
     31        }
     32
     33        function clean_up_global_scope() {
     34                buddypress()->bbp_nav                = buddypress()->bbp_options_nav = buddypress()->action_variables = buddypress()->canonical_stack = buddypress()->unfiltered_uri = $GLOBALS['bbp_unfiltered_uri'] = array();
     35                buddypress()->current_component     = buddypress()->current_item = buddypress()->current_action = '';
     36                buddypress()->unfiltered_uri_offset = 0;
     37                buddypress()->is_single_item        = false;
     38                buddypress()->current_user          = new stdClass();
     39                buddypress()->displayed_user        = new stdClass();
     40                buddypress()->loggedin_user         = new stdClass();
     41                buddypress()->avatar                = new stdClass();
     42
     43                parent::clean_up_global_scope();
     44        }
     45
     46        function assertPreConditions() {
     47                parent::assertPreConditions();
     48
     49                // Reinit some of the globals that might have been cleared by BBP_UnitTestCase::clean_up_global_scope().
     50                // This is here because it didn't work in clean_up_global_scope(); I don't know why.
     51                do_action( 'bbp_setup_globals' );
     52        }
     53
     54        function go_to( $url ) {
     55                // note: the WP and WP_Query classes like to silently fetch parameters
     56                // from all over the place (globals, GET, etc), which makes it tricky
     57                // to run them more than once without very carefully clearing everything
     58                $_GET = $_POST = array();
     59                foreach (array('query_string', 'id', 'postdata', 'authordata', 'day', 'currentmonth', 'page', 'pages', 'multipage', 'more', 'numpages', 'pagenow') as $v) {
     60                        if ( isset( $GLOBALS[$v] ) ) unset( $GLOBALS[$v] );
     61                }
     62                $parts = parse_url($url);
     63                if (isset($parts['scheme'])) {
     64                        // set the HTTP_HOST
     65                        $GLOBALS['_SERVER']['HTTP_HOST'] = $parts['host'];
     66
     67                        $req = $parts['path'];
     68                        if (isset($parts['query'])) {
     69                                $req .= '?' . $parts['query'];
     70                                // parse the url query vars into $_GET
     71                                parse_str($parts['query'], $_GET);
     72                        }
     73                } else {
     74                        $req = $url;
     75                }
     76                if ( ! isset( $parts['query'] ) ) {
     77                        $parts['query'] = '';
     78                }
     79
     80                // Scheme
     81                if ( 0 === strpos( $req, '/wp-admin' ) && force_ssl_admin() ) {
     82                        $_SERVER['HTTPS'] = 'on';
     83                } else {
     84                        unset( $_SERVER['HTTPS'] );
     85                }
     86
     87                // Set this for bbp_core_set_uri_globals()
     88                $GLOBALS['_SERVER']['REQUEST_URI'] = $req;
     89                unset($_SERVER['PATH_INFO']);
     90
     91                // setup $current_site and $current_blog globals for multisite based on
     92                // REQUEST_URI; mostly copied from /wp-includes/ms-settings.php
     93                if ( is_multisite() ) {
     94                        $domain = addslashes( $_SERVER['HTTP_HOST'] );
     95                        if ( false !== strpos( $domain, ':' ) ) {
     96                                if ( substr( $domain, -3 ) == ':80' ) {
     97                                        $domain = substr( $domain, 0, -3 );
     98                                        $_SERVER['HTTP_HOST'] = substr( $_SERVER['HTTP_HOST'], 0, -3 );
     99                                } elseif ( substr( $domain, -4 ) == ':443' ) {
     100                                        $domain = substr( $domain, 0, -4 );
     101                                        $_SERVER['HTTP_HOST'] = substr( $_SERVER['HTTP_HOST'], 0, -4 );
     102                                }
     103                        }
     104
     105                        $domain = rtrim( $domain, '.' );
     106                        $cookie_domain = $domain;
     107                        if ( substr( $cookie_domain, 0, 4 ) == 'www.' )
     108                                $cookie_domain = substr( $cookie_domain, 4 );
     109
     110                        $path = preg_replace( '|([a-z0-9-]+.php.*)|', '', $GLOBALS['_SERVER']['REQUEST_URI'] );
     111                        $path = str_replace ( '/wp-admin/', '/', $path );
     112                        $path = preg_replace( '|(/[a-z0-9-]+?/).*|', '$1', $path );
     113
     114                        $GLOBALS['current_site'] = wpmu_current_site();
     115                        if ( ! isset( $GLOBALS['current_site']->blog_id ) )
     116                                $GLOBALS['current_site']->blog_id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE domain = %s AND path = %s", $GLOBALS['current_site']->domain, $GLOBALS['current_site']->path ) );
     117
     118                        // unit tests only support subdirectory install at the moment
     119                        // removed object cache references
     120                        if ( ! is_subdomain_install() ) {
     121                                $blogname = htmlspecialchars( substr( $GLOBALS['_SERVER']['REQUEST_URI'], strlen( $path ) ) );
     122                                if ( false !== strpos( $blogname, '/' ) )
     123                                        $blogname = substr( $blogname, 0, strpos( $blogname, '/' ) );
     124                                if ( false !== strpos( $blogname, '?' ) )
     125                                        $blogname = substr( $blogname, 0, strpos( $blogname, '?' ) );
     126                                $reserved_blognames = array( 'page', 'comments', 'blog', 'wp-admin', 'wp-includes', 'wp-content', 'files', 'feed' );
     127                                if ( $blogname != '' && ! in_array( $blogname, $reserved_blognames ) && ! is_file( $blogname ) )
     128                                        $path .= $blogname . '/';
     129
     130                                $GLOBALS['current_blog'] = get_blog_details( array( 'domain' => $domain, 'path' => $path ), false );
     131
     132                                unset($reserved_blognames);
     133                        }
     134
     135                        $GLOBALS['blog_id'] = $GLOBALS['current_blog']->blog_id;
     136                }
     137
     138                unset($GLOBALS['wp_query'], $GLOBALS['wp_the_query']);
     139                $GLOBALS['wp_the_query'] = new WP_Query();
     140                $GLOBALS['wp_query'] = $GLOBALS['wp_the_query'];
     141                $GLOBALS['wp'] = new WP();
     142
     143                // clean out globals to stop them polluting wp and wp_query
     144                foreach ($GLOBALS['wp']->public_query_vars as $v) {
     145                        unset($GLOBALS[$v]);
     146                }
     147                foreach ($GLOBALS['wp']->private_query_vars as $v) {
     148                        unset($GLOBALS[$v]);
     149                }
     150
     151                $GLOBALS['wp']->main($parts['query']);
     152
     153                // For bbPress, James.
     154                $GLOBALS['bbp']->loggedin_user = NULL;
     155                do_action( 'bbp_init' );
     156        }
     157
     158        protected function checkRequirements() {
     159                if ( WP_TESTS_FORCE_KNOWN_BUGS )
     160                        return;
     161
     162                parent::checkRequirements();
     163
     164                $tickets = PHPUnit_Util_Test::getTickets( get_class( $this ), $this->getName( false ) );
     165                foreach ( $tickets as $ticket ) {
     166                        if ( 'BBP' == substr( $ticket, 0, 2 ) ) {
     167                                $ticket = substr( $ticket, 2 );
     168                                if ( $ticket && is_numeric( $ticket ) )
     169                                        $this->knownBBPBug( $ticket );
     170                        }
     171                }
     172        }
     173
     174        /**
     175         * Skips the current test if there is an open bbPress ticket with id $ticket_id
     176         */
     177        function knownBBPBug( $ticket_id ) {
     178                if ( WP_TESTS_FORCE_KNOWN_BUGS || in_array( $ticket_id, self::$forced_tickets ) )
     179                        return;
     180
     181                if ( ! TracTickets::isTracTicketClosed( 'http://buddypress.trac.wordpress.org', $ticket_id ) )
     182                        $this->markTestSkipped( sprintf( 'BuddyPress Ticket #%d is not fixed', $ticket_id ) );
     183        }
     184
     185        /**
     186         * WP's core tests use wp_set_current_user() to change the current
     187         * user during tests. BBP caches the current user differently, so we
     188         * have to do a bit more work to change it
     189         *
     190         * @global bbPres $bbp
     191         */
     192        function set_current_user( $user_id ) {
     193                global $bbp;
     194                $bbp->loggedin_user->id = $user_id;
     195                $bbp->loggedin_user->fullname       = bbp_core_get_user_displayname( $user_id );
     196                $bbp->loggedin_user->is_super_admin = $bbp->loggedin_user->is_site_admin = is_super_admin( $user_id );
     197                $bbp->loggedin_user->domain         = bbp_core_get_user_domain( $user_id );
     198                $bbp->loggedin_user->userdata       = bbp_core_get_core_userdata( $user_id );
     199
     200                wp_set_current_user( $user_id );
     201        }
     202
     203        /**
     204         * When creating a new user, it's almost always necessary to have the
     205         * last_activity usermeta set right away, so that the user shows up in
     206         * directory queries. This is a shorthand wrapper for the user factory
     207         * create() method.
     208         *
     209         * Also set a display name
     210         */
     211        function create_user( $args = array() ) {
     212                $r = wp_parse_args( $args, array(
     213                        'role' => 'subscriber',
     214                        'last_activity' => bbp_core_current_time() - 60*60*24*365,
     215                ) );
     216
     217                $last_activity = $r['last_activity'];
     218                unset( $r['last_activity'] );
     219
     220                $user_id = $this->factory->user->create( $args );
     221
     222                bbp_update_user_last_activity( $user_id, $last_activity );
     223
     224                if ( bbp_is_active( 'xprofile' ) ) {
     225                        $user = new WP_User( $user_id );
     226                        xprofile_set_field_data( 1, $user_id, $user->display_name );
     227                }
     228
     229                return $user_id;
     230        }
     231
     232        public static function add_user_to_group( $user_id, $group_id, $args = array() ) {
     233                $r = wp_parse_args( $args, array(
     234                        'date_modified' => bbp_core_current_time(),
     235                        'is_confirmed' => 1,
     236                ) );
     237
     238                $new_member                = new BBP_Groups_Member;
     239                $new_member->group_id      = $group_id;
     240                $new_member->user_id       = $user_id;
     241                $new_member->inviter_id    = 0;
     242                $new_member->is_admin      = 0;
     243                $new_member->user_title    = '';
     244                $new_member->date_modified = $r['date_modified'];
     245                $new_member->is_confirmed  = $r['is_confirmed'];
     246
     247                $new_member->save();
     248                return $new_member->id;
     249        }
     250
     251        /**
     252         * We can't use grant_super_admin() because we will need to modify
     253         * the list more than once, and grant_super_admin() can only be run
     254         * once because of its global check
     255         */
     256        public function grant_super_admin( $user_id ) {
     257                global $super_admins;
     258                if ( ! is_multisite() ) {
     259                        return;
     260                }
     261
     262                $user = get_userdata( $user_id );
     263                $super_admins[] = $user->user_login;
     264        }
     265
     266        public function restore_admins() {
     267                // We assume that the global can be wiped out
     268                // @see grant_super_admin()
     269                unset( $GLOBALS['super_admins'] );
     270        }
     271
     272        public function grant_bbp_moderate( $user_id ) {
     273                if ( ! isset( $this->temp_has_bbp_moderate[ $user_id ] ) ) {
     274                        $this->temp_has_bbp_moderate[ $user_id ] = 1;
     275                }
     276                add_filter( 'bbp_current_user_can', array( $this, 'grant_bbp_moderate_cb' ), 10, 2 );
     277        }
     278
     279        public function revoke_bbp_moderate( $user_id ) {
     280                if ( isset( $this->temp_has_bbp_moderate[ $user_id ] ) ) {
     281                        unset( $this->temp_has_bbp_moderate[ $user_id ] );
     282                }
     283                remove_filter( 'bbp_current_user_can', array( $this, 'grant_bbp_moderate_cb' ), 10, 2 );
     284        }
     285
     286        public function grant_bbp_moderate_cb( $retval, $capability ) {
     287                $current_user = bbp_loggedin_user_id();
     288                if ( ! isset( $this->temp_has_bbp_moderate[ $current_user ] ) ) {
     289                        return $retval;
     290                }
     291
     292                if ( 'bbp_moderate' == $capability ) {
     293                        $retval = true;
     294                }
     295
     296                return $retval;
     297        }
     298
     299        /**
     300         * Go to the root blog. This helps reset globals after moving between
     301         * blogs.
     302         */
     303        public function go_to_root() {
     304                $blog_1_url = get_blog_option( 1, 'home' );
     305                $this->go_to( str_replace( $blog_1_url, '', trailingslashit( bbp_get_root_domain() ) ) );
     306        }
     307
     308        /**
     309         * Set up globals necessary to avoid errors when using wp_mail()
     310         */
     311        public function setUp_wp_mail( $args ) {
     312                if ( isset( $_SERVER['SERVER_NAME'] ) ) {
     313                        $this->cached_SERVER_NAME = $_SERVER['SERVER_NAME'];
     314                }
     315
     316                $_SERVER['SERVER_NAME'] = 'example.com';
     317
     318                // passthrough
     319                return $args;
     320        }
     321
     322        /**
     323         * Tear down globals set up in setUp_wp_mail()
     324         */
     325        public function tearDown_wp_mail( $args ) {
     326                if ( ! empty( $this->cached_SERVER_NAME ) ) {
     327                        $_SERVER['SERVER_NAME'] = $this->cached_SERVER_NAME;
     328                        unset( $this->cached_SERVER_NAME );
     329                } else {
     330                        unset( $_SERVER['SERVER_NAME'] );
     331                }
     332
     333                // passthrough
     334                return $args;
     335        }
     336}
  • tests/includes/define-constants.php

     
     1<?php
     2
     3/**
     4 * Define constants needed by test suite.
     5 */
     6
     7define( 'BBP_PLUGIN_DIR', dirname( dirname( dirname( __FILE__ ) ) ) . '/' );
     8
     9if ( ! defined( 'BBP_TESTS_DIR' ) ) {
     10        define( 'BBP_TESTS_DIR', dirname( dirname( __FILE__ ) ) . '/' );
     11}
     12
     13/**
     14 * In the pre-develop.svn WP development environment, an environmental bash
     15 * variable would be set to run PHP Unit tests. However, this has been done
     16 * away with in a post-develop.svn world. We'll still check if this variable
     17 * is set for backwards compat.
     18 */
     19if ( getenv( 'WP_TESTS_DIR' ) ) {
     20        define( 'WP_TESTS_DIR', getenv( 'WP_TESTS_DIR' ) );
     21        define( 'WP_ROOT_DIR', WP_TESTS_DIR );
     22} else {
     23        define( 'WP_ROOT_DIR', dirname( dirname( dirname( dirname( dirname( dirname( __DIR__ ) ) ) ) ) ) );
     24        define( 'WP_TESTS_DIR', WP_ROOT_DIR . '/tests/phpunit' );
     25}
     26
     27// Based on the tests directory, look for a config file
     28if ( file_exists( WP_ROOT_DIR . '/wp-tests-config.php' ) ) {
     29        // Standard develop.svn.wordpress.org setup
     30        define( 'WP_TESTS_CONFIG_PATH', WP_ROOT_DIR . '/wp-tests-config.php' );
     31
     32} else if ( file_exists( WP_TESTS_DIR . '/wp-tests-config.php' ) ) {
     33        // Legacy unit-test.svn.wordpress.org setup
     34        define( 'WP_TESTS_CONFIG_PATH', WP_TESTS_DIR . '/wp-tests-config.php' );
     35
     36} else if ( file_exists( dirname( dirname( WP_TESTS_DIR ) ) . '/wp-tests-config.php' ) ) {
     37        // Environment variable exists and points to tests/phpunit of
     38        // develop.svn.wordpress.org setup
     39        define( 'WP_TESTS_CONFIG_PATH', dirname( dirname( WP_TESTS_DIR ) ) . '/wp-tests-config.php' );
     40
     41} else {
     42        die( "wp-tests-config.php could not be found.\n" );
     43}
  • tests/includes/factory.php

     
     1<?php
     2class BBP_UnitTest_Factory extends WP_UnitTest_Factory {
     3        public $activity = null;
     4
     5        function __construct() {
     6                parent::__construct();
     7
     8                $this->activity = new BBP_UnitTest_Factory_For_Activity( $this );
     9                $this->group = new BBP_UnitTest_Factory_For_Group( $this );
     10                $this->xprofile_group = new BBP_UnitTest_Factory_For_XProfileGroup( $this );
     11                $this->xprofile_field = new BBP_UnitTest_Factory_For_XProfileField( $this );
     12                $this->notification = new BBP_UnitTest_Factory_For_Notification( $this );
     13        }
     14}
     15
     16class BBP_UnitTest_Factory_For_Activity extends WP_UnitTest_Factory_For_Thing {
     17
     18        function __construct( $factory = null ) {
     19                parent::__construct( $factory );
     20
     21                $this->default_generation_definitions = array(
     22                        'action'       => new WP_UnitTest_Generator_Sequence( 'Activity action %s' ),
     23                        'component'    => buddypress()->activity->id,
     24                        'content'      => new WP_UnitTest_Generator_Sequence( 'Activity content %s' ),
     25                        'primary_link' => 'http://example.com',
     26                        'type'         => 'activity_update',
     27                        'recorded_time' => bbp_core_current_time(),
     28                );
     29        }
     30
     31        function create_object( $args ) {
     32                if ( ! isset( $args['user_id'] ) )
     33                        $args['user_id'] = get_current_user_id();
     34
     35                return bbp_activity_add( $args );
     36        }
     37
     38        function update_object( $activity_id, $fields ) {
     39                $activity = new BBP_Activity_Activity( $activity_id );
     40
     41                foreach ( $fields as $field_name => $value ) {
     42                        if ( isset( $activity->$field_name ) )
     43                                $activity->$field_name = $value;
     44                }
     45
     46                $activity->save();
     47                return $activity;
     48        }
     49
     50        function get_object_by_id( $user_id ) {
     51                return new BBP_Activity_Activity( $user_id );
     52        }
     53}
     54
     55class BBP_UnitTest_Factory_For_Group extends WP_UnitTest_Factory_For_Thing {
     56
     57        function __construct( $factory = null ) {
     58                parent::__construct( $factory );
     59
     60                $this->default_generation_definitions = array(
     61                        'name'         => new WP_UnitTest_Generator_Sequence( 'Group %s' ),
     62                        'description'  => new WP_UnitTest_Generator_Sequence( 'Group description %s' ),
     63                        'slug'         => new WP_UnitTest_Generator_Sequence( 'group-slug-%s' ),
     64                        'status'       => 'public',
     65                        'enable_forum' => true,
     66                        'date_created' => bbp_core_current_time(),
     67                );
     68        }
     69
     70        function create_object( $args ) {
     71                if ( ! isset( $args['creator_id'] ) ) {
     72                        $args['creator_id'] = get_current_user_id();
     73                }
     74
     75                $group_id = groups_create_group( $args );
     76
     77                groups_update_groupmeta( $group_id, 'total_member_count', 1 );
     78
     79                $last_activity = isset( $args['last_activity'] ) ? $args['last_activity'] : bbp_core_current_time();
     80                groups_update_groupmeta( $group_id, 'last_activity', $last_activity );
     81
     82                return $group_id;
     83        }
     84
     85        function update_object( $group_id, $fields ) {
     86                $group = new BBP_Groups_Group( $group_id );
     87
     88                foreach ( $fields as $field_name => $value ) {
     89                        if ( isset( $group->field_name ) )
     90                                $group->field_name = $value;
     91                }
     92
     93                $group->save();
     94                return $group;
     95        }
     96
     97        function get_object_by_id( $group_id ) {
     98                return new BBP_Groups_Group( $group_id );
     99        }
     100}
     101
     102class BBP_UnitTest_Factory_For_XProfileGroup extends WP_UnitTest_Factory_For_Thing {
     103
     104        function __construct( $factory = null ) {
     105                parent::__construct( $factory );
     106
     107                $this->default_generation_definitions = array(
     108                        'name'         => new WP_UnitTest_Generator_Sequence( 'XProfile group %s' ),
     109                        'description'  => new WP_UnitTest_Generator_Sequence( 'XProfile group description %s' ),
     110                        'slug'         => new WP_UnitTest_Generator_Sequence( 'xprofile-group-slug-%s' ),
     111                );
     112        }
     113
     114        function create_object( $args ) {
     115                $group_id = xprofile_insert_field_group( $args );
     116                return $this->get_object_by_id( $group_id );
     117        }
     118
     119        function update_object( $group_id, $fields ) {
     120        }
     121
     122        function get_object_by_id( $group_id ) {
     123                return new BBP_XProfile_Group( $group_id );
     124        }
     125}
     126
     127class BBP_UnitTest_Factory_For_XProfileField extends WP_UnitTest_Factory_For_Thing {
     128
     129        function __construct( $factory = null ) {
     130                parent::__construct( $factory );
     131
     132                $this->default_generation_definitions = array(
     133                        'name'         => new WP_UnitTest_Generator_Sequence( 'XProfile field %s' ),
     134                        'description'  => new WP_UnitTest_Generator_Sequence( 'XProfile field description %s' ),
     135                );
     136        }
     137
     138        function create_object( $args ) {
     139                $field_id = xprofile_insert_field( $args );
     140                return $this->get_object_by_id( $field_id );
     141        }
     142
     143        function update_object( $field_id, $fields ) {
     144        }
     145
     146        function get_object_by_id( $field_id ) {
     147                return new BBP_XProfile_Field( $field_id );
     148        }
     149}
     150
     151class BBP_UnitTest_Factory_For_Notification extends WP_UnitTest_Factory_For_Thing {
     152        public function __construct( $factory = null ) {
     153                parent::__construct( $factory );
     154        }
     155
     156        public function create_object( $args ) {
     157                return bbp_notifications_add_notification( $args );
     158        }
     159
     160        public function update_object( $id, $fields ) {}
     161
     162        public function get_object_by_id( $id ) {
     163                return new BBP_Notifications_Notification( $id );
     164        }
     165}
  • tests/includes/install.php

     
     1<?php
     2/**
     3 * Installs bbPress for the purpose of the unit-tests
     4 *
     5 * @todo Reuse the init/load code in init.php
     6 * @todo Support MULTIBLOG
     7 */
     8error_reporting( E_ALL & ~E_DEPRECATED & ~E_STRICT );
     9
     10$config_file_path = $argv[1];
     11$tests_dir_path = $argv[2];
     12$multisite = ! empty( $argv[3] );
     13
     14require_once $config_file_path;
     15require_once $tests_dir_path . '/includes/functions.php';
     16
     17function _load_bbpress() {
     18        require dirname( dirname( dirname( __FILE__ ) ) ) . '/bbpress.php';
     19}
     20tests_add_filter( 'muplugins_loaded', '_load_bbpress' );
     21
     22define( 'BBP_PLUGIN_DIR', dirname( dirname( dirname( __FILE__ ) ) ) . '/' );
     23define( 'BBP_ROOT_BLOG', 1 );
     24
     25// Always load admin bar
     26tests_add_filter( 'show_admin_bar', '__return_true' );
     27
     28$_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';
     29$_SERVER['HTTP_HOST'] = WP_TESTS_DOMAIN;
     30$PHP_SELF = $GLOBALS['PHP_SELF'] = $_SERVER['PHP_SELF'] = '/index.php';
     31
     32require_once ABSPATH . '/wp-settings.php';
     33
     34echo "Installing bbPress...\n";
     35
     36// Make sure that bbPress has been cleaned from all blogs before reinstalling
     37$blogs = is_multisite() ? $wpdb->get_col( "SELECT blog_id FROM {$wpdb->blogs}" ) : array( 1 );
     38foreach ( $blogs as $blog ) {
     39        if ( is_multisite() ) {
     40                switch_to_blog( $blog );
     41        }
     42
     43        $wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE '%bbp%'" );
     44
     45        if ( is_multisite() ) {
     46                restore_current_blog();
     47        }
     48}
     49
     50$wpdb->query( 'SET storage_engine = INNODB' );
     51$wpdb->select( DB_NAME, $wpdb->dbh );
     52
     53// Install BuddyPress
     54// bbp_version_updater();
  • tests/includes/loader.php

     
     1<?php
     2
     3require_once( dirname( __FILE__ ) . '/define-constants.php' );
     4
     5$multisite = (int) ( defined( 'WP_TESTS_MULTISITE') && WP_TESTS_MULTISITE );
     6system( WP_PHP_BINARY . ' ' . escapeshellarg( dirname( __FILE__ ) . '/install.php' ) . ' ' . escapeshellarg( WP_TESTS_CONFIG_PATH ) . ' ' . escapeshellarg( WP_TESTS_DIR ) . ' ' . $multisite );
     7
     8// Bootstrap BBP
     9require dirname( __FILE__ ) . '/../../bbpress.php';
  • tests/includes/testcase.php

     
     1<?php
     2
     3/**
     4 * WP's test suite wipes out BBP's directory page mappings with _delete_all_posts()
     5 * We must reestablish them before our tests can be successfully run
     6 */
     7
     8require_once dirname( __FILE__ ) . '/factory.php';
     9
     10class BBP_UnitTestCase extends WP_UnitTestCase {
     11
     12        protected $temp_has_bbp_moderate = array();
     13        protected $cached_SERVER_NAME = null;
     14
     15        public function setUp() {
     16                parent::setUp();
     17
     18                // Make sure all users are deleted
     19                // There's a bug in the multisite tests that causes the
     20                // transaction rollback to fail for the first user created,
     21                // which busts every other attempt to create users. This is a
     22                // hack workaround
     23                global $wpdb;
     24                $wpdb->query( "TRUNCATE TABLE {$wpdb->users}" );
     25
     26                // Fake WP mail globals, to avoid errors
     27                add_filter( 'wp_mail', array( $this, 'setUp_wp_mail' ) );
     28                add_filter( 'wp_mail_from', array( $this, 'tearDown_wp_mail' ) );
     29
     30                $this->factory = new BBP_UnitTest_Factory;
     31        }
     32
     33        function clean_up_global_scope() {
     34                buddypress()->bbp_nav                = buddypress()->bbp_options_nav = buddypress()->action_variables = buddypress()->canonical_stack = buddypress()->unfiltered_uri = $GLOBALS['bbp_unfiltered_uri'] = array();
     35                buddypress()->current_component     = buddypress()->current_item = buddypress()->current_action = '';
     36                buddypress()->unfiltered_uri_offset = 0;
     37                buddypress()->is_single_item        = false;
     38                buddypress()->current_user          = new stdClass();
     39                buddypress()->displayed_user        = new stdClass();
     40                buddypress()->loggedin_user         = new stdClass();
     41                buddypress()->avatar                = new stdClass();
     42
     43                parent::clean_up_global_scope();
     44        }
     45
     46        function assertPreConditions() {
     47                parent::assertPreConditions();
     48
     49                // Reinit some of the globals that might have been cleared by BBP_UnitTestCase::clean_up_global_scope().
     50                // This is here because it didn't work in clean_up_global_scope(); I don't know why.
     51                do_action( 'bbp_setup_globals' );
     52        }
     53
     54        function go_to( $url ) {
     55                // note: the WP and WP_Query classes like to silently fetch parameters
     56                // from all over the place (globals, GET, etc), which makes it tricky
     57                // to run them more than once without very carefully clearing everything
     58                $_GET = $_POST = array();
     59                foreach (array('query_string', 'id', 'postdata', 'authordata', 'day', 'currentmonth', 'page', 'pages', 'multipage', 'more', 'numpages', 'pagenow') as $v) {
     60                        if ( isset( $GLOBALS[$v] ) ) unset( $GLOBALS[$v] );
     61                }
     62                $parts = parse_url($url);
     63                if (isset($parts['scheme'])) {
     64                        // set the HTTP_HOST
     65                        $GLOBALS['_SERVER']['HTTP_HOST'] = $parts['host'];
     66
     67                        $req = $parts['path'];
     68                        if (isset($parts['query'])) {
     69                                $req .= '?' . $parts['query'];
     70                                // parse the url query vars into $_GET
     71                                parse_str($parts['query'], $_GET);
     72                        }
     73                } else {
     74                        $req = $url;
     75                }
     76                if ( ! isset( $parts['query'] ) ) {
     77                        $parts['query'] = '';
     78                }
     79
     80                // Scheme
     81                if ( 0 === strpos( $req, '/wp-admin' ) && force_ssl_admin() ) {
     82                        $_SERVER['HTTPS'] = 'on';
     83                } else {
     84                        unset( $_SERVER['HTTPS'] );
     85                }
     86
     87                // Set this for bbp_core_set_uri_globals()
     88                $GLOBALS['_SERVER']['REQUEST_URI'] = $req;
     89                unset($_SERVER['PATH_INFO']);
     90
     91                // setup $current_site and $current_blog globals for multisite based on
     92                // REQUEST_URI; mostly copied from /wp-includes/ms-settings.php
     93                if ( is_multisite() ) {
     94                        $domain = addslashes( $_SERVER['HTTP_HOST'] );
     95                        if ( false !== strpos( $domain, ':' ) ) {
     96                                if ( substr( $domain, -3 ) == ':80' ) {
     97                                        $domain = substr( $domain, 0, -3 );
     98                                        $_SERVER['HTTP_HOST'] = substr( $_SERVER['HTTP_HOST'], 0, -3 );
     99                                } elseif ( substr( $domain, -4 ) == ':443' ) {
     100                                        $domain = substr( $domain, 0, -4 );
     101                                        $_SERVER['HTTP_HOST'] = substr( $_SERVER['HTTP_HOST'], 0, -4 );
     102                                }
     103                        }
     104
     105                        $domain = rtrim( $domain, '.' );
     106                        $cookie_domain = $domain;
     107                        if ( substr( $cookie_domain, 0, 4 ) == 'www.' )
     108                                $cookie_domain = substr( $cookie_domain, 4 );
     109
     110                        $path = preg_replace( '|([a-z0-9-]+.php.*)|', '', $GLOBALS['_SERVER']['REQUEST_URI'] );
     111                        $path = str_replace ( '/wp-admin/', '/', $path );
     112                        $path = preg_replace( '|(/[a-z0-9-]+?/).*|', '$1', $path );
     113
     114                        $GLOBALS['current_site'] = wpmu_current_site();
     115                        if ( ! isset( $GLOBALS['current_site']->blog_id ) )
     116                                $GLOBALS['current_site']->blog_id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE domain = %s AND path = %s", $GLOBALS['current_site']->domain, $GLOBALS['current_site']->path ) );
     117
     118                        // unit tests only support subdirectory install at the moment
     119                        // removed object cache references
     120                        if ( ! is_subdomain_install() ) {
     121                                $blogname = htmlspecialchars( substr( $GLOBALS['_SERVER']['REQUEST_URI'], strlen( $path ) ) );
     122                                if ( false !== strpos( $blogname, '/' ) )
     123                                        $blogname = substr( $blogname, 0, strpos( $blogname, '/' ) );
     124                                if ( false !== strpos( $blogname, '?' ) )
     125                                        $blogname = substr( $blogname, 0, strpos( $blogname, '?' ) );
     126                                $reserved_blognames = array( 'page', 'comments', 'blog', 'wp-admin', 'wp-includes', 'wp-content', 'files', 'feed' );
     127                                if ( $blogname != '' && ! in_array( $blogname, $reserved_blognames ) && ! is_file( $blogname ) )
     128                                        $path .= $blogname . '/';
     129
     130                                $GLOBALS['current_blog'] = get_blog_details( array( 'domain' => $domain, 'path' => $path ), false );
     131
     132                                unset($reserved_blognames);
     133                        }
     134
     135                        $GLOBALS['blog_id'] = $GLOBALS['current_blog']->blog_id;
     136                }
     137
     138                unset($GLOBALS['wp_query'], $GLOBALS['wp_the_query']);
     139                $GLOBALS['wp_the_query'] = new WP_Query();
     140                $GLOBALS['wp_query'] = $GLOBALS['wp_the_query'];
     141                $GLOBALS['wp'] = new WP();
     142
     143                // clean out globals to stop them polluting wp and wp_query
     144                foreach ($GLOBALS['wp']->public_query_vars as $v) {
     145                        unset($GLOBALS[$v]);
     146                }
     147                foreach ($GLOBALS['wp']->private_query_vars as $v) {
     148                        unset($GLOBALS[$v]);
     149                }
     150
     151                $GLOBALS['wp']->main($parts['query']);
     152
     153                // For bbPress, James.
     154                $GLOBALS['bbp']->loggedin_user = NULL;
     155                do_action( 'bbp_init' );
     156        }
     157
     158        protected function checkRequirements() {
     159                if ( WP_TESTS_FORCE_KNOWN_BUGS )
     160                        return;
     161
     162                parent::checkRequirements();
     163
     164                $tickets = PHPUnit_Util_Test::getTickets( get_class( $this ), $this->getName( false ) );
     165                foreach ( $tickets as $ticket ) {
     166                        if ( 'BBP' == substr( $ticket, 0, 2 ) ) {
     167                                $ticket = substr( $ticket, 2 );
     168                                if ( $ticket && is_numeric( $ticket ) )
     169                                        $this->knownBBPBug( $ticket );
     170                        }
     171                }
     172        }
     173
     174        /**
     175         * Skips the current test if there is an open bbPress ticket with id $ticket_id
     176         */
     177        function knownBBPBug( $ticket_id ) {
     178                if ( WP_TESTS_FORCE_KNOWN_BUGS || in_array( $ticket_id, self::$forced_tickets ) )
     179                        return;
     180
     181                if ( ! TracTickets::isTracTicketClosed( 'http://buddypress.trac.wordpress.org', $ticket_id ) )
     182                        $this->markTestSkipped( sprintf( 'BuddyPress Ticket #%d is not fixed', $ticket_id ) );
     183        }
     184
     185        /**
     186         * WP's core tests use wp_set_current_user() to change the current
     187         * user during tests. BBP caches the current user differently, so we
     188         * have to do a bit more work to change it
     189         *
     190         * @global bbPres $bbp
     191         */
     192        function set_current_user( $user_id ) {
     193                global $bbp;
     194                $bbp->loggedin_user->id = $user_id;
     195                $bbp->loggedin_user->fullname       = bbp_core_get_user_displayname( $user_id );
     196                $bbp->loggedin_user->is_super_admin = $bbp->loggedin_user->is_site_admin = is_super_admin( $user_id );
     197                $bbp->loggedin_user->domain         = bbp_core_get_user_domain( $user_id );
     198                $bbp->loggedin_user->userdata       = bbp_core_get_core_userdata( $user_id );
     199
     200                wp_set_current_user( $user_id );
     201        }
     202
     203        /**
     204         * When creating a new user, it's almost always necessary to have the
     205         * last_activity usermeta set right away, so that the user shows up in
     206         * directory queries. This is a shorthand wrapper for the user factory
     207         * create() method.
     208         *
     209         * Also set a display name
     210         */
     211        function create_user( $args = array() ) {
     212                $r = wp_parse_args( $args, array(
     213                        'role' => 'subscriber',
     214                        'last_activity' => bbp_core_current_time() - 60*60*24*365,
     215                ) );
     216
     217                $last_activity = $r['last_activity'];
     218                unset( $r['last_activity'] );
     219
     220                $user_id = $this->factory->user->create( $args );
     221
     222                bbp_update_user_last_activity( $user_id, $last_activity );
     223
     224                if ( bbp_is_active( 'xprofile' ) ) {
     225                        $user = new WP_User( $user_id );
     226                        xprofile_set_field_data( 1, $user_id, $user->display_name );
     227                }
     228
     229                return $user_id;
     230        }
     231
     232        public static function add_user_to_group( $user_id, $group_id, $args = array() ) {
     233                $r = wp_parse_args( $args, array(
     234                        'date_modified' => bbp_core_current_time(),
     235                        'is_confirmed' => 1,
     236                ) );
     237
     238                $new_member                = new BBP_Groups_Member;
     239                $new_member->group_id      = $group_id;
     240                $new_member->user_id       = $user_id;
     241                $new_member->inviter_id    = 0;
     242                $new_member->is_admin      = 0;
     243                $new_member->user_title    = '';
     244                $new_member->date_modified = $r['date_modified'];
     245                $new_member->is_confirmed  = $r['is_confirmed'];
     246
     247                $new_member->save();
     248                return $new_member->id;
     249        }
     250
     251        /**
     252         * We can't use grant_super_admin() because we will need to modify
     253         * the list more than once, and grant_super_admin() can only be run
     254         * once because of its global check
     255         */
     256        public function grant_super_admin( $user_id ) {
     257                global $super_admins;
     258                if ( ! is_multisite() ) {
     259                        return;
     260                }
     261
     262                $user = get_userdata( $user_id );
     263                $super_admins[] = $user->user_login;
     264        }
     265
     266        public function restore_admins() {
     267                // We assume that the global can be wiped out
     268                // @see grant_super_admin()
     269                unset( $GLOBALS['super_admins'] );
     270        }
     271
     272        public function grant_bbp_moderate( $user_id ) {
     273                if ( ! isset( $this->temp_has_bbp_moderate[ $user_id ] ) ) {
     274                        $this->temp_has_bbp_moderate[ $user_id ] = 1;
     275                }
     276                add_filter( 'bbp_current_user_can', array( $this, 'grant_bbp_moderate_cb' ), 10, 2 );
     277        }
     278
     279        public function revoke_bbp_moderate( $user_id ) {
     280                if ( isset( $this->temp_has_bbp_moderate[ $user_id ] ) ) {
     281                        unset( $this->temp_has_bbp_moderate[ $user_id ] );
     282                }
     283                remove_filter( 'bbp_current_user_can', array( $this, 'grant_bbp_moderate_cb' ), 10, 2 );
     284        }
     285
     286        public function grant_bbp_moderate_cb( $retval, $capability ) {
     287                $current_user = bbp_loggedin_user_id();
     288                if ( ! isset( $this->temp_has_bbp_moderate[ $current_user ] ) ) {
     289                        return $retval;
     290                }
     291
     292                if ( 'bbp_moderate' == $capability ) {
     293                        $retval = true;
     294                }
     295
     296                return $retval;
     297        }
     298
     299        /**
     300         * Go to the root blog. This helps reset globals after moving between
     301         * blogs.
     302         */
     303        public function go_to_root() {
     304                $blog_1_url = get_blog_option( 1, 'home' );
     305                $this->go_to( str_replace( $blog_1_url, '', trailingslashit( bbp_get_root_domain() ) ) );
     306        }
     307
     308        /**
     309         * Set up globals necessary to avoid errors when using wp_mail()
     310         */
     311        public function setUp_wp_mail( $args ) {
     312                if ( isset( $_SERVER['SERVER_NAME'] ) ) {
     313                        $this->cached_SERVER_NAME = $_SERVER['SERVER_NAME'];
     314                }
     315
     316                $_SERVER['SERVER_NAME'] = 'example.com';
     317
     318                // passthrough
     319                return $args;
     320        }
     321
     322        /**
     323         * Tear down globals set up in setUp_wp_mail()
     324         */
     325        public function tearDown_wp_mail( $args ) {
     326                if ( ! empty( $this->cached_SERVER_NAME ) ) {
     327                        $_SERVER['SERVER_NAME'] = $this->cached_SERVER_NAME;
     328                        unset( $this->cached_SERVER_NAME );
     329                } else {
     330                        unset( $_SERVER['SERVER_NAME'] );
     331                }
     332
     333                // passthrough
     334                return $args;
     335        }
     336}
  • tests/multisite.xml

     
     1<phpunit
     2        bootstrap="bootstrap.php"
     3        backupGlobals="false"
     4        colors="true"
     5        convertErrorsToExceptions="true"
     6        convertNoticesToExceptions="true"
     7        convertWarningsToExceptions="true"
     8        >
     9        <php>
     10                <const name="WP_TESTS_MULTISITE" value="1" />
     11        </php>
     12        <testsuites>
     13                <testsuite>
     14                        <directory suffix=".php">./testcases/</directory>
     15                </testsuite>
     16        </testsuites>
     17</phpunit>
  • tests/phpunit.xml

     
     1<phpunit
     2        bootstrap="bootstrap.php"
     3        backupGlobals="false"
     4        colors="true"
     5        convertErrorsToExceptions="true"
     6        convertNoticesToExceptions="true"
     7        convertWarningsToExceptions="true"
     8        >
     9        <testsuites>
     10                <testsuite>
     11                        <directory suffix=".php">./testcases/</directory>
     12                </testsuite>
     13        </testsuites>
     14</phpunit>