Skip to:
Content

BuddyPress.org

Changeset 13986


Ignore:
Timestamp:
07/27/2024 07:16:17 PM (11 months ago)
Author:
espellcaste
Message:

Allow creating messages with non-empty content.

Use of the PHP empty function to check if the message content is empty is not ideal when creating a message. Update the logic to consider any string as non-empty.

Props emaralive, niftythree.

See #9175
Closes https://github.com/buddypress/buddypress/pull/330

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-messages/bp-messages-functions.php

    r13890 r13986  
    5757    );
    5858
     59    // Check if the message content is empty.
     60    $content       = $r['content'];
     61    $empty_content = false;
     62
     63    // Any string is considered non-empty.
     64    if ( ! is_string( $content ) || '' === $content ) {
     65        $empty_content = true;
     66    }
     67
    5968    // Bail if no sender or no content.
    60     if ( empty( $r['sender_id'] ) || empty( $r['content'] ) ) {
     69    if ( empty( $r['sender_id'] ) || $empty_content ) {
    6170        if ( 'wp_error' === $r['error_type'] ) {
    6271            if ( empty( $r['sender_id'] ) ) {
     
    6978
    7079            return new WP_Error( $error_code, $feedback );
    71 
    72         } else {
    73             return false;
    74         }
     80        }
     81
     82        return false;
    7583    }
    7684
  • trunk/tests/phpunit/testcases/messages/functions.php

    r13980 r13986  
    2727
    2828        // send another message and get recheck unread count
    29         $t2 = messages_new_message( array(
     29        messages_new_message( array(
    3030            'sender_id'  => $u1,
    3131            'recipients' => array( $u2 ),
     
    4040        // recheck unread count
    4141        $this->assertEquals( 1, messages_get_unread_count( $u2 ) );
     42    }
     43
     44    /**
     45     * @dataProvider provider_new_message_empty_content_options
     46     *
     47     * @ticket BP9175
     48     * @group messages_new_message
     49     */
     50    public function test_messages_new_message_empty_content( $content ) {
     51        $t1 = messages_new_message(
     52            array(
     53                'sender_id'  => self::factory()->user->create(),
     54                'recipients' => array( self::factory()->user->create() ),
     55                'subject'    => 'A new message',
     56                'content'    => $content,
     57                'error_type' => 'wp_error'
     58            )
     59        );
     60
     61        $this->assertSame(
     62            'Your message was not sent. Please enter some content.',
     63            $t1->get_error_message()
     64        );
     65    }
     66
     67    /**
     68     * @dataProvider provider_new_message_irregular_content_options
     69     *
     70     * @ticket BP9175
     71     * @group messages_new_message
     72     */
     73    public function test_messages_new_message_irregular_content( $content ) {
     74        $t1 = messages_new_message( array(
     75            'sender_id'  => self::factory()->user->create(),
     76            'recipients' => array( self::factory()->user->create() ),
     77            'subject'    => 'A new message',
     78            'content'    => $content,
     79            'error_type' => 'wp_error'
     80        ) );
     81
     82        $this->assertTrue( is_int( $t1 ) );
     83    }
     84
     85    /**
     86     * Provider for the test_messages_new_message_irregular_content_options() test.
     87     *
     88     * @return array
     89     */
     90    public function provider_new_message_irregular_content_options() {
     91        return array(
     92            array( '0' ),
     93            array( '00' ),
     94            array( 'false' ),
     95        );
     96    }
     97
     98    /**
     99     * Provider for the test_messages_new_message_empty_content() test.
     100     *
     101     * @return array
     102     */
     103    public function provider_new_message_empty_content_options() {
     104        return array(
     105            array( '' ),
     106            array( "" ),
     107            array( null ),
     108            array( false ),
     109            array( 0 ), // '0' is a valid message content.
     110            array( array() ),
     111            array( new stdClass() ),
     112        );
    42113    }
    43114
Note: See TracChangeset for help on using the changeset viewer.