| | 1 | <?php |
| | 2 | |
| | 3 | class Tests_Pluggable extends WP_UnitTestCase { |
| | 4 | |
| | 5 | /** |
| | 6 | * Tests that the signatures of all functions in pluggable.php match their expected signature. |
| | 7 | * |
| | 8 | * @dataProvider getDefinedPluggableFunctions |
| | 9 | */ |
| | 10 | public function testPluggableFunctionSignaturesMatch( $function ) { |
| | 11 | |
| | 12 | $signatures = $this->getPluggableFunctionSignatures(); |
| | 13 | |
| | 14 | $this->assertTrue( function_exists( $function ) ); |
| | 15 | $this->assertArrayHasKey( $function, $signatures ); |
| | 16 | |
| | 17 | $function_ref = new ReflectionFunction( $function ); |
| | 18 | $param_refs = $function_ref->getParameters(); |
| | 19 | |
| | 20 | $this->assertSame( count( $signatures[ $function ] ), count( $param_refs ) ); |
| | 21 | |
| | 22 | $i = 0; |
| | 23 | |
| | 24 | foreach ( $signatures[ $function ] as $name => $value ) { |
| | 25 | |
| | 26 | $param_ref = $param_refs[ $i ]; |
| | 27 | $msg = 'Parameter: ' . $param_ref->getName(); |
| | 28 | |
| | 29 | if ( is_numeric( $name ) ) { |
| | 30 | $name = $value; |
| | 31 | $this->assertFalse( $param_ref->isOptional(), $msg ); |
| | 32 | } else { |
| | 33 | $this->assertTrue( $param_ref->isOptional(), $msg ); |
| | 34 | $this->assertSame( $value, $param_ref->getDefaultValue(), $msg ); |
| | 35 | } |
| | 36 | |
| | 37 | $this->assertSame( $name, $param_ref->getName(), $msg ); |
| | 38 | |
| | 39 | $i++; |
| | 40 | |
| | 41 | } |
| | 42 | |
| | 43 | } |
| | 44 | |
| | 45 | /** |
| | 46 | * Test the tests. Makes sure all the expected pluggable functions exist and that they live in pluggable.php. |
| | 47 | */ |
| | 48 | public function testAllPluggableFunctionsExist() { |
| | 49 | |
| | 50 | $defined = wp_list_pluck( $this->getDefinedPluggableFunctions(), 0 ); |
| | 51 | $expected = $this->getPluggableFunctionSignatures(); |
| | 52 | |
| | 53 | foreach ( $expected as $function => $sig ) { |
| | 54 | $msg = 'Function: ' . $function . '()'; |
| | 55 | $this->assertTrue( function_exists( $function ), $msg ); |
| | 56 | $this->assertTrue( in_array( $function, $defined, true ), $msg ); |
| | 57 | } |
| | 58 | |
| | 59 | } |
| | 60 | |
| | 61 | /** |
| | 62 | * Data provider for our pluggable function signature tests. |
| | 63 | * |
| | 64 | * @return array Data provider array of pluggable function names. |
| | 65 | */ |
| | 66 | public function getDefinedPluggableFunctions() { |
| | 67 | |
| | 68 | preg_match_all( '#^function (\w+)#m', file_get_contents( ABSPATH . '/wp-includes/pluggable.php' ), $functions ); |
| | 69 | |
| | 70 | $data = array(); |
| | 71 | |
| | 72 | foreach ( $functions[1] as $function ) { |
| | 73 | $data[] = array( |
| | 74 | $function, |
| | 75 | ); |
| | 76 | } |
| | 77 | |
| | 78 | return $data; |
| | 79 | |
| | 80 | } |
| | 81 | |
| | 82 | /** |
| | 83 | * Expected pluggable function signatures. |
| | 84 | * |
| | 85 | * @return array Array of signatures keyed by their function name. |
| | 86 | */ |
| | 87 | public function getPluggableFunctionSignatures() { |
| | 88 | |
| | 89 | return array( |
| | 90 | 'wp_set_current_user' => array( 'id', 'name' => '' ), |
| | 91 | 'wp_get_current_user' => array(), |
| | 92 | 'get_currentuserinfo' => array(), |
| | 93 | 'get_userdata' => array( 'user_id' ), |
| | 94 | 'get_user_by' => array( 'field', 'value' ), |
| | 95 | 'cache_users' => array( 'user_ids' ), |
| | 96 | 'wp_mail' => array( 'to', 'subject', 'message', 'headers' => '', 'attachments' => array() ), |
| | 97 | 'wp_authenticate' => array( 'username', 'password' ), |
| | 98 | 'wp_logout' => array(), |
| | 99 | 'wp_validate_auth_cookie' => array( 'cookie' => '', 'scheme' => '' ), |
| | 100 | 'wp_generate_auth_cookie' => array( 'user_id', 'expiration', 'scheme' => 'auth', 'token' => '' ), |
| | 101 | 'wp_parse_auth_cookie' => array( 'cookie' => '', 'scheme' => '' ), |
| | 102 | 'wp_set_auth_cookie' => array( 'user_id', 'remember' => false, 'secure' => '', 'token' => '' ), |
| | 103 | 'wp_clear_auth_cookie' => array(), |
| | 104 | 'is_user_logged_in' => array(), |
| | 105 | 'auth_redirect' => array(), |
| | 106 | 'check_admin_referer' => array( 'action' => -1, 'query_arg' => '_wpnonce' ), |
| | 107 | 'check_ajax_referer' => array( 'action' => -1, 'query_arg' => false, 'die' => true ), |
| | 108 | 'wp_redirect' => array( 'location', 'status' => 302 ), |
| | 109 | 'wp_sanitize_redirect' => array( 'location' ), |
| | 110 | '_wp_sanitize_utf8_in_redirect' => array( 'matches' ), |
| | 111 | 'wp_safe_redirect' => array( 'location', 'status' => 302 ), |
| | 112 | 'wp_validate_redirect' => array( 'location', 'default' => '' ), |
| | 113 | 'wp_notify_postauthor' => array( 'comment_id', 'deprecated' => null ), |
| | 114 | 'wp_notify_moderator' => array( 'comment_id' ), |
| | 115 | 'wp_password_change_notification' => array( 'user' ), |
| | 116 | 'wp_new_user_notification' => array( 'user_id', 'deprecated' => null, 'notify' => '' ), |
| | 117 | 'wp_nonce_tick' => array(), |
| | 118 | 'wp_verify_nonce' => array( 'nonce', 'action' => -1 ), |
| | 119 | 'wp_create_nonce' => array( 'action' => -1 ), |
| | 120 | 'wp_salt' => array( 'scheme' => 'auth' ), |
| | 121 | 'wp_hash' => array( 'data', 'scheme' => 'auth' ), |
| | 122 | 'wp_hash_password' => array( 'password' ), |
| | 123 | 'wp_check_password' => array( 'password', 'hash', 'user_id' => '' ), |
| | 124 | 'wp_generate_password' => array( 'length' => 12, 'special_chars' => true, 'extra_special_chars' => false ), |
| | 125 | 'wp_rand' => array( 'min' => 0, 'max' => 0 ), |
| | 126 | 'wp_set_password' => array( 'password', 'user_id' ), |
| | 127 | 'get_avatar' => array( 'id_or_email', 'size' => 96, 'default' => '', 'alt' => '', 'args' => null ), |
| | 128 | 'wp_text_diff' => array( 'left_string', 'right_string', 'args' => null ), |
| | 129 | ); |
| | 130 | |
| | 131 | } |
| | 132 | |
| | 133 | } |