thumbnails.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. <?php
  2. /**
  3. * @group post
  4. * @group media
  5. */
  6. class Tests_Post_Thumbnail_Template extends WP_UnitTestCase {
  7. protected static $post;
  8. protected static $different_post;
  9. protected static $attachment_id;
  10. protected $current_size_filter_data = null;
  11. protected $current_size_filter_result = null;
  12. public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
  13. self::$post = $factory->post->create_and_get();
  14. self::$different_post = $factory->post->create_and_get();
  15. $file = DIR_TESTDATA . '/images/canola.jpg';
  16. self::$attachment_id = $factory->attachment->create_upload_object(
  17. $file,
  18. self::$post->ID,
  19. array(
  20. 'post_mime_type' => 'image/jpeg',
  21. )
  22. );
  23. }
  24. public static function tearDownAfterClass() {
  25. wp_delete_post( self::$attachment_id, true );
  26. parent::tearDownAfterClass();
  27. }
  28. function test_has_post_thumbnail() {
  29. $this->assertFalse( has_post_thumbnail( self::$post ) );
  30. $this->assertFalse( has_post_thumbnail( self::$post->ID ) );
  31. $this->assertFalse( has_post_thumbnail() );
  32. $GLOBALS['post'] = self::$post;
  33. $this->assertFalse( has_post_thumbnail() );
  34. unset( $GLOBALS['post'] );
  35. set_post_thumbnail( self::$post, self::$attachment_id );
  36. $this->assertTrue( has_post_thumbnail( self::$post ) );
  37. $this->assertTrue( has_post_thumbnail( self::$post->ID ) );
  38. $this->assertFalse( has_post_thumbnail() );
  39. $GLOBALS['post'] = self::$post;
  40. $this->assertTrue( has_post_thumbnail() );
  41. }
  42. function test_get_post_thumbnail_id() {
  43. $this->assertSame( 0, get_post_thumbnail_id( self::$post ) );
  44. $this->assertSame( 0, get_post_thumbnail_id( self::$post->ID ) );
  45. $this->assertFalse( get_post_thumbnail_id() );
  46. set_post_thumbnail( self::$post, self::$attachment_id );
  47. $this->assertSame( self::$attachment_id, get_post_thumbnail_id( self::$post ) );
  48. $this->assertSame( self::$attachment_id, get_post_thumbnail_id( self::$post->ID ) );
  49. $GLOBALS['post'] = self::$post;
  50. $this->assertSame( self::$attachment_id, get_post_thumbnail_id() );
  51. }
  52. function test_update_post_thumbnail_cache() {
  53. set_post_thumbnail( self::$post, self::$attachment_id );
  54. $query = new WP_Query(
  55. array(
  56. 'post_type' => 'any',
  57. 'post__in' => array( self::$post->ID ),
  58. 'orderby' => 'post__in',
  59. )
  60. );
  61. $this->assertFalse( $query->thumbnails_cached );
  62. update_post_thumbnail_cache( $query );
  63. $this->assertTrue( $query->thumbnails_cached );
  64. }
  65. /**
  66. * @ticket 12235
  67. */
  68. function test_get_the_post_thumbnail_caption() {
  69. $this->assertSame( '', get_the_post_thumbnail_caption() );
  70. $caption = 'This is a caption.';
  71. $post_id = self::factory()->post->create();
  72. $attachment_id = self::factory()->attachment->create_object(
  73. 'image.jpg',
  74. $post_id,
  75. array(
  76. 'post_mime_type' => 'image/jpeg',
  77. 'post_type' => 'attachment',
  78. 'post_excerpt' => $caption,
  79. )
  80. );
  81. set_post_thumbnail( $post_id, $attachment_id );
  82. $this->assertSame( $caption, get_the_post_thumbnail_caption( $post_id ) );
  83. }
  84. /**
  85. * @ticket 12235
  86. */
  87. function test_get_the_post_thumbnail_caption_empty() {
  88. $post_id = self::factory()->post->create();
  89. $attachment_id = self::factory()->attachment->create_object(
  90. 'image.jpg',
  91. $post_id,
  92. array(
  93. 'post_mime_type' => 'image/jpeg',
  94. 'post_type' => 'attachment',
  95. 'post_excerpt' => '',
  96. )
  97. );
  98. set_post_thumbnail( $post_id, $attachment_id );
  99. $this->assertSame( '', get_the_post_thumbnail_caption( $post_id ) );
  100. }
  101. /**
  102. * @ticket 12235
  103. */
  104. function test_the_post_thumbnail_caption() {
  105. $caption = 'This is a caption.';
  106. $post_id = self::factory()->post->create();
  107. $attachment_id = self::factory()->attachment->create_object(
  108. 'image.jpg',
  109. $post_id,
  110. array(
  111. 'post_mime_type' => 'image/jpeg',
  112. 'post_type' => 'attachment',
  113. 'post_excerpt' => $caption,
  114. )
  115. );
  116. set_post_thumbnail( $post_id, $attachment_id );
  117. $this->expectOutputString( $caption );
  118. the_post_thumbnail_caption( $post_id );
  119. }
  120. function test_get_the_post_thumbnail() {
  121. $this->assertSame( '', get_the_post_thumbnail() );
  122. $this->assertSame( '', get_the_post_thumbnail( self::$post ) );
  123. set_post_thumbnail( self::$post, self::$attachment_id );
  124. $expected = wp_get_attachment_image(
  125. self::$attachment_id,
  126. 'post-thumbnail',
  127. false,
  128. array(
  129. 'class' => 'attachment-post-thumbnail size-post-thumbnail wp-post-image',
  130. )
  131. );
  132. $this->assertSame( $expected, get_the_post_thumbnail( self::$post ) );
  133. $GLOBALS['post'] = self::$post;
  134. $this->assertSame( $expected, get_the_post_thumbnail() );
  135. }
  136. function test_the_post_thumbnail() {
  137. $this->expectOutputString( '' );
  138. the_post_thumbnail();
  139. $GLOBALS['post'] = self::$post;
  140. $this->expectOutputString( '' );
  141. the_post_thumbnail();
  142. set_post_thumbnail( self::$post, self::$attachment_id );
  143. $expected = wp_get_attachment_image(
  144. self::$attachment_id,
  145. 'post-thumbnail',
  146. false,
  147. array(
  148. 'class' => 'attachment-post-thumbnail size-post-thumbnail wp-post-image',
  149. )
  150. );
  151. $this->expectOutputString( $expected );
  152. the_post_thumbnail();
  153. }
  154. /**
  155. * @ticket 33070
  156. */
  157. function test_get_the_post_thumbnail_url() {
  158. $this->assertFalse( has_post_thumbnail( self::$post ) );
  159. $this->assertFalse( get_the_post_thumbnail_url() );
  160. $this->assertFalse( get_the_post_thumbnail_url( self::$post ) );
  161. set_post_thumbnail( self::$post, self::$attachment_id );
  162. $this->assertFalse( get_the_post_thumbnail_url() );
  163. $this->assertSame( wp_get_attachment_url( self::$attachment_id ), get_the_post_thumbnail_url( self::$post ) );
  164. $GLOBALS['post'] = self::$post;
  165. $this->assertSame( wp_get_attachment_url( self::$attachment_id ), get_the_post_thumbnail_url() );
  166. }
  167. /**
  168. * @ticket 33070
  169. */
  170. function test_get_the_post_thumbnail_url_with_invalid_post() {
  171. set_post_thumbnail( self::$post, self::$attachment_id );
  172. $this->assertNotFalse( get_the_post_thumbnail_url( self::$post->ID ) );
  173. $deleted = wp_delete_post( self::$post->ID, true );
  174. $this->assertNotEmpty( $deleted );
  175. $this->assertFalse( get_the_post_thumbnail_url( self::$post->ID ) );
  176. }
  177. /**
  178. * @ticket 33070
  179. */
  180. function test_the_post_thumbnail_url() {
  181. $GLOBALS['post'] = self::$post;
  182. $this->expectOutputString( '' );
  183. the_post_thumbnail_url();
  184. set_post_thumbnail( self::$post, self::$attachment_id );
  185. $this->expectOutputString( wp_get_attachment_url( self::$attachment_id ) );
  186. the_post_thumbnail_url();
  187. }
  188. /**
  189. * @ticket 12922
  190. */
  191. function test__wp_preview_post_thumbnail_filter() {
  192. $old_post = isset( $GLOBALS['post'] ) ? $GLOBALS['post'] : null;
  193. $GLOBALS['post'] = self::$post;
  194. $_REQUEST['_thumbnail_id'] = self::$attachment_id;
  195. $_REQUEST['preview_id'] = self::$post->ID;
  196. $result = _wp_preview_post_thumbnail_filter( '', self::$post->ID, '_thumbnail_id' );
  197. // Clean up.
  198. $GLOBALS['post'] = $old_post;
  199. unset( $_REQUEST['_thumbnail_id'] );
  200. unset( $_REQUEST['preview_id'] );
  201. $this->assertEquals( self::$attachment_id, $result );
  202. }
  203. /**
  204. * @ticket 37697
  205. */
  206. function test__wp_preview_post_thumbnail_filter_secondary_post() {
  207. $old_post = isset( $GLOBALS['post'] ) ? $GLOBALS['post'] : null;
  208. $secondary_post = self::factory()->post->create(
  209. array(
  210. 'post_stauts' => 'publish',
  211. )
  212. );
  213. $GLOBALS['post'] = self::$post;
  214. $_REQUEST['_thumbnail_id'] = self::$attachment_id;
  215. $_REQUEST['preview_id'] = $secondary_post;
  216. $result = _wp_preview_post_thumbnail_filter( '', self::$post->ID, '_thumbnail_id' );
  217. // Clean up.
  218. $GLOBALS['post'] = $old_post;
  219. unset( $_REQUEST['_thumbnail_id'] );
  220. unset( $_REQUEST['preview_id'] );
  221. $this->assertEmpty( $result );
  222. }
  223. /**
  224. * @ticket 12922
  225. */
  226. function test_insert_post_with_post_thumbnail() {
  227. $post_id = wp_insert_post(
  228. array(
  229. 'ID' => self::$post->ID,
  230. 'post_status' => 'publish',
  231. 'post_content' => 'Post content',
  232. 'post_title' => 'Post Title',
  233. '_thumbnail_id' => self::$attachment_id,
  234. )
  235. );
  236. $thumbnail_id = get_post_thumbnail_id( $post_id );
  237. $this->assertSame( self::$attachment_id, $thumbnail_id );
  238. $post_id = wp_insert_post(
  239. array(
  240. 'ID' => $post_id,
  241. 'post_status' => 'publish',
  242. 'post_content' => 'Post content',
  243. 'post_title' => 'Post Title',
  244. '_thumbnail_id' => - 1, // -1 removes post thumbnail.
  245. )
  246. );
  247. $thumbnail_id = get_post_thumbnail_id( $post_id );
  248. $this->assertEmpty( $thumbnail_id );
  249. }
  250. /**
  251. * @ticket 37658
  252. */
  253. function test_insert_attachment_with_post_thumbnail() {
  254. // Audio files support featured images.
  255. $post_id = wp_insert_post(
  256. array(
  257. 'post_type' => 'attachment',
  258. 'post_status' => 'inherit',
  259. 'post_content' => 'Post content',
  260. 'post_title' => 'Post Title',
  261. 'post_mime_type' => 'audio/mpeg',
  262. 'post_parent' => 0,
  263. 'file' => DIR_TESTDATA . '/audio/test-noise.mp3', // File does not exist, but does not matter here.
  264. '_thumbnail_id' => self::$attachment_id,
  265. )
  266. );
  267. $thumbnail_id = get_post_thumbnail_id( $post_id );
  268. $this->assertSame( self::$attachment_id, $thumbnail_id );
  269. // Images do not support featured images.
  270. $post_id = wp_insert_post(
  271. array(
  272. 'post_type' => 'attachment',
  273. 'post_status' => 'inherit',
  274. 'post_content' => 'Post content',
  275. 'post_title' => 'Post Title',
  276. 'post_mime_type' => 'image/jpeg',
  277. 'post_parent' => 0,
  278. 'file' => DIR_TESTDATA . '/images/canola.jpg',
  279. '_thumbnail_id' => self::$attachment_id,
  280. )
  281. );
  282. $thumbnail_id = get_post_thumbnail_id( $post_id );
  283. $this->assertEmpty( $thumbnail_id );
  284. }
  285. /**
  286. * @ticket 39030
  287. */
  288. function test_post_thumbnail_size_filter_simple() {
  289. $this->current_size_filter_data = 'medium';
  290. add_filter( 'post_thumbnail_size', array( $this, 'filter_post_thumbnail_size' ), 10, 2 );
  291. // This filter is used to capture the $size result.
  292. add_filter( 'post_thumbnail_html', array( $this, 'filter_set_post_thumbnail_size_result' ), 10, 4 );
  293. get_the_post_thumbnail( self::$post );
  294. $result = $this->current_size_filter_result;
  295. $this->current_size_filter_data = null;
  296. $this->current_size_filter_result = null;
  297. $this->assertSame( 'medium', $result );
  298. }
  299. /**
  300. * @ticket 39030
  301. * @dataProvider data_post_thumbnail_size_filter_complex
  302. */
  303. function test_post_thumbnail_size_filter_complex( $which_post, $expected ) {
  304. $this->current_size_filter_data = array(
  305. self::$post->ID => 'medium',
  306. self::$different_post->ID => 'thumbnail',
  307. );
  308. $post = 1 === $which_post ? self::$different_post : self::$post;
  309. add_filter( 'post_thumbnail_size', array( $this, 'filter_post_thumbnail_size' ), 10, 2 );
  310. // This filter is used to capture the $size result.
  311. add_filter( 'post_thumbnail_html', array( $this, 'filter_set_post_thumbnail_size_result' ), 10, 4 );
  312. get_the_post_thumbnail( $post );
  313. $result = $this->current_size_filter_result;
  314. $this->current_size_filter_data = null;
  315. $this->current_size_filter_result = null;
  316. $this->assertSame( $expected, $result );
  317. }
  318. function data_post_thumbnail_size_filter_complex() {
  319. return array(
  320. array( 0, 'medium' ),
  321. array( 1, 'thumbnail' ),
  322. );
  323. }
  324. function filter_post_thumbnail_size( $size, $post_id ) {
  325. if ( is_array( $this->current_size_filter_data ) && isset( $this->current_size_filter_data[ $post_id ] ) ) {
  326. return $this->current_size_filter_data[ $post_id ];
  327. }
  328. if ( is_string( $this->current_size_filter_data ) ) {
  329. return $this->current_size_filter_data;
  330. }
  331. return $size;
  332. }
  333. function filter_set_post_thumbnail_size_result( $html, $post_id, $post_thumbnail_id, $size ) {
  334. $this->current_size_filter_result = $size;
  335. return $html;
  336. }
  337. }