-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathminify-f-array-stubs.php
More file actions
142 lines (129 loc) · 3.42 KB
/
Copy pathminify-f-array-stubs.php
File metadata and controls
142 lines (129 loc) · 3.42 KB
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
<?php
/**
* Namespaced stubs for the standalone minify f_array security regression test.
*
* Defines just enough of the W3TC + bundled-Minify runtime to exercise the real
* `\W3TCL\Minify\Minify_Controller_MinApp::setupSources()` sink in isolation:
*
* - `\W3TC\Util_Environment` — faithful copies of `realpath()` / `normalize_path()`
* (lexical path collapse, no filesystem access — mirrors production exactly) plus
* a test-settable `document_root()`.
* - `\W3TCL\Minify\Minify` — the three symbols `Minify_Source` / `Minify_Controller_MinApp`
* reference at runtime (`URL_DEBUG`, `$uploaderHoursBehind`, `$recoverableError`).
* - WordPress `sanitize_text_field()` / `wp_unslash()` shims.
*
* Kept in a separate file because the runtime classes live under `namespace` blocks,
* which cannot be mixed with the test's top-level (global-namespace) body.
*
* @package W3TC\Tests
* @since 2.10.0
*/
namespace W3TC {
if ( ! \class_exists( 'W3TC\\Util_Environment' ) ) {
/**
* Minimal Util_Environment stub. `realpath()` / `normalize_path()` are
* verbatim copies of the production implementation so the test resolves
* paths exactly as the live code does.
*/
class Util_Environment {
/**
* Test-settable document root.
*
* @var string
*/
public static $docroot = '';
/**
* Returns the (test-controlled) document root.
*
* @return string
*/
public static function document_root() {
return self::$docroot;
}
/**
* Lexically normalizes a path (collapses separators, trims trailing slash).
*
* @param string $path Path.
*
* @return string
*/
public static function normalize_path( $path ) {
$path = \preg_replace( '~[/\\\]+~', '/', $path );
$path = \rtrim( $path, '/' );
return $path;
}
/**
* Lexically collapses `.` / `..` segments without touching the filesystem.
*
* @param string $path Path.
*
* @return string
*/
public static function realpath( $path ) {
$path = self::normalize_path( $path );
$parts = \explode( '/', $path );
$absolutes = array();
foreach ( $parts as $part ) {
if ( '.' === $part ) {
continue;
}
if ( '..' === $part ) {
\array_pop( $absolutes );
} else {
$absolutes[] = $part;
}
}
return \implode( '/', $absolutes );
}
}
}
}
namespace W3TCL\Minify {
if ( ! \class_exists( 'W3TCL\\Minify\\Minify' ) ) {
/**
* Stub of the bundled Minify facade — only the members the controller
* and source classes touch at runtime are defined.
*/
class Minify {
const URL_DEBUG = 'https://example.com/minify-debug';
/**
* Clock-skew offset (hours). Zero for tests.
*
* @var int
*/
public static $uploaderHoursBehind = 0;
/**
* Last recoverable error.
*
* @var mixed
*/
public static $recoverableError = null;
}
}
}
namespace {
if ( ! \function_exists( 'sanitize_text_field' ) ) {
/**
* Trivial sanitize_text_field shim.
*
* @param mixed $value Value.
*
* @return mixed
*/
function sanitize_text_field( $value ) {
return \is_string( $value ) ? \trim( $value ) : $value;
}
}
if ( ! \function_exists( 'wp_unslash' ) ) {
/**
* Trivial wp_unslash shim.
*
* @param mixed $value Value.
*
* @return mixed
*/
function wp_unslash( $value ) {
return \is_string( $value ) ? \stripslashes( $value ) : $value;
}
}
}