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.

git-svn-id: https://develop.svn.wordpress.org/trunk@37747 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Pascal Birchler
2016-06-19 12:01:11 +00:00
parent db843d80ab
commit d40f2eed2c
3 changed files with 68 additions and 31 deletions

View File

@@ -119,4 +119,42 @@ class Tests_Sanitize_Option extends WP_UnitTestCase {
$this->assertSame( $expected, sanitize_option( 'blogname', $value ) );
$this->assertSame( $expected, sanitize_option( 'blogdescription', $value ) );
}
/**
* @dataProvider permalink_structure_provider
*/
public function test_sanitize_permalink_structure( $provided, $expected, $valid ) {
global $wp_settings_errors;
$old_wp_settings_errors = (array) $wp_settings_errors;
$actual = sanitize_option( 'permalink_structure', $provided);
$errors = get_settings_errors( 'permalink_structure' );
// Clear errors.
$wp_settings_errors = $old_wp_settings_errors;
if ( $valid ) {
$this->assertEmpty( $errors );
} else {
$this->assertNotEmpty( $errors );
$this->assertEquals( 'invalid_permalink_structure', $errors[0]['code'] );
}
$this->assertEquals( $expected, $actual );
}
public function permalink_structure_provider() {
return array(
array( '', '', true ),
array( '%postname', false, false ),
array( '%/%', false, false ),
array( '%%%', false, false ),
array( '%a%', '%a%', true ),
array( '%postname%', '%postname%', true ),
array( '/%postname%/', '/%postname%/', true ),
array( '/%year%/%monthnum%/%day%/%postname%/', '/%year%/%monthnum%/%day%/%postname%/', true ),
array( '/%year/%postname%/', '/%year/%postname%/', true ),
);
}
}