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