[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/tests/phpunit/testcases/core/ -> avatars.php (source)

   1  <?php
   2  /**
   3   * @group core
   4   * @group avatars
   5   */
   6  class BP_Tests_Avatars extends BP_UnitTestCase {
   7      private $params = array();
   8  
   9  	private function clean_existing_avatars( $type = 'user' ) {
  10          if ( 'user' === $type ) {
  11              $avatar_dir = 'avatars';
  12          } elseif ( 'group' === $object ) {
  13              $avatar_dir = 'group-avatars';
  14          }
  15  
  16          $this->rrmdir( bp_core_avatar_upload_path() . '/' . $avatar_dir );
  17      }
  18  
  19      /**
  20       * @ticket BP4948
  21       */
  22  	function test_avatars_on_non_root_blog() {
  23          // Do not pass 'Go', do not collect $200
  24          if ( ! is_multisite() ) {
  25              $this->markTestSkipped();
  26          }
  27  
  28          if ( function_exists( 'wp_initialize_site' ) ) {
  29              $this->setExpectedDeprecated( 'wpmu_new_blog' );
  30          }
  31  
  32          $u = self::factory()->user->create();
  33  
  34          // get BP root blog's upload directory data
  35          $upload_dir = wp_upload_dir();
  36  
  37          // create new subsite
  38          $blog_id = self::factory()->blog->create( array(
  39              'user_id' => $u,
  40              'title'   => 'Test Title',
  41              'path'    => '/path' . rand() . time() . '/',
  42          ) );
  43  
  44          // emulate a page load on the new sub-site
  45          $this->go_to( get_blog_option( $blog_id, 'siteurl' ) );
  46  
  47          // test to see if the upload dir is correct
  48          $this->assertEquals( $upload_dir['baseurl'], bp_core_avatar_url() );
  49  
  50          // reset globals
  51          $this->go_to( '/' );
  52      }
  53  
  54      /**
  55       * @group bp_get_user_has_avatar
  56       */
  57      public function test_bp_get_user_has_avatar_no_avatar_uploaded() {
  58          $this->clean_existing_avatars();
  59  
  60          $u = self::factory()->user->create();
  61          $this->assertFalse( bp_get_user_has_avatar( $u ) );
  62      }
  63  
  64      /**
  65       * @group bp_get_user_has_avatar
  66       */
  67      public function test_bp_get_user_has_avatar_has_avatar_uploaded() {
  68          $u = self::factory()->user->create();
  69  
  70          // Fake it
  71          add_filter( 'bp_core_fetch_avatar_url', array( $this, 'avatar_cb' ) );
  72  
  73          $this->assertTrue( bp_get_user_has_avatar( $u ) );
  74  
  75          remove_filter( 'bp_core_fetch_avatar_url', array( $this, 'avatar_cb' ) );
  76      }
  77  
  78  	public function avatar_cb() {
  79          return 'foo';
  80      }
  81  
  82      /**
  83       * @group bp_core_fetch_avatar
  84       */
  85      public function test_bp_core_fetch_avatar_parameter_conservation() {
  86          // First, run the check with custom parameters, specifying no gravatar.
  87          $this->params = array(
  88              'item_id'       => 1406,
  89              'object'        => 'custom_object',
  90              'type'          => 'full',
  91              'avatar_dir'    => 'custom-dir',
  92              'width'         => 48,
  93              'height'        => 54,
  94              'class'         => 'custom-class',
  95              'css_id'        => 'custom-css-id',
  96              'alt'           => 'custom alt',
  97              'email'         => 'avatar@avatar.org',
  98              'no_grav'       => true,
  99              'html'          => true,
 100              'title'         => 'custom-title',
 101              'extra_attr'    => 'data-testing="buddypress"',
 102              'scheme'        => 'http',
 103              'rating'        => get_option( 'avatar_rating' ),
 104              'force_default' => false,
 105          );
 106  
 107          // Check to make sure the custom parameters survived the function all the way up to output
 108          add_filter( 'bp_core_fetch_avatar', array( $this, 'bp_core_fetch_avatar_filter_check' ), 12, 2 );
 109          $avatar = bp_core_fetch_avatar( $this->params );
 110  
 111          // Re-run check, allowing gravatars.
 112          $this->params['no_grav'] = false;
 113          $avatar = bp_core_fetch_avatar( $this->params );
 114  
 115          remove_filter( 'bp_core_fetch_avatar', array( $this, 'bp_core_fetch_avatar_filter_check' ), 12 );
 116  
 117          unset( $this->params );
 118      }
 119  
 120  	public function bp_core_fetch_avatar_filter_check( $html, $params ) {
 121          // Check that the passed parameters match the original custom parameters.
 122          $this->assertEmpty( array_merge( array_diff( $params, $this->params ), array_diff( $this->params, $params ) ) );
 123  
 124          // Check the returned html to see that it matches an expected value.
 125          // Get the correct default avatar, based on whether gravatars are allowed.
 126          if ( $params['no_grav'] ) {
 127              $avatar_url = bp_core_avatar_default( 'local', $params );
 128          } else {
 129              // This test has the slight odor of hokum since it recreates so much code that could be changed at any time.
 130              $bp = buddypress();
 131              $host = '//www.gravatar.com/avatar/';
 132  
 133              // Set expected gravatar type
 134              if ( empty( $bp->grav_default->{$this->params['object']} ) ) {
 135                  $default_grav = 'wavatar';
 136              } elseif ( 'mystery' == $bp->grav_default->{$this->params['object']} ) {
 137                  $default_grav = apply_filters( 'bp_core_mysteryman_src', 'mm', $this->params['width'] );
 138              } else {
 139                  $default_grav = $bp->grav_default->{$this->params['object']};
 140              }
 141  
 142              $avatar_url = $host . md5( strtolower( $this->params['email'] ) );
 143  
 144              // Main Gravatar URL args.
 145              $url_args = array(
 146                  's' => $this->params['width']
 147              );
 148  
 149              // Force default.
 150              if ( ! empty( $this->params['force_default'] ) ) {
 151                  $url_args['f'] = 'y';
 152              }
 153  
 154              // Gravatar rating; http://bit.ly/89QxZA
 155              $rating = strtolower( get_option( 'avatar_rating' ) );
 156              if ( ! empty( $rating ) ) {
 157                  $url_args['r'] = $rating;
 158              }
 159  
 160              // Default avatar.
 161              if ( 'gravatar_default' !== $default_grav ) {
 162                  $url_args['d'] = $default_grav;
 163              }
 164  
 165              // Set up the Gravatar URL.
 166              $avatar_url = esc_url( add_query_arg(
 167                  rawurlencode_deep( array_filter( $url_args ) ),
 168                  $avatar_url
 169              ) );
 170  
 171          }
 172  
 173          $expected_html = '<img src="' . $avatar_url . '" id="' . $this->params['css_id'] . '" class="' . $this->params['class'] . ' ' . $this->params['object'] . '-' . $this->params['item_id'] . '-avatar avatar-' . $this->params['width'] . ' photo" width="' . $this->params['width'] . '" height="' . $this->params['height'] . '" alt="' . $this->params['alt'] . '" title="' . $this->params['title'] . '" ' . $this->params['extra_attr'] . ' />';
 174  
 175          $this->assertEquals( $html, $expected_html );
 176      }
 177  
 178      /**
 179       * @group bp_core_fetch_avatar
 180       */
 181  	public function test_bp_core_fetch_avatar_class_attribute() {
 182          $u = self::factory()->user->create();
 183  
 184          $hw = 100;
 185          $args = array(
 186              'item_id'    => $u,
 187              'object'     => 'user',
 188              'type'       => 'full',
 189              'width'      => $hw,
 190              'height'     => $hw,
 191              'class'      => '',
 192              'no_grav'    => true,
 193              'html'       => true,
 194          );
 195  
 196          // Class attribute is empty
 197          $avatar = bp_core_fetch_avatar( $args );
 198          $expected = array( 'avatar', 'user-' . $u . '-avatar', 'avatar-' . $hw );
 199          preg_match( '/class=["\']?([^"\']*)["\' ]/is', $avatar, $matches );
 200          $classes = explode( ' ', $matches[1] );
 201          $this->assertSame( $expected, array_intersect_key( $expected, $classes ) );
 202  
 203          // Class attribute is a String
 204          $args['class'] = 'custom-class class-custom';
 205          $avatar = bp_core_fetch_avatar( $args );
 206          $expected = array_merge( explode( ' ', $args['class'] ), array( 'user-' . $u . '-avatar', 'avatar-' . $hw ) );
 207          preg_match( '/class=["\']?([^"\']*)["\' ]/is', $avatar, $matches );
 208          $classes = explode( ' ', $matches[1] );
 209          $this->assertSame( $expected, array_intersect_key( $expected, $classes ) );
 210  
 211          // Class attribute is an Array
 212          $args['class'] = array( 'custom-class', 'class-custom' );
 213          $avatar = bp_core_fetch_avatar( $args );
 214          $expected = array_merge( $args['class'], array( 'user-' . $u . '-avatar', 'avatar-' . $hw ) );
 215          preg_match( '/class=["\']?([^"\']*)["\' ]/is', $avatar, $matches );
 216          $classes = explode( ' ', $matches[1] );
 217          $this->assertSame( $expected, array_intersect_key( $expected, $classes ) );
 218      }
 219  
 220      /**
 221       * @group bp_core_check_avatar_type
 222       */
 223  	public function test_bp_core_check_avatar_type() {
 224          $plugin_dir = trailingslashit( buddypress()->plugin_dir );
 225  
 226          $file = array(
 227              'file' => array(
 228                  'name' => 'humans.txt',
 229                  'type' => 'text/plain',
 230                  'tmp_name' => $plugin_dir . 'humans.txt',
 231              )
 232          );
 233  
 234          $this->assertFalse( bp_core_check_avatar_type( $file ) );
 235  
 236          $file = array(
 237              'file' => array(
 238                  'name' => 'mystery-man.jpg',
 239                  'type' => 'image/jpeg',
 240                  'tmp_name' => $plugin_dir . 'bp-core/images/mystery-man.jpg',
 241              )
 242          );
 243  
 244          $this->assertTrue( bp_core_check_avatar_type( $file ) );
 245  
 246          $file = array(
 247              'file' => array(
 248                  'name' => 'mystery-man.jpg',
 249                  'type' => 'application/octet-stream',
 250                  'tmp_name' => $plugin_dir . 'bp-core/images/mystery-man.jpg',
 251              )
 252          );
 253  
 254          $this->assertTrue( bp_core_check_avatar_type( $file ), 'flash is using application/octet-stream for image uploads' );
 255      }
 256  
 257      /**
 258       * @group bp_core_check_avatar_type
 259       * @group bp_core_get_allowed_avatar_types
 260       */
 261  	public function test_bp_core_get_allowed_avatar_types_filter() {
 262          add_filter( 'bp_core_get_allowed_avatar_types', array( $this, 'avatar_types_filter_add_type' ) );
 263  
 264          $this->assertEquals( array( 'jpeg', 'gif', 'png' ), bp_core_get_allowed_avatar_types() );
 265  
 266          remove_filter( 'bp_core_get_allowed_avatar_types', array( $this, 'avatar_types_filter_add_type' ) );
 267  
 268          add_filter( 'bp_core_get_allowed_avatar_types', array( $this, 'avatar_types_filter_remove_type' ) );
 269  
 270          $this->assertEquals( array( 'gif', 'png' ), bp_core_get_allowed_avatar_types() );
 271  
 272          remove_filter( 'bp_core_get_allowed_avatar_types', array( $this, 'avatar_types_filter_remove_type' ) );
 273  
 274          add_filter( 'bp_core_get_allowed_avatar_types', '__return_empty_array' );
 275  
 276          $this->assertEquals( array( 'jpeg', 'gif', 'png' ), bp_core_get_allowed_avatar_types() );
 277  
 278          remove_filter( 'bp_core_get_allowed_avatar_types', '__return_empty_array' );
 279      }
 280  
 281      /**
 282       * @group bp_core_check_avatar_type
 283       * @group bp_core_get_allowed_avatar_mimes
 284       */
 285  	public function test_bp_core_get_allowed_avatar_mimes() {
 286          $mimes = bp_core_get_allowed_avatar_mimes();
 287  
 288          $this->assertEqualSets( array( 'jpeg', 'gif', 'png', 'jpg' ), array_keys( $mimes ) );
 289          $this->assertEqualSets( array( 'image/jpeg', 'image/gif', 'image/png', 'image/jpeg' ), array_values( $mimes ) );
 290  
 291          add_filter( 'bp_core_get_allowed_avatar_types', array( $this, 'avatar_types_filter_add_type' ) );
 292  
 293          $this->assertEqualSets( array( 'image/jpeg', 'image/gif', 'image/png', 'image/jpeg' ), array_values( bp_core_get_allowed_avatar_mimes() ) );
 294  
 295          remove_filter( 'bp_core_get_allowed_avatar_types', array( $this, 'avatar_types_filter_add_type' ) );
 296  
 297          add_filter( 'bp_core_get_allowed_avatar_types', array( $this, 'avatar_types_filter_remove_type' ) );
 298  
 299          $this->assertEqualSets( array( 'image/gif', 'image/png' ), array_values( bp_core_get_allowed_avatar_mimes() ) );
 300  
 301          remove_filter( 'bp_core_get_allowed_avatar_types', array( $this, 'avatar_types_filter_remove_type' ) );
 302  
 303          add_filter( 'bp_core_get_allowed_avatar_types', '__return_empty_array' );
 304  
 305          $this->assertEqualSets( array( 'image/jpeg', 'image/gif', 'image/png', 'image/jpeg' ), array_values( bp_core_get_allowed_avatar_mimes() ) );
 306  
 307          remove_filter( 'bp_core_get_allowed_avatar_types', '__return_empty_array' );
 308      }
 309  
 310  	public function avatar_types_filter_add_type( $types ) {
 311          $types[] = 'bmp';
 312  
 313          return $types;
 314      }
 315  
 316  	public function avatar_types_filter_remove_type( $types ) {
 317          $jpeg = array_shift( $types );
 318  
 319          return $types;
 320      }
 321  
 322      /**
 323       * @group BP7056
 324       */
 325      public function test_no_grav_default_should_respect_thumb_type() {
 326          $found = bp_core_fetch_avatar( array(
 327              'item_id' => 12345,
 328              'object' => 'user',
 329              'type' => 'thumb',
 330              'no_grav' => true,
 331              'html' => false,
 332          ) );
 333  
 334          $this->assertContains( 'mystery-man-50.jpg', $found );
 335      }
 336  
 337      /**
 338       * @group BP7056
 339       */
 340      public function test_no_grav_default_should_return_thumb_avatar_for_small_enough_width() {
 341          $found = bp_core_fetch_avatar( array(
 342              'item_id' => 12345,
 343              'object' => 'user',
 344              'type' => 'full',
 345              'width' => '50',
 346              'no_grav' => true,
 347              'html' => false,
 348          ) );
 349  
 350          $this->assertContains( 'mystery-man-50.jpg', $found );
 351      }
 352  
 353      /**
 354       * @group BP7056
 355       */
 356      public function test_no_grav_default_should_return_full_avatar_for_thumb_when_thumb_width_is_too_wide() {
 357          add_filter( 'bp_core_avatar_thumb_width', array( $this, 'filter_thumb_width' ) );
 358          $found = bp_core_fetch_avatar( array(
 359              'item_id' => 12345,
 360              'object' => 'user',
 361              'type' => 'thumb',
 362              'no_grav' => true,
 363              'html' => false,
 364          ) );
 365          remove_filter( 'bp_core_avatar_thumb_width', array( $this, 'filter_thumb_width' ) );
 366  
 367          $this->assertContains( 'mystery-man.jpg', $found );
 368      }
 369  
 370  	public function filter_thumb_width() {
 371          return 51;
 372      }
 373  }


Generated: Tue Nov 19 01:01:36 2019 Cross-referenced by PHPXref 0.7.1