PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/test-timber-comment-avatar.php

https://gitlab.com/aristath/timber
PHP | 140 lines | 96 code | 27 blank | 17 comment | 14 complexity | 38db5d69f5582f0e5f73be262018f520 MD5 | raw file
  1. <?php
  2. class TestTimberCommentAvatar extends Timber_UnitTestCase {
  3. function testAvatarSize() {
  4. if ( !TestTimberImage::is_connected() ) {
  5. $this->markTestSkipped('Cannot test avatar images when not connected to internet');
  6. }
  7. $post_id = $this->factory->post->create();
  8. $comment_id = $this->factory->comment->create(array('comment_post_ID' => $post_id));
  9. $comment = new TimberComment($comment_id);
  10. # test default gravatr holding image
  11. $avatar = $comment->avatar("mystery");
  12. $this->assertTrue(substr ( $avatar , 0, 5 ) == "http:");
  13. }
  14. function testAvatarFalse() {
  15. update_option('show_avatars', false);
  16. $post_id = $this->factory->post->create();
  17. $comment_id = $this->factory->comment->create(array('comment_post_ID' => $post_id));
  18. $comment = new TimberComment($comment_id);
  19. # test default gravatr holding image
  20. $avatar = $comment->avatar();
  21. $this->assertFalse($avatar);
  22. }
  23. function testAvatarBlank() {
  24. if ( !TestTimberImage::is_connected() ) {
  25. $this->markTestSkipped('Cannot test avatar images when not connected to internet');
  26. }
  27. $post_id = $this->factory->post->create();
  28. $comment_id = $this->factory->comment->create(array('comment_post_ID' => $post_id));
  29. $comment = new TimberComment($comment_id);
  30. # test default gravatr holding image
  31. $avatar = $comment->avatar(92, "blank");
  32. $this->assertTrue(substr ( $avatar , 0, 5 ) == "http:");
  33. }
  34. function testAvatarGravatarDefault() {
  35. if ( !TestTimberImage::is_connected() ) {
  36. $this->markTestSkipped('Cannot test avatar images when not connected to internet');
  37. }
  38. $post_id = $this->factory->post->create();
  39. $comment_id = $this->factory->comment->create(array('comment_post_ID' => $post_id));
  40. $comment = new TimberComment($comment_id);
  41. # test default gravatr holding image
  42. $avatar = $comment->avatar(92, "gravatar_default");
  43. $this->assertTrue(substr ( $avatar , 0, 5 ) == "http:");
  44. }
  45. function testGravatar() {
  46. if (!TestTimberImage::is_connected()){
  47. $this->markTestSkipped('Cannot test avatar images when not connected to internet');
  48. }
  49. $post_id = $this->factory->post->create();
  50. $comment_id = $this->factory->comment->create(array('comment_post_ID' => $post_id, 'comment_author' => 'jarednova', 'comment_author_email' => 'jarednova@upstatement.com'));
  51. $comment = new TimberComment($comment_id);
  52. $gravatar = md5(file_get_contents($comment->avatar()));
  53. $this->assertEquals($gravatar, md5(file_get_contents(dirname(__FILE__).'/assets/jarednova.jpeg')));
  54. $comment_id = $this->factory->comment->create(array('comment_post_ID' => $post_id, 'comment_author' => 'jarednova', 'comment_author_email' => 'notjared@upstatement.com'));
  55. $comment = new TimberComment($comment_id);
  56. $not_gravatar = md5(file_get_contents($comment->avatar()));
  57. $this->assertNotEquals($not_gravatar, md5(file_get_contents(dirname(__FILE__).'/assets/jarednova.jpeg')));
  58. }
  59. function testAvatar(){
  60. if (!TestTimberImage::is_connected()){
  61. $this->markTestSkipped('Cannot test avatar images when not connected to internet');
  62. }
  63. $post_id = $this->factory->post->create();
  64. $comment_id = $this->factory->comment->create(array('comment_post_ID' => $post_id));
  65. $comment = new TimberComment($comment_id);
  66. # test default gravatr holding image
  67. $avatar = $comment->avatar(32, "mystery");
  68. $this->assertTrue(substr ( $avatar , 0, 5 ) == "http:");
  69. # does it work if its SSL?
  70. $_SERVER['HTTPS'] = 'on';
  71. $avatar = $comment->avatar(32, "mystery");
  72. $this->assertTrue(200 === $this->crawl($avatar));
  73. $this->assertTrue(substr ( $avatar , 0, 6 ) == "https:");
  74. $_SERVER['HTTPS'] = 'off';
  75. # pass custom url on different domain. can't check by crawling as
  76. # i get a 302 regardless of default url
  77. # so just check it comes back with it in the url
  78. $this->valid_avatar($comment, "http://upload.wikimedia.org/wikipedia/en/b/bc/Wiki.png");
  79. # same domain.
  80. $this->valid_avatar($comment, get_template_directory_uri() . "/images/default.png");
  81. #relative
  82. $default_url = "/images/default.png";
  83. $avatar = $comment->avatar(32, $default_url );
  84. if (strstr($avatar, '?')){
  85. list($url, $params) = explode('?', $avatar);
  86. $default_url = get_template_directory_uri() . $default_url;
  87. # you get back the absoulte url to default in the avatar url?
  88. $this->assertEquals($params, "d=$default_url&amp;s=32");
  89. }
  90. # you get back url?
  91. $this->assertTrue(substr ( get_template_directory_uri() . $avatar , 0, 5 ) == "http:");
  92. }
  93. function valid_avatar($comment, $default_url){
  94. $avatar = $comment->avatar(32, $default_url);
  95. if (strstr($avatar, '?')){
  96. list($url, $params) = explode('?', $avatar);
  97. # you get back the default in the avatar url?
  98. $this->assertEquals($params, "d=$default_url&amp;s=32");
  99. }
  100. # you get back url?
  101. $this->assertTrue(substr ( $avatar , 0, 5 ) == "http:");
  102. }
  103. function crawl($url){
  104. $handle = curl_init($url);
  105. curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
  106. /* Get the HTML or whatever is linked in $url. */
  107. $response = curl_exec($handle);
  108. /* Check for 404 (file not found). */
  109. $httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
  110. curl_close($handle);
  111. return $httpCode;
  112. }
  113. }