<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[50771] trunk/tests/phpunit: Build/Test Tools: Add seeking support to stream test library.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/50771">50771</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/50771","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>peterwilsoncc</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2021-04-20 03:01:55 +0000 (Tue, 20 Apr 2021)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Build/Test Tools: Add seeking support to stream test library.

Introduces seeking to the `WP_Test_Stream` stream wrapper. This allows the testing of `wp_getimagesize()` and `wp_read_image_metadata()` among others. Includes tests for the latter.

Props hellofromTonya.
Fixes <a href="https://core.trac.wordpress.org/ticket/52922">#52922</a>.
See <a href="https://core.trac.wordpress.org/ticket/52826">#52826</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunktestsphpunitincludesclasswpteststreamphp">trunk/tests/phpunit/includes/class-wp-test-stream.php</a></li>
<li><a href="#trunktestsphpunittestsimagemetaphp">trunk/tests/phpunit/tests/image/meta.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunktestsphpunitincludesclasswpteststreamphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/includes/class-wp-test-stream.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/includes/class-wp-test-stream.php     2021-04-19 11:39:54 UTC (rev 50770)
+++ trunk/tests/phpunit/includes/class-wp-test-stream.php       2021-04-20 03:01:55 UTC (rev 50771)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -11,8 +11,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">  * 'DIRECTORY' to the static variable WP_Test_Stream::$data['bucket']['/foo/']
</span><span class="cx" style="display: block; padding: 0 10px">  * (note the trailing slash).
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * This class can be used to test that code works with basic read/write streams,
- * as such, operations such as seeking are not supported.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * This class can be used to test that code works with basic read/write streams.
</ins><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * This class does not register itself as a stream handler: test fixtures
</span><span class="cx" style="display: block; padding: 0 10px">  * should make the appropriate call to stream_wrapper_register().
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -24,12 +23,12 @@
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="cx" style="display: block; padding: 0 10px">         * In-memory storage for files and directories simulated by this wrapper.
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        static $data = array();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public static $data = array();
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        var $position;
-       var $file;
-       var $bucket;
-       var $data_ref;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public $position;
+       public $file;
+       public $bucket;
+       public $data_ref;
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="cx" style="display: block; padding: 0 10px">         * Initializes internal state for reading the given URL.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -66,7 +65,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @see streamWrapper::stream_open
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        function stream_open( $path, $mode, $options, &$opened_path ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function stream_open( $path, $mode, $options, &$opened_path ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                 $this->open( $path );
</span><span class="cx" style="display: block; padding: 0 10px">                return true;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -76,7 +75,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @see streamWrapper::stream_read
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        function stream_read( $count ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function stream_read( $count ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                 if ( ! isset( $this->data_ref ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        return '';
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -92,7 +91,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @see streamWrapper::stream_write
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        function stream_write( $data ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function stream_write( $data ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                 if ( ! isset( $this->data_ref ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $this->data_ref = '';
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -107,11 +106,53 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Seeks to specific location in a stream.
+        *
+        * @see streamWrapper::stream_seek
+        *
+        * @param int $offset The stream offset to seek to.
+        * @param int $whence Optional. Seek position.
+        * @return bool Returns true when position is updated, else false.
+        */
+       public function stream_seek( $offset, $whence = SEEK_SET ) {
+               if ( empty( $this->data_ref ) ) {
+                       return false;
+               }
+
+               $new_offset = $this->position;
+               switch ( $whence ) {
+                       case SEEK_CUR:
+                               $new_offset += $offset;
+                               break;
+
+                       case SEEK_END:
+                               $new_offset = strlen( $this->data_ref ) + $offset;
+                               break;
+
+                       case SEEK_SET:
+                               $new_offset = $offset;
+                               break;
+
+                       default:
+                               return false;
+               }
+
+               if ( $new_offset < 0 ) {
+                       return false;
+               }
+
+               // Save the new position.
+               $this->position = $new_offset;
+
+               return true;
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Retrieves the current position of a stream.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @see streamWrapper::stream_tell
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        function stream_tell() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function stream_tell() {
</ins><span class="cx" style="display: block; padding: 0 10px">                 return $this->position;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -120,7 +161,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @see streamWrapper::stream_eof
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        function stream_eof() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function stream_eof() {
</ins><span class="cx" style="display: block; padding: 0 10px">                 if ( ! isset( $this->data_ref ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        return true;
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -133,7 +174,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @see streamWrapper::stream_metadata
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        function stream_metadata( $path, $option, $var ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function stream_metadata( $path, $option, $var ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                 $this->open( $path );
</span><span class="cx" style="display: block; padding: 0 10px">                if ( STREAM_META_TOUCH === $option ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( ! isset( $this->data_ref ) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -149,7 +190,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @see streamWrapper::mkdir
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        function mkdir( $path, $mode, $options ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function mkdir( $path, $mode, $options ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                 $this->open( $path );
</span><span class="cx" style="display: block; padding: 0 10px">                $plainfile = rtrim( $this->file, '/' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span></span></pre></div>
<a id="trunktestsphpunittestsimagemetaphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/image/meta.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/image/meta.php  2021-04-19 11:39:54 UTC (rev 50770)
+++ trunk/tests/phpunit/tests/image/meta.php    2021-04-20 03:01:55 UTC (rev 50771)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -6,9 +6,28 @@
</span><span class="cx" style="display: block; padding: 0 10px">  * @group upload
</span><span class="cx" style="display: block; padding: 0 10px">  * @requires extension gd
</span><span class="cx" style="display: block; padding: 0 10px">  * @requires extension exif
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ *
+ * @covers ::wp_read_image_metadata
</ins><span class="cx" style="display: block; padding: 0 10px">  */
</span><span class="cx" style="display: block; padding: 0 10px"> class Tests_Image_Meta extends WP_UnitTestCase {
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
+               require_once DIR_TESTROOT . '/includes/class-wp-test-stream.php';
+               stream_wrapper_register( 'testimagemeta', 'WP_Test_Stream' );
+
+               WP_Test_Stream::$data = array(
+                       'wp_read_image_metadata' => array(
+                               '/image1.jpg' => file_get_contents( DIR_TESTDATA . '/images/test-image-upside-down.jpg' ),
+                               '/image2.jpg' => file_get_contents( DIR_TESTDATA . '/images/2004-07-22-DSC_0007.jpg' ),
+                               '/image3.jpg' => file_get_contents( DIR_TESTDATA . '/images/33772.jpg' ),
+                       ),
+               );
+       }
+
+       public static function wpTearDownAfterClass() {
+               stream_wrapper_unregister( 'testimagemeta' );
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         function test_exif_d70() {
</span><span class="cx" style="display: block; padding: 0 10px">                // Exif from a Nikon D70.
</span><span class="cx" style="display: block; padding: 0 10px">                $out = wp_read_image_metadata( DIR_TESTDATA . '/images/2004-07-22-DSC_0008.jpg' );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -150,4 +169,74 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertSame( array( 'beach', 'baywatch', 'LA', 'sunset' ), $out['keywords'] );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        /**
+        * @dataProvider data_stream
+        *
+        * @ticket 52826
+        * @ticket 52922
+        *
+        * @param string Stream's URI.
+        * @param array  Expected metadata.
+        */
+       public function test_stream( $file, $expected ) {
+               $actual = wp_read_image_metadata( $file );
+
+               $this->assertSame( $expected, $actual );
+       }
+
+       public function data_stream() {
+               return array(
+                       'Orientation only metadata'                => array(
+                               'file'     => 'testimagemeta://wp_read_image_metadata/image1.jpg',
+                               'metadata' => array(
+                                       'aperture'          => '0',
+                                       'credit'            => '',
+                                       'camera'            => '',
+                                       'caption'           => '',
+                                       'created_timestamp' => '0',
+                                       'copyright'         => '',
+                                       'focal_length'      => '0',
+                                       'iso'               => '0',
+                                       'shutter_speed'     => '0',
+                                       'title'             => '',
+                                       'orientation'       => '3',
+                                       'keywords'          => array(),
+                               ),
+                       ),
+                       'Exif from a Nikon D70 with IPTC data added later' => array(
+                               'file'     => 'testimagemeta://wp_read_image_metadata/image2.jpg',
+                               'metadata' => array(
+                                       'aperture'          => '6.3',
+                                       'credit'            => 'IPTC Creator',
+                                       'camera'            => 'NIKON D70',
+                                       'caption'           => 'IPTC Caption',
+                                       'created_timestamp' => '1090516475',
+                                       'copyright'         => 'IPTC Copyright',
+                                       'focal_length'      => '18',
+                                       'iso'               => '200',
+                                       'shutter_speed'     => '0.04',
+                                       'title'             => 'IPTC Headline',
+                                       'orientation'       => '0',
+                                       'keywords'          => array(),
+                               ),
+                       ),
+                       'Exif from a DMC-LX2 camera with keywords' => array(
+                               'file'     => 'testimagemeta://wp_read_image_metadata/image3.jpg',
+                               'metadata' => array(
+                                       'aperture'          => '8',
+                                       'credit'            => 'Photoshop Author',
+                                       'camera'            => 'DMC-LX2',
+                                       'caption'           => 'Photoshop Description',
+                                       'created_timestamp' => '1306315327',
+                                       'copyright'         => 'Photoshop Copyrright Notice',
+                                       'focal_length'      => '6.3',
+                                       'iso'               => '100',
+                                       'shutter_speed'     => '0.0025',
+                                       'title'             => 'Photoshop Document Ttitle',
+                                       'orientation'       => '1',
+                                       'keywords'          => array( 'beach', 'baywatch', 'LA', 'sunset' ),
+                               ),
+                       ),
+               );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>

</body>
</html>