| | 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( '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 | } |