<!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>[55204] trunk: Filesystem API: Add directory support to `WP_Filesystem_Direct::move()`.</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 { white-space: pre-line; 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" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/55204">55204</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/55204","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>azaozz</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2023-02-03 01:48:36 +0000 (Fri, 03 Feb 2023)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Filesystem API: Add directory support to `WP_Filesystem_Direct::move()`.
Introduces:
- New function: `wp_opcache_invalidate_directory()`, to recursively call `wp_opcache_invalidate()` after overwriting .php files.
- New function: `move_dir()`, similar to `copy_dir()` that uses `WP_Filesystem::move()` followed by `wp_opcache_invalidate_directory()`, and has a fallback to `copy_dir()`.
Props: costdev, afragen, peterwilsoncc, sergeybiryukov, ironprogrammer, flixos90, bronsonquick, mukesh27, azaozz.
Fixes <a href="https://core.trac.wordpress.org/ticket/57375">#57375</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpadminincludesclasswpfilesystemdirectphp">trunk/src/wp-admin/includes/class-wp-filesystem-direct.php</a></li>
<li><a href="#trunksrcwpadminincludesfilephp">trunk/src/wp-admin/includes/file.php</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li><a href="#trunktestsphpunittestsfilesystemmoveDirphp">trunk/tests/phpunit/tests/filesystem/moveDir.php</a></li>
<li><a href="#trunktestsphpunittestsfilesystemwpOpcacheInvalidateDirectoryphp">trunk/tests/phpunit/tests/filesystem/wpOpcacheInvalidateDirectory.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpadminincludesclasswpfilesystemdirectphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/includes/class-wp-filesystem-direct.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/includes/class-wp-filesystem-direct.php 2023-02-03 01:03:59 UTC (rev 55203)
+++ trunk/src/wp-admin/includes/class-wp-filesystem-direct.php 2023-02-03 01:48:36 UTC (rev 55204)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -316,8 +316,15 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Moves a file.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Moves a file or directory.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * After moving files or directories, OPcache will need to be invalidated.
+ *
+ * If moving a directory fails, `copy_dir()` can be used for a recursive copy.
+ *
+ * Use `move_dir()` for moving directories with OPcache invalidation and a
+ * fallback to `copy_dir()`.
+ *
</ins><span class="cx" style="display: block; padding: 0 10px"> * @since 2.5.0
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $source Path to the source file.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -331,12 +338,18 @@
</span><span class="cx" style="display: block; padding: 0 10px"> return false;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( $overwrite && $this->exists( $destination ) && ! $this->delete( $destination, true ) ) {
+ // Can't overwrite if the destination couldn't be deleted.
+ return false;
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> // Try using rename first. if that fails (for example, source is read only) try copy.
</span><span class="cx" style="display: block; padding: 0 10px"> if ( @rename( $source, $destination ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return true;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( $this->copy( $source, $destination, $overwrite ) && $this->exists( $destination ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Backward compatibility: Only fall back to `::copy()` for single files.
+ if ( $this->is_file( $source ) && $this->copy( $source, $destination, $overwrite ) && $this->exists( $destination ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->delete( $source );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> return true;
</span></span></pre></div>
<a id="trunksrcwpadminincludesfilephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/includes/file.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/includes/file.php 2023-02-03 01:03:59 UTC (rev 55203)
+++ trunk/src/wp-admin/includes/file.php 2023-02-03 01:48:36 UTC (rev 55204)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1947,6 +1947,75 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Moves a directory from one location to another.
+ *
+ * Recursively invalidates OPcache on success.
+ *
+ * If the renaming failed, falls back to copy_dir().
+ *
+ * Assumes that WP_Filesystem() has already been called and setup.
+ *
+ * @since 6.2.0
+ *
+ * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
+ *
+ * @param string $from Source directory.
+ * @param string $to Destination directory.
+ * @param bool $overwrite Optional. Whether to overwrite the destination directory if it exists.
+ * Default false.
+ * @return true|WP_Error True on success, WP_Error on failure.
+ */
+function move_dir( $from, $to, $overwrite = false ) {
+ global $wp_filesystem;
+
+ if ( trailingslashit( strtolower( $from ) ) === trailingslashit( strtolower( $to ) ) ) {
+ return new WP_Error(
+ 'source_destination_same_move_dir',
+ __( 'The source and destination are the same.' )
+ );
+ }
+
+ if ( ! $overwrite && $wp_filesystem->exists( $to ) ) {
+ return new WP_Error( 'destination_already_exists_move_dir', __( 'The destination folder already exists.' ), $to );
+ }
+
+ if ( $wp_filesystem->move( $from, $to, $overwrite ) ) {
+ /*
+ * When using an environment with shared folders,
+ * there is a delay in updating the filesystem's cache.
+ *
+ * This is a known issue in environments with a VirtualBox provider.
+ *
+ * A 200ms delay gives time for the filesystem to update its cache,
+ * prevents "Operation not permitted", and "No such file or directory" warnings.
+ *
+ * This delay is used in other projects, including Composer.
+ * @link https://github.com/composer/composer/blob/2.5.1/src/Composer/Util/Platform.php#L228-L233
+ */
+ usleep( 200000 );
+ wp_opcache_invalidate_directory( $to );
+
+ return true;
+ }
+
+ // Fall back to a recursive copy.
+ if ( ! $wp_filesystem->is_dir( $to ) ) {
+ if ( ! $wp_filesystem->mkdir( $to, FS_CHMOD_DIR ) ) {
+ return new WP_Error( 'mkdir_failed_move_dir', __( 'Could not create directory.' ), $to );
+ }
+ }
+
+ $result = copy_dir( $from, $to, array( basename( $to ) ) );
+
+ // Clear the source directory.
+ if ( true === $result ) {
+ $wp_filesystem->delete( $from, true );
+ }
+
+ return $result;
+}
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Initializes and connects the WordPress Filesystem Abstraction classes.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * This function will include the chosen transport and attempt connecting.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2557,3 +2626,65 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> return false;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+/**
+ * Attempts to clear the opcode cache for a directory of files.
+ *
+ * @since 6.2.0
+ *
+ * @see wp_opcache_invalidate()
+ * @link https://www.php.net/manual/en/function.opcache-invalidate.php
+ *
+ * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
+ *
+ * @param string $dir The path to the directory for which the opcode cache is to be cleared.
+ */
+function wp_opcache_invalidate_directory( $dir ) {
+ global $wp_filesystem;
+
+ if ( ! is_string( $dir ) || '' === trim( $dir ) ) {
+ if ( WP_DEBUG ) {
+ $error_message = sprintf(
+ /* translators: %s: The function name. */
+ __( '%s expects a non-empty string.' ),
+ '<code>wp_opcache_invalidate_directory()</code>'
+ );
+ // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error
+ trigger_error( $error_message );
+ }
+ return;
+ }
+
+ $dirlist = $wp_filesystem->dirlist( $dir, false, true );
+
+ if ( empty( $dirlist ) ) {
+ return;
+ }
+
+ /*
+ * Recursively invalidate opcache of files in a directory.
+ *
+ * WP_Filesystem_*::dirlist() returns an array of file and directory information.
+ *
+ * This does not include a path to the file or directory.
+ * To invalidate files within sub-directories, recursion is needed
+ * to prepend an absolute path containing the sub-directory's name.
+ *
+ * @param array $dirlist Array of file/directory information from WP_Filesystem_Base::dirlist(),
+ * with sub-directories represented as nested arrays.
+ * @param string $path Absolute path to the directory.
+ */
+ $invalidate_directory = function( $dirlist, $path ) use ( &$invalidate_directory ) {
+ $path = trailingslashit( $path );
+
+ foreach ( $dirlist as $name => $details ) {
+ if ( 'f' === $details['type'] ) {
+ wp_opcache_invalidate( $path . $name, true );
+ } elseif ( is_array( $details['files'] ) && ! empty( $details['files'] ) ) {
+ $invalidate_directory( $details['files'], $path . $name );
+ }
+ }
+ };
+
+ $invalidate_directory( $dirlist, $dir );
+}
</ins></span></pre></div>
<a id="trunktestsphpunittestsfilesystemmoveDirphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/filesystem/moveDir.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/filesystem/moveDir.php (rev 0)
+++ trunk/tests/phpunit/tests/filesystem/moveDir.php 2023-02-03 01:48:36 UTC (rev 55204)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,279 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+/**
+ * Tests move_dir().
+ *
+ * @group file.php
+ *
+ * @covers ::move_dir
+ */
+class Tests_Filesystem_MoveDir extends WP_UnitTestCase {
+
+ /**
+ * The test directory.
+ *
+ * @var string $test_dir
+ */
+ private static $test_dir;
+
+ /**
+ * The existing 'from' directory path.
+ *
+ * @var string $existing_from
+ */
+ private static $existing_from;
+
+ /**
+ * The existing 'from' sub-directory path.
+ *
+ * @var string $existing_from_subdir
+ */
+ private static $existing_from_subdir;
+
+ /**
+ * The existing 'from' file path.
+ *
+ * @var string $existing_from_file
+ */
+ private static $existing_from_file;
+
+ /**
+ * The existing 'from' sub-directory file path.
+ *
+ * @var string $existing_from_subdir_file
+ */
+ private static $existing_from_subdir_file;
+
+ /**
+ * The existing 'to' directory file path.
+ *
+ * @var string $existing_to
+ */
+ private static $existing_to;
+
+ /**
+ * The existing 'to' file path.
+ *
+ * @var string $existing_to_file
+ */
+ private static $existing_to_file;
+
+ /**
+ * Sets up the filesystem and directory structure properties
+ * before any tests run.
+ */
+ public static function set_up_before_class() {
+ parent::set_up_before_class();
+
+ require_once ABSPATH . 'wp-admin/includes/file.php';
+ WP_Filesystem();
+
+ self::$test_dir = get_temp_dir() . 'move_dir/';
+ self::$existing_from = self::$test_dir . 'existing_from/';
+ self::$existing_from_subdir = self::$existing_from . 'existing_from_subdir/';
+ self::$existing_from_file = self::$existing_from . 'existing_from_file.txt';
+ self::$existing_from_subdir_file = self::$existing_from_subdir . 'existing_from_subdir_file.txt';
+ self::$existing_to = self::$test_dir . 'existing_to/';
+ self::$existing_to_file = self::$existing_to . 'existing_to_file.txt';
+ }
+
+ /**
+ * Sets up the directory structure before each test.
+ */
+ public function set_up() {
+ global $wp_filesystem;
+
+ parent::set_up();
+
+ // Create the root directory.
+ $wp_filesystem->mkdir( self::$test_dir );
+
+ // Create the "from" directory structure.
+ $wp_filesystem->mkdir( self::$existing_from );
+ $wp_filesystem->touch( self::$existing_from_file );
+ $wp_filesystem->mkdir( self::$existing_from_subdir );
+ $wp_filesystem->touch( self::$existing_from_subdir_file );
+
+ // Create the "to" directory structure.
+ $wp_filesystem->mkdir( self::$existing_to );
+ $wp_filesystem->touch( self::$existing_to_file );
+ }
+
+ /**
+ * Removes the test directory structure after each test.
+ */
+ public function tear_down() {
+ global $wp_filesystem;
+
+ // Delete the root directory and its contents.
+ $wp_filesystem->delete( self::$test_dir, true );
+
+ parent::tear_down();
+ }
+
+ /**
+ * Tests that move_dir() returns a WP_Error object.
+ *
+ * @ticket 57375
+ *
+ * @dataProvider data_should_return_wp_error
+ *
+ * @param string $from The source directory path.
+ * @param string $to The destination directory path.
+ * @param bool $overwrite Whether to overwrite the destination directory.
+ * @param string $expected The expected WP_Error code.
+ */
+ public function test_should_return_wp_error( $from, $to, $overwrite, $expected ) {
+ global $wp_filesystem;
+
+ $from = self::$test_dir . $from;
+ $to = self::$test_dir . $to;
+ $result = move_dir( $from, $to, $overwrite );
+
+ $this->assertWPError(
+ $result,
+ 'move_dir() did not return a WP_Error object.'
+ );
+
+ $this->assertSame(
+ $expected,
+ $result->get_error_code(),
+ 'The expected error code was not returned.'
+ );
+
+ if ( 'source_destination_same_move_dir' !== $expected ) {
+ $this->assertTrue(
+ $wp_filesystem->exists( $from ),
+ 'The $from directory does not exist anymore.'
+ );
+
+ if ( false === $overwrite && 'existing_to' === untrailingslashit( $to ) ) {
+ $this->assertTrue(
+ $wp_filesystem->exists( $to ),
+ 'The $to directory does not exist anymore.'
+ );
+ }
+ }
+ }
+
+ /**
+ * Data provider.
+ *
+ * @return array[]
+ */
+ public function data_should_return_wp_error() {
+ return array(
+ '$overwrite is false and $to exists' => array(
+ 'from' => 'existing_from',
+ 'to' => 'existing_to',
+ 'overwrite' => false,
+ 'expected' => 'destination_already_exists_move_dir',
+ ),
+ 'same source and destination, source has trailing slash' => array(
+ 'from' => 'existing_from/',
+ 'to' => 'existing_from',
+ 'overwrite' => false,
+ 'expected' => 'source_destination_same_move_dir',
+ ),
+ 'same source and destination, destination has trailing slash' => array(
+ 'from' => 'existing_from',
+ 'to' => 'existing_from/',
+ 'overwrite' => false,
+ 'expected' => 'source_destination_same_move_dir',
+ ),
+ 'same source and destination, source lowercase, destination uppercase' => array(
+ 'from' => 'existing_from',
+ 'to' => 'EXISTING_FROM',
+ 'overwrite' => false,
+ 'expected' => 'source_destination_same_move_dir',
+ ),
+ 'same source and destination, source uppercase, destination lowercase' => array(
+ 'from' => 'EXISTING_FROM',
+ 'to' => 'existing_from',
+ 'overwrite' => false,
+ 'expected' => 'source_destination_same_move_dir',
+ ),
+ 'same source and destination, source and destination in inverted case' => array(
+ 'from' => 'ExIsTiNg_FrOm',
+ 'to' => 'eXiStInG_fRoM',
+ 'overwrite' => false,
+ 'expected' => 'source_destination_same_move_dir',
+ ),
+ );
+ }
+
+ /**
+ * Tests that move_dir() successfully moves a directory.
+ *
+ * @ticket 57375
+ *
+ * @dataProvider data_should_move_directory
+ *
+ * @param string $from The source directory path.
+ * @param string $to The destination directory path.
+ * @param bool $overwrite Whether to overwrite the destination directory.
+ */
+ public function test_should_move_directory( $from, $to, $overwrite ) {
+ global $wp_filesystem;
+
+ $from = self::$test_dir . $from;
+ $to = self::$test_dir . $to;
+ $result = move_dir( $from, $to, $overwrite );
+
+ $this->assertTrue(
+ $result,
+ 'The directory was not moved.'
+ );
+
+ $this->assertFalse(
+ $wp_filesystem->exists( $from ),
+ 'The source directory still exists.'
+ );
+
+ $this->assertTrue(
+ $wp_filesystem->exists( $to ),
+ 'The destination directory does not exist.'
+ );
+
+ $dirlist = $wp_filesystem->dirlist( $to, true, true );
+
+ // Prevent PHP array sorting bugs from breaking tests.
+ $to_contents = array_keys( $dirlist );
+
+ $this->assertSameSets(
+ array(
+ 'existing_from_file.txt',
+ 'existing_from_subdir',
+ ),
+ $to_contents,
+ 'The expected files were not moved.'
+ );
+
+ $this->assertSame(
+ array( 'existing_from_subdir_file.txt' ),
+ array_keys( $dirlist['existing_from_subdir']['files'] ),
+ 'Sub-directory files failed to move.'
+ );
+ }
+
+ /**
+ * Data provider.
+ *
+ * @return array[]
+ */
+ public function data_should_move_directory() {
+ return array(
+ '$overwrite is false and $to does not exist' => array(
+ 'from' => 'existing_from',
+ 'to' => 'non_existing_to',
+ 'overwrite' => false,
+ ),
+ '$overwrite is true and $to exists' => array(
+ 'from' => 'existing_from',
+ 'to' => 'existing_to',
+ 'overwrite' => true,
+ ),
+ );
+ }
+
+}
</ins></span></pre></div>
<a id="trunktestsphpunittestsfilesystemwpOpcacheInvalidateDirectoryphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/filesystem/wpOpcacheInvalidateDirectory.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/filesystem/wpOpcacheInvalidateDirectory.php (rev 0)
+++ trunk/tests/phpunit/tests/filesystem/wpOpcacheInvalidateDirectory.php 2023-02-03 01:48:36 UTC (rev 55204)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,101 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+/**
+ * Tests wp_opcache_invalidate_directory().
+ *
+ * @group file.php
+ *
+ * @covers ::wp_opcache_invalidate_directory
+ */
+class Tests_Filesystem_WpOpcacheInvalidateDirectory extends WP_UnitTestCase {
+
+ /**
+ * Sets up the filesystem before any tests run.
+ */
+ public static function set_up_before_class() {
+ global $wp_filesystem;
+
+ parent::set_up_before_class();
+
+ if ( ! $wp_filesystem ) {
+ require_once ABSPATH . 'wp-admin/includes/file.php';
+ WP_Filesystem();
+ }
+ }
+
+ /**
+ * Tests that wp_opcache_invalidate_directory() returns a WP_Error object
+ * when the $dir argument invalid.
+ *
+ * @ticket 57375
+ *
+ * @dataProvider data_should_trigger_error_with_invalid_dir
+ *
+ * @param mixed $dir An invalid directory path.
+ */
+ public function test_should_trigger_error_with_invalid_dir( $dir ) {
+ $this->expectError();
+ $this->expectErrorMessage(
+ '<code>wp_opcache_invalidate_directory()</code>',
+ 'The expected error was not triggered.'
+ );
+
+ wp_opcache_invalidate_directory( $dir );
+ }
+
+ /**
+ * Data provider.
+ *
+ * @return array[]
+ */
+ public function data_should_trigger_error_with_invalid_dir() {
+ return array(
+ 'an empty string' => array( '' ),
+ 'a string with spaces' => array( ' ' ),
+ 'a string with tabs' => array( "\t" ),
+ 'a string with new lines' => array( "\n" ),
+ 'a string with carriage returns' => array( "\r" ),
+ 'int -1' => array( -1 ),
+ 'int 0' => array( 0 ),
+ 'int 1' => array( 1 ),
+ 'float -1.0' => array( -1.0 ),
+ 'float 0.0' => array( 0.0 ),
+ 'float 1.0' => array( 1.0 ),
+ 'false' => array( false ),
+ 'true' => array( true ),
+ 'null' => array( null ),
+ 'an empty array' => array( array() ),
+ 'a non-empty array' => array( array( 'directory_path' ) ),
+ 'an empty object' => array( new stdClass() ),
+ 'a non-empty object' => array( (object) array( 'directory_path' ) ),
+ 'INF' => array( INF ),
+ 'NAN' => array( NAN ),
+ );
+ }
+
+ /**
+ * Tests that wp_opcache_invalidate_directory() does not trigger an error
+ * with a valid directory.
+ *
+ * @ticket 57375
+ *
+ * @dataProvider data_should_not_trigger_error_wp_opcache_valid_directory
+ *
+ * @param string $dir A directory path.
+ */
+ public function test_should_not_trigger_error_wp_opcache_valid_directory( $dir ) {
+ $this->assertNull( wp_opcache_invalidate_directory( $dir ) );
+ }
+
+ /**
+ * Data provider.
+ *
+ * @return array[]
+ */
+ public function data_should_not_trigger_error_wp_opcache_valid_directory() {
+ return array(
+ 'an existing directory' => array( DIR_TESTDATA ),
+ 'a non-existent directory' => array( 'non_existent_directory' ),
+ );
+ }
+}
</ins></span></pre>
</div>
</div>
</body>
</html>