forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetTheExcerpt.php
More file actions
207 lines (180 loc) · 4.69 KB
/
getTheExcerpt.php
File metadata and controls
207 lines (180 loc) · 4.69 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
/**
* @group post
* @group formatting
*/
class Tests_Post_GetTheExcerpt extends WP_UnitTestCase {
/**
* @ticket 27246
*/
public function test_the_excerpt_invalid_post() {
$this->assertSame( '', get_echo( 'the_excerpt' ) );
$this->assertSame( '', get_the_excerpt() );
}
/**
* @ticket 27246
* @expectedDeprecated get_the_excerpt
*/
public function test_the_excerpt_deprecated() {
$this->assertSame( '', get_the_excerpt( true ) );
$this->assertSame( '', get_the_excerpt( false ) );
}
/**
* @ticket 27246
*/
public function test_the_excerpt() {
$GLOBALS['post'] = self::factory()->post->create_and_get( array( 'post_excerpt' => 'Post excerpt' ) );
$this->assertSame( "<p>Post excerpt</p>\n", get_echo( 'the_excerpt' ) );
$this->assertSame( 'Post excerpt', get_the_excerpt() );
}
/**
* @ticket 27246
* @ticket 35486
*/
public function test_the_excerpt_password_protected_post() {
$post = self::factory()->post->create_and_get(
array(
'post_excerpt' => 'Post excerpt',
'post_password' => '1234',
)
);
$this->assertSame( 'There is no excerpt because this is a protected post.', get_the_excerpt( $post ) );
$GLOBALS['post'] = $post;
$this->assertSame( "<p>There is no excerpt because this is a protected post.</p>\n", get_echo( 'the_excerpt' ) );
}
/**
* @ticket 27246
*/
public function test_the_excerpt_specific_post() {
$GLOBALS['post'] = self::factory()->post->create_and_get( array( 'post_excerpt' => 'Foo' ) );
$post_id = self::factory()->post->create( array( 'post_excerpt' => 'Bar' ) );
$this->assertSame( 'Bar', get_the_excerpt( $post_id ) );
}
/**
* @ticket 42814
*/
public function test_should_fall_back_on_post_content_if_excerpt_is_empty_and_post_is_inferred_from_context() {
$post_id = self::factory()->post->create(
array(
'post_content' => 'Foo',
'post_excerpt' => '',
)
);
$q = new WP_Query(
array(
'p' => $post_id,
)
);
while ( $q->have_posts() ) {
$q->the_post();
$found = get_the_excerpt();
}
$this->assertSame( 'Foo', $found );
}
/**
* @ticket 42814
*/
public function test_should_fall_back_on_post_content_if_excerpt_is_empty_and_post_is_provided() {
$GLOBALS['post'] = self::factory()->post->create_and_get(
array(
'post_content' => 'Foo',
'post_excerpt' => '',
)
);
$this->assertSame( 'Foo', get_the_excerpt( $GLOBALS['post'] ) );
}
/**
* @ticket 42814
*/
public function test_should_respect_post_parameter_in_the_loop() {
$p1 = self::factory()->post->create_and_get( array( 'post_excerpt' => 'Foo' ) );
$p2 = self::factory()->post->create_and_get( array( 'post_excerpt' => 'Bar' ) );
$q = new WP_Query(
array(
'p' => $p1->ID,
)
);
while ( $q->have_posts() ) {
$q->the_post();
$found = get_the_excerpt( $p2 );
}
$this->assertSame( 'Bar', $found );
}
/**
* @ticket 42814
*/
public function test_should_respect_post_parameter_in_the_loop_when_falling_back_on_post_content() {
$p1 = self::factory()->post->create_and_get(
array(
'post_content' => 'Foo',
'post_excerpt' => '',
)
);
$p2 = self::factory()->post->create_and_get(
array(
'post_content' => 'Bar',
'post_excerpt' => '',
)
);
$q = new WP_Query(
array(
'p' => $p1->ID,
)
);
while ( $q->have_posts() ) {
$q->the_post();
$found = get_the_excerpt( $p2 );
}
$this->assertSame( 'Bar', $found );
}
/**
* @ticket 53604
*/
public function test_inner_blocks_excerpt() {
$content_1 = '<!-- wp:group -->
<div class="wp-block-group"><!-- wp:columns -->
<div class="wp-block-columns"><!-- wp:column -->
<div class="wp-block-column"><!-- wp:paragraph -->
<p>Column 1</p>
<!-- /wp:paragraph --></div>
<!-- /wp:column -->
<!-- wp:column -->
<div class="wp-block-column"><!-- wp:paragraph -->
<p>Column 2</p>
<!-- /wp:paragraph --></div>
<!-- /wp:column --></div>
<!-- /wp:columns --></div>
<!-- /wp:group -->
<!-- wp:paragraph -->
<p></p>
<!-- /wp:paragraph -->';
$content_2 = '<!-- wp:group -->
<div class="wp-block-group"><!-- wp:paragraph -->
<p>Paragraph inside group block</p>
<!-- /wp:paragraph --></div>
<!-- /wp:group -->
<!-- wp:paragraph -->
<p></p>
<!-- /wp:paragraph -->';
$post_1 = self::factory()->post->create_and_get(
array(
'post_content' => $content_1,
'post_excerpt' => '',
)
);
$post_2 = self::factory()->post->create_and_get(
array(
'post_content' => $content_2,
'post_excerpt' => '',
)
);
$this->assertSame(
'Column 1 Column 2',
get_the_excerpt( ( new WP_Query( array( 'p' => $post_1->ID ) ) )->posts[0] )
);
$this->assertSame(
'Paragraph inside group block',
get_the_excerpt( ( new WP_Query( array( 'p' => $post_2->ID ) ) )->posts[0] )
);
}
}