[ Index ]

PHP Cross Reference of BBPress

title

Body

[close]

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

   1  <?php
   2  
   3  class BBP_UnitTestCase extends WP_UnitTestCase {
   4  
   5      protected static $cached_SERVER_NAME = null;
   6  
   7      /**
   8       * Fake WP mail globals, to avoid errors
   9       */
  10  	public static function setUpBeforeClass() {
  11          add_filter( 'wp_mail',      array( 'BBP_UnitTestCase', 'setUp_wp_mail'    ) );
  12          add_filter( 'wp_mail_from', array( 'BBP_UnitTestCase', 'tearDown_wp_mail' ) );
  13      }
  14  
  15  	public function setUp() {
  16          parent::setUp();
  17  
  18          $this->factory = new BBP_UnitTest_Factory;
  19  
  20          if ( class_exists( 'BP_UnitTest_Factory' ) ) {
  21              $this->bp_factory = new BP_UnitTest_Factory();
  22          }
  23  
  24          global $wpdb;
  25  
  26          // Our default is ugly permalinks, so reset when needed.
  27          global $wp_rewrite;
  28          if ( $wp_rewrite->permalink_structure ) {
  29              $this->set_permalink_structure();
  30          }
  31      }
  32  
  33  	public function tearDown() {
  34          global $wpdb;
  35  
  36          parent::tearDown();
  37  
  38          if ( is_multisite() ) {
  39              foreach ( $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs WHERE blog_id != 1" ) as $blog_id ) {
  40                  wpmu_delete_blog( $blog_id, true );
  41              }
  42          }
  43  
  44          foreach ( $wpdb->get_col( "SELECT ID FROM $wpdb->users WHERE ID != 1" ) as $user_id ) {
  45              if ( is_multisite() ) {
  46                  wpmu_delete_user( $user_id );
  47              } else {
  48                  wp_delete_user( $user_id );
  49              }
  50          }
  51  
  52          $this->commit_transaction();
  53      }
  54  
  55  	function clean_up_global_scope() {
  56          parent::clean_up_global_scope();
  57      }
  58  
  59  	function assertPreConditions() {
  60          parent::assertPreConditions();
  61      }
  62  
  63  	function go_to( $url ) {
  64  
  65          parent::go_to( $url );
  66  
  67          do_action( 'bbp_init' );
  68      }
  69  
  70      /**
  71       * WP's core tests use wp_set_current_user() to change the current
  72       * user during tests. BP caches the current user differently, so we
  73       * have to do a bit more work to change it
  74       */
  75  	public static function set_current_user( $user_id ) {
  76          wp_set_current_user( $user_id );
  77      }
  78  
  79      /**
  80       * We can't use grant_super_admin() because we will need to modify
  81       * the list more than once, and grant_super_admin() can only be run
  82       * once because of its global check
  83       */
  84  	public function grant_super_admin( $user_id ) {
  85          global $super_admins;
  86          if ( ! is_multisite() ) {
  87              return;
  88          }
  89  
  90          $user = get_userdata( $user_id );
  91          $super_admins[] = $user->user_login;
  92      }
  93  
  94      /**
  95       * We assume that the global can be wiped out
  96       *
  97       * @see grant_super_admin()
  98       */
  99  	public function restore_admins() {
 100          unset( $GLOBALS['super_admins'] );
 101      }
 102  
 103      /**
 104       * Set up globals necessary to avoid errors when using wp_mail()
 105       */
 106  	public static function setUp_wp_mail( $args ) {
 107          if ( isset( $_SERVER['SERVER_NAME'] ) ) {
 108              self::$cached_SERVER_NAME = $_SERVER['SERVER_NAME'];
 109          }
 110  
 111          $_SERVER['SERVER_NAME'] = 'example.com';
 112  
 113          // passthrough
 114          return $args;
 115      }
 116  
 117      /**
 118       * Tear down globals set up in setUp_wp_mail()
 119       */
 120  	public static function tearDown_wp_mail( $args ) {
 121          if ( ! empty( self::$cached_SERVER_NAME ) ) {
 122              $_SERVER['SERVER_NAME'] = self::$cached_SERVER_NAME;
 123              self::$cached_SERVER_NAME = '';
 124          } else {
 125              unset( $_SERVER['SERVER_NAME'] );
 126          }
 127  
 128          // passthrough
 129          return $args;
 130      }
 131  
 132      /**
 133       * Commit a MySQL transaction.
 134       */
 135  	public static function commit_transaction() {
 136          global $wpdb;
 137          $wpdb->query( 'COMMIT;' );
 138      }
 139  
 140      /**
 141       * Utility method that resets permalinks and flushes rewrites.
 142       *
 143       * @since 2.6.0 bbPress (r5947)
 144       *
 145       * @global WP_Rewrite $wp_rewrite
 146       *
 147       * @param string $structure Optional. Permalink structure to set. Default empty.
 148       */
 149  	public function set_permalink_structure( $structure = '' ) {
 150  
 151          // Use WP 4.4+'s version if it exists.
 152          if ( method_exists( 'parent', 'set_permalink_structure' ) ) {
 153              parent::set_permalink_structure( $structure );
 154          } else {
 155              global $wp_rewrite;
 156  
 157              $wp_rewrite->init();
 158              $wp_rewrite->set_permalink_structure( $structure );
 159              $wp_rewrite->flush_rules();
 160          }
 161      }
 162  }


Generated: Tue Mar 19 01:01:02 2024 Cross-referenced by PHPXref 0.7.1