Make WordPress Core

source: trunk/tests/phpunit/includes/mock-fs.php

Last change on this file was 56548, checked in by SergeyBiryukov, 22 months ago

Coding Standards: Remove superfluous blank lines at the end of various functions.

Note: This is enforced by WPCS 3.0.0.

Follow-up to [56536], [56547].

Props jrf.
See #59161, #58831.

  • Property svn:eol-style set to native
File size: 6.1 KB
Line 
1<?php
2class WP_Filesystem_MockFS extends WP_Filesystem_Base {
3        private $cwd;
4
5        // Holds a array of objects which contain an array of objects, etc.
6        private $fs = null;
7
8        // Holds a array of /path/to/file.php and /path/to/dir/ map to an object in $fs above.
9        // A fast, more efficient way of determining if a path exists, and access to that node.
10        private $fs_map = array();
11
12        public $verbose = false; // Enable to debug WP_Filesystem_Base::find_folder() / etc.
13        public $errors  = array();
14        public $method  = 'MockFS';
15
16        public function __construct() {}
17
18        public function connect() {
19                return true;
20        }
21
22        // Copy of core's function, but accepts a path.
23        public function abspath( $path = false ) {
24                if ( ! $path ) {
25                        $path = ABSPATH;
26                }
27                $folder = $this->find_folder( $path );
28
29                // Perhaps the FTP folder is rooted at the WordPress installation.
30                // Check for wp-includes folder in root, could have some false positives, but rare.
31                if ( ! $folder && $this->is_dir( '/wp-includes' ) ) {
32                        $folder = '/';
33                }
34                return $folder;
35        }
36
37        // Mock FS-specific functions:
38
39        /**
40         * Sets initial filesystem environment and/or clears the current environment.
41         * Can also be passed the initial filesystem to be setup which is passed to self::setfs()
42         */
43        public function init( $paths = '', $home_dir = '/' ) {
44                $this->fs     = new MockFS_Directory_Node( '/' );
45                $this->fs_map = array(
46                        '/' => $this->fs,
47                );
48                $this->cache  = array(); // Used by find_folder() and friends.
49                $this->cwd    = isset( $this->fs_map[ $home_dir ] ) ? $this->fs_map[ $home_dir ] : '/';
50                $this->setfs( $paths );
51        }
52
53        /**
54         * "Bulk Loads" a filesystem into the internal virtual filesystem
55         */
56        public function setfs( $paths ) {
57                if ( ! is_array( $paths ) ) {
58                        $paths = explode( "\n", $paths );
59                }
60
61                $paths = array_filter( array_map( 'trim', $paths ) );
62
63                foreach ( $paths as $path ) {
64                        // Allow for comments.
65                        if ( '#' === $path[0] ) {
66                                continue;
67                        }
68
69                        // Directories.
70                        if ( '/' === $path[ strlen( $path ) - 1 ] ) {
71                                $this->mkdir( $path );
72                        } else { // Files (with dummy content for now).
73                                $this->put_contents( $path, 'This is a test file' );
74                        }
75                }
76        }
77
78        /**
79         * Locates a filesystem "node"
80         */
81        private function locate_node( $path ) {
82                return isset( $this->fs_map[ $path ] ) ? $this->fs_map[ $path ] : false;
83        }
84
85        /**
86         * Locates a filesystem node for the parent of the given item
87         */
88        private function locate_parent_node( $path ) {
89                $dirname = str_replace( '\\', '/', dirname( $path ) );
90                return $this->locate_node( trailingslashit( $dirname ) );
91        }
92
93        // Here starteth the WP_Filesystem functions.
94
95        public function mkdir( $path, /* Optional args are ignored */ $chmod = false, $chown = false, $chgrp = false ) {
96                $path = trailingslashit( $path );
97
98                $parent_node = $this->locate_parent_node( $path );
99                if ( ! $parent_node ) {
100                        $dirname = str_replace( '\\', '/', dirname( $path ) );
101                        $this->mkdir( $dirname );
102                        $parent_node = $this->locate_parent_node( $path );
103                        if ( ! $parent_node ) {
104                                return false;
105                        }
106                }
107
108                $node = new MockFS_Directory_Node( $path );
109
110                $parent_node->children[ $node->name ] = $node;
111                $this->fs_map[ $path ]                = $node;
112
113                return true;
114        }
115
116        public function put_contents( $path, $contents = '', $mode = null ) {
117                if ( ! $this->is_dir( dirname( $path ) ) ) {
118                        $this->mkdir( dirname( $path ) );
119                }
120
121                $parent   = $this->locate_parent_node( $path );
122                $new_file = new MockFS_File_Node( $path, $contents );
123
124                $parent->children[ $new_file->name ] = $new_file;
125                $this->fs_map[ $path ]               = $new_file;
126        }
127
128        public function get_contents( $file ) {
129                if ( ! $this->is_file( $file ) ) {
130                        return false;
131                }
132                return $this->fs_map[ $file ]->contents;
133        }
134
135        public function cwd() {
136                return $this->cwd->path;
137        }
138
139        public function chdir( $path ) {
140                if ( ! isset( $this->fs_map[ $path ] ) ) {
141                        return false;
142                }
143
144                $this->cwd = $this->fs_map[ $path ];
145                return true;
146        }
147
148        public function exists( $path ) {
149                return isset( $this->fs_map[ $path ] ) || isset( $this->fs_map[ trailingslashit( $path ) ] );
150        }
151
152        public function is_file( $file ) {
153                return isset( $this->fs_map[ $file ] ) && $this->fs_map[ $file ]->is_file();
154        }
155
156        public function is_dir( $path ) {
157                $path = trailingslashit( $path );
158
159                return isset( $this->fs_map[ $path ] ) && $this->fs_map[ $path ]->is_dir();
160        }
161
162        public function dirlist( $path = '.', $include_hidden = true, $recursive = false ) {
163
164                if ( empty( $path ) || '.' === $path ) {
165                        $path = $this->cwd();
166                }
167
168                if ( ! $this->exists( $path ) ) {
169                        return false;
170                }
171
172                $limit_file = false;
173                if ( $this->is_file( $path ) ) {
174                        $limit_file = $this->locate_node( $path )->name;
175                        $path       = dirname( $path ) . '/';
176                }
177
178                $ret = array();
179                foreach ( $this->fs_map[ $path ]->children as $entry ) {
180                        if ( '.' === $entry->name || '..' === $entry->name ) {
181                                continue;
182                        }
183
184                        if ( ! $include_hidden && '.' === $entry->name ) {
185                                continue;
186                        }
187
188                        if ( $limit_file && $entry->name !== $limit_file ) {
189                                continue;
190                        }
191
192                        $struc         = array();
193                        $struc['name'] = $entry->name;
194                        $struc['type'] = $entry->type;
195
196                        if ( 'd' === $struc['type'] ) {
197                                if ( $recursive ) {
198                                        $struc['files'] = $this->dirlist( trailingslashit( $path ) . trailingslashit( $struc['name'] ), $include_hidden, $recursive );
199                                } else {
200                                        $struc['files'] = array();
201                                }
202                        }
203
204                        $ret[ $entry->name ] = $struc;
205                }
206                return $ret;
207        }
208}
209
210class MockFS_Node {
211        public $name; // The "name" of the entry, does not include a slash (exception, root).
212        public $type; // The type of the entry 'f' for file, 'd' for directory.
213        public $path; // The full path to the entry.
214
215        public function __construct( $path ) {
216                $this->path = $path;
217                $this->name = basename( $path );
218        }
219
220        public function is_file() {
221                return 'f' === $this->type;
222        }
223
224        public function is_dir() {
225                return 'd' === $this->type;
226        }
227}
228
229class MockFS_Directory_Node extends MockFS_Node {
230        public $type     = 'd';
231        public $children = array(); // The child nodes of this directory.
232}
233
234class MockFS_File_Node extends MockFS_Node {
235        public $type     = 'f';
236        public $contents = ''; // The contents of the file.
237
238        public function __construct( $path, $contents = '' ) {
239                parent::__construct( $path );
240                $this->contents = $contents;
241        }
242}
Note: See TracBrowser for help on using the repository browser.