Make WordPress Core


Ignore:
Timestamp:
02/13/2023 06:56:59 PM (4 years ago)
Author:
SergeyBiryukov
Message:

Tests: Move wp_comments_personal_data_eraser() tests to their own file.

This aims to make the tests more discoverable and easier to expand.

Follow-up to [42994].

See #56793.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/comment.php

    r54949 r55319  
    11431143        }
    11441144
    1145         /**
    1146          * The `wp_comments_personal_data_eraser()` function should erase user's comments.
    1147          *
    1148          * @group privacy
    1149          * @ticket 43442
    1150          *
    1151          * @covers ::wp_comments_personal_data_eraser
    1152          */
    1153         public function test_wp_comments_personal_data_eraser() {
    1154 
    1155                 $post_id = self::factory()->post->create();
    1156                 $user_id = self::factory()->user->create();
    1157 
    1158                 $args       = array(
    1159                         'user_id'              => $user_id,
    1160                         'comment_post_ID'      => $post_id,
    1161                         'comment_author'       => 'Comment Author',
    1162                         'comment_author_email' => 'personal@local.host',
    1163                         'comment_author_url'   => 'https://local.host/',
    1164                         'comment_author_IP'    => '192.168.0.1',
    1165                         'comment_date'         => '2018-04-14 17:20:00',
    1166                         'comment_agent'        => 'COMMENT_AGENT',
    1167                         'comment_content'      => 'Comment Content',
    1168                 );
    1169                 $comment_id = self::factory()->comment->create( $args );
    1170 
    1171                 wp_comments_personal_data_eraser( $args['comment_author_email'] );
    1172 
    1173                 $comment = get_comment( $comment_id );
    1174 
    1175                 $actual = array(
    1176                         'comment_ID'           => $comment->comment_ID,
    1177                         'user_id'              => $comment->user_id,
    1178                         'comment_author'       => $comment->comment_author,
    1179                         'comment_author_email' => $comment->comment_author_email,
    1180                         'comment_author_url'   => $comment->comment_author_url,
    1181                         'comment_author_IP'    => $comment->comment_author_IP,
    1182                         'comment_date'         => $comment->comment_date,
    1183                         'comment_date_gmt'     => $comment->comment_date_gmt,
    1184                         'comment_agent'        => $comment->comment_agent,
    1185                         'comment_content'      => $comment->comment_content,
    1186                 );
    1187 
    1188                 $expected = array(
    1189                         'comment_ID'           => (string) $comment_id,
    1190                         'user_id'              => '0', // Anonymized.
    1191                         'comment_author'       => 'Anonymous', // Anonymized.
    1192                         'comment_author_email' => '', // Anonymized.
    1193                         'comment_author_url'   => '', // Anonymized.
    1194                         'comment_author_IP'    => '192.168.0.0', // Anonymized.
    1195                         'comment_date'         => '2018-04-14 17:20:00',
    1196                         'comment_date_gmt'     => '2018-04-14 17:20:00',
    1197                         'comment_agent'        => '', // Anonymized.
    1198                         'comment_content'      => 'Comment Content',
    1199                 );
    1200 
    1201                 $this->assertSame( $expected, $actual );
    1202         }
    1203 
    1204         /**
    1205          * Testing the `wp_comments_personal_data_eraser()` function's output on an empty first page.
    1206          *
    1207          * @group privacy
    1208          * @ticket 43442
    1209          *
    1210          * @covers ::wp_comments_personal_data_eraser
    1211          */
    1212         public function test_wp_comments_personal_data_eraser_empty_first_page_output() {
    1213 
    1214                 $actual   = wp_comments_personal_data_eraser( 'nocommentsfound@local.host' );
    1215                 $expected = array(
    1216                         'items_removed'  => false,
    1217                         'items_retained' => false,
    1218                         'messages'       => array(),
    1219                         'done'           => true,
    1220                 );
    1221 
    1222                 $this->assertSame( $expected, $actual );
    1223         }
    1224 
    1225         /**
    1226          * Testing the `wp_comments_personal_data_eraser()` function's output, for the non-empty first page.
    1227          *
    1228          * @group privacy
    1229          * @ticket 43442
    1230          *
    1231          * @covers ::wp_comments_personal_data_eraser
    1232          */
    1233         public function test_wp_comments_personal_data_eraser_non_empty_first_page_output() {
    1234 
    1235                 $post_id = self::factory()->post->create();
    1236                 $args    = array(
    1237                         'comment_post_ID'      => $post_id,
    1238                         'comment_author'       => 'Comment Author',
    1239                         'comment_author_email' => 'personal@local.host',
    1240                         'comment_author_url'   => 'https://local.host/',
    1241                         'comment_author_IP'    => '192.168.0.1',
    1242                         'comment_date'         => '2018-04-14 17:20:00',
    1243                         'comment_agent'        => 'COMMENT_AGENT',
    1244                         'comment_content'      => 'Comment Content',
    1245                 );
    1246                 self::factory()->comment->create( $args );
    1247 
    1248                 $actual   = wp_comments_personal_data_eraser( $args['comment_author_email'] );
    1249                 $expected = array(
    1250                         'items_removed'  => true,
    1251                         'items_retained' => false,
    1252                         'messages'       => array(),
    1253                         'done'           => true,
    1254                 );
    1255 
    1256                 $this->assertSame( $expected, $actual );
    1257         }
    1258 
    1259         /**
    1260          * Testing the `wp_comments_personal_data_eraser()` function's output, for an empty second page.
    1261          *
    1262          * @group privacy
    1263          * @ticket 43442
    1264          *
    1265          * @covers ::wp_comments_personal_data_eraser
    1266          */
    1267         public function test_wp_comments_personal_data_eraser_empty_second_page_output() {
    1268 
    1269                 $post_id = self::factory()->post->create();
    1270                 $args    = array(
    1271                         'comment_post_ID'      => $post_id,
    1272                         'comment_author'       => 'Comment Author',
    1273                         'comment_author_email' => 'personal@local.host',
    1274                         'comment_author_url'   => 'https://local.host/',
    1275                         'comment_author_IP'    => '192.168.0.1',
    1276                         'comment_date'         => '2018-04-14 17:20:00',
    1277                         'comment_agent'        => 'COMMENT_AGENT',
    1278                         'comment_content'      => 'Comment Content',
    1279                 );
    1280                 self::factory()->comment->create( $args );
    1281 
    1282                 $actual   = wp_comments_personal_data_eraser( $args['comment_author_email'], 2 );
    1283                 $expected = array(
    1284                         'items_removed'  => false,
    1285                         'items_retained' => false,
    1286                         'messages'       => array(),
    1287                         'done'           => true,
    1288                 );
    1289 
    1290                 $this->assertSame( $expected, $actual );
    1291         }
    1292 
    1293         /**
    1294          * Testing the `wp_anonymize_comment` filter, to prevent comment anonymization.
    1295          *
    1296          * @group privacy
    1297          * @ticket 43442
    1298          *
    1299          * @covers ::wp_comments_personal_data_eraser
    1300          */
    1301         public function test_wp_anonymize_comment_filter_to_prevent_comment_anonymization() {
    1302 
    1303                 $post_id    = self::factory()->post->create();
    1304                 $args       = array(
    1305                         'comment_post_ID'      => $post_id,
    1306                         'comment_author'       => 'Comment Author',
    1307                         'comment_author_email' => 'personal@local.host',
    1308                         'comment_author_url'   => 'https://local.host/',
    1309                         'comment_author_IP'    => '192.168.0.1',
    1310                         'comment_date'         => '2018-04-14 17:20:00',
    1311                         'comment_agent'        => 'COMMENT_AGENT',
    1312                         'comment_content'      => 'Comment Content',
    1313                 );
    1314                 $comment_id = self::factory()->comment->create( $args );
    1315 
    1316                 add_filter( 'wp_anonymize_comment', '__return_false' );
    1317                 $actual = wp_comments_personal_data_eraser( $args['comment_author_email'] );
    1318                 remove_filter( 'wp_anonymize_comment', '__return_false' );
    1319 
    1320                 $message = sprintf( 'Comment %d contains personal data but could not be anonymized.', $comment_id );
    1321 
    1322                 $expected = array(
    1323                         'items_removed'  => false,
    1324                         'items_retained' => true,
    1325                         'messages'       => array( $message ),
    1326                         'done'           => true,
    1327                 );
    1328 
    1329                 $this->assertSame( $expected, $actual );
    1330         }
    1331 
    1332         /**
    1333          * Testing the `wp_anonymize_comment` filter, to prevent comment anonymization, with a custom message.
    1334          *
    1335          * @group privacy
    1336          * @ticket 43442
    1337          *
    1338          * @covers ::wp_comments_personal_data_eraser
    1339          */
    1340         public function test_wp_anonymize_comment_filter_to_prevent_comment_anonymization_with_custom_message() {
    1341 
    1342                 $post_id    = self::factory()->post->create();
    1343                 $args       = array(
    1344                         'comment_post_ID'      => $post_id,
    1345                         'comment_author'       => 'Comment Author',
    1346                         'comment_author_email' => 'personal@local.host',
    1347                         'comment_author_url'   => 'https://local.host/',
    1348                         'comment_author_IP'    => '192.168.0.1',
    1349                         'comment_date'         => '2018-04-14 17:20:00',
    1350                         'comment_agent'        => 'COMMENT_AGENT',
    1351                         'comment_content'      => 'Comment Content',
    1352                 );
    1353                 $comment_id = self::factory()->comment->create( $args );
    1354 
    1355                 add_filter( 'wp_anonymize_comment', array( $this, 'wp_anonymize_comment_custom_message' ), 10, 3 );
    1356                 $actual = wp_comments_personal_data_eraser( $args['comment_author_email'] );
    1357                 remove_filter( 'wp_anonymize_comment', array( $this, 'wp_anonymize_comment_custom_message' ) );
    1358 
    1359                 $message = sprintf( 'Some custom message for comment %d.', $comment_id );
    1360 
    1361                 $expected = array(
    1362                         'items_removed'  => false,
    1363                         'items_retained' => true,
    1364                         'messages'       => array( $message ),
    1365                         'done'           => true,
    1366                 );
    1367 
    1368                 $this->assertSame( $expected, $actual );
    1369         }
    1370 
    1371         /**
    1372          * Callback for the `wp_anonymize_comment` filter.
    1373          *
    1374          * @param  bool|string $anonymize          Whether to apply the comment anonymization (bool).
    1375          *                                         Custom prevention message (string). Default true.
    1376          * @param  WP_Comment  $comment            WP_Comment object.
    1377          * @param  array       $anonymized_comment Anonymized comment data.
    1378          * @return string
    1379          */
    1380         public function wp_anonymize_comment_custom_message( $anonymize, $comment, $anonymized_comment ) {
    1381                 return sprintf( 'Some custom message for comment %d.', $comment->comment_ID );
    1382         }
    1383 
    13841145        public function test_update_should_invalidate_comment_cache() {
    13851146                global $wpdb;
Note: See TracChangeset for help on using the changeset viewer.