6
6
use AmpProject \AmpWP \Editor \EditorSupport ;
7
7
use AmpProject \AmpWP \Infrastructure \Registerable ;
8
8
use AmpProject \AmpWP \Infrastructure \Service ;
9
+ use AmpProject \AmpWP \Tests \Helpers \WithBlockEditorSupport ;
9
10
use AmpProject \AmpWP \Tests \TestCase ;
10
11
11
12
/** @coversDefaultClass \AmpProject\AmpWP\Editor\EditorSupport */
12
13
final class EditorSupportTest extends TestCase {
13
14
15
+ use WithBlockEditorSupport;
16
+
14
17
/** @var EditorSupport */
15
18
private $ instance ;
16
19
17
20
public function setUp () {
18
21
parent ::setUp ();
19
22
20
23
$ this ->instance = new EditorSupport ( new DependencySupport () );
24
+
25
+ unset( $ GLOBALS ['current_screen ' ], $ GLOBALS ['wp_scripts ' ] );
21
26
}
22
27
23
28
public function test_it_can_be_initialized () {
@@ -33,20 +38,34 @@ public function test_register() {
33
38
$ this ->assertEquals ( 99 , has_action ( 'admin_enqueue_scripts ' , [ $ this ->instance , 'maybe_show_notice ' ] ) );
34
39
}
35
40
36
- public function test_editor_supports_amp_block_editor_features () {
37
- if (
38
- defined ( 'GUTENBERG_VERSION ' )
39
- &&
40
- version_compare ( GUTENBERG_VERSION , DependencySupport::GB_MIN_VERSION , '>= ' )
41
- ) {
42
- $ this ->assertTrue ( $ this ->instance ->editor_supports_amp_block_editor_features () );
43
- } else {
44
- if ( version_compare ( get_bloginfo ( 'version ' ), DependencySupport::WP_MIN_VERSION , '>= ' ) ) {
45
- $ this ->assertTrue ( $ this ->instance ->editor_supports_amp_block_editor_features () );
46
- } else {
47
- $ this ->assertFalse ( $ this ->instance ->editor_supports_amp_block_editor_features () );
48
- }
49
- }
41
+ /**
42
+ * Test data for test_supports_current_screen().
43
+ *
44
+ * @return array
45
+ */
46
+ public function get_data_for_test_supports_current_screen () {
47
+ return [
48
+ 'supports post type and amp ' => [ true , true , true ],
49
+ 'supports only post type ' => [ true , false , false ],
50
+ 'supports only amp ' => [ false , true , false ],
51
+ 'does not support post type nor amp ' => [ false , false , false ],
52
+ ];
53
+ }
54
+
55
+ /**
56
+ * @covers ::is_current_screen_block_editor_for_amp_enabled_post_type()
57
+ * @dataProvider get_data_for_test_supports_current_screen()
58
+ *
59
+ * @param bool $post_type_uses_block_editor Whether post type can be edited in the block editor.
60
+ * @param bool $post_type_supports_amp Whether post type supports AMP.
61
+ * @param bool $expected_result Expected result for test assertions.
62
+ */
63
+ public function test_supports_current_screen ( $ post_type_uses_block_editor , $ post_type_supports_amp , $ expected_result ) {
64
+ $ this ->setup_environment ( $ post_type_uses_block_editor , $ post_type_supports_amp );
65
+
66
+ // Note: Without Gutenberg being installed on WP 4.9, the expected result would be `false`
67
+ // when `$post_type_uses_block_editor` and `$post_type_supports_amp` are `true`.
68
+ $ this ->assertSame ( $ expected_result , $ this ->instance ->is_current_screen_block_editor_for_amp_enabled_post_type () );
50
69
}
51
70
52
71
/** @covers ::maybe_show_notice() */
@@ -57,110 +76,56 @@ public function test_dont_show_notice_if_no_screen_defined() {
57
76
58
77
/** @covers ::maybe_show_notice() */
59
78
public function test_dont_show_notice_for_unsupported_post_type () {
60
- global $ post ;
61
-
62
- set_current_screen ( 'post.php ' );
63
- register_post_type ( 'my-post-type ' );
64
- $ post = $ this ->factory ()->post ->create ( [ 'post_type ' => 'my-post-type ' ] );
65
- setup_postdata ( get_post ( $ post ) );
66
-
67
- wp_set_current_user ( $ this ->factory ()->user ->create ( [ 'role ' => 'administrator ' ] ) );
79
+ $ this ->setup_environment ( true , false );
68
80
69
81
$ this ->instance ->maybe_show_notice ();
70
82
$ this ->assertFalse ( wp_scripts ()->print_inline_script ( 'wp-edit-post ' , 'after ' , false ) );
71
- unset( $ GLOBALS ['current_screen ' ] );
72
- unset( $ GLOBALS ['wp_scripts ' ] );
73
83
}
74
84
75
85
/** @covers ::maybe_show_notice() */
76
86
public function test_show_notice_for_supported_post_type () {
77
- global $ post ;
78
-
79
87
if ( version_compare ( get_bloginfo ( 'version ' ), DependencySupport::WP_MIN_VERSION , '< ' ) ) {
80
88
$ this ->markTestSkipped ();
81
89
}
82
90
83
- set_current_screen ( 'post.php ' );
84
- $ post = $ this ->factory ()->post ->create ();
85
- setup_postdata ( get_post ( $ post ) );
86
-
87
- wp_set_current_user ( $ this ->factory ()->user ->create ( [ 'role ' => 'administrator ' ] ) );
91
+ $ this ->setup_environment ( true , true );
88
92
89
93
$ this ->instance ->maybe_show_notice ();
90
- if ( $ this ->instance ->editor_supports_amp_block_editor_features () ) {
94
+ if ( $ this ->instance ->is_current_screen_block_editor_for_amp_enabled_post_type () ) {
91
95
$ this ->assertFalse ( wp_scripts ()->print_inline_script ( 'wp-edit-post ' , 'after ' , false ) );
92
96
} else {
93
97
$ this ->assertStringContainsString (
94
98
'AMP functionality is not available ' ,
95
99
wp_scripts ()->print_inline_script ( 'wp-edit-post ' , 'after ' , false )
96
100
);
97
101
}
98
- unset( $ GLOBALS ['current_screen ' ] );
99
- unset( $ GLOBALS ['wp_scripts ' ] );
100
102
}
101
103
102
104
/** @covers ::maybe_show_notice() */
103
105
public function test_maybe_show_notice_for_unsupported_user () {
104
- global $ post ;
105
-
106
- set_current_screen ( 'post.php ' );
107
- $ post = $ this ->factory ()->post ->create ();
108
- setup_postdata ( get_post ( $ post ) );
106
+ $ this ->setup_environment ( true , true );
107
+ wp_set_current_user ( self ::factory ()->user ->create () );
109
108
110
109
$ this ->instance ->maybe_show_notice ();
111
110
112
111
$ this ->assertFalse ( wp_scripts ()->print_inline_script ( 'wp-edit-post ' , 'after ' , false ) );
113
- unset( $ GLOBALS ['current_screen ' ] );
114
- unset( $ GLOBALS ['wp_scripts ' ] );
115
- }
116
-
117
- /** @covers ::maybe_show_notice() */
118
- public function test_maybe_show_notice_with_cpt_supporting_gutenberg_but_not_amp () {
119
- global $ post ;
120
-
121
- if ( ! $ this ->instance ->editor_supports_amp_block_editor_features () ) {
122
- $ this ->markTestSkipped ();
123
- }
124
-
125
- register_post_type (
126
- 'my-gb-post-type ' ,
127
- [
128
- 'public ' => true ,
129
- 'show_in_rest ' => true ,
130
- 'supports ' => [ 'editor ' ],
131
- ]
132
- );
133
-
134
- set_current_screen ( 'post.php ' );
135
- $ post = $ this ->factory ()->post ->create ( [ 'post_type ' => 'my-gb-post-type ' ] );
136
- setup_postdata ( get_post ( $ post ) );
137
- wp_set_current_user ( self ::factory ()->user ->create ( [ 'role ' => 'administrator ' ] ) );
138
-
139
- $ this ->instance ->maybe_show_notice ();
140
- $ this ->assertFalse ( wp_scripts ()->print_inline_script ( 'wp-edit-post ' , 'after ' , false ) );
141
- unset( $ GLOBALS ['current_screen ' ] );
142
- unset( $ GLOBALS ['wp_scripts ' ] );
143
112
}
144
113
145
114
/** @covers ::maybe_show_notice() */
146
115
public function test_maybe_show_notice_for_gutenberg_4_9 () {
147
- global $ post ;
148
116
if ( ! defined ( 'GUTENBERG_VERSION ' ) || version_compare ( GUTENBERG_VERSION , '4.9.0 ' , '> ' ) ) {
149
117
$ this ->markTestSkipped ( 'Test only applicable to Gutenberg v4.9.0 and older. ' );
150
118
}
151
119
152
- $ this ->assertFalse ( $ this ->instance ->editor_supports_amp_block_editor_features () );
120
+ $ this ->setup_environment ( true , true );
121
+ // WP < 5.0 doesn't include the block editor, but Gutenberg would be installed, so it should be supported.
122
+ $ this ->assertTrue ( $ this ->instance ->is_current_screen_block_editor_for_amp_enabled_post_type () );
153
123
154
124
gutenberg_register_packages_scripts ();
155
- set_current_screen ( 'post.php ' );
156
- $ post = $ this ->factory ()->post ->create ();
157
- setup_postdata ( get_post ( $ post ) );
158
125
wp_set_current_user ( self ::factory ()->user ->create ( [ 'role ' => 'administrator ' ] ) );
159
126
160
127
$ this ->instance ->maybe_show_notice ();
161
128
$ inline_script = wp_scripts ()->print_inline_script ( 'wp-edit-post ' , 'after ' , false );
162
129
$ this ->assertStringContainsString ( 'AMP functionality is not available ' , $ inline_script );
163
- unset( $ GLOBALS ['current_screen ' ] );
164
- unset( $ GLOBALS ['wp_scripts ' ] );
165
130
}
166
131
}
0 commit comments