This repository has been archived by the owner on May 20, 2024. It is now read-only.
forked from ampproject/amp-wp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-class-amp-meta-box.php
157 lines (136 loc) · 4.76 KB
/
test-class-amp-meta-box.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
/**
* Tests for AMP_Post_Meta_Box.
*
* @package AMP
*/
/**
* Tests for AMP_Post_Meta_Box.
*/
class Test_AMP_Post_Meta_Box extends WP_UnitTestCase {
/**
* Instance of AMP_Post_Meta_Box
*
* @var AMP_Post_Meta_Box
*/
public $instance;
/**
* Setup.
*
* @inheritdoc
*/
public function setUp() {
parent::setUp();
$this->instance = new AMP_Post_Meta_Box();
}
/**
* Test init.
*
* @see AMP_Settings::init()
*/
public function test_init() {
$this->instance->init();
$this->assertEquals( 10, has_action( 'admin_enqueue_scripts', array( $this->instance, 'enqueue_admin_assets' ) ) );
$this->assertEquals( 10, has_action( 'post_submitbox_misc_actions', array( $this->instance, 'render_status' ) ) );
$this->assertEquals( 10, has_action( 'save_post', array( $this->instance, 'save_amp_status' ) ) );
}
/**
* Test enqueue_admin_assets.
*
* @see AMP_Settings::enqueue_admin_assets()
*/
public function test_enqueue_admin_assets() {
// Test enqueue outside of a post with AMP support.
$this->assertFalse( wp_style_is( AMP_Post_Meta_Box::ASSETS_HANDLE ) );
$this->assertFalse( wp_script_is( AMP_Post_Meta_Box::ASSETS_HANDLE ) );
$this->instance->enqueue_admin_assets( 'foo-bar.php' );
$this->assertFalse( wp_style_is( AMP_Post_Meta_Box::ASSETS_HANDLE ) );
// Test enqueue on a post with AMP support.
$post = self::factory()->post->create_and_get();
$GLOBALS['post'] = $post;
set_current_screen( 'post.php' );
$this->instance->enqueue_admin_assets();
$this->assertTrue( wp_style_is( AMP_Post_Meta_Box::ASSETS_HANDLE ) );
$this->assertTrue( wp_script_is( AMP_Post_Meta_Box::ASSETS_HANDLE ) );
$script_data = wp_scripts()->get_data( AMP_Post_Meta_Box::ASSETS_HANDLE, 'after' );
if ( empty( $script_data ) ) {
$this->markTestIncomplete( 'Script data could not be found.' );
}
// Test inline script boot.
$this->assertTrue( false !== stripos( wp_json_encode( $script_data ), 'ampPostMetaBox.boot(' ) );
unset( $GLOBALS['post'] );
}
/**
* Test render_status.
*
* @see AMP_Settings::render_status()
*/
public function test_render_status() {
$post = $this->factory->post->create_and_get();
wp_set_current_user( $this->factory->user->create( array(
'role' => 'administrator',
) ) );
ob_start();
$this->instance->render_status( $post );
$this->assertContains( '<div class="misc-pub-section misc-amp-status"', ob_get_clean() );
remove_post_type_support( 'post', AMP_QUERY_VAR );
ob_start();
$this->instance->render_status( $post );
$this->assertEmpty( ob_get_clean() );
add_post_type_support( 'post', AMP_QUERY_VAR );
wp_set_current_user( $this->factory->user->create( array(
'role' => 'subscriber',
) ) );
ob_start();
$this->instance->render_status( $post );
$this->assertEmpty( ob_get_clean() );
}
/**
* Test save_amp_status.
*
* @see AMP_Settings::save_amp_status()
*/
public function test_save_amp_status() {
// Test failure.
$post_id = $this->factory->post->create();
$this->assertEmpty( get_post_meta( $post_id, AMP_Post_Meta_Box::STATUS_POST_META_KEY, true ) );
// Setup for success.
wp_set_current_user( $this->factory->user->create( array(
'role' => 'administrator',
) ) );
$_POST[ AMP_Post_Meta_Box::NONCE_NAME ] = wp_create_nonce( AMP_Post_Meta_Box::NONCE_ACTION );
$_POST[ AMP_Post_Meta_Box::STATUS_INPUT_NAME ] = 'disabled';
// Test revision bail.
$post_id = $this->factory->post->create();
delete_post_meta( $post_id, AMP_Post_Meta_Box::STATUS_POST_META_KEY );
wp_save_post_revision( $post_id );
$this->assertEmpty( get_post_meta( $post_id, AMP_Post_Meta_Box::STATUS_POST_META_KEY, true ) );
// Test post update success to disable.
$post_id = $this->factory->post->create();
delete_post_meta( $post_id, AMP_Post_Meta_Box::STATUS_POST_META_KEY );
wp_update_post( array(
'ID' => $post_id,
'post_title' => 'updated',
) );
$this->assertTrue( (bool) get_post_meta( $post_id, AMP_Post_Meta_Box::STATUS_POST_META_KEY, true ) );
// Test post update success to enable.
$_POST[ AMP_Post_Meta_Box::STATUS_INPUT_NAME ] = 'enabled';
delete_post_meta( $post_id, AMP_Post_Meta_Box::STATUS_POST_META_KEY );
wp_update_post( array(
'ID' => $post_id,
'post_title' => 'updated',
) );
$this->assertEquals( AMP_Post_Meta_Box::ENABLED_STATUS, get_post_meta( $post_id, AMP_Post_Meta_Box::STATUS_POST_META_KEY, true ) );
}
/**
* Test preview_post_link.
*
* @see AMP_Settings::preview_post_link()
*/
public function test_preview_post_link() {
$link = 'https://foo.bar';
$this->assertEquals( 'https://foo.bar', $this->instance->preview_post_link( $link ) );
$_POST['amp-preview'] = 'do-preview';
$this->assertEquals( 'https://foo.bar?' . AMP_QUERY_VAR . '=1', $this->instance->preview_post_link( $link ) );
}
}