summaryrefslogtreecommitdiffstatshomepage
path: root/tests/phpunit/includes/mock-fs.php
blob: 40705dc41141dacc01d224c7e2b8d798821ec3c6 (plain) (blame)
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
<?php
class WP_Filesystem_MockFS extends WP_Filesystem_Base {
	private $cwd;

	// Holds a array of objects which contain an array of objects, etc.
	private $fs = null;

	// Holds a array of /path/to/file.php and /path/to/dir/ map to an object in $fs above.
	// A fast, more efficient way of determining if a path exists, and access to that node.
	private $fs_map = array();

	public $verbose = false; // Enable to debug WP_Filesystem_Base::find_folder() / etc.
	public $errors  = array();
	public $method  = 'MockFS';

	public function __construct() {}

	public function connect() {
		return true;
	}

	// Copy of core's function, but accepts a path.
	public function abspath( $path = false ) {
		if ( ! $path ) {
			$path = ABSPATH;
		}
		$folder = $this->find_folder( $path );

		// Perhaps the FTP folder is rooted at the WordPress installation.
		// Check for wp-includes folder in root, could have some false positives, but rare.
		if ( ! $folder && $this->is_dir( '/wp-includes' ) ) {
			$folder = '/';
		}
		return $folder;
	}

	// Mock FS-specific functions:

	/**
	 * Sets initial filesystem environment and/or clears the current environment.
	 * Can also be passed the initial filesystem to be setup which is passed to self::setfs()
	 */
	public function init( $paths = '', $home_dir = '/' ) {
		$this->fs     = new MockFS_Directory_Node( '/' );
		$this->fs_map = array(
			'/' => $this->fs,
		);
		$this->cache  = array(); // Used by find_folder() and friends.
		$this->cwd    = isset( $this->fs_map[ $home_dir ] ) ? $this->fs_map[ $home_dir ] : '/';
		$this->setfs( $paths );
	}

	/**
	 * "Bulk Loads" a filesystem into the internal virtual filesystem
	 */
	public function setfs( $paths ) {
		if ( ! is_array( $paths ) ) {
			$paths = explode( "\n", $paths );
		}

		$paths = array_filter( array_map( 'trim', $paths ) );

		foreach ( $paths as $path ) {
			// Allow for comments.
			if ( '#' === $path[0] ) {
				continue;
			}

			// Directories.
			if ( '/' === $path[ strlen( $path ) - 1 ] ) {
				$this->mkdir( $path );
			} else { // Files (with dummy content for now).
				$this->put_contents( $path, 'This is a test file' );
			}
		}
	}

	/**
	 * Locates a filesystem "node"
	 */
	private function locate_node( $path ) {
		return isset( $this->fs_map[ $path ] ) ? $this->fs_map[ $path ] : false;
	}

	/**
	 * Locates a filesystem node for the parent of the given item
	 */
	private function locate_parent_node( $path ) {
		$dirname = str_replace( '\\', '/', dirname( $path ) );
		return $this->locate_node( trailingslashit( $dirname ) );
	}

	// Here starteth the WP_Filesystem functions.

	public function mkdir( $path, /* Optional args are ignored */ $chmod = false, $chown = false, $chgrp = false ) {
		$path = trailingslashit( $path );

		$parent_node = $this->locate_parent_node( $path );
		if ( ! $parent_node ) {
			$dirname = str_replace( '\\', '/', dirname( $path ) );
			$this->mkdir( $dirname );
			$parent_node = $this->locate_parent_node( $path );
			if ( ! $parent_node ) {
				return false;
			}
		}

		$node = new MockFS_Directory_Node( $path );

		$parent_node->children[ $node->name ] = $node;
		$this->fs_map[ $path ]                = $node;

		return true;
	}

	public function put_contents( $path, $contents = '', $mode = null ) {
		if ( ! $this->is_dir( dirname( $path ) ) ) {
			$this->mkdir( dirname( $path ) );
		}

		$parent   = $this->locate_parent_node( $path );
		$new_file = new MockFS_File_Node( $path, $contents );

		$parent->children[ $new_file->name ] = $new_file;
		$this->fs_map[ $path ]               = $new_file;
	}

	public function get_contents( $file ) {
		if ( ! $this->is_file( $file ) ) {
			return false;
		}
		return $this->fs_map[ $file ]->contents;
	}

	public function cwd() {
		return $this->cwd->path;
	}

	public function chdir( $path ) {
		if ( ! isset( $this->fs_map[ $path ] ) ) {
			return false;
		}

		$this->cwd = $this->fs_map[ $path ];
		return true;
	}

	public function exists( $path ) {
		return isset( $this->fs_map[ $path ] ) || isset( $this->fs_map[ trailingslashit( $path ) ] );
	}

	public function is_file( $file ) {
		return isset( $this->fs_map[ $file ] ) && $this->fs_map[ $file ]->is_file();
	}

	public function is_dir( $path ) {
		$path = trailingslashit( $path );

		return isset( $this->fs_map[ $path ] ) && $this->fs_map[ $path ]->is_dir();
	}

	public function dirlist( $path = '.', $include_hidden = true, $recursive = false ) {

		if ( empty( $path ) || '.' === $path ) {
			$path = $this->cwd();
		}

		if ( ! $this->exists( $path ) ) {
			return false;
		}

		$limit_file = false;
		if ( $this->is_file( $path ) ) {
			$limit_file = $this->locate_node( $path )->name;
			$path       = dirname( $path ) . '/';
		}

		$ret = array();
		foreach ( $this->fs_map[ $path ]->children as $entry ) {
			if ( '.' === $entry->name || '..' === $entry->name ) {
				continue;
			}

			if ( ! $include_hidden && '.' === $entry->name ) {
				continue;
			}

			if ( $limit_file && $entry->name !== $limit_file ) {
				continue;
			}

			$struc         = array();
			$struc['name'] = $entry->name;
			$struc['type'] = $entry->type;

			if ( 'd' === $struc['type'] ) {
				if ( $recursive ) {
					$struc['files'] = $this->dirlist( trailingslashit( $path ) . trailingslashit( $struc['name'] ), $include_hidden, $recursive );
				} else {
					$struc['files'] = array();
				}
			}

			$ret[ $entry->name ] = $struc;
		}
		return $ret;
	}
}

class MockFS_Node {
	public $name; // The "name" of the entry, does not include a slash (exception, root).
	public $type; // The type of the entry 'f' for file, 'd' for directory.
	public $path; // The full path to the entry.

	public function __construct( $path ) {
		$this->path = $path;
		$this->name = basename( $path );
	}

	public function is_file() {
		return 'f' === $this->type;
	}

	public function is_dir() {
		return 'd' === $this->type;
	}
}

class MockFS_Directory_Node extends MockFS_Node {
	public $type     = 'd';
	public $children = array(); // The child nodes of this directory.
}

class MockFS_File_Node extends MockFS_Node {
	public $type     = 'f';
	public $contents = ''; // The contents of the file.

	public function __construct( $path, $contents = '' ) {
		parent::__construct( $path );
		$this->contents = $contents;
	}
}