Make WordPress Core

Ticket #21332: 21332-ut.diff

File 21332-ut.diff, 2.5 KB (added by ryan, 11 years ago)

Quick unit tests

  • tests/functions.php

     
    242242                foreach ( $not_serialized as $case )
    243243                        $this->assertFalse( is_serialized($case), "Test data: $case" );
    244244        }
     245
     246        function test_add_query_arg() {
     247                $old_req_uri = $_SERVER['REQUEST_URI'];
     248
     249                $urls = array(
     250                        '/',
     251                        '/2012/07/30/',
     252                        'edit.php',
     253                        admin_url( 'edit.php' ),
     254                        admin_url( 'edit.php', 'https' ),
     255                );
     256
     257                $frag_urls = array(
     258                        '/#frag',
     259                        '/2012/07/30/#frag',
     260                        'edit.php#frag',
     261                        admin_url( 'edit.php#frag' ),
     262                        admin_url( 'edit.php#frag', 'https' ),
     263                );
     264
     265                foreach ( $urls as $url ) {
     266                        $_SERVER['REQUEST_URI'] = 'nothing';
     267
     268                        $this->assertEquals( "$url?foo=1", add_query_arg( 'foo', '1', $url ) );
     269                        $this->assertEquals( "$url?foo=1", add_query_arg( array( 'foo' => '1' ), $url ) );
     270                        $this->assertEquals( "$url?foo=2", add_query_arg( array( 'foo' => '1', 'foo' => '2' ), $url ) );
     271                        $this->assertEquals( "$url?foo=1&bar=2", add_query_arg( array( 'foo' => '1', 'bar' => '2' ), $url ) );
     272
     273                        $_SERVER['REQUEST_URI'] = $url;
     274
     275                        $this->assertEquals( "$url?foo=1", add_query_arg( 'foo', '1' ) );
     276                        $this->assertEquals( "$url?foo=1", add_query_arg( array( 'foo' => '1' ) ) );
     277                        $this->assertEquals( "$url?foo=2", add_query_arg( array( 'foo' => '1', 'foo' => '2' ) ) );
     278                        $this->assertEquals( "$url?foo=1&bar=2", add_query_arg( array( 'foo' => '1', 'bar' => '2' ) ) );
     279                }
     280
     281                foreach ( $frag_urls as $frag_url ) {
     282                        $_SERVER['REQUEST_URI'] = 'nothing';
     283                        $url = str_replace( '#frag', '', $frag_url );
     284
     285                        $this->assertEquals( "$url?foo=1#frag", add_query_arg( 'foo', '1', $frag_url ) );
     286                        $this->assertEquals( "$url?foo=1#frag", add_query_arg( array( 'foo' => '1' ), $frag_url ) );
     287                        $this->assertEquals( "$url?foo=2#frag", add_query_arg( array( 'foo' => '1', 'foo' => '2' ), $frag_url ) );
     288                        $this->assertEquals( "$url?foo=1&bar=2#frag", add_query_arg( array( 'foo' => '1', 'bar' => '2' ), $frag_url ) );
     289
     290                        $_SERVER['REQUEST_URI'] = $frag_url;
     291
     292                        $this->assertEquals( "$url?foo=1#frag", add_query_arg( 'foo', '1' ) );
     293                        $this->assertEquals( "$url?foo=1#frag", add_query_arg( array( 'foo' => '1' ) ) );
     294                        $this->assertEquals( "$url?foo=2#frag", add_query_arg( array( 'foo' => '1', 'foo' => '2' ) ) );
     295                        $this->assertEquals( "$url?foo=1&bar=2#frag", add_query_arg( array( 'foo' => '1', 'bar' => '2' ) ) );
     296                }
     297
     298                $_SERVER['REQUEST_URI'] = $old_req_uri;
     299        }
    245300}