media.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. <?php
  2. /**
  3. * @group media
  4. * @group shortcode
  5. */
  6. class Tests_Media extends WP_UnitTestCase {
  7. function setUp() {
  8. parent::setUp();
  9. $this->caption = 'A simple caption.';
  10. $this->html_content = <<<CAP
  11. A <strong class='classy'>bolded</strong> <em>caption</em> with a <a href="#">link</a>.
  12. CAP;
  13. $this->img_content = <<<CAP
  14. <img src="pic.jpg" id='anId' alt="pic"/>
  15. CAP;
  16. $this->img_name = 'image.jpg';
  17. $this->img_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $this->img_name;
  18. $this->img_html = '<img src="' . $this->img_url . '"/>';
  19. $this->img_dimensions = array( 'width' => 100, 'height' => 100 );
  20. }
  21. function test_img_caption_shortcode_added() {
  22. global $shortcode_tags;
  23. $this->assertEquals( 'img_caption_shortcode', $shortcode_tags['caption'] );
  24. $this->assertEquals( 'img_caption_shortcode', $shortcode_tags['wp_caption'] );
  25. }
  26. function test_img_caption_shortcode_with_empty_params() {
  27. $result = img_caption_shortcode( array() );
  28. $this->assertNull( $result );
  29. }
  30. function test_img_caption_shortcode_with_bad_attr() {
  31. $result = img_caption_shortcode( array(), 'content' );
  32. $this->assertEquals( 'content', 'content' );
  33. }
  34. function test_img_caption_shortcode_with_old_format() {
  35. $result = img_caption_shortcode(
  36. array( 'width' => 20, 'caption' => $this->caption )
  37. );
  38. $this->assertEquals( 2, preg_match_all( '/wp-caption/', $result, $_r ) );
  39. $this->assertEquals( 1, preg_match_all( '/alignnone/', $result, $_r ) );
  40. $this->assertEquals( 1, preg_match_all( "/{$this->caption}/", $result, $_r ) );
  41. $this->assertEquals( 1, preg_match_all( "/width: 30/", $result, $_r ) );
  42. }
  43. function test_img_caption_shortcode_with_old_format_id_and_align() {
  44. $result = img_caption_shortcode(
  45. array(
  46. 'width' => 20,
  47. 'caption' => $this->caption,
  48. 'id' => '"myId',
  49. 'align' => '&myAlignment'
  50. )
  51. );
  52. $this->assertEquals( 1, preg_match_all( '/wp-caption &amp;myAlignment/', $result, $_r ) );
  53. $this->assertEquals( 1, preg_match_all( '/id="&quot;myId"/', $result, $_r ) );
  54. $this->assertEquals( 1, preg_match_all( "/{$this->caption}/", $result, $_r ) );
  55. }
  56. function test_new_img_caption_shortcode_with_html_caption() {
  57. $result = img_caption_shortcode(
  58. array( 'width' => 20, 'caption' => $this->html_content )
  59. );
  60. $our_preg = preg_quote( $this->html_content );
  61. $this->assertEquals( 1, preg_match_all( "~{$our_preg}~", $result, $_r ) );
  62. }
  63. function test_new_img_caption_shortcode_new_format() {
  64. $result = img_caption_shortcode(
  65. array( 'width' => 20 ),
  66. $this->img_content . $this->html_content
  67. );
  68. $img_preg = preg_quote( $this->img_content );
  69. $content_preg = preg_quote( $this->html_content );
  70. $this->assertEquals( 1, preg_match_all( "~{$img_preg}.*wp-caption-text~", $result, $_r ) );
  71. $this->assertEquals( 1, preg_match_all( "~wp-caption-text.*{$content_preg}~", $result, $_r ) );
  72. }
  73. function test_new_img_caption_shortcode_new_format_and_linked_image() {
  74. $linked_image = "<a href='#'>{$this->img_content}</a>";
  75. $result = img_caption_shortcode(
  76. array( 'width' => 20 ),
  77. $linked_image . $this->html_content
  78. );
  79. $img_preg = preg_quote( $linked_image );
  80. $content_preg = preg_quote( $this->html_content );
  81. $this->assertEquals( 1, preg_match_all( "~{$img_preg}.*wp-caption-text~", $result, $_r ) );
  82. $this->assertEquals( 1, preg_match_all( "~wp-caption-text.*{$content_preg}~", $result, $_r ) );
  83. }
  84. function test_new_img_caption_shortcode_new_format_and_linked_image_with_newline() {
  85. $linked_image = "<a href='#'>{$this->img_content}</a>";
  86. $result = img_caption_shortcode(
  87. array( 'width' => 20 ),
  88. $linked_image . "\n\n" . $this->html_content
  89. );
  90. $img_preg = preg_quote( $linked_image );
  91. $content_preg = preg_quote( $this->html_content );
  92. $this->assertEquals( 1, preg_match_all( "~{$img_preg}.*wp-caption-text~", $result, $_r ) );
  93. $this->assertEquals( 1, preg_match_all( "~wp-caption-text.*{$content_preg}~", $result, $_r ) );
  94. }
  95. function test_add_remove_oembed_provider() {
  96. wp_oembed_add_provider( 'http://foo.bar/*', 'http://foo.bar/oembed' );
  97. $this->assertTrue( wp_oembed_remove_provider( 'http://foo.bar/*' ) );
  98. $this->assertFalse( wp_oembed_remove_provider( 'http://foo.bar/*' ) );
  99. }
  100. /**
  101. * Test secure youtube.com embeds
  102. *
  103. * @ticket 23149
  104. */
  105. function test_youtube_com_secure_embed() {
  106. global $wp_embed;
  107. $out = wp_oembed_get( 'http://www.youtube.com/watch?v=oHg5SJYRHA0' );
  108. $this->assertContains( 'http://www.youtube.com/embed/oHg5SJYRHA0?feature=oembed', $out );
  109. $out = wp_oembed_get( 'https://www.youtube.com/watch?v=oHg5SJYRHA0' );
  110. $this->assertContains( 'https://www.youtube.com/embed/oHg5SJYRHA0?feature=oembed', $out );
  111. $out = wp_oembed_get( 'https://youtu.be/zHjMoNQN7s0' );
  112. $this->assertContains( 'https://www.youtube.com/embed/zHjMoNQN7s0?feature=oembed', $out );
  113. }
  114. function test_wp_prepare_attachment_for_js() {
  115. // Attachment without media
  116. $id = wp_insert_attachment(array(
  117. 'post_status' => 'publish',
  118. 'post_title' => 'Prepare',
  119. 'post_content_filtered' => 'Prepare',
  120. 'post_type' => 'post'
  121. ));
  122. $post = get_post( $id );
  123. $prepped = wp_prepare_attachment_for_js( $post );
  124. $this->assertInternalType( 'array', $prepped );
  125. $this->assertEquals( 0, $prepped['uploadedTo'] );
  126. $this->assertEquals( '', $prepped['mime'] );
  127. $this->assertEquals( '', $prepped['type'] );
  128. $this->assertEquals( '', $prepped['subtype'] );
  129. $this->assertEquals( '', $prepped['url'] );
  130. $this->assertEquals( site_url( 'wp-includes/images/crystal/default.png' ), $prepped['icon'] );
  131. // Fake a mime
  132. $post->post_mime_type = 'image/jpeg';
  133. $prepped = wp_prepare_attachment_for_js( $post );
  134. $this->assertEquals( 'image/jpeg', $prepped['mime'] );
  135. $this->assertEquals( 'image', $prepped['type'] );
  136. $this->assertEquals( 'jpeg', $prepped['subtype'] );
  137. // Fake a mime without a slash. See #WP22532
  138. $post->post_mime_type = 'image';
  139. $prepped = wp_prepare_attachment_for_js( $post );
  140. $this->assertEquals( 'image', $prepped['mime'] );
  141. $this->assertEquals( 'image', $prepped['type'] );
  142. $this->assertEquals( '', $prepped['subtype'] );
  143. }
  144. /**
  145. * @ticket 19067
  146. * @expectedDeprecated wp_convert_bytes_to_hr
  147. */
  148. function test_wp_convert_bytes_to_hr() {
  149. $kb = 1024;
  150. $mb = $kb * 1024;
  151. $gb = $mb * 1024;
  152. $tb = $gb * 1024;
  153. // test if boundaries are correct
  154. $this->assertEquals( '1TB', wp_convert_bytes_to_hr( $tb ) );
  155. $this->assertEquals( '1GB', wp_convert_bytes_to_hr( $gb ) );
  156. $this->assertEquals( '1MB', wp_convert_bytes_to_hr( $mb ) );
  157. $this->assertEquals( '1kB', wp_convert_bytes_to_hr( $kb ) );
  158. $this->assertEquals( '1 TB', size_format( $tb ) );
  159. $this->assertEquals( '1 GB', size_format( $gb ) );
  160. $this->assertEquals( '1 MB', size_format( $mb ) );
  161. $this->assertEquals( '1 kB', size_format( $kb ) );
  162. // now some values around
  163. $hr = wp_convert_bytes_to_hr( $tb + $tb / 2 + $mb );
  164. $this->assertTrue( abs( 1.50000095367 - (float) str_replace( ',', '.', $hr ) ) < 0.0001 );
  165. $hr = wp_convert_bytes_to_hr( $tb - $mb - $kb );
  166. $this->assertTrue( abs( 1023.99902248 - (float) str_replace( ',', '.', $hr ) ) < 0.0001 );
  167. $hr = wp_convert_bytes_to_hr( $gb + $gb / 2 + $mb );
  168. $this->assertTrue( abs( 1.5009765625 - (float) str_replace( ',', '.', $hr ) ) < 0.0001 );
  169. $hr = wp_convert_bytes_to_hr( $gb - $mb - $kb );
  170. $this->assertTrue( abs( 1022.99902344 - (float) str_replace( ',', '.', $hr ) ) < 0.0001 );
  171. // edge
  172. $this->assertEquals( '-1B', wp_convert_bytes_to_hr( -1 ) );
  173. $this->assertEquals( '0B', wp_convert_bytes_to_hr( 0 ) );
  174. }
  175. /**
  176. * @ticket 22960
  177. */
  178. function test_get_attached_images() {
  179. $post_id = $this->factory->post->create();
  180. $attachment_id = $this->factory->attachment->create_object( $this->img_name, $post_id, array(
  181. 'post_mime_type' => 'image/jpeg',
  182. 'post_type' => 'attachment'
  183. ) );
  184. $images = get_attached_media( 'image', $post_id );
  185. $this->assertEquals( $images, array( $attachment_id => get_post( $attachment_id ) ) );
  186. }
  187. /**
  188. * @ticket 22960
  189. */
  190. function test_post_galleries_images() {
  191. $ids1 = array();
  192. $ids1_srcs = array();
  193. foreach ( range( 1, 3 ) as $i ) {
  194. $attachment_id = $this->factory->attachment->create_object( "image$i.jpg", 0, array(
  195. 'post_mime_type' => 'image/jpeg',
  196. 'post_type' => 'attachment'
  197. ) );
  198. wp_update_attachment_metadata( $attachment_id, $this->img_dimensions );
  199. $ids1[] = $attachment_id;
  200. $ids1_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
  201. }
  202. $ids2 = array();
  203. $ids2_srcs = array();
  204. foreach ( range( 4, 6 ) as $i ) {
  205. $attachment_id = $this->factory->attachment->create_object( "image$i.jpg", 0, array(
  206. 'post_mime_type' => 'image/jpeg',
  207. 'post_type' => 'attachment'
  208. ) );
  209. wp_update_attachment_metadata( $attachment_id, $this->img_dimensions );
  210. $ids2[] = $attachment_id;
  211. $ids2_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
  212. }
  213. $ids1_joined = join( ',', $ids1 );
  214. $ids2_joined = join( ',', $ids2 );
  215. $blob =<<<BLOB
  216. [gallery ids="$ids1_joined"]
  217. [gallery ids="$ids2_joined"]
  218. BLOB;
  219. $post_id = $this->factory->post->create( array( 'post_content' => $blob ) );
  220. $srcs = get_post_galleries_images( $post_id );
  221. $this->assertEquals( $srcs, array( $ids1_srcs, $ids2_srcs ) );
  222. }
  223. /**
  224. * @ticket 22960
  225. */
  226. function test_post_gallery_images() {
  227. $ids1 = array();
  228. $ids1_srcs = array();
  229. foreach ( range( 1, 3 ) as $i ) {
  230. $attachment_id = $this->factory->attachment->create_object( "image$i.jpg", 0, array(
  231. 'post_mime_type' => 'image/jpeg',
  232. 'post_type' => 'attachment'
  233. ) );
  234. wp_update_attachment_metadata( $attachment_id, $this->img_dimensions );
  235. $ids1[] = $attachment_id;
  236. $ids1_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
  237. }
  238. $ids2 = array();
  239. $ids2_srcs = array();
  240. foreach ( range( 4, 6 ) as $i ) {
  241. $attachment_id = $this->factory->attachment->create_object( "image$i.jpg", 0, array(
  242. 'post_mime_type' => 'image/jpeg',
  243. 'post_type' => 'attachment'
  244. ) );
  245. wp_update_attachment_metadata( $attachment_id, $this->img_dimensions );
  246. $ids2[] = $attachment_id;
  247. $ids2_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
  248. }
  249. $ids1_joined = join( ',', $ids1 );
  250. $ids2_joined = join( ',', $ids2 );
  251. $blob =<<<BLOB
  252. [gallery ids="$ids1_joined"]
  253. [gallery ids="$ids2_joined"]
  254. BLOB;
  255. $post_id = $this->factory->post->create( array( 'post_content' => $blob ) );
  256. $srcs = get_post_gallery_images( $post_id );
  257. $this->assertEquals( $srcs, $ids1_srcs );
  258. }
  259. function test_get_media_embedded_in_content() {
  260. $object =<<<OBJ
  261. <object src="this" data="that">
  262. <param name="value"/>
  263. </object>
  264. OBJ;
  265. $embed =<<<EMBED
  266. <embed src="something.mp4"/>
  267. EMBED;
  268. $iframe =<<<IFRAME
  269. <iframe src="youtube.com" width="7000" />
  270. IFRAME;
  271. $audio =<<<AUDIO
  272. <audio preload="none">
  273. <source />
  274. </audio>
  275. AUDIO;
  276. $video =<<<VIDEO
  277. <video preload="none">
  278. <source />
  279. </video>
  280. VIDEO;
  281. $content =<<<CONTENT
  282. This is a comment
  283. $object
  284. This is a comment
  285. $embed
  286. This is a comment
  287. $iframe
  288. This is a comment
  289. $audio
  290. This is a comment
  291. $video
  292. This is a comment
  293. CONTENT;
  294. $types = array( 'audio', 'video', 'object', 'embed', 'iframe' );
  295. $contents = array_values( compact( $types ) );
  296. $matches = get_media_embedded_in_content( $content, 'audio' );
  297. $this->assertEquals( array( $audio ), $matches );
  298. $matches = get_media_embedded_in_content( $content, 'video' );
  299. $this->assertEquals( array( $video ), $matches );
  300. $matches = get_media_embedded_in_content( $content, 'object' );
  301. $this->assertEquals( array( $object ), $matches );
  302. $matches = get_media_embedded_in_content( $content, 'embed' );
  303. $this->assertEquals( array( $embed ), $matches );
  304. $matches = get_media_embedded_in_content( $content, 'iframe' );
  305. $this->assertEquals( array( $iframe ), $matches );
  306. $matches = get_media_embedded_in_content( $content, $types );
  307. $this->assertEquals( $contents, $matches );
  308. }
  309. }