Make WordPress Core

Changeset 50771


Ignore:
Timestamp:
04/20/2021 03:01:55 AM (5 years ago)
Author:
peterwilsoncc
Message:

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 #52922.
See #52826.

Location:
trunk/tests/phpunit
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/includes/class-wp-test-stream.php

    r49545 r50771  
    1212 * (note the trailing slash).
    1313 *
    14  * This class can be used to test that code works with basic read/write streams,
    15  * as such, operations such as seeking are not supported.
     14 * This class can be used to test that code works with basic read/write streams.
    1615 *
    1716 * This class does not register itself as a stream handler: test fixtures
     
    2524         * In-memory storage for files and directories simulated by this wrapper.
    2625         */
    27         static $data = array();
    28 
    29         var $position;
    30         var $file;
    31         var $bucket;
    32         var $data_ref;
     26        public static $data = array();
     27
     28        public $position;
     29        public $file;
     30        public $bucket;
     31        public $data_ref;
    3332
    3433        /**
     
    6766         * @see streamWrapper::stream_open
    6867         */
    69         function stream_open( $path, $mode, $options, &$opened_path ) {
     68        public function stream_open( $path, $mode, $options, &$opened_path ) {
    7069                $this->open( $path );
    7170                return true;
     
    7776         * @see streamWrapper::stream_read
    7877         */
    79         function stream_read( $count ) {
     78        public function stream_read( $count ) {
    8079                if ( ! isset( $this->data_ref ) ) {
    8180                        return '';
     
    9392         * @see streamWrapper::stream_write
    9493         */
    95         function stream_write( $data ) {
     94        public function stream_write( $data ) {
    9695                if ( ! isset( $this->data_ref ) ) {
    9796                        $this->data_ref = '';
     
    108107
    109108        /**
     109         * Seeks to specific location in a stream.
     110         *
     111         * @see streamWrapper::stream_seek
     112         *
     113         * @param int $offset The stream offset to seek to.
     114         * @param int $whence Optional. Seek position.
     115         * @return bool Returns true when position is updated, else false.
     116         */
     117        public function stream_seek( $offset, $whence = SEEK_SET ) {
     118                if ( empty( $this->data_ref ) ) {
     119                        return false;
     120                }
     121
     122                $new_offset = $this->position;
     123                switch ( $whence ) {
     124                        case SEEK_CUR:
     125                                $new_offset += $offset;
     126                                break;
     127
     128                        case SEEK_END:
     129                                $new_offset = strlen( $this->data_ref ) + $offset;
     130                                break;
     131
     132                        case SEEK_SET:
     133                                $new_offset = $offset;
     134                                break;
     135
     136                        default:
     137                                return false;
     138                }
     139
     140                if ( $new_offset < 0 ) {
     141                        return false;
     142                }
     143
     144                // Save the new position.
     145                $this->position = $new_offset;
     146
     147                return true;
     148        }
     149
     150        /**
    110151         * Retrieves the current position of a stream.
    111152         *
    112153         * @see streamWrapper::stream_tell
    113154         */
    114         function stream_tell() {
     155        public function stream_tell() {
    115156                return $this->position;
    116157        }
     
    121162         * @see streamWrapper::stream_eof
    122163         */
    123         function stream_eof() {
     164        public function stream_eof() {
    124165                if ( ! isset( $this->data_ref ) ) {
    125166                        return true;
     
    134175         * @see streamWrapper::stream_metadata
    135176         */
    136         function stream_metadata( $path, $option, $var ) {
     177        public function stream_metadata( $path, $option, $var ) {
    137178                $this->open( $path );
    138179                if ( STREAM_META_TOUCH === $option ) {
     
    150191         * @see streamWrapper::mkdir
    151192         */
    152         function mkdir( $path, $mode, $options ) {
     193        public function mkdir( $path, $mode, $options ) {
    153194                $this->open( $path );
    154195                $plainfile = rtrim( $this->file, '/' );
  • trunk/tests/phpunit/tests/image/meta.php

    r49025 r50771  
    77 * @requires extension gd
    88 * @requires extension exif
     9 *
     10 * @covers ::wp_read_image_metadata
    911 */
    1012class Tests_Image_Meta extends WP_UnitTestCase {
     13
     14        public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
     15                require_once DIR_TESTROOT . '/includes/class-wp-test-stream.php';
     16                stream_wrapper_register( 'testimagemeta', 'WP_Test_Stream' );
     17
     18                WP_Test_Stream::$data = array(
     19                        'wp_read_image_metadata' => array(
     20                                '/image1.jpg' => file_get_contents( DIR_TESTDATA . '/images/test-image-upside-down.jpg' ),
     21                                '/image2.jpg' => file_get_contents( DIR_TESTDATA . '/images/2004-07-22-DSC_0007.jpg' ),
     22                                '/image3.jpg' => file_get_contents( DIR_TESTDATA . '/images/33772.jpg' ),
     23                        ),
     24                );
     25        }
     26
     27        public static function wpTearDownAfterClass() {
     28                stream_wrapper_unregister( 'testimagemeta' );
     29        }
    1130
    1231        function test_exif_d70() {
     
    151170        }
    152171
     172        /**
     173         * @dataProvider data_stream
     174         *
     175         * @ticket 52826
     176         * @ticket 52922
     177         *
     178         * @param string Stream's URI.
     179         * @param array  Expected metadata.
     180         */
     181        public function test_stream( $file, $expected ) {
     182                $actual = wp_read_image_metadata( $file );
     183
     184                $this->assertSame( $expected, $actual );
     185        }
     186
     187        public function data_stream() {
     188                return array(
     189                        'Orientation only metadata'                => array(
     190                                'file'     => 'testimagemeta://wp_read_image_metadata/image1.jpg',
     191                                'metadata' => array(
     192                                        'aperture'          => '0',
     193                                        'credit'            => '',
     194                                        'camera'            => '',
     195                                        'caption'           => '',
     196                                        'created_timestamp' => '0',
     197                                        'copyright'         => '',
     198                                        'focal_length'      => '0',
     199                                        'iso'               => '0',
     200                                        'shutter_speed'     => '0',
     201                                        'title'             => '',
     202                                        'orientation'       => '3',
     203                                        'keywords'          => array(),
     204                                ),
     205                        ),
     206                        'Exif from a Nikon D70 with IPTC data added later' => array(
     207                                'file'     => 'testimagemeta://wp_read_image_metadata/image2.jpg',
     208                                'metadata' => array(
     209                                        'aperture'          => '6.3',
     210                                        'credit'            => 'IPTC Creator',
     211                                        'camera'            => 'NIKON D70',
     212                                        'caption'           => 'IPTC Caption',
     213                                        'created_timestamp' => '1090516475',
     214                                        'copyright'         => 'IPTC Copyright',
     215                                        'focal_length'      => '18',
     216                                        'iso'               => '200',
     217                                        'shutter_speed'     => '0.04',
     218                                        'title'             => 'IPTC Headline',
     219                                        'orientation'       => '0',
     220                                        'keywords'          => array(),
     221                                ),
     222                        ),
     223                        'Exif from a DMC-LX2 camera with keywords' => array(
     224                                'file'     => 'testimagemeta://wp_read_image_metadata/image3.jpg',
     225                                'metadata' => array(
     226                                        'aperture'          => '8',
     227                                        'credit'            => 'Photoshop Author',
     228                                        'camera'            => 'DMC-LX2',
     229                                        'caption'           => 'Photoshop Description',
     230                                        'created_timestamp' => '1306315327',
     231                                        'copyright'         => 'Photoshop Copyrright Notice',
     232                                        'focal_length'      => '6.3',
     233                                        'iso'               => '100',
     234                                        'shutter_speed'     => '0.0025',
     235                                        'title'             => 'Photoshop Document Ttitle',
     236                                        'orientation'       => '1',
     237                                        'keywords'          => array( 'beach', 'baywatch', 'LA', 'sunset' ),
     238                                ),
     239                        ),
     240                );
     241        }
    153242}
Note: See TracChangeset for help on using the changeset viewer.