avatar.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?php
  2. /**
  3. * Test avatar related functions
  4. *
  5. * @group avatar
  6. */
  7. class Tests_Avatar extends WP_UnitTestCase {
  8. /**
  9. * @ticket 21195
  10. */
  11. public function test_get_avatar_url_gravatar_url() {
  12. $url = get_avatar_url( 1 );
  13. $this->assertSame( preg_match( '|^http?://[0-9]+.gravatar.com/avatar/[0-9a-f]{32}\?|', $url ), 1 );
  14. }
  15. /**
  16. * @ticket 21195
  17. */
  18. public function test_get_avatar_url_size() {
  19. $url = get_avatar_url( 1 );
  20. $this->assertSame( preg_match( '|\?.*s=96|', $url ), 1 );
  21. $args = array( 'size' => 100 );
  22. $url = get_avatar_url( 1, $args );
  23. $this->assertSame( preg_match( '|\?.*s=100|', $url ), 1 );
  24. }
  25. /**
  26. * @ticket 21195
  27. */
  28. public function test_get_avatar_url_default() {
  29. $url = get_avatar_url( 1 );
  30. $this->assertSame( preg_match( '|\?.*d=mm|', $url ), 1 );
  31. $args = array( 'default' => 'wavatar' );
  32. $url = get_avatar_url( 1, $args );
  33. $this->assertSame( preg_match( '|\?.*d=wavatar|', $url ), 1 );
  34. $this->assertSame( preg_match( '|\?.*f=y|', $url ), 0 );
  35. $args = array( 'force_default' => true );
  36. $url = get_avatar_url( 1, $args );
  37. $this->assertSame( preg_match( '|\?.*f=y|', $url ), 1 );
  38. }
  39. /**
  40. * @ticket 21195
  41. */
  42. public function test_get_avatar_url_rating() {
  43. $url = get_avatar_url( 1 );
  44. $this->assertSame( preg_match( '|\?.*r=g|', $url ), 1 );
  45. $args = array( 'rating' => 'M' );
  46. $url = get_avatar_url( 1, $args );
  47. $this->assertSame( preg_match( '|\?.*r=m|', $url ), 1 );
  48. }
  49. /**
  50. * @ticket 21195
  51. */
  52. public function test_get_avatar_url_scheme() {
  53. $url = get_avatar_url( 1 );
  54. $this->assertSame( preg_match( '|^http://|', $url ), 1 );
  55. $args = array( 'scheme' => 'https' );
  56. $url = get_avatar_url( 1, $args );
  57. $this->assertSame( preg_match( '|^https://|', $url ), 1 );
  58. $args = array( 'scheme' => 'lolcat' );
  59. $url = get_avatar_url( 1, $args );
  60. $this->assertSame( preg_match( '|^lolcat://|', $url ), 0 );
  61. }
  62. /**
  63. * @ticket 21195
  64. */
  65. public function test_get_avatar_url_user() {
  66. $url = get_avatar_url( 1 );
  67. $url2 = get_avatar_url( WP_TESTS_EMAIL );
  68. $this->assertSame( $url, $url2 );
  69. $url2 = get_avatar_url( md5( WP_TESTS_EMAIL ) . '@md5.gravatar.com' );
  70. $this->assertSame( $url, $url2 );
  71. $user = get_user_by( 'id', 1 );
  72. $url2 = get_avatar_url( $user );
  73. $this->assertSame( $url, $url2 );
  74. $post_id = self::factory()->post->create( array( 'post_author' => 1 ) );
  75. $post = get_post( $post_id );
  76. $url2 = get_avatar_url( $post );
  77. $this->assertSame( $url, $url2 );
  78. $comment_id = self::factory()->comment->create(
  79. array(
  80. 'comment_post_ID' => $post_id,
  81. 'user_id' => 1,
  82. )
  83. );
  84. $comment = get_comment( $comment_id );
  85. $url2 = get_avatar_url( $comment );
  86. $this->assertSame( $url, $url2 );
  87. }
  88. protected $fake_url;
  89. /**
  90. * @ticket 21195
  91. */
  92. public function test_pre_get_avatar_url_filter() {
  93. $this->fake_url = 'haha wat';
  94. add_filter( 'pre_get_avatar_data', array( $this, 'pre_get_avatar_url_filter' ), 10, 1 );
  95. $url = get_avatar_url( 1 );
  96. remove_filter( 'pre_get_avatar_data', array( $this, 'pre_get_avatar_url_filter' ), 10 );
  97. $this->assertSame( $url, $this->fake_url );
  98. }
  99. public function pre_get_avatar_url_filter( $args ) {
  100. $args['url'] = $this->fake_url;
  101. return $args;
  102. }
  103. /**
  104. * @ticket 21195
  105. */
  106. public function test_get_avatar_url_filter() {
  107. $this->fake_url = 'omg lol';
  108. add_filter( 'get_avatar_url', array( $this, 'get_avatar_url_filter' ), 10, 1 );
  109. $url = get_avatar_url( 1 );
  110. remove_filter( 'get_avatar_url', array( $this, 'get_avatar_url_filter' ), 10 );
  111. $this->assertSame( $url, $this->fake_url );
  112. }
  113. public function get_avatar_url_filter( $url ) {
  114. return $this->fake_url;
  115. }
  116. /**
  117. * @ticket 21195
  118. */
  119. public function test_get_avatar_comment_types_filter() {
  120. $url = get_avatar_url( 1 );
  121. $post_id = self::factory()->post->create( array( 'post_author' => 1 ) );
  122. $comment_id = self::factory()->comment->create(
  123. array(
  124. 'comment_post_ID' => $post_id,
  125. 'user_id' => 1,
  126. 'comment_type' => 'pingback',
  127. )
  128. );
  129. $comment = get_comment( $comment_id );
  130. $url2 = get_avatar_url( $comment );
  131. $this->assertFalse( $url2 );
  132. add_filter( 'get_avatar_comment_types', array( $this, 'get_avatar_comment_types_filter' ), 10, 1 );
  133. $url2 = get_avatar_url( $comment );
  134. remove_filter( 'get_avatar_comment_types', array( $this, 'get_avatar_comment_types_filter' ), 10 );
  135. $this->assertSame( $url, $url2 );
  136. }
  137. public function get_avatar_comment_types_filter( $comment_types ) {
  138. $comment_types[] = 'pingback';
  139. return $comment_types;
  140. }
  141. public function test_get_avatar() {
  142. $img = get_avatar( 1 );
  143. $this->assertSame( preg_match( "|^<img alt='[^']*' src='[^']*' srcset='[^']*' class='[^']*' height='[^']*' width='[^']*' loading='lazy'/>$|", $img ), 1 );
  144. }
  145. public function test_get_avatar_size() {
  146. $size = '100';
  147. $img = get_avatar( 1, $size );
  148. $this->assertSame( preg_match( "|^<img .*height='$size'.*width='$size'|", $img ), 1 );
  149. }
  150. public function test_get_avatar_alt() {
  151. $alt = 'Mr Hyde';
  152. $img = get_avatar( 1, 96, '', $alt );
  153. $this->assertSame( preg_match( "|^<img alt='$alt'|", $img ), 1 );
  154. }
  155. public function test_get_avatar_class() {
  156. $class = 'first';
  157. $img = get_avatar( 1, 96, '', '', array( 'class' => $class ) );
  158. $this->assertSame( preg_match( "|^<img .*class='[^']*{$class}[^']*'|", $img ), 1 );
  159. }
  160. public function test_get_avatar_default_class() {
  161. $img = get_avatar( 1, 96, '', '', array( 'force_default' => true ) );
  162. $this->assertSame( preg_match( "|^<img .*class='[^']*avatar-default[^']*'|", $img ), 1 );
  163. }
  164. public function test_get_avatar_force_display() {
  165. $old = get_option( 'show_avatars' );
  166. update_option( 'show_avatars', false );
  167. $this->assertFalse( get_avatar( 1 ) );
  168. $this->assertNotEmpty( get_avatar( 1, 96, '', '', array( 'force_display' => true ) ) );
  169. update_option( 'show_avatars', $old );
  170. }
  171. protected $fake_img;
  172. /**
  173. * @ticket 21195
  174. */
  175. public function test_pre_get_avatar_filter() {
  176. $this->fake_img = 'YOU TOO?!';
  177. add_filter( 'pre_get_avatar', array( $this, 'pre_get_avatar_filter' ), 10, 1 );
  178. $img = get_avatar( 1 );
  179. remove_filter( 'pre_get_avatar', array( $this, 'pre_get_avatar_filter' ), 10 );
  180. $this->assertSame( $img, $this->fake_img );
  181. }
  182. public function pre_get_avatar_filter( $img ) {
  183. return $this->fake_img;
  184. }
  185. /**
  186. * @ticket 21195
  187. */
  188. public function test_get_avatar_filter() {
  189. $this->fake_url = 'YA RLY';
  190. add_filter( 'get_avatar', array( $this, 'get_avatar_filter' ), 10, 1 );
  191. $img = get_avatar( 1 );
  192. remove_filter( 'get_avatar', array( $this, 'get_avatar_filter' ), 10 );
  193. $this->assertSame( $img, $this->fake_url );
  194. }
  195. public function get_avatar_filter( $img ) {
  196. return $this->fake_url;
  197. }
  198. /**
  199. * The `get_avatar_data()` function should return gravatar url when comment type allowed to retrieve avatars.
  200. *
  201. * @ticket 44033
  202. */
  203. public function test_get_avatar_data_should_return_gravatar_url_when_input_avatar_comment_type() {
  204. $comment_type = 'comment';
  205. $comment = self::factory()->comment->create_and_get(
  206. array(
  207. 'comment_author_email' => 'commenter@example.com',
  208. 'comment_type' => $comment_type,
  209. )
  210. );
  211. $actual_data = get_avatar_data( $comment );
  212. $this->assertTrue( is_avatar_comment_type( $comment_type ) );
  213. $this->assertRegexp( '|^http?://[0-9]+.gravatar.com/avatar/[0-9a-f]{32}\?|', $actual_data['url'] );
  214. }
  215. /**
  216. * The `get_avatar_data()` function should return invalid url when comment type not allowed to retrieve avatars.
  217. *
  218. * @ticket 44033
  219. */
  220. public function test_get_avatar_data_should_return_invalid_url_when_input_not_avatar_comment_type() {
  221. $comment_type = 'review';
  222. $comment = self::factory()->comment->create_and_get(
  223. array(
  224. 'comment_author_email' => 'commenter@example.com',
  225. 'comment_type' => $comment_type,
  226. )
  227. );
  228. $actual_data = get_avatar_data( $comment );
  229. $this->assertFalse( is_avatar_comment_type( $comment_type ) );
  230. $this->assertFalse( $actual_data['url'] );
  231. }
  232. }