<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[25053] trunk/tests: Adding some basic Unit Tests for the Filesystem methods, primarily, WP_Filesystem_Base::find_folder() which is used during FTP upgrades.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://core.trac.wordpress.org/changeset/25053">25053</a></dd>
<dt>Author</dt> <dd>dd32</dd>
<dt>Date</dt> <dd>2013-08-20 05:39:20 +0000 (Tue, 20 Aug 2013)</dd>
</dl>

<h3>Log Message</h3>
<pre>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.</pre>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunktestsincludesmockfsphp">trunk/tests/includes/mock-fs.php</a></li>
<li>trunk/tests/tests/filesystem/</li>
<li><a href="#trunkteststestsfilesystembasephp">trunk/tests/tests/filesystem/base.php</a></li>
<li><a href="#trunkteststestsfilesystemfind_folderphp">trunk/tests/tests/filesystem/find_folder.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunktestsincludesmockfsphp"></a>
<div class="addfile"><h4>Added: trunk/tests/includes/mock-fs.php (0 => 25053)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/tests/includes/mock-fs.php                         (rev 0)
+++ trunk/tests/includes/mock-fs.php    2013-08-20 05:39:20 UTC (rev 25053)
</span><span class="lines">@@ -0,0 +1,226 @@
</span><ins>+<?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';
+
+       function __construct() {}
+
+       function connect() {
+               return true;
+       }
+
+       // Copy of core's function, but accepts a path.
+       function abspath( $path = false ) {
+               if ( ! $path )
+                       $path = ABSPATH;
+               $folder = $this->find_folder( $path );
+
+               // Perhaps the FTP folder is rooted at the WordPress install, 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()
+        */
+       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
+        */
+       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 ) {
+               return $this->locate_node( trailingslashit( dirname( $path ) ) );
+       }
+
+       // Here starteth the WP_Filesystem functions.
+
+       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 ) {
+                       $this->mkdir( dirname( $path ) );
+                       $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;
+       }
+
+       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;
+       }
+
+       function get_contents( $file ) {
+               if ( ! $this->is_file( $file ) )
+                       return false;
+               return $this->fs_map[ $file ]->contents;
+       }
+
+       function cwd() {
+               return $this->cwd->path;
+       }
+
+       function chdir( $path ) {
+               if ( ! isset( $this->fs_map[ $path ] ) )
+                       return false;
+
+               $this->cwd = $this->fs_map[ $path ];
+               return true;
+       }
+
+       function exists( $path ) {
+               return isset( $this->fs_map[ $path ] ) || isset( $this->fs_map[ trailingslashit( $path ) ] );
+       }
+
+       function is_file( $file ) {
+               return isset( $this->fs_map[ $file ] ) && $this->fs_map[ $file ]->is_file();
+       }
+
+       function is_dir( $path ) {
+               $path = trailingslashit( $path );
+
+               return isset( $this->fs_map[ $path ] ) && $this->fs_map[ $path ]->is_dir();
+       }
+
+       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.
+
+       function __construct( $path ) {
+               $this->path = $path;
+               $this->name = basename( $path );
+       }
+
+       function is_file() {
+               return $this->type == 'f';
+       }
+
+       function is_dir() {
+               return $this->type == 'd';
+       }
+}
+
+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
+
+       function __construct( $path, $contents = '' ) {
+               parent::__construct( $path );
+               $this->contents = $contents;
+       }
+}
</ins><span class="cx">\ No newline at end of file
</span></span></pre></div>
<a id="trunkteststestsfilesystembasephp"></a>
<div class="addfile"><h4>Added: trunk/tests/tests/filesystem/base.php (0 => 25053)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/tests/tests/filesystem/base.php                            (rev 0)
+++ trunk/tests/tests/filesystem/base.php       2013-08-20 05:39:20 UTC (rev 25053)
</span><span class="lines">@@ -0,0 +1,43 @@
</span><ins>+<?php
+
+/**
+ * This class is designed to make use of MockFS, a Virtual in-memory filesystem compatible with WP_Filesystem
+ */
+abstract class WP_Filesystem_UnitTestCase extends WP_UnitTestCase {
+       function setUp() {
+               add_filter( 'filesystem_method_file', array( $this, 'filter_abstraction_file' ) );
+               add_filter( 'filesystem_method', array( $this, 'filter_fs_method' ) );
+               WP_Filesystem();
+       }
+
+       function tearDown() {
+               global $wp_filesystem;
+               remove_filter( 'filesystem_method_file', array( $this, 'filter_abstraction_file' ) );
+               remove_filter( 'filesystem_method', array( $this, 'filter_fs_method' ) );
+               unset( $wp_filesystem );
+
+               parent::tearDown();
+       }
+
+       function filter_fs_method( $method ) {
+               return 'MockFS';
+       }
+       function filter_abstraction_file( $file ) {
+               return dirname( dirname( dirname( __FILE__ ) ) ) . '/includes/mock-fs.php';
+       }
+
+       function test_is_MockFS_sane() {
+               global $wp_filesystem;
+               $this->assertTrue( is_a( $wp_filesystem, 'WP_Filesystem_MockFS' ) );
+
+               $wp_filesystem->init('/');
+
+               // Test creation/exists checks
+               $this->assertFalse( $wp_filesystem->is_dir( '/test/' ) );
+               $wp_filesystem->mkdir( '/test' );
+               $this->assertTrue( $wp_filesystem->exists( '/test' ) );
+               $this->assertTrue( $wp_filesystem->is_dir( '/test/' ) );
+               $this->assertFalse( $wp_filesystem->is_file( '/test' ) );
+               //$this->assertFalse( true );
+       }
+}
</ins><span class="cx">\ No newline at end of file
</span></span></pre></div>
<a id="trunkteststestsfilesystemfind_folderphp"></a>
<div class="addfile"><h4>Added: trunk/tests/tests/filesystem/find_folder.php (0 => 25053)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/tests/tests/filesystem/find_folder.php                             (rev 0)
+++ trunk/tests/tests/filesystem/find_folder.php        2013-08-20 05:39:20 UTC (rev 25053)
</span><span class="lines">@@ -0,0 +1,107 @@
</span><ins>+<?php
+
+/**
+ * @group filesystem
+ * @group wp-filesystem
+ */
+class WP_Filesystem_find_folder_UnitTestCases extends WP_Filesystem_UnitTestCase {
+
+       function test_ftp_has_root_access() {
+               global $wp_filesystem;
+               $fs = $wp_filesystem;
+               $fs->init('
+                       /var/www/wordpress/
+                       /var/www/wordpress/wp-includes/
+                       /var/www/wordpress/index.php
+               ');
+
+               $path = $fs->find_folder( '/var/www/wordpress/' );
+               $this->assertEquals( '/var/www/wordpress/', $path );
+
+               $path = $fs->find_folder( '/this/directory/doesnt/exist/' );
+               $this->assertFalse( $path );
+
+       }
+
+       function test_sibling_wordpress_in_subdir() {
+               global $wp_filesystem;
+               $fs = $wp_filesystem;
+               $fs->init('
+                       /www/example.com/wordpress/
+                       /www/example.com/wordpress/wp-includes/
+                       /www/example.com/wordpress/index.php
+                       /www/wp.example.com/wordpress/
+                       /www/wp.example.com/wordpress/wp-includes/
+                       /www/wp.example.com/wordpress/wp-content/
+                       /www/wp.example.com/wordpress/index.php
+                       /www/index.php
+               ');
+
+               $path = $fs->find_folder( '/var/www/example.com/wordpress/' );
+               $this->assertEquals( '/www/example.com/wordpress/', $path );
+               
+               $path = $fs->find_folder( '/var/www/wp.example.com/wordpress/wp-content/' );
+               $this->assertEquals( '/www/wp.example.com/wordpress/wp-content/', $path );
+
+       }
+
+       /**
+        * Two WordPress installs, with one contained within the other
+        * FTP / = /var/www/example.com/ on Disk
+        * example.com at /
+        * wp.example.com at /wp.example.com/wordpress/
+        */
+       function test_subdir_of_another() {
+               global $wp_filesystem;
+               $fs = $wp_filesystem;
+               $fs->init('
+                       /wp.example.com/index.php
+                       /wp.example.com/wordpress/
+                       /wp.example.com/wordpress/wp-includes/
+                       /wp.example.com/wordpress/index.php
+                       /wp-includes/
+                       /index.php
+               ');
+
+               $path = $fs->abspath( '/var/www/example.com/wp.example.com/wordpress/' );
+               $this->assertEquals( '/wp.example.com/wordpress/', $path );
+               
+               $path = $fs->abspath( '/var/www/example.com/' );
+               $this->assertEquals( '/', $path );
+
+       }
+
+       /**
+        * Test the WordPress ABSPATH containing TWO tokens (www) of which exists in the current FTP home.
+        *
+        * @ticket 20934
+        */
+       function test_multiple_tokens_in_path1() {
+               global $wp_filesystem;
+               $fs = $wp_filesystem;
+               $fs->init('
+                       # www.example.com
+                       /example.com/www/index.php
+                       /example.com/www/wp-includes/
+                       /example.com/www/wp-content/plugins/
+                       
+                       # sub.example.com
+                       /example.com/sub/index.php
+                       /example.com/sub/wp-includes/
+                       /example.com/sub/wp-content/plugins/
+               ');
+
+               // www.example.com
+               $path = $fs->abspath( '/var/www/example.com/www/' );
+               $this->assertEquals( '/example.com/www/', $path );
+
+               // sub.example.com
+               $path = $fs->abspath( '/var/www/example.com/sub/' );
+               $this->assertEquals( '/example.com/sub/', $path );
+
+               // sub.example.com - Plugins
+               $path = $fs->find_folder( '/var/www/example.com/sub/wp-content/plugins/' );
+               $this->assertEquals( '/example.com/sub/wp-content/plugins/', $path );
+       }
+
+}
</ins><span class="cx">\ No newline at end of file
</span></span></pre>
</div>
</div>

</body>
</html>