<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[BuddyPress][9831] trunk: Make sure the BP Attachments API behaves the right way when WordPress < 4.0</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="http://buddypress.trac.wordpress.org/changeset/9831">9831</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"http://buddypress.trac.wordpress.org/changeset/9831","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>imath</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2015-05-02 20:55:07 +0000 (Sat, 02 May 2015)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Make sure the BP Attachments API behaves the right way when WordPress < 4.0
Before WordPress 4.0, there was no dynamic filter based on the name of the action posted in the upload form. So we also need to filter `wp_handle_upload_prefilter` to make sure the `BP_Attachments->validate_upload()` function will actually validate the uploads for versions of WordPress < 4.0.
Improve the i18n of the avatar type error, as the avatar types can now be filtered in the `BP_Attachment_Avatar` class.
Add unit tests for the `BP Attachement` class.
Fixes <a href="http://buddypress.trac.wordpress.org/ticket/6278">#6278</a></pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcbpcoreclassesclassbpattachmentavatarphp">trunk/src/bp-core/classes/class-bp-attachment-avatar.php</a></li>
<li><a href="#trunksrcbpcoreclassesclassbpattachmentphp">trunk/src/bp-core/classes/class-bp-attachment.php</a></li>
<li><a href="#trunktestsphpunitincludestestcasephp">trunk/tests/phpunit/includes/testcase.php</a></li>
<li><a href="#trunktestsphpunittestcasescoreavatarsphp">trunk/tests/phpunit/testcases/core/avatars.php</a></li>
<li><a href="#trunktestsphpunittestcasescorefunctionsphp">trunk/tests/phpunit/testcases/core/functions.php</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li><a href="#trunktestsphpunitassetsattachmentextensionsphp">trunk/tests/phpunit/assets/attachment-extensions.php</a></li>
<li><a href="#trunktestsphpunittestcasescoreclassbpattachmentphp">trunk/tests/phpunit/testcases/core/class-bp-attachment.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcbpcoreclassesclassbpattachmentavatarphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-core/classes/class-bp-attachment-avatar.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-core/classes/class-bp-attachment-avatar.php 2015-05-02 01:36:28 UTC (rev 9830)
+++ trunk/src/bp-core/classes/class-bp-attachment-avatar.php 2015-05-02 20:55:07 UTC (rev 9831)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -28,6 +28,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @uses BP_Attachment::__construct()
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function __construct() {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Allowed avatar types
+ $allowed_types = bp_core_get_allowed_avatar_types();
+
</ins><span class="cx" style="display: block; padding: 0 10px"> parent::__construct( array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'action' => 'bp_avatar_upload',
</span><span class="cx" style="display: block; padding: 0 10px"> 'file_input' => 'file',
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -36,12 +39,24 @@
</span><span class="cx" style="display: block; padding: 0 10px"> // Specific errors for avatars
</span><span class="cx" style="display: block; padding: 0 10px"> 'upload_error_strings' => array(
</span><span class="cx" style="display: block; padding: 0 10px"> 9 => sprintf( __( 'That photo is too big. Please upload one smaller than %s', 'buddypress' ), size_format( bp_core_avatar_original_max_filesize() ) ),
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 10 => __( 'Please upload only JPG, GIF or PNG photos.', 'buddypress' ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 10 => sprintf( _n( 'Please upload only this file type: %s.', 'Please upload only these file types: %s.', count( $allowed_types ), 'buddypress' ), self::get_avatar_types( $allowed_types ) ),
</ins><span class="cx" style="display: block; padding: 0 10px"> ),
</span><span class="cx" style="display: block; padding: 0 10px"> ) );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Gets the available avatar types
+ *
+ * @since BuddyPress (2.3.0)
+ * @return string comma separated list of allowed avatar types
+ */
+ public static function get_avatar_types( $allowed_types = array() ) {
+ $types = array_map( 'strtoupper', $allowed_types );
+ $comma = _x( ',', 'avatar types separator', 'buddypress' );
+ return join( $comma . ' ', $types );
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Set Upload Dir data for avatars
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since BuddyPress (2.3.0)
</span></span></pre></div>
<a id="trunksrcbpcoreclassesclassbpattachmentphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-core/classes/class-bp-attachment.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-core/classes/class-bp-attachment.php 2015-05-02 01:36:28 UTC (rev 9830)
+++ trunk/src/bp-core/classes/class-bp-attachment.php 2015-05-02 20:55:07 UTC (rev 9831)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -219,6 +219,13 @@
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> add_filter( "{$this->action}_prefilter", array( $this, 'validate_upload' ), 10, 1 );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * The above dynamic filter was introduced in WordPress 4.0, as we support WordPress
+ * back to 3.6, we need to also use the pre 4.0 static filter and remove it after
+ * the upload was processed.
+ */
+ add_filter( 'wp_handle_upload_prefilter', array( $this, 'validate_upload' ), 10, 1 );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> // Set Default overrides
</span><span class="cx" style="display: block; padding: 0 10px"> $overrides = array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'action' => $this->action,
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -265,6 +272,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> // Restore WordPress Uploads data
</span><span class="cx" style="display: block; padding: 0 10px"> remove_filter( 'upload_dir', $upload_dir_filter, 10, 0 );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Remove the pre WordPress 4.0 static filter
+ remove_filter( 'wp_handle_upload_prefilter', array( $this, 'validate_upload' ), 10, 1 );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> // Finally return the uploaded file or the error
</span><span class="cx" style="display: block; padding: 0 10px"> return $this->attachment;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunktestsphpunitassetsattachmentextensionsphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/assets/attachment-extensions.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/assets/attachment-extensions.php (rev 0)
+++ trunk/tests/phpunit/assets/attachment-extensions.php 2015-05-02 20:55:07 UTC (rev 9831)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,10 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * The following implementations of BP_Attachment act as dummy plugins
+ * for our unit tests
+ */
+class BPTest_Attachment_Extension extends BP_Attachment {
+ public function __construct( $args = array() ) {
+ return parent::__construct( $args );
+ }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/tests/phpunit/assets/attachment-extensions.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="trunktestsphpunitincludestestcasephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/includes/testcase.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/includes/testcase.php 2015-05-02 01:36:28 UTC (rev 9830)
+++ trunk/tests/phpunit/includes/testcase.php 2015-05-02 20:55:07 UTC (rev 9831)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -423,4 +423,28 @@
</span><span class="cx" style="display: block; padding: 0 10px"> global $wpdb;
</span><span class="cx" style="display: block; padding: 0 10px"> $wpdb->query( 'COMMIT;' );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ /**
+ * Clean up created directories/files
+ */
+ public function rrmdir( $dir ) {
+ // Make sure we are only removing files/dir from uploads
+ if ( 0 !== strpos( $dir, bp_core_avatar_upload_path() ) ) {
+ return;
+ }
+
+ $d = glob( $dir . '/*' );
+
+ if ( ! empty( $d ) ) {
+ foreach ( $d as $file ) {
+ if ( is_dir( $file ) ) {
+ $this->rrmdir( $file );
+ } else {
+ @unlink( $file );
+ }
+ }
+ }
+
+ @rmdir( $dir );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunktestsphpunittestcasescoreavatarsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/testcases/core/avatars.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/testcases/core/avatars.php 2015-05-02 01:36:28 UTC (rev 9830)
+++ trunk/tests/phpunit/testcases/core/avatars.php 2015-05-02 20:55:07 UTC (rev 9831)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -16,24 +16,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->rrmdir( bp_core_avatar_upload_path() . '/' . $avatar_dir );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- private function rrmdir( $dir ) {
- $d = glob( $dir . '/*' );
-
- if ( empty( $d ) ) {
- return;
- }
-
- foreach ( $d as $file ) {
- if ( is_dir( $file ) ) {
- $this->rrmdir( $file );
- } else {
- @unlink( $file );
- }
- }
-
- @rmdir( $dir );
- }
-
</del><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket BP4948
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span></span></pre></div>
<a id="trunktestsphpunittestcasescoreclassbpattachmentphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/testcases/core/class-bp-attachment.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/testcases/core/class-bp-attachment.php (rev 0)
+++ trunk/tests/phpunit/testcases/core/class-bp-attachment.php 2015-05-02 20:55:07 UTC (rev 9831)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,302 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+include_once BP_TESTS_DIR . 'assets/attachment-extensions.php';
+
+/**
+ * @group bp_attachments
+ * @group BP_Attachment
+ */
+class BP_Tests_BP_Attachment_TestCases extends BP_UnitTestCase {
+ private $upload_results;
+ private $image_file;
+
+ public function setUp() {
+ parent::setUp();
+ add_filter( 'bp_attachment_upload_overrides', array( $this, 'filter_overrides' ), 10, 1 );
+ add_filter( 'bp_attachment_upload_dir', array( $this, 'filter_upload_dir' ), 10, 1 );
+ add_filter( 'xprofile_avatar_upload_dir', array( $this, 'filter_upload_dir' ), 10, 1 );
+ add_filter( 'groups_avatar_upload_dir', array( $this, 'filter_upload_dir' ), 10, 1 );
+ $this->upload_results = array();
+ $this->image_file = trailingslashit( buddypress()->plugin_dir ) . 'bp-core/images/mystery-man.jpg';
+ }
+
+ public function tearDown() {
+ parent::tearDown();
+ remove_filter( 'bp_attachment_upload_overrides', array( $this, 'filter_overrides' ), 10, 1 );
+ remove_filter( 'bp_attachment_upload_dir', array( $this, 'filter_upload_dir' ), 10, 1 );
+ remove_filter( 'xprofile_avatar_upload_dir', array( $this, 'filter_upload_dir' ), 10, 1 );
+ remove_filter( 'groups_avatar_upload_dir', array( $this, 'filter_upload_dir' ), 10, 1 );
+ $this->upload_results = array();
+ $this->image_file = '';
+ }
+
+ public function filter_overrides( $overrides ) {
+ $overrides['upload_error_handler'] = array( $this, 'upload_error_handler' );
+
+ // Don't test upload for WordPress < 4.0
+ $overrides['test_upload'] = false;
+ return $overrides;
+ }
+
+ public function filter_upload_dir( $upload_dir ) {
+ $upload_dir['error'] = 'fake_upload_success';
+
+ $this->upload_results = array(
+ 'new_file' => $upload_dir['path'] . '/mystery-man.jpg',
+ 'url' => $upload_dir['url'] . '/mystery-man.jpg',
+ );
+
+ return $upload_dir;
+ }
+
+ /**
+ * To avoid copying files in tests, we're faking a succesfull uploads
+ * as soon as all the test_form have been executed in _wp_handle_upload
+ */
+ public function upload_error_handler( &$file, $message ) {
+ if ( 'fake_upload_success' !== $message ) {
+ return array( 'error' => $message );
+ } else {
+ return array(
+ 'file' => $this->upload_results['new_file'],
+ 'url' => $this->upload_results['url'],
+ 'type' => 'image/jpeg',
+ );
+ }
+ }
+
+ private function clean_files( $basedir = 'attachment_base_dir' ) {
+ $upload_dir = bp_upload_dir();
+
+ $this->rrmdir( $upload_dir['basedir'] . '/' . $basedir );
+ }
+
+ private function clean_avatars( $type = 'user' ) {
+ if ( 'user' === $type ) {
+ $avatar_dir = 'avatars';
+ } elseif ( 'group' === $type ) {
+ $avatar_dir = 'group-avatars';
+ }
+
+ $this->rrmdir( bp_core_avatar_upload_path() . '/' . $avatar_dir );
+ }
+
+ public function max_filesize() {
+ return 1000;
+ }
+
+ public function test_bp_attachment_construct_missing_required_parameter() {
+ $reset_files = $_FILES;
+ $reset_post = $_POST;
+
+ $_FILES['file'] = array(
+ 'name' => 'mystery-man.jpg',
+ 'type' => 'image/jpeg',
+ 'error' => 0,
+ 'size' => 1000
+ );
+
+ $attachment_class = new BPTest_Attachment_Extension();
+ $upload = $attachment_class->upload( $_FILES );
+
+ $this->assertTrue( empty( $upload ) );
+
+ $_FILES = $reset_files;
+ $_POST = $reset_post;
+ }
+
+ public function test_bp_attachment_set_upload_dir() {
+ $upload_dir = bp_upload_dir();
+
+ $attachment_class = new BPTest_Attachment_Extension( array(
+ 'action' => 'attachment_action',
+ 'file_input' => 'attachment_file_input'
+ ) );
+
+ $this->assertSame( $attachment_class->upload_dir, bp_upload_dir() );
+
+ $attachment_class = new BPTest_Attachment_Extension( array(
+ 'action' => 'attachment_action',
+ 'file_input' => 'attachment_file_input',
+ 'base_dir' => 'attachment_base_dir',
+ ) );
+
+ $this->assertTrue( file_exists( $upload_dir['basedir'] . '/attachment_base_dir' ) );
+
+ // clean up
+ $this->clean_files();
+ }
+
+ /**
+ * @group upload
+ */
+ public function test_bp_attachment_upload() {
+ $reset_files = $_FILES;
+ $reset_post = $_POST;
+
+ $attachment_class = new BPTest_Attachment_Extension( array(
+ 'action' => 'attachment_action',
+ 'file_input' => 'attachment_file_input',
+ 'base_dir' => 'attachment_base_dir',
+ 'original_max_filesize' => 1000,
+ ) );
+
+ $_POST['action'] = $attachment_class->action;
+ $_FILES[ $attachment_class->file_input ] = array(
+ 'tmp_name' => $this->image_file,
+ 'name' => 'mystery-man.jpg',
+ 'type' => 'image/jpeg',
+ 'error' => 0,
+ 'size' => filesize( $this->image_file ),
+ );
+
+ // Error: file size
+ $upload = $attachment_class->upload( $_FILES );
+ $this->assertFalse( empty( $upload['error'] ) );
+
+ $attachment_class->allowed_mime_types = array( 'pdf' );
+ $attachment_class->original_max_filesize = false;
+
+ // Error: file type
+ $upload = $attachment_class->upload( $_FILES );
+ $this->assertFalse( empty( $upload['error'] ) );
+
+ $attachment_class->allowed_mime_types = array();
+
+ // Success
+ $upload = $attachment_class->upload( $_FILES );
+ $this->assertEquals( $upload['file'], $attachment_class->upload_path . '/mystery-man.jpg' );
+
+ // clean up!
+ $_FILES = $reset_files;
+ $_POST = $reset_post;
+ $this->clean_files();
+ }
+
+ /**
+ * @group upload
+ * @group avatar
+ */
+ public function test_bp_attachment_avatar_user_upload() {
+ $reset_files = $_FILES;
+ $reset_post = $_POST;
+ $bp = buddypress();
+ $displayed_user = $bp->displayed_user;
+ $bp->displayed_user = new stdClass;
+
+ $u1 = $this->factory->user->create();
+ $bp->displayed_user->id = $u1;
+
+ // Upload the file
+ $avatar_attachment = new BP_Attachment_Avatar();
+ $_POST['action'] = $avatar_attachment->action;
+ $_FILES[ $avatar_attachment->file_input ] = array(
+ 'tmp_name' => $this->image_file,
+ 'name' => 'mystery-man.jpg',
+ 'type' => 'image/jpeg',
+ 'error' => 0,
+ 'size' => filesize( $this->image_file )
+ );
+
+ /* No error */
+ $user_avatar = $avatar_attachment->upload( $_FILES, 'xprofile_avatar_upload_dir' );
+ $this->assertEquals( $user_avatar['file'], $bp->avatar->upload_path . '/avatars/' . $u1 .'/mystery-man.jpg' );
+
+ /* File size error */
+ add_filter( 'bp_core_avatar_original_max_filesize', array( $this, 'max_filesize' ) );
+
+ $user_avatar = $avatar_attachment->upload( $_FILES, 'xprofile_avatar_upload_dir' );
+
+ remove_filter( 'bp_core_avatar_original_max_filesize', array( $this, 'max_filesize' ) );
+ $this->assertFalse( empty( $user_avatar['error'] ) );
+
+ /* File type error */
+ $_FILES[ $avatar_attachment->file_input ]['name'] = 'buddypress_logo.pdf';
+ $_FILES[ $avatar_attachment->file_input ]['type'] = 'application/pdf';
+
+ $user_avatar = $avatar_attachment->upload( $_FILES, 'xprofile_avatar_upload_dir' );
+ $this->assertFalse( empty( $user_avatar['error'] ) );
+
+ // clean up!
+ $bp->displayed_user = $displayed_user;
+ $this->clean_avatars();
+ $_FILES = $reset_files;
+ $_POST = $reset_post;
+ }
+
+ /**
+ * @group upload
+ * @group avatar
+ */
+ public function test_bp_attachment_avatar_group_upload() {
+ $bp = buddypress();
+ $reset_files = $_FILES;
+ $reset_post = $_POST;
+ $reset_current_group = $bp->groups->current_group;
+
+ $g = $this->factory->group->create();
+
+ $bp->groups->current_group = groups_get_group( array(
+ 'group_id' => $g,
+ 'populate_extras' => true,
+ ) );
+
+ // Upload the file
+ $avatar_attachment = new BP_Attachment_Avatar();
+ $_POST['action'] = $avatar_attachment->action;
+ $_FILES[ $avatar_attachment->file_input ] = array(
+ 'tmp_name' => $this->image_file,
+ 'name' => 'mystery-man.jpg',
+ 'type' => 'image/jpeg',
+ 'error' => 0,
+ 'size' => filesize( $this->image_file )
+ );
+
+ $group_avatar = $avatar_attachment->upload( $_FILES, 'groups_avatar_upload_dir' );
+ $this->assertEquals( $group_avatar['file'], $bp->avatar->upload_path . '/group-avatars/' . $g .'/mystery-man.jpg' );
+
+ // clean up!
+ $this->clean_avatars( 'group' );
+ $bp->groups->current_group = $reset_current_group;
+ $_FILES = $reset_files;
+ $_POST = $reset_post;
+ }
+
+ /**
+ * @group crop
+ */
+ public function test_bp_attachment_crop() {
+ $crop_args = array(
+ 'original_file' => $this->image_file,
+ 'crop_x' => 0,
+ 'crop_y' => 0,
+ 'crop_w' => 150,
+ 'crop_h' => 150,
+ 'dst_w' => 150,
+ 'dst_h' => 150,
+ );
+
+ $attachment_class = new BPTest_Attachment_Extension( array(
+ 'action' => 'attachment_action',
+ 'file_input' => 'attachment_file_input',
+ 'base_dir' => 'attachment_base_dir',
+ ) );
+
+ $cropped = $attachment_class->crop( $crop_args );
+
+ // Image must come from the upload basedir
+ $this->assertTrue( is_wp_error( $cropped ) );
+
+ $crop_args['original_file'] = $attachment_class->upload_path . '/mystery-man.jpg';
+
+ // Image must stay in the upload basedir
+ $crop_args['dst_file'] = BP_TESTS_DIR . 'assets/error.jpg';
+ $cropped = $attachment_class->crop( $crop_args );
+
+ // Image must stay in the upload basedir
+ $this->assertTrue( is_wp_error( $cropped ) );
+
+ // clean up!
+ $this->clean_files();
+ }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/tests/phpunit/testcases/core/class-bp-attachment.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="trunktestsphpunittestcasescorefunctionsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/testcases/core/functions.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/testcases/core/functions.php 2015-05-02 01:36:28 UTC (rev 9830)
+++ trunk/tests/phpunit/testcases/core/functions.php 2015-05-02 20:55:07 UTC (rev 9831)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -598,4 +598,26 @@
</span><span class="cx" style="display: block; padding: 0 10px"> date_default_timezone_set( $tz_backup );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ /**
+ * @group bp_attachments
+ * @group bp_upload_dir
+ */
+ public function test_bp_upload_dir_ms() {
+ if ( ! is_multisite() ) {
+ $this->markTestSkipped( __METHOD__ . ' is a multisite-only test.' );
+ }
+
+ $expected_upload_dir = wp_upload_dir();
+
+ $b = $this->factory->blog->create();
+
+ switch_to_blog( $b );
+
+ $tested_upload_dir = bp_upload_dir();
+
+ restore_current_blog();
+
+ $this->assertSame( $expected_upload_dir, $tested_upload_dir );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>
</body>
</html>