Make WordPress Core

Ticket #32351: 32351.4.diff

File 32351.4.diff, 4.2 KB (added by johnbillion, 11 years ago)
  • tests/phpunit/tests/option/sanitize-option.php

     
     1<?php
     2
     3/**
     4 * @group option
     5 */
     6class Tests_Sanitize_Option extends WP_UnitTestCase {
     7
     8        /**
     9         * Data provider to test all of the sanitize_option() case
     10         *
     11         * Inner array params: $option_name, $sanitized, $original
     12         * @return array
     13         *
     14         */
     15        public function sanitize_option_provider() {
     16                return array(
     17                        array( 'admin_email', 'mail@example.com', 'mail@example.com' ),
     18                        array( 'admin_email', get_option( 'admin_email' ), 'invalid' ),
     19                        array( 'page_on_front', 0, 0 ),
     20                        array( 'page_on_front', 10, '-10' ),
     21                        array( 'posts_per_page', 10, 10 ),
     22                        array( 'posts_per_page', -1, -1 ),
     23                        array( 'posts_per_page', 2, -2 ),
     24                        array( 'posts_per_page', 1, 'ten' ),
     25                        array( 'default_ping_status', 'open', 'open' ),
     26                        array( 'default_ping_status', 'closed', '' ),
     27                        array( 'blogname', 'My Site', 'My Site' ),
     28                        array( 'blogname', '&lt;i&gt;My Site&lt;/i&gt;', '<i>My Site</i>' ),
     29                        array( 'blog_charset', 'UTF-8', 'UTF-8' ),
     30                        array( 'blog_charset', 'charset', '">charset<"' ),
     31                        array( 'blog_public', 1, null ),
     32                        array( 'blog_public', 1, '1' ),
     33                        array( 'blog_public', -2, '-2' ),
     34                        array( 'date_format', 'F j, Y', 'F j, Y' ),
     35                        array( 'date_format', 'F j, Y', 'F j, <strong>Y</strong>' ),
     36                        array( 'ping_sites', 'http://rpc.pingomatic.com/', 'http://rpc.pingomatic.com/' ),
     37                        array( 'ping_sites', "http://www.example.com\nhttp://example.org", "www.example.com \n\texample.org\n\n" ),
     38                        array( 'gmt_offset', '0', 0 ),
     39                        array( 'gmt_offset', '1.5', '1.5' ),
     40                        array( 'siteurl', 'http://example.org', 'http://example.org' ),
     41                        array( 'siteurl', 'http://example.org/subdir', 'http://example.org/subdir' ),
     42                        array( 'siteurl', get_option( 'siteurl' ), '' ),
     43                        array( 'home', 'http://example.org', 'example.org' ),
     44                        array( 'home', 'https://example.org', 'https://example.org' ),
     45                        array( 'home', 'http://localhost:8000', 'http://localhost:8000' ),
     46                        array( 'home', get_option( 'home' ), '' ),
     47                        array( 'WPLANG', 0, 0 ),
     48                        array( 'WPLANG', '', '' ),
     49                        array(
     50                                'illegal_names',
     51                                array( 'www', 'web', 'root', 'admin', 'main', 'invite', 'administrator', 'files' ),
     52                                array( 'www', 'web', 'root', 'admin', 'main', 'invite', 'administrator', 'files' ),
     53                        ),
     54                        array(
     55                                'illegal_names',
     56                                array( 'www', 'web', 'root', 'admin', 'main', 'invite', 'administrator', 'files' ),
     57                                "www     web root admin main invite administrator files",
     58                        ),
     59                        array(
     60                                'banned_email_domains',
     61                                array( 'mail.com', 'gmail.com' ),
     62                                array( 'mail.com', 'gmail.com' ),
     63                        ),
     64                        array(
     65                                'banned_email_domains',
     66                                array( 'mail.com' ),
     67                                "mail.com\ngmail,com",
     68                        ),
     69                        array( 'timezone_string', 0, 0 ),
     70                        array( 'timezone_string', 'Europe/London', 'Europe/London' ),
     71                        array( 'timezone_string', get_option( 'timezone_string' ), 'invalid' ),
     72                        array( 'permalink_structure', '', '' ),
     73                        array( 'permalink_structure', '/%year%/%postname%', '/%year%/ %postname%' ),
     74                        array( 'default_role', 'subscriber', 'subscriber' ),
     75                        array( 'default_role', 'subscriber', 'invalid' ),
     76                        array( 'default_role', 'editor', 'editor' ),
     77                        array( 'moderation_keys', 'string of words', 'string of words' ),
     78                        array( 'moderation_keys', "one\ntwo three", "one\none\ntwo three" ),
     79                );
     80        }
     81
     82        /**
     83         * @dataProvider sanitize_option_provider
     84         */
     85        function test_sanitize_option( $option_name, $sanitized, $original ) {
     86                $this->assertSame( $sanitized, sanitize_option( $option_name, $original ) );
     87        }
     88
     89        public function upload_path_provider()  {
     90                return array(
     91                        array( '<a href="http://www.example.com">Link</a>', 'Link' ),
     92                        array( '<scr' . 'ipt>url</scr' . 'ipt>', 'url' ),
     93                        array( '/path/to/things', '/path/to/things' ),
     94                        array( '\path\to\things', '\path\to\things' ),
     95                );
     96        }
     97
     98        /**
     99         * @dataProvider upload_path_provider
     100         */
     101        function test_sanitize_option_upload_path( $provided, $expected ) {
     102                $this->assertSame( $expected, sanitize_option( 'upload_path', $provided ) );
     103        }
     104}