- Timestamp:
- 11/08/2016 05:54:22 AM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php
r39126 r39155 13 13 protected static $post_id; 14 14 15 protected static $superadmin_id; 15 16 protected static $editor_id; 16 17 protected static $author_id; … … 24 25 self::$post_id = $factory->post->create(); 25 26 27 self::$superadmin_id = $factory->user->create( array( 28 'role' => 'administrator', 29 'user_login' => 'superadmin', 30 ) ); 26 31 self::$editor_id = $factory->user->create( array( 27 32 'role' => 'editor', … … 33 38 'role' => 'contributor', 34 39 ) ); 40 41 if ( is_multisite() ) { 42 update_site_option( 'site_admins', array( 'superadmin' ) ); 43 } 35 44 36 45 // Only support 'post' and 'gallery' … … 2002 2011 2003 2012 $this->assertErrorResponse( 'rest_cannot_assign_term', $response, 403 ); 2013 } 2014 2015 public function verify_post_roundtrip( $input = array(), $expected_output = array() ) { 2016 // Create the post 2017 $request = new WP_REST_Request( 'POST', '/wp/v2/posts' ); 2018 foreach ( $input as $name => $value ) { 2019 $request->set_param( $name, $value ); 2020 } 2021 $response = $this->server->dispatch( $request ); 2022 $this->assertEquals( 201, $response->get_status() ); 2023 $actual_output = $response->get_data(); 2024 2025 // Compare expected API output to actual API output 2026 $this->assertEquals( $expected_output['title']['raw'] , $actual_output['title']['raw'] ); 2027 $this->assertEquals( $expected_output['title']['rendered'] , trim( $actual_output['title']['rendered'] ) ); 2028 $this->assertEquals( $expected_output['content']['raw'] , $actual_output['content']['raw'] ); 2029 $this->assertEquals( $expected_output['content']['rendered'], trim( $actual_output['content']['rendered'] ) ); 2030 $this->assertEquals( $expected_output['excerpt']['raw'] , $actual_output['excerpt']['raw'] ); 2031 $this->assertEquals( $expected_output['excerpt']['rendered'], trim( $actual_output['excerpt']['rendered'] ) ); 2032 2033 // Compare expected API output to WP internal values 2034 $post = get_post( $actual_output['id'] ); 2035 $this->assertEquals( $expected_output['title']['raw'] , $post->post_title ); 2036 $this->assertEquals( $expected_output['content']['raw'], $post->post_content ); 2037 $this->assertEquals( $expected_output['excerpt']['raw'], $post->post_excerpt ); 2038 2039 // Update the post 2040 $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $actual_output['id'] ) ); 2041 foreach ( $input as $name => $value ) { 2042 $request->set_param( $name, $value ); 2043 } 2044 $response = $this->server->dispatch( $request ); 2045 $this->assertEquals( 200, $response->get_status() ); 2046 $actual_output = $response->get_data(); 2047 2048 // Compare expected API output to actual API output 2049 $this->assertEquals( $expected_output['title']['raw'] , $actual_output['title']['raw'] ); 2050 $this->assertEquals( $expected_output['title']['rendered'] , trim( $actual_output['title']['rendered'] ) ); 2051 $this->assertEquals( $expected_output['content']['raw'] , $actual_output['content']['raw'] ); 2052 $this->assertEquals( $expected_output['content']['rendered'], trim( $actual_output['content']['rendered'] ) ); 2053 $this->assertEquals( $expected_output['excerpt']['raw'] , $actual_output['excerpt']['raw'] ); 2054 $this->assertEquals( $expected_output['excerpt']['rendered'], trim( $actual_output['excerpt']['rendered'] ) ); 2055 2056 // Compare expected API output to WP internal values 2057 $post = get_post( $actual_output['id'] ); 2058 $this->assertEquals( $expected_output['title']['raw'] , $post->post_title ); 2059 $this->assertEquals( $expected_output['content']['raw'], $post->post_content ); 2060 $this->assertEquals( $expected_output['excerpt']['raw'], $post->post_excerpt ); 2061 } 2062 2063 public static function post_roundtrip_provider() { 2064 return array( 2065 array( 2066 // Raw values. 2067 array( 2068 'title' => '\o/ ¯\_(ツ)_/¯ 🚢', 2069 'content' => '\o/ ¯\_(ツ)_/¯ 🚢', 2070 'excerpt' => '\o/ ¯\_(ツ)_/¯ 🚢', 2071 ), 2072 // Expected returned values. 2073 array( 2074 'title' => array( 2075 'raw' => '\o/ ¯\_(ツ)_/¯ 🚢', 2076 'rendered' => '\o/ ¯\_(ツ)_/¯ 🚢', 2077 ), 2078 'content' => array( 2079 'raw' => '\o/ ¯\_(ツ)_/¯ 🚢', 2080 'rendered' => '<p>\o/ ¯\_(ツ)_/¯ 🚢</p>', 2081 ), 2082 'excerpt' => array( 2083 'raw' => '\o/ ¯\_(ツ)_/¯ 🚢', 2084 'rendered' => '<p>\o/ ¯\_(ツ)_/¯ 🚢</p>', 2085 ), 2086 ) 2087 ), 2088 array( 2089 // Raw values. 2090 array( 2091 'title' => '\\\&\\\ & &invalid; < < &lt;', 2092 'content' => '\\\&\\\ & &invalid; < < &lt;', 2093 'excerpt' => '\\\&\\\ & &invalid; < < &lt;', 2094 ), 2095 // Expected returned values. 2096 array( 2097 'title' => array( 2098 'raw' => '\\\&\\\ & &invalid; < < &lt;', 2099 'rendered' => '\\\&\\\ & &invalid; < < &lt;', 2100 ), 2101 'content' => array( 2102 'raw' => '\\\&\\\ & &invalid; < < &lt;', 2103 'rendered' => '<p>\\\&\\\ & &invalid; < < &lt;</p>', 2104 ), 2105 'excerpt' => array( 2106 'raw' => '\\\&\\\ & &invalid; < < &lt;', 2107 'rendered' => '<p>\\\&\\\ & &invalid; < < &lt;</p>', 2108 ), 2109 ), 2110 ), 2111 array( 2112 // Raw values. 2113 array( 2114 'title' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', 2115 'content' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', 2116 'excerpt' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', 2117 ), 2118 // Expected returned values. 2119 array( 2120 'title' => array( 2121 'raw' => 'div <strong>strong</strong> oh noes', 2122 'rendered' => 'div <strong>strong</strong> oh noes', 2123 ), 2124 'content' => array( 2125 'raw' => '<div>div</div> <strong>strong</strong> oh noes', 2126 'rendered' => "<div>div</div>\n<p> <strong>strong</strong> oh noes</p>", 2127 ), 2128 'excerpt' => array( 2129 'raw' => '<div>div</div> <strong>strong</strong> oh noes', 2130 'rendered' => "<div>div</div>\n<p> <strong>strong</strong> oh noes</p>", 2131 ), 2132 ) 2133 ), 2134 array( 2135 // Raw values. 2136 array( 2137 'title' => '<a href="#" target="_blank" data-unfiltered=true>link</a>', 2138 'content' => '<a href="#" target="_blank" data-unfiltered=true>link</a>', 2139 'excerpt' => '<a href="#" target="_blank" data-unfiltered=true>link</a>', 2140 ), 2141 // Expected returned values. 2142 array( 2143 'title' => array( 2144 'raw' => '<a href="#">link</a>', 2145 'rendered' => '<a href="#">link</a>', 2146 ), 2147 'content' => array( 2148 'raw' => '<a href="#" target="_blank">link</a>', 2149 'rendered' => '<p><a href="#" target="_blank">link</a></p>', 2150 ), 2151 'excerpt' => array( 2152 'raw' => '<a href="#" target="_blank">link</a>', 2153 'rendered' => '<p><a href="#" target="_blank">link</a></p>', 2154 ), 2155 ) 2156 ), 2157 ); 2158 } 2159 2160 /** 2161 * @dataProvider post_roundtrip_provider 2162 */ 2163 public function test_post_roundtrip_as_author( $raw, $expected ) { 2164 wp_set_current_user( self::$author_id ); 2165 $this->assertFalse( current_user_can( 'unfiltered_html' ) ); 2166 $this->verify_post_roundtrip( $raw, $expected ); 2167 } 2168 2169 public function test_post_roundtrip_as_editor_unfiltered_html() { 2170 wp_set_current_user( self::$editor_id ); 2171 if ( is_multisite() ) { 2172 $this->assertFalse( current_user_can( 'unfiltered_html' ) ); 2173 $this->verify_post_roundtrip( array( 2174 'title' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', 2175 'content' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', 2176 'excerpt' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', 2177 ), array( 2178 'title' => array( 2179 'raw' => 'div <strong>strong</strong> oh noes', 2180 'rendered' => 'div <strong>strong</strong> oh noes', 2181 ), 2182 'content' => array( 2183 'raw' => '<div>div</div> <strong>strong</strong> oh noes', 2184 'rendered' => "<div>div</div>\n<p> <strong>strong</strong> oh noes</p>", 2185 ), 2186 'excerpt' => array( 2187 'raw' => '<div>div</div> <strong>strong</strong> oh noes', 2188 'rendered' => "<div>div</div>\n<p> <strong>strong</strong> oh noes</p>", 2189 ), 2190 ) ); 2191 } else { 2192 $this->assertTrue( current_user_can( 'unfiltered_html' ) ); 2193 $this->verify_post_roundtrip( array( 2194 'title' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', 2195 'content' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', 2196 'excerpt' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', 2197 ), array( 2198 'title' => array( 2199 'raw' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', 2200 'rendered' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', 2201 ), 2202 'content' => array( 2203 'raw' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', 2204 'rendered' => "<div>div</div>\n<p> <strong>strong</strong> <script>oh noes</script></p>", 2205 ), 2206 'excerpt' => array( 2207 'raw' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', 2208 'rendered' => "<div>div</div>\n<p> <strong>strong</strong> <script>oh noes</script></p>", 2209 ), 2210 ) ); 2211 } 2212 } 2213 2214 public function test_post_roundtrip_as_superadmin_unfiltered_html() { 2215 wp_set_current_user( self::$superadmin_id ); 2216 $this->assertTrue( current_user_can( 'unfiltered_html' ) ); 2217 $this->verify_post_roundtrip( array( 2218 'title' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', 2219 'content' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', 2220 'excerpt' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', 2221 ), array( 2222 'title' => array( 2223 'raw' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', 2224 'rendered' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', 2225 ), 2226 'content' => array( 2227 'raw' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', 2228 'rendered' => "<div>div</div>\n<p> <strong>strong</strong> <script>oh noes</script></p>", 2229 ), 2230 'excerpt' => array( 2231 'raw' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', 2232 'rendered' => "<div>div</div>\n<p> <strong>strong</strong> <script>oh noes</script></p>", 2233 ), 2234 ) ); 2004 2235 } 2005 2236
Note: See TracChangeset
for help on using the changeset viewer.