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