diff --git a/tests/test-class-amp-comment-walker.php b/tests/test-class-amp-comment-walker.php new file mode 100644 index 00000000000..1e13edb8c8b --- /dev/null +++ b/tests/test-class-amp-comment-walker.php @@ -0,0 +1,129 @@ +walker = new AMP_Comment_Walker(); + } + + /** + * Test AMP_Comment_Walker::start_el. + * + * @covers AMP_Comment_Walker::start_el() + */ + public function test_start_el() { + $GLOBALS['post'] = $this->factory()->post->create(); // WPCS: global override OK. + $output = '
'; + $base_args = array( + 'format' => 'baz', + 'avatar_size' => 100, + 'max_depth' => 5, + ); + $args = array_merge( + $base_args, + array( + 'style' => 'baz', + ) + ); + $comment = $this->factory()->comment->create_and_get(); + $this->walker->start_el( $output, $comment, 0, $args ); + $this->assertContains( '
  • '; + $args = array_merge( + $base_args, + array( + 'style' => 'div', + ) + ); + $comment = $this->factory()->comment->create_and_get(); + $this->walker->start_el( $output, $comment, 0, $args ); + $this->assertContains( '
    factory()->post->create(); // WPCS: global override OK. + $comments = $this->get_comments(); + $args = array( + 'format' => 'div', + 'style' => 'baz', + 'avatar_size' => 100, + 'max_depth' => 5, + ); + $output = $this->walker->paged_walk( $comments, 5, 1, 5, $args ); + + foreach ( $comments as $comment ) { + $this->assertContains( $comment->comment_author, $output ); + $this->assertContains( $comment->comment_content, $output ); + } + } + + /** + * Test AMP_Comment_Walker::build_thread_latest_date. + * + * @covers AMP_Comment_Walker::build_thread_latest_date() + */ + public function test_build_thread_latest_date() { + $comments = $this->get_comments(); + $reflection = new ReflectionObject( $this->walker ); + $tested_method = $reflection->getMethod( 'build_thread_latest_date' ); + $tested_method->setAccessible( true ); + $latest_time = $tested_method->invoke( $this->walker, $comments ); + $comment_thread_age = $reflection->getProperty( 'comment_thread_age' ); + $comment_thread_age->setAccessible( true ); + $comment_thread_value = $comment_thread_age->getValue( $this->walker ); + + foreach ( $comments as $comment ) { + $this->assertEquals( strtotime( $comment->comment_date ), $comment_thread_value[ $comment->comment_ID ] ); + } + + $last_comment = end( $comments ); + $this->assertEquals( strtotime( $last_comment->comment_date ), $latest_time ); + } + + /** + * Gets comments for tests. + * + * @return array $comments An array of WP_Comment instances. + */ + public function get_comments() { + $comments = array(); + for ( $i = 0; $i < 5; $i++ ) { + $comments[] = $this->factory()->comment->create_and_get( array( + 'comment_date' => gmdate( 'Y-m-d H:i:s', ( time() + $i ) ), // Ensure each comment has a different date. + ) ); + } + return $comments; + } + +} diff --git a/tests/test-class-amp-comments-sanitizer.php b/tests/test-class-amp-comments-sanitizer.php new file mode 100644 index 00000000000..d5cfda24882 --- /dev/null +++ b/tests/test-class-amp-comments-sanitizer.php @@ -0,0 +1,131 @@ +factory()->post->create_and_get(); // WPCS: global override ok. + $this->dom = new DOMDocument(); + $this->instance = new AMP_Comments_Sanitizer( $this->dom ); + } + + /** + * Test AMP_Comments_Sanitizer::sanitize. + * + * @covers AMP_Comments_Sanitizer::sanitize() + */ + public function test_sanitize() { + $form = $this->create_form( 'incorrect-action.php' ); + $this->instance->sanitize(); + $on = $form->getAttribute( 'on' ); + $this->assertNotContains( 'submit:AMP.setState(', $on ); + $this->assertNotContains( 'submit-error:AMP.setState(', $on ); + foreach ( $this->get_element_names() as $name ) { + $this->assertNotContains( $name, $on ); + } + + // Use an allowed action. + $form = $this->create_form( '/wp-comments-post.php' ); + $this->instance->sanitize(); + $on = $form->getAttribute( 'on' ); + $this->assertContains( 'submit:AMP.setState(', $on ); + $this->assertContains( 'submit-error:AMP.setState(', $on ); + foreach ( $this->get_element_names() as $name ) { + $this->assertContains( $name, $on ); + } + } + + /** + * Test AMP_Comments_Sanitizer::process_comment_form. + * + * @covers AMP_Comments_Sanitizer::process_comment_form() + */ + public function test_process_comment_form() { + $form = $this->create_form( '/wp-comments-post.php' ); + $reflection = new ReflectionObject( $this->instance ); + $tested_method = $reflection->getMethod( 'process_comment_form' ); + $tested_method->setAccessible( true ); + $tested_method->invoke( $this->instance, $form ); + $on = $form->getAttribute( 'on' ); + $amp_state = $this->dom->getElementsByTagName( 'amp-state' )->item( 0 ); + + $this->assertContains( 'submit:AMP.setState(', $on ); + $this->assertContains( 'submit-error:AMP.setState(', $on ); + $this->assertContains( 'submit-success:AMP.setState(', $on ); + $this->assertContains( strval( $GLOBALS['post']->ID ), $on ); + $this->assertEquals( 'script', $amp_state->firstChild->nodeName ); + + foreach ( $this->get_element_names() as $name ) { + $this->assertContains( $name, $on ); + $this->assertContains( $name, $amp_state->nodeValue ); + } + foreach ( $form->getElementsByTagName( 'input' ) as $input ) { + $on = $input->getAttribute( 'on' ); + $this->assertContains( 'change:AMP.setState(', $on ); + $this->assertContains( strval( $GLOBALS['post']->ID ), $on ); + } + } + + /** + * Creates a form for testing. + * + * @param string $action_value Value of the 'action' attribute. + * @return DomElement $form A form element. + */ + public function create_form( $action_value ) { + $form = $this->dom->createElement( 'form' ); + $this->dom->appendChild( $form ); + $form->setAttribute( 'action', $action_value ); + + foreach ( $this->get_element_names() as $name ) { + $element = $this->dom->createElement( 'input' ); + $element->setAttribute( 'name', $name ); + $element->setAttribute( 'value', $GLOBALS['post']->ID ); + $form->appendChild( $element ); + } + return $form; + } + + /** + * Gets the element names to add to the
    . + * + * @return array An array of strings to add to the . + */ + public function get_element_names() { + return array( + 'comment_post_ID', + 'foo', + 'bar', + ); + } + +}