Skip to content

Commit

Permalink
ref #602 added tests for shortcodes
Browse files Browse the repository at this point in the history
  • Loading branch information
jarednova committed Jul 29, 2015
1 parent 07a71c0 commit e215afe
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 37 deletions.
1 change: 0 additions & 1 deletion lib/timber-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,6 @@ function get_preview($len = 50, $force = false, $readmore = 'Read More', $strip
}
if (!strlen($text)) {
$text = TimberHelper::trim_words($this->get_content(), $len, false);
$text = do_shortcode($text);
$trimmed = true;
}
if (!strlen(trim($text))) {
Expand Down
60 changes: 60 additions & 0 deletions tests/test-timber-post-preview.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

class TestTimberPostPreview extends WP_UnitTestCase {

function testDoubleEllipsis(){
$post_id = $this->factory->post->create();
$post = new TimberPost($post_id);
$post->post_excerpt = 'this is super dooper trooper long words';
$prev = $post->get_preview(3, true);
$this->assertEquals(1, substr_count($prev, '&hellip;'));
}

function testGetPreview() {
global $wp_rewrite;
$struc = false;
$wp_rewrite->permalink_structure = $struc;
update_option('permalink_structure', $struc);
$post_id = $this->factory->post->create(array('post_content' => 'this is super dooper trooper long words'));
$post = new TimberPost($post_id);

// no excerpt
$post->post_excerpt = '';
$preview = $post->get_preview(3);
$this->assertRegExp('/this is super &hellip; <a href="http:\/\/example.org\/\?p=\d+" class="read-more">Read More<\/a>/', $preview);

// excerpt set, force is false, no read more
$post->post_excerpt = 'this is excerpt longer than three words';
$preview = $post->get_preview(3, false, '');
$this->assertEquals($preview, $post->post_excerpt);

// custom read more set
$post->post_excerpt = '';
$preview = $post->get_preview(3, false, 'Custom more');
$this->assertRegExp('/this is super &hellip; <a href="http:\/\/example.org\/\?p=\d+" class="read-more">Custom more<\/a>/', $preview);

// content with <!--more--> tag, force false
$post->post_content = 'this is super dooper<!--more--> trooper long words';
$preview = $post->get_preview(2, false, '');
$this->assertEquals('this is super dooper', $preview);
}

function testShortcodesInPreviewFromContent() {
add_shortcode('mythang', function($text) {
return 'mythangy';
});
$pid = $this->factory->post->create( array('post_content' => 'jared [mythang]', 'post_excerpt' => '') );
$post = new TimberPost( $pid );
$this->assertEquals('jared mythangy &hellip; <a href="'.$post->link().'" class="read-more">Read More</a>', $post->get_preview());
}

function testShortcodesInPreviewFromContentWithMoreTag() {
add_shortcode('duck', function($text) {
return 'Quack!';
});
$pid = $this->factory->post->create( array('post_content' => 'jared [duck] <!--more--> joojoo', 'post_excerpt' => '') );
$post = new TimberPost( $pid );
$this->assertEquals('jared Quack! <a href="'.$post->link().'" class="read-more">Read More</a>', $post->get_preview());
}

}
37 changes: 1 addition & 36 deletions tests/test-timber-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,14 +236,6 @@ function testUpdate(){
$this->assertEquals($rand, $post->test_meta);
}

function testDoubleEllipsis(){
$post_id = $this->factory->post->create();
$post = new TimberPost($post_id);
$post->post_excerpt = 'this is super dooper trooper long words';
$prev = $post->get_preview(3, true);
$this->assertEquals(1, substr_count($prev, '&hellip;'));
}

function testCanEdit(){
wp_set_current_user(1);
$post_id = $this->factory->post->create(array('post_author' => 1));
Expand All @@ -252,34 +244,7 @@ function testCanEdit(){
wp_set_current_user(0);
}

function testGetPreview() {
global $wp_rewrite;
$struc = false;
$wp_rewrite->permalink_structure = $struc;
update_option('permalink_structure', $struc);
$post_id = $this->factory->post->create(array('post_content' => 'this is super dooper trooper long words'));
$post = new TimberPost($post_id);

// no excerpt
$post->post_excerpt = '';
$preview = $post->get_preview(3);
$this->assertRegExp('/this is super &hellip; <a href="http:\/\/example.org\/\?p=\d+" class="read-more">Read More<\/a>/', $preview);

// excerpt set, force is false, no read more
$post->post_excerpt = 'this is excerpt longer than three words';
$preview = $post->get_preview(3, false, '');
$this->assertEquals($preview, $post->post_excerpt);

// custom read more set
$post->post_excerpt = '';
$preview = $post->get_preview(3, false, 'Custom more');
$this->assertRegExp('/this is super &hellip; <a href="http:\/\/example.org\/\?p=\d+" class="read-more">Custom more<\/a>/', $preview);

// content with <!--more--> tag, force false
$post->post_content = 'this is super dooper<!--more--> trooper long words';
$preview = $post->get_preview(2, false, '');
$this->assertEquals('this is super dooper', $preview);
}


function testTitle(){
$title = 'Fifteen Million Merits';
Expand Down

0 comments on commit e215afe

Please sign in to comment.