1
|
|
|
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
2
|
|
|
/** |
3
|
|
|
* File for testing Redirects. |
4
|
|
|
* |
5
|
|
|
* @package Automattic/jetpack-redirect |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Automattic\Jetpack; |
9
|
|
|
|
10
|
|
|
use Brain\Monkey; |
11
|
|
|
use Brain\Monkey\Functions; |
12
|
|
|
use PHPUnit\Framework\TestCase; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Test Redirect class |
16
|
|
|
*/ |
17
|
|
|
class RedirectTest extends TestCase { |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Test setup. |
21
|
|
|
*/ |
22
|
|
|
public function setUp() { |
23
|
|
|
Monkey\setUp(); |
24
|
|
|
$this->status = new Status(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Test teardown. |
29
|
|
|
*/ |
30
|
|
|
public function tearDown() { |
31
|
|
|
Monkey\tearDown(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Basic tests to get_url method. |
36
|
|
|
*/ |
37
|
|
|
public function test_get_url() { |
38
|
|
|
Functions\when( 'home_url' )->justReturn( 'https://example.org' ); |
39
|
|
|
|
40
|
|
|
$url = Redirect::get_url( 'simple' ); |
41
|
|
|
$this->assertEquals( 'https://jetpack.com/redirect/?source=simple&site=example.org', $url ); |
42
|
|
|
|
43
|
|
|
// Test invalid parameter. |
44
|
|
|
$url = Redirect::get_url( 'simple', array( 'invalid' => 'value' ) ); |
45
|
|
|
$this->assertEquals( 'https://jetpack.com/redirect/?source=simple&site=example.org', $url ); |
46
|
|
|
|
47
|
|
|
// Test path. |
48
|
|
|
$url = Redirect::get_url( 'simple', array( 'path' => 'value' ) ); |
49
|
|
|
$this->assertEquals( 'https://jetpack.com/redirect/?source=simple&site=example.org&path=value', $url ); |
50
|
|
|
|
51
|
|
|
// Test path special chars. |
52
|
|
|
$url = Redirect::get_url( 'simple', array( 'path' => 'weird value!' ) ); |
53
|
|
|
$v = rawurlencode( 'weird value!' ); |
54
|
|
|
$this->assertEquals( 'https://jetpack.com/redirect/?source=simple&site=example.org&path=' . $v, $url ); |
55
|
|
|
|
56
|
|
|
// Test query. |
57
|
|
|
$url = Redirect::get_url( 'simple', array( 'query' => 'value' ) ); |
58
|
|
|
$this->assertEquals( 'https://jetpack.com/redirect/?source=simple&site=example.org&query=value', $url ); |
59
|
|
|
|
60
|
|
|
// Test query special chars. |
61
|
|
|
$url = Redirect::get_url( 'simple', array( 'query' => 'key=value&key2=value2' ) ); |
62
|
|
|
$v = rawurlencode( 'key=value&key2=value2' ); |
63
|
|
|
$this->assertEquals( 'https://jetpack.com/redirect/?source=simple&site=example.org&query=' . $v, $url ); |
64
|
|
|
|
65
|
|
|
// Test anchor. |
66
|
|
|
$url = Redirect::get_url( 'simple', array( 'anchor' => 'value' ) ); |
67
|
|
|
$this->assertEquals( 'https://jetpack.com/redirect/?source=simple&site=example.org&anchor=value', $url ); |
68
|
|
|
|
69
|
|
|
// Test anchor special chars. |
70
|
|
|
$url = Redirect::get_url( 'simple', array( 'anchor' => 'key=value&key2=value2' ) ); |
71
|
|
|
$v = rawurlencode( 'key=value&key2=value2' ); |
72
|
|
|
$this->assertEquals( 'https://jetpack.com/redirect/?source=simple&site=example.org&anchor=' . $v, $url ); |
73
|
|
|
|
74
|
|
|
// Test informing URL. |
75
|
|
|
$url = Redirect::get_url( 'https://wordpress.com/support' ); |
76
|
|
|
$v = rawurlencode( 'https://wordpress.com/support' ); |
77
|
|
|
$this->assertEquals( 'https://jetpack.com/redirect/?url=' . $v . '&site=example.org', $url ); |
78
|
|
|
|
79
|
|
|
// Test URL and query. |
80
|
|
|
$url = Redirect::get_url( 'https://wordpress.com/support', array( 'query' => 'key=value&key2=value2' ) ); |
81
|
|
|
$v = rawurlencode( 'key=value&key2=value2' ); |
82
|
|
|
$v_url = rawurlencode( 'https://wordpress.com/support' ); |
83
|
|
|
$this->assertEquals( 'https://jetpack.com/redirect/?url=' . $v_url . '&site=example.org&query=' . $v, $url ); |
84
|
|
|
|
85
|
|
|
// Test URL and query, discarding info from url. |
86
|
|
|
$url = Redirect::get_url( 'https://wordpress.com/support?this=that#super', array( 'query' => 'key=value&key2=value2' ) ); |
87
|
|
|
$v = rawurlencode( 'key=value&key2=value2' ); |
88
|
|
|
$v_url = rawurlencode( 'https://wordpress.com/support' ); |
89
|
|
|
$this->assertEquals( 'https://jetpack.com/redirect/?url=' . $v_url . '&site=example.org&query=' . $v, $url ); |
90
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
} |
94
|
|
|
|