Skip to content

Commit 0ef9f04

Browse files
committed
Add tests for modifying the eager-loaded post meta
- Test for 'short circuit' where no meta should be eager loaded - Test for custom list of eager-loaded post meta
1 parent 3463b0d commit 0ef9f04

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

tests/test-timber-post.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,47 @@ function testPagedContent(){
319319
$this->assertEquals($page2, trim(strip_tags( $post->get_paged_content() )));
320320
}
321321

322+
function testMetaCustomPreFilterDisable(){
323+
324+
$callable = function(){ return false; };
325+
326+
add_filter( 'timber_post_get_meta_pre', $callable );
327+
328+
$post_id = $this->factory->post->create();
329+
330+
update_post_meta($post_id, 'hidden_value', 'Super secret value');
331+
332+
$post = new TimberPost($post_id);
333+
334+
$this->assertCount( 0, $post->custom);
335+
336+
remove_filter( 'timber_post_get_meta_pre', $callable );
337+
}
338+
339+
function testMetaCustomPreFilterAlter(){
340+
341+
$callable = function( $customs, $pid, $post ) {
342+
$key = 'critical_value';
343+
344+
return [
345+
$key => get_post_meta( $pid, $key ),
346+
];
347+
};
348+
349+
add_filter( 'timber_post_get_meta_pre', $callable , 10, 3);
350+
351+
$post_id = $this->factory->post->create();
352+
353+
update_post_meta($post_id, 'hidden_value', 'super-big-secret');
354+
update_post_meta($post_id, 'critical_value', 'I am needed, all the time');
355+
356+
$post = new TimberPost($post_id);
357+
$this->assertCount( 1, $post->custom );
358+
$this->assertEquals( $post->custom, array( 'critical_value' => 'I am needed, all the time' ) );
359+
360+
remove_filter( 'timber_post_get_meta_pre', $callable );
361+
}
362+
322363
function testMetaCustomArrayFilter(){
323364
add_filter('timber_post_get_meta', function($customs){
324365
foreach($customs as $key=>$value){

0 commit comments

Comments
 (0)