WordPress.org

Make WordPress Core

Ticket #36416: 36416.diff

File 36416.diff, 2.8 KB (added by mikeschroder, 18 months ago)

Refresh of trimed.patch

  • src/wp-includes/functions.php

    diff --git src/wp-includes/functions.php src/wp-includes/functions.php
    index 6c5184f697..01bf984624 100644
    function get_weekstartend( $mysqlstring, $start_of_week = '' ) { 
    591591 */
    592592function maybe_unserialize( $original ) {
    593593        if ( is_serialized( $original ) ) { // Don't attempt to unserialize data that wasn't serialized going in.
    594                 return @unserialize( $original );
     594                return @unserialize( trim( $original ) );
    595595        }
    596596        return $original;
    597597}
  • new file tests/phpunit/tests/functions/maybe_unserialize.php

    diff --git tests/phpunit/tests/functions/maybe_unserialize.php tests/phpunit/tests/functions/maybe_unserialize.php
    new file mode 100644
    index 0000000000..5ff83fc6ce
    - +  
     1<?php
     2/**
     3 * Test functions against maybe_unserialize.
     4 * Tested scenarios:
     5 *     1)
     6 * @group functions.php
     7 */
     8
     9if ( ! defined( 'WPINC' ) ) {
     10        die;
     11}
     12
     13
     14class Tests_Functions_maybe_unserialize extends WP_UnitTestCase {
     15
     16        //Passing an array should return the same array back
     17        public function test_maybe_unserialize_array() {
     18                $expected = array(
     19                        'start' => 'here',
     20                        'end'   => 'here',
     21                );
     22
     23                $this->assertEquals( $expected, maybe_unserialize( $expected ) );
     24        }
     25
     26        //Tests normal string unserialization
     27        public function test_maybe_unserialize_string() {
     28                $this->assertEquals( 'string', maybe_unserialize( 'string' ) );
     29
     30                $this->assertEquals( '[string]', maybe_unserialize( '[string]' ) );
     31
     32                $this->assertEquals( '{s:5:"start";s:4:"here";s:3:"end";s:4:"here";}', maybe_unserialize( '{s:5:"start";s:4:"here";s:3:"end";s:4:"here";}' ) );
     33
     34        }
     35
     36        //Passing an int should return the same int
     37        public function test_maybe_unserialize_int() {
     38
     39                $this->assertEquals( 100, maybe_unserialize( 100 ) );
     40        }
     41
     42        //Passing null should return null
     43        public function test_maybe_unserialize_null() {
     44
     45                $this->assertEquals( null, maybe_unserialize( null ) );
     46        }
     47
     48        //Passing null should return null
     49        public function test_maybe_unserialize_object() {
     50                $object = new stdClass();
     51
     52                $this->assertEquals( $object, maybe_unserialize( $object ) );
     53        }
     54
     55        public function test_maybe_unserialize_to_array() {
     56                $expected = array(
     57                        'start' => 'here',
     58                        'end'   => 'here',
     59                );
     60
     61                $this->assertEquals( $expected, maybe_unserialize( 'a:2:{s:5:"start";s:4:"here";s:3:"end";s:4:"here";}' ) );
     62        }
     63
     64        public function test_maybe_unserialize_bad_serialized_string() {
     65
     66                // a:3 not a:2 as it should be
     67                $this->assertFalse( maybe_unserialize( 'a:3:{s:5:"start";s:4:"here";s:3:"end";s:4:"here";}' ) );
     68        }
     69
     70        public function test_maybe_unserialize_to_array_un_trimed() {
     71                $expected = array(
     72                        'start' => 'here',
     73                        'end'   => 'here',
     74                );
     75
     76                $this->assertEquals( $expected, maybe_unserialize( '    a:2:{s:5:"start";s:4:"here";s:3:"end";s:4:"here";}     ' ) );
     77        }
     78}
     79 No newline at end of file