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

api-check.php « http « tests « tests - github.com/YOURLS/YOURLS.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7b7141658eb7f660c47c26eb748f1870d5df3a1a (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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
<?php

/**
 * HTTP functions related to api.yourls.org
 *
 * @group http
 * @group AYO
 * @since 0.1
 */
class HTTP_AYO_Tests extends PHPUnit\Framework\TestCase {

    protected $actions, $core_version_checks;

    protected function setUp(): void {
        global $yourls_actions;
        $this->actions = $yourls_actions;
        $this->core_version_checks = yourls_get_option( 'core_version_checks' );
    }

    protected function tearDown(): void {
        yourls_remove_all_filters( 'is_admin' );
        yourls_remove_all_filters( 'shunt_yourls_http_request' );
        global $yourls_actions;
        $yourls_actions = $this->actions;
        yourls_update_option( 'core_version_checks', $this->core_version_checks );
    }

    /**
     * Emulate successful HTTP request to api.yourls.org
     */
    public function fake_http_request_success() {
        $return = (object) array();
        $return->body = '{
            "latest": "1.0.1",
            "zipurl": "https:\/\/api.github.com\/repos\/YOURLS\/YOURLS\/zipball\/1.0.1"
            }';
        $return->success = true;

        return $return;
    }

    /**
     * Emulate failed HTTP request to api.yourls.org
     */
    public function fake_http_request_failure() {
        return 'cURL error 28: Connection timed out after 3000 milliseconds (GET on http://api.yourls.org/)';
    }

    /**
     * Emulate HTTP request to api.yourls.org with a server error
     */
    public function fake_http_request_server_error() {
        $return = (object) array();
        $return->body = 'Error 500';
        $return->success = false;

        return $return;
    }

    /**
     * Check that version checking returns false if host is unreachable, and that failed attemps counter increments
     */
    public function test_api_failed_request() {
        yourls_add_filter('shunt_yourls_http_request', array($this,'fake_http_request_failure') );
        $this->check_and_assert();
    }

    /**
     * Check that version checking returns false if host errors, and that failed attemps counter increments
     */
    public function test_api_failed_request_server_error() {
        yourls_add_filter('shunt_yourls_http_request', array($this,'fake_http_request_server_error') );
        $this->check_and_assert();
    }

    /**
     * Helper function for test_api_failed_request() and test_api_failed_request_server_error()
     */
    public function check_and_assert() {
        $checks = yourls_get_option( 'core_version_checks' );
        if( !is_object( $checks ) ) {
            $checks = new stdClass;
            $checks->failed_attempts = 0;
        }
        $before_check = $checks->failed_attempts;

        $this->assertFalse( yourls_check_core_version() );

        $checks = yourls_get_option( 'core_version_checks' );
        yourls_ut_var_dump( $checks );
        $after_check = $checks->failed_attempts;

        $this->assertEquals( $after_check, $before_check + 1 );
    }

    /**
     * Check that version checking returns expected stuff and updates the relevant option
     *
     * @since 0.1
     */
    public function test_check_core_version() {
        yourls_add_filter('shunt_yourls_http_request', array($this,'fake_http_request_success') );

        $before = yourls_get_option( 'core_version_checks' );
        $check = yourls_check_core_version();
        $after = yourls_get_option( 'core_version_checks' );

        $this->assertNotSame( $before, $after );
        $this->assertTrue( isset( $check->latest ) );
        $this->assertTrue( isset( $check->zipurl ) );
    }

    /**
     * Check that version checking happens only when visiting the admin area
     *
     * @since 0.1
     */
    public function test_check_only_in_admin() {
        yourls_add_filter('shunt_yourls_http_request', array($this,'fake_http_request_success') );

        yourls_add_filter( 'is_admin', 'yourls_return_false' );
        $this->assertFalse( yourls_maybe_check_core_version() );
    }

    /**
     * Generate an object to mock last attempt of checking against api.yourls.org
     */
    public function return_case_object( $failed_attempts, $last_attempt, $version_checked ) {
        $checks = new stdClass();
        $checks->last_result     = rand_str();
        $checks->failed_attempts = $failed_attempts;
        $checks->last_attempt    = $last_attempt;
        $checks->version_checked = $version_checked;

        return $checks;
    }

    /**
     * Provider of various test cases
     */
    public function case_scenario() {

        $return = array();

        /**
         * Case 0 :
         * - a previous check was NEVER done
         * Then : we DO want to check
         */
        $name = 'Case 0';
        $checks = '';
        $expected = true;
        $return[] = array( $name, $checks, $expected );

        // Now all possible cases : see https://gist.github.com/ozh/17ea830b2f688613927f

        /**
         * Case 1 : previous check
         * - was SUCCESSFUL,
         * - was performed MORE than 24 hours ago,
         * - and version checked DID match current version
         * Then : we DO want to check
         */
        $name = 'Case 1';
        $checks = $this->return_case_object(
            0,                  // 0 previously failed attempt
            time() - 26 * 3600, // 26 hours ago
            YOURLS_VERSION      // version match
        );
        $expected = true;
        $return[] = array( $name, $checks, $expected );

        /**
         * Case 2 : previous check
         * - was SUCCESSFUL,
         * - was performed MORE than 24 hours ago,
         * - and version checked DID NOT match current version
         * Then : we DO want to check
         */
        $name = 'Case 2';
        $checks = $this->return_case_object(
            0,                  // 0 previously failed attempt
            time() - 26 * 3600, // 26 hours ago
            rand_str()          // version mismatch
        );
        $expected = true;
        $return[] = array( $name, $checks, $expected );

        /**
         * Case 3 : previous check
         * - was SUCCESSFUL,
         * - was performed LESS than 24 hours ago,
         * - and version checked DID match current version
         * Then : we DON'T want to check
         */
        $name = 'Case 3';
        $checks = $this->return_case_object(
            0,                  // 0 previously failed attempt
            time() - 10 * 3600, // 10 hours ago
            YOURLS_VERSION      // version match
        );
        $expected = false;
        $return[] = array( $name, $checks, $expected );

        /**
         * Case 4 : previous check
         * - was SUCCESSFUL,
         * - was performed LESS than 24 hours ago,
         * - and version checked DID NOT match current version
         * Then : we DO want to check
         */
        $name = 'Case 4';
        $checks = $this->return_case_object(
            0,                  // 0 previously failed attempt
            time() - 10 * 3600, // 10 hours ago
            rand_str()          // version mismatch
        );
        $expected = true;
        $return[] = array( $name, $checks, $expected );

        /**
         * Case 5 : previous check
         * - was NOT SUCCESSFUL,
         * - was performed MORE than 2 hours ago,
         * - and version checked DID match current version
         * Then : we DO want to check
         */
        $name = 'Case 5';
        $checks = $this->return_case_object(
            1337,               // some previously failed attempts
            time() - 3 * 3600,  // 3 hours ago
            YOURLS_VERSION      // version match
        );
        $expected = true;
        $return[] = array( $name, $checks, $expected );

        /**
         * Case 6 : previous check
         * - was NOT SUCCESSFUL,
         * - was performed MORE than 2 hours ago,
         * - and version checked DID NOT match current version
         * Then : we DO want to check
         */
        $name = 'Case 6';
        $checks = $this->return_case_object(
            1337,               // some previously failed attempts
            time() - 3 * 3600,  // 3 hours ago
            rand_str()          // version mismatch
        );
        $expected = true;
        $return[] = array( $name, $checks, $expected );

        /**
         * Case 7 : previous check
         * - was NOT SUCCESSFUL,
         * - was performed LESS than 2 hours ago,
         * - and version checked DID match current version
         * Then : we DON'T want to check
         */
        $name = 'Case 7';
        $checks = $this->return_case_object(
            1337,               // some previously failed attempts
            time() - 1 * 3600,  // 1 hour ago
            YOURLS_VERSION      // version match
        );
        $expected = false;
        $return[] = array( $name, $checks, $expected );

        /**
         * Case 8 : previous check
         * - was NOT SUCCESSFUL,
         * - was performed LESS than 2 hours ago,
         * - and version checked DID NOT match current version
         * Then : we DO want to check
         */
        $name = 'Case 8';
        $checks = $this->return_case_object(
            1337,               // some previously failed attempts
            time() - 1 * 3600,  // 1 hour ago
            rand_str()          // version mismatch
        );
        $expected = true;
        $return[] = array( $name, $checks, $expected );

        return $return;
    }

    /**
     * Check if we should poll api.yourls.org under various circumstances
     *
     * @dataProvider case_scenario
     * @since 0.1
     */
    public function test_api_check_in_various_scenario( $name, $checks, $expected ) {
        yourls_add_filter('shunt_yourls_http_request', array($this,'fake_http_request_success') );

        yourls_add_filter( 'is_admin', 'yourls_return_true' );
        yourls_update_option( 'core_version_checks', $checks );

        $this->assertSame( $expected, yourls_maybe_check_core_version() );
    }

    /**
     *  Provide fake JSON responses from api.yourls.org and a boolean stating if they should be accepted or not
     */
    public function json_responses() {
        $return = array();

        $return['expected'] = array(
            (object)array(
                'latest' => '1.2.3',
                'zipurl' => 'https://api.github.com/repos/YOURLS/YOURLS/zipball/1.2.3',
            ),
            true);

        $return['unexpected version number format'] = array(
            (object)array(
                'latest' => '1.2.3-something',
                'zipurl' => 'https://api.github.com/repos/YOURLS/YOURLS/zipball/1.2.3',
            ),
            false);

        $return['version mismatch'] = array(
            (object)array(
                'latest' => '1.2.3',
                'zipurl' => 'https://api.github.com/repos/YOURLS/YOURLS/zipball/1.2.4',
            ),
            false);

        $return['url not part of github.com'] = array(
            (object)array(
                'latest' => '1.2.3',
                'zipurl' => 'https://notgithub.com/repos/YOURLS/YOURLS/zipball/1.2.3',
            ),
            false);

        $return['no version'] = array(
            (object)array(
                'zipurl' => 'https://api.github.com/repos/YOURLS/YOURLS/zipball/1.2.3',
            ),
            false);

        $return['no URL'] = array(
            (object)array(
                'latest' => '1.2.3',
            ),
            false);

        $return['nothing 1'] = array(
            (object)[],
            false);

        $return['nothing 2'] = array([],
            false);

        $return['nothing 3'] = array(false,
            false);

        return $return;
    }

    /**
     * Validate various json responses from api.yourls.org and make sure they're legit
     *
     * @dataProvider json_responses
     * @since 0.1
     */
    public function test_validate_api_json_response($json, $expected) {
        $this->assertSame( $expected, yourls_validate_core_version_response($json) );
    }

    /**
     * Provide various scenarios for version reported by api.yourls.org / current version
     */
    public function new_version_scenarios() {
        $return = array();

        //            AYO       current      notice
        $return[] = ['1.2.3',  '1.2.2',      true];  // new version - display notice
/*        $return[] = ['1.3',    '1.2.2',      true];  // new version - display notice
        $return[] = ['1.3',    '1.22',       true];  // older version - don't display version
        $return[] = ['1.8.22', '1.8.3',      true];  // new version - display notice
        $return[] = ['1.2.3',  '1.2.3-beta', true];  // new version - display notice
        $return[] = ['1.2.2',  '1.2.2',      false]; // same version - don't display notice
        $return[] = ['1.2.2',  '1.2.3',      false]; // older version - don't display version
        $return[] = ['99.9.9',  false,       false]; // newer version compared to actual current YOURLS version - display notice*/

        return $return;
    }

    /**
     * Test various YOURLS version strings from api.yourls.org, compare them to the actual version
     * and make sure we display the correct update notice
     *
     * @dataProvider new_version_scenarios
     */
    public function test_new_version_notice( $api_version, $current_version, $expected ) {
        // fake the api response
        $check = (object)array(
            'last_result' => (object)array(
                'latest' => $api_version,
            ),
        );
        yourls_add_option('core_version_checks', $check);

        // trigger yourls_core_version_notice() and check we had expected action
        yourls_new_core_version_notice($current_version);
        $this->assertSame($expected, yourls_did_action('new_core_version_notice'));
    }

    /**
     * Test various zipball URLs and get version number from it
     */
    function various_zipball_url_version() {
        $return = [];

        $return[] = ['https://api.github.com/repos/YOURLS/YOURLS/zipball/1.2.3', '1.2.3'];
        $return[] = ['https://api.github.com/repos/YOURLS/YOURLS/zipball/1.2.3-beta', '1.2.3-beta'];
        $return[] = ['https://api.github.com/repos/YOURLS/YOURLS/zipball/1.2.3/lol', 'lol'];
        $return[] = ['http://hey', ''];
        $return[] = ['lol', ''];
        $return[] = ['', ''];

        return $return;
    }

    /**
     * Test various zipball URLs and get version number from it
     *
     * @dataProvider various_zipball_url_version
     */
    public function test_get_version_from_zipball_url($url, $expected) {
        $this->assertSame($expected, yourls_get_version_from_zipball_url($url));
    }


    /**
     * test various core version JSON responses from api.yourls.org
     */
    function get_various_json_response_keys() {
        $return = [];

        $return['latest & zipurl'] = [['latest' => 'ok', 'zipurl' => 'ok'], true];
        $return['no latest'] = [['zipurl' => 'ok'], false];
        $return['no zipurl'] = [['latest' => 'ok'], false];
        $return['latest & other key'] = [['latest' => 'ok', 'other' => 'oops'], false];
        $return['zipurl & other key'] = [['zipurl' => 'ok', 'other' => 'oops'], false];
        $return['nothing'] = [[], false];
        $return['extra key'] = [['latest' => 'ok', 'zipurl' => 'ok', 'extra' => 'oops'], false];

        return $return;
    }

    /**
     * Check yourls_validate_core_version_response_keys() works as expected
     * @dataProvider get_various_json_response_keys
     */
    function test_yourls_validate_core_version_response_keys($json, $expected) {
        $this->assertSame(yourls_validate_core_version_response_keys((object)$json), $expected);
    }

}