| | 1894 | public function verify_comment_roundtrip( $input = array(), $expected_output = array() ) { |
| | 1895 | // Create the comment |
| | 1896 | $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); |
| | 1897 | $request->set_param( 'author_email', 'cbg@androidsdungeon.com' ); |
| | 1898 | $request->set_param( 'post', self::$post_id ); |
| | 1899 | foreach ( $input as $name => $value ) { |
| | 1900 | $request->set_param( $name, $value ); |
| | 1901 | } |
| | 1902 | $response = $this->server->dispatch( $request ); |
| | 1903 | $this->assertEquals( 201, $response->get_status() ); |
| | 1904 | $actual_output = $response->get_data(); |
| | 1905 | |
| | 1906 | // Compare expected API output to actual API output |
| | 1907 | $this->assertEquals( $expected_output['content']['raw'] , $actual_output['content']['raw'] ); |
| | 1908 | $this->assertEquals( $expected_output['content']['rendered'], trim( $actual_output['content']['rendered'] ) ); |
| | 1909 | $this->assertEquals( $expected_output['author_name'] , $actual_output['author_name'] ); |
| | 1910 | $this->assertEquals( $expected_output['author_user_agent'] , $actual_output['author_user_agent'] ); |
| | 1911 | |
| | 1912 | // Compare expected API output to WP internal values |
| | 1913 | $comment = get_comment( $actual_output['id'] ); |
| | 1914 | $this->assertEquals( $expected_output['content']['raw'] , $comment->comment_content ); |
| | 1915 | $this->assertEquals( $expected_output['author_name'] , $comment->comment_author ); |
| | 1916 | $this->assertEquals( $expected_output['author_user_agent'], $comment->comment_agent ); |
| | 1917 | |
| | 1918 | // Update the comment |
| | 1919 | $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', $actual_output['id'] ) ); |
| | 1920 | foreach ( $input as $name => $value ) { |
| | 1921 | $request->set_param( $name, $value ); |
| | 1922 | } |
| | 1923 | // FIXME at least one value must change, or update fails |
| | 1924 | // See https://core.trac.wordpress.org/ticket/38700 |
| | 1925 | $request->set_param( 'author_ip', '127.0.0.2' ); |
| | 1926 | $response = $this->server->dispatch( $request ); |
| | 1927 | $this->assertEquals( 200, $response->get_status() ); |
| | 1928 | $actual_output = $response->get_data(); |
| | 1929 | |
| | 1930 | // Compare expected API output to actual API output |
| | 1931 | $this->assertEquals( $expected_output['content']['raw'] , $actual_output['content']['raw'] ); |
| | 1932 | $this->assertEquals( $expected_output['content']['rendered'], trim( $actual_output['content']['rendered'] ) ); |
| | 1933 | $this->assertEquals( $expected_output['author_name'] , $actual_output['author_name'] ); |
| | 1934 | $this->assertEquals( $expected_output['author_user_agent'] , $actual_output['author_user_agent'] ); |
| | 1935 | |
| | 1936 | // Compare expected API output to WP internal values |
| | 1937 | $comment = get_comment( $actual_output['id'] ); |
| | 1938 | $this->assertEquals( $expected_output['content']['raw'] , $comment->comment_content ); |
| | 1939 | $this->assertEquals( $expected_output['author_name'] , $comment->comment_author ); |
| | 1940 | $this->assertEquals( $expected_output['author_user_agent'], $comment->comment_agent ); |
| | 1941 | } |
| | 1942 | |
| | 1943 | public function test_comment_roundtrip_as_editor_1() { |
| | 1944 | wp_set_current_user( self::$editor_id ); |
| | 1945 | $this->assertEquals( ! is_multisite(), current_user_can( 'unfiltered_html' ) ); |
| | 1946 | $this->verify_comment_roundtrip( array( |
| | 1947 | 'content' => '\o/ ¯\_(ツ)_/¯ 🚢', |
| | 1948 | 'author_name' => '\o/ ¯\_(ツ)_/¯ 🚢', |
| | 1949 | 'author_user_agent' => '\o/ ¯\_(ツ)_/¯ 🚢', |
| | 1950 | ), array( |
| | 1951 | 'content' => array( |
| | 1952 | 'raw' => '\o/ ¯\_(ツ)_/¯ 🚢', |
| | 1953 | 'rendered' => '<p>\o/ ¯\_(ツ)_/¯ 🚢</p>', |
| | 1954 | ), |
| | 1955 | 'author_name' => '\o/ ¯\_(ツ)_/¯ 🚢', |
| | 1956 | 'author_user_agent' => '\o/ ¯\_(ツ)_/¯ 🚢', |
| | 1957 | ) ); |
| | 1958 | } |
| | 1959 | |
| | 1960 | public function test_comment_roundtrip_as_editor_2() { |
| | 1961 | wp_set_current_user( self::$editor_id ); |
| | 1962 | if ( is_multisite() ) { |
| | 1963 | $this->assertFalse( current_user_can( 'unfiltered_html' ) ); |
| | 1964 | $this->verify_comment_roundtrip( array( |
| | 1965 | 'content' => '\\\&\\\ & &invalid; < < &lt;', |
| | 1966 | 'author_name' => '\\\&\\\ & &invalid; < < &lt;', |
| | 1967 | 'author_user_agent' => '\\\&\\\ & &invalid; < < &lt;', |
| | 1968 | ), array( |
| | 1969 | 'content' => array( |
| | 1970 | 'raw' => '\\\&\\\ & &invalid; < < &lt;', |
| | 1971 | 'rendered' => '<p>\\\&\\\ & &invalid; < < &lt;</p>', |
| | 1972 | ), |
| | 1973 | 'author_name' => '\\\&\\\ & &invalid; < < &lt;', |
| | 1974 | 'author_user_agent' => '\\\&\\\ & &invalid; < < &lt;', |
| | 1975 | ) ); |
| | 1976 | } else { |
| | 1977 | $this->assertTrue( current_user_can( 'unfiltered_html' ) ); |
| | 1978 | $this->verify_comment_roundtrip( array( |
| | 1979 | 'content' => '\\\&\\\ & &invalid; < < &lt;', |
| | 1980 | 'author_name' => '\\\&\\\ & &invalid; < < &lt;', |
| | 1981 | 'author_user_agent' => '\\\&\\\ & &invalid; < < &lt;', |
| | 1982 | ), array( |
| | 1983 | 'content' => array( |
| | 1984 | 'raw' => '\\\&\\\ & &invalid; < < &lt;', |
| | 1985 | 'rendered' => '<p>\\\&\\\ & &invalid; < < &lt;' . "\n</p>", |
| | 1986 | ), |
| | 1987 | 'author_name' => '\\\&\\\ & &invalid; < < &lt;', |
| | 1988 | 'author_user_agent' => '\\\&\\\ & &invalid; < < &lt;', |
| | 1989 | ) ); |
| | 1990 | } |
| | 1991 | } |
| | 1992 | |
| | 1993 | public function test_comment_roundtrip_as_editor_unfiltered_html_1() { |
| | 1994 | wp_set_current_user( self::$editor_id ); |
| | 1995 | if ( is_multisite() ) { |
| | 1996 | $this->assertFalse( current_user_can( 'unfiltered_html' ) ); |
| | 1997 | $this->verify_comment_roundtrip( array( |
| | 1998 | 'content' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', |
| | 1999 | 'author_name' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', |
| | 2000 | 'author_user_agent' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', |
| | 2001 | ), array( |
| | 2002 | 'content' => array( |
| | 2003 | 'raw' => 'div <strong>strong</strong> oh noes', |
| | 2004 | 'rendered' => '<p>div <strong>strong</strong> oh noes</p>', |
| | 2005 | ), |
| | 2006 | 'author_name' => 'div strong', |
| | 2007 | 'author_user_agent' => 'div strong', |
| | 2008 | ) ); |
| | 2009 | } else { |
| | 2010 | $this->assertTrue( current_user_can( 'unfiltered_html' ) ); |
| | 2011 | $this->verify_comment_roundtrip( array( |
| | 2012 | 'content' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', |
| | 2013 | 'author_name' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', |
| | 2014 | 'author_user_agent' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', |
| | 2015 | ), array( |
| | 2016 | 'content' => array( |
| | 2017 | 'raw' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', |
| | 2018 | 'rendered' => "<div>div</div>\n<p> <strong>strong</strong> <script>oh noes</script></p>", |
| | 2019 | ), |
| | 2020 | 'author_name' => 'div strong', |
| | 2021 | 'author_user_agent' => 'div strong', |
| | 2022 | ) ); |
| | 2023 | } |
| | 2024 | } |
| | 2025 | |
| | 2026 | public function test_comment_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_comment_roundtrip( array( |
| | 2031 | 'content' => '<a href="#" target="_blank" data-unfiltered=true>link</a>', |
| | 2032 | 'author_name' => '<a href="#" target="_blank" data-unfiltered=true>link</a>', |
| | 2033 | 'author_user_agent' => '<a href="#" target="_blank" data-unfiltered=true>link</a>', |
| | 2034 | ), array( |
| | 2035 | 'content' => array( |
| | 2036 | 'raw' => '<a href="#" rel="nofollow">link</a>', |
| | 2037 | 'rendered' => '<p><a href="#" rel="nofollow">link</a></p>', |
| | 2038 | ), |
| | 2039 | 'author_name' => 'link', |
| | 2040 | 'author_user_agent' => 'link', |
| | 2041 | ) ); |
| | 2042 | } else { |
| | 2043 | $this->assertTrue( current_user_can( 'unfiltered_html' ) ); |
| | 2044 | $this->verify_comment_roundtrip( array( |
| | 2045 | 'content' => '<a href="#" target="_blank" data-unfiltered=true>link</a>', |
| | 2046 | 'author_name' => '<a href="#" target="_blank" data-unfiltered=true>link</a>', |
| | 2047 | 'author_user_agent' => '<a href="#" target="_blank" data-unfiltered=true>link</a>', |
| | 2048 | ), array( |
| | 2049 | 'content' => array( |
| | 2050 | 'raw' => '<a href="#" target="_blank" data-unfiltered=true rel="nofollow">link</a>', |
| | 2051 | 'rendered' => '<p><a href="#" target="_blank" data-unfiltered=true rel="nofollow">link</a></p>', |
| | 2052 | ), |
| | 2053 | 'author_name' => 'link', |
| | 2054 | 'author_user_agent' => 'link', |
| | 2055 | ) ); |
| | 2056 | } |
| | 2057 | } |
| | 2058 | |
| | 2059 | public function test_comment_roundtrip_as_superadmin_1() { |
| | 2060 | wp_set_current_user( self::$superadmin_id ); |
| | 2061 | $this->assertTrue( current_user_can( 'unfiltered_html' ) ); |
| | 2062 | $this->verify_comment_roundtrip( array( |
| | 2063 | 'content' => '\o/ ¯\_(ツ)_/¯ 🚢', |
| | 2064 | 'author_name' => '\o/ ¯\_(ツ)_/¯ 🚢', |
| | 2065 | 'author_user_agent' => '\o/ ¯\_(ツ)_/¯ 🚢', |
| | 2066 | ), array( |
| | 2067 | 'content' => array( |
| | 2068 | 'raw' => '\o/ ¯\_(ツ)_/¯ 🚢', |
| | 2069 | 'rendered' => '<p>\o/ ¯\_(ツ)_/¯ 🚢</p>', |
| | 2070 | ), |
| | 2071 | 'author_name' => '\o/ ¯\_(ツ)_/¯ 🚢', |
| | 2072 | 'author_user_agent' => '\o/ ¯\_(ツ)_/¯ 🚢', |
| | 2073 | ) ); |
| | 2074 | } |
| | 2075 | |
| | 2076 | public function test_comment_roundtrip_as_superadmin_2() { |
| | 2077 | wp_set_current_user( self::$superadmin_id ); |
| | 2078 | $this->assertTrue( current_user_can( 'unfiltered_html' ) ); |
| | 2079 | $this->verify_comment_roundtrip( array( |
| | 2080 | 'content' => '\\\&\\\ & &invalid; < < &lt;', |
| | 2081 | 'author_name' => '\\\&\\\ & &invalid; < < &lt;', |
| | 2082 | 'author_user_agent' => '\\\&\\\ & &invalid; < < &lt;', |
| | 2083 | ), array( |
| | 2084 | 'content' => array( |
| | 2085 | 'raw' => '\\\&\\\ & &invalid; < < &lt;', |
| | 2086 | 'rendered' => '<p>\\\&\\\ & &invalid; < < &lt;' . "\n</p>", |
| | 2087 | ), |
| | 2088 | 'author_name' => '\\\&\\\ & &invalid; < < &lt;', |
| | 2089 | 'author_user_agent' => '\\\&\\\ & &invalid; < < &lt;', |
| | 2090 | ) ); |
| | 2091 | } |
| | 2092 | |
| | 2093 | public function test_comment_roundtrip_as_superadmin_unfiltered_html_1() { |
| | 2094 | wp_set_current_user( self::$superadmin_id ); |
| | 2095 | $this->assertTrue( current_user_can( 'unfiltered_html' ) ); |
| | 2096 | $this->verify_comment_roundtrip( array( |
| | 2097 | 'content' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', |
| | 2098 | 'author_name' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', |
| | 2099 | 'author_user_agent' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', |
| | 2100 | ), array( |
| | 2101 | 'content' => array( |
| | 2102 | 'raw' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', |
| | 2103 | 'rendered' => "<div>div</div>\n<p> <strong>strong</strong> <script>oh noes</script></p>", |
| | 2104 | ), |
| | 2105 | 'author_name' => 'div strong', |
| | 2106 | 'author_user_agent' => 'div strong', |
| | 2107 | ) ); |
| | 2108 | } |
| | 2109 | |
| | 2110 | public function test_comment_roundtrip_as_superadmin_unfiltered_html_2() { |
| | 2111 | wp_set_current_user( self::$superadmin_id ); |
| | 2112 | $this->assertTrue( current_user_can( 'unfiltered_html' ) ); |
| | 2113 | $this->verify_comment_roundtrip( array( |
| | 2114 | 'content' => '<a href="#" target="_blank" data-unfiltered=true>link</a>', |
| | 2115 | 'author_name' => '<a href="#" target="_blank" data-unfiltered=true>link</a>', |
| | 2116 | 'author_user_agent' => '<a href="#" target="_blank" data-unfiltered=true>link</a>', |
| | 2117 | ), array( |
| | 2118 | 'content' => array( |
| | 2119 | 'raw' => '<a href="#" target="_blank" data-unfiltered=true rel="nofollow">link</a>', |
| | 2120 | 'rendered' => '<p><a href="#" target="_blank" data-unfiltered=true rel="nofollow">link</a></p>', |
| | 2121 | ), |
| | 2122 | 'author_name' => 'link', |
| | 2123 | 'author_user_agent' => 'link', |
| | 2124 | ) ); |
| | 2125 | } |
| | 2126 | |