Make WordPress Core

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

Last change on this file since 25380 was 25053, checked in by dd32, 12 years ago

Adding some basic Unit Tests for the Filesystem methods, primarily, WP_Filesystem_Base::find_folder() which is used during FTP upgrades.
This introduces a Mock Filesystem in memory to avoid having to setup a different VM for every combination of filesystem structures possible.

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