1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Contains tests for WP_Hydra::replace_domain() |
4
|
|
|
* |
5
|
|
|
* @package wp-hydra |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Tests for WP_Hydra::replace_domain() |
10
|
|
|
*/ |
11
|
|
|
class WpHydraReplaceDomainTest extends WP_UnitTestCase { |
12
|
|
|
/** |
13
|
|
|
* Test setup |
14
|
|
|
*/ |
15
|
|
|
public function setUp() { |
16
|
|
|
$this->wp_hydra = $this->getMockBuilder( 'WP_Hydra_Exposed_RD' )->setMethods( array( 'is_ssl' ) )->getMock(); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Test teardown |
21
|
|
|
*/ |
22
|
|
|
public function tearDown() { |
23
|
|
|
unset( $this->wp_hydra ); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Test with HTTP URL and HTTPS off. |
28
|
|
|
* |
29
|
|
|
* @covers WP_Hydra::replace_domain |
30
|
|
|
*/ |
31
|
|
View Code Duplication |
public function testWithHttpUrlAndHttpsOff() { |
|
|
|
|
32
|
|
|
$this->wp_hydra->expects( $this->any() ) |
33
|
|
|
->method( 'is_ssl' ) |
34
|
|
|
->will( $this->returnValue( false ) ); |
35
|
|
|
|
36
|
|
|
$url = 'http://example.com/'; |
37
|
|
|
$result = $this->wp_hydra->replace_domain_exposed( $url, 'example.com', 'example.com' ); |
38
|
|
|
$this->assertSame( $url, $result ); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Test with HTTP URL and HTTPS on. |
43
|
|
|
* |
44
|
|
|
* @covers WP_Hydra::replace_domain |
45
|
|
|
*/ |
46
|
|
View Code Duplication |
public function testWithHttpUrlAndHttpsOn() { |
|
|
|
|
47
|
|
|
$this->wp_hydra->expects( $this->any() ) |
48
|
|
|
->method( 'is_ssl' ) |
49
|
|
|
->will( $this->returnValue( true ) ); |
50
|
|
|
|
51
|
|
|
$url = 'http://example.com/'; |
52
|
|
|
$expected = 'https://example.com/'; |
53
|
|
|
$actual = $this->wp_hydra->replace_domain_exposed( $url, 'example.com', 'example.com' ); |
54
|
|
|
$this->assertSame( $expected, $actual ); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Test with HTTPS URL and HTTPS off. |
59
|
|
|
* |
60
|
|
|
* @covers WP_Hydra::replace_domain |
61
|
|
|
*/ |
62
|
|
View Code Duplication |
public function testWithHttpsUrlAndHttpsOff() { |
|
|
|
|
63
|
|
|
$this->wp_hydra->expects( $this->any() ) |
64
|
|
|
->method( 'is_ssl' ) |
65
|
|
|
->will( $this->returnValue( false ) ); |
66
|
|
|
|
67
|
|
|
$url = 'https://example.com/'; |
68
|
|
|
$expected = 'http://example.com/'; |
69
|
|
|
$actual = $this->wp_hydra->replace_domain_exposed( $url, 'example.com', 'example.com' ); |
70
|
|
|
$this->assertSame( $expected, $actual ); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Test with HTTPS URL and HTTPS on. |
75
|
|
|
* |
76
|
|
|
* @covers WP_Hydra::replace_domain |
77
|
|
|
*/ |
78
|
|
View Code Duplication |
public function testWithHttpsUrlAndHttpsOn() { |
|
|
|
|
79
|
|
|
$this->wp_hydra->expects( $this->any() ) |
80
|
|
|
->method( 'is_ssl' ) |
81
|
|
|
->will( $this->returnValue( true ) ); |
82
|
|
|
|
83
|
|
|
$url = 'https://example.com/'; |
84
|
|
|
$actual = $this->wp_hydra->replace_domain_exposed( $url, 'example.com', 'example.com' ); |
85
|
|
|
$this->assertSame( $url, $actual ); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Test with HTTP URL and HTTPS off and different domain. |
90
|
|
|
* |
91
|
|
|
* @covers WP_Hydra::replace_domain |
92
|
|
|
*/ |
93
|
|
View Code Duplication |
public function testWithHttpUrlAndHttpsOffAndDifferentDomain() { |
|
|
|
|
94
|
|
|
$this->wp_hydra->expects( $this->any() ) |
95
|
|
|
->method( 'is_ssl' ) |
96
|
|
|
->will( $this->returnValue( false ) ); |
97
|
|
|
|
98
|
|
|
$url = 'http://example.com/'; |
99
|
|
|
$expected = 'http://foobar.com/'; |
100
|
|
|
$result = $this->wp_hydra->replace_domain_exposed( $url, 'example.com', 'foobar.com' ); |
101
|
|
|
$this->assertSame( $expected, $result ); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Test with HTTP URL and HTTPS on and different domain. |
106
|
|
|
* |
107
|
|
|
* @covers WP_Hydra::replace_domain |
108
|
|
|
*/ |
109
|
|
View Code Duplication |
public function testWithHttpUrlAndHttpsOnAndDifferentDomain() { |
|
|
|
|
110
|
|
|
$this->wp_hydra->expects( $this->any() ) |
111
|
|
|
->method( 'is_ssl' ) |
112
|
|
|
->will( $this->returnValue( true ) ); |
113
|
|
|
|
114
|
|
|
$url = 'http://example.com/'; |
115
|
|
|
$expected = 'https://foobar.com/'; |
116
|
|
|
$actual = $this->wp_hydra->replace_domain_exposed( $url, 'example.com', 'foobar.com' ); |
117
|
|
|
$this->assertSame( $expected, $actual ); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Test with HTTPS URL and HTTPS off and different domain. |
122
|
|
|
* |
123
|
|
|
* @covers WP_Hydra::replace_domain |
124
|
|
|
*/ |
125
|
|
View Code Duplication |
public function testWithHttpsUrlAndHttpsOffAndDifferentDomain() { |
|
|
|
|
126
|
|
|
$this->wp_hydra->expects( $this->any() ) |
127
|
|
|
->method( 'is_ssl' ) |
128
|
|
|
->will( $this->returnValue( false ) ); |
129
|
|
|
|
130
|
|
|
$url = 'https://example.com/'; |
131
|
|
|
$expected = 'http://foobar.com/'; |
132
|
|
|
$actual = $this->wp_hydra->replace_domain_exposed( $url, 'example.com', 'foobar.com' ); |
133
|
|
|
$this->assertSame( $expected, $actual ); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Test with HTTPS URL and HTTPS on and different domain. |
138
|
|
|
* |
139
|
|
|
* @covers WP_Hydra::replace_domain |
140
|
|
|
*/ |
141
|
|
View Code Duplication |
public function testWithHttpsUrlAndHttpsOnAndDifferentDomain() { |
|
|
|
|
142
|
|
|
$this->wp_hydra->expects( $this->any() ) |
143
|
|
|
->method( 'is_ssl' ) |
144
|
|
|
->will( $this->returnValue( true ) ); |
145
|
|
|
|
146
|
|
|
$url = 'https://example.com/'; |
147
|
|
|
$expected = 'https://foobar.com/'; |
148
|
|
|
$actual = $this->wp_hydra->replace_domain_exposed( $url, 'example.com', 'foobar.com' ); |
149
|
|
|
$this->assertSame( $expected, $actual ); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Class that exposes the private WP_Hydra::replace_domain() method. |
156
|
|
|
*/ |
157
|
|
|
class WP_Hydra_Exposed_RD extends WP_Hydra { |
158
|
|
|
/** |
159
|
|
|
* Exposes the private WP_Hydra::replace_domain() method. |
160
|
|
|
* |
161
|
|
|
* @param string $url The domain. |
162
|
|
|
* @param string $original_domain The original domain. |
163
|
|
|
* @param string $current_domain The current domain. |
164
|
|
|
* @return string String with domain replaced. |
165
|
|
|
*/ |
166
|
|
|
public function replace_domain_exposed( $url, $original_domain, $current_domain ) { |
167
|
|
|
return $this->replace_domain( $url, $original_domain, $current_domain ); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.