Skip to:
Content

BuddyPress.org

Ticket #6278: 6278.unitests.02.patch

File 6278.unitests.02.patch, 11.9 KB (added by imath, 10 years ago)
  • tests/phpunit/assets/attachment-extensions.php

    diff --git tests/phpunit/assets/attachment-extensions.php tests/phpunit/assets/attachment-extensions.php
    index e69de29..9981683 100644
     
     1<?php
     2/**
     3 * The following implementations of BP_Attachment act as dummy plugins
     4 * for our unit tests
     5 */
     6class BPTest_Attachment_Extension extends BP_Attachment {
     7        public function __construct( $args = array() ) {
     8                return parent::__construct( $args );
     9        }
     10}
  • tests/phpunit/includes/testcase.php

    diff --git tests/phpunit/includes/testcase.php tests/phpunit/includes/testcase.php
    index d14fe76..866dbc3 100644
    class BP_UnitTestCase extends WP_UnitTestCase { 
    423423                global $wpdb;
    424424                $wpdb->query( 'COMMIT;' );
    425425        }
     426
     427        /**
     428         * Clean up created directories/files
     429         */
     430        public function rrmdir( $dir ) {
     431                $d = glob( $dir . '/*' );
     432
     433                if ( ! empty( $d ) ) {
     434                        foreach ( $d as $file ) {
     435                                if ( is_dir( $file ) ) {
     436                                        $this->rrmdir( $file );
     437                                } else {
     438                                        @unlink( $file );
     439                                }
     440                        }
     441                }
     442
     443                @rmdir( $dir );
     444        }
    426445}
  • tests/phpunit/testcases/core/avatars.php

    diff --git tests/phpunit/testcases/core/avatars.php tests/phpunit/testcases/core/avatars.php
    index d37fe1a..330b18e 100644
    class BP_Tests_Avatars extends BP_UnitTestCase { 
    1616                $this->rrmdir( bp_core_avatar_upload_path() . '/' . $avatar_dir );
    1717        }
    1818
    19         private function rrmdir( $dir ) {
    20                 $d = glob( $dir . '/*' );
    21 
    22                 if ( empty( $d ) ) {
    23                         return;
    24                 }
    25 
    26                 foreach ( $d as $file ) {
    27                         if ( is_dir( $file ) ) {
    28                                 $this->rrmdir( $file );
    29                         } else {
    30                                 @unlink( $file );
    31                         }
    32                 }
    33 
    34                 @rmdir( $dir );
    35         }
    36 
    3719        /**
    3820         * @ticket BP4948
    3921         */
  • tests/phpunit/testcases/core/class-bp-attachment.php

    diff --git tests/phpunit/testcases/core/class-bp-attachment.php tests/phpunit/testcases/core/class-bp-attachment.php
    index e69de29..110b4d2 100644
     
     1<?php
     2
     3include_once BP_TESTS_DIR . 'assets/attachment-extensions.php';
     4
     5/**
     6 * @group bp_attachments
     7 * @group BP_Attachment
     8 */
     9class 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( 'bp_attachment_upload_dir',       array( $this, 'filter_upload_dir' ), 10, 1 );
     17                add_filter( 'xprofile_avatar_upload_dir',     array( $this, 'filter_upload_dir' ), 10, 1 );
     18                add_filter( 'groups_avatar_upload_dir',       array( $this, 'filter_upload_dir' ), 10, 1 );
     19                $this->upload_results = array();
     20                $this->image_file = trailingslashit( buddypress()->plugin_dir ) . 'bp-core/images/mystery-man.jpg';
     21        }
     22
     23        public function tearDown() {
     24                parent::tearDown();
     25                remove_filter( 'bp_attachment_upload_overrides', array( $this, 'filter_overrides' ),  10, 1 );
     26                remove_filter( 'bp_attachment_upload_dir',       array( $this, 'filter_upload_dir' ), 10, 1 );
     27                remove_filter( 'xprofile_avatar_upload_dir',     array( $this, 'filter_upload_dir' ), 10, 1 );
     28                remove_filter( 'groups_avatar_upload_dir',       array( $this, 'filter_upload_dir' ), 10, 1 );
     29                $this->upload_results = array();
     30                $this->image_file = '';
     31        }
     32
     33        public function filter_overrides( $overrides ) {
     34                $overrides['upload_error_handler'] = array( $this, 'upload_error_handler' );
     35                return $overrides;
     36        }
     37
     38        public function filter_upload_dir( $upload_dir ) {
     39                $upload_dir['error'] = 'fake_upload_success';
     40
     41                $this->upload_results = array(
     42                        'new_file' => $upload_dir['path'] . '/mystery-man.jpg',
     43                        'url'      => $upload_dir['url'] . '/mystery-man.jpg',
     44                );
     45
     46                return $upload_dir;
     47        }
     48
     49        /**
     50         * To avoid copying files in tests, we're faking a succesfull uploads
     51         * as soon as all the test_form have been executed in _wp_handle_upload
     52         */
     53        public function upload_error_handler( &$file, $message ) {
     54                if ( 'fake_upload_success' !== $message ) {
     55                        return array( 'error' => $message );
     56                } else {
     57                        return array(
     58                                'file' => $this->upload_results['new_file'],
     59                                'url'  => $this->upload_results['url'],
     60                                'type' => 'image/jpeg',
     61                        );
     62                }
     63        }
     64
     65        private function clean_files( $basedir = 'attachment_base_dir' ) {
     66                $upload_dir = bp_upload_dir();
     67
     68                $this->rrmdir( $upload_dir['basedir'] . '/' . $basedir );
     69        }
     70
     71        private function clean_avatars( $type = 'user' ) {
     72                if ( 'user' === $type ) {
     73                        $avatar_dir = 'avatars';
     74                } elseif ( 'group' === $type ) {
     75                        $avatar_dir = 'group-avatars';
     76                }
     77
     78                $this->rrmdir( bp_core_avatar_upload_path() . '/' . $avatar_dir );
     79        }
     80
     81        public function max_filesize() {
     82                return 1000;
     83        }
     84
     85        public function test_bp_attachment_construct_missing_required_parameter() {
     86                $reset_files = $_FILES;
     87                $reset_post = $_POST;
     88
     89                $_FILES['file'] = array(
     90                        'name'     => 'mystery-man.jpg',
     91                        'type'     => 'image/jpeg',
     92                        'error'    => 0,
     93                        'size'     => 1000
     94                );
     95
     96                $attachment_class = new BPTest_Attachment_Extension();
     97                $upload = $attachment_class->upload( $_FILES );
     98
     99                $this->assertTrue( empty( $upload ) );
     100
     101                $_FILES = $reset_files;
     102                $_POST = $reset_post;
     103        }
     104
     105        public function test_bp_attachment_set_upload_dir() {
     106                $upload_dir = bp_upload_dir();
     107
     108                $attachment_class = new BPTest_Attachment_Extension( array(
     109                        'action'     => 'attachment_action',
     110                        'file_input' => 'attachment_file_input'
     111                ) );
     112
     113                $this->assertSame( $attachment_class->upload_dir, bp_upload_dir() );
     114
     115                $attachment_class = new BPTest_Attachment_Extension( array(
     116                        'action'     => 'attachment_action',
     117                        'file_input' => 'attachment_file_input',
     118                        'base_dir'   => 'attachment_base_dir',
     119                ) );
     120
     121                $this->assertTrue( file_exists( $upload_dir['basedir'] . '/attachment_base_dir'  ) );
     122
     123                // clean up
     124                $this->clean_files();
     125        }
     126
     127        /**
     128         * @group rupload
     129         */
     130        public function test_bp_attachment_upload() {
     131                $reset_files = $_FILES;
     132                $reset_post = $_POST;
     133
     134                $attachment_class = new BPTest_Attachment_Extension( array(
     135                        'action'                => 'attachment_action',
     136                        'file_input'            => 'attachment_file_input',
     137                        'base_dir'                  => 'attachment_base_dir',
     138                        'original_max_filesize' => 1000,
     139                ) );
     140
     141                $_POST['action'] = $attachment_class->action;
     142                $_FILES[ $attachment_class->file_input ] = array(
     143                        'tmp_name' => $this->image_file,
     144                        'name'     => 'mystery-man.jpg',
     145                        'type'     => 'image/jpeg',
     146                        'error'    => 0,
     147                        'size'     => filesize( $this->image_file ),
     148                );
     149
     150                // Error: file size
     151                $upload = $attachment_class->upload( $_FILES );
     152                $this->assertFalse( empty( $upload['error'] ) );
     153
     154                $attachment_class->allowed_mime_types    = array( 'pdf' );
     155                $attachment_class->original_max_filesize = false;
     156
     157                // Error: file type
     158                $upload = $attachment_class->upload( $_FILES );
     159                $this->assertFalse( empty( $upload['error'] ) );
     160
     161                $attachment_class->allowed_mime_types = array();
     162
     163                // Success
     164                $upload = $attachment_class->upload( $_FILES );
     165                $this->assertEquals( $upload['file'], $attachment_class->upload_path . '/mystery-man.jpg' );
     166
     167                // clean up!
     168                $_FILES = $reset_files;
     169                $_POST = $reset_post;
     170                $this->clean_files();
     171        }
     172
     173        /**
     174         * @group upload
     175         * @group avatar
     176         */
     177        public function test_bp_attachment_avatar_user_upload() {
     178                $reset_files = $_FILES;
     179                $reset_post = $_POST;
     180                $bp = buddypress();
     181                $displayed_user = $bp->displayed_user;
     182                $bp->displayed_user = new stdClass;
     183
     184                $u1 = $this->factory->user->create();
     185                $bp->displayed_user->id = $u1;
     186
     187                // Upload the file
     188                $avatar_attachment = new BP_Attachment_Avatar();
     189                $_POST['action'] = $avatar_attachment->action;
     190                $_FILES[ $avatar_attachment->file_input ] = array(
     191                        'tmp_name' => $this->image_file,
     192                        'name'     => 'mystery-man.jpg',
     193                        'type'     => 'image/jpeg',
     194                        'error'    => 0,
     195                        'size'     => filesize( $this->image_file )
     196                );
     197
     198                /* No error */
     199                $user_avatar = $avatar_attachment->upload( $_FILES, 'xprofile_avatar_upload_dir' );
     200                $this->assertEquals( $user_avatar['file'], $bp->avatar->upload_path . '/avatars/' . $u1 .'/mystery-man.jpg' );
     201
     202                /* File size error */
     203                add_filter( 'bp_core_avatar_original_max_filesize', array( $this, 'max_filesize' ) );
     204
     205                $user_avatar = $avatar_attachment->upload( $_FILES, 'xprofile_avatar_upload_dir' );
     206
     207                remove_filter( 'bp_core_avatar_original_max_filesize', array( $this, 'max_filesize' ) );
     208                $this->assertFalse( empty( $user_avatar['error'] ) );
     209
     210                /* File type error */
     211                $_FILES[ $avatar_attachment->file_input ]['name'] = 'buddypress_logo.pdf';
     212                $_FILES[ $avatar_attachment->file_input ]['type'] = 'application/pdf';
     213
     214                $user_avatar = $avatar_attachment->upload( $_FILES, 'xprofile_avatar_upload_dir' );
     215                $this->assertFalse( empty( $user_avatar['error'] ) );
     216
     217                // clean up!
     218                $bp->displayed_user = $displayed_user;
     219                $this->clean_avatars();
     220                $_FILES = $reset_files;
     221                $_POST = $reset_post;
     222        }
     223
     224        /**
     225         * @group upload
     226         * @group avatar
     227         */
     228        public function test_bp_attachment_avatar_group_upload() {
     229                $bp = buddypress();
     230                $reset_files = $_FILES;
     231                $reset_post = $_POST;
     232                $reset_current_group = $bp->groups->current_group;
     233
     234                $g = $this->factory->group->create();
     235
     236                $bp->groups->current_group = groups_get_group( array(
     237                        'group_id'        => $g,
     238                        'populate_extras' => true,
     239                ) );
     240
     241                // Upload the file
     242                $avatar_attachment = new BP_Attachment_Avatar();
     243                $_POST['action'] = $avatar_attachment->action;
     244                $_FILES[ $avatar_attachment->file_input ] = array(
     245                        'tmp_name' => $this->image_file,
     246                        'name'     => 'mystery-man.jpg',
     247                        'type'     => 'image/jpeg',
     248                        'error'    => 0,
     249                        'size'     => filesize( $this->image_file )
     250                );
     251
     252                $group_avatar = $avatar_attachment->upload( $_FILES, 'groups_avatar_upload_dir' );
     253                $this->assertEquals( $group_avatar['file'], $bp->avatar->upload_path . '/group-avatars/' . $g .'/mystery-man.jpg' );
     254
     255                // clean up!
     256                $this->clean_avatars( 'group' );
     257                $bp->groups->current_group = $reset_current_group;
     258                $_FILES = $reset_files;
     259                $_POST = $reset_post;
     260        }
     261
     262        /**
     263         * @group crop
     264         */
     265        public function test_bp_attachment_crop() {
     266                $crop_args = array(
     267                        'original_file' => $this->image_file,
     268                        'crop_x'        => 0,
     269                        'crop_y'        => 0,
     270                        'crop_w'        => 150,
     271                        'crop_h'        => 150,
     272                        'dst_w'         => 150,
     273                        'dst_h'         => 150,
     274                );
     275
     276                $attachment_class = new BPTest_Attachment_Extension( array(
     277                        'action'                => 'attachment_action',
     278                        'file_input'            => 'attachment_file_input',
     279                        'base_dir'                  => 'attachment_base_dir',
     280                ) );
     281
     282                $cropped = $attachment_class->crop( $crop_args );
     283
     284                // Image must come from the upload basedir
     285                $this->assertTrue( is_wp_error( $cropped ) );
     286
     287                $crop_args['original_file'] = $attachment_class->upload_path . '/mystery-man.jpg';
     288
     289                // Image must stay in the upload basedir
     290                $crop_args['dst_file'] = BP_TESTS_DIR . 'assets/error.jpg';
     291                $cropped = $attachment_class->crop( $crop_args );
     292
     293                // Image must stay in the upload basedir
     294                $this->assertTrue( is_wp_error( $cropped ) );
     295
     296                // clean up!
     297                $this->clean_files();
     298        }
     299}
  • tests/phpunit/testcases/core/functions.php

    diff --git tests/phpunit/testcases/core/functions.php tests/phpunit/testcases/core/functions.php
    index 41d77a3..d183690 100644
    class BP_Tests_Core_Functions extends BP_UnitTestCase { 
    598598                        date_default_timezone_set( $tz_backup );
    599599                }
    600600        }
     601
     602        /**
     603         * @group bp_attachments
     604         * @group bp_upload_dir
     605         */
     606        public function test_bp_upload_dir_ms() {
     607                if ( ! is_multisite() ) {
     608                        $this->markTestSkipped();
     609                }
     610
     611                $expected_upload_dir = wp_upload_dir();
     612
     613                $b = $this->factory->blog->create();
     614
     615                switch_to_blog( $b );
     616
     617                $tested_upload_dir = bp_upload_dir();
     618
     619                restore_current_blog();
     620
     621                $this->assertSame( $expected_upload_dir, $tested_upload_dir );
     622        }
    601623}