Make WordPress Core

Ticket #48022: 48022.5.diff

File 48022.5.diff, 6.6 KB (added by audrasjb, 6 years ago)

Adding unit tests for rel gc

  • src/wp-includes/comment-template.php

    diff --git a/src/wp-includes/comment-template.php b/src/wp-includes/comment-template.php
    index b10fe1415e..8c3038e4cc 100644
    a b function get_comment_author_link( $comment_ID = 0 ) { 
    224224        if ( empty( $url ) || 'http://' == $url ) {
    225225                $return = $author;
    226226        } else {
    227                 $return = "<a href='$url' rel='external nofollow' class='url'>$author</a>";
     227                $return = "<a href='$url' rel='external nofollow ugc' class='url'>$author</a>";
    228228        }
    229229
    230230        /**
  • src/wp-includes/default-filters.php

    diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php
    index 423db4d540..025cabda19 100644
    a b add_filter( 'pre_kses', 'wp_pre_kses_less_than' ); 
    246246add_filter( 'sanitize_title', 'sanitize_title_with_dashes', 10, 3 );
    247247add_action( 'check_comment_flood', 'check_comment_flood_db', 10, 4 );
    248248add_filter( 'comment_flood_filter', 'wp_throttle_comment_flood', 10, 3 );
    249 add_filter( 'pre_comment_content', 'wp_rel_nofollow', 15 );
     249add_filter( 'pre_comment_content', 'wp_rel_ugc', 15 );
    250250add_filter( 'comment_email', 'antispambot' );
    251251add_filter( 'option_tag_base', '_wp_filter_taxonomy_base' );
    252252add_filter( 'option_category_base', '_wp_filter_taxonomy_base' );
  • src/wp-includes/formatting.php

    diff --git a/src/wp-includes/formatting.php b/src/wp-includes/formatting.php
    index 1b7923c1b1..1d2809e3e6 100644
    a b function wp_rel_nofollow_callback( $matches ) { 
    30713071        return "<a $text rel=\"" . esc_attr( $rel ) . '">';
    30723072}
    30733073
     3074/**
     3075 * Adds rel ugc string to all HTML A elements in content.
     3076 *
     3077 * @since 5.3.0
     3078 *
     3079 * @param string $text Content that may contain HTML A elements.
     3080 * @return string Converted content.
     3081 */
     3082function wp_rel_ugc( $text ) {
     3083        // This is a pre save filter, so text is already escaped.
     3084        $text = stripslashes( $text );
     3085        $text = preg_replace_callback( '|<a (.+?)>|i', 'wp_rel_ugc_callback', $text );
     3086        return wp_slash( $text );
     3087}
     3088
     3089/**
     3090 * Callback to add rel=ugc string to HTML A element.
     3091 *
     3092 * Will remove already existing rel="ugc" and rel='ugc' from the
     3093 * string to prevent from invalidating (X)HTML.
     3094 *
     3095 * @since 5.3.0
     3096 *
     3097 * @param array $matches Single Match
     3098 * @return string HTML A Element with rel ugc.
     3099 */
     3100function wp_rel_ugc_callback( $matches ) {
     3101        $text = $matches[1];
     3102        $atts = wp_kses_hair( $matches[1], wp_allowed_protocols() );
     3103        $rel  = 'ugc nofollow';
     3104
     3105        if ( ! empty( $atts['href'] ) ) {
     3106                if ( in_array( strtolower( wp_parse_url( $atts['href']['value'], PHP_URL_SCHEME ) ), array( 'http', 'https' ), true ) ) {
     3107                        if ( strtolower( wp_parse_url( $atts['href']['value'], PHP_URL_HOST ) ) === strtolower( wp_parse_url( home_url(), PHP_URL_HOST ) ) ) {
     3108                                return "<a $text>";
     3109                        }
     3110                }
     3111        }
     3112
     3113        if ( ! empty( $atts['rel'] ) ) {
     3114                $parts = array_map( 'trim', explode( ' ', $atts['rel']['value'] ) );
     3115                if ( false === array_search( 'ugc', $parts ) ) {
     3116                        $parts[] = 'ugc';
     3117                }
     3118                $rel = implode( ' ', $parts );
     3119                unset( $atts['rel'] );
     3120
     3121                $html = '';
     3122                foreach ( $atts as $name => $value ) {
     3123                        if ( isset( $value['vless'] ) && 'y' === $value['vless'] ) {
     3124                                $html .= $name . ' ';
     3125                        } else {
     3126                                $html .= "{$name}=\"" . esc_attr( $value['value'] ) . '" ';
     3127                        }
     3128                }
     3129                $text = trim( $html );
     3130        }
     3131        return "<a $text rel=\"" . esc_attr( $rel ) . '">';
     3132}
     3133
    30743134/**
    30753135 * Adds rel noreferrer and noopener to all HTML A elements that have a target.
    30763136 *
  • new file tests/phpunit/tests/formatting/WPRelUgc.php

    diff --git a/tests/phpunit/tests/formatting/WPRelUgc.php b/tests/phpunit/tests/formatting/WPRelUgc.php
    new file mode 100644
    index 0000000000..515eebb736
    - +  
     1<?php
     2
     3/**
     4 * @group formatting
     5 */
     6class Tests_Rel_Ugc extends WP_UnitTestCase {
     7
     8        /**
     9         * @ticket 48022
     10         */
     11        public function test_add_ugc() {
     12                $content  = '<p>This is some cool <a href="/">Code</a></p>';
     13                $expected = '<p>This is some cool <a href=\"/\" rel=\"nofollow ugc\">Code</a></p>';
     14                $this->assertEquals( $expected, wp_rel_ugc( $content ) );
     15        }
     16
     17        /**
     18         * @ticket 48022
     19         */
     20        public function test_convert_ugc() {
     21                $content  = '<p>This is some cool <a href="/" rel="weird">Code</a></p>';
     22                $expected = '<p>This is some cool <a href=\"/\" rel=\"weird nofollow ugc\">Code</a></p>';
     23                $this->assertEquals( $expected, wp_rel_ugc( $content ) );
     24        }
     25
     26        /**
     27         * @ticket 48022
     28         * @dataProvider data_wp_rel_ugc
     29         */
     30        public function test_wp_rel_ugc( $input, $output ) {
     31                return $this->assertEquals( wp_slash( $output ), wp_rel_ugc( $input ) );
     32        }
     33
     34        public function data_wp_rel_ugc() {
     35                $home_url_http  = set_url_scheme( home_url(), 'http' );
     36                $home_url_https = set_url_scheme( home_url(), 'https' );
     37
     38                return array(
     39                        array(
     40                                '<a href="">Double Quotes</a>',
     41                                '<a href="" rel="nofollow ugc">Double Quotes</a>',
     42                        ),
     43                        array(
     44                                '<a href="https://wordpress.org">Double Quotes</a>',
     45                                '<a href="https://wordpress.org" rel="nofollow ugc">Double Quotes</a>',
     46                        ),
     47                        array(
     48                                "<a href='https://wordpress.org'>Single Quotes</a>",
     49                                "<a href='https://wordpress.org' rel=\"nofollow ugc\">Single Quotes</a>",
     50                        ),
     51                        array(
     52                                '<a href="https://wordpress.org" title="Title">Multiple attributes</a>',
     53                                '<a href="https://wordpress.org" title="Title" rel="nofollow ugc">Multiple attributes</a>',
     54                        ),
     55                        array(
     56                                '<a title="Title" href="https://wordpress.org">Multiple attributes</a>',
     57                                '<a title="Title" href="https://wordpress.org" rel="nofollow ugc">Multiple attributes</a>',
     58                        ),
     59                        array(
     60                                '<a data-someflag href="https://wordpress.org">Multiple attributes</a>',
     61                                '<a data-someflag href="https://wordpress.org" rel="nofollow ugc">Multiple attributes</a>',
     62                        ),
     63                        array(
     64                                '<a  data-someflag  title="Title"  href="https://wordpress.org" onclick=""  >Everything at once</a>',
     65                                '<a  data-someflag  title="Title"  href="https://wordpress.org" onclick=""   rel="nofollow ugc">Everything at once</a>',
     66                        ),
     67                        array(
     68                                '<a href="' . $home_url_http . '/some-url">Home URL (http)</a>',
     69                                '<a href="' . $home_url_http . '/some-url">Home URL (http)</a>',
     70                        ),
     71                        array(
     72                                '<a href="' . $home_url_https . '/some-url">Home URL (https)</a>',
     73                                '<a href="' . $home_url_https . '/some-url">Home URL (https)</a>',
     74                        ),
     75                );
     76        }
     77
     78        public function test_append_ugc_with_valueless_attribute() {
     79                $content  = '<p>This is some cool <a href="demo.com" download rel="hola">Code</a></p>';
     80                $expected = '<p>This is some cool <a href=\"demo.com\" download rel=\"hola nofollow ugc\">Code</a></p>';
     81                $this->assertEquals( $expected, wp_rel_ugc( $content ) );
     82        }
     83}