/**
  * Tests normalizing a stream
  *
  * Tests WP_Stream::normalize()
  */
 public function test_normalize()
 {
     /**
      * Test malformed URI at the scheme/target junction
      */
     $malformed_uri = 'test:///example/path1/path2/hello_world.txt';
     $expected = 'test://example/path1/path2/hello_world.txt';
     $this->assertEquals($expected, WP_Stream::normalize($malformed_uri));
     /**
      * Test malformed URI with multiple separators in target
      */
     $malformed_uri = 'test://example/path1//path2/hello_world.txt';
     $expected = 'test://example/path1/path2/hello_world.txt';
     $this->assertEquals($expected, WP_Stream::normalize($malformed_uri));
     /**
      * Test malformed URI with mutliple problems
      */
     $malformed_uri = 'test:////example/path1//path2//hello_world.txt';
     $expected = 'test://example/path1/path2/hello_world.txt';
     $this->assertEquals($expected, WP_Stream::normalize($malformed_uri));
     /**
      * Test method call when there is no target
      */
     $malformed_uri = 'test://';
     $expected = 'test://';
     $this->assertEquals($expected, WP_Stream::normalize($malformed_uri));
     /**
      * Test method call when there is no target but the URI is malformed
      */
     $malformed_uri = 'test:///';
     $expected = 'test://';
     $this->assertEquals($expected, WP_Stream::normalize($malformed_uri));
 }
/**
 * Creates a file with a unique name
 *
 * PHP's tempnam() does not support stream wrappers. This helper function
 * properly adds this missing support.
 *
 * This function is fully compatible with PHP's tempnam() function and may be
 * called in the same way. For example, both a URI and a normal filepath
 * can be provided for the $uri parameter.
 *
 * Note: The function name used is temporary until the conflict with
 * wp_tempnam() in wp-admin/includes/file.php can be resolved.
 *
 * @param string $directory
 *   the directory where the temporary filename will be created.
 * @param string $prefix
 *   the prefix of the generated temporary filename.
 *   Note: Windows uses only the first three characters of $prefix
 *
 * @return mixed
 *   the new temporary filename, or false on failure.
 *
 * @link    http://php.net/manual/en/function.tempnam.php
 * @see     tempnam()
 * @since   1.0.0
 */
function wp_tempnam_stream_compatible($directory, $prefix)
{
    /**
     * NOTE: This is a temporary function name. This function should
     * be named wp_tempnam(), but that currently conflicts with an existing
     * declaration in wp-admin/includes/file.php.
     *
     * @todo Merge the two functions.
     */
    $scheme = WP_Stream::scheme($directory);
    if ($scheme && WP_Stream::scheme_valid($scheme)) {
        $wrapper = WP_Stream::new_wrapper_instance($scheme . '://');
        $path = wp_realpath($directory);
        if ($path && ($filename = tempnam($path, $prefix))) {
            return WP_Stream::normalize($directory . '/' . basename($filename));
        } else {
            return false;
        }
    } else {
        return tempnam($directory, $prefix);
    }
}