Make WordPress Core

Changeset 909 in tests for trunk/tests/functions/listFilter.php


Ignore:
Timestamp:
07/19/2012 01:52:37 AM (13 years ago)
Author:
nacin
Message:

Complete the renaming described in [904].

Location:
trunk/tests/functions
Files:
1 added
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/tests/functions/listFilter.php

    r903 r909  
    11<?php
    2 
    3 /**
    4  * @group functions.php
    5  */
    6 class TestFunctions extends WP_UnitTestCase {
    7     function test_wp_parse_args_object() {
    8         $x = new MockClass;
    9         $x->_baba = 5;
    10         $x->yZ = "baba";
    11         $x->a = array(5, 111, 'x');
    12         $this->assertEquals(array('_baba' => 5, 'yZ' => 'baba', 'a' => array(5, 111, 'x')), wp_parse_args($x));
    13         $y = new MockClass;
    14         $this->assertEquals(array(), wp_parse_args($y));
    15     }
    16     function test_wp_parse_args_array()  {
    17         // arrays
    18         $a = array();
    19         $this->assertEquals(array(), wp_parse_args($a));
    20         $b = array('_baba' => 5, 'yZ' => 'baba', 'a' => array(5, 111, 'x'));
    21         $this->assertEquals(array('_baba' => 5, 'yZ' => 'baba', 'a' => array(5, 111, 'x')), wp_parse_args($b));
    22     }
    23     function test_wp_parse_args_defaults() {
    24         $x = new MockClass;
    25         $x->_baba = 5;
    26         $x->yZ = "baba";
    27         $x->a = array(5, 111, 'x');
    28         $d = array('pu' => 'bu');
    29         $this->assertEquals(array('pu' => 'bu', '_baba' => 5, 'yZ' => 'baba', 'a' => array(5, 111, 'x')), wp_parse_args($x, $d));
    30         $e = array('_baba' => 6);
    31         $this->assertEquals(array('_baba' => 5, 'yZ' => 'baba', 'a' => array(5, 111, 'x')), wp_parse_args($x, $e));
    32     }
    33     function test_wp_parse_args_other() {
    34         $b = true;
    35         wp_parse_str($b, $s);
    36         $this->assertEquals($s, wp_parse_args($b));
    37         $q = 'x=5&_baba=dudu&';
    38         wp_parse_str($q, $ss);
    39         $this->assertEquals($ss, wp_parse_args($q));
    40     }
    41     function test_size_format() {
    42         $kb = 1024;
    43         $mb = $kb*1024;
    44         $gb = $mb*1024;
    45         $tb = $gb*1024;
    46         // test if boundaries are correct
    47         $this->assertEquals('1 GB', size_format($gb, 0));
    48         $this->assertEquals('1 MB', size_format($mb, 0));
    49         $this->assertEquals('1 kB', size_format($kb, 0));
    50         // now some values around
    51         // add some bytes to make sure the result isn't 1.4999999
    52         $this->assertEquals('1.5 TB', size_format($tb + $tb/2 + $mb, 1));
    53         $this->assertEquals('1,023.999 GB', size_format($tb-$mb-$kb, 3));
    54         // edge
    55         $this->assertFalse(size_format(-1));
    56         $this->assertFalse(size_format(0));
    57         $this->assertFalse(size_format('baba'));
    58         $this->assertFalse(size_format(array()));
    59     }
    60 
    61     function test_path_is_absolute() {
    62         if ( !is_callable('path_is_absolute') )
    63             $this->markTestSkipped();
    64 
    65         $absolute_paths = array(
    66             '/',
    67             '/foo/',
    68             '/foo',
    69             '/FOO/bar',
    70             '/foo/bar/',
    71             '/foo/../bar/',
    72             '\\WINDOWS',
    73             'C:\\',
    74             'C:\\WINDOWS',
    75             '\\\\sambashare\\foo',
    76             );
    77         foreach ($absolute_paths as $path)
    78             $this->assertTrue( path_is_absolute($path), "path_is_absolute('$path') should return true" );
    79     }
    80 
    81     function test_path_is_not_absolute() {
    82         if ( !is_callable('path_is_absolute') )
    83             $this->markTestSkipped();
    84 
    85         $relative_paths = array(
    86             '',
    87             '.',
    88             '..',
    89             '../foo',
    90             '../',
    91             '../foo.bar',
    92             'foo/bar',
    93             'foo',
    94             'FOO',
    95             '..\\WINDOWS',
    96             );
    97         foreach ($relative_paths as $path)
    98             $this->assertFalse( path_is_absolute($path), "path_is_absolute('$path') should return false" );
    99     }
    100 
    101 
    102     function test_wp_unique_filename() {
    103         /* this test requires:
    104            - that you have dir + file 'data/images/test-image.png',
    105            - and that this dir is writeable
    106            - there is an image 'test-image.png' that will be used to test unique filenames
    107 
    108            NB: there is a hardcoded dependency that the testing file is '.png'; however,
    109                this limitation is arbitary, so change it if you like.
    110         */
    111         $testdir = DIR_TESTDATA . '/images/';
    112         $testimg = 'test-image.png';
    113         $this->assertTrue( file_exists($testdir) );
    114         $this->assertTrue( is_writable($testdir) );
    115         $this->assertTrue( file_exists($testdir . $testimg) );
    116 
    117         $cases = array(
    118             // null case
    119             'null' . $testimg,
    120 
    121             // edge cases: '.png', 'abc.', 'abc', 'abc0', 'abc1', 'abc0.png', 'abc1.png' (num @ end)
    122             '.png',
    123             'abc',
    124             'abc.',
    125             'abc0',
    126             'abc1',
    127             'abc0.png',
    128             'abc1.png',
    129 
    130             // replacing # with _
    131             str_replace('-', '#', $testimg), // test#image.png
    132             str_replace('-', '##', $testimg), // test##image.png
    133             str_replace(array('-', 'e'), '#', $testimg), // t#st#imag#.png
    134             str_replace(array('-', 'e'), '##', $testimg), // t##st##imag##.png
    135 
    136             // replacing \ or ' with nothing
    137             str_replace('-', '\\', $testimg), // test\image.png
    138             str_replace('-', '\\\\', $testimg), // test\\image.png
    139             str_replace(array('-', 'e'), '\\', $testimg), // t\st\imag\.png
    140             str_replace(array('-', 'e'), '\\\\', $testimg), // t\\st\\imag\\.png
    141             str_replace('-', "'", $testimg), // test'image.png
    142             str_replace('-', "'", $testimg), // test''image.png
    143             str_replace(array('-', 'e'), "'", $testimg), // t'st'imag'.png
    144             str_replace(array('-', 'e'), "''", $testimg), // t''st''imag''.png
    145             str_replace('-', "\'", $testimg), // test\'image.png
    146             str_replace('-', "\'\'", $testimg), // test\'\'image.png
    147             str_replace(array('-', 'e'), "\'", $testimg), // t\'st\'imag\'.png
    148             str_replace(array('-', 'e'), "\'\'", $testimg), // t\'\'st\'\'imag\'\'.png
    149 
    150             'test' . str_replace('e', 'é', $testimg), // testtést-imagé.png
    151 
    152             '12%af34567890~!@#$..%^&*()|_+qwerty  fgh`jkl zx<>?:"{}[]="\'/?.png', // kitchen sink
    153             $testdir.'test-image-with-path.png',
    154         );
    155 
    156         // what we expect the replacements will do
    157         $expected = array(
    158                 'null' . $testimg,
    159 
    160                 'png',
    161                 'abc',
    162                 'abc',
    163                 'abc0',
    164                 'abc1',
    165                 'abc0.png',
    166                 'abc1.png',
    167 
    168                 'testimage.png',
    169                 'testimage.png',
    170                 'tstimag.png',
    171                 'tstimag.png',
    172 
    173                 'testimage.png',
    174                 'testimage.png',
    175                 'tstimag.png',
    176                 'tstimag.png',
    177                 'testimage.png',
    178                 'testimage.png',
    179                 'tstimag.png',
    180                 'tstimag.png',
    181                 'testimage.png',
    182                 'testimage.png',
    183                 'tstimag.png',
    184                 'tstimag.png',
    185 
    186                 'testtést-imagé.png',
    187 
    188                 '12%af34567890@..%^_+qwerty-fghjkl-zx.png',
    189                 str_replace( array( '\\', '/', ':' ), '', $testdir ).'test-image-with-path.png',
    190             );
    191 
    192         foreach ($cases as $key => $case) {
    193             // make sure expected file doesn't exist already
    194             // happens when tests fail and the unlinking doesn't happen
    195             if( $expected[$key] !== $testimg && file_exists($testdir . $expected[$key]) )
    196                 unlink($testdir . $expected[$key]);
    197 
    198             // -- TEST 1: the replacement is as expected
    199             $this->assertEquals( $expected[$key], wp_unique_filename($testdir, $case, NULL), $case );
    200             // -- end TEST 1
    201 
    202             // -- TEST 2: the renaming will produce a unique name
    203             // create the expected file
    204             copy($testdir . $testimg, $testdir . $expected[$key]);
    205             // test that wp_unique_filename actually returns a unique filename
    206             $this->assertFileNotExists( $testdir . wp_unique_filename($testdir, $case, NULL) );
    207             // -- end TEST 2
    208 
    209             // cleanup
    210             if( $expected[$key] !== $testimg &&  file_exists($testdir . $expected[$key]) )
    211                 unlink($testdir . $expected[$key]);
    212         }
    213     }
    214 
    215     /**
    216      * @ticket 9930
    217      */
    218     function test_is_serialized() {
    219         $cases = array(
    220             serialize(null),
    221             serialize(true),
    222             serialize(false),
    223             serialize(-25),
    224             serialize(25),
    225             serialize(1.1),
    226             serialize(2.1E+200),
    227             serialize('this string will be serialized'),
    228             serialize("a\nb"),
    229             serialize(array()),
    230             serialize(array(1,1,2,3,5,8,13)),
    231             serialize( (object)array('test' => true, '3', 4) )
    232         );
    233         foreach ( $cases as $case )
    234             $this->assertTrue( is_serialized($case), "Serialized data: $case" );
    235 
    236         $not_serialized = array(
    237             'a string',
    238             'garbage:a:0:garbage;',
    239             'b:4;',
    240             's:4:test;'
    241         );
    242         foreach ( $not_serialized as $case )
    243             $this->assertFalse( is_serialized($case), "Test data: $case" );
    244     }
    245 
    246 }
    2472
    2483/**
     
    2516 * @group functions.php
    2527 */
    253 class TestListFilter extends WP_UnitTestCase {
     8class Tests_Functions_ListFilter extends WP_UnitTestCase {
    2549    var $object_list = array();
    25510    var $array_list = array();
     
    356111    }
    357112}
    358 
    359 /**
    360  * @group http
    361  */
    362 class TestHTTPFunctions extends WP_UnitTestCase {
    363     function test_head_request() {
    364         // this url give a direct 200 response
    365         $url = 'http://asdftestblog1.files.wordpress.com/2007/09/2007-06-30-dsc_4700-1.jpg';
    366         $response = wp_remote_head( $url );
    367         $headers = wp_remote_retrieve_headers( $response );
    368 
    369         $this->assertTrue( is_array( $headers ) );
    370         $this->assertEquals( 'image/jpeg', $headers['content-type'] );
    371         $this->assertEquals( '40148', $headers['content-length'] );
    372         $this->assertEquals( '200', wp_remote_retrieve_response_code( $response ) );
    373     }
    374 
    375     function test_head_redirect() {
    376         // this url will 301 redirect
    377         $url = 'http://asdftestblog1.wordpress.com/files/2007/09/2007-06-30-dsc_4700-1.jpg';
    378         $response = wp_remote_head( $url );
    379         $this->assertEquals( '301', wp_remote_retrieve_response_code( $response ) );
    380     }
    381 
    382     function test_head_404() {
    383         $url = 'http://asdftestblog1.files.wordpress.com/2007/09/awefasdfawef.jpg';
    384         $response = wp_remote_head( $url );
    385 
    386         $this->assertTrue( is_array($response) );
    387         $this->assertEquals( '404', wp_remote_retrieve_response_code( $response ) );
    388     }
    389 
    390     function test_get_request() {
    391         $url = 'http://asdftestblog1.files.wordpress.com/2007/09/2007-06-30-dsc_4700-1.jpg';
    392         $file = tempnam('/tmp', 'testfile');
    393 
    394         $headers = wp_get_http($url, $file);
    395 
    396         // should return the same headers as a head request
    397         $this->assertTrue( is_array($headers) );
    398         $this->assertEquals( 'image/jpeg', $headers['content-type'] );
    399         $this->assertEquals( '40148', $headers['content-length'] );
    400         $this->assertEquals( '200', $headers['response'] );
    401 
    402         // make sure the file is ok
    403         $this->assertEquals( 40148, filesize($file) );
    404         $this->assertEquals( 'b0371a0fc575fcf77f62cd298571f53b', md5_file($file) );
    405     }
    406 
    407     function test_get_redirect() {
    408         // this will redirect to asdftestblog1.files.wordpress.com
    409         $url = 'http://asdftestblog1.wordpress.com/files/2007/09/2007-06-30-dsc_4700-1.jpg';
    410         $file = tempnam('/tmp', 'testfile');
    411 
    412         $headers = wp_get_http($url, $file);
    413 
    414         // should return the same headers as a head request
    415         $this->assertTrue( is_array($headers) );
    416         $this->assertEquals( 'image/jpeg', $headers['content-type'] );
    417         $this->assertEquals( '40148', $headers['content-length'] );
    418         $this->assertEquals( '200', $headers['response'] );
    419 
    420         // make sure the file is ok
    421         $this->assertEquals( 40148, filesize($file) );
    422         $this->assertEquals( 'b0371a0fc575fcf77f62cd298571f53b', md5_file($file) );
    423     }
    424 
    425     function test_get_redirect_limit_exceeded() {
    426         // this will redirect to asdftestblog1.files.wordpress.com
    427         $url = 'http://asdftestblog1.wordpress.com/files/2007/09/2007-06-30-dsc_4700-1.jpg';
    428         $file = tempnam('/tmp', 'testfile');
    429         // pretend we've already redirected 5 times
    430         $headers = wp_get_http( $url, $file, 6 );
    431         $this->assertFalse( $headers );
    432     }
    433 }
    434 
    435 /**
    436  * @group themes
    437  * @group plugins
    438  */
    439 class Test_WP_File_Headers extends WP_UnitTestCase {
    440     function test_get_file_data() {
    441         $theme_headers = array(
    442             'Name' => 'Theme Name',
    443             'ThemeURI' => 'Theme URI',
    444             'Description' => 'Description',
    445             'Version' => 'Version',
    446             'Author' => 'Author',
    447             'AuthorURI' => 'Author URI',
    448         );
    449 
    450         $actual = get_file_data( DIR_TESTDATA . '/themedir1/default/style.css', $theme_headers );
    451 
    452         $expected = array(
    453             'Name' => 'WordPress Default',
    454             'ThemeURI' => 'http://wordpress.org/',
    455             'Description' => 'The default WordPress theme based on the famous <a href="http://binarybonsai.com/kubrick/">Kubrick</a>.',
    456             'Version' => '1.6',
    457             'Author' => 'Michael Heilemann',
    458             'AuthorURI' => 'http://binarybonsai.com/',
    459         );
    460 
    461         foreach ( $actual as $header => $value )
    462             $this->assertEquals( $expected[ $header ], $value, $header );
    463     }
    464 
    465     function test_get_file_data_cr_line_endings() {
    466         $headers = array( 'SomeHeader' => 'Some Header', 'Description' => 'Description', 'Author' => 'Author' );
    467         $actual = get_file_data( DIR_TESTDATA . '/formatting/cr-line-endings-file-header.php', $headers );
    468         $expected = array(
    469             'SomeHeader' => 'Some header value!',
    470             'Description' => 'This file is using CR line endings for a testcase.',
    471             'Author' => 'A Very Old Mac',
    472         );
    473 
    474         foreach ( $actual as $header => $value )
    475             $this->assertEquals( $expected[ $header ], $value, $header );
    476     }
    477 }
Note: See TracChangeset for help on using the changeset viewer.