Welcome to mirror list, hosted at ThFree Co, Russian Federation.

general.php « format « tests « tests - github.com/YOURLS/YOURLS.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3dbf834dc5f58fb0ee47803f552df52f8c30b760 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<?php

/**
 * General formatting functions.
 *
 * @group formatting
 * @since 0.1
 */
class Format_General extends PHPUnit\Framework\TestCase {

    /**
     * Data to serialize
     */
    function serialize_data() {
        return array(
            array( null ),
            array( true ),
            array( false ),
            array( -25 ),
            array( 25 ),
            array( 1.1 ),
            array( 'this string will be serialized' ),
            array( "a\nb" ),
            array( array() ),
            array( array(1,1,2,3,5,8,13) ),
            array( (object)array('test' => true, '3', 4) ),
        );
    }

    /**
     * Unserialized data
     */
    function not_serialized_data() {
        return array(
            array( 'a string' ),
            array( 'garbage:a:0:garbage;' ),
            // array( 'b:4;' ), // this test fails in WP test suite, not sure if intentional or what...
            array( 's:4:test;' ),
        );
    }

    /**
     * Check that yourls_is_serialized detects serialized data
     *
     * @dataProvider serialize_data
     * @since 0.1
     */
    public function test_is_serialized( $data ) {
        $this->assertTrue( yourls_is_serialized( serialize( $data ) ) );
    }

    /**
     * Check that yourls_is_serialized doesn't assume garbage is serialized
     *
     * @dataProvider not_serialized_data
     * @since 0.1
     */
    public function test_is_not_serialized( $data ) {
        $this->assertFalse( yourls_is_serialized( $data ) );
    }

    /**
     * Integer (1337) to string (3jk) to integer
     *
     * @since 0.1
     */
    public function test_int_to_string_to_int() {
        // 10 random integers
        $rnd = array();
        for( $i=0; $i<10; $i++ ) {
            $rnd[]= mt_rand( 1, 1000000 );
        }

        foreach( $rnd as $integer ) {
            $this->assertEquals( $integer, yourls_string2int( yourls_int2string( $integer ) ) );
        }

    }

    /**
     * String (3jk) to integer (1337) to string
     *
     * @since 0.1
     */
    public function test_string_to_int_to_string() {
        // 10 random strings that do not start with a zero
        $rnd = array();
        $i = 0;
        while( $i < 10 ) {
            if( $notempty = ltrim( rand_str( mt_rand( 2, 10 ) ), '0' ) ) {
                $rnd[]= $notempty;
                $i++;
            }
        }

        foreach( $rnd as $string ) {
            $this->assertEquals( $string, yourls_int2string( yourls_string2int( $string ) ) );
        }
    }

    /**
     * Checking that yourls_unique_element_id is a unique string
     *
     */
    public function test_string2htmlid() {
        $id1 = yourls_unique_element_id();
        $id2 = yourls_unique_element_id();
        $this->assertIsString($id1);
        $this->assertIsString($id2);
        $this->assertNotSame($id1, $id2);
    }

    /**
     * Generating valid regexp from the allowed charset
     *
     * @since 0.1
     */
    function test_valid_regexp() {
        $pattern = yourls_make_regexp_pattern( yourls_get_shorturl_charset() );

        /* To validate a RegExp just run it against an empty string.
           If it returns explicit false (=== false), it's broken. Otherwise it's valid.
           From: https://stackoverflow.com/a/12941133/36850
           Cool to know :)

           We're testing it as used in yourls_sanitize_keyword()
           TODO: more random char strings to test?
        */

        $this->assertFalse( preg_match( '![^' . $pattern . ']!', '' ) === false );
    }

    /**
     * Trim long strings
     *
     * @since 0.1
     */
    function test_trim_long_strings() {
        $long = "The Plague That Makes Your Booty Move... It's The Infectious Grooves";
        $trim = "The Plague That Makes Your Booty Move... It's The Infec[...]";
        $this->assertSame( $trim, yourls_trim_long_string( $long ) );

        $long = "The Plague That Makes Your Booty Move... It's The Infectious Grooves";
        $trim = "The Plague That Makes Your Booty[...]";
        $this->assertSame( $trim, yourls_trim_long_string( $long, 37 ) );

        $long = "The Plague That Makes Your Booty Move... It's The Infectious Grooves";
        $trim = "The Plague That Makes Your Booty Mo..";
        $this->assertSame( $trim, yourls_trim_long_string( $long, 37, '..' ) );
    }

    /**
     * Return true for UTF8 strings
     *
     * Note: As of 1.7.1, function yourls_seem_utf8() is still unused. In 2.0 consider simply deleting it if still not needed
     *
     * @dataProvider valid_utf8
     * @since 0.1
     */
    function test_is_utf8( $string ) {
        $this->assertTrue( yourls_seems_utf8( $string ) );
    }

    /**
     * Return false for non UTF8 strings
     *
     * Note: As of 1.7.1, function yourls_seem_utf8() is still unused. In 2.0 consider simply deleting it if still not needed
     *
     * @dataProvider invalid_utf8
     * @since 0.1
     */
    function test_is_not_utf8( $string ) {
        $this->assertFalse( yourls_seems_utf8( $string ) );
    }

    function valid_utf8() {
        return $this->get_data( YOURLS_TESTDATA_DIR . '/formatting/utf-8.txt' );
    }

    function invalid_utf8() {
        return $this->get_data( YOURLS_TESTDATA_DIR . '/formatting/big5.txt' );
    }

    /**
     * Parse a file and return its content as a data provider
     */
    function get_data( $filename ) {
        $strings = file( $filename );
        foreach ( $strings as &$string ) {
            $string = (array) trim( $string );
        }
        unset( $string );
        return $strings;
    }

    /**
     * Test yourls_backslashit
     *
     * @since 0.1
     */
    function test_backslashit() {
        $this->assertSame( yourls_backslashit( 'hello world 123 !' ), '\h\e\l\l\o \w\o\r\l\d 123 !' );
        $this->assertSame( yourls_backslashit( '1, 2, 3' ), '\\\1, 2, 3' );
    }

    /**
     * Test the bookmarklet generator
     *
     * Note: we're not testing that the bookmarklet generator produces valid JS code: the
     * bookmarklet class has tests for this, see https://github.com/ozh/bookmarkletgen
     * We're just testing that content is returned
     *
     * @since 0.1
     */
    function test_bookmarklet() {
        $code = yourls_make_bookmarklet( 'hello' );
        $this->assertTrue( is_string( $code ) );
    }

    /**
     * Test yourls_specialchars basics
     *
     * @since 0.1
     */
    function test_specialchars_decode_basics() {
        $html =  "&amp;&lt;hello world&gt;";
        $this->assertEquals( $html, yourls_specialchars( $html ) );

        $double = "&amp;amp;&amp;lt;hello world&amp;gt;";
        $this->assertEquals( $double, yourls_specialchars( $html, ENT_NOQUOTES, true ) );
    }

    /**
     * Test yourls_specialchars escape quotes
     *
     * @since 0.1
     */
    function test_specialchars_escapes_quotes() {
        $source = "\"'hello!'\"";
        $this->assertEquals( '"&#039;hello!&#039;"', yourls_specialchars( $source, 'single' ) );
        $this->assertEquals( "&quot;'hello!'&quot;", yourls_specialchars( $source, 'double' ) );
        $this->assertEquals( '&quot;&#039;hello!&#039;&quot;', yourls_specialchars( $source, ENT_QUOTES, true ) );
        $this->assertEquals( '&quot;&#039;hello!&#039;&quot;', yourls_specialchars( $source, 'omg', true ) ); // unaccepted value should be treated as ENT_QUOTES
        $this->assertEquals( $source, yourls_specialchars( $source ) );
    }

    /**
     * Test yourls_specialchars doesn't change allowed entities
     *
     * @since 0.1
     */
    function test_specialchars_allowed_entities() {
        foreach ( yourls_kses_allowed_entities() as $ent ) {
            $ent = '&' . $ent . ';';
            $this->assertEquals( $ent, yourls_specialchars( $ent ) );
        }
    }

    /**
     * Test yourls_specialchars with unallowed entities
     *
     * @since 0.1
     */
    function test_specialchars_unallowed_entities() {
        $ents = array( 'iacut', 'aposs', 'pos', 'apo', 'apo?', 'apo.*', '.*apo.*', 'apos ', ' apos', ' apos ' );

        foreach ( $ents as $ent ) {
            $escaped = '&amp;' . $ent . ';';
            $ent = '&' . $ent . ';';
            $this->assertEquals( $escaped, yourls_specialchars( $ent ) );
        }
    }

}