Skip to content

Commit 652a186

Browse files
authored
Merge pull request #292 from rtCamp/test/refactor-ContentBlockResolver
tests: backfill tests for `ContentBlockResolver`
2 parents 76f85a6 + a38e479 commit 652a186

File tree

2 files changed

+215
-0
lines changed

2 files changed

+215
-0
lines changed

.changeset/weak-days-suffer.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@wpengine/wp-graphql-content-blocks": patch
3+
---
4+
5+
tests : backfill tests for ContentBlockResolver

tests/unit/ContentBlocksResolverTest.php

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,214 @@ static function ( $parsed_block ) {
139139
$this->assertEquals( 4, count( $parsed_blocks ) );
140140
$this->assertEquals( $allowed, $actual_block_names );
141141
}
142+
143+
/**
144+
* Test the wpgraphql_content_blocks_pre_resolve_blocks filter.
145+
*/
146+
public function test_pre_resolved_blocks_filter_returns_non_null() {
147+
add_filter(
148+
'wpgraphql_content_blocks_pre_resolve_blocks',
149+
static function () {
150+
return [
151+
[
152+
'blockName' => 'core/paragraph',
153+
'attrs' => [ 'content' => 'Test content' ],
154+
],
155+
];
156+
},
157+
10,
158+
);
159+
160+
$post = new Post( get_post( $this->post_id ) );
161+
162+
$resolved_blocks = $this->instance->resolve_content_blocks( $post, [] );
163+
// The filter should return a block.
164+
$this->assertCount( 1, $resolved_blocks );
165+
$this->assertEquals( 'core/paragraph', $resolved_blocks[0]['blockName'] );
166+
$this->assertEquals( 'Test content', $resolved_blocks[0]['attrs']['content'] );
167+
168+
// Cleanup.
169+
remove_all_filters( 'wpgraphql_content_blocks_pre_resolve_blocks' );
170+
}
171+
172+
/**
173+
* Tests that an empty array is returned when the post content is empty.
174+
*/
175+
public function test_returns_empty_array_for_empty_content() {
176+
$post_id = self::factory()->post->create( [ 'post_content' => '' ] );
177+
$post = new Post( get_post( $post_id ) );
178+
179+
$resolved_blocks = $this->instance->resolve_content_blocks( $post, [] );
180+
181+
// The block should be resolved from the post node.
182+
$this->assertIsArray( $resolved_blocks );
183+
$this->assertEmpty( $resolved_blocks );
184+
185+
// Cleanup.
186+
wp_delete_post( $post_id, true );
187+
}
188+
189+
/**
190+
* Tests that the wpgraphql_content_blocks_resolve_blocks filter is applied.
191+
*/
192+
public function test_filters_wpgraphql_content_blocks_resolve_blocks() {
193+
add_filter(
194+
'wpgraphql_content_blocks_resolve_blocks',
195+
static function ( $blocks, $node, $args, $allowed_block_names ) {
196+
return [ [ 'blockName' => 'core/test-filter' ] ];
197+
},
198+
10,
199+
4
200+
);
201+
202+
$post = new Post( get_post( $this->post_id ) );
203+
204+
$resolved_blocks = $this->instance->resolve_content_blocks( $post, [] );
205+
206+
// The block should be resolved from the post node.
207+
$this->assertCount( 1, $resolved_blocks );
208+
$this->assertEquals( 'core/test-filter', $resolved_blocks[0]['blockName'] );
209+
210+
// Cleanup.
211+
remove_all_filters( 'wpgraphql_content_blocks_resolve_blocks' );
212+
}
213+
214+
/**
215+
* Tests that flat and nested blocks are resolved correctly.
216+
*/
217+
public function test_inner_blocks() {
218+
$post_content = '
219+
<!-- wp:columns -->
220+
<div class="wp-block-columns">
221+
<!-- wp:column -->
222+
<div class="wp-block-column">
223+
<!-- wp:heading -->
224+
<h2>Heading</h2>
225+
<!-- /wp:heading -->
226+
<!-- wp:paragraph -->
227+
<p>Paragraph</p>
228+
<!-- /wp:paragraph -->
229+
</div>
230+
<!-- /wp:column -->
231+
<!-- wp:column -->
232+
<div class="wp-block-column">
233+
<!-- wp:heading -->
234+
<h2>Heading</h2>
235+
<!-- /wp:heading -->
236+
<!-- wp:paragraph -->
237+
<p>Paragraph</p>
238+
<!-- /wp:paragraph -->
239+
</div>
240+
<!-- /wp:column -->
241+
</div>
242+
<!-- /wp:columns -->';
243+
244+
wp_update_post(
245+
[
246+
'ID' => $this->post_id,
247+
'post_content' => $post_content,
248+
]
249+
);
250+
251+
$post = new Post( get_post( $this->post_id ) );
252+
253+
// Resolve blocks as nested.
254+
$resolved_blocks = $this->instance->resolve_content_blocks( $post, [ 'flat' => false ] );
255+
256+
$this->assertCount( 1, $resolved_blocks, 'There should be only one top-level block (columns).' );
257+
$this->assertEquals( 'core/columns', $resolved_blocks[0]['blockName'] );
258+
$this->assertNotEmpty( $resolved_blocks[0]['clientId'], 'The clientId should be set.' );
259+
$this->assertArrayNotHasKey( 'parentClientId', $resolved_blocks[0], 'The parentClientId should be empty.' );
260+
261+
$this->assertCount( 2, $resolved_blocks[0]['innerBlocks'], 'There should be two inner blocks (columns).' );
262+
263+
// Check the inner blocks.
264+
$expected_parent_client_id = $resolved_blocks[0]['clientId'];
265+
266+
foreach ( $resolved_blocks[0]['innerBlocks'] as $inner_block ) {
267+
$this->assertEquals( 'core/column', $inner_block['blockName'] );
268+
$this->assertCount( 2, $inner_block['innerBlocks'], 'There should be two inner blocks (column).' );
269+
$this->assertNotEmpty( $inner_block['clientId'], 'The clientId should be set.' );
270+
$this->assertArrayNotHasKey( 'parentClientId', $resolved_blocks[0], 'The parentClientId should only be set when flattening.' ); // @todo This is incorrect, the parentClientId should be set for nested blocks.
271+
272+
// Check the inner inner blocks.
273+
$expected_parent_client_id = $inner_block['clientId'];
274+
275+
foreach ( $inner_block['innerBlocks'] as $inner_inner_block ) {
276+
$this->assertNotEmpty( $inner_inner_block['clientId'], 'The clientId should be set.' );
277+
$this->assertArrayNotHasKey( 'parentClientId', $resolved_blocks[0], 'The parentClientId should only be set when flattening.' ); // @todo This is incorrect, the parentClientId should be set for nested blocks.
278+
}
279+
}
280+
281+
// Resolve blocks as flat.
282+
$expected_parent_client_id = null;
283+
$expected_blocks = $resolved_blocks;
284+
285+
$resolved_blocks = $this->instance->resolve_content_blocks( $post, [ 'flat' => true ] );
286+
287+
$this->assertCount( 7, $resolved_blocks, 'There should be five blocks when flattened.' );
288+
289+
// Check the top-level block (columns).
290+
$this->assertNotEmpty( $resolved_blocks[0]['clientId'], 'The clientId should be set.' );
291+
$this->assertEqualBlocks( $expected_blocks[0], $resolved_blocks[0], 'The top-level block should match.' );
292+
293+
// Check first inner block (column).
294+
$expected_parent_client_id = $resolved_blocks[0]['clientId'];
295+
$this->assertNotEmpty( $resolved_blocks[1]['clientId'], 'The clientId should be set.' );
296+
$this->assertEquals( $expected_parent_client_id, $resolved_blocks[1]['parentClientId'], 'The parentClientId should match.' );
297+
$this->assertEqualBlocks( $expected_blocks[0]['innerBlocks'][0], $resolved_blocks[1], 'The first inner block should match.' );
298+
299+
// Check first inner block children.
300+
$expected_parent_client_id = $resolved_blocks[1]['clientId'];
301+
$this->assertNotEmpty( $resolved_blocks[2]['clientId'], 'The clientId should be set.' );
302+
$this->assertEquals( $expected_parent_client_id, $resolved_blocks[2]['parentClientId'], 'The parentClientId should match.' );
303+
$this->assertEqualBlocks( $expected_blocks[0]['innerBlocks'][0]['innerBlocks'][0], $resolved_blocks[2], 'The first inner inner block should match.' );
304+
305+
$this->assertNotEmpty( $resolved_blocks[3]['clientId'], 'The clientId should be set.' );
306+
$this->assertEquals( $expected_parent_client_id, $resolved_blocks[3]['parentClientId'], 'The parentClientId should match.' );
307+
$this->assertEqualBlocks( $expected_blocks[0]['innerBlocks'][0]['innerBlocks'][1], $resolved_blocks[3], 'The second inner inner block should match.' );
308+
309+
// Check second inner block (column).
310+
$expected_parent_client_id = $resolved_blocks[0]['clientId'];
311+
$this->assertNotEmpty( $resolved_blocks[4]['clientId'], 'The clientId should be set.' );
312+
$this->assertEquals( $expected_parent_client_id, $resolved_blocks[4]['parentClientId'], 'The parentClientId should match.' );
313+
$this->assertEqualBlocks( $expected_blocks[0]['innerBlocks'][1], $resolved_blocks[4], 'The first inner block should match.' );
314+
315+
// Check second inner block children.
316+
$expected_parent_client_id = $resolved_blocks[4]['clientId'];
317+
$this->assertNotEmpty( $resolved_blocks[5]['clientId'], 'The clientId should be set.' );
318+
$this->assertEquals( $expected_parent_client_id, $resolved_blocks[5]['parentClientId'], 'The parentClientId should match.' );
319+
$this->assertEqualBlocks( $expected_blocks[0]['innerBlocks'][1]['innerBlocks'][0], $resolved_blocks[5], 'The first inner inner block should match.' );
320+
321+
$this->assertNotEmpty( $resolved_blocks[6]['clientId'], 'The clientId should be set.' );
322+
$this->assertEquals( $expected_parent_client_id, $resolved_blocks[6]['parentClientId'], 'The parentClientId should match.' );
323+
$this->assertEqualBlocks( $expected_blocks[0]['innerBlocks'][1]['innerBlocks'][1], $resolved_blocks[6], 'The second inner inner block should match.' );
324+
}
325+
326+
/**
327+
* Asserts two blocks are equal, ignoring clientId and parentClientId.
328+
*
329+
* @param array<string,mixed> $expected The expected block.
330+
* @param array<string,mixed> $actual The actual block.
331+
* @param string $message The message to display if the assertion fails.
332+
*/
333+
protected function assertEqualBlocks( $expected, $actual, $message = '' ) {
334+
// Remove clientId and parentClientId from comparison.
335+
unset( $expected['clientId'] );
336+
unset( $expected['parentClientId'] );
337+
unset( $actual['clientId'] );
338+
unset( $actual['parentClientId'] );
339+
340+
$expected_inner_blocks = $expected['innerBlocks'] ?? [];
341+
$actual_inner_blocks = $actual['innerBlocks'] ?? [];
342+
343+
unset( $expected['innerBlocks'] );
344+
unset( $actual['innerBlocks'] );
345+
346+
$this->assertEquals( $expected, $actual, $message );
347+
348+
foreach ( $expected_inner_blocks as $index => $expected_inner_block ) {
349+
$this->assertEqualBlocks( $expected_inner_block, $actual_inner_blocks[ $index ], $message );
350+
}
351+
}
142352
}

0 commit comments

Comments
 (0)