[ Index ] |
PHP Cross Reference of BuddyPress |
[Summary view] [Print] [Text view]
1 <?php 2 3 include_once BP_TESTS_DIR . 'assets/attachment-extensions.php'; 4 5 /** 6 * @group bp_attachments 7 * @group BP_Attachment 8 */ 9 class BP_Tests_BP_Attachment_TestCases extends BP_UnitTestCase { 10 private $upload_results; 11 private $image_file; 12 13 public function setUp() { 14 parent::setUp(); 15 add_filter( 'bp_attachment_upload_overrides', array( $this, 'filter_overrides' ), 10, 1 ); 16 add_filter( 'upload_dir', array( $this, 'filter_upload_dir' ), 20, 1 ); 17 add_filter( 'bp_attachments_cover_image_upload_dir', array( $this, 'filter_cover_image_dir' ), 10, 2 ); 18 $this->upload_results = array(); 19 $this->image_file = trailingslashit( buddypress()->plugin_dir ) . 'bp-core/images/mystery-man.jpg'; 20 $this->original_upload_dir = array(); 21 } 22 23 public function tearDown() { 24 parent::tearDown(); 25 remove_filter( 'bp_attachment_upload_overrides', array( $this, 'filter_overrides' ), 10 ); 26 remove_filter( 'upload_dir', array( $this, 'filter_upload_dir' ), 20 ); 27 add_filter( 'bp_attachments_cover_image_upload_dir', array( $this, 'filter_cover_image_dir' ), 10, 2 ); 28 $this->upload_results = array(); 29 $this->image_file = ''; 30 $this->original_upload_dir = array(); 31 } 32 33 public function filter_overrides( $overrides ) { 34 $overrides['upload_error_handler'] = array( $this, 'upload_error_handler' ); 35 36 // Don't test upload for WordPress < 4.0 37 $overrides['test_upload'] = false; 38 return $overrides; 39 } 40 41 public function filter_upload_dir( $upload_dir ) { 42 $upload_dir['error'] = 'fake_upload_success'; 43 44 $this->upload_results = array( 45 'new_file' => $upload_dir['path'] . '/mystery-man.jpg', 46 'url' => $upload_dir['url'] . '/mystery-man.jpg', 47 ); 48 49 return $upload_dir; 50 } 51 52 public function filter_cover_image_dir( $cover_dir, $upload_dir ) { 53 $this->original_upload_dir = $upload_dir; 54 55 return $cover_dir; 56 } 57 58 /** 59 * To avoid copying files in tests, we're faking a succesfull uploads 60 * as soon as all the test_form have been executed in _wp_handle_upload 61 */ 62 public function upload_error_handler( $file, $message ) { 63 if ( 'fake_upload_success' !== $message ) { 64 return array( 'error' => $message ); 65 } else { 66 return array( 67 'file' => $this->upload_results['new_file'], 68 'url' => $this->upload_results['url'], 69 'type' => 'image/jpeg', 70 ); 71 } 72 } 73 74 private function clean_files( $basedir = 'attachment_base_dir' ) { 75 $upload_dir = bp_upload_dir(); 76 77 $this->rrmdir( $upload_dir['basedir'] . '/' . $basedir ); 78 } 79 80 private function clean_avatars( $type = 'user' ) { 81 if ( 'user' === $type ) { 82 $avatar_dir = 'avatars'; 83 } elseif ( 'group' === $type ) { 84 $avatar_dir = 'group-avatars'; 85 } 86 87 $this->rrmdir( bp_core_avatar_upload_path() . '/' . $avatar_dir ); 88 } 89 90 public function max_filesize() { 91 return 1000; 92 } 93 94 public function test_bp_attachment_construct_missing_required_parameter() { 95 $reset_files = $_FILES; 96 $reset_post = $_POST; 97 98 $_FILES['file'] = array( 99 'name' => 'mystery-man.jpg', 100 'type' => 'image/jpeg', 101 'error' => 0, 102 'size' => 1000 103 ); 104 105 $attachment_class = new BPTest_Attachment_Extension(); 106 $upload = $attachment_class->upload( $_FILES ); 107 108 $this->assertTrue( empty( $upload ) ); 109 110 $_FILES = $reset_files; 111 $_POST = $reset_post; 112 } 113 114 public function test_bp_attachment_set_upload_dir() { 115 $upload_dir = bp_upload_dir(); 116 117 $attachment_class = new BPTest_Attachment_Extension( array( 118 'action' => 'attachment_action', 119 'file_input' => 'attachment_file_input' 120 ) ); 121 122 $this->assertSame( $attachment_class->upload_dir, bp_upload_dir() ); 123 124 $attachment_class = new BPTest_Attachment_Extension( array( 125 'action' => 'attachment_action', 126 'file_input' => 'attachment_file_input', 127 'base_dir' => 'attachment_base_dir', 128 ) ); 129 130 $this->assertTrue( file_exists( $upload_dir['basedir'] . '/attachment_base_dir' ) ); 131 132 // clean up 133 $this->clean_files(); 134 } 135 136 /** 137 * @group upload 138 */ 139 public function test_bp_attachment_upload() { 140 $reset_files = $_FILES; 141 $reset_post = $_POST; 142 143 $attachment_class = new BPTest_Attachment_Extension( array( 144 'action' => 'attachment_action', 145 'file_input' => 'attachment_file_input', 146 'base_dir' => 'attachment_base_dir', 147 'original_max_filesize' => 1000, 148 ) ); 149 150 $_POST['action'] = $attachment_class->action; 151 $_FILES[ $attachment_class->file_input ] = array( 152 'tmp_name' => $this->image_file, 153 'name' => 'mystery-man.jpg', 154 'type' => 'image/jpeg', 155 'error' => 0, 156 'size' => filesize( $this->image_file ), 157 ); 158 159 // Error: file size 160 $upload = $attachment_class->upload( $_FILES ); 161 $this->assertFalse( empty( $upload['error'] ) ); 162 163 $attachment_class->allowed_mime_types = array( 'pdf' ); 164 $attachment_class->original_max_filesize = false; 165 166 // Error: file type 167 $upload = $attachment_class->upload( $_FILES ); 168 $this->assertFalse( empty( $upload['error'] ) ); 169 170 $attachment_class->allowed_mime_types = array(); 171 172 // Success 173 $upload = $attachment_class->upload( $_FILES ); 174 $this->assertEquals( $upload['file'], $attachment_class->upload_path . '/mystery-man.jpg' ); 175 176 // clean up! 177 $_FILES = $reset_files; 178 $_POST = $reset_post; 179 $this->clean_files(); 180 } 181 182 /** 183 * @group upload 184 */ 185 public function test_bp_attachment_upload_no_base_dir_specific_time() { 186 $reset_files = $_FILES; 187 $reset_post = $_POST; 188 189 $attachment_class = new BPTest_Attachment_Extension( array( 190 'action' => 'attachment_action', 191 'file_input' => 'attachment_file_input', 192 ) ); 193 194 $_POST['action'] = $attachment_class->action; 195 $_FILES[ $attachment_class->file_input ] = array( 196 'tmp_name' => $this->image_file, 197 'name' => 'mystery-man.jpg', 198 'type' => 'image/jpeg', 199 'error' => 0, 200 'size' => filesize( $this->image_file ), 201 ); 202 203 $time = '2015/01'; 204 205 $upload = $attachment_class->upload( $_FILES, '', $time ); 206 207 // If no base_dir was provided, default WordPress uploads dir should be used. 208 $this->assertEquals( $upload['file'], $attachment_class->upload_path . '/' . $time . '/mystery-man.jpg' ); 209 210 // clean up! 211 $_FILES = $reset_files; 212 $_POST = $reset_post; 213 } 214 215 /** 216 * @group upload 217 * @group avatar 218 */ 219 public function test_bp_attachment_avatar_user_upload() { 220 $reset_files = $_FILES; 221 $reset_post = $_POST; 222 $bp = buddypress(); 223 $displayed_user = $bp->displayed_user; 224 $bp->displayed_user = new stdClass; 225 226 $u1 = self::factory()->user->create(); 227 $bp->displayed_user->id = $u1; 228 229 // Upload the file 230 $avatar_attachment = new BP_Attachment_Avatar(); 231 $_POST['action'] = $avatar_attachment->action; 232 $_FILES[ $avatar_attachment->file_input ] = array( 233 'tmp_name' => $this->image_file, 234 'name' => 'mystery-man.jpg', 235 'type' => 'image/jpeg', 236 'error' => 0, 237 'size' => filesize( $this->image_file ) 238 ); 239 240 /* No error */ 241 $user_avatar = $avatar_attachment->upload( $_FILES, 'bp_members_avatar_upload_dir' ); 242 $this->assertEquals( $user_avatar['file'], $bp->avatar->upload_path . '/avatars/' . $u1 .'/mystery-man.jpg' ); 243 244 /* File size error */ 245 add_filter( 'bp_core_avatar_original_max_filesize', array( $this, 'max_filesize' ) ); 246 247 $user_avatar = $avatar_attachment->upload( $_FILES, 'bp_members_avatar_upload_dir' ); 248 249 remove_filter( 'bp_core_avatar_original_max_filesize', array( $this, 'max_filesize' ) ); 250 $this->assertFalse( empty( $user_avatar['error'] ) ); 251 252 /* File type error */ 253 $_FILES[ $avatar_attachment->file_input ]['name'] = 'buddypress_logo.pdf'; 254 $_FILES[ $avatar_attachment->file_input ]['type'] = 'application/pdf'; 255 256 $user_avatar = $avatar_attachment->upload( $_FILES, 'bp_members_avatar_upload_dir' ); 257 $this->assertFalse( empty( $user_avatar['error'] ) ); 258 259 // clean up! 260 $bp->displayed_user = $displayed_user; 261 $this->clean_avatars(); 262 $_FILES = $reset_files; 263 $_POST = $reset_post; 264 } 265 266 /** 267 * @group upload 268 * @group avatar 269 */ 270 public function test_bp_attachment_avatar_group_upload() { 271 $bp = buddypress(); 272 $reset_files = $_FILES; 273 $reset_post = $_POST; 274 $reset_current_group = $bp->groups->current_group; 275 276 $g = self::factory()->group->create(); 277 278 $bp->groups->current_group = groups_get_group( $g ); 279 280 // Upload the file 281 $avatar_attachment = new BP_Attachment_Avatar(); 282 $_POST['action'] = $avatar_attachment->action; 283 $_FILES[ $avatar_attachment->file_input ] = array( 284 'tmp_name' => $this->image_file, 285 'name' => 'mystery-man.jpg', 286 'type' => 'image/jpeg', 287 'error' => 0, 288 'size' => filesize( $this->image_file ) 289 ); 290 291 $group_avatar = $avatar_attachment->upload( $_FILES, 'groups_avatar_upload_dir' ); 292 $this->assertEquals( $group_avatar['file'], $bp->avatar->upload_path . '/group-avatars/' . $g .'/mystery-man.jpg' ); 293 294 // clean up! 295 $this->clean_avatars( 'group' ); 296 $bp->groups->current_group = $reset_current_group; 297 $_FILES = $reset_files; 298 $_POST = $reset_post; 299 } 300 301 /** 302 * @group crop 303 */ 304 public function test_bp_attachment_crop() { 305 $crop_args = array( 306 'original_file' => $this->image_file, 307 'crop_x' => 0, 308 'crop_y' => 0, 309 'crop_w' => 150, 310 'crop_h' => 150, 311 'dst_w' => 150, 312 'dst_h' => 150, 313 ); 314 315 $attachment_class = new BPTest_Attachment_Extension( array( 316 'action' => 'attachment_action', 317 'file_input' => 'attachment_file_input', 318 'base_dir' => 'attachment_base_dir', 319 ) ); 320 321 $cropped = $attachment_class->crop( $crop_args ); 322 323 // Image must come from the upload basedir 324 $this->assertTrue( is_wp_error( $cropped ) ); 325 326 $crop_args['original_file'] = $attachment_class->upload_path . '/mystery-man.jpg'; 327 328 // Image must stay in the upload basedir 329 $crop_args['dst_file'] = BP_TESTS_DIR . 'assets/error.jpg'; 330 $cropped = $attachment_class->crop( $crop_args ); 331 332 // Image must stay in the upload basedir 333 $this->assertTrue( is_wp_error( $cropped ) ); 334 335 // clean up! 336 $this->clean_files(); 337 } 338 339 /** 340 * @group upload 341 * @group cover_image 342 */ 343 public function test_bp_attachment_cover_image_user_upload() { 344 $reset_files = $_FILES; 345 $reset_post = $_POST; 346 $bp = buddypress(); 347 $displayed_user = $bp->displayed_user; 348 $bp->displayed_user = new stdClass; 349 350 $u1 = self::factory()->user->create(); 351 $bp->displayed_user->id = $u1; 352 353 // Upload the file 354 $cover_image_attachment = new BP_Attachment_Cover_Image(); 355 $_POST['action'] = $cover_image_attachment->action; 356 $_FILES[ $cover_image_attachment->file_input ] = array( 357 'tmp_name' => $this->image_file, 358 'name' => 'mystery-man.jpg', 359 'type' => 'image/jpeg', 360 'error' => 0, 361 'size' => filesize( $this->image_file ) 362 ); 363 364 /* No error */ 365 $cover_image = $cover_image_attachment->upload( $_FILES ); 366 $this->assertEquals( $cover_image['file'], $bp->avatar->upload_path . '/buddypress/members/' . $u1 .'/cover-image/mystery-man.jpg' ); 367 368 // clean up! 369 $bp->displayed_user = $displayed_user; 370 $this->clean_files( 'buddypress' ); 371 $_FILES = $reset_files; 372 $_POST = $reset_post; 373 } 374 375 /** 376 * @group shrink 377 * @group avatars 378 */ 379 public function test_bp_attachment_avatar_shrink() { 380 if ( false === _wp_image_editor_choose() || version_compare( phpversion(), '7.0' , '<' ) ) { 381 $this->markTestSkipped( 'This test requires PHP >= 7.0 and to have a valid image editor that is compatible with WordPress.' ); 382 } 383 384 $image = BP_TESTS_DIR . 'assets/upside-down.jpg'; 385 386 $dir_copy = bp_upload_dir(); 387 388 // in case cleaning files fails 389 if ( ! is_dir( $dir_copy['basedir'] . '/shrink' ) ) { 390 mkdir( $dir_copy['basedir'] . '/shrink' ); 391 } 392 393 $abs_path_copy = $dir_copy['basedir'] . '/shrink/upside-down.jpg'; 394 395 copy( $image, $abs_path_copy ); 396 397 add_filter( 'bp_core_avatar_original_max_width', array( $this, 'limit_to_50px' ) ); 398 399 $shrink = BP_Attachment_Avatar::shrink( $abs_path_copy ); 400 401 remove_filter( 'bp_core_avatar_original_max_width', array( $this, 'limit_to_50px' ) ); 402 403 $this->assertTrue( 50 === $shrink['width'] && 50 === $shrink['height'] ); 404 405 // Cleanup 406 $this->clean_files( 'shrink' ); 407 } 408 409 /** 410 * @group add_revision 411 */ 412 public function test_bp_attachment_add_revision() { 413 if ( false === _wp_image_editor_choose() || version_compare( phpversion(), '7.0' , '<' ) ) { 414 $this->markTestSkipped( 'This test requires PHP >= 7.0 and to have a valid image editor that is compatible with WordPress.' ); 415 } 416 417 $image = BP_TESTS_DIR . 'assets/upside-down.jpg'; 418 419 $attachment = new BPTest_Attachment_Extension( 420 array( 421 'base_dir' => 'add_revision', 422 'action' => 'attachment_action', 423 'file_input' => 'attachment_file_input', 424 ) 425 ); 426 427 $abs_path_copy = $attachment->upload_path . '/upside-down.jpg'; 428 copy( $image, $abs_path_copy ); 429 430 $revision = $attachment->add_revision( 431 'media', 432 array( 433 'file_abspath' => $abs_path_copy, 434 'file_id' => 'media', 435 ) 436 ); 437 438 $this->assertFalse( file_exists( $abs_path_copy ) ); 439 $this->assertTrue( file_exists( $revision->path ) ); 440 441 // Cleanup 442 @unlink( $revision->path ); 443 @rmdir( dirname( $revision->path ) ); 444 $this->clean_files( 'add_revision' ); 445 } 446 447 /** 448 * @group add_revision 449 * @group avatars 450 */ 451 public function test_bp_attachment_add_avatar_history() { 452 if ( false === _wp_image_editor_choose() || version_compare( phpversion(), '7.0' , '<' ) ) { 453 $this->markTestSkipped( 'This test requires PHP >= 7.0 and to have a valid image editor that is compatible with WordPress.' ); 454 } 455 456 $image = BP_TESTS_DIR . 'assets/upside-down.jpg'; 457 458 $attachment = new BPTest_Attachment_Extension( 459 array( 460 'base_dir' => 'add_history', 461 'action' => 'attachment_action', 462 'file_input' => 'attachment_file_input', 463 ) 464 ); 465 466 $abs_path_copy = $attachment->upload_path . '/upside-down.jpg'; 467 copy( $image, $abs_path_copy ); 468 469 $revision = $attachment->add_revision( 470 'avatar', 471 array( 472 'file_abspath' => $abs_path_copy, 473 'file_id' => 'avatar', 474 ) 475 ); 476 477 $this->assertFalse( file_exists( $abs_path_copy ) ); 478 $this->assertTrue( file_exists( $revision->path ) ); 479 $this->assertSame( $attachment->url . '/history/upside-down.jpg', $revision->url ); 480 481 // Cleanup 482 @unlink( $revision->path ); 483 @rmdir( dirname( $revision->path ) ); 484 $this->clean_files( 'add_history' ); 485 } 486 487 public function limit_to_50px( $max_width ) { 488 return 50; 489 } 490 491 /** 492 * @group shrink 493 * @group avatars 494 */ 495 public function test_bp_attachment_avatar_shrink_not_needed() { 496 $shrink = BP_Attachment_Avatar::shrink( $this->image_file ); 497 498 $this->assertTrue( empty( $shrink ) ); 499 } 500 501 /** 502 * @group shrink 503 * @group cover_images 504 */ 505 public function test_bp_attachment_cover_image_fit() { 506 if ( false === _wp_image_editor_choose() ) { 507 $this->markTestSkipped( 'This test requires PHP to have a valid image editor that is compatible with WordPress.' ); 508 } 509 510 $image = BP_TESTS_DIR . 'assets/test-image-large.jpg'; 511 512 $cover_image_class = new BP_Attachment_Cover_Image(); 513 514 $abs_path_copy = $cover_image_class->upload_path . '/test-image-large.jpg'; 515 516 copy( $image, $abs_path_copy ); 517 518 $fit = $cover_image_class->fit( $abs_path_copy, array( 'width' => 50, 'height' => 50 ) ); 519 520 $this->assertTrue( 50 === $fit['width'] && 50 === $fit['height'] ); 521 522 // Cleanup 523 $this->clean_files( 'buddypress' ); 524 } 525 526 /** 527 * @group shrink 528 * @group cover_images 529 */ 530 public function test_bp_attachment_cover_image_fit_not_needed() { 531 $cover_image_class = new BP_Attachment_Cover_Image(); 532 $fit = $cover_image_class->fit( $this->image_file, array( 'width' => 1300, 'height' => 225 ) ); 533 534 $this->assertTrue( empty( $fit ) ); 535 536 // Cleanup 537 $this->clean_files( 'buddypress' ); 538 } 539 540 /** 541 * @group avatars 542 * @group cover_images 543 */ 544 public function test_bp_attachment_get_image_data() { 545 if ( ! is_callable( 'exif_read_data' ) || version_compare( phpversion(), '7.0' , '<' ) ) { 546 $this->markTestSkipped( 'This test requires PHP >= 7.0 and to be compiled with EXIF support.' ); 547 } 548 549 $image_data = BP_Attachment::get_image_data( BP_TESTS_DIR . 'assets/upside-down.jpg' ); 550 551 $this->assertTrue( 3 == $image_data['meta']['orientation'] ); 552 } 553 554 /** 555 * @group upload 556 * @group cover_images 557 */ 558 public function test_bp_attachment_upload_dir_filter_arg() { 559 $reset_files = $_FILES; 560 $reset_post = $_POST; 561 562 $attachment_class = new BPTest_Attachment_Extension( array( 563 'action' => 'attachment_action', 564 'file_input' => 'attachment_file_input', 565 'base_dir' => 'attachment_base_dir', 566 'upload_dir_filter_args' => 1, 567 ) ); 568 569 $_POST['action'] = $attachment_class->action; 570 $_FILES[ $attachment_class->file_input ] = array( 571 'tmp_name' => $this->image_file, 572 'name' => 'mystery-man.jpg', 573 'type' => 'image/jpeg', 574 'error' => 0, 575 'size' => filesize( $this->image_file ), 576 ); 577 578 // Simulate an upload 579 $attachment_class->upload( $_FILES ); 580 581 // Remove the filter used to fake uploads 582 remove_filter( 'upload_dir', array( $this, 'filter_upload_dir' ), 20 ); 583 584 $this->assertSame( $attachment_class->original_upload_dir, wp_upload_dir() ); 585 586 // Restore the filter used to fake uploads 587 add_filter( 'upload_dir', array( $this, 'filter_upload_dir' ), 20, 1 ); 588 589 $this->assertTrue( 1 === $attachment_class->upload_dir_filter_args ); 590 591 $cover_image_class = new BP_Attachment_Cover_Image(); 592 593 // Simulate an upload 594 $cover_image_class->upload( $_FILES ); 595 596 // Should be empty 597 $this->assertEmpty( $this->original_upload_dir ); 598 599 $this->assertTrue( 0 === $cover_image_class->upload_dir_filter_args ); 600 601 $_FILES = $reset_files; 602 $_POST = $reset_post; 603 } 604 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sun Dec 22 01:00:54 2024 | Cross-referenced by PHPXref 0.7.1 |