Commit d7f6fc50 authored by Joe Hoyle's avatar Joe Hoyle
Browse files

Pull registering the stream wrapper into a method

this makes testing a lot easier
parent baee7903
Loading
Loading
Loading
Loading
+6 −1
Original line number Diff line number Diff line
@@ -30,14 +30,19 @@ class S3_Uploads {
		$this->secret = $secret;
		$this->bucket_url = $bucket_url;
		$this->region = $region;
	}

	/**
	 * Register the stream wrapper for s3
	 */
	public function register_stream_wrapper() {
		if ( defined( 'S3_UPLOADS_USE_LOCAL' ) && S3_UPLOADS_USE_LOCAL ) {
			require_once dirname( __FILE__ ) . '/class-s3-uploads-local-stream-wrapper.php';
			stream_wrapper_register( 's3', 'S3_Uploads_Local_Stream_Wrapper', STREAM_IS_URL );
		} else {
			$s3 = $this->s3();
			S3_Uploads_Stream_Wrapper::register( $s3 );
			stream_context_set_option( stream_context_get_default(), 's3', 'ACL', Aws\S3\Enum\CannedAcl::PUBLIC_READ );
			stream_context_set_option( stream_context_get_default(), 's3', 'ACL', 'public-read' );
		}

		stream_context_set_option( stream_context_get_default(), 's3', 'seekable', true );
+14 −42
Original line number Diff line number Diff line
@@ -8,14 +8,22 @@ class Test_S3_Uploads_Stream_Wrapper extends WP_UnitTestCase {

	}

	public function tearDown() {
		stream_wrapper_unregister( 's3' );
		S3_Uploads::get_instance()->register_stream_wrapper();
	}

	public function test_stream_wrapper_is_registered() {
		$this->assertTrue( in_array( 's3', stream_get_wrappers() ) );
	}

	public function test_copy_via_stream_wrapper() {

		$result = copy( dirname( __FILE__ ) . '/data/canola.jpg', 's3://' . S3_UPLOADS_BUCKET . '/canola.jpg' );
		$local_path = dirname( __FILE__ ) . '/data/canola.jpg';
		$remote_path = 's3://' . S3_UPLOADS_BUCKET . '/canola.jpg';
		$result = copy( $local_path, $remote_path );
		$this->assertTrue( $result );
		$this->assertEquals( file_get_contents( $local_path ), file_get_contents( $remote_path ) );
	}

	public function test_rename_via_stream_wrapper() {
@@ -35,13 +43,6 @@ class Test_S3_Uploads_Stream_Wrapper extends WP_UnitTestCase {
	}

	public function test_copy_via_stream_wrapper_fails_on_invalid_permission() {
		stream_wrapper_unregister( 's3' );
		$params = array( 'key' => S3_UPLOADS_KEY, 'secret' => S3_UPLOADS_SECRET );
		$s3 = Aws\Common\Aws::factory( $params )->get( 's3' );

		S3_Uploads_Stream_Wrapper::register( $s3 );
		stream_context_set_option( stream_context_get_default(), 's3', 'ACL', Aws\S3\Enum\CannedAcl::PUBLIC_READ );

		$bucket_root = strtok( S3_UPLOADS_BUCKET, '/' );
		$result = @copy( dirname( __FILE__ ) . '/data/canola.jpg', 's3://' . $bucket_root . '/canola.jpg' );

@@ -50,13 +51,6 @@ class Test_S3_Uploads_Stream_Wrapper extends WP_UnitTestCase {

	public function test_rename_via_stream_wrapper_fails_on_invalid_permission() {

		stream_wrapper_unregister( 's3' );
		$params = array( 'key' => S3_UPLOADS_KEY, 'secret' => S3_UPLOADS_SECRET );
		$s3 = Aws\Common\Aws::factory( $params )->get( 's3' );

		S3_Uploads_Stream_Wrapper::register( $s3 );
		stream_context_set_option( stream_context_get_default(), 's3', 'ACL', Aws\S3\Enum\CannedAcl::PUBLIC_READ );

		copy( dirname( __FILE__ ) . '/data/canola.jpg', 's3://' . S3_UPLOADS_BUCKET . '/canola.jpg' );
		$bucket_root = strtok( S3_UPLOADS_BUCKET, '/' );
		$result = @rename( 's3://' . S3_UPLOADS_BUCKET . '/canola.jpg', 's3://' . $bucket_root . '/canola.jpg' );
@@ -72,12 +66,8 @@ class Test_S3_Uploads_Stream_Wrapper extends WP_UnitTestCase {

		stream_wrapper_unregister( 's3' );

		// incorrect secret so we'll ato fail if any writing / reading is attempted
		$params = array( 'key' => S3_UPLOADS_KEY, 'secret' => 123 );
		$s3 = Aws\Common\Aws::factory( $params )->get( 's3' );

		S3_Uploads_Stream_Wrapper::register( $s3 );
		stream_context_set_option( stream_context_get_default(), 's3', 'ACL', Aws\S3\Enum\CannedAcl::PUBLIC_READ );
		$uploads = new S3_Uploads( S3_UPLOADS_BUCKET, S3_UPLOADS_KEY, '123' );
		$uploads->register_stream_wrapper();

		$bucket_root = strtok( S3_UPLOADS_BUCKET, '/' );

@@ -89,27 +79,9 @@ class Test_S3_Uploads_Stream_Wrapper extends WP_UnitTestCase {
		$this->assertTrue( $result );
	}

	public function test_http_expires_headers() {
		$expires = strtotime( "+15 days" );
		define( 'S3_UPLOADS_HTTP_EXPIRES', $expires );

		copy( dirname( __FILE__ ) . '/data/canola.jpg', 's3://' . S3_UPLOADS_BUCKET . '/canola.jpg' );

		$request = wp_remote_head( 'https://s3.amazonaws.com/' . S3_UPLOADS_BUCKET . '/canola.jpg' );
		$headers = wp_remote_retrieve_headers( $request );

		$this->assertEquals( $expires, strtotime( $headers['expires'] ) );
	}

	public function test_http_cache_control_headers() {

		define( 'S3_UPLOADS_HTTP_CACHE_CONTROL', 'private, max-age=600' );

	public function get_file_exists_via_stream_wrapper() {
		copy( dirname( __FILE__ ) . '/data/canola.jpg', 's3://' . S3_UPLOADS_BUCKET . '/canola.jpg' );

		$request = wp_remote_head( 'https://s3.amazonaws.com/' . S3_UPLOADS_BUCKET . '/canola.jpg' );
		$headers = wp_remote_retrieve_headers( $request );

		$this->assertEquals( 'private, max-age=600', $headers['cache-control'] );
		$this->assertTrue( file_exists( 's3://' . S3_UPLOADS_BUCKET . '/canola.jpg' ) );
		$this->assertFalse( file_exists( 's3://' . S3_UPLOADS_BUCKET . '/canola-missing.jpg' ) );
	}
}
 No newline at end of file