| | 1866 | public function verify_post_roundtrip( $input = array(), $expected_output = array() ) { |
| | 1867 | // Create the post |
| | 1868 | $request = new WP_REST_Request( 'POST', '/wp/v2/posts' ); |
| | 1869 | foreach ( $input as $name => $value ) { |
| | 1870 | $request->set_param( $name, $value ); |
| | 1871 | } |
| | 1872 | $response = $this->server->dispatch( $request ); |
| | 1873 | $this->assertEquals( 201, $response->get_status() ); |
| | 1874 | |
| | 1875 | $expected_output = array_merge( $input, $expected_output ); |
| | 1876 | |
| | 1877 | $actual_output = $response->get_data(); |
| | 1878 | $actual_output['content'] = $actual_output['content']['raw']; |
| | 1879 | $actual_output['title'] = $actual_output['title']['raw']; |
| | 1880 | $actual_output['excerpt'] = $actual_output['excerpt']['raw']; |
| | 1881 | |
| | 1882 | // Compare expected API output to actual API output |
| | 1883 | foreach ( $expected_output as $name => $value ) { |
| | 1884 | $this->assertEquals( $expected_output[ $name ], $actual_output[ $name ], "bad $name on create (API)" ); |
| | 1885 | } |
| | 1886 | |
| | 1887 | // Compare expected API output to WP internal values |
| | 1888 | $post = get_post( $actual_output['id'] ); |
| | 1889 | $this->assertEquals( $expected_output['title'], $post->post_title, 'bad title on create (WP)' ); |
| | 1890 | $this->assertEquals( $expected_output['content'], $post->post_content, 'bad content on create (WP)' ); |
| | 1891 | $this->assertEquals( $expected_output['excerpt'], $post->post_excerpt, 'bad excerpt on create (WP)' ); |
| | 1892 | |
| | 1893 | // Update the post |
| | 1894 | $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $actual_output['id'] ) ); |
| | 1895 | foreach ( $input as $name => $value ) { |
| | 1896 | $request->set_param( $name, $value ); |
| | 1897 | } |
| | 1898 | $response = $this->server->dispatch( $request ); |
| | 1899 | $this->assertEquals( 200, $response->get_status() ); |
| | 1900 | |
| | 1901 | $actual_output = $response->get_data(); |
| | 1902 | $actual_output['content'] = $actual_output['content']['raw']; |
| | 1903 | $actual_output['title'] = $actual_output['title']['raw']; |
| | 1904 | $actual_output['excerpt'] = $actual_output['excerpt']['raw']; |
| | 1905 | |
| | 1906 | // Compare expected API output to actual API output |
| | 1907 | foreach ( $expected_output as $name => $value ) { |
| | 1908 | $this->assertEquals( $expected_output[ $name ], $actual_output[ $name ], "bad $name on update (API)" ); |
| | 1909 | } |
| | 1910 | |
| | 1911 | // Compare expected API output to WP internal values |
| | 1912 | $post = get_post( $actual_output['id'] ); |
| | 1913 | $this->assertEquals( $expected_output['title'], $post->post_title, 'bad title on update (WP)' ); |
| | 1914 | $this->assertEquals( $expected_output['content'], $post->post_content, 'bad content on update (WP)' ); |
| | 1915 | $this->assertEquals( $expected_output['excerpt'], $post->post_excerpt, 'bad excerpt on update (WP)' ); |
| | 1916 | } |
| | 1917 | |
| | 1918 | public function test_post_roundtrip_as_author_1() { |
| | 1919 | wp_set_current_user( self::$author_id ); |
| | 1920 | $this->assertFalse( current_user_can( 'unfiltered_html' ) ); |
| | 1921 | $this->verify_post_roundtrip( array( |
| | 1922 | 'title' => '\o/ ¯\_(ツ)_/¯ 🚢', |
| | 1923 | 'content' => '\o/ ¯\_(ツ)_/¯ 🚢', |
| | 1924 | 'excerpt' => '\o/ ¯\_(ツ)_/¯ 🚢', |
| | 1925 | ) ); |
| | 1926 | } |
| | 1927 | |
| | 1928 | public function test_post_roundtrip_as_author_2() { |
| | 1929 | wp_set_current_user( self::$author_id ); |
| | 1930 | $this->assertFalse( current_user_can( 'unfiltered_html' ) ); |
| | 1931 | $this->verify_post_roundtrip( array( |
| | 1932 | 'title' => '\\\&\\\ & &invalid; < < &lt;', |
| | 1933 | 'content' => '\\\&\\\ & &invalid; < < &lt;', |
| | 1934 | 'excerpt' => '\\\&\\\ & &invalid; < < &lt;', |
| | 1935 | ), array( |
| | 1936 | 'title' => '\\\&\\\ & &invalid; < < &lt;', |
| | 1937 | 'content' => '\\\&\\\ & &invalid; < < &lt;', |
| | 1938 | 'excerpt' => '\\\&\\\ & &invalid; < < &lt;', |
| | 1939 | ) ); |
| | 1940 | } |
| | 1941 | |
| | 1942 | public function test_post_roundtrip_as_author_unfiltered_html_1() { |
| | 1943 | wp_set_current_user( self::$author_id ); |
| | 1944 | $this->assertFalse( current_user_can( 'unfiltered_html' ) ); |
| | 1945 | $this->verify_post_roundtrip( array( |
| | 1946 | 'title' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', |
| | 1947 | 'content' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', |
| | 1948 | 'excerpt' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', |
| | 1949 | ), array( |
| | 1950 | 'title' => 'div <strong>strong</strong> oh noes', |
| | 1951 | 'content' => '<div>div</div> <strong>strong</strong> oh noes', |
| | 1952 | 'excerpt' => '<div>div</div> <strong>strong</strong> oh noes', |
| | 1953 | ) ); |
| | 1954 | } |
| | 1955 | |
| | 1956 | public function test_post_roundtrip_as_author_unfiltered_html_2() { |
| | 1957 | wp_set_current_user( self::$author_id ); |
| | 1958 | $this->assertFalse( current_user_can( 'unfiltered_html' ) ); |
| | 1959 | $this->verify_post_roundtrip( array( |
| | 1960 | 'title' => '<a href="#" target="_blank" data-unfiltered=true>link</a>', |
| | 1961 | 'content' => '<a href="#" target="_blank" data-unfiltered=true>link</a>', |
| | 1962 | 'excerpt' => '<a href="#" target="_blank" data-unfiltered=true>link</a>', |
| | 1963 | ), array( |
| | 1964 | 'title' => '<a href="#">link</a>', |
| | 1965 | 'content' => '<a href="#" target="_blank">link</a>', |
| | 1966 | 'excerpt' => '<a href="#" target="_blank">link</a>', |
| | 1967 | ) ); |
| | 1968 | } |
| | 1969 | |
| | 1970 | public function test_post_roundtrip_as_editor_1() { |
| | 1971 | wp_set_current_user( self::$editor_id ); |
| | 1972 | $this->assertEquals( ! is_multisite(), current_user_can( 'unfiltered_html' ) ); |
| | 1973 | $this->verify_post_roundtrip( array( |
| | 1974 | 'title' => '\o/ ¯\_(ツ)_/¯ 🚢', |
| | 1975 | 'content' => '\o/ ¯\_(ツ)_/¯ 🚢', |
| | 1976 | 'excerpt' => '\o/ ¯\_(ツ)_/¯ 🚢', |
| | 1977 | ) ); |
| | 1978 | } |
| | 1979 | |
| | 1980 | public function test_post_roundtrip_as_editor_2() { |
| | 1981 | wp_set_current_user( self::$editor_id ); |
| | 1982 | if ( is_multisite() ) { |
| | 1983 | $this->assertFalse( current_user_can( 'unfiltered_html' ) ); |
| | 1984 | $this->verify_post_roundtrip( array( |
| | 1985 | 'title' => '\\\&\\\ & &invalid; < < &lt;', |
| | 1986 | 'content' => '\\\&\\\ & &invalid; < < &lt;', |
| | 1987 | 'excerpt' => '\\\&\\\ & &invalid; < < &lt;', |
| | 1988 | ), array( |
| | 1989 | 'title' => '\\\&\\\ & &invalid; < < &lt;', |
| | 1990 | 'content' => '\\\&\\\ & &invalid; < < &lt;', |
| | 1991 | 'excerpt' => '\\\&\\\ & &invalid; < < &lt;', |
| | 1992 | ) ); |
| | 1993 | } else { |
| | 1994 | $this->assertTrue( current_user_can( 'unfiltered_html' ) ); |
| | 1995 | $this->verify_post_roundtrip( array( |
| | 1996 | 'title' => '\\\&\\\ & &invalid; < < &lt;', |
| | 1997 | 'content' => '\\\&\\\ & &invalid; < < &lt;', |
| | 1998 | 'excerpt' => '\\\&\\\ & &invalid; < < &lt;', |
| | 1999 | ) ); |
| | 2000 | } |
| | 2001 | } |
| | 2002 | |
| | 2003 | public function test_post_roundtrip_as_editor_unfiltered_html_1() { |
| | 2004 | wp_set_current_user( self::$editor_id ); |
| | 2005 | if ( is_multisite() ) { |
| | 2006 | $this->assertFalse( current_user_can( 'unfiltered_html' ) ); |
| | 2007 | $this->verify_post_roundtrip( array( |
| | 2008 | 'title' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', |
| | 2009 | 'content' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', |
| | 2010 | 'excerpt' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', |
| | 2011 | ), array( |
| | 2012 | 'title' => 'div <strong>strong</strong> oh noes', |
| | 2013 | 'content' => '<div>div</div> <strong>strong</strong> oh noes', |
| | 2014 | 'excerpt' => '<div>div</div> <strong>strong</strong> oh noes', |
| | 2015 | ) ); |
| | 2016 | } else { |
| | 2017 | $this->assertTrue( current_user_can( 'unfiltered_html' ) ); |
| | 2018 | $this->verify_post_roundtrip( array( |
| | 2019 | 'title' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', |
| | 2020 | 'content' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', |
| | 2021 | 'excerpt' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', |
| | 2022 | ) ); |
| | 2023 | } |
| | 2024 | } |
| | 2025 | |
| | 2026 | public function test_post_roundtrip_as_editor_unfiltered_html_2() { |
| | 2027 | wp_set_current_user( self::$editor_id ); |
| | 2028 | if ( is_multisite() ) { |
| | 2029 | $this->assertFalse( current_user_can( 'unfiltered_html' ) ); |
| | 2030 | $this->verify_post_roundtrip( array( |
| | 2031 | 'title' => '<a href="#" target="_blank" data-unfiltered=true>link</a>', |
| | 2032 | 'content' => '<a href="#" target="_blank" data-unfiltered=true>link</a>', |
| | 2033 | 'excerpt' => '<a href="#" target="_blank" data-unfiltered=true>link</a>', |
| | 2034 | ), array( |
| | 2035 | 'title' => '<a href="#">link</a>', |
| | 2036 | 'content' => '<a href="#" target="_blank">link</a>', |
| | 2037 | 'excerpt' => '<a href="#" target="_blank">link</a>', |
| | 2038 | ) ); |
| | 2039 | } else { |
| | 2040 | $this->assertTrue( current_user_can( 'unfiltered_html' ) ); |
| | 2041 | $this->verify_post_roundtrip( array( |
| | 2042 | 'title' => '<a href="#" target="_blank" data-unfiltered=true>link</a>', |
| | 2043 | 'content' => '<a href="#" target="_blank" data-unfiltered=true>link</a>', |
| | 2044 | 'excerpt' => '<a href="#" target="_blank" data-unfiltered=true>link</a>', |
| | 2045 | ) ); |
| | 2046 | } |
| | 2047 | } |
| | 2048 | |
| | 2049 | public function test_post_roundtrip_as_superadmin_1() { |
| | 2050 | wp_set_current_user( self::$superadmin_id ); |
| | 2051 | $this->assertTrue( current_user_can( 'unfiltered_html' ) ); |
| | 2052 | $this->verify_post_roundtrip( array( |
| | 2053 | 'title' => '\o/ ¯\_(ツ)_/¯ 🚢', |
| | 2054 | 'content' => '\o/ ¯\_(ツ)_/¯ 🚢', |
| | 2055 | 'excerpt' => '\o/ ¯\_(ツ)_/¯ 🚢', |
| | 2056 | ) ); |
| | 2057 | } |
| | 2058 | |
| | 2059 | public function test_post_roundtrip_as_superadmin_2() { |
| | 2060 | wp_set_current_user( self::$superadmin_id ); |
| | 2061 | $this->assertTrue( current_user_can( 'unfiltered_html' ) ); |
| | 2062 | $this->verify_post_roundtrip( array( |
| | 2063 | 'title' => '\\\&\\\ & &invalid; < < &lt;', |
| | 2064 | 'content' => '\\\&\\\ & &invalid; < < &lt;', |
| | 2065 | 'excerpt' => '\\\&\\\ & &invalid; < < &lt;', |
| | 2066 | ) ); |
| | 2067 | } |
| | 2068 | |
| | 2069 | public function test_post_roundtrip_as_superadmin_unfiltered_html_1() { |
| | 2070 | wp_set_current_user( self::$superadmin_id ); |
| | 2071 | $this->assertTrue( current_user_can( 'unfiltered_html' ) ); |
| | 2072 | $this->verify_post_roundtrip( array( |
| | 2073 | 'title' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', |
| | 2074 | 'content' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', |
| | 2075 | 'excerpt' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', |
| | 2076 | ), array( |
| | 2077 | 'title' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', |
| | 2078 | 'content' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', |
| | 2079 | 'excerpt' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', |
| | 2080 | ) ); |
| | 2081 | } |
| | 2082 | |
| | 2083 | public function test_post_roundtrip_as_superadmin_unfiltered_html_2() { |
| | 2084 | wp_set_current_user( self::$superadmin_id ); |
| | 2085 | $this->assertTrue( current_user_can( 'unfiltered_html' ) ); |
| | 2086 | $this->verify_post_roundtrip( array( |
| | 2087 | 'title' => '<a href="#" target="_blank" data-unfiltered=true>link</a>', |
| | 2088 | 'content' => '<a href="#" target="_blank" data-unfiltered=true>link</a>', |
| | 2089 | 'excerpt' => '<a href="#" target="_blank" data-unfiltered=true>link</a>', |
| | 2090 | ), array( |
| | 2091 | 'title' => '<a href="#" target="_blank" data-unfiltered=true>link</a>', |
| | 2092 | 'content' => '<a href="#" target="_blank" data-unfiltered=true>link</a>', |
| | 2093 | 'excerpt' => '<a href="#" target="_blank" data-unfiltered=true>link</a>', |
| | 2094 | ) ); |
| | 2095 | } |
| | 2096 | |