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