Skip to content

Commit 1a48487

Browse files
committed
Fix Test_AMP_Validation_Manager::test_enqueue_block_validation() test
1 parent 54a0694 commit 1a48487

File tree

3 files changed

+51
-31
lines changed

3 files changed

+51
-31
lines changed

tests/php/src/Editor/EditorSupportTest.php

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@
22

33
namespace AmpProject\AmpWP\Tests\Editor;
44

5-
use AMP_Options_Manager;
65
use AmpProject\AmpWP\DependencySupport;
76
use AmpProject\AmpWP\Editor\EditorSupport;
87
use AmpProject\AmpWP\Infrastructure\Registerable;
98
use AmpProject\AmpWP\Infrastructure\Service;
10-
use AmpProject\AmpWP\Option;
9+
use AmpProject\AmpWP\Tests\Helpers\WithBlockEditorSupport;
1110
use AmpProject\AmpWP\Tests\TestCase;
1211

1312
/** @coversDefaultClass \AmpProject\AmpWP\Editor\EditorSupport */
1413
final class EditorSupportTest extends TestCase {
1514

15+
use WithBlockEditorSupport;
16+
1617
/** @var EditorSupport */
1718
private $instance;
1819

@@ -135,30 +136,4 @@ public function test_maybe_show_notice_for_gutenberg_4_9() {
135136
$inline_script = wp_scripts()->print_inline_script( 'wp-edit-post', 'after', false );
136137
$this->assertStringContainsString( 'AMP functionality is not available', $inline_script );
137138
}
138-
139-
/**
140-
* Setup test environment to ensure the correct result for ::supports_current_screen().
141-
*
142-
* @param bool $post_type_uses_block_editor Whether the post type uses the block editor.
143-
* @param bool $post_type_supports_amp Whether the post type supports AMP.
144-
* @param string $post_type Post type ID.
145-
*/
146-
private function setup_environment( $post_type_uses_block_editor, $post_type_supports_amp, $post_type = 'foo' ) {
147-
if ( $post_type_uses_block_editor ) {
148-
set_current_screen( 'post.php' );
149-
get_current_screen()->is_block_editor = $post_type_uses_block_editor;
150-
}
151-
152-
if ( $post_type_supports_amp ) {
153-
register_post_type( $post_type, [ 'public' => true ] );
154-
$GLOBALS['post'] = self::factory()->post->create( [ 'post_type' => $post_type ] );
155-
wp_set_current_user( self::factory()->user->create( [ 'role' => 'administrator' ] ) );
156-
157-
$supported_post_types = array_merge(
158-
AMP_Options_Manager::get_option( Option::SUPPORTED_POST_TYPES ),
159-
[ $post_type ]
160-
);
161-
AMP_Options_Manager::update_option( Option::SUPPORTED_POST_TYPES, $supported_post_types );
162-
}
163-
}
164139
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/**
3+
* Trait WithoutBlockPreRendering.
4+
*
5+
* @package AmpProject\AmpWP
6+
*/
7+
8+
namespace AmpProject\AmpWP\Tests\Helpers;
9+
10+
use AMP_Options_Manager;
11+
use AmpProject\AmpWP\Option;
12+
13+
/**
14+
* Helper trait to help with setting up the test environment for block editor support.
15+
*/
16+
trait WithBlockEditorSupport {
17+
18+
/**
19+
* Setup test environment to ensure the correct result for ::supports_current_screen().
20+
*
21+
* @param bool $post_type_uses_block_editor Whether the post type uses the block editor.
22+
* @param bool $post_type_supports_amp Whether the post type supports AMP.
23+
* @param string $post_type Post type ID.
24+
*/
25+
public function setup_environment( $post_type_uses_block_editor, $post_type_supports_amp, $post_type = 'foo' ) {
26+
if ( $post_type_uses_block_editor ) {
27+
set_current_screen( 'post.php' );
28+
get_current_screen()->is_block_editor = $post_type_uses_block_editor;
29+
}
30+
31+
if ( $post_type_supports_amp ) {
32+
register_post_type( $post_type, [ 'public' => true ] );
33+
$GLOBALS['post'] = self::factory()->post->create( [ 'post_type' => $post_type ] );
34+
wp_set_current_user( self::factory()->user->create( [ 'role' => 'administrator' ] ) );
35+
36+
$supported_post_types = array_merge(
37+
AMP_Options_Manager::get_option( Option::SUPPORTED_POST_TYPES ),
38+
[ $post_type ]
39+
);
40+
AMP_Options_Manager::update_option( Option::SUPPORTED_POST_TYPES, $supported_post_types );
41+
}
42+
}
43+
}

tests/php/validation/test-class-amp-validation-manager.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use AmpProject\AmpWP\Tests\DependencyInjectedTestCase;
1515
use AmpProject\AmpWP\Tests\Helpers\HandleValidation;
1616
use AmpProject\AmpWP\Tests\Helpers\PrivateAccess;
17+
use AmpProject\AmpWP\Tests\Helpers\WithBlockEditorSupport;
1718
use AmpProject\AmpWP\Tests\Helpers\WithoutBlockPreRendering;
1819
use AmpProject\Dom\Document;
1920

@@ -27,6 +28,7 @@ class Test_AMP_Validation_Manager extends DependencyInjectedTestCase {
2728

2829
use HandleValidation;
2930
use PrivateAccess;
31+
use WithBlockEditorSupport;
3032
use WithoutBlockPreRendering {
3133
setUp as public prevent_block_pre_render;
3234
}
@@ -2401,10 +2403,10 @@ public function test_enqueue_block_validation() {
24012403
$this->markTestSkipped( 'Block editor is too old.' );
24022404
}
24032405

2404-
global $post;
2405-
$post = self::factory()->post->create_and_get();
2406-
$slug = 'amp-block-validation';
2406+
$this->setup_environment( true, true );
24072407
AMP_Validation_Manager::enqueue_block_validation();
2408+
2409+
$slug = 'amp-block-validation';
24082410
$this->assertFalse( wp_script_is( $slug, 'enqueued' ) );
24092411

24102412
// Ensure not displayed when dev tools is disabled.

0 commit comments

Comments
 (0)