media.php 93 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182
  1. <?php
  2. /**
  3. * @group media
  4. * @group shortcode
  5. */
  6. class Tests_Media extends WP_UnitTestCase {
  7. protected static $large_id;
  8. protected static $_sizes;
  9. protected static $large_filename = 'test-image-large.jpg';
  10. protected static $post_ids;
  11. public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
  12. self::$_sizes = wp_get_additional_image_sizes();
  13. $GLOBALS['_wp_additional_image_sizes'] = array();
  14. $filename = DIR_TESTDATA . '/images/' . self::$large_filename;
  15. self::$large_id = $factory->attachment->create_upload_object( $filename );
  16. $post_statuses = array( 'publish', 'future', 'draft', 'auto-draft', 'trash' );
  17. foreach ( $post_statuses as $post_status ) {
  18. $date = '';
  19. if ( 'future' === $post_status ) {
  20. strftime( '%Y-%m-%d %H:%M:%S', strtotime( '+1 year' ) );
  21. }
  22. self::$post_ids[ $post_status ] = $factory->post->create(
  23. array(
  24. 'post_status' => 'trash' === $post_status ? 'publish' : $post_status,
  25. 'post_date' => $date,
  26. 'post_name' => "$post_status-post",
  27. )
  28. );
  29. // Attachments without media.
  30. self::$post_ids[ "$post_status-attachment" ] = $factory->attachment->create_object(
  31. array(
  32. 'post_parent' => self::$post_ids[ $post_status ],
  33. 'post_status' => 'inherit',
  34. 'post_name' => "$post_status-attachment",
  35. 'post_date' => $date,
  36. )
  37. );
  38. }
  39. // Trash the trash post.
  40. wp_trash_post( self::$post_ids['trash'] );
  41. }
  42. public static function wpTearDownAfterClass() {
  43. $GLOBALS['_wp_additional_image_sizes'] = self::$_sizes;
  44. }
  45. public static function tearDownAfterClass() {
  46. wp_delete_post( self::$large_id, true );
  47. parent::tearDownAfterClass();
  48. }
  49. function setUp() {
  50. parent::setUp();
  51. $this->caption = 'A simple caption.';
  52. $this->alternate_caption = 'Alternate caption.';
  53. $this->html_content = <<<CAP
  54. A <strong class='classy'>bolded</strong> <em>caption</em> with a <a href="#">link</a>.
  55. CAP;
  56. $this->img_content = <<<CAP
  57. <img src="pic.jpg" id='anId' alt="pic"/>
  58. CAP;
  59. $this->img_name = 'image.jpg';
  60. $this->img_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $this->img_name;
  61. $this->img_html = '<img src="' . $this->img_url . '"/>';
  62. $this->img_meta = array(
  63. 'width' => 100,
  64. 'height' => 100,
  65. 'sizes' => '',
  66. );
  67. }
  68. function test_img_caption_shortcode_added() {
  69. global $shortcode_tags;
  70. $this->assertSame( 'img_caption_shortcode', $shortcode_tags['caption'] );
  71. $this->assertSame( 'img_caption_shortcode', $shortcode_tags['wp_caption'] );
  72. }
  73. function test_img_caption_shortcode_with_empty_params() {
  74. $result = img_caption_shortcode( array() );
  75. $this->assertNull( $result );
  76. }
  77. /**
  78. * @ticket 33981
  79. */
  80. function test_img_caption_shortcode_with_empty_params_but_content() {
  81. $result = img_caption_shortcode( array(), $this->caption );
  82. $this->assertSame( $this->caption, $result );
  83. }
  84. /**
  85. * @ticket 33981
  86. */
  87. function test_img_caption_shortcode_short_circuit_filter() {
  88. add_filter( 'img_caption_shortcode', array( $this, '_return_alt_caption' ) );
  89. $result = img_caption_shortcode( array(), $this->caption );
  90. $this->assertSame( $this->alternate_caption, $result );
  91. }
  92. /**
  93. * Filter used in test_img_caption_shortcode_short_circuit_filter()
  94. */
  95. function _return_alt_caption() {
  96. return $this->alternate_caption;
  97. }
  98. /**
  99. * @ticket 33981
  100. */
  101. function test_img_caption_shortcode_empty_width() {
  102. $result = img_caption_shortcode(
  103. array(
  104. 'width' => 0,
  105. ),
  106. $this->caption
  107. );
  108. $this->assertSame( $this->caption, $result );
  109. }
  110. /**
  111. * @ticket 33981
  112. */
  113. function test_img_caption_shortcode_empty_caption() {
  114. $result = img_caption_shortcode(
  115. array(
  116. 'caption' => '',
  117. )
  118. );
  119. $this->assertNull( $result );
  120. }
  121. /**
  122. * @ticket 33981
  123. */
  124. function test_img_caption_shortcode_empty_caption_and_content() {
  125. $result = img_caption_shortcode(
  126. array(
  127. 'caption' => '',
  128. ),
  129. $this->caption
  130. );
  131. $this->assertSame( $this->caption, $result );
  132. }
  133. function test_img_caption_shortcode_with_old_format() {
  134. $result = img_caption_shortcode(
  135. array(
  136. 'width' => 20,
  137. 'caption' => $this->caption,
  138. )
  139. );
  140. $this->assertSame( 2, preg_match_all( '/wp-caption/', $result, $_r ) );
  141. $this->assertSame( 1, preg_match_all( '/alignnone/', $result, $_r ) );
  142. $this->assertSame( 1, preg_match_all( "/{$this->caption}/", $result, $_r ) );
  143. if ( current_theme_supports( 'html5', 'caption' ) ) {
  144. $this->assertSame( 1, preg_match_all( '/width: 20/', $result, $_r ) );
  145. } else {
  146. $this->assertSame( 1, preg_match_all( '/width: 30/', $result, $_r ) );
  147. }
  148. }
  149. function test_img_caption_shortcode_with_old_format_id_and_align() {
  150. $result = img_caption_shortcode(
  151. array(
  152. 'width' => 20,
  153. 'caption' => $this->caption,
  154. 'id' => '"myId',
  155. 'align' => '&myAlignment',
  156. )
  157. );
  158. $this->assertSame( 1, preg_match_all( '/wp-caption &amp;myAlignment/', $result, $_r ) );
  159. $this->assertSame( 1, preg_match_all( '/id="myId"/', $result, $_r ) );
  160. $this->assertSame( 1, preg_match_all( "/{$this->caption}/", $result, $_r ) );
  161. }
  162. function test_img_caption_shortcode_with_old_format_and_class() {
  163. $result = img_caption_shortcode(
  164. array(
  165. 'width' => 20,
  166. 'class' => 'some-class another-class',
  167. 'caption' => $this->caption,
  168. )
  169. );
  170. $this->assertSame( 1, preg_match_all( '/wp-caption alignnone some-class another-class/', $result, $_r ) );
  171. }
  172. function test_new_img_caption_shortcode_with_html_caption() {
  173. $result = img_caption_shortcode(
  174. array(
  175. 'width' => 20,
  176. 'caption' => $this->html_content,
  177. )
  178. );
  179. $our_preg = preg_quote( $this->html_content );
  180. $this->assertSame( 1, preg_match_all( "~{$our_preg}~", $result, $_r ) );
  181. }
  182. function test_new_img_caption_shortcode_new_format() {
  183. $result = img_caption_shortcode(
  184. array( 'width' => 20 ),
  185. $this->img_content . $this->html_content
  186. );
  187. $img_preg = preg_quote( $this->img_content );
  188. $content_preg = preg_quote( $this->html_content );
  189. $this->assertSame( 1, preg_match_all( "~{$img_preg}.*wp-caption-text~", $result, $_r ) );
  190. $this->assertSame( 1, preg_match_all( "~wp-caption-text.*{$content_preg}~", $result, $_r ) );
  191. }
  192. function test_new_img_caption_shortcode_new_format_and_linked_image() {
  193. $linked_image = "<a href='#'>{$this->img_content}</a>";
  194. $result = img_caption_shortcode(
  195. array( 'width' => 20 ),
  196. $linked_image . $this->html_content
  197. );
  198. $img_preg = preg_quote( $linked_image );
  199. $content_preg = preg_quote( $this->html_content );
  200. $this->assertSame( 1, preg_match_all( "~{$img_preg}.*wp-caption-text~", $result, $_r ) );
  201. $this->assertSame( 1, preg_match_all( "~wp-caption-text.*{$content_preg}~", $result, $_r ) );
  202. }
  203. function test_new_img_caption_shortcode_new_format_and_linked_image_with_newline() {
  204. $linked_image = "<a href='#'>{$this->img_content}</a>";
  205. $result = img_caption_shortcode(
  206. array( 'width' => 20 ),
  207. $linked_image . "\n\n" . $this->html_content
  208. );
  209. $img_preg = preg_quote( $linked_image );
  210. $content_preg = preg_quote( $this->html_content );
  211. $this->assertSame( 1, preg_match_all( "~{$img_preg}.*wp-caption-text~", $result, $_r ) );
  212. $this->assertSame( 1, preg_match_all( "~wp-caption-text.*{$content_preg}~", $result, $_r ) );
  213. }
  214. /**
  215. * @ticket 34595
  216. */
  217. function test_img_caption_shortcode_has_aria_describedby() {
  218. $result = img_caption_shortcode(
  219. array(
  220. 'width' => 20,
  221. 'id' => 'myId',
  222. ),
  223. $this->img_content . $this->html_content
  224. );
  225. $this->assertSame( 1, preg_match_all( '/aria-describedby="caption-myId"/', $result, $_r ) );
  226. }
  227. function test_add_remove_oembed_provider() {
  228. wp_oembed_add_provider( 'http://foo.bar/*', 'http://foo.bar/oembed' );
  229. $this->assertTrue( wp_oembed_remove_provider( 'http://foo.bar/*' ) );
  230. $this->assertFalse( wp_oembed_remove_provider( 'http://foo.bar/*' ) );
  231. }
  232. /**
  233. * @ticket 23776
  234. */
  235. function test_autoembed_empty() {
  236. global $wp_embed;
  237. $content = '';
  238. $result = $wp_embed->autoembed( $content );
  239. $this->assertSame( $content, $result );
  240. }
  241. /**
  242. * @ticket 23776
  243. *
  244. * @group external-http
  245. */
  246. function test_autoembed_no_paragraphs_around_urls() {
  247. global $wp_embed;
  248. $content = <<<EOF
  249. $ my command
  250. First line.
  251. http://example.com/1/
  252. http://example.com/2/
  253. Last line.
  254. <pre>http://some.link/
  255. http://some.other.link/</pre>
  256. EOF;
  257. $result = $wp_embed->autoembed( $content );
  258. $this->assertSame( $content, $result );
  259. }
  260. function data_autoembed() {
  261. return array(
  262. // Should embed.
  263. array(
  264. 'https://w.org',
  265. '[embed]',
  266. ),
  267. array(
  268. 'test
  269. https://w.org
  270. test',
  271. 'test
  272. [embed]
  273. test',
  274. ),
  275. array(
  276. '<p class="test">https://w.org</p>',
  277. '<p class="test">[embed]</p>',
  278. ),
  279. array(
  280. '<p> https://w.org </p>',
  281. '<p> [embed] </p>',
  282. ),
  283. array(
  284. '<p>test
  285. https://w.org
  286. test</p>',
  287. '<p>test
  288. [embed]
  289. test</p>',
  290. ),
  291. array(
  292. '<p>https://w.org
  293. </p>',
  294. '<p>[embed]
  295. </p>',
  296. ),
  297. // Should NOT embed.
  298. array(
  299. 'test https://w.org</p>',
  300. ),
  301. array(
  302. '<span>https://w.org</a>',
  303. ),
  304. array(
  305. '<pre>https://w.org
  306. </p>',
  307. ),
  308. array(
  309. '<a href="https://w.org">
  310. https://w.org</a>',
  311. ),
  312. );
  313. }
  314. /**
  315. * @dataProvider data_autoembed
  316. */
  317. function test_autoembed( $content, $result = null ) {
  318. $wp_embed = new Test_Autoembed;
  319. $this->assertSame( $wp_embed->autoembed( $content ), $result ? $result : $content );
  320. }
  321. function test_wp_prepare_attachment_for_js() {
  322. // Attachment without media.
  323. $id = wp_insert_attachment(
  324. array(
  325. 'post_status' => 'publish',
  326. 'post_title' => 'Prepare',
  327. 'post_content_filtered' => 'Prepare',
  328. 'post_type' => 'post',
  329. )
  330. );
  331. $post = get_post( $id );
  332. $prepped = wp_prepare_attachment_for_js( $post );
  333. $this->assertInternalType( 'array', $prepped );
  334. $this->assertSame( 0, $prepped['uploadedTo'] );
  335. $this->assertSame( '', $prepped['mime'] );
  336. $this->assertSame( '', $prepped['type'] );
  337. $this->assertSame( '', $prepped['subtype'] );
  338. // #21963, there will be a GUID always, so there will be a URL.
  339. $this->assertNotEquals( '', $prepped['url'] );
  340. $this->assertSame( site_url( 'wp-includes/images/media/default.png' ), $prepped['icon'] );
  341. // Fake a mime.
  342. $post->post_mime_type = 'image/jpeg';
  343. $prepped = wp_prepare_attachment_for_js( $post );
  344. $this->assertSame( 'image/jpeg', $prepped['mime'] );
  345. $this->assertSame( 'image', $prepped['type'] );
  346. $this->assertSame( 'jpeg', $prepped['subtype'] );
  347. // Fake a mime without a slash. See #WP22532.
  348. $post->post_mime_type = 'image';
  349. $prepped = wp_prepare_attachment_for_js( $post );
  350. $this->assertSame( 'image', $prepped['mime'] );
  351. $this->assertSame( 'image', $prepped['type'] );
  352. $this->assertSame( '', $prepped['subtype'] );
  353. // Test that if author is not found, we return "(no author)" as `display_name`.
  354. // The previously used test post contains no author, so we can reuse it.
  355. $this->assertSame( '(no author)', $prepped['authorName'] );
  356. // Test that if author has HTML entities in display_name, they're decoded correctly.
  357. $html_entity_author = self::factory()->user->create(
  358. array(
  359. 'display_name' => 'You &amp; Me',
  360. )
  361. );
  362. $post->post_author = $html_entity_author;
  363. $prepped = wp_prepare_attachment_for_js( $post );
  364. $this->assertSame( 'You & Me', $prepped['authorName'] );
  365. }
  366. /**
  367. * @ticket 38965
  368. */
  369. function test_wp_prepare_attachment_for_js_without_image_sizes() {
  370. // Create the attachement post.
  371. $id = wp_insert_attachment(
  372. array(
  373. 'post_title' => 'Attachment Title',
  374. 'post_type' => 'attachment',
  375. 'post_parent' => 0,
  376. 'post_mime_type' => 'image/jpeg',
  377. 'guid' => 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test-image.jpg',
  378. )
  379. );
  380. // Add attachment metadata without sizes.
  381. wp_update_attachment_metadata(
  382. $id,
  383. array(
  384. 'width' => 50,
  385. 'height' => 50,
  386. 'file' => 'test-image.jpg',
  387. )
  388. );
  389. $prepped = wp_prepare_attachment_for_js( get_post( $id ) );
  390. $this->assertTrue( isset( $prepped['sizes'] ) );
  391. }
  392. /**
  393. * @ticket 19067
  394. * @expectedDeprecated wp_convert_bytes_to_hr
  395. */
  396. function test_wp_convert_bytes_to_hr() {
  397. $kb = 1024;
  398. $mb = $kb * 1024;
  399. $gb = $mb * 1024;
  400. $tb = $gb * 1024;
  401. // Test if boundaries are correct.
  402. $this->assertSame( '1TB', wp_convert_bytes_to_hr( $tb ) );
  403. $this->assertSame( '1GB', wp_convert_bytes_to_hr( $gb ) );
  404. $this->assertSame( '1MB', wp_convert_bytes_to_hr( $mb ) );
  405. $this->assertSame( '1KB', wp_convert_bytes_to_hr( $kb ) );
  406. $this->assertSame( '1 TB', size_format( $tb ) );
  407. $this->assertSame( '1 GB', size_format( $gb ) );
  408. $this->assertSame( '1 MB', size_format( $mb ) );
  409. $this->assertSame( '1 KB', size_format( $kb ) );
  410. // Now some values around.
  411. $hr = wp_convert_bytes_to_hr( $tb + $tb / 2 + $mb );
  412. $this->assertEqualsWithDelta( 1.50000095367, (float) str_replace( ',', '.', $hr ), 0.0001, 'The values should be equal' );
  413. $hr = wp_convert_bytes_to_hr( $tb - $mb - $kb );
  414. $this->assertEqualsWithDelta( 1023.99902248, (float) str_replace( ',', '.', $hr ), 0.0001, 'The values should be equal' );
  415. $hr = wp_convert_bytes_to_hr( $gb + $gb / 2 + $mb );
  416. $this->assertEqualsWithDelta( 1.5009765625, (float) str_replace( ',', '.', $hr ), 0.0001, 'The values should be equal' );
  417. $hr = wp_convert_bytes_to_hr( $gb - $mb - $kb );
  418. $this->assertEqualsWithDelta( 1022.99902344, (float) str_replace( ',', '.', $hr ), 0.0001, 'The values should be equal' );
  419. // Edge.
  420. $this->assertSame( '-1B', wp_convert_bytes_to_hr( -1 ) );
  421. $this->assertSame( '0B', wp_convert_bytes_to_hr( 0 ) );
  422. }
  423. /**
  424. * @ticket 22960
  425. */
  426. function test_get_attached_images() {
  427. $post_id = self::factory()->post->create();
  428. $attachment_id = self::factory()->attachment->create_object(
  429. $this->img_name,
  430. $post_id,
  431. array(
  432. 'post_mime_type' => 'image/jpeg',
  433. 'post_type' => 'attachment',
  434. )
  435. );
  436. $images = get_attached_media( 'image', $post_id );
  437. $this->assertEquals( $images, array( $attachment_id => get_post( $attachment_id ) ) );
  438. }
  439. /**
  440. * @ticket 22960
  441. */
  442. function test_post_galleries_images() {
  443. $ids1 = array();
  444. $ids1_srcs = array();
  445. foreach ( range( 1, 3 ) as $i ) {
  446. $attachment_id = self::factory()->attachment->create_object(
  447. "image$i.jpg",
  448. 0,
  449. array(
  450. 'post_mime_type' => 'image/jpeg',
  451. 'post_type' => 'attachment',
  452. )
  453. );
  454. $metadata = array_merge( array( 'file' => "image$i.jpg" ), $this->img_meta );
  455. wp_update_attachment_metadata( $attachment_id, $metadata );
  456. $ids1[] = $attachment_id;
  457. $ids1_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
  458. }
  459. $ids2 = array();
  460. $ids2_srcs = array();
  461. foreach ( range( 4, 6 ) as $i ) {
  462. $attachment_id = self::factory()->attachment->create_object(
  463. "image$i.jpg",
  464. 0,
  465. array(
  466. 'post_mime_type' => 'image/jpeg',
  467. 'post_type' => 'attachment',
  468. )
  469. );
  470. $metadata = array_merge( array( 'file' => "image$i.jpg" ), $this->img_meta );
  471. wp_update_attachment_metadata( $attachment_id, $metadata );
  472. $ids2[] = $attachment_id;
  473. $ids2_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
  474. }
  475. $ids1_joined = implode( ',', $ids1 );
  476. $ids2_joined = implode( ',', $ids2 );
  477. $blob = <<<BLOB
  478. [gallery ids="$ids1_joined"]
  479. [gallery ids="$ids2_joined"]
  480. BLOB;
  481. $post_id = self::factory()->post->create( array( 'post_content' => $blob ) );
  482. $srcs = get_post_galleries_images( $post_id );
  483. $this->assertSame( $srcs, array( $ids1_srcs, $ids2_srcs ) );
  484. }
  485. /**
  486. * @ticket 39304
  487. */
  488. function test_post_galleries_images_without_global_post() {
  489. // Set up an unattached image.
  490. $this->factory->attachment->create_object(
  491. array(
  492. 'file' => 'test.jpg',
  493. 'post_parent' => 0,
  494. 'post_mime_type' => 'image/jpeg',
  495. 'post_type' => 'attachment',
  496. )
  497. );
  498. $post_id = $this->factory->post->create(
  499. array(
  500. 'post_content' => '[gallery]',
  501. )
  502. );
  503. $galleries = get_post_galleries( $post_id, false );
  504. $this->assertEmpty( $galleries[0]['src'] );
  505. }
  506. /**
  507. * @ticket 39304
  508. */
  509. function test_post_galleries_ignores_global_post() {
  510. $global_post_id = $this->factory->post->create(
  511. array(
  512. 'post_content' => 'Global Post',
  513. )
  514. );
  515. $post_id = $this->factory->post->create(
  516. array(
  517. 'post_content' => '[gallery]',
  518. )
  519. );
  520. $this->factory->attachment->create_object(
  521. array(
  522. 'file' => 'test.jpg',
  523. 'post_parent' => $post_id,
  524. 'post_mime_type' => 'image/jpeg',
  525. 'post_type' => 'attachment',
  526. )
  527. );
  528. $expected_srcs = array(
  529. 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test.jpg',
  530. );
  531. // Set the global $post context to the other post.
  532. $GLOBALS['post'] = get_post( $global_post_id );
  533. $galleries = get_post_galleries( $post_id, false );
  534. $this->assertNotEmpty( $galleries[0]['src'] );
  535. $this->assertSame( $galleries[0]['src'], $expected_srcs );
  536. }
  537. /**
  538. * @ticket 39304
  539. */
  540. function test_post_galleries_respects_id_attrs() {
  541. $post_id = $this->factory->post->create(
  542. array(
  543. 'post_content' => 'No gallery defined',
  544. )
  545. );
  546. $post_id_two = $this->factory->post->create(
  547. array(
  548. 'post_content' => "[gallery id='$post_id']",
  549. )
  550. );
  551. $this->factory->attachment->create_object(
  552. array(
  553. 'file' => 'test.jpg',
  554. 'post_parent' => $post_id,
  555. 'post_mime_type' => 'image/jpeg',
  556. 'post_type' => 'attachment',
  557. )
  558. );
  559. $expected_srcs = array(
  560. 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test.jpg',
  561. );
  562. $galleries = get_post_galleries( $post_id_two, false );
  563. // Set the global $post context.
  564. $GLOBALS['post'] = get_post( $post_id_two );
  565. $galleries_with_global_context = get_post_galleries( $post_id_two, false );
  566. // Check that the global post state doesn't affect the results.
  567. $this->assertSame( $galleries, $galleries_with_global_context );
  568. $this->assertNotEmpty( $galleries[0]['src'] );
  569. $this->assertSame( $galleries[0]['src'], $expected_srcs );
  570. }
  571. /**
  572. * @ticket 22960
  573. */
  574. function test_post_gallery_images() {
  575. $ids1 = array();
  576. $ids1_srcs = array();
  577. foreach ( range( 1, 3 ) as $i ) {
  578. $attachment_id = self::factory()->attachment->create_object(
  579. "image$i.jpg",
  580. 0,
  581. array(
  582. 'post_mime_type' => 'image/jpeg',
  583. 'post_type' => 'attachment',
  584. )
  585. );
  586. $metadata = array_merge( array( 'file' => "image$i.jpg" ), $this->img_meta );
  587. wp_update_attachment_metadata( $attachment_id, $metadata );
  588. $ids1[] = $attachment_id;
  589. $ids1_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
  590. }
  591. $ids2 = array();
  592. $ids2_srcs = array();
  593. foreach ( range( 4, 6 ) as $i ) {
  594. $attachment_id = self::factory()->attachment->create_object(
  595. "image$i.jpg",
  596. 0,
  597. array(
  598. 'post_mime_type' => 'image/jpeg',
  599. 'post_type' => 'attachment',
  600. )
  601. );
  602. $metadata = array_merge( array( 'file' => "image$i.jpg" ), $this->img_meta );
  603. wp_update_attachment_metadata( $attachment_id, $metadata );
  604. $ids2[] = $attachment_id;
  605. $ids2_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
  606. }
  607. $ids1_joined = implode( ',', $ids1 );
  608. $ids2_joined = implode( ',', $ids2 );
  609. $blob = <<<BLOB
  610. [gallery ids="$ids1_joined"]
  611. [gallery ids="$ids2_joined"]
  612. BLOB;
  613. $post_id = self::factory()->post->create( array( 'post_content' => $blob ) );
  614. $srcs = get_post_gallery_images( $post_id );
  615. $this->assertSame( $srcs, $ids1_srcs );
  616. }
  617. function test_get_media_embedded_in_content() {
  618. $object = <<<OBJ
  619. <object src="this" data="that">
  620. <param name="value"/>
  621. </object>
  622. OBJ;
  623. $embed = <<<EMBED
  624. <embed src="something.mp4"/>
  625. EMBED;
  626. $iframe = <<<IFRAME
  627. <iframe src="youtube.com" width="7000" />
  628. IFRAME;
  629. $audio = <<<AUDIO
  630. <audio preload="none">
  631. <source />
  632. </audio>
  633. AUDIO;
  634. $video = <<<VIDEO
  635. <video preload="none">
  636. <source />
  637. </video>
  638. VIDEO;
  639. $content = <<<CONTENT
  640. This is a comment
  641. $object
  642. This is a comment
  643. $embed
  644. This is a comment
  645. $iframe
  646. This is a comment
  647. $audio
  648. This is a comment
  649. $video
  650. This is a comment
  651. CONTENT;
  652. $types = array( 'object', 'embed', 'iframe', 'audio', 'video' );
  653. $contents = array_values( compact( $types ) );
  654. $matches = get_media_embedded_in_content( $content, 'audio' );
  655. $this->assertSame( array( $audio ), $matches );
  656. $matches = get_media_embedded_in_content( $content, 'video' );
  657. $this->assertSame( array( $video ), $matches );
  658. $matches = get_media_embedded_in_content( $content, 'object' );
  659. $this->assertSame( array( $object ), $matches );
  660. $matches = get_media_embedded_in_content( $content, 'embed' );
  661. $this->assertSame( array( $embed ), $matches );
  662. $matches = get_media_embedded_in_content( $content, 'iframe' );
  663. $this->assertSame( array( $iframe ), $matches );
  664. $matches = get_media_embedded_in_content( $content, $types );
  665. $this->assertSame( $contents, $matches );
  666. }
  667. function test_get_media_embedded_in_content_order() {
  668. $audio = <<<AUDIO
  669. <audio preload="none">
  670. <source />
  671. </audio>
  672. AUDIO;
  673. $video = <<<VIDEO
  674. <video preload="none">
  675. <source />
  676. </video>
  677. VIDEO;
  678. $content = $audio . $video;
  679. $matches1 = get_media_embedded_in_content( $content, array( 'audio', 'video' ) );
  680. $this->assertSame( array( $audio, $video ), $matches1 );
  681. $reversed = $video . $audio;
  682. $matches2 = get_media_embedded_in_content( $reversed, array( 'audio', 'video' ) );
  683. $this->assertSame( array( $video, $audio ), $matches2 );
  684. }
  685. /**
  686. * @ticket 35367
  687. */
  688. function test_wp_audio_shortcode_with_empty_params() {
  689. $this->assertNull( wp_audio_shortcode( array() ) );
  690. }
  691. /**
  692. * @ticket 35367
  693. */
  694. function test_wp_audio_shortcode_with_bad_attr() {
  695. $this->assertSame(
  696. '<a class="wp-embedded-audio" href="https://example.com/foo.php">https://example.com/foo.php</a>',
  697. wp_audio_shortcode(
  698. array(
  699. 'src' => 'https://example.com/foo.php',
  700. )
  701. )
  702. );
  703. }
  704. /**
  705. * @ticket 35367
  706. */
  707. function test_wp_audio_shortcode_attributes() {
  708. $actual = wp_audio_shortcode(
  709. array(
  710. 'src' => 'https://example.com/foo.mp3',
  711. )
  712. );
  713. $this->assertContains( 'src="https://example.com/foo.mp3', $actual );
  714. $this->assertNotContains( 'loop', $actual );
  715. $this->assertNotContains( 'autoplay', $actual );
  716. $this->assertContains( 'preload="none"', $actual );
  717. $this->assertContains( 'class="wp-audio-shortcode"', $actual );
  718. $this->assertContains( 'style="width: 100%;"', $actual );
  719. $actual = wp_audio_shortcode(
  720. array(
  721. 'src' => 'https://example.com/foo.mp3',
  722. 'loop' => true,
  723. 'autoplay' => true,
  724. 'preload' => true,
  725. 'class' => 'foobar',
  726. 'style' => 'padding:0;',
  727. )
  728. );
  729. $this->assertContains( 'src="https://example.com/foo.mp3', $actual );
  730. $this->assertContains( 'loop="1"', $actual );
  731. $this->assertContains( 'autoplay="1"', $actual );
  732. $this->assertContains( 'preload="1"', $actual );
  733. $this->assertContains( 'class="foobar"', $actual );
  734. $this->assertContains( 'style="padding:0;"', $actual );
  735. }
  736. /**
  737. * Test [video] shortcode processing
  738. */
  739. function test_video_shortcode_body() {
  740. $width = 720;
  741. $height = 480;
  742. $w = empty( $GLOBALS['content_width'] ) ? 640 : $GLOBALS['content_width'];
  743. if ( $width > $w ) {
  744. $width = $w;
  745. }
  746. $post_id = get_post() ? get_the_ID() : 0;
  747. $video = <<<VIDEO
  748. [video width="$width" height="480" mp4="http://domain.tld/wp-content/uploads/2013/12/xyz.mp4"]
  749. <!-- WebM/VP8 for Firefox4, Opera, and Chrome -->
  750. <source type="video/webm" src="myvideo.webm" />
  751. <!-- Ogg/Vorbis for older Firefox and Opera versions -->
  752. <source type="video/ogg" src="myvideo.ogv" />
  753. <!-- Optional: Add subtitles for each language -->
  754. <track kind="subtitles" src="subtitles.srt" srclang="en" />
  755. <!-- Optional: Add chapters -->
  756. <track kind="chapters" src="chapters.srt" srclang="en" />
  757. [/video]
  758. VIDEO;
  759. $h = ceil( ( $height * $width ) / $width );
  760. $content = apply_filters( 'the_content', $video );
  761. $expected = '<div style="width: ' . $width . 'px;" class="wp-video">' .
  762. "<!--[if lt IE 9]><script>document.createElement('video');</script><![endif]-->\n" .
  763. '<video class="wp-video-shortcode" id="video-' . $post_id . '-1" width="' . $width . '" height="' . $h . '" preload="metadata" controls="controls">' .
  764. '<source type="video/mp4" src="http://domain.tld/wp-content/uploads/2013/12/xyz.mp4?_=1" />' .
  765. '<!-- WebM/VP8 for Firefox4, Opera, and Chrome --><source type="video/webm" src="myvideo.webm" />' .
  766. '<!-- Ogg/Vorbis for older Firefox and Opera versions --><source type="video/ogg" src="myvideo.ogv" />' .
  767. '<!-- Optional: Add subtitles for each language --><track kind="subtitles" src="subtitles.srt" srclang="en" />' .
  768. '<!-- Optional: Add chapters --><track kind="chapters" src="chapters.srt" srclang="en" />' .
  769. '<a href="http://domain.tld/wp-content/uploads/2013/12/xyz.mp4">' .
  770. "http://domain.tld/wp-content/uploads/2013/12/xyz.mp4</a></video></div>\n";
  771. $this->assertSame( $expected, $content );
  772. }
  773. /**
  774. * @ticket 35367
  775. * @depends test_video_shortcode_body
  776. */
  777. function test_wp_video_shortcode_with_empty_params() {
  778. $this->assertNull( wp_video_shortcode( array() ) );
  779. }
  780. /**
  781. * @ticket 35367
  782. * @depends test_video_shortcode_body
  783. */
  784. function test_wp_video_shortcode_with_bad_attr() {
  785. $this->assertSame(
  786. '<a class="wp-embedded-video" href="https://example.com/foo.php">https://example.com/foo.php</a>',
  787. wp_video_shortcode(
  788. array(
  789. 'src' => 'https://example.com/foo.php',
  790. )
  791. )
  792. );
  793. }
  794. /**
  795. * @ticket 35367
  796. * @depends test_video_shortcode_body
  797. */
  798. function test_wp_video_shortcode_attributes() {
  799. $actual = wp_video_shortcode(
  800. array(
  801. 'src' => 'https://example.com/foo.mp4',
  802. )
  803. );
  804. $this->assertContains( 'src="https://example.com/foo.mp4', $actual );
  805. $this->assertNotContains( 'loop', $actual );
  806. $this->assertNotContains( 'autoplay', $actual );
  807. $this->assertContains( 'preload="metadata"', $actual );
  808. $this->assertContains( 'width="640"', $actual );
  809. $this->assertContains( 'height="360"', $actual );
  810. $this->assertContains( 'class="wp-video-shortcode"', $actual );
  811. $actual = wp_video_shortcode(
  812. array(
  813. 'src' => 'https://example.com/foo.mp4',
  814. 'poster' => 'https://example.com/foo.png',
  815. 'loop' => true,
  816. 'autoplay' => true,
  817. 'preload' => true,
  818. 'width' => 123,
  819. 'height' => 456,
  820. 'class' => 'foobar',
  821. )
  822. );
  823. $this->assertContains( 'src="https://example.com/foo.mp4', $actual );
  824. $this->assertContains( 'poster="https://example.com/foo.png', $actual );
  825. $this->assertContains( 'loop="1"', $actual );
  826. $this->assertContains( 'autoplay="1"', $actual );
  827. $this->assertContains( 'preload="1"', $actual );
  828. $this->assertContains( 'width="123"', $actual );
  829. $this->assertContains( 'height="456"', $actual );
  830. $this->assertContains( 'class="foobar"', $actual );
  831. }
  832. /**
  833. * @ticket 40866
  834. * @depends test_video_shortcode_body
  835. */
  836. function test_wp_video_shortcode_youtube_remove_feature() {
  837. $actual = wp_video_shortcode(
  838. array(
  839. 'src' => 'https://www.youtube.com/watch?v=72xdCU__XCk&feature=youtu.be',
  840. )
  841. );
  842. $this->assertNotContains( 'feature=youtu.be', $actual );
  843. }
  844. /**
  845. * @ticket 40866
  846. * @depends test_video_shortcode_body
  847. */
  848. function test_wp_video_shortcode_youtube_force_ssl() {
  849. $actual = wp_video_shortcode(
  850. array(
  851. 'src' => 'http://www.youtube.com/watch?v=72xdCU__XCk',
  852. )
  853. );
  854. $this->assertContains( 'src="https://www.youtube.com/watch?v=72xdCU__XCk', $actual );
  855. }
  856. /**
  857. * @ticket 40866
  858. * @depends test_video_shortcode_body
  859. */
  860. function test_wp_video_shortcode_vimeo_force_ssl_remove_query_args() {
  861. $actual = wp_video_shortcode(
  862. array(
  863. 'src' => 'http://vimeo.com/76979871?blah=meh',
  864. )
  865. );
  866. $this->assertContains( 'src="https://vimeo.com/76979871', $actual );
  867. $this->assertNotContains( 'blah=meh', $actual );
  868. }
  869. /**
  870. * @ticket 40977
  871. * @depends test_video_shortcode_body
  872. */
  873. function test_wp_video_shortcode_vimeo_adds_loop() {
  874. $actual = wp_video_shortcode(
  875. array(
  876. 'src' => 'http://vimeo.com/76979871',
  877. )
  878. );
  879. $this->assertContains( 'src="https://vimeo.com/76979871?loop=0', $actual );
  880. }
  881. /**
  882. * @ticket 40977
  883. * @depends test_video_shortcode_body
  884. */
  885. function test_wp_video_shortcode_vimeo_force_adds_loop_true() {
  886. $actual = wp_video_shortcode(
  887. array(
  888. 'src' => 'http://vimeo.com/76979871',
  889. 'loop' => true,
  890. )
  891. );
  892. $this->assertContains( 'src="https://vimeo.com/76979871?loop=1', $actual );
  893. }
  894. /**
  895. * @ticket 26768
  896. */
  897. function test_add_image_size() {
  898. $_wp_additional_image_sizes = wp_get_additional_image_sizes();
  899. remove_image_size( 'test-size' );
  900. $this->assertArrayNotHasKey( 'test-size', $_wp_additional_image_sizes );
  901. add_image_size( 'test-size', 200, 600 );
  902. $sizes = wp_get_additional_image_sizes();
  903. // Clean up.
  904. remove_image_size( 'test-size' );
  905. $this->assertArrayHasKey( 'test-size', $sizes );
  906. $this->assertSame( 200, $sizes['test-size']['width'] );
  907. $this->assertSame( 600, $sizes['test-size']['height'] );
  908. }
  909. /**
  910. * @ticket 26768
  911. */
  912. function test_remove_image_size() {
  913. add_image_size( 'test-size', 200, 600 );
  914. $this->assertTrue( has_image_size( 'test-size' ) );
  915. remove_image_size( 'test-size' );
  916. $this->assertFalse( has_image_size( 'test-size' ) );
  917. }
  918. /**
  919. * @ticket 26951
  920. */
  921. function test_has_image_size() {
  922. add_image_size( 'test-size', 200, 600 );
  923. $this->assertTrue( has_image_size( 'test-size' ) );
  924. // Clean up.
  925. remove_image_size( 'test-size' );
  926. }
  927. /**
  928. * @ticket 30346
  929. */
  930. function test_attachment_url_to_postid() {
  931. $image_path = '2014/11/' . $this->img_name;
  932. $attachment_id = self::factory()->attachment->create_object(
  933. $image_path,
  934. 0,
  935. array(
  936. 'post_mime_type' => 'image/jpeg',
  937. 'post_type' => 'attachment',
  938. )
  939. );
  940. $image_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_path;
  941. $this->assertSame( $attachment_id, attachment_url_to_postid( $image_url ) );
  942. }
  943. /**
  944. * @ticket 33109
  945. */
  946. function test_attachment_url_to_postid_with_different_scheme() {
  947. $image_path = '2014/11/' . $this->img_name;
  948. $attachment_id = self::factory()->attachment->create_object(
  949. $image_path,
  950. 0,
  951. array(
  952. 'post_mime_type' => 'image/jpeg',
  953. 'post_type' => 'attachment',
  954. )
  955. );
  956. $image_url = 'https://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_path;
  957. $this->assertSame( $attachment_id, attachment_url_to_postid( $image_url ) );
  958. }
  959. /**
  960. * @ticket 39768
  961. */
  962. function test_attachment_url_to_postid_should_be_case_sensitive() {
  963. $image_path_lower_case = '2014/11/' . $this->img_name;
  964. $attachment_id_lower_case = self::factory()->attachment->create_object(
  965. $image_path_lower_case,
  966. 0,
  967. array(
  968. 'post_mime_type' => 'image/jpeg',
  969. 'post_type' => 'attachment',
  970. )
  971. );
  972. $image_path_upper_case = '2014/11/' . ucfirst( $this->img_name );
  973. $attachment_id_upper_case = self::factory()->attachment->create_object(
  974. $image_path_upper_case,
  975. 0,
  976. array(
  977. 'post_mime_type' => 'image/jpeg',
  978. 'post_type' => 'attachment',
  979. )
  980. );
  981. $image_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_path_upper_case;
  982. $this->assertSame( $attachment_id_upper_case, attachment_url_to_postid( $image_url ) );
  983. }
  984. function test_attachment_url_to_postid_filtered() {
  985. $image_path = '2014/11/' . $this->img_name;
  986. $attachment_id = self::factory()->attachment->create_object(
  987. $image_path,
  988. 0,
  989. array(
  990. 'post_mime_type' => 'image/jpeg',
  991. 'post_type' => 'attachment',
  992. )
  993. );
  994. add_filter( 'upload_dir', array( $this, '_upload_dir' ) );
  995. $image_url = 'http://192.168.1.20.com/wp-content/uploads/' . $image_path;
  996. $this->assertSame( $attachment_id, attachment_url_to_postid( $image_url ) );
  997. remove_filter( 'upload_dir', array( $this, '_upload_dir' ) );
  998. }
  999. function _upload_dir( $dir ) {
  1000. $dir['baseurl'] = 'http://192.168.1.20.com/wp-content/uploads';
  1001. return $dir;
  1002. }
  1003. /**
  1004. * @ticket 31044
  1005. */
  1006. function test_attachment_url_to_postid_with_empty_url() {
  1007. $post_id = attachment_url_to_postid( '' );
  1008. $this->assertInternalType( 'int', $post_id );
  1009. $this->assertSame( 0, $post_id );
  1010. }
  1011. /**
  1012. * @ticket 22768
  1013. */
  1014. public function test_media_handle_upload_sets_post_excerpt() {
  1015. $iptc_file = DIR_TESTDATA . '/images/test-image-iptc.jpg';
  1016. // Make a copy of this file as it gets moved during the file upload.
  1017. $tmp_name = wp_tempnam( $iptc_file );
  1018. copy( $iptc_file, $tmp_name );
  1019. $_FILES['upload'] = array(
  1020. 'tmp_name' => $tmp_name,
  1021. 'name' => 'test-image-iptc.jpg',
  1022. 'type' => 'image/jpeg',
  1023. 'error' => 0,
  1024. 'size' => filesize( $iptc_file ),
  1025. );
  1026. $post_id = media_handle_upload(
  1027. 'upload',
  1028. 0,
  1029. array(),
  1030. array(
  1031. 'action' => 'test_iptc_upload',
  1032. 'test_form' => false,
  1033. )
  1034. );
  1035. unset( $_FILES['upload'] );
  1036. $post = get_post( $post_id );
  1037. // Clean up.
  1038. wp_delete_attachment( $post_id );
  1039. $this->assertSame( 'This is a comment. / Это комментарий. / Βλέπετε ένα σχόλιο.', $post->post_excerpt );
  1040. }
  1041. /**
  1042. * @ticket 37989
  1043. */
  1044. public function test_media_handle_upload_expected_titles() {
  1045. $test_file = DIR_TESTDATA . '/images/test-image.jpg';
  1046. // Make a copy of this file as it gets moved during the file upload.
  1047. $tmp_name = wp_tempnam( $test_file );
  1048. copy( $test_file, $tmp_name );
  1049. $_FILES['upload'] = array(
  1050. 'tmp_name' => $tmp_name,
  1051. 'name' => 'This is a test.jpg',
  1052. 'type' => 'image/jpeg',
  1053. 'error' => 0,
  1054. 'size' => filesize( $test_file ),
  1055. );
  1056. $post_id = media_handle_upload(
  1057. 'upload',
  1058. 0,
  1059. array(),
  1060. array(
  1061. 'action' => 'test_upload_titles',
  1062. 'test_form' => false,
  1063. )
  1064. );
  1065. unset( $_FILES['upload'] );
  1066. $post = get_post( $post_id );
  1067. // Clean up.
  1068. wp_delete_attachment( $post_id );
  1069. $this->assertSame( 'This is a test', $post->post_title );
  1070. }
  1071. /**
  1072. * @ticket 33016
  1073. */
  1074. function test_multiline_cdata() {
  1075. global $wp_embed;
  1076. $content = <<<EOF
  1077. <script>// <![CDATA[
  1078. _my_function('data');
  1079. // ]]>
  1080. </script>
  1081. EOF;
  1082. $result = $wp_embed->autoembed( $content );
  1083. $this->assertSame( $content, $result );
  1084. }
  1085. /**
  1086. * @ticket 33016
  1087. */
  1088. function test_multiline_comment() {
  1089. global $wp_embed;
  1090. $content = <<<EOF
  1091. <script><!--
  1092. my_function();
  1093. // --> </script>
  1094. EOF;
  1095. $result = $wp_embed->autoembed( $content );
  1096. $this->assertSame( $content, $result );
  1097. }
  1098. /**
  1099. * @ticket 33016
  1100. *
  1101. * @group external-http
  1102. */
  1103. function test_multiline_comment_with_embeds() {
  1104. $content = <<<EOF
  1105. Start.
  1106. [embed]http://www.youtube.com/embed/TEST01YRHA0[/embed]
  1107. <script><!--
  1108. my_function();
  1109. // --> </script>
  1110. http://www.youtube.com/embed/TEST02YRHA0
  1111. [embed]http://www.example.com/embed/TEST03YRHA0[/embed]
  1112. http://www.example.com/embed/TEST04YRHA0
  1113. Stop.
  1114. EOF;
  1115. $expected = <<<EOF
  1116. <p>Start.<br />
  1117. https://youtube.com/watch?v=TEST01YRHA0<br />
  1118. <script><!--
  1119. my_function();
  1120. // --> </script><br />
  1121. https://youtube.com/watch?v=TEST02YRHA0<br />
  1122. <a href="http://www.example.com/embed/TEST03YRHA0">http://www.example.com/embed/TEST03YRHA0</a><br />
  1123. http://www.example.com/embed/TEST04YRHA0<br />
  1124. Stop.</p>
  1125. EOF;
  1126. $result = apply_filters( 'the_content', $content );
  1127. $this->assertSameIgnoreEOL( $expected, $result );
  1128. }
  1129. /**
  1130. * @ticket 33016
  1131. */
  1132. function filter_wp_embed_shortcode_custom( $content, $url ) {
  1133. if ( 'https://www.example.com/?video=1' === $url ) {
  1134. $content = '@embed URL was replaced@';
  1135. }
  1136. return $content;
  1137. }
  1138. /**
  1139. * @ticket 33016
  1140. *
  1141. * @group external-http
  1142. */
  1143. function test_oembed_explicit_media_link() {
  1144. global $wp_embed;
  1145. add_filter( 'embed_maybe_make_link', array( $this, 'filter_wp_embed_shortcode_custom' ), 10, 2 );
  1146. $content = <<<EOF
  1147. https://www.example.com/?video=1
  1148. EOF;
  1149. $expected = <<<EOF
  1150. @embed URL was replaced@
  1151. EOF;
  1152. $result = $wp_embed->autoembed( $content );
  1153. $this->assertSame( $expected, $result );
  1154. $content = <<<EOF
  1155. <a href="https://www.example.com/?video=1">https://www.example.com/?video=1</a>
  1156. <script>// <![CDATA[
  1157. _my_function('data');
  1158. myvar = 'Hello world
  1159. https://www.example.com/?video=1
  1160. do not break this';
  1161. // ]]>
  1162. </script>
  1163. EOF;
  1164. $result = $wp_embed->autoembed( $content );
  1165. $this->assertSame( $content, $result );
  1166. remove_filter( 'embed_maybe_make_link', array( $this, 'filter_wp_embed_shortcode_custom' ), 10 );
  1167. }
  1168. /**
  1169. * Tests the default output of `wp_get_attachment_image()`.
  1170. *
  1171. * @ticket 34635
  1172. */
  1173. function test_wp_get_attachment_image_defaults() {
  1174. $image = image_downsize( self::$large_id, 'thumbnail' );
  1175. $expected = sprintf(
  1176. '<img width="%1$d" height="%2$d" src="%3$s" class="attachment-thumbnail size-thumbnail" alt="" loading="lazy" />',
  1177. $image[1],
  1178. $image[2],
  1179. $image[0]
  1180. );
  1181. $this->assertSame( $expected, wp_get_attachment_image( self::$large_id ) );
  1182. }
  1183. /**
  1184. * @ticket 50801
  1185. */
  1186. function test_wp_get_attachment_image_filter_output() {
  1187. $image = image_downsize( self::$large_id, 'thumbnail' );
  1188. $expected = 'Override wp_get_attachment_image';
  1189. add_filter( 'wp_get_attachment_image', array( $this, 'filter_wp_get_attachment_image' ) );
  1190. $output = wp_get_attachment_image( self::$large_id );
  1191. remove_filter( 'wp_get_attachment_image', array( $this, 'filter_wp_get_attachment_image' ) );
  1192. $this->assertSame( $expected, $output );
  1193. }
  1194. function filter_wp_get_attachment_image() {
  1195. return 'Override wp_get_attachment_image';
  1196. }
  1197. /**
  1198. * Test that `wp_get_attachment_image()` returns a proper alt value.
  1199. *
  1200. * @ticket 34635
  1201. */
  1202. function test_wp_get_attachment_image_with_alt() {
  1203. // Add test alt metadata.
  1204. update_post_meta( self::$large_id, '_wp_attachment_image_alt', 'Some very clever alt text', true );
  1205. $image = image_downsize( self::$large_id, 'thumbnail' );
  1206. $expected = sprintf(
  1207. '<img width="%1$d" height="%2$d" src="%3$s" class="attachment-thumbnail size-thumbnail" alt="Some very clever alt text" loading="lazy" />',
  1208. $image[1],
  1209. $image[2],
  1210. $image[0]
  1211. );
  1212. $this->assertSame( $expected, wp_get_attachment_image( self::$large_id ) );
  1213. // Cleanup.
  1214. update_post_meta( self::$large_id, '_wp_attachment_image_alt', '', true );
  1215. }
  1216. /**
  1217. * @ticket 33878
  1218. */
  1219. function test_wp_get_attachment_image_url() {
  1220. $this->assertFalse( wp_get_attachment_image_url( 0 ) );
  1221. $post_id = self::factory()->post->create();
  1222. $attachment_id = self::factory()->attachment->create_object(
  1223. $this->img_name,
  1224. $post_id,
  1225. array(
  1226. 'post_mime_type' => 'image/jpeg',
  1227. 'post_type' => 'attachment',
  1228. )
  1229. );
  1230. $image = wp_get_attachment_image_src( $attachment_id, 'thumbnail', false );
  1231. $this->assertSame( $image[0], wp_get_attachment_image_url( $attachment_id ) );
  1232. }
  1233. /**
  1234. * @ticket 12235
  1235. */
  1236. function test_wp_get_attachment_caption() {
  1237. $this->assertFalse( wp_get_attachment_caption( 0 ) );
  1238. $caption = 'This is a caption.';
  1239. $post_id = self::factory()->post->create();
  1240. $attachment_id = self::factory()->attachment->create_object(
  1241. $this->img_name,
  1242. $post_id,
  1243. array(
  1244. 'post_mime_type' => 'image/jpeg',
  1245. 'post_type' => 'attachment',
  1246. 'post_excerpt' => $caption,
  1247. )
  1248. );
  1249. $this->assertFalse( wp_get_attachment_caption( $post_id ) );
  1250. $this->assertSame( $caption, wp_get_attachment_caption( $attachment_id ) );
  1251. }
  1252. /**
  1253. * @ticket 12235
  1254. */
  1255. function test_wp_get_attachment_caption_empty() {
  1256. $post_id = self::factory()->post->create();
  1257. $attachment_id = self::factory()->attachment->create_object(
  1258. $this->img_name,
  1259. $post_id,
  1260. array(
  1261. 'post_mime_type' => 'image/jpeg',
  1262. 'post_type' => 'attachment',
  1263. 'post_excerpt' => '',
  1264. )
  1265. );
  1266. $this->assertSame( '', wp_get_attachment_caption( $attachment_id ) );
  1267. }
  1268. /**
  1269. * Helper function to get image size array from size "name".
  1270. */
  1271. function _get_image_size_array_from_meta( $image_meta, $size_name ) {
  1272. $array = false;
  1273. if ( is_array( $image_meta ) ) {
  1274. if ( 'full' === $size_name && isset( $image_meta['width'] ) && isset( $image_meta['height'] ) ) {
  1275. $array = array( $image_meta['width'], $image_meta['height'] );
  1276. } elseif ( isset( $image_meta['sizes'][ $size_name ]['width'] ) && isset( $image_meta['sizes'][ $size_name ]['height'] ) ) {
  1277. $array = array( $image_meta['sizes'][ $size_name ]['width'], $image_meta['sizes'][ $size_name ]['height'] );
  1278. }
  1279. }
  1280. if ( ! $array ) {
  1281. $this->fail( sprintf( "Could not retrieve image metadata for size '%s'.", $size_name ) );
  1282. }
  1283. return $array;
  1284. }
  1285. /**
  1286. * Helper function to move the src image to the first position in the expected srcset string.
  1287. */
  1288. function _src_first( $srcset, $src_url, $src_width ) {
  1289. $src_string = $src_url . ' ' . $src_width . 'w';
  1290. $src_not_first = ', ' . $src_string;
  1291. if ( strpos( $srcset, $src_not_first ) ) {
  1292. $srcset = str_replace( $src_not_first, '', $srcset );
  1293. $srcset = $src_string . ', ' . $srcset;
  1294. }
  1295. return $srcset;
  1296. }
  1297. /**
  1298. * @ticket 33641
  1299. * @requires function imagejpeg
  1300. */
  1301. function test_wp_calculate_image_srcset() {
  1302. $_wp_additional_image_sizes = wp_get_additional_image_sizes();
  1303. $year_month = gmdate( 'Y/m' );
  1304. $image_meta = wp_get_attachment_metadata( self::$large_id );
  1305. $uploads_dir_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/';
  1306. // Set up test cases for all expected size names.
  1307. $intermediates = array( 'medium', 'medium_large', 'large', 'full' );
  1308. // Add any soft crop intermediate sizes.
  1309. foreach ( $_wp_additional_image_sizes as $name => $additional_size ) {
  1310. if ( ! $_wp_additional_image_sizes[ $name ]['crop'] || 0 === $_wp_additional_image_sizes[ $name ]['height'] ) {
  1311. $intermediates[] = $name;
  1312. }
  1313. }
  1314. $expected = '';
  1315. foreach ( $image_meta['sizes'] as $name => $size ) {
  1316. // Allow the sizes that should be included so we pick up 'medium_large' in 4.4.
  1317. if ( in_array( $name, $intermediates, true ) ) {
  1318. $expected .= $uploads_dir_url . $year_month . '/' . $size['file'] . ' ' . $size['width'] . 'w, ';
  1319. }
  1320. }
  1321. $expected = trim( $expected, ' ,' );
  1322. foreach ( $intermediates as $int ) {
  1323. $image_url = wp_get_attachment_image_url( self::$large_id, $int );
  1324. $size_array = $this->_get_image_size_array_from_meta( $image_meta, $int );
  1325. if ( 'full' === $int ) {
  1326. // Add the full size image. Expected to be in the srcset when the full size image is used as src.
  1327. $_expected = $uploads_dir_url . $image_meta['file'] . ' ' . $image_meta['width'] . 'w, ' . $expected;
  1328. } else {
  1329. $_expected = $expected;
  1330. }
  1331. $expected_srcset = $this->_src_first( $_expected, $image_url, $size_array[0] );
  1332. $this->assertSame( $expected_srcset, wp_calculate_image_srcset( $size_array, $image_url, $image_meta ) );
  1333. }
  1334. }
  1335. /**
  1336. * @ticket 33641
  1337. * @requires function imagejpeg
  1338. */
  1339. function test_wp_calculate_image_srcset_no_date_uploads() {
  1340. $_wp_additional_image_sizes = wp_get_additional_image_sizes();
  1341. // Disable date organized uploads.
  1342. add_filter( 'upload_dir', '_upload_dir_no_subdir' );
  1343. // Make an image.
  1344. $filename = DIR_TESTDATA . '/images/' . self::$large_filename;
  1345. $id = self::factory()->attachment->create_upload_object( $filename );
  1346. $image_meta = wp_get_attachment_metadata( $id );
  1347. $uploads_dir_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/';
  1348. // Set up test cases for all expected size names.
  1349. $intermediates = array( 'medium', 'medium_large', 'large', 'full' );
  1350. foreach ( $_wp_additional_image_sizes as $name => $additional_size ) {
  1351. if ( ! $_wp_additional_image_sizes[ $name ]['crop'] || 0 === $_wp_additional_image_sizes[ $name ]['height'] ) {
  1352. $intermediates[] = $name;
  1353. }
  1354. }
  1355. $expected = '';
  1356. foreach ( $image_meta['sizes'] as $name => $size ) {
  1357. // Allow the sizes that should be included so we pick up 'medium_large' in 4.4.
  1358. if ( in_array( $name, $intermediates, true ) ) {
  1359. $expected .= $uploads_dir_url . $size['file'] . ' ' . $size['width'] . 'w, ';
  1360. }
  1361. }
  1362. $expected = trim( $expected, ' ,' );
  1363. foreach ( $intermediates as $int ) {
  1364. $size_array = $this->_get_image_size_array_from_meta( $image_meta, $int );
  1365. $image_url = wp_get_attachment_image_url( $id, $int );
  1366. if ( 'full' === $int ) {
  1367. // Add the full size image. Expected to be in the srcset when the full size image is used as src.
  1368. $_expected = $uploads_dir_url . $image_meta['file'] . ' ' . $image_meta['width'] . 'w, ' . $expected;
  1369. } else {
  1370. $_expected = $expected;
  1371. }
  1372. $expected_srcset = $this->_src_first( $_expected, $image_url, $size_array[0] );
  1373. $this->assertSame( $expected_srcset, wp_calculate_image_srcset( $size_array, $image_url, $image_meta ) );
  1374. }
  1375. // Remove the attachment.
  1376. wp_delete_attachment( $id );
  1377. remove_filter( 'upload_dir', '_upload_dir_no_subdir' );
  1378. }
  1379. /**
  1380. * @ticket 33641
  1381. * @requires function imagejpeg
  1382. */
  1383. function test_wp_calculate_image_srcset_with_edits() {
  1384. // For this test we're going to mock metadata changes from an edit.
  1385. // Start by getting the attachment metadata.
  1386. $image_meta = wp_get_attachment_metadata( self::$large_id );
  1387. $image_url = wp_get_attachment_image_url( self::$large_id, 'medium' );
  1388. $size_array = $this->_get_image_size_array_from_meta( $image_meta, 'medium' );
  1389. // Copy hash generation method used in wp_save_image().
  1390. $hash = 'e' . time() . rand( 100, 999 );
  1391. $filename_base = wp_basename( self::$large_filename, '.jpg' );
  1392. $filename_hash = "{$filename_base}-{$hash}";
  1393. // Add the hash to the image URL.
  1394. $image_url = str_replace( $filename_base, $filename_hash, $image_url );
  1395. // Replace file paths for full and medium sizes with hashed versions.
  1396. $image_meta['sizes']['medium']['file'] = str_replace( $filename_base, $filename_hash, $image_meta['sizes']['medium']['file'] );
  1397. $image_meta['sizes']['medium_large']['file'] = str_replace( $filename_base, $filename_hash, $image_meta['sizes']['medium_large']['file'] );
  1398. $image_meta['sizes']['large']['file'] = str_replace( $filename_base, $filename_hash, $image_meta['sizes']['large']['file'] );
  1399. // Calculate a srcset array.
  1400. $sizes = explode( ', ', wp_calculate_image_srcset( $size_array, $image_url, $image_meta ) );
  1401. // Test to confirm all sources in the array include the same edit hash.
  1402. foreach ( $sizes as $size ) {
  1403. $this->assertNotFalse( strpos( $size, $hash ) );
  1404. }
  1405. }
  1406. /**
  1407. * @ticket 35106
  1408. * @requires function imagejpeg
  1409. */
  1410. function test_wp_calculate_image_srcset_with_absolute_path_in_meta() {
  1411. $_wp_additional_image_sizes = wp_get_additional_image_sizes();
  1412. $year_month = gmdate( 'Y/m' );
  1413. $image_meta = wp_get_attachment_metadata( self::$large_id );
  1414. $uploads_dir_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/';
  1415. // Set up test cases for all expected size names.
  1416. $intermediates = array( 'medium', 'medium_large', 'large', 'full' );
  1417. // Add any soft crop intermediate sizes.
  1418. foreach ( $_wp_additional_image_sizes as $name => $additional_size ) {
  1419. if ( ! $_wp_additional_image_sizes[ $name ]['crop'] || 0 === $_wp_additional_image_sizes[ $name ]['height'] ) {
  1420. $intermediates[] = $name;
  1421. }
  1422. }
  1423. $expected = '';
  1424. foreach ( $image_meta['sizes'] as $name => $size ) {
  1425. // Allow the sizes that should be included so we pick up 'medium_large' in 4.4.
  1426. if ( in_array( $name, $intermediates, true ) ) {
  1427. $expected .= $uploads_dir_url . $year_month . '/' . $size['file'] . ' ' . $size['width'] . 'w, ';
  1428. }
  1429. }
  1430. $expected = trim( $expected, ' ,' );
  1431. $full_size_file = $image_meta['file'];
  1432. // Prepend an absolute path to simulate a pre-2.7 upload.
  1433. $image_meta['file'] = 'H:\home\wordpress\trunk/wp-content/uploads/' . $image_meta['file'];
  1434. foreach ( $intermediates as $int ) {
  1435. $image_url = wp_get_attachment_image_url( self::$large_id, $int );
  1436. $size_array = $this->_get_image_size_array_from_meta( $image_meta, $int );
  1437. if ( 'full' === $int ) {
  1438. // Add the full size image. Expected to be in the srcset when the full size image is used as src.
  1439. $_expected = $uploads_dir_url . $full_size_file . ' ' . $image_meta['width'] . 'w, ' . $expected;
  1440. } else {
  1441. $_expected = $expected;
  1442. }
  1443. $expected_srcset = $this->_src_first( $_expected, $image_url, $size_array[0] );
  1444. $this->assertSame( $expected_srcset, wp_calculate_image_srcset( $size_array, $image_url, $image_meta ) );
  1445. }
  1446. }
  1447. /**
  1448. * @ticket 33641
  1449. */
  1450. function test_wp_calculate_image_srcset_false() {
  1451. $sizes = wp_calculate_image_srcset( array( 400, 300 ), 'file.png', array() );
  1452. // For canola.jpg we should return.
  1453. $this->assertFalse( $sizes );
  1454. }
  1455. /**
  1456. * @ticket 33641
  1457. */
  1458. function test_wp_calculate_image_srcset_no_width() {
  1459. $file = get_attached_file( self::$large_id );
  1460. $image_url = wp_get_attachment_image_url( self::$large_id, 'medium' );
  1461. $image_meta = wp_generate_attachment_metadata( self::$large_id, $file );
  1462. $size_array = array( 0, 0 );
  1463. $srcset = wp_calculate_image_srcset( $size_array, $image_url, $image_meta );
  1464. // The srcset should be false.
  1465. $this->assertFalse( $srcset );
  1466. }
  1467. /**
  1468. * @ticket 34955
  1469. * @ticket 33641
  1470. */
  1471. function test_wp_calculate_image_srcset_ratio_variance() {
  1472. // Mock data for this test.
  1473. $size_array = array( 218, 300 );
  1474. $image_src = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-768x1055-218x300.png';
  1475. $image_meta = array(
  1476. 'width' => 768,
  1477. 'height' => 1055,
  1478. 'file' => '2015/12/test-768x1055.png',
  1479. 'sizes' => array(
  1480. 'thumbnail' => array(
  1481. 'file' => 'test-768x1055-150x150.png',
  1482. 'width' => 150,
  1483. 'height' => 150,
  1484. 'mime-type' => 'image/png',
  1485. ),
  1486. 'medium' => array(
  1487. 'file' => 'test-768x1055-218x300.png',
  1488. 'width' => 218,
  1489. 'height' => 300,
  1490. 'mime-type' => 'image/png',
  1491. ),
  1492. 'custom-600' => array(
  1493. 'file' => 'test-768x1055-600x824.png',
  1494. 'width' => 600,
  1495. 'height' => 824,
  1496. 'mime-type' => 'image/png',
  1497. ),
  1498. 'post-thumbnail' => array(
  1499. 'file' => 'test-768x1055-768x510.png',
  1500. 'width' => 768,
  1501. 'height' => 510,
  1502. 'mime-type' => 'image/png',
  1503. ),
  1504. ),
  1505. );
  1506. $uploads_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/';
  1507. $expected_srcset = $uploads_url . 'test-768x1055-218x300.png 218w, ' .
  1508. $uploads_url . 'test-768x1055-600x824.png 600w, ' .
  1509. $uploads_url . 'test-768x1055.png 768w';
  1510. $this->assertSame( $expected_srcset, wp_calculate_image_srcset( $size_array, $image_src, $image_meta ) );
  1511. }
  1512. /**
  1513. * @ticket 35108
  1514. * @ticket 33641
  1515. */
  1516. function test_wp_calculate_image_srcset_include_src() {
  1517. // Mock data for this test.
  1518. $size_array = array( 2000, 1000 );
  1519. $image_src = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test.png';
  1520. $image_meta = array(
  1521. 'width' => 2000,
  1522. 'height' => 1000,
  1523. 'file' => '2015/12/test.png',
  1524. 'sizes' => array(
  1525. 'thumbnail' => array(
  1526. 'file' => 'test-150x150.png',
  1527. 'width' => 150,
  1528. 'height' => 150,
  1529. 'mime-type' => 'image/png',
  1530. ),
  1531. 'medium' => array(
  1532. 'file' => 'test-300x150.png',
  1533. 'width' => 300,
  1534. 'height' => 150,
  1535. 'mime-type' => 'image/png',
  1536. ),
  1537. 'medium_large' => array(
  1538. 'file' => 'test-768x384.png',
  1539. 'width' => 768,
  1540. 'height' => 384,
  1541. 'mime-type' => 'image/png',
  1542. ),
  1543. 'large' => array(
  1544. 'file' => 'test-1024x512.png',
  1545. 'width' => 1024,
  1546. 'height' => 512,
  1547. 'mime-type' => 'image/png',
  1548. ),
  1549. ),
  1550. );
  1551. $uploads_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/';
  1552. $expected_srcset = $uploads_url . 'test.png 2000w, ' .
  1553. $uploads_url . 'test-300x150.png 300w, ' .
  1554. $uploads_url . 'test-768x384.png 768w, ' .
  1555. $uploads_url . 'test-1024x512.png 1024w';
  1556. $this->assertSame( $expected_srcset, wp_calculate_image_srcset( $size_array, $image_src, $image_meta ) );
  1557. }
  1558. /**
  1559. * @ticket 35480
  1560. */
  1561. function test_wp_calculate_image_srcset_corrupted_image_meta() {
  1562. $size_array = array( 300, 150 );
  1563. $image_src = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-300x150.png';
  1564. $image_meta = array(
  1565. 'width' => 1600,
  1566. 'height' => 800,
  1567. 'file' => '2015/12/test.png',
  1568. 'sizes' => array(
  1569. 'thumbnail' => array(
  1570. 'file' => 'test-150x150.png',
  1571. 'width' => 150,
  1572. 'height' => 150,
  1573. 'mime-type' => 'image/png',
  1574. ),
  1575. 'medium' => array(
  1576. 'file' => 'test-300x150.png',
  1577. 'width' => 300,
  1578. 'height' => 150,
  1579. 'mime-type' => 'image/png',
  1580. ),
  1581. 'medium_large' => array(
  1582. 'file' => 'test-768x384.png',
  1583. 'width' => 768,
  1584. 'height' => 384,
  1585. 'mime-type' => 'image/png',
  1586. ),
  1587. 'large' => array(
  1588. 'file' => 'test-1024x512.png',
  1589. 'width' => 1024,
  1590. 'height' => 512,
  1591. 'mime-type' => 'image/png',
  1592. ),
  1593. ),
  1594. );
  1595. $srcset = array(
  1596. 300 => 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-300x150.png 300w',
  1597. 768 => 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-768x384.png 768w',
  1598. 1024 => 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-1024x512.png 1024w',
  1599. 1600 => 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test.png 1600w',
  1600. );
  1601. // No sizes array.
  1602. $image_meta1 = $image_meta;
  1603. unset( $image_meta1['sizes'] );
  1604. $this->assertFalse( wp_calculate_image_srcset( $size_array, $image_src, $image_meta1 ) );
  1605. // Sizes is string instead of array; only full size available means no srcset.
  1606. $image_meta2 = $image_meta;
  1607. $image_meta2['sizes'] = '';
  1608. $this->assertFalse( wp_calculate_image_srcset( $size_array, $image_src, $image_meta2 ) );
  1609. // File name is incorrect.
  1610. $image_meta3 = $image_meta;
  1611. $image_meta3['file'] = '/';
  1612. $this->assertFalse( wp_calculate_image_srcset( $size_array, $image_src, $image_meta3 ) );
  1613. // File name is incorrect.
  1614. $image_meta4 = $image_meta;
  1615. unset( $image_meta4['file'] );
  1616. $this->assertFalse( wp_calculate_image_srcset( $size_array, $image_src, $image_meta4 ) );
  1617. // Intermediate size is string instead of array.
  1618. $image_meta5 = $image_meta;
  1619. $image_meta5['sizes']['medium_large'] = '';
  1620. unset( $srcset[768] );
  1621. $expected_srcset = implode( ', ', $srcset );
  1622. $this->assertSame( $expected_srcset, wp_calculate_image_srcset( $size_array, $image_src, $image_meta5 ) );
  1623. }
  1624. /**
  1625. * @ticket 36549
  1626. * @ticket 33641
  1627. */
  1628. function test_wp_calculate_image_srcset_with_spaces_in_filenames() {
  1629. // Mock data for this test.
  1630. $image_src = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test image-300x150.png';
  1631. $image_meta = array(
  1632. 'width' => 3000,
  1633. 'height' => 1500,
  1634. 'file' => '2015/12/test image.png',
  1635. 'sizes' => array(
  1636. 'thumbnail' => array(
  1637. 'file' => 'test image-150x150.png',
  1638. 'width' => 150,
  1639. 'height' => 150,
  1640. 'mime-type' => 'image/png',
  1641. ),
  1642. 'medium' => array(
  1643. 'file' => 'test image-300x150.png',
  1644. 'width' => 300,
  1645. 'height' => 150,
  1646. 'mime-type' => 'image/png',
  1647. ),
  1648. 'medium_large' => array(
  1649. 'file' => 'test image-768x384.png',
  1650. 'width' => 768,
  1651. 'height' => 384,
  1652. 'mime-type' => 'image/png',
  1653. ),
  1654. 'large' => array(
  1655. 'file' => 'test image-1024x512.png',
  1656. 'width' => 1024,
  1657. 'height' => 512,
  1658. 'mime-type' => 'image/png',
  1659. ),
  1660. ),
  1661. );
  1662. $uploads_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/';
  1663. $expected_srcset = $uploads_url . 'test%20image-300x150.png 300w, ' .
  1664. $uploads_url . 'test%20image-768x384.png 768w, ' .
  1665. $uploads_url . 'test%20image-1024x512.png 1024w';
  1666. $this->assertSame( $expected_srcset, wp_calculate_image_srcset( array( 300, 150 ), $image_src, $image_meta ) );
  1667. }
  1668. /**
  1669. * @ticket 33641
  1670. * @requires function imagejpeg
  1671. */
  1672. function test_wp_get_attachment_image_srcset() {
  1673. $_wp_additional_image_sizes = wp_get_additional_image_sizes();
  1674. $image_meta = wp_get_attachment_metadata( self::$large_id );
  1675. $size_array = array( $image_meta['width'], $image_meta['height'] ); // Full size.
  1676. $srcset = wp_get_attachment_image_srcset( self::$large_id, $size_array, $image_meta );
  1677. $year_month = gmdate( 'Y/m' );
  1678. $uploads_dir = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/';
  1679. // Set up test cases for all expected size names.
  1680. $intermediates = array( 'medium', 'medium_large', 'large', 'full' );
  1681. foreach ( $_wp_additional_image_sizes as $name => $additional_size ) {
  1682. if ( ! $_wp_additional_image_sizes[ $name ]['crop'] || 0 === $_wp_additional_image_sizes[ $name ]['height'] ) {
  1683. $intermediates[] = $name;
  1684. }
  1685. }
  1686. $expected = '';
  1687. foreach ( $image_meta['sizes'] as $name => $size ) {
  1688. // Allow the sizes that should be included so we pick up 'medium_large' in 4.4.
  1689. if ( in_array( $name, $intermediates, true ) ) {
  1690. $expected .= $uploads_dir . $year_month . '/' . $size['file'] . ' ' . $size['width'] . 'w, ';
  1691. }
  1692. }
  1693. $expected .= $uploads_dir . $image_meta['file'] . ' ' . $image_meta['width'] . 'w';
  1694. $expected_srcset = $this->_src_first( $expected, $uploads_dir . $image_meta['file'], $size_array[0] );
  1695. $this->assertSame( $expected_srcset, $srcset );
  1696. }
  1697. /**
  1698. * @ticket 33641
  1699. */
  1700. function test_wp_get_attachment_image_srcset_single_srcset() {
  1701. $image_meta = wp_get_attachment_metadata( self::$large_id );
  1702. $size_array = array( 150, 150 );
  1703. /*
  1704. * In our tests, thumbnails will only return a single srcset candidate,
  1705. * so we shouldn't return a srcset value in order to avoid unneeded markup.
  1706. */
  1707. $sizes = wp_get_attachment_image_srcset( self::$large_id, $size_array, $image_meta );
  1708. $this->assertFalse( $sizes );
  1709. }
  1710. /**
  1711. * @ticket 33641
  1712. */
  1713. function test_wp_get_attachment_image_srcset_invalidsize() {
  1714. $image_meta = wp_get_attachment_metadata( self::$large_id );
  1715. $invalid_size = 'nailthumb';
  1716. $original_size = array( 1600, 1200 );
  1717. $srcset = wp_get_attachment_image_srcset( self::$large_id, $invalid_size, $image_meta );
  1718. // Expect a srcset for the original full size image to be returned.
  1719. $expected = wp_get_attachment_image_srcset( self::$large_id, $original_size, $image_meta );
  1720. $this->assertSame( $expected, $srcset );
  1721. }
  1722. /**
  1723. * @ticket 33641
  1724. */
  1725. function test_wp_get_attachment_image_sizes() {
  1726. // Test sizes against the default WP sizes.
  1727. $intermediates = array( 'thumbnail', 'medium', 'medium_large', 'large' );
  1728. // Make sure themes aren't filtering the sizes array.
  1729. remove_all_filters( 'wp_calculate_image_sizes' );
  1730. foreach ( $intermediates as $int_size ) {
  1731. $image = wp_get_attachment_image_src( self::$large_id, $int_size );
  1732. $expected = '(max-width: ' . $image[1] . 'px) 100vw, ' . $image[1] . 'px';
  1733. $sizes = wp_get_attachment_image_sizes( self::$large_id, $int_size );
  1734. $this->assertSame( $expected, $sizes );
  1735. }
  1736. }
  1737. /**
  1738. * @ticket 33641
  1739. * @requires function imagejpeg
  1740. */
  1741. function test_wp_calculate_image_sizes() {
  1742. // Test sizes against the default WP sizes.
  1743. $intermediates = array( 'thumbnail', 'medium', 'medium_large', 'large' );
  1744. $image_meta = wp_get_attachment_metadata( self::$large_id );
  1745. // Make sure themes aren't filtering the sizes array.
  1746. remove_all_filters( 'wp_calculate_image_sizes' );
  1747. foreach ( $intermediates as $int_size ) {
  1748. $size_array = $this->_get_image_size_array_from_meta( $image_meta, $int_size );
  1749. $image_src = $image_meta['sizes'][ $int_size ]['file'];
  1750. list( $width, $height ) = $size_array;
  1751. $expected = '(max-width: ' . $width . 'px) 100vw, ' . $width . 'px';
  1752. $sizes = wp_calculate_image_sizes( $size_array, $image_src, $image_meta );
  1753. $this->assertSame( $expected, $sizes );
  1754. }
  1755. }
  1756. /**
  1757. * @ticket 33641
  1758. * @requires function imagejpeg
  1759. */
  1760. function test_wp_filter_content_tags_srcset_sizes() {
  1761. $image_meta = wp_get_attachment_metadata( self::$large_id );
  1762. $size_array = $this->_get_image_size_array_from_meta( $image_meta, 'medium' );
  1763. $srcset = sprintf( 'srcset="%s"', wp_get_attachment_image_srcset( self::$large_id, $size_array, $image_meta ) );
  1764. $sizes = sprintf( 'sizes="%s"', wp_get_attachment_image_sizes( self::$large_id, $size_array, $image_meta ) );
  1765. // Function used to build HTML for the editor.
  1766. $img = get_image_tag( self::$large_id, '', '', '', 'medium' );
  1767. $img_no_size_in_class = str_replace( 'size-', '', $img );
  1768. $img_no_width_height = str_replace( ' width="' . $size_array[0] . '"', '', $img );
  1769. $img_no_width_height = str_replace( ' height="' . $size_array[1] . '"', '', $img_no_width_height );
  1770. $img_no_size_id = str_replace( 'wp-image-', 'id-', $img );
  1771. $img_with_sizes_attr = str_replace( '<img ', '<img sizes="99vw" ', $img );
  1772. $img_xhtml = str_replace( ' />', '/>', $img );
  1773. $img_html5 = str_replace( ' />', '>', $img );
  1774. // Manually add srcset and sizes to the markup from get_image_tag().
  1775. $respimg = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img );
  1776. $respimg_no_size_in_class = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img_no_size_in_class );
  1777. $respimg_no_width_height = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img_no_width_height );
  1778. $respimg_with_sizes_attr = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . $srcset . ' />', $img_with_sizes_attr );
  1779. $respimg_xhtml = preg_replace( '|<img ([^>]+)/>|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img_xhtml );
  1780. $respimg_html5 = preg_replace( '|<img ([^>]+)>|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img_html5 );
  1781. $content = '
  1782. <p>Image, standard. Should have srcset and sizes.</p>
  1783. %1$s
  1784. <p>Image, no size class. Should have srcset and sizes.</p>
  1785. %2$s
  1786. <p>Image, no width and height attributes. Should have srcset and sizes (from matching the file name).</p>
  1787. %3$s
  1788. <p>Image, no attachment ID class. Should NOT have srcset and sizes.</p>
  1789. %4$s
  1790. <p>Image, with sizes attribute. Should NOT have two sizes attributes.</p>
  1791. %5$s
  1792. <p>Image, XHTML 1.0 style (no space before the closing slash). Should have srcset and sizes.</p>
  1793. %6$s
  1794. <p>Image, HTML 5.0 style. Should have srcset and sizes.</p>
  1795. %7$s';
  1796. $content_unfiltered = sprintf(
  1797. $content,
  1798. $img,
  1799. $img_no_size_in_class,
  1800. $img_no_width_height,
  1801. $img_no_size_id,
  1802. $img_with_sizes_attr,
  1803. $img_xhtml,
  1804. $img_html5
  1805. );
  1806. $content_filtered = sprintf(
  1807. $content,
  1808. $respimg,
  1809. $respimg_no_size_in_class,
  1810. $respimg_no_width_height,
  1811. $img_no_size_id,
  1812. $respimg_with_sizes_attr,
  1813. $respimg_xhtml,
  1814. $respimg_html5
  1815. );
  1816. // Do not add width, height, and loading.
  1817. add_filter( 'wp_img_tag_add_width_and_height_attr', '__return_false' );
  1818. add_filter( 'wp_img_tag_add_loading_attr', '__return_false' );
  1819. $this->assertSame( $content_filtered, wp_filter_content_tags( $content_unfiltered ) );
  1820. remove_filter( 'wp_img_tag_add_width_and_height_attr', '__return_false' );
  1821. remove_filter( 'wp_img_tag_add_loading_attr', '__return_false' );
  1822. }
  1823. /**
  1824. * When rendering attributes for responsive images,
  1825. * we rely on the 'wp-image-*' class to find the image by ID.
  1826. * The class name may not be consistent with attachment IDs in DB when
  1827. * working with imported content or when a user has edited
  1828. * the 'src' attribute manually. To avoid incorrect images
  1829. * being displayed, ensure we don't add attributes in this case.
  1830. *
  1831. * @ticket 34898
  1832. * @ticket 33641
  1833. */
  1834. function test_wp_filter_content_tags_srcset_sizes_wrong() {
  1835. $img = get_image_tag( self::$large_id, '', '', '', 'medium' );
  1836. $img = wp_img_tag_add_loading_attr( $img, 'test' );
  1837. // Replace the src URL.
  1838. $image_wrong_src = preg_replace( '|src="[^"]+"|', 'src="http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/foo.jpg"', $img );
  1839. $this->assertSame( $image_wrong_src, wp_filter_content_tags( $image_wrong_src ) );
  1840. }
  1841. /**
  1842. * @ticket 33641
  1843. */
  1844. function test_wp_filter_content_tags_srcset_sizes_with_preexisting_srcset() {
  1845. // Generate HTML and add a dummy srcset attribute.
  1846. $img = get_image_tag( self::$large_id, '', '', '', 'medium' );
  1847. $img = wp_img_tag_add_loading_attr( $img, 'test' );
  1848. $img = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . 'srcset="image2x.jpg 2x" />', $img );
  1849. // The content filter should return the image unchanged.
  1850. $this->assertSame( $img, wp_filter_content_tags( $img ) );
  1851. }
  1852. /**
  1853. * @ticket 33641
  1854. * @ticket 34528
  1855. */
  1856. function test_wp_calculate_image_srcset_animated_gifs() {
  1857. // Mock meta for an animated gif.
  1858. $image_meta = array(
  1859. 'width' => 1200,
  1860. 'height' => 600,
  1861. 'file' => 'animated.gif',
  1862. 'sizes' => array(
  1863. 'thumbnail' => array(
  1864. 'file' => 'animated-150x150.gif',
  1865. 'width' => 150,
  1866. 'height' => 150,
  1867. 'mime-type' => 'image/gif',
  1868. ),
  1869. 'medium' => array(
  1870. 'file' => 'animated-300x150.gif',
  1871. 'width' => 300,
  1872. 'height' => 150,
  1873. 'mime-type' => 'image/gif',
  1874. ),
  1875. 'large' => array(
  1876. 'file' => 'animated-1024x512.gif',
  1877. 'width' => 1024,
  1878. 'height' => 512,
  1879. 'mime-type' => 'image/gif',
  1880. ),
  1881. ),
  1882. );
  1883. $full_src = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_meta['file'];
  1884. $large_src = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_meta['sizes']['large']['file'];
  1885. // Test with soft resized size array.
  1886. $size_array = array( 900, 450 );
  1887. // Full size GIFs should not return a srcset.
  1888. $this->assertFalse( wp_calculate_image_srcset( $size_array, $full_src, $image_meta ) );
  1889. // Intermediate sized GIFs should not include the full size in the srcset.
  1890. $this->assertFalse( strpos( wp_calculate_image_srcset( $size_array, $large_src, $image_meta ), $full_src ) );
  1891. }
  1892. /**
  1893. * @ticket 35045
  1894. * @ticket 33641
  1895. * @requires function imagejpeg
  1896. */
  1897. function test_wp_filter_content_tags_schemes() {
  1898. $image_meta = wp_get_attachment_metadata( self::$large_id );
  1899. $size_array = $this->_get_image_size_array_from_meta( $image_meta, 'medium' );
  1900. $srcset = sprintf( 'srcset="%s"', wp_get_attachment_image_srcset( self::$large_id, $size_array, $image_meta ) );
  1901. $sizes = sprintf( 'sizes="%s"', wp_get_attachment_image_sizes( self::$large_id, $size_array, $image_meta ) );
  1902. // Build HTML for the editor.
  1903. $img = get_image_tag( self::$large_id, '', '', '', 'medium' );
  1904. $img = wp_img_tag_add_loading_attr( $img, 'test' );
  1905. $img_https = str_replace( 'http://', 'https://', $img );
  1906. $img_relative = str_replace( 'http://', '//', $img );
  1907. // Manually add srcset and sizes to the markup from get_image_tag().
  1908. $respimg = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img );
  1909. $respimg_https = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img_https );
  1910. $respimg_relative = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img_relative );
  1911. $content = '
  1912. <p>Image, http: protocol. Should have srcset and sizes.</p>
  1913. %1$s
  1914. <p>Image, https: protocol. Should have srcset and sizes.</p>
  1915. %2$s
  1916. <p>Image, protocol-relative. Should have srcset and sizes.</p>
  1917. %3$s';
  1918. $unfiltered = sprintf(
  1919. $content,
  1920. $img,
  1921. $img_https,
  1922. $img_relative
  1923. );
  1924. $expected = sprintf(
  1925. $content,
  1926. $respimg,
  1927. $respimg_https,
  1928. $respimg_relative
  1929. );
  1930. $actual = wp_filter_content_tags( $unfiltered );
  1931. $this->assertSame( $expected, $actual );
  1932. }
  1933. /**
  1934. * @ticket 34945
  1935. * @ticket 33641
  1936. */
  1937. function test_wp_get_attachment_image_with_https_on() {
  1938. // Mock meta for the image.
  1939. $image_meta = array(
  1940. 'width' => 1200,
  1941. 'height' => 600,
  1942. 'file' => 'test.jpg',
  1943. 'sizes' => array(
  1944. 'thumbnail' => array(
  1945. 'file' => 'test-150x150.jpg',
  1946. 'width' => 150,
  1947. 'height' => 150,
  1948. ),
  1949. 'medium' => array(
  1950. 'file' => 'test-300x150.jpg',
  1951. 'width' => 300,
  1952. 'height' => 150,
  1953. ),
  1954. 'large' => array(
  1955. 'file' => 'test-1024x512.jpg',
  1956. 'width' => 1024,
  1957. 'height' => 512,
  1958. ),
  1959. ),
  1960. );
  1961. // Test using the large file size.
  1962. $size_array = array( 1024, 512 );
  1963. $image_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_meta['sizes']['large']['file'];
  1964. $_SERVER['HTTPS'] = 'on';
  1965. $uploads_url = 'https://' . WP_TESTS_DOMAIN . '/wp-content/uploads/';
  1966. $expected = $uploads_url . 'test-1024x512.jpg 1024w, ' .
  1967. $uploads_url . 'test-300x150.jpg 300w, ' .
  1968. $uploads_url . 'test.jpg 1200w';
  1969. $actual = wp_calculate_image_srcset( $size_array, $image_url, $image_meta );
  1970. $this->assertSame( $expected, $actual );
  1971. }
  1972. /**
  1973. * @ticket 36084
  1974. */
  1975. function test_get_image_send_to_editor_defaults() {
  1976. $id = self::$large_id;
  1977. $caption = '';
  1978. $title = 'A test title value.';
  1979. $align = 'left';
  1980. // Calculate attachment data (default is medium).
  1981. $attachment = wp_get_attachment_image_src( $id, 'medium' );
  1982. $html = '<img src="%1$s" alt="" width="%2$d" height="%3$d" class="align%4$s size-medium wp-image-%5$d" />';
  1983. $expected = sprintf( $html, $attachment[0], $attachment[1], $attachment[2], $align, $id );
  1984. $this->assertSame( $expected, get_image_send_to_editor( $id, $caption, $title, $align ) );
  1985. $this->assertSame( $expected, get_image_send_to_editor( $id, $caption, $title, $align ) );
  1986. }
  1987. /**
  1988. * @ticket 36084
  1989. */
  1990. function test_get_image_send_to_editor_defaults_with_optional_params() {
  1991. $id = self::$large_id;
  1992. $caption = 'A test caption.';
  1993. $title = 'A test title value.';
  1994. $align = 'left';
  1995. $url = get_permalink( $id );
  1996. $rel = true;
  1997. $size = 'thumbnail';
  1998. $alt = 'An example alt value.';
  1999. // Calculate attachment data.
  2000. $attachment = wp_get_attachment_image_src( $id, $size );
  2001. $html = '<a href="%1$s" rel="%2$s"><img src="%3$s" alt="%4$s" width="%5$d" height="%6$d" class="size-%8$s wp-image-%9$d" /></a>';
  2002. $html = '[caption id="attachment_%9$d" align="align%7$s" width="%5$d"]' . $html . ' %10$s[/caption]';
  2003. $expected = sprintf( $html, $url, 'attachment wp-att-' . $id, $attachment[0], $alt, $attachment[1], $attachment[2], $align, $size, $id, $caption );
  2004. $this->assertSame( $expected, get_image_send_to_editor( $id, $caption, $title, $align, $url, $rel, $size, $alt ) );
  2005. }
  2006. /**
  2007. * @ticket 36084
  2008. */
  2009. function test_get_image_send_to_editor_defaults_no_caption_no_rel() {
  2010. $id = self::$large_id;
  2011. $caption = '';
  2012. $title = 'A test title value.';
  2013. $align = 'left';
  2014. $url = get_permalink( $id );
  2015. $rel = '';
  2016. $size = 'thumbnail';
  2017. $alt = 'An example alt value.';
  2018. // Calculate attachment data.
  2019. $attachment = wp_get_attachment_image_src( $id, $size );
  2020. $html = '<a href="%1$s"><img src="%2$s" alt="%3$s" width="%4$d" height="%5$d" class="align%6$s size-%7$s wp-image-%8$d" /></a>';
  2021. $expected = sprintf( $html, $url, $attachment[0], $alt, $attachment[1], $attachment[2], $align, $size, $id );
  2022. $this->assertSame( $expected, get_image_send_to_editor( $id, $caption, $title, $align, $url, $rel, $size, $alt ) );
  2023. }
  2024. /**
  2025. * Tests if wp_get_attachment_image() uses wp_get_attachment_metadata().
  2026. *
  2027. * In this way, the meta data can be filtered using the filter
  2028. * `wp_get_attachment_metadata`.
  2029. *
  2030. * The test checks if the image size that is added in the filter is
  2031. * used in the output of `wp_get_attachment_image()`.
  2032. *
  2033. * @ticket 36246
  2034. * @requires function imagejpeg
  2035. */
  2036. function test_wp_get_attachment_image_should_use_wp_get_attachment_metadata() {
  2037. add_filter( 'wp_get_attachment_metadata', array( $this, '_filter_36246' ), 10, 2 );
  2038. remove_all_filters( 'wp_calculate_image_sizes' );
  2039. $basename = wp_basename( self::$large_filename, '.jpg' );
  2040. $year_month = gmdate( 'Y/m' );
  2041. $uploads_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $year_month . '/';
  2042. $expected = '<img width="999" height="999" ' .
  2043. 'src="' . $uploads_url . 'test-image-testsize-999x999.jpg" ' .
  2044. 'class="attachment-testsize size-testsize" alt="" loading="lazy" ' .
  2045. 'srcset="' . $uploads_url . 'test-image-testsize-999x999.jpg 999w, ' . $uploads_url . $basename . '-150x150.jpg 150w" ' .
  2046. 'sizes="(max-width: 999px) 100vw, 999px" />';
  2047. $actual = wp_get_attachment_image( self::$large_id, 'testsize' );
  2048. remove_filter( 'wp_get_attachment_metadata', array( $this, '_filter_36246' ) );
  2049. $this->assertSame( $expected, $actual );
  2050. }
  2051. function _filter_36246( $data, $attachment_id ) {
  2052. $data['sizes']['testsize'] = array(
  2053. 'file' => 'test-image-testsize-999x999.jpg',
  2054. 'width' => 999,
  2055. 'height' => 999,
  2056. 'mime-type' => 'image/jpg',
  2057. );
  2058. return $data;
  2059. }
  2060. /**
  2061. * @ticket 50679
  2062. */
  2063. function test_wp_get_attachment_metadata_should_return_false_if_no_attachment() {
  2064. $post_id = self::factory()->post->create();
  2065. $data = wp_get_attachment_metadata( $post_id );
  2066. $this->assertFalse( $data );
  2067. }
  2068. /**
  2069. * @ticket 37813
  2070. */
  2071. public function test_return_type_when_inserting_attachment_with_error_in_data() {
  2072. $data = array(
  2073. 'post_status' => 'publish',
  2074. 'post_content' => 'Attachment content',
  2075. 'post_title' => 'Attachment Title',
  2076. 'post_date' => '2012-02-30 00:00:00',
  2077. );
  2078. $attachment_id = wp_insert_attachment( $data, '', 0, true );
  2079. $this->assertWPError( $attachment_id );
  2080. $this->assertSame( 'invalid_date', $attachment_id->get_error_code() );
  2081. $attachment_id = wp_insert_attachment( $data, '', 0 );
  2082. $this->assertSame( 0, $attachment_id );
  2083. }
  2084. /**
  2085. * @ticket 35218
  2086. */
  2087. function test_wp_get_media_creation_timestamp_video_asf() {
  2088. $metadata = array(
  2089. 'fileformat' => 'asf',
  2090. 'asf' => array(
  2091. 'file_properties_object' => array(
  2092. 'creation_date_unix' => 123,
  2093. ),
  2094. ),
  2095. );
  2096. $this->assertSame( 123, wp_get_media_creation_timestamp( $metadata ) );
  2097. }
  2098. /**
  2099. * @ticket 35218
  2100. */
  2101. function test_wp_get_media_creation_timestamp_video_matroska() {
  2102. $metadata = array(
  2103. 'fileformat' => 'matroska',
  2104. 'matroska' => array(
  2105. 'comments' => array(
  2106. 'creation_time' => array(
  2107. '2015-12-24T17:40:09Z',
  2108. ),
  2109. ),
  2110. ),
  2111. );
  2112. $this->assertSame( 1450978809, wp_get_media_creation_timestamp( $metadata ) );
  2113. }
  2114. /**
  2115. * @ticket 35218
  2116. */
  2117. function test_wp_get_media_creation_timestamp_video_quicktime() {
  2118. $metadata = array(
  2119. 'fileformat' => 'quicktime',
  2120. 'quicktime' => array(
  2121. 'moov' => array(
  2122. 'subatoms' => array(
  2123. array(
  2124. 'creation_time_unix' => 1450978805,
  2125. ),
  2126. ),
  2127. ),
  2128. ),
  2129. );
  2130. $this->assertSame( 1450978805, wp_get_media_creation_timestamp( $metadata ) );
  2131. }
  2132. /**
  2133. * @ticket 35218
  2134. */
  2135. function test_wp_get_media_creation_timestamp_video_webm() {
  2136. $metadata = array(
  2137. 'fileformat' => 'webm',
  2138. 'matroska' => array(
  2139. 'info' => array(
  2140. array(
  2141. 'DateUTC_unix' => 1265680539,
  2142. ),
  2143. ),
  2144. ),
  2145. );
  2146. $this->assertSame( 1265680539, wp_get_media_creation_timestamp( $metadata ) );
  2147. }
  2148. /**
  2149. * Test created timestamp is properly read from an MP4 file.
  2150. *
  2151. * This MP4 video file has an AAC audio track, so it can be used to test
  2152. *`wp_read_audio_metadata()`.
  2153. *
  2154. * @ticket 42017
  2155. */
  2156. function test_wp_read_audio_metadata_adds_creation_date_with_mp4() {
  2157. $video = DIR_TESTDATA . '/uploads/small-video.mp4';
  2158. $metadata = wp_read_audio_metadata( $video );
  2159. $this->assertSame( 1269120551, $metadata['created_timestamp'] );
  2160. }
  2161. /**
  2162. * @ticket 35218
  2163. */
  2164. function test_wp_read_video_metadata_adds_creation_date_with_quicktime() {
  2165. $video = DIR_TESTDATA . '/uploads/small-video.mov';
  2166. $metadata = wp_read_video_metadata( $video );
  2167. $this->assertSame( 1269120551, $metadata['created_timestamp'] );
  2168. }
  2169. /**
  2170. * @ticket 35218
  2171. */
  2172. function test_wp_read_video_metadata_adds_creation_date_with_mp4() {
  2173. $video = DIR_TESTDATA . '/uploads/small-video.mp4';
  2174. $metadata = wp_read_video_metadata( $video );
  2175. $this->assertSame( 1269120551, $metadata['created_timestamp'] );
  2176. }
  2177. /**
  2178. * @ticket 35218
  2179. */
  2180. function test_wp_read_video_metadata_adds_creation_date_with_mkv() {
  2181. $video = DIR_TESTDATA . '/uploads/small-video.mkv';
  2182. $metadata = wp_read_video_metadata( $video );
  2183. $this->assertSame( 1269120551, $metadata['created_timestamp'] );
  2184. }
  2185. /**
  2186. * @ticket 35218
  2187. */
  2188. function test_wp_read_video_metadata_adds_creation_date_with_webm() {
  2189. $video = DIR_TESTDATA . '/uploads/small-video.webm';
  2190. $metadata = wp_read_video_metadata( $video );
  2191. $this->assertSame( 1269120551, $metadata['created_timestamp'] );
  2192. }
  2193. /**
  2194. * @ticket 10752
  2195. */
  2196. public function test_media_handle_upload_uses_post_parent_for_directory_date() {
  2197. $iptc_file = DIR_TESTDATA . '/images/test-image-iptc.jpg';
  2198. // Make a copy of this file as it gets moved during the file upload.
  2199. $tmp_name = wp_tempnam( $iptc_file );
  2200. copy( $iptc_file, $tmp_name );
  2201. $_FILES['upload'] = array(
  2202. 'tmp_name' => $tmp_name,
  2203. 'name' => 'test-image-iptc.jpg',
  2204. 'type' => 'image/jpeg',
  2205. 'error' => 0,
  2206. 'size' => filesize( $iptc_file ),
  2207. );
  2208. $parent_id = self::factory()->post->create( array( 'post_date' => '2010-01-01' ) );
  2209. $post_id = media_handle_upload(
  2210. 'upload',
  2211. $parent_id,
  2212. array(),
  2213. array(
  2214. 'action' => 'test_iptc_upload',
  2215. 'test_form' => false,
  2216. )
  2217. );
  2218. unset( $_FILES['upload'] );
  2219. $url = wp_get_attachment_url( $post_id );
  2220. $uploads_dir = wp_upload_dir( '2010/01' );
  2221. $expected = $uploads_dir['url'] . '/test-image-iptc.jpg';
  2222. // Clean up.
  2223. wp_delete_attachment( $post_id );
  2224. wp_delete_post( $parent_id );
  2225. $this->assertSame( $expected, $url );
  2226. }
  2227. /**
  2228. * @ticket 10752
  2229. */
  2230. public function test_media_handle_upload_ignores_page_parent_for_directory_date() {
  2231. $iptc_file = DIR_TESTDATA . '/images/test-image-iptc.jpg';
  2232. // Make a copy of this file as it gets moved during the file upload.
  2233. $tmp_name = wp_tempnam( $iptc_file );
  2234. copy( $iptc_file, $tmp_name );
  2235. $_FILES['upload'] = array(
  2236. 'tmp_name' => $tmp_name,
  2237. 'name' => 'test-image-iptc.jpg',
  2238. 'type' => 'image/jpeg',
  2239. 'error' => 0,
  2240. 'size' => filesize( $iptc_file ),
  2241. );
  2242. $parent_id = self::factory()->post->create(
  2243. array(
  2244. 'post_date' => '2010-01-01',
  2245. 'post_type' => 'page',
  2246. )
  2247. );
  2248. $parent = get_post( $parent_id );
  2249. $post_id = media_handle_upload(
  2250. 'upload',
  2251. $parent_id,
  2252. array(),
  2253. array(
  2254. 'action' => 'test_iptc_upload',
  2255. 'test_form' => false,
  2256. )
  2257. );
  2258. unset( $_FILES['upload'] );
  2259. $url = wp_get_attachment_url( $post_id );
  2260. $uploads_dir = wp_upload_dir( current_time( 'mysql' ) );
  2261. $expected = $uploads_dir['url'] . '/test-image-iptc.jpg';
  2262. // Clean up.
  2263. wp_delete_attachment( $post_id );
  2264. wp_delete_post( $parent_id );
  2265. $this->assertSame( $expected, $url );
  2266. }
  2267. /**
  2268. * @ticket 50367
  2269. * @requires function imagejpeg
  2270. */
  2271. function test_wp_filter_content_tags_width_height() {
  2272. $image_meta = wp_get_attachment_metadata( self::$large_id );
  2273. $size_array = $this->_get_image_size_array_from_meta( $image_meta, 'medium' );
  2274. $img = get_image_tag( self::$large_id, '', '', '', 'medium' );
  2275. $img_no_width_height = str_replace( ' width="' . $size_array[0] . '"', '', $img );
  2276. $img_no_width_height = str_replace( ' height="' . $size_array[1] . '"', '', $img_no_width_height );
  2277. $img_no_width = str_replace( ' width="' . $size_array[0] . '"', '', $img );
  2278. $img_no_height = str_replace( ' height="' . $size_array[1] . '"', '', $img );
  2279. $hwstring = image_hwstring( $size_array[0], $size_array[1] );
  2280. // Manually add width and height to the markup from get_image_tag().
  2281. $respimg_no_width_height = str_replace( '<img ', '<img ' . $hwstring, $img_no_width_height );
  2282. $content = '
  2283. <p>Image, with width and height. Should NOT be modified.</p>
  2284. %1$s
  2285. <p>Image, no width and height attributes. Should have width, height, srcset and sizes (from matching the file name).</p>
  2286. %2$s
  2287. <p>Image, no width but height attribute. Should NOT be modified.</p>
  2288. %3$s
  2289. <p>Image, no height but width attribute. Should NOT be modified.</p>
  2290. %4$s';
  2291. $content_unfiltered = sprintf( $content, $img, $img_no_width_height, $img_no_width, $img_no_height );
  2292. $content_filtered = sprintf( $content, $img, $respimg_no_width_height, $img_no_width, $img_no_height );
  2293. // Do not add loading, srcset, and sizes.
  2294. add_filter( 'wp_img_tag_add_loading_attr', '__return_false' );
  2295. add_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' );
  2296. $this->assertSame( $content_filtered, wp_filter_content_tags( $content_unfiltered ) );
  2297. remove_filter( 'wp_img_tag_add_loading_attr', '__return_false' );
  2298. remove_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' );
  2299. }
  2300. /**
  2301. * @ticket 44427
  2302. * @ticket 50367
  2303. * @ticket 50756
  2304. * @requires function imagejpeg
  2305. */
  2306. function test_wp_filter_content_tags_loading_lazy() {
  2307. $image_meta = wp_get_attachment_metadata( self::$large_id );
  2308. $size_array = $this->_get_image_size_array_from_meta( $image_meta, 'medium' );
  2309. $img = get_image_tag( self::$large_id, '', '', '', 'medium' );
  2310. $img_xhtml = str_replace( ' />', '/>', $img );
  2311. $img_html5 = str_replace( ' />', '>', $img );
  2312. $img_no_width_height = str_replace( ' width="' . $size_array[0] . '"', '', $img );
  2313. $img_no_width_height = str_replace( ' height="' . $size_array[1] . '"', '', $img_no_width_height );
  2314. $iframe = '<iframe src="https://www.example.com" width="640" height="360"></iframe>';
  2315. $iframe_no_width_height = '<iframe src="https://www.example.com"></iframe>';
  2316. $lazy_img = wp_img_tag_add_loading_attr( $img, 'test' );
  2317. $lazy_img_xhtml = wp_img_tag_add_loading_attr( $img_xhtml, 'test' );
  2318. $lazy_img_html5 = wp_img_tag_add_loading_attr( $img_html5, 'test' );
  2319. $lazy_iframe = wp_iframe_tag_add_loading_attr( $iframe, 'test' );
  2320. // The following should not be modified because there already is a 'loading' attribute.
  2321. $img_eager = str_replace( ' />', ' loading="eager" />', $img );
  2322. $iframe_eager = str_replace( '">', '" loading="eager">', $iframe );
  2323. $content = '
  2324. <p>Image, standard.</p>
  2325. %1$s
  2326. <p>Image, XHTML 1.0 style (no space before the closing slash).</p>
  2327. %2$s
  2328. <p>Image, HTML 5.0 style.</p>
  2329. %3$s
  2330. <p>Image, with pre-existing "loading" attribute. Should not be modified.</p>
  2331. %4$s
  2332. <p>Image, without dimension attributes. Should not be modified.</p>
  2333. %5$s
  2334. <p>Iframe, standard.</p>
  2335. %6$s
  2336. <p>Iframe, with pre-existing "loading" attribute. Should not be modified.</p>
  2337. %7$s
  2338. <p>Iframe, without dimension attributes. Should not be modified.</p>
  2339. %8$s';
  2340. $content_unfiltered = sprintf( $content, $img, $img_xhtml, $img_html5, $img_eager, $img_no_width_height, $iframe, $iframe_eager, $iframe_no_width_height );
  2341. $content_filtered = sprintf( $content, $lazy_img, $lazy_img_xhtml, $lazy_img_html5, $img_eager, $img_no_width_height, $lazy_iframe, $iframe_eager, $iframe_no_width_height );
  2342. // Do not add width, height, srcset, and sizes.
  2343. add_filter( 'wp_img_tag_add_width_and_height_attr', '__return_false' );
  2344. add_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' );
  2345. $this->assertSame( $content_filtered, wp_filter_content_tags( $content_unfiltered ) );
  2346. remove_filter( 'wp_img_tag_add_width_and_height_attr', '__return_false' );
  2347. remove_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' );
  2348. }
  2349. /**
  2350. * @ticket 44427
  2351. * @ticket 50756
  2352. */
  2353. function test_wp_filter_content_tags_loading_lazy_opted_in() {
  2354. $img = get_image_tag( self::$large_id, '', '', '', 'medium' );
  2355. $lazy_img = wp_img_tag_add_loading_attr( $img, 'test' );
  2356. $iframe = '<iframe src="https://www.example.com" width="640" height="360"></iframe>';
  2357. $lazy_iframe = wp_iframe_tag_add_loading_attr( $iframe, 'test' );
  2358. $content = '
  2359. <p>Image, standard.</p>
  2360. %1$s
  2361. <p>Iframe, standard.</p>
  2362. %2$s';
  2363. $content_unfiltered = sprintf( $content, $img, $iframe );
  2364. $content_filtered = sprintf( $content, $lazy_img, $lazy_iframe );
  2365. // Do not add srcset and sizes while testing.
  2366. add_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' );
  2367. // Enable globally for all tags.
  2368. add_filter( 'wp_lazy_loading_enabled', '__return_true' );
  2369. $this->assertSame( $content_filtered, wp_filter_content_tags( $content_unfiltered ) );
  2370. remove_filter( 'wp_lazy_loading_enabled', '__return_true' );
  2371. remove_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' );
  2372. }
  2373. /**
  2374. * @ticket 44427
  2375. * @ticket 50756
  2376. */
  2377. function test_wp_filter_content_tags_loading_lazy_opted_out() {
  2378. $img = get_image_tag( self::$large_id, '', '', '', 'medium' );
  2379. $iframe = '<iframe src="https://www.example.com" width="640" height="360"></iframe>';
  2380. $content = '
  2381. <p>Image, standard.</p>
  2382. %1$s
  2383. <p>Iframe, standard.</p>
  2384. %2$s';
  2385. $content = sprintf( $content, $img, $iframe );
  2386. // Do not add srcset and sizes while testing.
  2387. add_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' );
  2388. // Disable globally for all tags.
  2389. add_filter( 'wp_lazy_loading_enabled', '__return_false' );
  2390. $this->assertSame( $content, wp_filter_content_tags( $content ) );
  2391. remove_filter( 'wp_lazy_loading_enabled', '__return_false' );
  2392. remove_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' );
  2393. }
  2394. /**
  2395. * @ticket 44427
  2396. * @ticket 50367
  2397. */
  2398. function test_wp_img_tag_add_loading_attr() {
  2399. $img = '<img src="example.png" alt=" width="300" height="225" />';
  2400. $img = wp_img_tag_add_loading_attr( $img, 'test' );
  2401. $this->assertContains( ' loading="lazy"', $img );
  2402. }
  2403. /**
  2404. * @ticket 44427
  2405. * @ticket 50367
  2406. */
  2407. function test_wp_img_tag_add_loading_attr_without_src() {
  2408. $img = '<img alt=" width="300" height="225" />';
  2409. $img = wp_img_tag_add_loading_attr( $img, 'test' );
  2410. $this->assertNotContains( ' loading=', $img );
  2411. }
  2412. /**
  2413. * @ticket 44427
  2414. * @ticket 50367
  2415. */
  2416. function test_wp_img_tag_add_loading_attr_with_single_quotes() {
  2417. $img = "<img src='example.png' alt=' width='300' height='225' />";
  2418. $img = wp_img_tag_add_loading_attr( $img, 'test' );
  2419. $this->assertNotContains( ' loading=', $img );
  2420. // Test specifically that the attribute is not there with double-quotes,
  2421. // to avoid regressions.
  2422. $this->assertNotContains( ' loading="lazy"', $img );
  2423. }
  2424. /**
  2425. * @ticket 44427
  2426. * @ticket 50425
  2427. */
  2428. function test_wp_img_tag_add_loading_attr_opt_out() {
  2429. $img = '<img src="example.png" alt=" width="300" height="225" />';
  2430. add_filter( 'wp_img_tag_add_loading_attr', '__return_false' );
  2431. $this->assertNotContains( ' loading=', $img );
  2432. }
  2433. /**
  2434. * @ticket 50756
  2435. */
  2436. function test_wp_iframe_tag_add_loading_attr() {
  2437. $iframe = '<iframe src="https://www.example.com" width="640" height="360"></iframe>';
  2438. $iframe = wp_iframe_tag_add_loading_attr( $iframe, 'test' );
  2439. $this->assertContains( ' loading="lazy"', $iframe );
  2440. }
  2441. /**
  2442. * @ticket 50756
  2443. */
  2444. function test_wp_iframe_tag_add_loading_attr_without_src() {
  2445. $iframe = '<iframe width="640" height="360"></iframe>';
  2446. $iframe = wp_iframe_tag_add_loading_attr( $iframe, 'test' );
  2447. $this->assertNotContains( ' loading=', $iframe );
  2448. }
  2449. /**
  2450. * @ticket 50756
  2451. */
  2452. function test_wp_iframe_tag_add_loading_attr_with_single_quotes() {
  2453. $iframe = "<iframe src='https://www.example.com' width='640' height='360'></iframe>";
  2454. $iframe = wp_iframe_tag_add_loading_attr( $iframe, 'test' );
  2455. $this->assertNotContains( ' loading=', $iframe );
  2456. // Test specifically that the attribute is not there with double-quotes,
  2457. // to avoid regressions.
  2458. $this->assertNotContains( ' loading="lazy"', $iframe );
  2459. }
  2460. /**
  2461. * @ticket 50756
  2462. */
  2463. function test_wp_iframe_tag_add_loading_attr_opt_out() {
  2464. $iframe = '<iframe src="https://www.example.com" width="640" height="360"></iframe>';
  2465. add_filter( 'wp_iframe_tag_add_loading_attr', '__return_false' );
  2466. $this->assertNotContains( ' loading=', $iframe );
  2467. }
  2468. /**
  2469. * @ticket 44427
  2470. * @ticket 50425
  2471. */
  2472. function test_wp_get_attachment_image_loading() {
  2473. $img = wp_get_attachment_image( self::$large_id );
  2474. $this->assertContains( ' loading="lazy"', $img );
  2475. }
  2476. /**
  2477. * @ticket 44427
  2478. * @ticket 50425
  2479. */
  2480. function test_wp_get_attachment_image_loading_opt_out() {
  2481. add_filter( 'wp_lazy_loading_enabled', '__return_false' );
  2482. $img = wp_get_attachment_image( self::$large_id );
  2483. // There should not be any loading attribute in this case.
  2484. $this->assertNotContains( ' loading=', $img );
  2485. }
  2486. /**
  2487. * @ticket 44427
  2488. * @ticket 50425
  2489. */
  2490. function test_wp_get_attachment_image_loading_opt_out_individual() {
  2491. // The default is already tested above, the filter below ensures that
  2492. // lazy-loading is definitely enabled globally for images.
  2493. add_filter( 'wp_lazy_loading_enabled', '__return_true' );
  2494. $img = wp_get_attachment_image( self::$large_id, 'thumbnail', false, array( 'loading' => false ) );
  2495. // There should not be any loading attribute in this case.
  2496. $this->assertNotContains( ' loading=', $img );
  2497. }
  2498. /**
  2499. * @ticket 44427
  2500. * @ticket 50425
  2501. * @ticket 50756
  2502. * @dataProvider data_wp_lazy_loading_enabled_tag_name_defaults
  2503. *
  2504. * @param string $tag_name Tag name.
  2505. * @param bool $expected Expected return value.
  2506. */
  2507. function test_wp_lazy_loading_enabled_tag_name_defaults( $tag_name, $expected ) {
  2508. if ( $expected ) {
  2509. $this->assertTrue( wp_lazy_loading_enabled( $tag_name, 'the_content' ) );
  2510. } else {
  2511. $this->assertFalse( wp_lazy_loading_enabled( $tag_name, 'the_content' ) );
  2512. }
  2513. }
  2514. function data_wp_lazy_loading_enabled_tag_name_defaults() {
  2515. return array(
  2516. 'img => true' => array( 'img', true ),
  2517. 'iframe => true' => array( 'iframe', true ),
  2518. 'arbitrary tag => false' => array( 'blink', false ),
  2519. );
  2520. }
  2521. /**
  2522. * @ticket 50425
  2523. * @dataProvider data_wp_lazy_loading_enabled_context_defaults
  2524. *
  2525. * @param string $context Function context.
  2526. * @param bool $expected Expected return value.
  2527. */
  2528. function test_wp_lazy_loading_enabled_context_defaults( $context, $expected ) {
  2529. if ( $expected ) {
  2530. $this->assertTrue( wp_lazy_loading_enabled( 'img', $context ) );
  2531. } else {
  2532. $this->assertFalse( wp_lazy_loading_enabled( 'img', $context ) );
  2533. }
  2534. }
  2535. function data_wp_lazy_loading_enabled_context_defaults() {
  2536. return array(
  2537. 'wp_get_attachment_image => true' => array( 'wp_get_attachment_image', true ),
  2538. 'the_content => true' => array( 'the_content', true ),
  2539. 'the_excerpt => true' => array( 'the_excerpt', true ),
  2540. 'widget_text_content => true' => array( 'widget_text_content', true ),
  2541. 'get_avatar => true' => array( 'get_avatar', true ),
  2542. 'arbitrary context => true' => array( 'something_completely_arbitrary', true ),
  2543. );
  2544. }
  2545. /**
  2546. * @ticket 50543
  2547. */
  2548. function test_wp_image_file_matches_image_meta() {
  2549. $image_meta = wp_get_attachment_metadata( self::$large_id );
  2550. $image_src_full = wp_get_attachment_image_url( self::$large_id, 'full' );
  2551. $image_src_medium = wp_get_attachment_image_url( self::$large_id, 'medium' );
  2552. $this->assertTrue( wp_image_file_matches_image_meta( $image_src_full, $image_meta ) );
  2553. $this->assertTrue( wp_image_file_matches_image_meta( $image_src_medium, $image_meta ) );
  2554. }
  2555. /**
  2556. * @ticket 50543
  2557. */
  2558. function test_wp_image_file_matches_image_meta_no_subsizes() {
  2559. $image_meta = wp_get_attachment_metadata( self::$large_id );
  2560. $image_src = wp_get_attachment_image_url( self::$large_id, 'full' );
  2561. $image_meta['sizes'] = array();
  2562. $this->assertTrue( wp_image_file_matches_image_meta( $image_src, $image_meta ) );
  2563. }
  2564. /**
  2565. * @ticket 50543
  2566. */
  2567. function test_wp_image_file_matches_image_meta_invalid_meta() {
  2568. $image_meta = ''; // Attachment is not an image.
  2569. $image_src = $this->img_url;
  2570. $this->assertFalse( wp_image_file_matches_image_meta( $image_src, $image_meta ) );
  2571. }
  2572. /**
  2573. * @ticket 50543
  2574. */
  2575. function test_wp_image_file_matches_image_meta_different_meta() {
  2576. $image_meta = wp_get_attachment_metadata( self::$large_id );
  2577. $image_src = $this->img_url; // Different image.
  2578. $this->assertFalse( wp_image_file_matches_image_meta( $image_src, $image_meta ) );
  2579. }
  2580. /**
  2581. * @ticket 50543
  2582. */
  2583. function test_wp_image_file_matches_image_meta_original_image() {
  2584. $image_meta = wp_get_attachment_metadata( self::$large_id );
  2585. $image_src = wp_get_original_image_url( self::$large_id );
  2586. $this->assertTrue( wp_image_file_matches_image_meta( $image_src, $image_meta ) );
  2587. }
  2588. /**
  2589. * @ticket 22101
  2590. */
  2591. function test_gallery_shortcode_when_is_feed_true() {
  2592. $this->go_to( '/?feed=rss2' );
  2593. // Default: Links to image attachment page URL.
  2594. $actual = gallery_shortcode(
  2595. array(
  2596. 'ids' => self::$large_id,
  2597. )
  2598. );
  2599. $this->assertContains( '?attachment_id=', $actual );
  2600. // File: Links to image file URL.
  2601. $actual = gallery_shortcode(
  2602. array(
  2603. 'ids' => self::$large_id,
  2604. 'link' => 'file',
  2605. )
  2606. );
  2607. $this->assertSame( 2, substr_count( $actual, '.jpg' ) );
  2608. // None: Does not link.
  2609. $actual = gallery_shortcode(
  2610. array(
  2611. 'ids' => self::$large_id,
  2612. 'link' => 'none',
  2613. )
  2614. );
  2615. $this->assertNotContains( '<a ', $actual );
  2616. }
  2617. /**
  2618. * Test attachment permalinks based on parent post status.
  2619. *
  2620. * @dataProvider data_attachment_permalinks_based_on_parent_status
  2621. * @ticket 51776
  2622. *
  2623. * @param string $post_key Post as keyed in the shared fixture array.
  2624. * @param string $expected_url Expected permalink.
  2625. * @param bool $expected_404 Whether the page is expected to return a 404 result.
  2626. *
  2627. */
  2628. function test_attachment_permalinks_based_on_parent_status( $post_key, $expected_url, $expected_404 ) {
  2629. $this->set_permalink_structure( '/%postname%' );
  2630. $post = get_post( self::$post_ids[ $post_key ] );
  2631. /*
  2632. * The dataProvider runs before the fixures are set up, therefore the
  2633. * post object IDs are placeholders that needs to be replaced.
  2634. */
  2635. $expected_url = home_url( str_replace( '%ID%', $post->ID, $expected_url ) );
  2636. $this->go_to( get_permalink( $post ) );
  2637. $this->assertSame( $expected_url, get_permalink( $post ) );
  2638. if ( $expected_404 ) {
  2639. $this->assertQueryTrue( 'is_404' );
  2640. } else {
  2641. $this->assertQueryTrue( 'is_attachment', 'is_single', 'is_singular' );
  2642. }
  2643. $this->assertSame( 'attachment', $post->post_type );
  2644. }
  2645. /**
  2646. * Data provider for test_attachment_permalinks_based_on_parent_status().
  2647. *
  2648. * @return array[] {
  2649. * @type string $post_key Post as keyed in the shared fixture array.
  2650. * @type string $expected_url Expected permalink.
  2651. * $type bool $expected_404 Whether the page is expected to return a 404 result.
  2652. * }
  2653. */
  2654. function data_attachment_permalinks_based_on_parent_status() {
  2655. return array(
  2656. array( 'draft-attachment', '/?attachment_id=%ID%', true ),
  2657. array( 'publish-attachment', '/publish-post/publish-attachment', false ),
  2658. array( 'future-attachment', '/future-post/future-attachment', false ),
  2659. array( 'auto-draft-attachment', '/?attachment_id=%ID%', true ),
  2660. array( 'trash-attachment', '/?attachment_id=%ID%', false ),
  2661. );
  2662. }
  2663. }
  2664. /**
  2665. * Helper class for `test_autoembed`.
  2666. */
  2667. class Test_Autoembed extends WP_Embed {
  2668. public function shortcode( $attr, $url = '' ) {
  2669. return '[embed]';
  2670. }
  2671. }