Make WordPress Core

Ticket #19623: 19623.backpat.patch

File 19623.backpat.patch, 9.2 KB (added by boonebgorges, 11 years ago)
  • new file tests/phpunit/tests/comment/comment-template-backpat.php

    diff --git tests/phpunit/tests/comment/comment-template-backpat.php tests/phpunit/tests/comment/comment-template-backpat.php
    new file mode 100644
    index 0000000..2424a32
    - +  
     1<?php
     2
     3class Comment_Template_Backpat_Tests extends WP_UnitTestCase {
     4        protected $comments_array = array();
     5        protected $comment_ids = array();
     6
     7        public function test_comments_template_logged_in() {
     8                $u = $this->factory->user->create();
     9
     10                $current_user = get_current_user_id();
     11                wp_set_current_user( $u );
     12
     13                $p1 = $this->factory->post->create();
     14                $p2 = $this->factory->post->create();
     15
     16                // On this post, by another author, approved
     17                $c1 = $this->factory->comment->create( array(
     18                        'comment_post_ID' => $p1,
     19                        'comment_approved' => 1,
     20                ) );
     21
     22                // On this post, by another author, unapproved
     23                $c2 = $this->factory->comment->create( array(
     24                        'comment_post_ID' => $p1,
     25                        'comment_approved' => 0,
     26                ) );
     27
     28                // On this post, by current author, unapproved
     29                $c3 = $this->factory->comment->create( array(
     30                        'comment_post_ID' => $p1,
     31                        'user_id' => $u,
     32                        'comment_approved' => 0,
     33                ) );
     34
     35                // On this post, by current author, approved
     36                $c4 = $this->factory->comment->create( array(
     37                        'comment_post_ID' => $p1,
     38                        'user_id' => $u,
     39                        'comment_approved' => 1,
     40                ) );
     41
     42                // On another post, by current author, approved
     43                $c5 = $this->factory->comment->create( array(
     44                        'comment_post_ID' => $p2,
     45                        'user_id' => $u,
     46                        'comment_approved' => 1,
     47                ) );
     48
     49                $this->go_to( get_permalink( $p1 ) );
     50
     51                ob_start();
     52                add_filter( 'comments_array', array( $this, 'catch_comments_array' ) );
     53                comments_template();
     54                ob_end_clean();
     55
     56                $this->assertEquals( array( $c1, $c3, $c4 ), wp_list_pluck( $this->comments_array, 'comment_ID' ) );
     57
     58                // Clean up
     59                remove_filter( 'comments_array', array( $this, 'catch_comments_array' ) );
     60                wp_set_current_user( $current_user );
     61                $this->comments_array = array();
     62        }
     63
     64        public function test_comments_template_not_logged_in() {
     65                $u = $this->factory->user->create();
     66
     67                $current_user = get_current_user_id();
     68                wp_set_current_user( 0 );
     69
     70                $p1 = $this->factory->post->create();
     71                $p2 = $this->factory->post->create();
     72
     73                $commenter = $this->filter_wp_get_current_commenter();
     74
     75                // On this post, by this author, approved
     76                $c1 = $this->factory->comment->create( array(
     77                        'comment_post_ID' => $p1,
     78                        'comment_approved' => 1,
     79                        'comment_author' => $commenter['comment_author'],
     80                        'comment_author_email' => $commenter['comment_author_email'],
     81                        'comment_author_url' => $commenter['comment_author_url'],
     82                ) );
     83
     84                // On this post, by this author, unapproved
     85                $c2 = $this->factory->comment->create( array(
     86                        'comment_post_ID' => $p1,
     87                        'comment_approved' => 0,
     88                        'comment_author' => $commenter['comment_author'],
     89                        'comment_author_email' => $commenter['comment_author_email'],
     90                        'comment_author_url' => $commenter['comment_author_url'],
     91                ) );
     92
     93                // On this post, by different author, unapproved
     94                $c3 = $this->factory->comment->create( array(
     95                        'comment_post_ID' => $p1,
     96                        'user_id' => $u,
     97                        'comment_approved' => 0,
     98                ) );
     99
     100                // On this post, by different author, approved
     101                $c4 = $this->factory->comment->create( array(
     102                        'comment_post_ID' => $p1,
     103                        'user_id' => $u,
     104                        'comment_approved' => 1,
     105                ) );
     106
     107                // On another post, by this author, approved
     108                $c5 = $this->factory->comment->create( array(
     109                        'comment_post_ID' => $p2,
     110                        'comment_author' => $commenter['comment_author'],
     111                        'comment_author_email' => $commenter['comment_author_email'],
     112                        'comment_author_url' => $commenter['comment_author_url'],
     113                        'comment_approved' => 1,
     114                ) );
     115
     116                $this->go_to( get_permalink( $p1 ) );
     117
     118                ob_start();
     119                add_filter( 'wp_get_current_commenter', array( $this, 'filter_wp_get_current_commenter' ) );
     120                add_filter( 'comments_array', array( $this, 'catch_comments_array' ) );
     121                comments_template();
     122                ob_end_clean();
     123
     124                $this->assertEquals( array( $c1, $c2, $c4 ), wp_list_pluck( $this->comments_array, 'comment_ID' ) );
     125
     126                // clean up
     127                remove_filter( 'wp_get_current_commenter', array( $this, 'filter_wp_get_current_commenter' ) );
     128                remove_filter( 'comments_array', array( $this, 'catch_comments_array' ) );
     129                wp_set_current_user( $current_user );
     130                $this->comments_array = array();
     131        }
     132
     133        public function test_comments_template_no_author_data() {
     134                $u = $this->factory->user->create();
     135
     136                $current_user = get_current_user_id();
     137                wp_set_current_user( 0 );
     138
     139                $p1 = $this->factory->post->create();
     140                $p2 = $this->factory->post->create();
     141
     142                $commenter = $this->filter_wp_get_current_commenter();
     143
     144                // On this post, by this author, approved
     145                $c1 = $this->factory->comment->create( array(
     146                        'comment_post_ID' => $p1,
     147                        'comment_approved' => 1,
     148                        'comment_author' => $commenter['comment_author'],
     149                        'comment_author_email' => $commenter['comment_author_email'],
     150                        'comment_author_url' => $commenter['comment_author_url'],
     151                ) );
     152
     153                // On this post, by this author, unapproved
     154                $c2 = $this->factory->comment->create( array(
     155                        'comment_post_ID' => $p1,
     156                        'comment_approved' => 0,
     157                        'comment_author' => $commenter['comment_author'],
     158                        'comment_author_email' => $commenter['comment_author_email'],
     159                        'comment_author_url' => $commenter['comment_author_url'],
     160                ) );
     161
     162                // On this post, by different author, unapproved
     163                $c3 = $this->factory->comment->create( array(
     164                        'comment_post_ID' => $p1,
     165                        'user_id' => $u,
     166                        'comment_approved' => 0,
     167                ) );
     168
     169                // On this post, by different author, approved
     170                $c4 = $this->factory->comment->create( array(
     171                        'comment_post_ID' => $p1,
     172                        'user_id' => $u,
     173                        'comment_approved' => 1,
     174                ) );
     175
     176                // On another post, by this author, approved
     177                $c5 = $this->factory->comment->create( array(
     178                        'comment_post_ID' => $p2,
     179                        'comment_author' => $commenter['comment_author'],
     180                        'comment_author_email' => $commenter['comment_author_email'],
     181                        'comment_author_url' => $commenter['comment_author_url'],
     182                        'comment_approved' => 1,
     183                ) );
     184
     185                $this->go_to( get_permalink( $p1 ) );
     186
     187                ob_start();
     188                add_filter( 'wp_get_current_commenter', array( $this, 'filter_wp_get_current_commenter_null' ) );
     189                add_filter( 'comments_array', array( $this, 'catch_comments_array' ) );
     190                comments_template();
     191                ob_end_clean();
     192
     193                $this->assertEquals( array( $c1, $c4 ), wp_list_pluck( $this->comments_array, 'comment_ID' ) );
     194
     195                // clean up
     196                remove_filter( 'wp_get_current_commenter', array( $this, 'filter_wp_get_current_commenter_null' ) );
     197                remove_filter( 'comments_array', array( $this, 'catch_comments_array' ) );
     198                wp_set_current_user( $current_user );
     199                $this->comments_array = array();
     200        }
     201
     202        public function test_comments_template_comments_array_filter_backpat() {
     203                $u = $this->factory->user->create();
     204
     205                $current_user = get_current_user_id();
     206                wp_set_current_user( $u );
     207
     208                $p1 = $this->factory->post->create();
     209                $p2 = $this->factory->post->create();
     210
     211                // On this post, by another author, approved
     212                $c1 = $this->factory->comment->create( array(
     213                        'comment_post_ID' => $p1,
     214                        'comment_approved' => 1,
     215                ) );
     216
     217                // On this post, by another author, unapproved
     218                $c2 = $this->factory->comment->create( array(
     219                        'comment_post_ID' => $p1,
     220                        'comment_approved' => 0,
     221                ) );
     222
     223                // On this post, by current author, unapproved
     224                $c3 = $this->factory->comment->create( array(
     225                        'comment_post_ID' => $p1,
     226                        'user_id' => $u,
     227                        'comment_approved' => 0,
     228                ) );
     229
     230                // On this post, by current author, approved
     231                $c4 = $this->factory->comment->create( array(
     232                        'comment_post_ID' => $p1,
     233                        'user_id' => $u,
     234                        'comment_approved' => 1,
     235                ) );
     236
     237                // On another post, by current author, approved
     238                $c5 = $this->factory->comment->create( array(
     239                        'comment_post_ID' => $p2,
     240                        'user_id' => $u,
     241                        'comment_approved' => 1,
     242                ) );
     243
     244                $this->comment_ids = array( $c1, $c2, $c3, $c4, $c5 );
     245
     246                $this->go_to( get_permalink( $p1 ) );
     247
     248                ob_start();
     249
     250                // This is the filter we're testing
     251                add_filter( 'comments_array', array( $this, 'filter_comments_array' ), 5 );
     252
     253                add_filter( 'comments_array', array( $this, 'catch_comments_array' ) );
     254                comments_template();
     255                ob_end_clean();
     256
     257                $this->assertEquals( array( $c1 ), wp_list_pluck( $this->comments_array, 'comment_ID' ) );
     258
     259                // Clean up
     260                remove_filter( 'comments_array', array( $this, 'filter_comments_array' ), 5 );
     261                remove_filter( 'comments_array', array( $this, 'catch_comments_array' ) );
     262                wp_set_current_user( $current_user );
     263                $this->comments_array = array();
     264        }
     265
     266        public function catch_comments_array( $comments ) {
     267                $this->comments_array = $comments;
     268                return $comments;
     269        }
     270
     271        public function filter_comments_array( $comments ) {
     272                $cid = $this->comment_ids[0];
     273                return array( get_comment( $cid ) );
     274        }
     275
     276        public function filter_wp_get_current_commenter( $commenter = false ) {
     277                return array(
     278                        'comment_author' => 'Foo',
     279                        'comment_author_email' => 'foo@example.com',
     280                        'comment_author_url' => 'http://foo.example.com',
     281                );
     282        }
     283
     284        public function filter_wp_get_current_commenter_null( $commenter = false ) {
     285                return array(
     286                        'comment_author' => '',
     287                        'comment_author_email' => '',
     288                        'comment_author_url' => '',
     289                );
     290        }
     291}