WordPress.org

Make WordPress Core


Ignore:
Timestamp:
06/19/2016 12:01:11 PM (4 years ago)
Author:
swissspidy
Message:

Permalinks: Validate custom permalink structures.

Custom permalink structures require at least one valid structure tag, e.g. %postname%. If none is included, it would leave users with broken permalinks.
Let's make sure this won't happen by validating the permalink structure.

Adds unit tests.

Props rockwell15 for initial patch.
Fixes #35936.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/option/sanitize-option.php

    r37470 r37747  
    120120        $this->assertSame( $expected, sanitize_option( 'blogdescription', $value ) );
    121121    }
     122
     123    /**
     124     * @dataProvider permalink_structure_provider
     125     */
     126    public function test_sanitize_permalink_structure( $provided, $expected, $valid ) {
     127        global $wp_settings_errors;
     128
     129        $old_wp_settings_errors = (array) $wp_settings_errors;
     130
     131        $actual = sanitize_option( 'permalink_structure', $provided);
     132        $errors = get_settings_errors( 'permalink_structure' );
     133
     134        // Clear errors.
     135        $wp_settings_errors = $old_wp_settings_errors;
     136
     137        if ( $valid ) {
     138            $this->assertEmpty( $errors );
     139        } else {
     140            $this->assertNotEmpty( $errors );
     141            $this->assertEquals( 'invalid_permalink_structure', $errors[0]['code'] );
     142        }
     143
     144        $this->assertEquals( $expected, $actual );
     145    }
     146
     147    public function permalink_structure_provider() {
     148        return array(
     149            array( '', '', true ),
     150            array( '%postname', false, false ),
     151            array( '%/%', false, false ),
     152            array( '%%%', false, false ),
     153            array( '%a%', '%a%', true ),
     154            array( '%postname%', '%postname%', true ),
     155            array( '/%postname%/', '/%postname%/', true ),
     156            array( '/%year%/%monthnum%/%day%/%postname%/', '/%year%/%monthnum%/%day%/%postname%/', true ),
     157            array( '/%year/%postname%/', '/%year/%postname%/', true ),
     158        );
     159    }
    122160}
Note: See TracChangeset for help on using the changeset viewer.