12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182 |
- <?php
- /**
- * @group media
- * @group shortcode
- */
- class Tests_Media extends WP_UnitTestCase {
- protected static $large_id;
- protected static $_sizes;
- protected static $large_filename = 'test-image-large.jpg';
- protected static $post_ids;
- public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
- self::$_sizes = wp_get_additional_image_sizes();
- $GLOBALS['_wp_additional_image_sizes'] = array();
- $filename = DIR_TESTDATA . '/images/' . self::$large_filename;
- self::$large_id = $factory->attachment->create_upload_object( $filename );
- $post_statuses = array( 'publish', 'future', 'draft', 'auto-draft', 'trash' );
- foreach ( $post_statuses as $post_status ) {
- $date = '';
- if ( 'future' === $post_status ) {
- strftime( '%Y-%m-%d %H:%M:%S', strtotime( '+1 year' ) );
- }
- self::$post_ids[ $post_status ] = $factory->post->create(
- array(
- 'post_status' => 'trash' === $post_status ? 'publish' : $post_status,
- 'post_date' => $date,
- 'post_name' => "$post_status-post",
- )
- );
- // Attachments without media.
- self::$post_ids[ "$post_status-attachment" ] = $factory->attachment->create_object(
- array(
- 'post_parent' => self::$post_ids[ $post_status ],
- 'post_status' => 'inherit',
- 'post_name' => "$post_status-attachment",
- 'post_date' => $date,
- )
- );
- }
- // Trash the trash post.
- wp_trash_post( self::$post_ids['trash'] );
- }
- public static function wpTearDownAfterClass() {
- $GLOBALS['_wp_additional_image_sizes'] = self::$_sizes;
- }
- public static function tearDownAfterClass() {
- wp_delete_post( self::$large_id, true );
- parent::tearDownAfterClass();
- }
- function setUp() {
- parent::setUp();
- $this->caption = 'A simple caption.';
- $this->alternate_caption = 'Alternate caption.';
- $this->html_content = <<<CAP
- A <strong class='classy'>bolded</strong> <em>caption</em> with a <a href="#">link</a>.
- CAP;
- $this->img_content = <<<CAP
- <img src="pic.jpg" id='anId' alt="pic"/>
- CAP;
- $this->img_name = 'image.jpg';
- $this->img_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $this->img_name;
- $this->img_html = '<img src="' . $this->img_url . '"/>';
- $this->img_meta = array(
- 'width' => 100,
- 'height' => 100,
- 'sizes' => '',
- );
- }
- function test_img_caption_shortcode_added() {
- global $shortcode_tags;
- $this->assertSame( 'img_caption_shortcode', $shortcode_tags['caption'] );
- $this->assertSame( 'img_caption_shortcode', $shortcode_tags['wp_caption'] );
- }
- function test_img_caption_shortcode_with_empty_params() {
- $result = img_caption_shortcode( array() );
- $this->assertNull( $result );
- }
- /**
- * @ticket 33981
- */
- function test_img_caption_shortcode_with_empty_params_but_content() {
- $result = img_caption_shortcode( array(), $this->caption );
- $this->assertSame( $this->caption, $result );
- }
- /**
- * @ticket 33981
- */
- function test_img_caption_shortcode_short_circuit_filter() {
- add_filter( 'img_caption_shortcode', array( $this, '_return_alt_caption' ) );
- $result = img_caption_shortcode( array(), $this->caption );
- $this->assertSame( $this->alternate_caption, $result );
- }
- /**
- * Filter used in test_img_caption_shortcode_short_circuit_filter()
- */
- function _return_alt_caption() {
- return $this->alternate_caption;
- }
- /**
- * @ticket 33981
- */
- function test_img_caption_shortcode_empty_width() {
- $result = img_caption_shortcode(
- array(
- 'width' => 0,
- ),
- $this->caption
- );
- $this->assertSame( $this->caption, $result );
- }
- /**
- * @ticket 33981
- */
- function test_img_caption_shortcode_empty_caption() {
- $result = img_caption_shortcode(
- array(
- 'caption' => '',
- )
- );
- $this->assertNull( $result );
- }
- /**
- * @ticket 33981
- */
- function test_img_caption_shortcode_empty_caption_and_content() {
- $result = img_caption_shortcode(
- array(
- 'caption' => '',
- ),
- $this->caption
- );
- $this->assertSame( $this->caption, $result );
- }
- function test_img_caption_shortcode_with_old_format() {
- $result = img_caption_shortcode(
- array(
- 'width' => 20,
- 'caption' => $this->caption,
- )
- );
- $this->assertSame( 2, preg_match_all( '/wp-caption/', $result, $_r ) );
- $this->assertSame( 1, preg_match_all( '/alignnone/', $result, $_r ) );
- $this->assertSame( 1, preg_match_all( "/{$this->caption}/", $result, $_r ) );
- if ( current_theme_supports( 'html5', 'caption' ) ) {
- $this->assertSame( 1, preg_match_all( '/width: 20/', $result, $_r ) );
- } else {
- $this->assertSame( 1, preg_match_all( '/width: 30/', $result, $_r ) );
- }
- }
- function test_img_caption_shortcode_with_old_format_id_and_align() {
- $result = img_caption_shortcode(
- array(
- 'width' => 20,
- 'caption' => $this->caption,
- 'id' => '"myId',
- 'align' => '&myAlignment',
- )
- );
- $this->assertSame( 1, preg_match_all( '/wp-caption &myAlignment/', $result, $_r ) );
- $this->assertSame( 1, preg_match_all( '/id="myId"/', $result, $_r ) );
- $this->assertSame( 1, preg_match_all( "/{$this->caption}/", $result, $_r ) );
- }
- function test_img_caption_shortcode_with_old_format_and_class() {
- $result = img_caption_shortcode(
- array(
- 'width' => 20,
- 'class' => 'some-class another-class',
- 'caption' => $this->caption,
- )
- );
- $this->assertSame( 1, preg_match_all( '/wp-caption alignnone some-class another-class/', $result, $_r ) );
- }
- function test_new_img_caption_shortcode_with_html_caption() {
- $result = img_caption_shortcode(
- array(
- 'width' => 20,
- 'caption' => $this->html_content,
- )
- );
- $our_preg = preg_quote( $this->html_content );
- $this->assertSame( 1, preg_match_all( "~{$our_preg}~", $result, $_r ) );
- }
- function test_new_img_caption_shortcode_new_format() {
- $result = img_caption_shortcode(
- array( 'width' => 20 ),
- $this->img_content . $this->html_content
- );
- $img_preg = preg_quote( $this->img_content );
- $content_preg = preg_quote( $this->html_content );
- $this->assertSame( 1, preg_match_all( "~{$img_preg}.*wp-caption-text~", $result, $_r ) );
- $this->assertSame( 1, preg_match_all( "~wp-caption-text.*{$content_preg}~", $result, $_r ) );
- }
- function test_new_img_caption_shortcode_new_format_and_linked_image() {
- $linked_image = "<a href='#'>{$this->img_content}</a>";
- $result = img_caption_shortcode(
- array( 'width' => 20 ),
- $linked_image . $this->html_content
- );
- $img_preg = preg_quote( $linked_image );
- $content_preg = preg_quote( $this->html_content );
- $this->assertSame( 1, preg_match_all( "~{$img_preg}.*wp-caption-text~", $result, $_r ) );
- $this->assertSame( 1, preg_match_all( "~wp-caption-text.*{$content_preg}~", $result, $_r ) );
- }
- function test_new_img_caption_shortcode_new_format_and_linked_image_with_newline() {
- $linked_image = "<a href='#'>{$this->img_content}</a>";
- $result = img_caption_shortcode(
- array( 'width' => 20 ),
- $linked_image . "\n\n" . $this->html_content
- );
- $img_preg = preg_quote( $linked_image );
- $content_preg = preg_quote( $this->html_content );
- $this->assertSame( 1, preg_match_all( "~{$img_preg}.*wp-caption-text~", $result, $_r ) );
- $this->assertSame( 1, preg_match_all( "~wp-caption-text.*{$content_preg}~", $result, $_r ) );
- }
- /**
- * @ticket 34595
- */
- function test_img_caption_shortcode_has_aria_describedby() {
- $result = img_caption_shortcode(
- array(
- 'width' => 20,
- 'id' => 'myId',
- ),
- $this->img_content . $this->html_content
- );
- $this->assertSame( 1, preg_match_all( '/aria-describedby="caption-myId"/', $result, $_r ) );
- }
- function test_add_remove_oembed_provider() {
- wp_oembed_add_provider( 'http://foo.bar/*', 'http://foo.bar/oembed' );
- $this->assertTrue( wp_oembed_remove_provider( 'http://foo.bar/*' ) );
- $this->assertFalse( wp_oembed_remove_provider( 'http://foo.bar/*' ) );
- }
- /**
- * @ticket 23776
- */
- function test_autoembed_empty() {
- global $wp_embed;
- $content = '';
- $result = $wp_embed->autoembed( $content );
- $this->assertSame( $content, $result );
- }
- /**
- * @ticket 23776
- *
- * @group external-http
- */
- function test_autoembed_no_paragraphs_around_urls() {
- global $wp_embed;
- $content = <<<EOF
- $ my command
- First line.
- http://example.com/1/
- http://example.com/2/
- Last line.
- <pre>http://some.link/
- http://some.other.link/</pre>
- EOF;
- $result = $wp_embed->autoembed( $content );
- $this->assertSame( $content, $result );
- }
- function data_autoembed() {
- return array(
- // Should embed.
- array(
- 'https://w.org',
- '[embed]',
- ),
- array(
- 'test
- https://w.org
- test',
- 'test
- [embed]
- test',
- ),
- array(
- '<p class="test">https://w.org</p>',
- '<p class="test">[embed]</p>',
- ),
- array(
- '<p> https://w.org </p>',
- '<p> [embed] </p>',
- ),
- array(
- '<p>test
- https://w.org
- test</p>',
- '<p>test
- [embed]
- test</p>',
- ),
- array(
- '<p>https://w.org
- </p>',
- '<p>[embed]
- </p>',
- ),
- // Should NOT embed.
- array(
- 'test https://w.org</p>',
- ),
- array(
- '<span>https://w.org</a>',
- ),
- array(
- '<pre>https://w.org
- </p>',
- ),
- array(
- '<a href="https://w.org">
- https://w.org</a>',
- ),
- );
- }
- /**
- * @dataProvider data_autoembed
- */
- function test_autoembed( $content, $result = null ) {
- $wp_embed = new Test_Autoembed;
- $this->assertSame( $wp_embed->autoembed( $content ), $result ? $result : $content );
- }
- function test_wp_prepare_attachment_for_js() {
- // Attachment without media.
- $id = wp_insert_attachment(
- array(
- 'post_status' => 'publish',
- 'post_title' => 'Prepare',
- 'post_content_filtered' => 'Prepare',
- 'post_type' => 'post',
- )
- );
- $post = get_post( $id );
- $prepped = wp_prepare_attachment_for_js( $post );
- $this->assertInternalType( 'array', $prepped );
- $this->assertSame( 0, $prepped['uploadedTo'] );
- $this->assertSame( '', $prepped['mime'] );
- $this->assertSame( '', $prepped['type'] );
- $this->assertSame( '', $prepped['subtype'] );
- // #21963, there will be a GUID always, so there will be a URL.
- $this->assertNotEquals( '', $prepped['url'] );
- $this->assertSame( site_url( 'wp-includes/images/media/default.png' ), $prepped['icon'] );
- // Fake a mime.
- $post->post_mime_type = 'image/jpeg';
- $prepped = wp_prepare_attachment_for_js( $post );
- $this->assertSame( 'image/jpeg', $prepped['mime'] );
- $this->assertSame( 'image', $prepped['type'] );
- $this->assertSame( 'jpeg', $prepped['subtype'] );
- // Fake a mime without a slash. See #WP22532.
- $post->post_mime_type = 'image';
- $prepped = wp_prepare_attachment_for_js( $post );
- $this->assertSame( 'image', $prepped['mime'] );
- $this->assertSame( 'image', $prepped['type'] );
- $this->assertSame( '', $prepped['subtype'] );
- // Test that if author is not found, we return "(no author)" as `display_name`.
- // The previously used test post contains no author, so we can reuse it.
- $this->assertSame( '(no author)', $prepped['authorName'] );
- // Test that if author has HTML entities in display_name, they're decoded correctly.
- $html_entity_author = self::factory()->user->create(
- array(
- 'display_name' => 'You & Me',
- )
- );
- $post->post_author = $html_entity_author;
- $prepped = wp_prepare_attachment_for_js( $post );
- $this->assertSame( 'You & Me', $prepped['authorName'] );
- }
- /**
- * @ticket 38965
- */
- function test_wp_prepare_attachment_for_js_without_image_sizes() {
- // Create the attachement post.
- $id = wp_insert_attachment(
- array(
- 'post_title' => 'Attachment Title',
- 'post_type' => 'attachment',
- 'post_parent' => 0,
- 'post_mime_type' => 'image/jpeg',
- 'guid' => 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test-image.jpg',
- )
- );
- // Add attachment metadata without sizes.
- wp_update_attachment_metadata(
- $id,
- array(
- 'width' => 50,
- 'height' => 50,
- 'file' => 'test-image.jpg',
- )
- );
- $prepped = wp_prepare_attachment_for_js( get_post( $id ) );
- $this->assertTrue( isset( $prepped['sizes'] ) );
- }
- /**
- * @ticket 19067
- * @expectedDeprecated wp_convert_bytes_to_hr
- */
- function test_wp_convert_bytes_to_hr() {
- $kb = 1024;
- $mb = $kb * 1024;
- $gb = $mb * 1024;
- $tb = $gb * 1024;
- // Test if boundaries are correct.
- $this->assertSame( '1TB', wp_convert_bytes_to_hr( $tb ) );
- $this->assertSame( '1GB', wp_convert_bytes_to_hr( $gb ) );
- $this->assertSame( '1MB', wp_convert_bytes_to_hr( $mb ) );
- $this->assertSame( '1KB', wp_convert_bytes_to_hr( $kb ) );
- $this->assertSame( '1 TB', size_format( $tb ) );
- $this->assertSame( '1 GB', size_format( $gb ) );
- $this->assertSame( '1 MB', size_format( $mb ) );
- $this->assertSame( '1 KB', size_format( $kb ) );
- // Now some values around.
- $hr = wp_convert_bytes_to_hr( $tb + $tb / 2 + $mb );
- $this->assertEqualsWithDelta( 1.50000095367, (float) str_replace( ',', '.', $hr ), 0.0001, 'The values should be equal' );
- $hr = wp_convert_bytes_to_hr( $tb - $mb - $kb );
- $this->assertEqualsWithDelta( 1023.99902248, (float) str_replace( ',', '.', $hr ), 0.0001, 'The values should be equal' );
- $hr = wp_convert_bytes_to_hr( $gb + $gb / 2 + $mb );
- $this->assertEqualsWithDelta( 1.5009765625, (float) str_replace( ',', '.', $hr ), 0.0001, 'The values should be equal' );
- $hr = wp_convert_bytes_to_hr( $gb - $mb - $kb );
- $this->assertEqualsWithDelta( 1022.99902344, (float) str_replace( ',', '.', $hr ), 0.0001, 'The values should be equal' );
- // Edge.
- $this->assertSame( '-1B', wp_convert_bytes_to_hr( -1 ) );
- $this->assertSame( '0B', wp_convert_bytes_to_hr( 0 ) );
- }
- /**
- * @ticket 22960
- */
- function test_get_attached_images() {
- $post_id = self::factory()->post->create();
- $attachment_id = self::factory()->attachment->create_object(
- $this->img_name,
- $post_id,
- array(
- 'post_mime_type' => 'image/jpeg',
- 'post_type' => 'attachment',
- )
- );
- $images = get_attached_media( 'image', $post_id );
- $this->assertEquals( $images, array( $attachment_id => get_post( $attachment_id ) ) );
- }
- /**
- * @ticket 22960
- */
- function test_post_galleries_images() {
- $ids1 = array();
- $ids1_srcs = array();
- foreach ( range( 1, 3 ) as $i ) {
- $attachment_id = self::factory()->attachment->create_object(
- "image$i.jpg",
- 0,
- array(
- 'post_mime_type' => 'image/jpeg',
- 'post_type' => 'attachment',
- )
- );
- $metadata = array_merge( array( 'file' => "image$i.jpg" ), $this->img_meta );
- wp_update_attachment_metadata( $attachment_id, $metadata );
- $ids1[] = $attachment_id;
- $ids1_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
- }
- $ids2 = array();
- $ids2_srcs = array();
- foreach ( range( 4, 6 ) as $i ) {
- $attachment_id = self::factory()->attachment->create_object(
- "image$i.jpg",
- 0,
- array(
- 'post_mime_type' => 'image/jpeg',
- 'post_type' => 'attachment',
- )
- );
- $metadata = array_merge( array( 'file' => "image$i.jpg" ), $this->img_meta );
- wp_update_attachment_metadata( $attachment_id, $metadata );
- $ids2[] = $attachment_id;
- $ids2_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
- }
- $ids1_joined = implode( ',', $ids1 );
- $ids2_joined = implode( ',', $ids2 );
- $blob = <<<BLOB
- [gallery ids="$ids1_joined"]
- [gallery ids="$ids2_joined"]
- BLOB;
- $post_id = self::factory()->post->create( array( 'post_content' => $blob ) );
- $srcs = get_post_galleries_images( $post_id );
- $this->assertSame( $srcs, array( $ids1_srcs, $ids2_srcs ) );
- }
- /**
- * @ticket 39304
- */
- function test_post_galleries_images_without_global_post() {
- // Set up an unattached image.
- $this->factory->attachment->create_object(
- array(
- 'file' => 'test.jpg',
- 'post_parent' => 0,
- 'post_mime_type' => 'image/jpeg',
- 'post_type' => 'attachment',
- )
- );
- $post_id = $this->factory->post->create(
- array(
- 'post_content' => '[gallery]',
- )
- );
- $galleries = get_post_galleries( $post_id, false );
- $this->assertEmpty( $galleries[0]['src'] );
- }
- /**
- * @ticket 39304
- */
- function test_post_galleries_ignores_global_post() {
- $global_post_id = $this->factory->post->create(
- array(
- 'post_content' => 'Global Post',
- )
- );
- $post_id = $this->factory->post->create(
- array(
- 'post_content' => '[gallery]',
- )
- );
- $this->factory->attachment->create_object(
- array(
- 'file' => 'test.jpg',
- 'post_parent' => $post_id,
- 'post_mime_type' => 'image/jpeg',
- 'post_type' => 'attachment',
- )
- );
- $expected_srcs = array(
- 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test.jpg',
- );
- // Set the global $post context to the other post.
- $GLOBALS['post'] = get_post( $global_post_id );
- $galleries = get_post_galleries( $post_id, false );
- $this->assertNotEmpty( $galleries[0]['src'] );
- $this->assertSame( $galleries[0]['src'], $expected_srcs );
- }
- /**
- * @ticket 39304
- */
- function test_post_galleries_respects_id_attrs() {
- $post_id = $this->factory->post->create(
- array(
- 'post_content' => 'No gallery defined',
- )
- );
- $post_id_two = $this->factory->post->create(
- array(
- 'post_content' => "[gallery id='$post_id']",
- )
- );
- $this->factory->attachment->create_object(
- array(
- 'file' => 'test.jpg',
- 'post_parent' => $post_id,
- 'post_mime_type' => 'image/jpeg',
- 'post_type' => 'attachment',
- )
- );
- $expected_srcs = array(
- 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test.jpg',
- );
- $galleries = get_post_galleries( $post_id_two, false );
- // Set the global $post context.
- $GLOBALS['post'] = get_post( $post_id_two );
- $galleries_with_global_context = get_post_galleries( $post_id_two, false );
- // Check that the global post state doesn't affect the results.
- $this->assertSame( $galleries, $galleries_with_global_context );
- $this->assertNotEmpty( $galleries[0]['src'] );
- $this->assertSame( $galleries[0]['src'], $expected_srcs );
- }
- /**
- * @ticket 22960
- */
- function test_post_gallery_images() {
- $ids1 = array();
- $ids1_srcs = array();
- foreach ( range( 1, 3 ) as $i ) {
- $attachment_id = self::factory()->attachment->create_object(
- "image$i.jpg",
- 0,
- array(
- 'post_mime_type' => 'image/jpeg',
- 'post_type' => 'attachment',
- )
- );
- $metadata = array_merge( array( 'file' => "image$i.jpg" ), $this->img_meta );
- wp_update_attachment_metadata( $attachment_id, $metadata );
- $ids1[] = $attachment_id;
- $ids1_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
- }
- $ids2 = array();
- $ids2_srcs = array();
- foreach ( range( 4, 6 ) as $i ) {
- $attachment_id = self::factory()->attachment->create_object(
- "image$i.jpg",
- 0,
- array(
- 'post_mime_type' => 'image/jpeg',
- 'post_type' => 'attachment',
- )
- );
- $metadata = array_merge( array( 'file' => "image$i.jpg" ), $this->img_meta );
- wp_update_attachment_metadata( $attachment_id, $metadata );
- $ids2[] = $attachment_id;
- $ids2_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
- }
- $ids1_joined = implode( ',', $ids1 );
- $ids2_joined = implode( ',', $ids2 );
- $blob = <<<BLOB
- [gallery ids="$ids1_joined"]
- [gallery ids="$ids2_joined"]
- BLOB;
- $post_id = self::factory()->post->create( array( 'post_content' => $blob ) );
- $srcs = get_post_gallery_images( $post_id );
- $this->assertSame( $srcs, $ids1_srcs );
- }
- function test_get_media_embedded_in_content() {
- $object = <<<OBJ
- <object src="this" data="that">
- <param name="value"/>
- </object>
- OBJ;
- $embed = <<<EMBED
- <embed src="something.mp4"/>
- EMBED;
- $iframe = <<<IFRAME
- <iframe src="youtube.com" width="7000" />
- IFRAME;
- $audio = <<<AUDIO
- <audio preload="none">
- <source />
- </audio>
- AUDIO;
- $video = <<<VIDEO
- <video preload="none">
- <source />
- </video>
- VIDEO;
- $content = <<<CONTENT
- This is a comment
- $object
- This is a comment
- $embed
- This is a comment
- $iframe
- This is a comment
- $audio
- This is a comment
- $video
- This is a comment
- CONTENT;
- $types = array( 'object', 'embed', 'iframe', 'audio', 'video' );
- $contents = array_values( compact( $types ) );
- $matches = get_media_embedded_in_content( $content, 'audio' );
- $this->assertSame( array( $audio ), $matches );
- $matches = get_media_embedded_in_content( $content, 'video' );
- $this->assertSame( array( $video ), $matches );
- $matches = get_media_embedded_in_content( $content, 'object' );
- $this->assertSame( array( $object ), $matches );
- $matches = get_media_embedded_in_content( $content, 'embed' );
- $this->assertSame( array( $embed ), $matches );
- $matches = get_media_embedded_in_content( $content, 'iframe' );
- $this->assertSame( array( $iframe ), $matches );
- $matches = get_media_embedded_in_content( $content, $types );
- $this->assertSame( $contents, $matches );
- }
- function test_get_media_embedded_in_content_order() {
- $audio = <<<AUDIO
- <audio preload="none">
- <source />
- </audio>
- AUDIO;
- $video = <<<VIDEO
- <video preload="none">
- <source />
- </video>
- VIDEO;
- $content = $audio . $video;
- $matches1 = get_media_embedded_in_content( $content, array( 'audio', 'video' ) );
- $this->assertSame( array( $audio, $video ), $matches1 );
- $reversed = $video . $audio;
- $matches2 = get_media_embedded_in_content( $reversed, array( 'audio', 'video' ) );
- $this->assertSame( array( $video, $audio ), $matches2 );
- }
- /**
- * @ticket 35367
- */
- function test_wp_audio_shortcode_with_empty_params() {
- $this->assertNull( wp_audio_shortcode( array() ) );
- }
- /**
- * @ticket 35367
- */
- function test_wp_audio_shortcode_with_bad_attr() {
- $this->assertSame(
- '<a class="wp-embedded-audio" href="https://example.com/foo.php">https://example.com/foo.php</a>',
- wp_audio_shortcode(
- array(
- 'src' => 'https://example.com/foo.php',
- )
- )
- );
- }
- /**
- * @ticket 35367
- */
- function test_wp_audio_shortcode_attributes() {
- $actual = wp_audio_shortcode(
- array(
- 'src' => 'https://example.com/foo.mp3',
- )
- );
- $this->assertContains( 'src="https://example.com/foo.mp3', $actual );
- $this->assertNotContains( 'loop', $actual );
- $this->assertNotContains( 'autoplay', $actual );
- $this->assertContains( 'preload="none"', $actual );
- $this->assertContains( 'class="wp-audio-shortcode"', $actual );
- $this->assertContains( 'style="width: 100%;"', $actual );
- $actual = wp_audio_shortcode(
- array(
- 'src' => 'https://example.com/foo.mp3',
- 'loop' => true,
- 'autoplay' => true,
- 'preload' => true,
- 'class' => 'foobar',
- 'style' => 'padding:0;',
- )
- );
- $this->assertContains( 'src="https://example.com/foo.mp3', $actual );
- $this->assertContains( 'loop="1"', $actual );
- $this->assertContains( 'autoplay="1"', $actual );
- $this->assertContains( 'preload="1"', $actual );
- $this->assertContains( 'class="foobar"', $actual );
- $this->assertContains( 'style="padding:0;"', $actual );
- }
- /**
- * Test [video] shortcode processing
- */
- function test_video_shortcode_body() {
- $width = 720;
- $height = 480;
- $w = empty( $GLOBALS['content_width'] ) ? 640 : $GLOBALS['content_width'];
- if ( $width > $w ) {
- $width = $w;
- }
- $post_id = get_post() ? get_the_ID() : 0;
- $video = <<<VIDEO
- [video width="$width" height="480" mp4="http://domain.tld/wp-content/uploads/2013/12/xyz.mp4"]
- <!-- WebM/VP8 for Firefox4, Opera, and Chrome -->
- <source type="video/webm" src="myvideo.webm" />
- <!-- Ogg/Vorbis for older Firefox and Opera versions -->
- <source type="video/ogg" src="myvideo.ogv" />
- <!-- Optional: Add subtitles for each language -->
- <track kind="subtitles" src="subtitles.srt" srclang="en" />
- <!-- Optional: Add chapters -->
- <track kind="chapters" src="chapters.srt" srclang="en" />
- [/video]
- VIDEO;
- $h = ceil( ( $height * $width ) / $width );
- $content = apply_filters( 'the_content', $video );
- $expected = '<div style="width: ' . $width . 'px;" class="wp-video">' .
- "<!--[if lt IE 9]><script>document.createElement('video');</script><![endif]-->\n" .
- '<video class="wp-video-shortcode" id="video-' . $post_id . '-1" width="' . $width . '" height="' . $h . '" preload="metadata" controls="controls">' .
- '<source type="video/mp4" src="http://domain.tld/wp-content/uploads/2013/12/xyz.mp4?_=1" />' .
- '<!-- WebM/VP8 for Firefox4, Opera, and Chrome --><source type="video/webm" src="myvideo.webm" />' .
- '<!-- Ogg/Vorbis for older Firefox and Opera versions --><source type="video/ogg" src="myvideo.ogv" />' .
- '<!-- Optional: Add subtitles for each language --><track kind="subtitles" src="subtitles.srt" srclang="en" />' .
- '<!-- Optional: Add chapters --><track kind="chapters" src="chapters.srt" srclang="en" />' .
- '<a href="http://domain.tld/wp-content/uploads/2013/12/xyz.mp4">' .
- "http://domain.tld/wp-content/uploads/2013/12/xyz.mp4</a></video></div>\n";
- $this->assertSame( $expected, $content );
- }
- /**
- * @ticket 35367
- * @depends test_video_shortcode_body
- */
- function test_wp_video_shortcode_with_empty_params() {
- $this->assertNull( wp_video_shortcode( array() ) );
- }
- /**
- * @ticket 35367
- * @depends test_video_shortcode_body
- */
- function test_wp_video_shortcode_with_bad_attr() {
- $this->assertSame(
- '<a class="wp-embedded-video" href="https://example.com/foo.php">https://example.com/foo.php</a>',
- wp_video_shortcode(
- array(
- 'src' => 'https://example.com/foo.php',
- )
- )
- );
- }
- /**
- * @ticket 35367
- * @depends test_video_shortcode_body
- */
- function test_wp_video_shortcode_attributes() {
- $actual = wp_video_shortcode(
- array(
- 'src' => 'https://example.com/foo.mp4',
- )
- );
- $this->assertContains( 'src="https://example.com/foo.mp4', $actual );
- $this->assertNotContains( 'loop', $actual );
- $this->assertNotContains( 'autoplay', $actual );
- $this->assertContains( 'preload="metadata"', $actual );
- $this->assertContains( 'width="640"', $actual );
- $this->assertContains( 'height="360"', $actual );
- $this->assertContains( 'class="wp-video-shortcode"', $actual );
- $actual = wp_video_shortcode(
- array(
- 'src' => 'https://example.com/foo.mp4',
- 'poster' => 'https://example.com/foo.png',
- 'loop' => true,
- 'autoplay' => true,
- 'preload' => true,
- 'width' => 123,
- 'height' => 456,
- 'class' => 'foobar',
- )
- );
- $this->assertContains( 'src="https://example.com/foo.mp4', $actual );
- $this->assertContains( 'poster="https://example.com/foo.png', $actual );
- $this->assertContains( 'loop="1"', $actual );
- $this->assertContains( 'autoplay="1"', $actual );
- $this->assertContains( 'preload="1"', $actual );
- $this->assertContains( 'width="123"', $actual );
- $this->assertContains( 'height="456"', $actual );
- $this->assertContains( 'class="foobar"', $actual );
- }
- /**
- * @ticket 40866
- * @depends test_video_shortcode_body
- */
- function test_wp_video_shortcode_youtube_remove_feature() {
- $actual = wp_video_shortcode(
- array(
- 'src' => 'https://www.youtube.com/watch?v=72xdCU__XCk&feature=youtu.be',
- )
- );
- $this->assertNotContains( 'feature=youtu.be', $actual );
- }
- /**
- * @ticket 40866
- * @depends test_video_shortcode_body
- */
- function test_wp_video_shortcode_youtube_force_ssl() {
- $actual = wp_video_shortcode(
- array(
- 'src' => 'http://www.youtube.com/watch?v=72xdCU__XCk',
- )
- );
- $this->assertContains( 'src="https://www.youtube.com/watch?v=72xdCU__XCk', $actual );
- }
- /**
- * @ticket 40866
- * @depends test_video_shortcode_body
- */
- function test_wp_video_shortcode_vimeo_force_ssl_remove_query_args() {
- $actual = wp_video_shortcode(
- array(
- 'src' => 'http://vimeo.com/76979871?blah=meh',
- )
- );
- $this->assertContains( 'src="https://vimeo.com/76979871', $actual );
- $this->assertNotContains( 'blah=meh', $actual );
- }
- /**
- * @ticket 40977
- * @depends test_video_shortcode_body
- */
- function test_wp_video_shortcode_vimeo_adds_loop() {
- $actual = wp_video_shortcode(
- array(
- 'src' => 'http://vimeo.com/76979871',
- )
- );
- $this->assertContains( 'src="https://vimeo.com/76979871?loop=0', $actual );
- }
- /**
- * @ticket 40977
- * @depends test_video_shortcode_body
- */
- function test_wp_video_shortcode_vimeo_force_adds_loop_true() {
- $actual = wp_video_shortcode(
- array(
- 'src' => 'http://vimeo.com/76979871',
- 'loop' => true,
- )
- );
- $this->assertContains( 'src="https://vimeo.com/76979871?loop=1', $actual );
- }
- /**
- * @ticket 26768
- */
- function test_add_image_size() {
- $_wp_additional_image_sizes = wp_get_additional_image_sizes();
- remove_image_size( 'test-size' );
- $this->assertArrayNotHasKey( 'test-size', $_wp_additional_image_sizes );
- add_image_size( 'test-size', 200, 600 );
- $sizes = wp_get_additional_image_sizes();
- // Clean up.
- remove_image_size( 'test-size' );
- $this->assertArrayHasKey( 'test-size', $sizes );
- $this->assertSame( 200, $sizes['test-size']['width'] );
- $this->assertSame( 600, $sizes['test-size']['height'] );
- }
- /**
- * @ticket 26768
- */
- function test_remove_image_size() {
- add_image_size( 'test-size', 200, 600 );
- $this->assertTrue( has_image_size( 'test-size' ) );
- remove_image_size( 'test-size' );
- $this->assertFalse( has_image_size( 'test-size' ) );
- }
- /**
- * @ticket 26951
- */
- function test_has_image_size() {
- add_image_size( 'test-size', 200, 600 );
- $this->assertTrue( has_image_size( 'test-size' ) );
- // Clean up.
- remove_image_size( 'test-size' );
- }
- /**
- * @ticket 30346
- */
- function test_attachment_url_to_postid() {
- $image_path = '2014/11/' . $this->img_name;
- $attachment_id = self::factory()->attachment->create_object(
- $image_path,
- 0,
- array(
- 'post_mime_type' => 'image/jpeg',
- 'post_type' => 'attachment',
- )
- );
- $image_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_path;
- $this->assertSame( $attachment_id, attachment_url_to_postid( $image_url ) );
- }
- /**
- * @ticket 33109
- */
- function test_attachment_url_to_postid_with_different_scheme() {
- $image_path = '2014/11/' . $this->img_name;
- $attachment_id = self::factory()->attachment->create_object(
- $image_path,
- 0,
- array(
- 'post_mime_type' => 'image/jpeg',
- 'post_type' => 'attachment',
- )
- );
- $image_url = 'https://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_path;
- $this->assertSame( $attachment_id, attachment_url_to_postid( $image_url ) );
- }
- /**
- * @ticket 39768
- */
- function test_attachment_url_to_postid_should_be_case_sensitive() {
- $image_path_lower_case = '2014/11/' . $this->img_name;
- $attachment_id_lower_case = self::factory()->attachment->create_object(
- $image_path_lower_case,
- 0,
- array(
- 'post_mime_type' => 'image/jpeg',
- 'post_type' => 'attachment',
- )
- );
- $image_path_upper_case = '2014/11/' . ucfirst( $this->img_name );
- $attachment_id_upper_case = self::factory()->attachment->create_object(
- $image_path_upper_case,
- 0,
- array(
- 'post_mime_type' => 'image/jpeg',
- 'post_type' => 'attachment',
- )
- );
- $image_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_path_upper_case;
- $this->assertSame( $attachment_id_upper_case, attachment_url_to_postid( $image_url ) );
- }
- function test_attachment_url_to_postid_filtered() {
- $image_path = '2014/11/' . $this->img_name;
- $attachment_id = self::factory()->attachment->create_object(
- $image_path,
- 0,
- array(
- 'post_mime_type' => 'image/jpeg',
- 'post_type' => 'attachment',
- )
- );
- add_filter( 'upload_dir', array( $this, '_upload_dir' ) );
- $image_url = 'http://192.168.1.20.com/wp-content/uploads/' . $image_path;
- $this->assertSame( $attachment_id, attachment_url_to_postid( $image_url ) );
- remove_filter( 'upload_dir', array( $this, '_upload_dir' ) );
- }
- function _upload_dir( $dir ) {
- $dir['baseurl'] = 'http://192.168.1.20.com/wp-content/uploads';
- return $dir;
- }
- /**
- * @ticket 31044
- */
- function test_attachment_url_to_postid_with_empty_url() {
- $post_id = attachment_url_to_postid( '' );
- $this->assertInternalType( 'int', $post_id );
- $this->assertSame( 0, $post_id );
- }
- /**
- * @ticket 22768
- */
- public function test_media_handle_upload_sets_post_excerpt() {
- $iptc_file = DIR_TESTDATA . '/images/test-image-iptc.jpg';
- // Make a copy of this file as it gets moved during the file upload.
- $tmp_name = wp_tempnam( $iptc_file );
- copy( $iptc_file, $tmp_name );
- $_FILES['upload'] = array(
- 'tmp_name' => $tmp_name,
- 'name' => 'test-image-iptc.jpg',
- 'type' => 'image/jpeg',
- 'error' => 0,
- 'size' => filesize( $iptc_file ),
- );
- $post_id = media_handle_upload(
- 'upload',
- 0,
- array(),
- array(
- 'action' => 'test_iptc_upload',
- 'test_form' => false,
- )
- );
- unset( $_FILES['upload'] );
- $post = get_post( $post_id );
- // Clean up.
- wp_delete_attachment( $post_id );
- $this->assertSame( 'This is a comment. / Ðто комментарий. / ΒλÎπετε Îνα σχόλιο.', $post->post_excerpt );
- }
- /**
- * @ticket 37989
- */
- public function test_media_handle_upload_expected_titles() {
- $test_file = DIR_TESTDATA . '/images/test-image.jpg';
- // Make a copy of this file as it gets moved during the file upload.
- $tmp_name = wp_tempnam( $test_file );
- copy( $test_file, $tmp_name );
- $_FILES['upload'] = array(
- 'tmp_name' => $tmp_name,
- 'name' => 'This is a test.jpg',
- 'type' => 'image/jpeg',
- 'error' => 0,
- 'size' => filesize( $test_file ),
- );
- $post_id = media_handle_upload(
- 'upload',
- 0,
- array(),
- array(
- 'action' => 'test_upload_titles',
- 'test_form' => false,
- )
- );
- unset( $_FILES['upload'] );
- $post = get_post( $post_id );
- // Clean up.
- wp_delete_attachment( $post_id );
- $this->assertSame( 'This is a test', $post->post_title );
- }
- /**
- * @ticket 33016
- */
- function test_multiline_cdata() {
- global $wp_embed;
- $content = <<<EOF
- <script>// <![CDATA[
- _my_function('data');
- // ]]>
- </script>
- EOF;
- $result = $wp_embed->autoembed( $content );
- $this->assertSame( $content, $result );
- }
- /**
- * @ticket 33016
- */
- function test_multiline_comment() {
- global $wp_embed;
- $content = <<<EOF
- <script><!--
- my_function();
- // --> </script>
- EOF;
- $result = $wp_embed->autoembed( $content );
- $this->assertSame( $content, $result );
- }
- /**
- * @ticket 33016
- *
- * @group external-http
- */
- function test_multiline_comment_with_embeds() {
- $content = <<<EOF
- Start.
- [embed]http://www.youtube.com/embed/TEST01YRHA0[/embed]
- <script><!--
- my_function();
- // --> </script>
- http://www.youtube.com/embed/TEST02YRHA0
- [embed]http://www.example.com/embed/TEST03YRHA0[/embed]
- http://www.example.com/embed/TEST04YRHA0
- Stop.
- EOF;
- $expected = <<<EOF
- <p>Start.<br />
- https://youtube.com/watch?v=TEST01YRHA0<br />
- <script><!--
- my_function();
- // --> </script><br />
- https://youtube.com/watch?v=TEST02YRHA0<br />
- <a href="http://www.example.com/embed/TEST03YRHA0">http://www.example.com/embed/TEST03YRHA0</a><br />
- http://www.example.com/embed/TEST04YRHA0<br />
- Stop.</p>
- EOF;
- $result = apply_filters( 'the_content', $content );
- $this->assertSameIgnoreEOL( $expected, $result );
- }
- /**
- * @ticket 33016
- */
- function filter_wp_embed_shortcode_custom( $content, $url ) {
- if ( 'https://www.example.com/?video=1' === $url ) {
- $content = '@embed URL was replaced@';
- }
- return $content;
- }
- /**
- * @ticket 33016
- *
- * @group external-http
- */
- function test_oembed_explicit_media_link() {
- global $wp_embed;
- add_filter( 'embed_maybe_make_link', array( $this, 'filter_wp_embed_shortcode_custom' ), 10, 2 );
- $content = <<<EOF
- https://www.example.com/?video=1
- EOF;
- $expected = <<<EOF
- @embed URL was replaced@
- EOF;
- $result = $wp_embed->autoembed( $content );
- $this->assertSame( $expected, $result );
- $content = <<<EOF
- <a href="https://www.example.com/?video=1">https://www.example.com/?video=1</a>
- <script>// <![CDATA[
- _my_function('data');
- myvar = 'Hello world
- https://www.example.com/?video=1
- do not break this';
- // ]]>
- </script>
- EOF;
- $result = $wp_embed->autoembed( $content );
- $this->assertSame( $content, $result );
- remove_filter( 'embed_maybe_make_link', array( $this, 'filter_wp_embed_shortcode_custom' ), 10 );
- }
- /**
- * Tests the default output of `wp_get_attachment_image()`.
- *
- * @ticket 34635
- */
- function test_wp_get_attachment_image_defaults() {
- $image = image_downsize( self::$large_id, 'thumbnail' );
- $expected = sprintf(
- '<img width="%1$d" height="%2$d" src="%3$s" class="attachment-thumbnail size-thumbnail" alt="" loading="lazy" />',
- $image[1],
- $image[2],
- $image[0]
- );
- $this->assertSame( $expected, wp_get_attachment_image( self::$large_id ) );
- }
- /**
- * @ticket 50801
- */
- function test_wp_get_attachment_image_filter_output() {
- $image = image_downsize( self::$large_id, 'thumbnail' );
- $expected = 'Override wp_get_attachment_image';
- add_filter( 'wp_get_attachment_image', array( $this, 'filter_wp_get_attachment_image' ) );
- $output = wp_get_attachment_image( self::$large_id );
- remove_filter( 'wp_get_attachment_image', array( $this, 'filter_wp_get_attachment_image' ) );
- $this->assertSame( $expected, $output );
- }
- function filter_wp_get_attachment_image() {
- return 'Override wp_get_attachment_image';
- }
- /**
- * Test that `wp_get_attachment_image()` returns a proper alt value.
- *
- * @ticket 34635
- */
- function test_wp_get_attachment_image_with_alt() {
- // Add test alt metadata.
- update_post_meta( self::$large_id, '_wp_attachment_image_alt', 'Some very clever alt text', true );
- $image = image_downsize( self::$large_id, 'thumbnail' );
- $expected = sprintf(
- '<img width="%1$d" height="%2$d" src="%3$s" class="attachment-thumbnail size-thumbnail" alt="Some very clever alt text" loading="lazy" />',
- $image[1],
- $image[2],
- $image[0]
- );
- $this->assertSame( $expected, wp_get_attachment_image( self::$large_id ) );
- // Cleanup.
- update_post_meta( self::$large_id, '_wp_attachment_image_alt', '', true );
- }
- /**
- * @ticket 33878
- */
- function test_wp_get_attachment_image_url() {
- $this->assertFalse( wp_get_attachment_image_url( 0 ) );
- $post_id = self::factory()->post->create();
- $attachment_id = self::factory()->attachment->create_object(
- $this->img_name,
- $post_id,
- array(
- 'post_mime_type' => 'image/jpeg',
- 'post_type' => 'attachment',
- )
- );
- $image = wp_get_attachment_image_src( $attachment_id, 'thumbnail', false );
- $this->assertSame( $image[0], wp_get_attachment_image_url( $attachment_id ) );
- }
- /**
- * @ticket 12235
- */
- function test_wp_get_attachment_caption() {
- $this->assertFalse( wp_get_attachment_caption( 0 ) );
- $caption = 'This is a caption.';
- $post_id = self::factory()->post->create();
- $attachment_id = self::factory()->attachment->create_object(
- $this->img_name,
- $post_id,
- array(
- 'post_mime_type' => 'image/jpeg',
- 'post_type' => 'attachment',
- 'post_excerpt' => $caption,
- )
- );
- $this->assertFalse( wp_get_attachment_caption( $post_id ) );
- $this->assertSame( $caption, wp_get_attachment_caption( $attachment_id ) );
- }
- /**
- * @ticket 12235
- */
- function test_wp_get_attachment_caption_empty() {
- $post_id = self::factory()->post->create();
- $attachment_id = self::factory()->attachment->create_object(
- $this->img_name,
- $post_id,
- array(
- 'post_mime_type' => 'image/jpeg',
- 'post_type' => 'attachment',
- 'post_excerpt' => '',
- )
- );
- $this->assertSame( '', wp_get_attachment_caption( $attachment_id ) );
- }
- /**
- * Helper function to get image size array from size "name".
- */
- function _get_image_size_array_from_meta( $image_meta, $size_name ) {
- $array = false;
- if ( is_array( $image_meta ) ) {
- if ( 'full' === $size_name && isset( $image_meta['width'] ) && isset( $image_meta['height'] ) ) {
- $array = array( $image_meta['width'], $image_meta['height'] );
- } elseif ( isset( $image_meta['sizes'][ $size_name ]['width'] ) && isset( $image_meta['sizes'][ $size_name ]['height'] ) ) {
- $array = array( $image_meta['sizes'][ $size_name ]['width'], $image_meta['sizes'][ $size_name ]['height'] );
- }
- }
- if ( ! $array ) {
- $this->fail( sprintf( "Could not retrieve image metadata for size '%s'.", $size_name ) );
- }
- return $array;
- }
- /**
- * Helper function to move the src image to the first position in the expected srcset string.
- */
- function _src_first( $srcset, $src_url, $src_width ) {
- $src_string = $src_url . ' ' . $src_width . 'w';
- $src_not_first = ', ' . $src_string;
- if ( strpos( $srcset, $src_not_first ) ) {
- $srcset = str_replace( $src_not_first, '', $srcset );
- $srcset = $src_string . ', ' . $srcset;
- }
- return $srcset;
- }
- /**
- * @ticket 33641
- * @requires function imagejpeg
- */
- function test_wp_calculate_image_srcset() {
- $_wp_additional_image_sizes = wp_get_additional_image_sizes();
- $year_month = gmdate( 'Y/m' );
- $image_meta = wp_get_attachment_metadata( self::$large_id );
- $uploads_dir_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/';
- // Set up test cases for all expected size names.
- $intermediates = array( 'medium', 'medium_large', 'large', 'full' );
- // Add any soft crop intermediate sizes.
- foreach ( $_wp_additional_image_sizes as $name => $additional_size ) {
- if ( ! $_wp_additional_image_sizes[ $name ]['crop'] || 0 === $_wp_additional_image_sizes[ $name ]['height'] ) {
- $intermediates[] = $name;
- }
- }
- $expected = '';
- foreach ( $image_meta['sizes'] as $name => $size ) {
- // Allow the sizes that should be included so we pick up 'medium_large' in 4.4.
- if ( in_array( $name, $intermediates, true ) ) {
- $expected .= $uploads_dir_url . $year_month . '/' . $size['file'] . ' ' . $size['width'] . 'w, ';
- }
- }
- $expected = trim( $expected, ' ,' );
- foreach ( $intermediates as $int ) {
- $image_url = wp_get_attachment_image_url( self::$large_id, $int );
- $size_array = $this->_get_image_size_array_from_meta( $image_meta, $int );
- if ( 'full' === $int ) {
- // Add the full size image. Expected to be in the srcset when the full size image is used as src.
- $_expected = $uploads_dir_url . $image_meta['file'] . ' ' . $image_meta['width'] . 'w, ' . $expected;
- } else {
- $_expected = $expected;
- }
- $expected_srcset = $this->_src_first( $_expected, $image_url, $size_array[0] );
- $this->assertSame( $expected_srcset, wp_calculate_image_srcset( $size_array, $image_url, $image_meta ) );
- }
- }
- /**
- * @ticket 33641
- * @requires function imagejpeg
- */
- function test_wp_calculate_image_srcset_no_date_uploads() {
- $_wp_additional_image_sizes = wp_get_additional_image_sizes();
- // Disable date organized uploads.
- add_filter( 'upload_dir', '_upload_dir_no_subdir' );
- // Make an image.
- $filename = DIR_TESTDATA . '/images/' . self::$large_filename;
- $id = self::factory()->attachment->create_upload_object( $filename );
- $image_meta = wp_get_attachment_metadata( $id );
- $uploads_dir_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/';
- // Set up test cases for all expected size names.
- $intermediates = array( 'medium', 'medium_large', 'large', 'full' );
- foreach ( $_wp_additional_image_sizes as $name => $additional_size ) {
- if ( ! $_wp_additional_image_sizes[ $name ]['crop'] || 0 === $_wp_additional_image_sizes[ $name ]['height'] ) {
- $intermediates[] = $name;
- }
- }
- $expected = '';
- foreach ( $image_meta['sizes'] as $name => $size ) {
- // Allow the sizes that should be included so we pick up 'medium_large' in 4.4.
- if ( in_array( $name, $intermediates, true ) ) {
- $expected .= $uploads_dir_url . $size['file'] . ' ' . $size['width'] . 'w, ';
- }
- }
- $expected = trim( $expected, ' ,' );
- foreach ( $intermediates as $int ) {
- $size_array = $this->_get_image_size_array_from_meta( $image_meta, $int );
- $image_url = wp_get_attachment_image_url( $id, $int );
- if ( 'full' === $int ) {
- // Add the full size image. Expected to be in the srcset when the full size image is used as src.
- $_expected = $uploads_dir_url . $image_meta['file'] . ' ' . $image_meta['width'] . 'w, ' . $expected;
- } else {
- $_expected = $expected;
- }
- $expected_srcset = $this->_src_first( $_expected, $image_url, $size_array[0] );
- $this->assertSame( $expected_srcset, wp_calculate_image_srcset( $size_array, $image_url, $image_meta ) );
- }
- // Remove the attachment.
- wp_delete_attachment( $id );
- remove_filter( 'upload_dir', '_upload_dir_no_subdir' );
- }
- /**
- * @ticket 33641
- * @requires function imagejpeg
- */
- function test_wp_calculate_image_srcset_with_edits() {
- // For this test we're going to mock metadata changes from an edit.
- // Start by getting the attachment metadata.
- $image_meta = wp_get_attachment_metadata( self::$large_id );
- $image_url = wp_get_attachment_image_url( self::$large_id, 'medium' );
- $size_array = $this->_get_image_size_array_from_meta( $image_meta, 'medium' );
- // Copy hash generation method used in wp_save_image().
- $hash = 'e' . time() . rand( 100, 999 );
- $filename_base = wp_basename( self::$large_filename, '.jpg' );
- $filename_hash = "{$filename_base}-{$hash}";
- // Add the hash to the image URL.
- $image_url = str_replace( $filename_base, $filename_hash, $image_url );
- // Replace file paths for full and medium sizes with hashed versions.
- $image_meta['sizes']['medium']['file'] = str_replace( $filename_base, $filename_hash, $image_meta['sizes']['medium']['file'] );
- $image_meta['sizes']['medium_large']['file'] = str_replace( $filename_base, $filename_hash, $image_meta['sizes']['medium_large']['file'] );
- $image_meta['sizes']['large']['file'] = str_replace( $filename_base, $filename_hash, $image_meta['sizes']['large']['file'] );
- // Calculate a srcset array.
- $sizes = explode( ', ', wp_calculate_image_srcset( $size_array, $image_url, $image_meta ) );
- // Test to confirm all sources in the array include the same edit hash.
- foreach ( $sizes as $size ) {
- $this->assertNotFalse( strpos( $size, $hash ) );
- }
- }
- /**
- * @ticket 35106
- * @requires function imagejpeg
- */
- function test_wp_calculate_image_srcset_with_absolute_path_in_meta() {
- $_wp_additional_image_sizes = wp_get_additional_image_sizes();
- $year_month = gmdate( 'Y/m' );
- $image_meta = wp_get_attachment_metadata( self::$large_id );
- $uploads_dir_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/';
- // Set up test cases for all expected size names.
- $intermediates = array( 'medium', 'medium_large', 'large', 'full' );
- // Add any soft crop intermediate sizes.
- foreach ( $_wp_additional_image_sizes as $name => $additional_size ) {
- if ( ! $_wp_additional_image_sizes[ $name ]['crop'] || 0 === $_wp_additional_image_sizes[ $name ]['height'] ) {
- $intermediates[] = $name;
- }
- }
- $expected = '';
- foreach ( $image_meta['sizes'] as $name => $size ) {
- // Allow the sizes that should be included so we pick up 'medium_large' in 4.4.
- if ( in_array( $name, $intermediates, true ) ) {
- $expected .= $uploads_dir_url . $year_month . '/' . $size['file'] . ' ' . $size['width'] . 'w, ';
- }
- }
- $expected = trim( $expected, ' ,' );
- $full_size_file = $image_meta['file'];
- // Prepend an absolute path to simulate a pre-2.7 upload.
- $image_meta['file'] = 'H:\home\wordpress\trunk/wp-content/uploads/' . $image_meta['file'];
- foreach ( $intermediates as $int ) {
- $image_url = wp_get_attachment_image_url( self::$large_id, $int );
- $size_array = $this->_get_image_size_array_from_meta( $image_meta, $int );
- if ( 'full' === $int ) {
- // Add the full size image. Expected to be in the srcset when the full size image is used as src.
- $_expected = $uploads_dir_url . $full_size_file . ' ' . $image_meta['width'] . 'w, ' . $expected;
- } else {
- $_expected = $expected;
- }
- $expected_srcset = $this->_src_first( $_expected, $image_url, $size_array[0] );
- $this->assertSame( $expected_srcset, wp_calculate_image_srcset( $size_array, $image_url, $image_meta ) );
- }
- }
- /**
- * @ticket 33641
- */
- function test_wp_calculate_image_srcset_false() {
- $sizes = wp_calculate_image_srcset( array( 400, 300 ), 'file.png', array() );
- // For canola.jpg we should return.
- $this->assertFalse( $sizes );
- }
- /**
- * @ticket 33641
- */
- function test_wp_calculate_image_srcset_no_width() {
- $file = get_attached_file( self::$large_id );
- $image_url = wp_get_attachment_image_url( self::$large_id, 'medium' );
- $image_meta = wp_generate_attachment_metadata( self::$large_id, $file );
- $size_array = array( 0, 0 );
- $srcset = wp_calculate_image_srcset( $size_array, $image_url, $image_meta );
- // The srcset should be false.
- $this->assertFalse( $srcset );
- }
- /**
- * @ticket 34955
- * @ticket 33641
- */
- function test_wp_calculate_image_srcset_ratio_variance() {
- // Mock data for this test.
- $size_array = array( 218, 300 );
- $image_src = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-768x1055-218x300.png';
- $image_meta = array(
- 'width' => 768,
- 'height' => 1055,
- 'file' => '2015/12/test-768x1055.png',
- 'sizes' => array(
- 'thumbnail' => array(
- 'file' => 'test-768x1055-150x150.png',
- 'width' => 150,
- 'height' => 150,
- 'mime-type' => 'image/png',
- ),
- 'medium' => array(
- 'file' => 'test-768x1055-218x300.png',
- 'width' => 218,
- 'height' => 300,
- 'mime-type' => 'image/png',
- ),
- 'custom-600' => array(
- 'file' => 'test-768x1055-600x824.png',
- 'width' => 600,
- 'height' => 824,
- 'mime-type' => 'image/png',
- ),
- 'post-thumbnail' => array(
- 'file' => 'test-768x1055-768x510.png',
- 'width' => 768,
- 'height' => 510,
- 'mime-type' => 'image/png',
- ),
- ),
- );
- $uploads_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/';
- $expected_srcset = $uploads_url . 'test-768x1055-218x300.png 218w, ' .
- $uploads_url . 'test-768x1055-600x824.png 600w, ' .
- $uploads_url . 'test-768x1055.png 768w';
- $this->assertSame( $expected_srcset, wp_calculate_image_srcset( $size_array, $image_src, $image_meta ) );
- }
- /**
- * @ticket 35108
- * @ticket 33641
- */
- function test_wp_calculate_image_srcset_include_src() {
- // Mock data for this test.
- $size_array = array( 2000, 1000 );
- $image_src = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test.png';
- $image_meta = array(
- 'width' => 2000,
- 'height' => 1000,
- 'file' => '2015/12/test.png',
- 'sizes' => array(
- 'thumbnail' => array(
- 'file' => 'test-150x150.png',
- 'width' => 150,
- 'height' => 150,
- 'mime-type' => 'image/png',
- ),
- 'medium' => array(
- 'file' => 'test-300x150.png',
- 'width' => 300,
- 'height' => 150,
- 'mime-type' => 'image/png',
- ),
- 'medium_large' => array(
- 'file' => 'test-768x384.png',
- 'width' => 768,
- 'height' => 384,
- 'mime-type' => 'image/png',
- ),
- 'large' => array(
- 'file' => 'test-1024x512.png',
- 'width' => 1024,
- 'height' => 512,
- 'mime-type' => 'image/png',
- ),
- ),
- );
- $uploads_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/';
- $expected_srcset = $uploads_url . 'test.png 2000w, ' .
- $uploads_url . 'test-300x150.png 300w, ' .
- $uploads_url . 'test-768x384.png 768w, ' .
- $uploads_url . 'test-1024x512.png 1024w';
- $this->assertSame( $expected_srcset, wp_calculate_image_srcset( $size_array, $image_src, $image_meta ) );
- }
- /**
- * @ticket 35480
- */
- function test_wp_calculate_image_srcset_corrupted_image_meta() {
- $size_array = array( 300, 150 );
- $image_src = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-300x150.png';
- $image_meta = array(
- 'width' => 1600,
- 'height' => 800,
- 'file' => '2015/12/test.png',
- 'sizes' => array(
- 'thumbnail' => array(
- 'file' => 'test-150x150.png',
- 'width' => 150,
- 'height' => 150,
- 'mime-type' => 'image/png',
- ),
- 'medium' => array(
- 'file' => 'test-300x150.png',
- 'width' => 300,
- 'height' => 150,
- 'mime-type' => 'image/png',
- ),
- 'medium_large' => array(
- 'file' => 'test-768x384.png',
- 'width' => 768,
- 'height' => 384,
- 'mime-type' => 'image/png',
- ),
- 'large' => array(
- 'file' => 'test-1024x512.png',
- 'width' => 1024,
- 'height' => 512,
- 'mime-type' => 'image/png',
- ),
- ),
- );
- $srcset = array(
- 300 => 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-300x150.png 300w',
- 768 => 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-768x384.png 768w',
- 1024 => 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-1024x512.png 1024w',
- 1600 => 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test.png 1600w',
- );
- // No sizes array.
- $image_meta1 = $image_meta;
- unset( $image_meta1['sizes'] );
- $this->assertFalse( wp_calculate_image_srcset( $size_array, $image_src, $image_meta1 ) );
- // Sizes is string instead of array; only full size available means no srcset.
- $image_meta2 = $image_meta;
- $image_meta2['sizes'] = '';
- $this->assertFalse( wp_calculate_image_srcset( $size_array, $image_src, $image_meta2 ) );
- // File name is incorrect.
- $image_meta3 = $image_meta;
- $image_meta3['file'] = '/';
- $this->assertFalse( wp_calculate_image_srcset( $size_array, $image_src, $image_meta3 ) );
- // File name is incorrect.
- $image_meta4 = $image_meta;
- unset( $image_meta4['file'] );
- $this->assertFalse( wp_calculate_image_srcset( $size_array, $image_src, $image_meta4 ) );
- // Intermediate size is string instead of array.
- $image_meta5 = $image_meta;
- $image_meta5['sizes']['medium_large'] = '';
- unset( $srcset[768] );
- $expected_srcset = implode( ', ', $srcset );
- $this->assertSame( $expected_srcset, wp_calculate_image_srcset( $size_array, $image_src, $image_meta5 ) );
- }
- /**
- * @ticket 36549
- * @ticket 33641
- */
- function test_wp_calculate_image_srcset_with_spaces_in_filenames() {
- // Mock data for this test.
- $image_src = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test image-300x150.png';
- $image_meta = array(
- 'width' => 3000,
- 'height' => 1500,
- 'file' => '2015/12/test image.png',
- 'sizes' => array(
- 'thumbnail' => array(
- 'file' => 'test image-150x150.png',
- 'width' => 150,
- 'height' => 150,
- 'mime-type' => 'image/png',
- ),
- 'medium' => array(
- 'file' => 'test image-300x150.png',
- 'width' => 300,
- 'height' => 150,
- 'mime-type' => 'image/png',
- ),
- 'medium_large' => array(
- 'file' => 'test image-768x384.png',
- 'width' => 768,
- 'height' => 384,
- 'mime-type' => 'image/png',
- ),
- 'large' => array(
- 'file' => 'test image-1024x512.png',
- 'width' => 1024,
- 'height' => 512,
- 'mime-type' => 'image/png',
- ),
- ),
- );
- $uploads_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/';
- $expected_srcset = $uploads_url . 'test%20image-300x150.png 300w, ' .
- $uploads_url . 'test%20image-768x384.png 768w, ' .
- $uploads_url . 'test%20image-1024x512.png 1024w';
- $this->assertSame( $expected_srcset, wp_calculate_image_srcset( array( 300, 150 ), $image_src, $image_meta ) );
- }
- /**
- * @ticket 33641
- * @requires function imagejpeg
- */
- function test_wp_get_attachment_image_srcset() {
- $_wp_additional_image_sizes = wp_get_additional_image_sizes();
- $image_meta = wp_get_attachment_metadata( self::$large_id );
- $size_array = array( $image_meta['width'], $image_meta['height'] ); // Full size.
- $srcset = wp_get_attachment_image_srcset( self::$large_id, $size_array, $image_meta );
- $year_month = gmdate( 'Y/m' );
- $uploads_dir = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/';
- // Set up test cases for all expected size names.
- $intermediates = array( 'medium', 'medium_large', 'large', 'full' );
- foreach ( $_wp_additional_image_sizes as $name => $additional_size ) {
- if ( ! $_wp_additional_image_sizes[ $name ]['crop'] || 0 === $_wp_additional_image_sizes[ $name ]['height'] ) {
- $intermediates[] = $name;
- }
- }
- $expected = '';
- foreach ( $image_meta['sizes'] as $name => $size ) {
- // Allow the sizes that should be included so we pick up 'medium_large' in 4.4.
- if ( in_array( $name, $intermediates, true ) ) {
- $expected .= $uploads_dir . $year_month . '/' . $size['file'] . ' ' . $size['width'] . 'w, ';
- }
- }
- $expected .= $uploads_dir . $image_meta['file'] . ' ' . $image_meta['width'] . 'w';
- $expected_srcset = $this->_src_first( $expected, $uploads_dir . $image_meta['file'], $size_array[0] );
- $this->assertSame( $expected_srcset, $srcset );
- }
- /**
- * @ticket 33641
- */
- function test_wp_get_attachment_image_srcset_single_srcset() {
- $image_meta = wp_get_attachment_metadata( self::$large_id );
- $size_array = array( 150, 150 );
- /*
- * In our tests, thumbnails will only return a single srcset candidate,
- * so we shouldn't return a srcset value in order to avoid unneeded markup.
- */
- $sizes = wp_get_attachment_image_srcset( self::$large_id, $size_array, $image_meta );
- $this->assertFalse( $sizes );
- }
- /**
- * @ticket 33641
- */
- function test_wp_get_attachment_image_srcset_invalidsize() {
- $image_meta = wp_get_attachment_metadata( self::$large_id );
- $invalid_size = 'nailthumb';
- $original_size = array( 1600, 1200 );
- $srcset = wp_get_attachment_image_srcset( self::$large_id, $invalid_size, $image_meta );
- // Expect a srcset for the original full size image to be returned.
- $expected = wp_get_attachment_image_srcset( self::$large_id, $original_size, $image_meta );
- $this->assertSame( $expected, $srcset );
- }
- /**
- * @ticket 33641
- */
- function test_wp_get_attachment_image_sizes() {
- // Test sizes against the default WP sizes.
- $intermediates = array( 'thumbnail', 'medium', 'medium_large', 'large' );
- // Make sure themes aren't filtering the sizes array.
- remove_all_filters( 'wp_calculate_image_sizes' );
- foreach ( $intermediates as $int_size ) {
- $image = wp_get_attachment_image_src( self::$large_id, $int_size );
- $expected = '(max-width: ' . $image[1] . 'px) 100vw, ' . $image[1] . 'px';
- $sizes = wp_get_attachment_image_sizes( self::$large_id, $int_size );
- $this->assertSame( $expected, $sizes );
- }
- }
- /**
- * @ticket 33641
- * @requires function imagejpeg
- */
- function test_wp_calculate_image_sizes() {
- // Test sizes against the default WP sizes.
- $intermediates = array( 'thumbnail', 'medium', 'medium_large', 'large' );
- $image_meta = wp_get_attachment_metadata( self::$large_id );
- // Make sure themes aren't filtering the sizes array.
- remove_all_filters( 'wp_calculate_image_sizes' );
- foreach ( $intermediates as $int_size ) {
- $size_array = $this->_get_image_size_array_from_meta( $image_meta, $int_size );
- $image_src = $image_meta['sizes'][ $int_size ]['file'];
- list( $width, $height ) = $size_array;
- $expected = '(max-width: ' . $width . 'px) 100vw, ' . $width . 'px';
- $sizes = wp_calculate_image_sizes( $size_array, $image_src, $image_meta );
- $this->assertSame( $expected, $sizes );
- }
- }
- /**
- * @ticket 33641
- * @requires function imagejpeg
- */
- function test_wp_filter_content_tags_srcset_sizes() {
- $image_meta = wp_get_attachment_metadata( self::$large_id );
- $size_array = $this->_get_image_size_array_from_meta( $image_meta, 'medium' );
- $srcset = sprintf( 'srcset="%s"', wp_get_attachment_image_srcset( self::$large_id, $size_array, $image_meta ) );
- $sizes = sprintf( 'sizes="%s"', wp_get_attachment_image_sizes( self::$large_id, $size_array, $image_meta ) );
- // Function used to build HTML for the editor.
- $img = get_image_tag( self::$large_id, '', '', '', 'medium' );
- $img_no_size_in_class = str_replace( 'size-', '', $img );
- $img_no_width_height = str_replace( ' width="' . $size_array[0] . '"', '', $img );
- $img_no_width_height = str_replace( ' height="' . $size_array[1] . '"', '', $img_no_width_height );
- $img_no_size_id = str_replace( 'wp-image-', 'id-', $img );
- $img_with_sizes_attr = str_replace( '<img ', '<img sizes="99vw" ', $img );
- $img_xhtml = str_replace( ' />', '/>', $img );
- $img_html5 = str_replace( ' />', '>', $img );
- // Manually add srcset and sizes to the markup from get_image_tag().
- $respimg = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img );
- $respimg_no_size_in_class = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img_no_size_in_class );
- $respimg_no_width_height = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img_no_width_height );
- $respimg_with_sizes_attr = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . $srcset . ' />', $img_with_sizes_attr );
- $respimg_xhtml = preg_replace( '|<img ([^>]+)/>|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img_xhtml );
- $respimg_html5 = preg_replace( '|<img ([^>]+)>|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img_html5 );
- $content = '
- <p>Image, standard. Should have srcset and sizes.</p>
- %1$s
- <p>Image, no size class. Should have srcset and sizes.</p>
- %2$s
- <p>Image, no width and height attributes. Should have srcset and sizes (from matching the file name).</p>
- %3$s
- <p>Image, no attachment ID class. Should NOT have srcset and sizes.</p>
- %4$s
- <p>Image, with sizes attribute. Should NOT have two sizes attributes.</p>
- %5$s
- <p>Image, XHTML 1.0 style (no space before the closing slash). Should have srcset and sizes.</p>
- %6$s
- <p>Image, HTML 5.0 style. Should have srcset and sizes.</p>
- %7$s';
- $content_unfiltered = sprintf(
- $content,
- $img,
- $img_no_size_in_class,
- $img_no_width_height,
- $img_no_size_id,
- $img_with_sizes_attr,
- $img_xhtml,
- $img_html5
- );
- $content_filtered = sprintf(
- $content,
- $respimg,
- $respimg_no_size_in_class,
- $respimg_no_width_height,
- $img_no_size_id,
- $respimg_with_sizes_attr,
- $respimg_xhtml,
- $respimg_html5
- );
- // Do not add width, height, and loading.
- add_filter( 'wp_img_tag_add_width_and_height_attr', '__return_false' );
- add_filter( 'wp_img_tag_add_loading_attr', '__return_false' );
- $this->assertSame( $content_filtered, wp_filter_content_tags( $content_unfiltered ) );
- remove_filter( 'wp_img_tag_add_width_and_height_attr', '__return_false' );
- remove_filter( 'wp_img_tag_add_loading_attr', '__return_false' );
- }
- /**
- * When rendering attributes for responsive images,
- * we rely on the 'wp-image-*' class to find the image by ID.
- * The class name may not be consistent with attachment IDs in DB when
- * working with imported content or when a user has edited
- * the 'src' attribute manually. To avoid incorrect images
- * being displayed, ensure we don't add attributes in this case.
- *
- * @ticket 34898
- * @ticket 33641
- */
- function test_wp_filter_content_tags_srcset_sizes_wrong() {
- $img = get_image_tag( self::$large_id, '', '', '', 'medium' );
- $img = wp_img_tag_add_loading_attr( $img, 'test' );
- // Replace the src URL.
- $image_wrong_src = preg_replace( '|src="[^"]+"|', 'src="http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/foo.jpg"', $img );
- $this->assertSame( $image_wrong_src, wp_filter_content_tags( $image_wrong_src ) );
- }
- /**
- * @ticket 33641
- */
- function test_wp_filter_content_tags_srcset_sizes_with_preexisting_srcset() {
- // Generate HTML and add a dummy srcset attribute.
- $img = get_image_tag( self::$large_id, '', '', '', 'medium' );
- $img = wp_img_tag_add_loading_attr( $img, 'test' );
- $img = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . 'srcset="image2x.jpg 2x" />', $img );
- // The content filter should return the image unchanged.
- $this->assertSame( $img, wp_filter_content_tags( $img ) );
- }
- /**
- * @ticket 33641
- * @ticket 34528
- */
- function test_wp_calculate_image_srcset_animated_gifs() {
- // Mock meta for an animated gif.
- $image_meta = array(
- 'width' => 1200,
- 'height' => 600,
- 'file' => 'animated.gif',
- 'sizes' => array(
- 'thumbnail' => array(
- 'file' => 'animated-150x150.gif',
- 'width' => 150,
- 'height' => 150,
- 'mime-type' => 'image/gif',
- ),
- 'medium' => array(
- 'file' => 'animated-300x150.gif',
- 'width' => 300,
- 'height' => 150,
- 'mime-type' => 'image/gif',
- ),
- 'large' => array(
- 'file' => 'animated-1024x512.gif',
- 'width' => 1024,
- 'height' => 512,
- 'mime-type' => 'image/gif',
- ),
- ),
- );
- $full_src = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_meta['file'];
- $large_src = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_meta['sizes']['large']['file'];
- // Test with soft resized size array.
- $size_array = array( 900, 450 );
- // Full size GIFs should not return a srcset.
- $this->assertFalse( wp_calculate_image_srcset( $size_array, $full_src, $image_meta ) );
- // Intermediate sized GIFs should not include the full size in the srcset.
- $this->assertFalse( strpos( wp_calculate_image_srcset( $size_array, $large_src, $image_meta ), $full_src ) );
- }
- /**
- * @ticket 35045
- * @ticket 33641
- * @requires function imagejpeg
- */
- function test_wp_filter_content_tags_schemes() {
- $image_meta = wp_get_attachment_metadata( self::$large_id );
- $size_array = $this->_get_image_size_array_from_meta( $image_meta, 'medium' );
- $srcset = sprintf( 'srcset="%s"', wp_get_attachment_image_srcset( self::$large_id, $size_array, $image_meta ) );
- $sizes = sprintf( 'sizes="%s"', wp_get_attachment_image_sizes( self::$large_id, $size_array, $image_meta ) );
- // Build HTML for the editor.
- $img = get_image_tag( self::$large_id, '', '', '', 'medium' );
- $img = wp_img_tag_add_loading_attr( $img, 'test' );
- $img_https = str_replace( 'http://', 'https://', $img );
- $img_relative = str_replace( 'http://', '//', $img );
- // Manually add srcset and sizes to the markup from get_image_tag().
- $respimg = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img );
- $respimg_https = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img_https );
- $respimg_relative = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img_relative );
- $content = '
- <p>Image, http: protocol. Should have srcset and sizes.</p>
- %1$s
- <p>Image, https: protocol. Should have srcset and sizes.</p>
- %2$s
- <p>Image, protocol-relative. Should have srcset and sizes.</p>
- %3$s';
- $unfiltered = sprintf(
- $content,
- $img,
- $img_https,
- $img_relative
- );
- $expected = sprintf(
- $content,
- $respimg,
- $respimg_https,
- $respimg_relative
- );
- $actual = wp_filter_content_tags( $unfiltered );
- $this->assertSame( $expected, $actual );
- }
- /**
- * @ticket 34945
- * @ticket 33641
- */
- function test_wp_get_attachment_image_with_https_on() {
- // Mock meta for the image.
- $image_meta = array(
- 'width' => 1200,
- 'height' => 600,
- 'file' => 'test.jpg',
- 'sizes' => array(
- 'thumbnail' => array(
- 'file' => 'test-150x150.jpg',
- 'width' => 150,
- 'height' => 150,
- ),
- 'medium' => array(
- 'file' => 'test-300x150.jpg',
- 'width' => 300,
- 'height' => 150,
- ),
- 'large' => array(
- 'file' => 'test-1024x512.jpg',
- 'width' => 1024,
- 'height' => 512,
- ),
- ),
- );
- // Test using the large file size.
- $size_array = array( 1024, 512 );
- $image_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_meta['sizes']['large']['file'];
- $_SERVER['HTTPS'] = 'on';
- $uploads_url = 'https://' . WP_TESTS_DOMAIN . '/wp-content/uploads/';
- $expected = $uploads_url . 'test-1024x512.jpg 1024w, ' .
- $uploads_url . 'test-300x150.jpg 300w, ' .
- $uploads_url . 'test.jpg 1200w';
- $actual = wp_calculate_image_srcset( $size_array, $image_url, $image_meta );
- $this->assertSame( $expected, $actual );
- }
- /**
- * @ticket 36084
- */
- function test_get_image_send_to_editor_defaults() {
- $id = self::$large_id;
- $caption = '';
- $title = 'A test title value.';
- $align = 'left';
- // Calculate attachment data (default is medium).
- $attachment = wp_get_attachment_image_src( $id, 'medium' );
- $html = '<img src="%1$s" alt="" width="%2$d" height="%3$d" class="align%4$s size-medium wp-image-%5$d" />';
- $expected = sprintf( $html, $attachment[0], $attachment[1], $attachment[2], $align, $id );
- $this->assertSame( $expected, get_image_send_to_editor( $id, $caption, $title, $align ) );
- $this->assertSame( $expected, get_image_send_to_editor( $id, $caption, $title, $align ) );
- }
- /**
- * @ticket 36084
- */
- function test_get_image_send_to_editor_defaults_with_optional_params() {
- $id = self::$large_id;
- $caption = 'A test caption.';
- $title = 'A test title value.';
- $align = 'left';
- $url = get_permalink( $id );
- $rel = true;
- $size = 'thumbnail';
- $alt = 'An example alt value.';
- // Calculate attachment data.
- $attachment = wp_get_attachment_image_src( $id, $size );
- $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>';
- $html = '[caption id="attachment_%9$d" align="align%7$s" width="%5$d"]' . $html . ' %10$s[/caption]';
- $expected = sprintf( $html, $url, 'attachment wp-att-' . $id, $attachment[0], $alt, $attachment[1], $attachment[2], $align, $size, $id, $caption );
- $this->assertSame( $expected, get_image_send_to_editor( $id, $caption, $title, $align, $url, $rel, $size, $alt ) );
- }
- /**
- * @ticket 36084
- */
- function test_get_image_send_to_editor_defaults_no_caption_no_rel() {
- $id = self::$large_id;
- $caption = '';
- $title = 'A test title value.';
- $align = 'left';
- $url = get_permalink( $id );
- $rel = '';
- $size = 'thumbnail';
- $alt = 'An example alt value.';
- // Calculate attachment data.
- $attachment = wp_get_attachment_image_src( $id, $size );
- $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>';
- $expected = sprintf( $html, $url, $attachment[0], $alt, $attachment[1], $attachment[2], $align, $size, $id );
- $this->assertSame( $expected, get_image_send_to_editor( $id, $caption, $title, $align, $url, $rel, $size, $alt ) );
- }
- /**
- * Tests if wp_get_attachment_image() uses wp_get_attachment_metadata().
- *
- * In this way, the meta data can be filtered using the filter
- * `wp_get_attachment_metadata`.
- *
- * The test checks if the image size that is added in the filter is
- * used in the output of `wp_get_attachment_image()`.
- *
- * @ticket 36246
- * @requires function imagejpeg
- */
- function test_wp_get_attachment_image_should_use_wp_get_attachment_metadata() {
- add_filter( 'wp_get_attachment_metadata', array( $this, '_filter_36246' ), 10, 2 );
- remove_all_filters( 'wp_calculate_image_sizes' );
- $basename = wp_basename( self::$large_filename, '.jpg' );
- $year_month = gmdate( 'Y/m' );
- $uploads_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $year_month . '/';
- $expected = '<img width="999" height="999" ' .
- 'src="' . $uploads_url . 'test-image-testsize-999x999.jpg" ' .
- 'class="attachment-testsize size-testsize" alt="" loading="lazy" ' .
- 'srcset="' . $uploads_url . 'test-image-testsize-999x999.jpg 999w, ' . $uploads_url . $basename . '-150x150.jpg 150w" ' .
- 'sizes="(max-width: 999px) 100vw, 999px" />';
- $actual = wp_get_attachment_image( self::$large_id, 'testsize' );
- remove_filter( 'wp_get_attachment_metadata', array( $this, '_filter_36246' ) );
- $this->assertSame( $expected, $actual );
- }
- function _filter_36246( $data, $attachment_id ) {
- $data['sizes']['testsize'] = array(
- 'file' => 'test-image-testsize-999x999.jpg',
- 'width' => 999,
- 'height' => 999,
- 'mime-type' => 'image/jpg',
- );
- return $data;
- }
- /**
- * @ticket 50679
- */
- function test_wp_get_attachment_metadata_should_return_false_if_no_attachment() {
- $post_id = self::factory()->post->create();
- $data = wp_get_attachment_metadata( $post_id );
- $this->assertFalse( $data );
- }
- /**
- * @ticket 37813
- */
- public function test_return_type_when_inserting_attachment_with_error_in_data() {
- $data = array(
- 'post_status' => 'publish',
- 'post_content' => 'Attachment content',
- 'post_title' => 'Attachment Title',
- 'post_date' => '2012-02-30 00:00:00',
- );
- $attachment_id = wp_insert_attachment( $data, '', 0, true );
- $this->assertWPError( $attachment_id );
- $this->assertSame( 'invalid_date', $attachment_id->get_error_code() );
- $attachment_id = wp_insert_attachment( $data, '', 0 );
- $this->assertSame( 0, $attachment_id );
- }
- /**
- * @ticket 35218
- */
- function test_wp_get_media_creation_timestamp_video_asf() {
- $metadata = array(
- 'fileformat' => 'asf',
- 'asf' => array(
- 'file_properties_object' => array(
- 'creation_date_unix' => 123,
- ),
- ),
- );
- $this->assertSame( 123, wp_get_media_creation_timestamp( $metadata ) );
- }
- /**
- * @ticket 35218
- */
- function test_wp_get_media_creation_timestamp_video_matroska() {
- $metadata = array(
- 'fileformat' => 'matroska',
- 'matroska' => array(
- 'comments' => array(
- 'creation_time' => array(
- '2015-12-24T17:40:09Z',
- ),
- ),
- ),
- );
- $this->assertSame( 1450978809, wp_get_media_creation_timestamp( $metadata ) );
- }
- /**
- * @ticket 35218
- */
- function test_wp_get_media_creation_timestamp_video_quicktime() {
- $metadata = array(
- 'fileformat' => 'quicktime',
- 'quicktime' => array(
- 'moov' => array(
- 'subatoms' => array(
- array(
- 'creation_time_unix' => 1450978805,
- ),
- ),
- ),
- ),
- );
- $this->assertSame( 1450978805, wp_get_media_creation_timestamp( $metadata ) );
- }
- /**
- * @ticket 35218
- */
- function test_wp_get_media_creation_timestamp_video_webm() {
- $metadata = array(
- 'fileformat' => 'webm',
- 'matroska' => array(
- 'info' => array(
- array(
- 'DateUTC_unix' => 1265680539,
- ),
- ),
- ),
- );
- $this->assertSame( 1265680539, wp_get_media_creation_timestamp( $metadata ) );
- }
- /**
- * Test created timestamp is properly read from an MP4 file.
- *
- * This MP4 video file has an AAC audio track, so it can be used to test
- *`wp_read_audio_metadata()`.
- *
- * @ticket 42017
- */
- function test_wp_read_audio_metadata_adds_creation_date_with_mp4() {
- $video = DIR_TESTDATA . '/uploads/small-video.mp4';
- $metadata = wp_read_audio_metadata( $video );
- $this->assertSame( 1269120551, $metadata['created_timestamp'] );
- }
- /**
- * @ticket 35218
- */
- function test_wp_read_video_metadata_adds_creation_date_with_quicktime() {
- $video = DIR_TESTDATA . '/uploads/small-video.mov';
- $metadata = wp_read_video_metadata( $video );
- $this->assertSame( 1269120551, $metadata['created_timestamp'] );
- }
- /**
- * @ticket 35218
- */
- function test_wp_read_video_metadata_adds_creation_date_with_mp4() {
- $video = DIR_TESTDATA . '/uploads/small-video.mp4';
- $metadata = wp_read_video_metadata( $video );
- $this->assertSame( 1269120551, $metadata['created_timestamp'] );
- }
- /**
- * @ticket 35218
- */
- function test_wp_read_video_metadata_adds_creation_date_with_mkv() {
- $video = DIR_TESTDATA . '/uploads/small-video.mkv';
- $metadata = wp_read_video_metadata( $video );
- $this->assertSame( 1269120551, $metadata['created_timestamp'] );
- }
- /**
- * @ticket 35218
- */
- function test_wp_read_video_metadata_adds_creation_date_with_webm() {
- $video = DIR_TESTDATA . '/uploads/small-video.webm';
- $metadata = wp_read_video_metadata( $video );
- $this->assertSame( 1269120551, $metadata['created_timestamp'] );
- }
- /**
- * @ticket 10752
- */
- public function test_media_handle_upload_uses_post_parent_for_directory_date() {
- $iptc_file = DIR_TESTDATA . '/images/test-image-iptc.jpg';
- // Make a copy of this file as it gets moved during the file upload.
- $tmp_name = wp_tempnam( $iptc_file );
- copy( $iptc_file, $tmp_name );
- $_FILES['upload'] = array(
- 'tmp_name' => $tmp_name,
- 'name' => 'test-image-iptc.jpg',
- 'type' => 'image/jpeg',
- 'error' => 0,
- 'size' => filesize( $iptc_file ),
- );
- $parent_id = self::factory()->post->create( array( 'post_date' => '2010-01-01' ) );
- $post_id = media_handle_upload(
- 'upload',
- $parent_id,
- array(),
- array(
- 'action' => 'test_iptc_upload',
- 'test_form' => false,
- )
- );
- unset( $_FILES['upload'] );
- $url = wp_get_attachment_url( $post_id );
- $uploads_dir = wp_upload_dir( '2010/01' );
- $expected = $uploads_dir['url'] . '/test-image-iptc.jpg';
- // Clean up.
- wp_delete_attachment( $post_id );
- wp_delete_post( $parent_id );
- $this->assertSame( $expected, $url );
- }
- /**
- * @ticket 10752
- */
- public function test_media_handle_upload_ignores_page_parent_for_directory_date() {
- $iptc_file = DIR_TESTDATA . '/images/test-image-iptc.jpg';
- // Make a copy of this file as it gets moved during the file upload.
- $tmp_name = wp_tempnam( $iptc_file );
- copy( $iptc_file, $tmp_name );
- $_FILES['upload'] = array(
- 'tmp_name' => $tmp_name,
- 'name' => 'test-image-iptc.jpg',
- 'type' => 'image/jpeg',
- 'error' => 0,
- 'size' => filesize( $iptc_file ),
- );
- $parent_id = self::factory()->post->create(
- array(
- 'post_date' => '2010-01-01',
- 'post_type' => 'page',
- )
- );
- $parent = get_post( $parent_id );
- $post_id = media_handle_upload(
- 'upload',
- $parent_id,
- array(),
- array(
- 'action' => 'test_iptc_upload',
- 'test_form' => false,
- )
- );
- unset( $_FILES['upload'] );
- $url = wp_get_attachment_url( $post_id );
- $uploads_dir = wp_upload_dir( current_time( 'mysql' ) );
- $expected = $uploads_dir['url'] . '/test-image-iptc.jpg';
- // Clean up.
- wp_delete_attachment( $post_id );
- wp_delete_post( $parent_id );
- $this->assertSame( $expected, $url );
- }
- /**
- * @ticket 50367
- * @requires function imagejpeg
- */
- function test_wp_filter_content_tags_width_height() {
- $image_meta = wp_get_attachment_metadata( self::$large_id );
- $size_array = $this->_get_image_size_array_from_meta( $image_meta, 'medium' );
- $img = get_image_tag( self::$large_id, '', '', '', 'medium' );
- $img_no_width_height = str_replace( ' width="' . $size_array[0] . '"', '', $img );
- $img_no_width_height = str_replace( ' height="' . $size_array[1] . '"', '', $img_no_width_height );
- $img_no_width = str_replace( ' width="' . $size_array[0] . '"', '', $img );
- $img_no_height = str_replace( ' height="' . $size_array[1] . '"', '', $img );
- $hwstring = image_hwstring( $size_array[0], $size_array[1] );
- // Manually add width and height to the markup from get_image_tag().
- $respimg_no_width_height = str_replace( '<img ', '<img ' . $hwstring, $img_no_width_height );
- $content = '
- <p>Image, with width and height. Should NOT be modified.</p>
- %1$s
- <p>Image, no width and height attributes. Should have width, height, srcset and sizes (from matching the file name).</p>
- %2$s
- <p>Image, no width but height attribute. Should NOT be modified.</p>
- %3$s
- <p>Image, no height but width attribute. Should NOT be modified.</p>
- %4$s';
- $content_unfiltered = sprintf( $content, $img, $img_no_width_height, $img_no_width, $img_no_height );
- $content_filtered = sprintf( $content, $img, $respimg_no_width_height, $img_no_width, $img_no_height );
- // Do not add loading, srcset, and sizes.
- add_filter( 'wp_img_tag_add_loading_attr', '__return_false' );
- add_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' );
- $this->assertSame( $content_filtered, wp_filter_content_tags( $content_unfiltered ) );
- remove_filter( 'wp_img_tag_add_loading_attr', '__return_false' );
- remove_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' );
- }
- /**
- * @ticket 44427
- * @ticket 50367
- * @ticket 50756
- * @requires function imagejpeg
- */
- function test_wp_filter_content_tags_loading_lazy() {
- $image_meta = wp_get_attachment_metadata( self::$large_id );
- $size_array = $this->_get_image_size_array_from_meta( $image_meta, 'medium' );
- $img = get_image_tag( self::$large_id, '', '', '', 'medium' );
- $img_xhtml = str_replace( ' />', '/>', $img );
- $img_html5 = str_replace( ' />', '>', $img );
- $img_no_width_height = str_replace( ' width="' . $size_array[0] . '"', '', $img );
- $img_no_width_height = str_replace( ' height="' . $size_array[1] . '"', '', $img_no_width_height );
- $iframe = '<iframe src="https://www.example.com" width="640" height="360"></iframe>';
- $iframe_no_width_height = '<iframe src="https://www.example.com"></iframe>';
- $lazy_img = wp_img_tag_add_loading_attr( $img, 'test' );
- $lazy_img_xhtml = wp_img_tag_add_loading_attr( $img_xhtml, 'test' );
- $lazy_img_html5 = wp_img_tag_add_loading_attr( $img_html5, 'test' );
- $lazy_iframe = wp_iframe_tag_add_loading_attr( $iframe, 'test' );
- // The following should not be modified because there already is a 'loading' attribute.
- $img_eager = str_replace( ' />', ' loading="eager" />', $img );
- $iframe_eager = str_replace( '">', '" loading="eager">', $iframe );
- $content = '
- <p>Image, standard.</p>
- %1$s
- <p>Image, XHTML 1.0 style (no space before the closing slash).</p>
- %2$s
- <p>Image, HTML 5.0 style.</p>
- %3$s
- <p>Image, with pre-existing "loading" attribute. Should not be modified.</p>
- %4$s
- <p>Image, without dimension attributes. Should not be modified.</p>
- %5$s
- <p>Iframe, standard.</p>
- %6$s
- <p>Iframe, with pre-existing "loading" attribute. Should not be modified.</p>
- %7$s
- <p>Iframe, without dimension attributes. Should not be modified.</p>
- %8$s';
- $content_unfiltered = sprintf( $content, $img, $img_xhtml, $img_html5, $img_eager, $img_no_width_height, $iframe, $iframe_eager, $iframe_no_width_height );
- $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 );
- // Do not add width, height, srcset, and sizes.
- add_filter( 'wp_img_tag_add_width_and_height_attr', '__return_false' );
- add_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' );
- $this->assertSame( $content_filtered, wp_filter_content_tags( $content_unfiltered ) );
- remove_filter( 'wp_img_tag_add_width_and_height_attr', '__return_false' );
- remove_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' );
- }
- /**
- * @ticket 44427
- * @ticket 50756
- */
- function test_wp_filter_content_tags_loading_lazy_opted_in() {
- $img = get_image_tag( self::$large_id, '', '', '', 'medium' );
- $lazy_img = wp_img_tag_add_loading_attr( $img, 'test' );
- $iframe = '<iframe src="https://www.example.com" width="640" height="360"></iframe>';
- $lazy_iframe = wp_iframe_tag_add_loading_attr( $iframe, 'test' );
- $content = '
- <p>Image, standard.</p>
- %1$s
- <p>Iframe, standard.</p>
- %2$s';
- $content_unfiltered = sprintf( $content, $img, $iframe );
- $content_filtered = sprintf( $content, $lazy_img, $lazy_iframe );
- // Do not add srcset and sizes while testing.
- add_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' );
- // Enable globally for all tags.
- add_filter( 'wp_lazy_loading_enabled', '__return_true' );
- $this->assertSame( $content_filtered, wp_filter_content_tags( $content_unfiltered ) );
- remove_filter( 'wp_lazy_loading_enabled', '__return_true' );
- remove_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' );
- }
- /**
- * @ticket 44427
- * @ticket 50756
- */
- function test_wp_filter_content_tags_loading_lazy_opted_out() {
- $img = get_image_tag( self::$large_id, '', '', '', 'medium' );
- $iframe = '<iframe src="https://www.example.com" width="640" height="360"></iframe>';
- $content = '
- <p>Image, standard.</p>
- %1$s
- <p>Iframe, standard.</p>
- %2$s';
- $content = sprintf( $content, $img, $iframe );
- // Do not add srcset and sizes while testing.
- add_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' );
- // Disable globally for all tags.
- add_filter( 'wp_lazy_loading_enabled', '__return_false' );
- $this->assertSame( $content, wp_filter_content_tags( $content ) );
- remove_filter( 'wp_lazy_loading_enabled', '__return_false' );
- remove_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' );
- }
- /**
- * @ticket 44427
- * @ticket 50367
- */
- function test_wp_img_tag_add_loading_attr() {
- $img = '<img src="example.png" alt=" width="300" height="225" />';
- $img = wp_img_tag_add_loading_attr( $img, 'test' );
- $this->assertContains( ' loading="lazy"', $img );
- }
- /**
- * @ticket 44427
- * @ticket 50367
- */
- function test_wp_img_tag_add_loading_attr_without_src() {
- $img = '<img alt=" width="300" height="225" />';
- $img = wp_img_tag_add_loading_attr( $img, 'test' );
- $this->assertNotContains( ' loading=', $img );
- }
- /**
- * @ticket 44427
- * @ticket 50367
- */
- function test_wp_img_tag_add_loading_attr_with_single_quotes() {
- $img = "<img src='example.png' alt=' width='300' height='225' />";
- $img = wp_img_tag_add_loading_attr( $img, 'test' );
- $this->assertNotContains( ' loading=', $img );
- // Test specifically that the attribute is not there with double-quotes,
- // to avoid regressions.
- $this->assertNotContains( ' loading="lazy"', $img );
- }
- /**
- * @ticket 44427
- * @ticket 50425
- */
- function test_wp_img_tag_add_loading_attr_opt_out() {
- $img = '<img src="example.png" alt=" width="300" height="225" />';
- add_filter( 'wp_img_tag_add_loading_attr', '__return_false' );
- $this->assertNotContains( ' loading=', $img );
- }
- /**
- * @ticket 50756
- */
- function test_wp_iframe_tag_add_loading_attr() {
- $iframe = '<iframe src="https://www.example.com" width="640" height="360"></iframe>';
- $iframe = wp_iframe_tag_add_loading_attr( $iframe, 'test' );
- $this->assertContains( ' loading="lazy"', $iframe );
- }
- /**
- * @ticket 50756
- */
- function test_wp_iframe_tag_add_loading_attr_without_src() {
- $iframe = '<iframe width="640" height="360"></iframe>';
- $iframe = wp_iframe_tag_add_loading_attr( $iframe, 'test' );
- $this->assertNotContains( ' loading=', $iframe );
- }
- /**
- * @ticket 50756
- */
- function test_wp_iframe_tag_add_loading_attr_with_single_quotes() {
- $iframe = "<iframe src='https://www.example.com' width='640' height='360'></iframe>";
- $iframe = wp_iframe_tag_add_loading_attr( $iframe, 'test' );
- $this->assertNotContains( ' loading=', $iframe );
- // Test specifically that the attribute is not there with double-quotes,
- // to avoid regressions.
- $this->assertNotContains( ' loading="lazy"', $iframe );
- }
- /**
- * @ticket 50756
- */
- function test_wp_iframe_tag_add_loading_attr_opt_out() {
- $iframe = '<iframe src="https://www.example.com" width="640" height="360"></iframe>';
- add_filter( 'wp_iframe_tag_add_loading_attr', '__return_false' );
- $this->assertNotContains( ' loading=', $iframe );
- }
- /**
- * @ticket 44427
- * @ticket 50425
- */
- function test_wp_get_attachment_image_loading() {
- $img = wp_get_attachment_image( self::$large_id );
- $this->assertContains( ' loading="lazy"', $img );
- }
- /**
- * @ticket 44427
- * @ticket 50425
- */
- function test_wp_get_attachment_image_loading_opt_out() {
- add_filter( 'wp_lazy_loading_enabled', '__return_false' );
- $img = wp_get_attachment_image( self::$large_id );
- // There should not be any loading attribute in this case.
- $this->assertNotContains( ' loading=', $img );
- }
- /**
- * @ticket 44427
- * @ticket 50425
- */
- function test_wp_get_attachment_image_loading_opt_out_individual() {
- // The default is already tested above, the filter below ensures that
- // lazy-loading is definitely enabled globally for images.
- add_filter( 'wp_lazy_loading_enabled', '__return_true' );
- $img = wp_get_attachment_image( self::$large_id, 'thumbnail', false, array( 'loading' => false ) );
- // There should not be any loading attribute in this case.
- $this->assertNotContains( ' loading=', $img );
- }
- /**
- * @ticket 44427
- * @ticket 50425
- * @ticket 50756
- * @dataProvider data_wp_lazy_loading_enabled_tag_name_defaults
- *
- * @param string $tag_name Tag name.
- * @param bool $expected Expected return value.
- */
- function test_wp_lazy_loading_enabled_tag_name_defaults( $tag_name, $expected ) {
- if ( $expected ) {
- $this->assertTrue( wp_lazy_loading_enabled( $tag_name, 'the_content' ) );
- } else {
- $this->assertFalse( wp_lazy_loading_enabled( $tag_name, 'the_content' ) );
- }
- }
- function data_wp_lazy_loading_enabled_tag_name_defaults() {
- return array(
- 'img => true' => array( 'img', true ),
- 'iframe => true' => array( 'iframe', true ),
- 'arbitrary tag => false' => array( 'blink', false ),
- );
- }
- /**
- * @ticket 50425
- * @dataProvider data_wp_lazy_loading_enabled_context_defaults
- *
- * @param string $context Function context.
- * @param bool $expected Expected return value.
- */
- function test_wp_lazy_loading_enabled_context_defaults( $context, $expected ) {
- if ( $expected ) {
- $this->assertTrue( wp_lazy_loading_enabled( 'img', $context ) );
- } else {
- $this->assertFalse( wp_lazy_loading_enabled( 'img', $context ) );
- }
- }
- function data_wp_lazy_loading_enabled_context_defaults() {
- return array(
- 'wp_get_attachment_image => true' => array( 'wp_get_attachment_image', true ),
- 'the_content => true' => array( 'the_content', true ),
- 'the_excerpt => true' => array( 'the_excerpt', true ),
- 'widget_text_content => true' => array( 'widget_text_content', true ),
- 'get_avatar => true' => array( 'get_avatar', true ),
- 'arbitrary context => true' => array( 'something_completely_arbitrary', true ),
- );
- }
- /**
- * @ticket 50543
- */
- function test_wp_image_file_matches_image_meta() {
- $image_meta = wp_get_attachment_metadata( self::$large_id );
- $image_src_full = wp_get_attachment_image_url( self::$large_id, 'full' );
- $image_src_medium = wp_get_attachment_image_url( self::$large_id, 'medium' );
- $this->assertTrue( wp_image_file_matches_image_meta( $image_src_full, $image_meta ) );
- $this->assertTrue( wp_image_file_matches_image_meta( $image_src_medium, $image_meta ) );
- }
- /**
- * @ticket 50543
- */
- function test_wp_image_file_matches_image_meta_no_subsizes() {
- $image_meta = wp_get_attachment_metadata( self::$large_id );
- $image_src = wp_get_attachment_image_url( self::$large_id, 'full' );
- $image_meta['sizes'] = array();
- $this->assertTrue( wp_image_file_matches_image_meta( $image_src, $image_meta ) );
- }
- /**
- * @ticket 50543
- */
- function test_wp_image_file_matches_image_meta_invalid_meta() {
- $image_meta = ''; // Attachment is not an image.
- $image_src = $this->img_url;
- $this->assertFalse( wp_image_file_matches_image_meta( $image_src, $image_meta ) );
- }
- /**
- * @ticket 50543
- */
- function test_wp_image_file_matches_image_meta_different_meta() {
- $image_meta = wp_get_attachment_metadata( self::$large_id );
- $image_src = $this->img_url; // Different image.
- $this->assertFalse( wp_image_file_matches_image_meta( $image_src, $image_meta ) );
- }
- /**
- * @ticket 50543
- */
- function test_wp_image_file_matches_image_meta_original_image() {
- $image_meta = wp_get_attachment_metadata( self::$large_id );
- $image_src = wp_get_original_image_url( self::$large_id );
- $this->assertTrue( wp_image_file_matches_image_meta( $image_src, $image_meta ) );
- }
- /**
- * @ticket 22101
- */
- function test_gallery_shortcode_when_is_feed_true() {
- $this->go_to( '/?feed=rss2' );
- // Default: Links to image attachment page URL.
- $actual = gallery_shortcode(
- array(
- 'ids' => self::$large_id,
- )
- );
- $this->assertContains( '?attachment_id=', $actual );
- // File: Links to image file URL.
- $actual = gallery_shortcode(
- array(
- 'ids' => self::$large_id,
- 'link' => 'file',
- )
- );
- $this->assertSame( 2, substr_count( $actual, '.jpg' ) );
- // None: Does not link.
- $actual = gallery_shortcode(
- array(
- 'ids' => self::$large_id,
- 'link' => 'none',
- )
- );
- $this->assertNotContains( '<a ', $actual );
- }
- /**
- * Test attachment permalinks based on parent post status.
- *
- * @dataProvider data_attachment_permalinks_based_on_parent_status
- * @ticket 51776
- *
- * @param string $post_key Post as keyed in the shared fixture array.
- * @param string $expected_url Expected permalink.
- * @param bool $expected_404 Whether the page is expected to return a 404 result.
- *
- */
- function test_attachment_permalinks_based_on_parent_status( $post_key, $expected_url, $expected_404 ) {
- $this->set_permalink_structure( '/%postname%' );
- $post = get_post( self::$post_ids[ $post_key ] );
- /*
- * The dataProvider runs before the fixures are set up, therefore the
- * post object IDs are placeholders that needs to be replaced.
- */
- $expected_url = home_url( str_replace( '%ID%', $post->ID, $expected_url ) );
- $this->go_to( get_permalink( $post ) );
- $this->assertSame( $expected_url, get_permalink( $post ) );
- if ( $expected_404 ) {
- $this->assertQueryTrue( 'is_404' );
- } else {
- $this->assertQueryTrue( 'is_attachment', 'is_single', 'is_singular' );
- }
- $this->assertSame( 'attachment', $post->post_type );
- }
- /**
- * Data provider for test_attachment_permalinks_based_on_parent_status().
- *
- * @return array[] {
- * @type string $post_key Post as keyed in the shared fixture array.
- * @type string $expected_url Expected permalink.
- * $type bool $expected_404 Whether the page is expected to return a 404 result.
- * }
- */
- function data_attachment_permalinks_based_on_parent_status() {
- return array(
- array( 'draft-attachment', '/?attachment_id=%ID%', true ),
- array( 'publish-attachment', '/publish-post/publish-attachment', false ),
- array( 'future-attachment', '/future-post/future-attachment', false ),
- array( 'auto-draft-attachment', '/?attachment_id=%ID%', true ),
- array( 'trash-attachment', '/?attachment_id=%ID%', false ),
- );
- }
- }
- /**
- * Helper class for `test_autoembed`.
- */
- class Test_Autoembed extends WP_Embed {
- public function shortcode( $attr, $url = '' ) {
- return '[embed]';
- }
- }
|