[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/tests/phpunit/testcases/core/ -> class-bp-attachment.php (source)

   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, 'xprofile_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, 'xprofile_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, 'xprofile_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() ) {
 381              $this->markTestSkipped( 'This test requires PHP 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  	public function limit_to_50px( $max_width ) {
 410          return 50;
 411      }
 412  
 413      /**
 414       * @group shrink
 415       * @group avatars
 416       */
 417  	public function test_bp_attachment_avatar_shrink_not_needed() {
 418          $shrink = BP_Attachment_Avatar::shrink( $this->image_file );
 419  
 420          $this->assertTrue( empty( $shrink ) );
 421      }
 422  
 423      /**
 424       * @group shrink
 425       * @group cover_images
 426       */
 427  	public function test_bp_attachment_cover_image_fit() {
 428          if ( false === _wp_image_editor_choose() ) {
 429              $this->markTestSkipped( 'This test requires PHP to have a valid image editor that is compatible with WordPress.' );
 430          }
 431  
 432          $image = BP_TESTS_DIR . 'assets/upside-down.jpg';
 433  
 434          $cover_image_class = new BP_Attachment_Cover_Image();
 435  
 436          $abs_path_copy = $cover_image_class->upload_path . '/upside-down.jpg';
 437  
 438          copy( $image, $abs_path_copy );
 439  
 440          $fit = $cover_image_class->fit( $abs_path_copy, array( 'width' => 50, 'height' => 50 ) );
 441  
 442          $this->assertTrue( 50 === $fit['width'] && 50 === $fit['height'] );
 443  
 444          // Cleanup
 445          $this->clean_files( 'buddypress' );
 446      }
 447  
 448      /**
 449       * @group shrink
 450       * @group cover_images
 451       */
 452  	public function test_bp_attachment_cover_image_fit_not_needed() {
 453          $cover_image_class = new BP_Attachment_Cover_Image();
 454          $fit = $cover_image_class->fit( $this->image_file, array( 'width' => 1300, 'height' => 225 ) );
 455  
 456          $this->assertTrue( empty( $fit ) );
 457  
 458          // Cleanup
 459          $this->clean_files( 'buddypress' );
 460      }
 461  
 462      /**
 463       * @group avatars
 464       * @group cover_images
 465       */
 466  	public function test_bp_attachment_get_image_data() {
 467          if ( ! is_callable( 'exif_read_data' ) ) {
 468              $this->markTestSkipped( 'This test requires PHP to be compiled with EXIF support.' );
 469          }
 470  
 471          $image_data = BP_Attachment::get_image_data( BP_TESTS_DIR . 'assets/upside-down.jpg' );
 472  
 473          $this->assertTrue( 3 == $image_data['meta']['orientation'] );
 474      }
 475  
 476      /**
 477       * @group upload
 478       * @group cover_images
 479       */
 480  	public function test_bp_attachment_upload_dir_filter_arg() {
 481          $reset_files = $_FILES;
 482          $reset_post = $_POST;
 483  
 484          $attachment_class = new BPTest_Attachment_Extension( array(
 485              'action'                 => 'attachment_action',
 486              'file_input'             => 'attachment_file_input',
 487              'base_dir'               => 'attachment_base_dir',
 488              'upload_dir_filter_args' => 1,
 489          ) );
 490  
 491          $_POST['action'] = $attachment_class->action;
 492          $_FILES[ $attachment_class->file_input ] = array(
 493              'tmp_name' => $this->image_file,
 494              'name'     => 'mystery-man.jpg',
 495              'type'     => 'image/jpeg',
 496              'error'    => 0,
 497              'size'     => filesize( $this->image_file ),
 498          );
 499  
 500          // Simulate an upload
 501          $attachment_class->upload( $_FILES );
 502  
 503          // Remove the filter used to fake uploads
 504          remove_filter( 'upload_dir', array( $this, 'filter_upload_dir' ), 20 );
 505  
 506          $this->assertSame( $attachment_class->original_upload_dir, wp_upload_dir() );
 507  
 508          // Restore the filter used to fake uploads
 509          add_filter( 'upload_dir', array( $this, 'filter_upload_dir' ), 20, 1 );
 510  
 511          $this->assertTrue( 1 === $attachment_class->upload_dir_filter_args );
 512  
 513          $cover_image_class = new BP_Attachment_Cover_Image();
 514  
 515          // Simulate an upload
 516          $cover_image_class->upload( $_FILES );
 517  
 518          // Should be empty
 519          $this->assertEmpty( $this->original_upload_dir );
 520  
 521          $this->assertTrue( 0 === $cover_image_class->upload_dir_filter_args );
 522  
 523          $_FILES = $reset_files;
 524          $_POST = $reset_post;
 525      }
 526  }


Generated: Tue Jul 16 01:01:43 2019 Cross-referenced by PHPXref 0.7.1