Make WordPress Core

Ticket #31108: tests_phpunit_tests_comment.php.3.patch

File tests_phpunit_tests_comment.php.3.patch, 4.1 KB (added by CalEvans, 11 years ago)

Tightend up the code a bit. This is the last version.

  • tests/phpunit/tests/comment.php

     
    7272
    7373                $this->assertSame( array(), $found );
    7474        }
     75
     76    public function test_check_comment() {
     77        /*
     78         * Setup
     79         */
     80        global $wpdb;
     81        $post_args = array( 'post_title' => 'some-post', 'post_type' => 'post' );
     82        $post      = $this->factory->post->create_and_get( $post_args );
     83        $comment_args = array( 'comment_post_ID'      => $post->ID,
     84                               'comment_approved'     => '0',
     85                               'comment_content'      => 'This is a test comment.' ,
     86                               'comment_approved'     =>0,
     87                               'comment_author_email' => 'bob@example.com',
     88                               'comment_author' => 'BobtheBuilder' );
     89
     90
     91        /*
     92         *  Test #1 Comment Whitelisting
     93         */
     94        update_option('comment_whitelist',0);
     95        $results = check_comment('BobtheBuilder', 'bob@example.com', 'http://example.com', 'This is a test comment.' , '192.168.0.1', '', '');
     96        $this->assertTrue($results);
     97        update_option('comment_whitelist',1);
     98        $results = check_comment('BobtheBuilder', 'bob@example.com', 'http://example.com', 'This is a test comment.' , '192.168.0.1', '', '');
     99        $this->assertFalse($results);
     100
     101
     102        /*
     103         * Test 2: Must have at least 2 approved comments
     104         */
     105        $comment_ids = $this->factory->comment->create_post_comments($post->ID,5, $comment_args );
     106        $results = check_comment('BobtheBuilder', 'bob@example.com', 'http://example.com', 'This is a test comment.' , '192.168.0.1', '', '');
     107        $this->assertFalse($results);
     108
     109        for($lcvA=0;$lcvA<count($comment_ids);$lcvA++) {
     110            $comment = get_comment($comment_ids[$lcvA]);
     111            $comment->comment_approved=1;
     112            wp_update_comment((array)$comment);
     113        }
     114        $results = check_comment('BobtheBuilder', 'bob@example.com', 'http://example.com', 'This is a test comment.' , '192.168.0.1', '', '');
     115        $this->assertTrue($results);
     116       
     117
     118        /*
     119         *  Test #3 Comment Moderation
     120         */
     121        update_option('comment_moderation',1);
     122        $results = check_comment('BobtheBuilder', 'bob@example.com', 'http://example.com', 'This is a test comment.' , '192.168.0.1', '', '');
     123        $this->assertFalse($results);
     124        update_option('comment_moderation',0);
     125        $results = check_comment('BobtheBuilder', 'bob@example.com', 'http://example.com', 'This is a test comment.' , '192.168.0.1', '', '');
     126        $this->assertTrue($results);
     127
     128
     129        /*
     130         * Test #4 Number of comment links
     131         */
     132         $max_links = get_option( 'comment_max_links' );
     133         $comment_content = $comment_args['comment_content'] . "\n";
     134         for($lcvA=0;$lcvA<$max_links;$lcvA++) {
     135            $comment_content .= '<a href="'.home_url( '?p=' . rand() ) . '">Click Me!</a>' . "\n";
     136         }
     137        $results = check_comment('BobtheBuilder', 'bob@example.com', 'http://example.com', $comment_content , '192.168.0.1', '', '');
     138        $this->assertFalse($results);
     139        update_option('comment_max_links',5);
     140        $results = check_comment('BobtheBuilder', 'bob@example.com', 'http://example.com', $comment_content , '192.168.0.1', '', '');
     141        $this->assertTrue($results);
     142
     143
     144        /*
     145         * Test #5 Moderated keywords
     146         */
     147        update_option('moderation_keys',"poop\nhiney\ncomment");
     148        $results = check_comment('BobtheBuilder', 'bob@example.com', 'http://example.com', 'This is a test comment.' , '192.168.0.1', '', '');
     149        $this->assertFalse($results);
     150        update_option('moderation_keys',"poop\nhiney\n");
     151        $results = check_comment('BobtheBuilder', 'bob@example.com', 'http://example.com', 'This is a test comment.' , '192.168.0.1', '', '');
     152        $this->assertTrue($results);
     153
     154        return;
     155
     156    }
     157
    75158}
     159