From 9f3a80c978eb83c937674810741e4ba295f5e827 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Wed, 6 Oct 2021 21:29:36 -0700 Subject: [PATCH] Improve tests for send_validate_response --- tests/php/test-class-amp-theme-support.php | 43 ++++++++++++------- .../test-class-amp-validation-manager.php | 3 +- 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/tests/php/test-class-amp-theme-support.php b/tests/php/test-class-amp-theme-support.php index d166127dff9..475f4db55d7 100644 --- a/tests/php/test-class-amp-theme-support.php +++ b/tests/php/test-class-amp-theme-support.php @@ -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, + ], ], ]; } @@ -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( '' ); + AMP_Theme_Support::finish_init(); + $response = AMP_Theme_Support::prepare_response( '' ); $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'] ); diff --git a/tests/php/validation/test-class-amp-validation-manager.php b/tests/php/validation/test-class-amp-validation-manager.php index 92cbd83f89c..5ee13fd203a 100644 --- a/tests/php/validation/test-class-amp-validation-manager.php +++ b/tests/php/validation/test-class-amp-validation-manager.php @@ -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 );