Skip to:
Content

BuddyPress.org

Changeset 14035


Ignore:
Timestamp:
10/02/2024 04:13:14 AM (19 months ago)
Author:
espellcaste
Message:

BP REST API: Allow creating messages with non-empty content.

Make sure it is possible to create messages with non-empty content in the BP REST API. This is a followup from [13986].

Props imath and emaralive.

See #9145
Fixes #9175

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-messages/classes/class-bp-messages-rest-controller.php

    r14026 r14035  
    340340     */
    341341    public function create_item( $request ) {
     342        $create_args = $this->prepare_item_for_database( $request );
     343
     344        // Let's return the original error if possible.
     345        $create_args->error_type = 'wp_error';
     346
    342347        // Create the message or the reply.
    343         $thread_id = messages_new_message( $this->prepare_item_for_database( $request ) );
     348        $thread_id = messages_new_message( $create_args );
    344349
    345350        // Validate it created a Thread or was added to it.
    346         if ( false === $thread_id ) {
     351        if ( $thread_id instanceof WP_Error ) {
    347352            return new WP_Error(
    348353                'bp_rest_messages_create_failed',
    349                 __( 'There was an error trying to create the message.', 'buddypress' ),
    350                 array(
    351                     'status' => 500,
    352                 )
     354                $thread_id->get_error_message(),
     355                array( 'status' => 500 )
    353356            );
    354357        }
     
    780783        }
    781784
    782         if ( ! empty( $schema['properties']['message'] ) && ! empty( $request->get_param( 'message' ) ) ) {
     785        if ( ! empty( $thread->message ) ) {
     786            $prepared_thread->message = $thread->message;
     787        } elseif ( ! empty( $schema['properties']['message'] ) ) {
    783788            $prepared_thread->content = $request->get_param( 'message' );
    784         } elseif ( ! empty( $thread->message ) ) {
    785             $prepared_thread->message = $thread->message;
    786789        }
    787790
  • trunk/src/bp-messages/classes/class-bp-messages-thread.php

    r13878 r14035  
    5252
    5353    /**
     54     * The user ID of the sender of the message thread.
     55     *
     56     * @since 1.2.0
     57     * @var int
     58     */
     59    public $sender_id;
     60
     61    /**
    5462     * The user IDs of all messages in the message thread.
    5563     *
     
    119127     *
    120128     * @since 1.0.0
    121      * @since 10.0.0 Updated the `$args` with new paremeters.
     129     * @since 10.0.0 Updated the `$args` with new parameters.
    122130     *
    123131     * @param int    $thread_id          The message thread ID.
     
    149157     *
    150158     * @since 1.0.0
    151      * @since 10.0.0 Updated the `$args` with new paremeters.
     159     * @since 10.0.0 Updated the `$args` with new parameters.
    152160     *
    153161     * @param int    $thread_id                   The message thread ID.
     
    518526
    519527        $thread_id = (int) $thread_id;
    520         $user_id = (int) $user_id;
     528        $user_id   = (int) $user_id;
    521529
    522530        if ( empty( $user_id ) ) {
     
    733741        );
    734742
    735         $pag_sql = $type_sql = $search_sql = $user_id_sql = $sender_sql = '';
     743        $pag_sql        = $type_sql = $search_sql = $user_id_sql = $sender_sql = '';
    736744        $meta_query_sql = array(
    737745            'join'  => '',
     
    740748
    741749        if ( $r['limit'] && $r['page'] ) {
    742             $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $r['page'] - 1 ) * $r['limit'] ), intval( $r['limit'] ) );
     750            $pag_sql = $wpdb->prepare( ' LIMIT %d, %d', intval( ( $r['page'] - 1 ) * $r['limit'] ), intval( $r['limit'] ) );
    743751        }
    744752
    745753        if ( $r['type'] == 'unread' ) {
    746             $type_sql = " AND r.unread_count != 0 ";
     754            $type_sql = ' AND r.unread_count != 0 ';
    747755        } elseif ( $r['type'] == 'read' ) {
    748             $type_sql = " AND r.unread_count = 0 ";
     756            $type_sql = ' AND r.unread_count = 0 ';
    749757        }
    750758
    751759        if ( ! empty( $r['search_terms'] ) ) {
    752760            $search_terms_like = '%' . bp_esc_like( $r['search_terms'] ) . '%';
    753             $search_sql        = $wpdb->prepare( "AND ( subject LIKE %s OR message LIKE %s )", $search_terms_like, $search_terms_like );
     761            $search_sql        = $wpdb->prepare( 'AND ( subject LIKE %s OR message LIKE %s )', $search_terms_like, $search_terms_like );
    754762        }
    755763
     
    788796
    789797        // Set up SQL array.
    790         $sql = array();
     798        $sql           = array();
    791799        $sql['select'] = 'SELECT m.thread_id, MAX(m.date_sent) AS date_sent';
    792800        $sql['from']   = "FROM {$bp->messages->table_name_recipients} r INNER JOIN {$bp->messages->table_name_messages} m ON m.thread_id = r.thread_id {$meta_query_sql['join']}";
  • trunk/tests/phpunit/testcases/members/test-controller.php

    r14034 r14035  
    571571        $this->assertTrue( bp_is_user_spammer( $u ) );
    572572
    573         wp_set_current_user( $u2 );
     573        $this->bp::set_current_user( $u2 );
    574574
    575575        $request = new WP_REST_Request( 'GET', sprintf( $this->endpoint_url . '/%d', $u ) );
     
    591591        $this->assertTrue( bp_is_user_spammer( $u ) );
    592592
    593         wp_set_current_user( $u );
     593        $this->bp::set_current_user( $u );
    594594
    595595        $request = new WP_REST_Request( 'GET', sprintf( $this->endpoint_url . '/%d', $u ) );
     
    611611        $this->assertTrue( bp_is_user_spammer( $u ) );
    612612
    613         wp_set_current_user( $this->user );
     613        $this->bp::set_current_user( $this->user );
    614614
    615615        $request = new WP_REST_Request( 'GET', sprintf( $this->endpoint_url . '/%d', $u ) );
  • trunk/tests/phpunit/testcases/messages/test-controller.php

    r14026 r14035  
    424424
    425425    /**
     426     * @dataProvider provider_create_item_with_empty_content_options
     427     *
     428     * @ticket BP9175
    426429     * @group create_item
    427430     */
    428     public function test_create_item_with_no_content() {
     431    public function test_create_item_with_empty_content_options( $content ) {
    429432        $this->bp::set_current_user( $this->user );
    430433
     
    435438                'sender_id'  => $this->user,
    436439                'recipients' => array( static::factory()->user->create() ),
    437                 'subject'    => 'Foo',
     440                'subject'    => 'A new message',
     441                'message'    => $content,
     442            )
     443        );
     444
     445        $response = $this->server->dispatch( $request );
     446
     447        $this->assertErrorResponse( 'bp_rest_messages_create_failed', $response, 500 );
     448        $this->assertSame( 'Your message was not sent. Please enter some content.', $response->get_data()['message'] );
     449    }
     450
     451    /**
     452     * @ticket BP9175
     453     * @group create_item
     454     */
     455    public function test_create_item_with_null_content() {
     456        $this->bp::set_current_user( $this->user );
     457
     458        $request = new WP_REST_Request( 'POST', $this->endpoint_url );
     459        $request->set_param( 'context', 'edit' );
     460        $request->set_query_params(
     461            array(
     462                'sender_id'  => $this->user,
     463                'recipients' => array( static::factory()->user->create() ),
     464                'subject'    => 'A new message',
     465                'message'    => null,
    438466            )
    439467        );
     
    447475
    448476    /**
     477     * @dataProvider provider_create_item_irregular_content_options
     478     *
     479     * @ticket BP9175
    449480     * @group create_item
    450481     */
    451     public function test_create_item_with_no_receipts() {
     482    public function test_create_item_with_irregular_content_options( $content ) {
     483        $this->bp::set_current_user( $this->user );
     484
     485        $request = new WP_REST_Request( 'POST', $this->endpoint_url );
     486        $request->set_param( 'context', 'edit' );
     487        $request->set_query_params(
     488            array(
     489                'sender_id'  => $this->user,
     490                'recipients' => array( static::factory()->user->create() ),
     491                'subject'    => 'A new message',
     492                'message'    => $content,
     493            )
     494        );
     495
     496        $response = $this->server->dispatch( $request );
     497
     498        $this->assertEquals( 200, $response->get_status() );
     499    }
     500
     501    /**
     502     * Provider for the test_create_item_with_empty_content_options() test.
     503     *
     504     * @return array
     505     */
     506    public function provider_create_item_with_empty_content_options() {
     507        return array(
     508            array( '' ),
     509            array( '' ),
     510            array( false ),
     511            array( 0 ), // '0' is a valid message content.
     512            array( array() ),
     513        );
     514    }
     515
     516    /**
     517     * Provider for the test_create_item_with_irregular_content_options() test.
     518     *
     519     * @return array
     520     */
     521    public function provider_create_item_irregular_content_options() {
     522        return array(
     523            array( '0' ),
     524            array( '00' ),
     525            array( 'false' ),
     526        );
     527    }
     528
     529    /**
     530     * @group create_item
     531     */
     532    public function test_create_item_with_no_recipients() {
    452533        $this->bp::set_current_user( $this->user );
    453534
Note: See TracChangeset for help on using the changeset viewer.