Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
10/02/2024 01:34:07 PM (20 months ago)
Author:
espellcaste
Message:

BP REST API: Anonymise message data for deleted users.

Messages from deleted users are anonymised properly.

Props niftythree.

See #9160

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/testcases/messages/test-controller.php

    r14035 r14037  
    337337
    338338    /**
     339     * @ticket BP9160
     340     * @group get_item
     341     */
     342    public function test_get_thread_deleted_messages() {
     343        $u1           = static::factory()->user->create();
     344        $deleted_user = static::factory()->user->create();
     345        $m            = $this->bp::factory()->message->create_and_get(
     346            array(
     347                'sender_id'  => $deleted_user,
     348                'recipients' => array( $u1 ),
     349                'subject'    => 'Foo',
     350                'content'    => 'Content',
     351            )
     352        );
     353
     354        $this->bp::factory()->message->create(
     355            array(
     356                'thread_id'  => $m->thread_id,
     357                'sender_id'  => $u1,
     358                'recipients' => array( $deleted_user ),
     359                'content'    => 'Bar',
     360            )
     361        );
     362
     363        // Delete user.
     364        if ( is_multisite() ) {
     365            wpmu_delete_user( $deleted_user );
     366        } else {
     367            wp_delete_user( $deleted_user );
     368        }
     369
     370        $this->bp::set_current_user( $u1 );
     371
     372        $request = new WP_REST_Request( 'GET', $this->endpoint_url . '/' . $m->thread_id );
     373        $request->set_param( 'context', 'view' );
     374        $response = $this->server->dispatch( $request );
     375
     376        $this->assertEquals( 200, $response->get_status() );
     377
     378        $all_data          = $response->get_data();
     379        $deleted_recipient = array_values(
     380            wp_filter_object_list(
     381                $all_data['recipients'],
     382                array( 'user_id' => $deleted_user ),
     383                'AND',
     384                'is_deleted'
     385            )
     386        );
     387
     388        $this->assertSame( $deleted_user, $all_data['last_sender_id'] );
     389        $this->assertContains( $deleted_user, $all_data['sender_ids'] );
     390        $this->assertStringContainsString( '<p>[deleted]</p>', $all_data['message']['rendered'] );
     391        $this->assertStringContainsString( '[deleted]', $all_data['excerpt']['rendered'] );
     392        $this->assertTrue( $deleted_recipient[0] );
     393        $this->assertCount( 2, $all_data['recipients'] );
     394    }
     395
     396    /**
    339397     * @group get_item
    340398     */
     
    13801438            }
    13811439        }
    1382     }
    1383 
    1384     /**
    1385      * @group prepare_links
    1386      */
    1387     public function test_prepare_add_links_to_response() {
    1388         $this->markTestSkipped();
    1389 
    1390         $u1 = static::factory()->user->create();
    1391         $u2 = static::factory()->user->create();
    1392         $m1 = $this->bp::factory()->message->create_and_get(
    1393             array(
    1394                 'sender_id'  => $u1,
    1395                 'recipients' => array( $u2 ),
    1396                 'subject'    => 'Bar',
    1397                 'content'    => 'Content',
    1398             )
    1399         );
    1400 
    1401         $r1 = $this->bp::factory()->message->create_and_get(
    1402             array(
    1403                 'thread_id' => $m1->thread_id,
    1404                 'sender_id' => $u2,
    1405                 'content'   => 'Reply',
    1406             )
    1407         );
    1408 
    1409         $this->bp::set_current_user( $u2 );
    1410 
    1411         $request = new WP_REST_Request( 'GET', $this->endpoint_url . '/' . $m1->thread_id );
    1412         $request->set_param( 'context', 'view' );
    1413         $response = $this->server->dispatch( $request );
    1414 
    1415         $this->assertEquals( 200, $response->get_status() );
    1416 
    1417         $get_links = $response->get_data();
    1418 
    1419         $this->assertNotEmpty( $get_links );
    1420 
    1421         $links = $get_links['_links'];
    1422 
    1423         $this->assertEquals( rest_url( $this->endpoint_url . '/' ), $links['collection'][0]['href'] );
    1424         $this->assertEquals( rest_url( $this->endpoint_url . '/' . $m1->thread_id ), $links['self'][0]['href'] );
    1425         $this->assertEquals( rest_url( $this->endpoint_url . '/' . bp_get_messages_starred_slug() . '/' . $m1->id ), $links[ $m1->id ][0]['href'] );
    1426         $this->assertEquals( rest_url( $this->endpoint_url . '/' . bp_get_messages_starred_slug() . '/' . $r1->id ), $links[ $r1->id ][0]['href'] );
    14271440    }
    14281441
     
    14461459        $this->assertEquals( bp_rest_prepare_date_response( $thread->last_message_date ), $data['date_gmt'] );
    14471460        $this->assertEquals( $thread->unread_count, $data['unread_count'] );
    1448         $this->assertEquals( $thread->sender_ids, $data['sender_ids'] );
     1461        $this->assertEquals( array_values( $thread->sender_ids ), $data['sender_ids'] );
    14491462    }
    14501463
Note: See TracChangeset for help on using the changeset viewer.