| 207 | |
| 208 | /** |
| 209 | * @group bp_core_check_avatar_type |
| 210 | */ |
| 211 | public function test_bp_core_check_avatar_type() { |
| 212 | $file = array( |
| 213 | 'file' => array( |
| 214 | 'name' => 'buddypress_logo.pdf', |
| 215 | 'type' => 'application/pdf', |
| 216 | 'tmp_name' => BP_TESTS_DIR . 'assets/files/buddypress_logo.pdf', |
| 217 | ) |
| 218 | ); |
| 219 | |
| 220 | $this->assertFalse( bp_core_check_avatar_type( $file ) ); |
| 221 | |
| 222 | $file = array( |
| 223 | 'file' => array( |
| 224 | 'name' => 'disc.png', |
| 225 | 'type' => 'image/png', |
| 226 | 'tmp_name' => BP_TESTS_DIR . 'assets/files/disc.png', |
| 227 | ) |
| 228 | ); |
| 229 | |
| 230 | $this->assertTrue( bp_core_check_avatar_type( $file ) ); |
| 231 | |
| 232 | $file = array( |
| 233 | 'file' => array( |
| 234 | 'name' => 'disc.png', |
| 235 | 'type' => 'application/octet-stream', |
| 236 | 'tmp_name' => BP_TESTS_DIR . 'assets/files/disc.png', |
| 237 | ) |
| 238 | ); |
| 239 | |
| 240 | $this->assertTrue( bp_core_check_avatar_type( $file ), 'flash is using application/octet-stream for image uploads' ); |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * @group bp_core_check_avatar_type |
| 245 | * @group bp_core_get_allowed_avatar_types |
| 246 | */ |
| 247 | public function test_bp_core_get_allowed_avatar_types_filter() { |
| 248 | add_filter( 'bp_core_get_allowed_avatar_types', array( $this, 'avatar_types_filter_add_type' ) ); |
| 249 | |
| 250 | $this->assertEquals( array( 'jpeg', 'gif', 'png' ), bp_core_get_allowed_avatar_types() ); |
| 251 | |
| 252 | remove_filter( 'bp_core_get_allowed_avatar_types', array( $this, 'avatar_types_filter_add_type' ) ); |
| 253 | |
| 254 | add_filter( 'bp_core_get_allowed_avatar_types', array( $this, 'avatar_types_filter_remove_type' ) ); |
| 255 | |
| 256 | $this->assertEquals( array( 'gif', 'png' ), bp_core_get_allowed_avatar_types() ); |
| 257 | |
| 258 | remove_filter( 'bp_core_get_allowed_avatar_types', array( $this, 'avatar_types_filter_remove_type' ) ); |
| 259 | |
| 260 | add_filter( 'bp_core_get_allowed_avatar_types', '__return_empty_array' ); |
| 261 | |
| 262 | $this->assertEquals( array( 'jpeg', 'gif', 'png' ), bp_core_get_allowed_avatar_types() ); |
| 263 | |
| 264 | remove_filter( 'bp_core_get_allowed_avatar_types', '__return_empty_array' ); |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * @group bp_core_check_avatar_type |
| 269 | * @group bp_core_get_allowed_avatar_mimes |
| 270 | */ |
| 271 | public function test_bp_core_get_allowed_avatar_mimes() { |
| 272 | $mimes = bp_core_get_allowed_avatar_mimes(); |
| 273 | |
| 274 | $this->assertEquals( array( 'jpeg', 'gif', 'png', 'jpg' ), array_keys( $mimes ) ); |
| 275 | $this->assertEquals( array( 'image/jpeg', 'image/gif', 'image/png', 'image/jpeg' ), array_values( $mimes ) ); |
| 276 | |
| 277 | add_filter( 'bp_core_get_allowed_avatar_types', array( $this, 'avatar_types_filter_add_type' ) ); |
| 278 | |
| 279 | $this->assertEquals( array( 'image/jpeg', 'image/gif', 'image/png', 'image/jpeg' ), array_values( bp_core_get_allowed_avatar_mimes() ) ); |
| 280 | |
| 281 | remove_filter( 'bp_core_get_allowed_avatar_types', array( $this, 'avatar_types_filter_add_type' ) ); |
| 282 | |
| 283 | add_filter( 'bp_core_get_allowed_avatar_types', array( $this, 'avatar_types_filter_remove_type' ) ); |
| 284 | |
| 285 | $this->assertEquals( array( 'image/gif', 'image/png' ), array_values( bp_core_get_allowed_avatar_mimes() ) ); |
| 286 | |
| 287 | remove_filter( 'bp_core_get_allowed_avatar_types', array( $this, 'avatar_types_filter_remove_type' ) ); |
| 288 | |
| 289 | add_filter( 'bp_core_get_allowed_avatar_types', '__return_empty_array' ); |
| 290 | |
| 291 | $this->assertEquals( array( 'image/jpeg', 'image/gif', 'image/png', 'image/jpeg' ), array_values( bp_core_get_allowed_avatar_mimes() ) ); |
| 292 | |
| 293 | remove_filter( 'bp_core_get_allowed_avatar_types', '__return_empty_array' ); |
| 294 | } |
| 295 | |
| 296 | public function avatar_types_filter_add_type( $types ) { |
| 297 | $types[] = 'bmp'; |
| 298 | |
| 299 | return $types; |
| 300 | } |
| 301 | |
| 302 | public function avatar_types_filter_remove_type( $types ) { |
| 303 | $jpeg = array_shift( $types ); |
| 304 | |
| 305 | return $types; |
| 306 | } |