-
Notifications
You must be signed in to change notification settings - Fork 381
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1049 from Automattic/add/868-comments-unit-tests
- Loading branch information
Showing
2 changed files
with
260 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
<?php | ||
/** | ||
* Tests for AMP_Comment_Walker class. | ||
* | ||
* @package AMP | ||
*/ | ||
|
||
/** | ||
* Tests for AMP_Comment_Walker class. | ||
* | ||
* @since 0.7 | ||
*/ | ||
class Test_AMP_Comment_Walker extends WP_UnitTestCase { | ||
|
||
/** | ||
* The comment walker. | ||
* | ||
* @var AMP_Comment_Walker | ||
*/ | ||
public $walker; | ||
|
||
/** | ||
* Setup. | ||
* | ||
* @inheritdoc | ||
*/ | ||
public function setUp() { | ||
parent::setUp(); | ||
$this->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 = '<div></div>'; | ||
$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( '<li data-sort-time=', $output ); | ||
$this->assertContains( $comment->comment_ID, $output ); | ||
$this->assertContains( strval( strtotime( $comment->comment_date ) ), $output ); | ||
|
||
$output = '<div></div>'; | ||
$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( '<div data-sort-time=', $output ); | ||
} | ||
|
||
/** | ||
* Test AMP_Comment_Walker::paged_walk. | ||
* | ||
* @covers AMP_Comment_Walker::paged_walk() | ||
*/ | ||
public function test_paged_walk() { | ||
$GLOBALS['post'] = $this->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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
<?php | ||
/** | ||
* Tests for AMP_Comments_Sanitizer class. | ||
* | ||
* @package AMP | ||
*/ | ||
|
||
/** | ||
* Tests for AMP_Comments_Sanitizer class. | ||
* | ||
* @since 0.7 | ||
*/ | ||
class Test_AMP_Comments_Sanitizer extends WP_UnitTestCase { | ||
|
||
/** | ||
* The tested instance. | ||
* | ||
* @var AMP_Comments_Sanitizer | ||
*/ | ||
public $instance; | ||
|
||
/** | ||
* Representation of the DOM. | ||
* | ||
* @var DOMDocument | ||
*/ | ||
public $dom; | ||
|
||
/** | ||
* Setup. | ||
* | ||
* @inheritdoc | ||
*/ | ||
public function setUp() { | ||
parent::setUp(); | ||
$GLOBALS['post'] = $this->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 <form>. | ||
* | ||
* @return array An array of strings to add to the <form>. | ||
*/ | ||
public function get_element_names() { | ||
return array( | ||
'comment_post_ID', | ||
'foo', | ||
'bar', | ||
); | ||
} | ||
|
||
} |