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