Make WordPress Core

Ticket #37208: 37208.2.diff

File 37208.2.diff, 3.9 KB (added by rachelbaker, 7 years ago)

Added unit test

  • src/wp-includes/comment.php

     
    10551055        $mod_keys = trim( get_option('blacklist_keys') );
    10561056        if ( '' == $mod_keys )
    10571057                return false; // If moderation keys are empty
     1058
     1059        // Ensure HTML tags are not being used to bypass the blacklist.
     1060        $comment_without_html = wp_kses( $comment, array() );
     1061
    10581062        $words = explode("\n", $mod_keys );
    10591063
    10601064        foreach ( (array) $words as $word ) {
     
    10721076                           preg_match($pattern, $author)
    10731077                        || preg_match($pattern, $email)
    10741078                        || preg_match($pattern, $url)
    1075                         || preg_match($pattern, $comment)
     1079                        || preg_match($pattern, $comment_without_html)
    10761080                        || preg_match($pattern, $user_ip)
    10771081                        || preg_match($pattern, $user_agent)
    10781082                 )
  • tests/phpunit/tests/comment/wpBlacklistCheck.php

     
     1<?php
     2
     3/**
     4 * @group comment
     5 */
     6class Tests_WP_Blacklist_Check extends WP_UnitTestCase {
     7
     8        public function test_should_return_true_when_content_matches_blacklist_keys() {
     9                $author       = 'Sting';
     10                $author_email = 'sting@example.com';
     11                $author_url   = 'http://example.com';
     12                $comment      = "There's a hole in my heart. As deep as a well. For that poor little boy. Who's stuck halfway to Hell.";
     13                $author_ip    = '192.168.0.1';
     14                $user_agent   = '';
     15
     16                update_option( 'blacklist_keys',"well\nfoo" );
     17
     18                $result = wp_blacklist_check( $author, $author_email, $author_url, $comment, $author_ip, $user_agent );
     19
     20                $this->assertTrue( $result );
     21        }
     22
     23        public function test_should_return_true_when_content_with_html_matches_blacklist_keys() {
     24                $author       = 'Sting';
     25                $author_email = 'sting@example.com';
     26                $author_url   = 'http://example.com';
     27                $comment      = "There's a hole in my heart. As deep as a well. For that poor little boy. Who's stuck <b>half</b>way to Hell.";
     28                $author_ip    = '192.168.0.1';
     29                $user_agent   = '';
     30
     31                update_option( 'blacklist_keys',"halfway\nfoo" );
     32
     33                $result = wp_blacklist_check( $author, $author_email, $author_url, $comment, $author_ip, $user_agent );
     34
     35                $this->assertTrue( $result );
     36        }
     37
     38        public function test_should_return_true_when_author_matches_blacklist_keys() {
     39                $author       = 'Sideshow Mel';
     40                $author_email = 'mel@example.com';
     41                $author_url   = 'http://example.com';
     42                $comment      = "Though we can't get him out. We'll do the next best thing.";
     43                $author_ip    = '192.168.0.1';
     44                $user_agent   = '';
     45
     46                update_option( 'blacklist_keys',"sideshow\nfoo" );
     47
     48                $result = wp_blacklist_check( $author, $author_email, $author_url, $comment, $author_ip, $user_agent );
     49
     50                $this->assertTrue( $result );
     51        }
     52
     53        public function test_should_return_true_when_url_matches_blacklist_keys() {
     54                $author       = 'Rainier Wolfcastle';
     55                $author_email = 'rainier@wolfcastle.com';
     56                $author_url   = 'http://example.com';
     57                $comment      = 'We go on TV and sing, sing, sing.';
     58                $author_ip    = '192.168.0.1';
     59                $user_agent   = '';
     60
     61                update_option( 'blacklist_keys',"example\nfoo" );
     62
     63                $result = wp_blacklist_check( $author, $author_email, $author_url, $comment, $author_ip, $user_agent );
     64
     65                $this->assertTrue( $result );
     66        }
     67
     68        public function test_should_return_false_when_no_match() {
     69                $author       = 'Krusty the Clown';
     70                $author_email = 'krusty@example.com';
     71                $author_url   = 'http://example.com';
     72                $comment      = "And we're sending our love down the well.";
     73                $author_ip    = '192.168.0.1';
     74                $user_agent   = '';
     75
     76                update_option( 'blacklist_keys',"sideshow\nfoobar" );
     77
     78                $result = wp_blacklist_check( $author, $author_email, $author_url, $comment, $author_ip, $user_agent );
     79
     80                $this->assertFalse( $result );
     81        }
     82}