Make WordPress Core

Ticket #12668: 12668-comments-query-new-tests.patch

File 12668-comments-query-new-tests.patch, 7.1 KB (added by dancameron, 10 years ago)

New tests for comments query and get_approved_comments prior to any patch.

  • tests/phpunit/tests/comment/query.php

     
    1616        }
    1717
    1818        /**
     19         * Comments query
     20         * @ticket 12668
     21         */
     22        public function test_query() {
     23                $c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1' ) );
     24                $c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ) );
     25                $c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ) );
     26                $c4 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'mario' ) );
     27                $c5 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'luigi' ) );
     28
     29                $q = new WP_Comment_Query();
     30                $found = $q->query( array(
     31                        'fields' => 'ids',
     32                ) );
     33
     34                $this->assertEquals( array( $c1, $c2, $c3, $c4, $c5 ), $found );
     35        }
     36
     37        /**
     38         * Query comments with blank
     39         * @ticket 12668
     40         */
     41        public function test_query_for___type() {
     42                $c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1' ) );
     43                $c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ) );
     44                $c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ) );
     45                $c4 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'mario' ) );
     46                $c5 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'luigi' ) );
     47
     48                $q = new WP_Comment_Query();
     49                $found = $q->query( array(
     50                        'type' => '',
     51                        'fields' => 'ids',
     52                ) );
     53
     54                $this->assertEquals( array( $c1, $c2, $c3, $c4, $c5 ), $found );
     55        }
     56
     57        /**
     58         * Query comments with 'comments'
     59         * @ticket 12668
     60         */
     61        public function test_query_for_comment_type() {
     62                $c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1' ) );
     63                $c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ) );
     64                $c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ) );
     65                $c4 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'mario' ) );
     66                $c5 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'luigi' ) );
     67
     68                $q = new WP_Comment_Query();
     69                $found = $q->query( array(
     70                        'type' => 'comment',
     71                        'fields' => 'ids',
     72                ) );
     73
     74                $this->assertEquals( array( $c1 ), $found );
     75        }
     76
     77        /**
     78         * Query pingback  via string
     79         * @ticket 12668
     80         */
     81        public function test_query_for_pingback_type() {
     82                $c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1' ) );
     83                $c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ) );
     84                $c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ) );
     85                $c4 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ) );
     86
     87                $q = new WP_Comment_Query();
     88                $found = $q->query( array(
     89                        'type' => 'pingback',
     90                        'fields' => 'ids',
     91                ) );
     92
     93                $this->assertEquals( array( $c2, $c3 ), $found );
     94
     95        }
     96
     97        /**
     98         * Query trackback  via string
     99         * @ticket 12668
     100         */
     101        public function test_query_for_trackback_type() {
     102                $c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1' ) );
     103                $c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ) );
     104                $c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ) );
     105                $c4 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ) );
     106
     107                $q = new WP_Comment_Query();
     108                $found = $q->query( array(
     109                        'type' => 'trackback',
     110                        'fields' => 'ids',
     111                ) );
     112
     113                $this->assertEquals( array( $c2, $c3 ), $found );
     114
     115        }
     116
     117        /**
     118         * Comments and Pings
     119         * @ticket 12668
     120         */
     121        public function test_query_for_pings() {
     122                $c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1' ) );
     123                $c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ) );
     124                $c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ) );
     125                $c4 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'mario' ) );
     126                $c5 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'luigi' ) );
     127
     128                $q = new WP_Comment_Query();
     129                $found = $q->query( array(
     130                        'type' => 'pings',
     131                        'fields' => 'ids',
     132                ) );
     133
     134                $this->assertEquals( array( $c2, $c3 ), $found );
     135        }
     136
     137        /**
     138         * Comments query
     139         * @ticket 12668
     140         */
     141        public function test_get_approved_comments() {
     142                $ca1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1' ) );
     143                $ca2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1' ) );
     144                $ca3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '0' ) );
     145                $c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'pingback' ) );
     146                $c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'trackback' ) );
     147                $c4 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'mario' ) );
     148                $c5 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1', 'comment_type' => 'luigi' ) );
     149
     150                $q = get_approved_comments( $this->post_id );
     151                $found = array();
     152                foreach ( $q as $key => $c ) {
     153                        $found[] = (int) $c->comment_ID;
     154                }
     155                // all comments types will be returned
     156                $this->assertEquals( array( $ca1, $ca2, $c2, $c3, $c4, $c5 ), $found );
     157        }
     158
     159        /**
    19160         * @ticket 21101
    20161         */
    21162        public function test_status_hold() {