Skip to content

Commit

Permalink
Merge pull request #795 from jarednova/tests_comments
Browse files Browse the repository at this point in the history
Tests comments
  • Loading branch information
jarednova committed Dec 16, 2015
2 parents 75c263b + 1ec8cc5 commit d88c60a
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 83 deletions.
3 changes: 0 additions & 3 deletions lib/timber-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,6 @@ protected function prepare_post_info( $pid = 0 ) {
$post = get_post($pid);
if ( $post ) {
return $post;
} else {
$post = get_page($pid);
return $post;
}
}
//we can skip if already is WP_Post
Expand Down
140 changes: 140 additions & 0 deletions tests/test-timber-comment-avatar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<?php

class TestTimberCommentAvatar extends Timber_UnitTestCase {

function testAvatarSize() {
if ( !TestTimberImage::is_connected() ) {
$this->markTestSkipped('Cannot test avatar images when not connected to internet');
}
$post_id = $this->factory->post->create();
$comment_id = $this->factory->comment->create(array('comment_post_ID' => $post_id));
$comment = new TimberComment($comment_id);

# test default gravatr holding image
$avatar = $comment->avatar("mystery");

$this->assertTrue(substr ( $avatar , 0, 5 ) == "http:");
}

function testAvatarFalse() {
update_option('show_avatars', false);
$post_id = $this->factory->post->create();
$comment_id = $this->factory->comment->create(array('comment_post_ID' => $post_id));
$comment = new TimberComment($comment_id);

# test default gravatr holding image
$avatar = $comment->avatar();

$this->assertFalse($avatar);
}

function testAvatarBlank() {
if ( !TestTimberImage::is_connected() ) {
$this->markTestSkipped('Cannot test avatar images when not connected to internet');
}
$post_id = $this->factory->post->create();
$comment_id = $this->factory->comment->create(array('comment_post_ID' => $post_id));
$comment = new TimberComment($comment_id);

# test default gravatr holding image
$avatar = $comment->avatar(92, "blank");

$this->assertTrue(substr ( $avatar , 0, 5 ) == "http:");
}

function testAvatarGravatarDefault() {
if ( !TestTimberImage::is_connected() ) {
$this->markTestSkipped('Cannot test avatar images when not connected to internet');
}
$post_id = $this->factory->post->create();
$comment_id = $this->factory->comment->create(array('comment_post_ID' => $post_id));
$comment = new TimberComment($comment_id);

# test default gravatr holding image
$avatar = $comment->avatar(92, "gravatar_default");

$this->assertTrue(substr ( $avatar , 0, 5 ) == "http:");
}

function testGravatar() {
if (!TestTimberImage::is_connected()){
$this->markTestSkipped('Cannot test avatar images when not connected to internet');
}
$post_id = $this->factory->post->create();
$comment_id = $this->factory->comment->create(array('comment_post_ID' => $post_id, 'comment_author' => 'jarednova', 'comment_author_email' => 'jarednova@upstatement.com'));
$comment = new TimberComment($comment_id);
$gravatar = md5(file_get_contents($comment->avatar()));
$this->assertEquals($gravatar, md5(file_get_contents(dirname(__FILE__).'/assets/jarednova.jpeg')));

$comment_id = $this->factory->comment->create(array('comment_post_ID' => $post_id, 'comment_author' => 'jarednova', 'comment_author_email' => 'notjared@upstatement.com'));
$comment = new TimberComment($comment_id);
$not_gravatar = md5(file_get_contents($comment->avatar()));
$this->assertNotEquals($not_gravatar, md5(file_get_contents(dirname(__FILE__).'/assets/jarednova.jpeg')));
}

function testAvatar(){
if (!TestTimberImage::is_connected()){
$this->markTestSkipped('Cannot test avatar images when not connected to internet');
}
$post_id = $this->factory->post->create();
$comment_id = $this->factory->comment->create(array('comment_post_ID' => $post_id));
$comment = new TimberComment($comment_id);

# test default gravatr holding image
$avatar = $comment->avatar(32, "mystery");

$this->assertTrue(substr ( $avatar , 0, 5 ) == "http:");

# does it work if its SSL?
$_SERVER['HTTPS'] = 'on';
$avatar = $comment->avatar(32, "mystery");
$this->assertTrue(200 === $this->crawl($avatar));
$this->assertTrue(substr ( $avatar , 0, 6 ) == "https:");
$_SERVER['HTTPS'] = 'off';

# pass custom url on different domain. can't check by crawling as
# i get a 302 regardless of default url
# so just check it comes back with it in the url
$this->valid_avatar($comment, "http://upload.wikimedia.org/wikipedia/en/b/bc/Wiki.png");

# same domain.
$this->valid_avatar($comment, get_template_directory_uri() . "/images/default.png");

#relative
$default_url = "/images/default.png";
$avatar = $comment->avatar(32, $default_url );
if (strstr($avatar, '?')){
list($url, $params) = explode('?', $avatar);
$default_url = get_template_directory_uri() . $default_url;
# you get back the absoulte url to default in the avatar url?
$this->assertEquals($params, "d=$default_url&amp;s=32");
}
# you get back url?
$this->assertTrue(substr ( get_template_directory_uri() . $avatar , 0, 5 ) == "http:");
}


function valid_avatar($comment, $default_url){
$avatar = $comment->avatar(32, $default_url);
if (strstr($avatar, '?')){
list($url, $params) = explode('?', $avatar);
# you get back the default in the avatar url?
$this->assertEquals($params, "d=$default_url&amp;s=32");
}
# you get back url?
$this->assertTrue(substr ( $avatar , 0, 5 ) == "http:");
}


function crawl($url){
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
/* Get the HTML or whatever is linked in $url. */
$response = curl_exec($handle);
/* Check for 404 (file not found). */
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
curl_close($handle);
return $httpCode;
}

}
88 changes: 9 additions & 79 deletions tests/test-timber-comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ function testAnonymousComment() {
$this->assertEquals('Anonymous', $result);
}

function testAnonymousCommentWithName() {
$post_id = $this->factory->post->create();
$comment_id = $this->factory->comment->create(array('comment_post_ID' => $post_id, 'comment_content' => 'Mystery', 'user_id' => 0, 'comment_author' => 'Milhouse Van Houten'));
$comment = new TimberComment($comment_id);
$twig_string = '{{comment.author.name}}';
$result = Timber::compile_string($twig_string, array('comment' => $comment));
$this->assertEquals('Milhouse Van Houten', $result);
}

function testCommentWithChildren() {
$kramer = $this->factory->user->create(array('display_name' => 'Cosmo Kramer'));
$post_id = $this->factory->post->create();
Expand All @@ -89,85 +98,6 @@ function testCommentWithChildren() {
$this->assertEquals('Cosmo Kramer', $result);
}

function testGravatar() {
if (!TestTimberImage::is_connected()){
$this->markTestSkipped('Cannot test avatar images when not connected to internet');
}
$post_id = $this->factory->post->create();
$comment_id = $this->factory->comment->create(array('comment_post_ID' => $post_id, 'comment_author' => 'jarednova', 'comment_author_email' => 'jarednova@upstatement.com'));
$comment = new TimberComment($comment_id);
$gravatar = md5(file_get_contents($comment->avatar()));
$this->assertEquals($gravatar, md5(file_get_contents(dirname(__FILE__).'/assets/jarednova.jpeg')));

$comment_id = $this->factory->comment->create(array('comment_post_ID' => $post_id, 'comment_author' => 'jarednova', 'comment_author_email' => 'notjared@upstatement.com'));
$comment = new TimberComment($comment_id);
$not_gravatar = md5(file_get_contents($comment->avatar()));
$this->assertNotEquals($not_gravatar, md5(file_get_contents(dirname(__FILE__).'/assets/jarednova.jpeg')));
}

function testAvatar(){
if (!TestTimberImage::is_connected()){
$this->markTestSkipped('Cannot test avatar images when not connected to internet');
}
$post_id = $this->factory->post->create();
$comment_id = $this->factory->comment->create(array('comment_post_ID' => $post_id));
$comment = new TimberComment($comment_id);

# test default gravatr holding image
$avatar = $comment->avatar(32, "mystery");

$this->assertTrue(substr ( $avatar , 0, 5 ) == "http:");

# does it work if its SSL?
$_SERVER['HTTPS'] = 'on';
$avatar = $comment->avatar(32, "mystery");
$this->assertTrue(200 === $this->crawl($avatar));
$this->assertTrue(substr ( $avatar , 0, 6 ) == "https:");
$_SERVER['HTTPS'] = 'off';

# pass custom url on different domain. can't check by crawling as
# i get a 302 regardless of default url
# so just check it comes back with it in the url
$this->valid_avatar($comment, "http://upload.wikimedia.org/wikipedia/en/b/bc/Wiki.png");

# same domain.
$this->valid_avatar($comment, get_template_directory_uri() . "/images/default.png");

#relative
$default_url = "/images/default.png";
$avatar = $comment->avatar(32, $default_url );
if (strstr($avatar, '?')){
list($url, $params) = explode('?', $avatar);
$default_url = get_template_directory_uri() . $default_url;
# you get back the absoulte url to default in the avatar url?
$this->assertEquals($params, "d=$default_url&amp;s=32");
}
# you get back url?
$this->assertTrue(substr ( get_template_directory_uri() . $avatar , 0, 5 ) == "http:");
}


function valid_avatar($comment, $default_url){
$avatar = $comment->avatar(32, $default_url);
if (strstr($avatar, '?')){
list($url, $params) = explode('?', $avatar);
# you get back the default in the avatar url?
$this->assertEquals($params, "d=$default_url&amp;s=32");
}
# you get back url?
$this->assertTrue(substr ( $avatar , 0, 5 ) == "http:");
}


function crawl($url){
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
/* Get the HTML or whatever is linked in $url. */
$response = curl_exec($handle);
/* Check for 404 (file not found). */
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
curl_close($handle);
return $httpCode;
}

}
21 changes: 20 additions & 1 deletion tests/test-timber-menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function testMenuItemLink() {
$this->assertGreaterThanOrEqual( 3, count( $menu->get_items() ) );
$items = $menu->get_items();
$item = $items[1];
$this->assertTrue( $item->is_external() );
$this->assertTrue( $item->external() );
$struc = get_option( 'permalink_structure' );
$this->assertEquals( 'http://upstatement.com', $item->permalink() );
$this->assertEquals( 'http://upstatement.com', $item->get_permalink() );
Expand Down Expand Up @@ -174,7 +174,9 @@ function _createSimpleMenu( $name = 'My Menu' ) {
update_post_meta( $parent_id, '_menu_item_menu_item_parent', 0 );
update_post_meta( $parent_id, '_menu_item_object_id', $parent_page );
update_post_meta( $parent_id, '_menu_item_url', '' );
update_post_meta( $parent_id, 'flood', 'molasses' );
$menu_items[] = $parent_id;
$this->insertIntoMenu($menu_term['term_id'], $menu_items);
return $menu_term;
}

Expand Down Expand Up @@ -403,6 +405,23 @@ function testMenuLevels() {
$this->assertEquals(2, $grandchild->level);
}

function testMenuLevelsChildren() {
$this->_createTestMenu();
$menu = new TimberMenu();
$parent = $menu->items[0];
$this->assertEquals(0, $parent->level);
$children = $parent->children();
$this->assertEquals(1, count($children));
$this->assertEquals('Child Page', $children[0]->title());
}

function testMenuItemMeta() {
$menu_info = $this->_createSimpleMenu();
$menu = new TimberMenu($menu_info['term_id']);
$item = $menu->items[0];
$this->assertEquals('molasses', $item->meta('flood'));
}

function testMenuName() {
$this->_createTestMenu();
$menu = new TimberMenu();
Expand Down
7 changes: 7 additions & 0 deletions tests/test-timber-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,13 @@ function testPostTypeObject() {
$this->assertEquals('Posts', $pto->label);
}

function testPage() {
$pid = $this->factory->post->create(array('post_type' => 'page', 'post_title' => 'My Page'));
$post = new TimberPost($pid);
$this->assertEquals($pid, $post->ID);
$this->assertEquals('My Page', $post->title());
}

function testEditUrl() {
$pid = $this->factory->post->create(array('post_author' => 1));
$post = new TimberPost($pid);
Expand Down
10 changes: 10 additions & 0 deletions tests/test-timber-term.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,14 @@ function testTermChildren() {
$this->assertEquals('Local', $children[0]->name);
}

function testTermEditLink() {
wp_set_current_user(1);
$tid = $this->factory->term->create(array('name' => 'News', 'taxonomy' => 'category'));
$term = new TimberTerm($tid);
$links = array();
$links[] = 'http://example.org/wp-admin/edit-tags.php?action=edit&taxonomy=category&tag_ID='.$tid.'&post_type=post';
$links[] = 'http://example.org/wp-admin/edit-tags.php?action=edit&taxonomy=category&tag_ID='.$tid;
$this->assertContains($term->edit_link(), $links);
}

}

0 comments on commit d88c60a

Please sign in to comment.