Skip to content

Commit

Permalink
Improve tests for send_validate_response
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter authored and delawski committed Oct 13, 2021
1 parent 6510c4d commit 5b46ffd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
43 changes: 27 additions & 16 deletions tests/php/test-class-amp-theme-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -1900,11 +1900,23 @@ public function test_prepare_response_for_validating_non_amp_page() {
/** @return array */
public function get_data_to_test_prepare_response_for_validating_amp_page() {
return [
'no-store' => [
false,
'no-store' => [
'args' => [
AMP_Validation_Manager::VALIDATE_QUERY_VAR_NONCE => AMP_Validation_Manager::get_amp_validate_nonce(),
],
],
'store' => [
true,
'store' => [
'args' => [
AMP_Validation_Manager::VALIDATE_QUERY_VAR_NONCE => AMP_Validation_Manager::get_amp_validate_nonce(),
AMP_Validation_Manager::VALIDATE_QUERY_VAR_CACHE => true,
],
],
'store_but_omit_styleshets' => [
'args' => [
AMP_Validation_Manager::VALIDATE_QUERY_VAR_NONCE => AMP_Validation_Manager::get_amp_validate_nonce(),
AMP_Validation_Manager::VALIDATE_QUERY_VAR_CACHE => true,
AMP_Validation_Manager::VALIDATE_QUERY_VAR_OMIT_STYLESHEETS => true,
],
],
];
}
Expand All @@ -1915,34 +1927,33 @@ public function get_data_to_test_prepare_response_for_validating_amp_page() {
* @dataProvider get_data_to_test_prepare_response_for_validating_amp_page
* @covers AMP_Theme_Support::prepare_response()
* @covers AMP_Validation_Manager::send_validate_response()
*
* @param bool $store Whether to store results.
*/
public function test_prepare_response_for_validating_amp_page( $store ) {
public function test_prepare_response_for_validating_amp_page( $args ) {
wp_set_current_user( self::factory()->user->create( [ 'role' => 'administrator' ] ) );
$this->set_template_mode( AMP_Theme_Support::STANDARD_MODE_SLUG );
$this->go_to( '/' );

$_GET[ AMP_Validation_Manager::VALIDATE_QUERY_VAR ] = [
AMP_Validation_Manager::VALIDATE_QUERY_VAR_NONCE => AMP_Validation_Manager::get_amp_validate_nonce(),
];
if ( $store ) {
$_GET[ AMP_Validation_Manager::VALIDATE_QUERY_VAR ][ AMP_Validation_Manager::VALIDATE_QUERY_VAR_CACHE ] = 'true';
}
$_GET[ AMP_Validation_Manager::VALIDATE_QUERY_VAR ] = $args;
AMP_Validation_Manager::init_validate_request();
$response = AMP_Theme_Support::prepare_response( '<html amp><head></head><body><amp-layout layout="bad"></amp-layout></body></html>' );
AMP_Theme_Support::finish_init();
$response = AMP_Theme_Support::prepare_response( '<html amp><head><style>body{color:red}</style></head><body><amp-layout layout="bad"></amp-layout></body></html>' );
$this->assertJson( $response );
$data = json_decode( $response, true );
$this->assertArrayHasKey( 'http_status_code', $data );
$this->assertArrayHasKey( 'php_fatal_error', $data );
$this->assertArrayHasKey( 'queried_object', $data );
$this->assertArrayHasKey( 'url', $data );
$this->assertArrayHasKey( 'stylesheets', $data );
if ( ! empty( $args[ AMP_Validation_Manager::VALIDATE_QUERY_VAR_OMIT_STYLESHEETS ] ) ) {
$this->assertArrayNotHasKey( 'stylesheets', $data );
} else {
$this->assertArrayHasKey( 'stylesheets', $data );
}
$this->assertArrayHasKey( 'results', $data );
$this->assertCount( 1, $data['results'] );
$this->assertEquals( 'SPECIFIED_LAYOUT_INVALID', $data['results'][0]['error']['code'] );
$this->assertTrue( $data['revalidated'] );

if ( $store ) {
if ( ! empty( $args[ AMP_Validation_Manager::VALIDATE_QUERY_VAR_CACHE ] ) ) {
$this->assertArrayHasKey( 'validated_url_post', $data );
$this->assertArrayHasKey( 'id', $data['validated_url_post'] );
$this->assertArrayHasKey( 'edit_link', $data['validated_url_post'] );
Expand Down
3 changes: 2 additions & 1 deletion tests/php/validation/test-class-amp-validation-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2408,14 +2408,15 @@ function () {
return;
}

$this->assertJson( $response );
$data = json_decode( $response, true );
$this->assertEquals( JSON_ERROR_NONE, json_last_error() );
$this->assertIsArray( $data );

$this->assertArrayHasKey( 'http_status_code', $data );
$this->assertArrayHasKey( 'php_fatal_error', $data );
$this->assertArrayHasKey( 'queried_object', $data );
$this->assertArrayHasKey( 'url', $data );
$this->assertFalse( $data['revalidated'] );
$this->assertEquals( $url, $data['url'] );
if ( ! empty( $args[ AMP_Validation_Manager::VALIDATE_QUERY_VAR_OMIT_STYLESHEETS ] ) ) {
$this->assertArrayNotHasKey( 'stylesheets', $data );
Expand Down

0 comments on commit 5b46ffd

Please sign in to comment.