Make WordPress Core


Ignore:
Timestamp:
09/30/2015 01:10:03 AM (11 years ago)
Author:
wonderboymusic
Message:

Rewrite: allow add_rewrite_rule|WP_Rewrite::add_rule() to accept an associative array for the value of $redirect instead of requiring a query string.

Adds unit tests.

Props scribu, DrewAPicture.
Fixes #16840.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rewrite.php

    r34215 r34708  
    3232        }
    3333
     34        /**
     35         * @ticket 16840
     36         */
     37        public function test_add_rule() {
     38                global $wp_rewrite;
     39
     40                $pattern  = 'path/to/rewrite/([^/]+)/?$';
     41                $redirect = 'index.php?test_var1=$matches[1]&test_var2=1';
     42
     43                $wp_rewrite->add_rule( $pattern, $redirect );
     44
     45                $wp_rewrite->flush_rules();
     46
     47                $rewrite_rules = $wp_rewrite->rewrite_rules();
     48
     49                $this->assertSame( $redirect, $rewrite_rules[ $pattern ] );
     50        }
     51
     52        /**
     53         * @ticket 16840
     54         */
     55        public function test_add_rule_redirect_array() {
     56                global $wp_rewrite;
     57
     58                $pattern  = 'path/to/rewrite/([^/]+)/?$';
     59                $redirect = 'index.php?test_var1=$matches[1]&test_var2=1';
     60
     61                $wp_rewrite->add_rule( $pattern, array(
     62                        'test_var1' => '$matches[1]',
     63                        'test_var2' => '1'
     64                ) );
     65
     66                $wp_rewrite->flush_rules();
     67
     68                $rewrite_rules = $wp_rewrite->rewrite_rules();
     69
     70                $this->assertSame( $redirect, $rewrite_rules[ $pattern ] );
     71        }
     72
     73        /**
     74         * @ticket 16840
     75         */
     76        public function test_add_rule_top() {
     77                global $wp_rewrite;
     78
     79                $pattern  = 'path/to/rewrite/([^/]+)/?$';
     80                $redirect = 'index.php?test_var1=$matches[1]&test_var2=1';
     81
     82                $wp_rewrite->add_rule( $pattern, $redirect, 'top' );
     83
     84                $wp_rewrite->flush_rules();
     85
     86                $extra_rules_top = $wp_rewrite->extra_rules_top;
     87
     88                $this->assertContains( $redirect, $extra_rules_top[ $pattern ] );
     89        }
     90
    3491        function test_url_to_postid() {
    3592
Note: See TracChangeset for help on using the changeset viewer.