View source
<?php
namespace Drupal\amazons3Test;
use Aws\Common\Credentials\Credentials;
use Aws\S3\S3Client;
use Drupal\amazons3\Matchable\BasicPath;
use Drupal\amazons3\Matchable\MatchablePaths;
use Drupal\amazons3\Matchable\PresignedPath;
use Drupal\amazons3Test\Stub\S3Client as DrupalS3Client;
use Drupal\amazons3Test\Stub\StreamWrapper;
use Drupal\amazons3Test\Stub\StreamWrapperConfiguration;
use Guzzle\Http\Message\Response;
use Guzzle\Http\Url;
use Guzzle\Tests\GuzzleTestCase;
class StreamWrapperTest extends GuzzleTestCase {
protected $wrapper;
public function setUp() {
StreamWrapper::setS3ClientClass('Drupal\\amazons3Test\\Stub\\S3Client');
$config = StreamWrapperConfiguration::fromConfig([
'bucket' => 'bucket.example.com',
'region' => 'region',
'caching' => FALSE,
'expiration' => 0,
]);
StreamWrapper::setDefaultConfig($config);
StreamWrapper::setClient(S3Client::factory([
'credentials' => new Credentials('placeholder', 'placeholder'),
]));
$this->wrapper = new StreamWrapper($config);
if (in_array('s3', stream_get_wrappers())) {
stream_wrapper_unregister('s3');
}
stream_wrapper_register('s3', '\\Drupal\\amazons3\\StreamWrapper', STREAM_IS_URL);
}
public function testSetDefaultConfig() {
$oldConfig = StreamWrapper::getDefaultConfig();
$config = StreamWrapperConfiguration::fromConfig([
'bucket' => 'bucket.example.com',
'region' => 'region',
'caching' => FALSE,
]);
StreamWrapper::setDefaultConfig($config);
$this
->assertSame($config, StreamWrapper::getDefaultConfig());
if ($oldConfig) {
StreamWrapper::setDefaultConfig($oldConfig);
}
}
public function testConstructDefaultConfig() {
$config = StreamWrapperConfiguration::fromConfig([
'bucket' => 'defaultconfig.example.com',
'region' => 'region',
'caching' => FALSE,
]);
StreamWrapper::setDefaultConfig($config);
$wrapper = new StreamWrapper();
$wrapper
->setUri('s3://');
$this
->assertEquals('s3://defaultconfig.example.com', $wrapper
->getUri());
}
public function testCreateClient() {
StreamWrapper::setClient(null);
$config = StreamWrapperConfiguration::fromConfig([
'bucket' => 'bucket.example.com',
'region' => 'region',
'caching' => FALSE,
'region' => 'us-east-1',
]);
$wrapper = new StreamWrapper($config);
$this
->assertNotNull($wrapper
->getClient());
}
public function testCreateCache() {
$config = StreamWrapperConfiguration::fromConfig([
'bucket' => 'bucket.example.com',
'region' => 'region',
'caching' => TRUE,
'expiration' => 0,
]);
$wrapper = new StreamWrapper($config);
$reflect = new \ReflectionObject($wrapper);
$cache = $reflect
->getProperty('cache');
$cache
->setAccessible(TRUE);
try {
$this
->assertInstanceOf('Doctrine\\Common\\Cache\\ChainCache', $cache
->getValue()
->getCacheObject());
} catch (\Exception $e) {
}
StreamWrapper::detachCache();
if (isset($e)) {
throw $e;
}
}
public function testSetClient() {
$client = S3Client::factory([
'credentials' => new Credentials('placeholder', 'placeholder'),
]);
StreamWrapper::setClient($client);
$this
->assertSame($client, StreamWrapper::getClient());
}
public function testSetUri() {
$wrapper = new StreamWrapper();
$uri = 's3://bucket.example.com/key';
$wrapper
->setUri($uri);
$this
->assertEquals($uri, $wrapper
->getUri());
}
public function testSetSchemeUri() {
$wrapper = new StreamWrapper();
$wrapper
->setUri('s3://');
$this
->assertEquals('s3://bucket.example.com', $wrapper
->getUri());
}
public function testExternalUriNotSet() {
$wrapper = new StreamWrapper();
$wrapper
->getExternalUrl();
}
public function testExternalImageStyleUri() {
$wrapper = new StreamWrapper();
$wrapper
->setUri('s3://bucket.example.com/styles/thumbnail/image.jpg');
$this
->assertEquals('http://amazons3.example.com/' . StreamWrapper::stylesCallback . '/bucket.example.com/styles/thumbnail/image.jpg', $wrapper
->getExternalUrl());
}
public function testExternalUri() {
$wrapper = new StreamWrapper();
$wrapper
->setUri('s3://bucket.example.com/image.jpg');
$this
->assertEquals('https://s3.amazonaws.com/bucket.example.com/image.jpg', $wrapper
->getExternalUrl());
}
public function testExternalUriEmptyPathSegments() {
$wrapper = new StreamWrapper();
$wrapper
->setUri('s3://bucket.example.com/');
$this
->assertEquals('https://s3.amazonaws.com/bucket.example.com', $wrapper
->getExternalUrl());
}
public function testGetMimeType() {
$mimeType = StreamWrapper::getMimeType('s3://bucket.example.com/image.jpg');
$this
->assertEquals('image/jpeg', $mimeType);
}
public function testGetMimeTypeDefault() {
$mimeType = StreamWrapper::getMimeType('s3://bucket.example.com/image');
$this
->assertEquals('application/octet-stream', $mimeType);
}
public function testDirnameNull() {
$this
->assertEquals('s3://bucket.example.com', $this->wrapper
->dirname());
}
public function testDirnameBucket() {
$this
->assertEquals('s3://bucket.different.com', $this->wrapper
->dirname('s3://bucket.different.com'));
}
public function testDirnameSubdir() {
$this
->assertEquals('s3://bucket.example.com', $this->wrapper
->dirname('s3://bucket.example.com/subdir'));
}
public function testDirnameNested() {
$this
->assertEquals('s3://bucket.example.com/subdir', $this->wrapper
->dirname('s3://bucket.example.com/subdir/second-subdir'));
}
public function testDirnameTrailingSlash() {
$this
->assertEquals('s3://bucket.example.com/subdir/second-subdir', $this->wrapper
->dirname('s3://bucket.example.com/subdir/second-subdir/'));
}
public function testRegister() {
$client = S3Client::factory([
'credentials' => new Credentials('placeholder', 'placeholder'),
]);
StreamWrapper::register($client);
}
public function testGetOptions() {
$wrapper = new StreamWrapper();
$wrapper
->setUri('s3://bucket.example.com');
$this
->assertArraySubset(array(
'ACL' => 'public-read',
), $wrapper
->getOptions());
}
public function testGetOptionsNoUri() {
$wrapper = new StreamWrapper();
$wrapper
->getOptions();
}
public function testSetS3ClientClass() {
StreamWrapper::setS3ClientClass('Drupal\\amazons3Test\\Stub\\S3Client');
StreamWrapper::setClient(NULL);
DrupalS3Client::resetCalled();
new StreamWrapper();
$this
->assertTrue(DrupalS3Client::isFactoryCalled());
}
public function testBasename() {
$config = StreamWrapperConfiguration::fromConfig([
'bucket' => 'bucket.example.com',
'region' => 'region',
'caching' => FALSE,
'expiration' => 0,
]);
$wrapper = new StreamWrapper($config);
$wrapper
->setUri('s3://bucket.example.com/force-download/test.jpg');
$this
->assertEquals('test.jpg', $wrapper
->getBasename());
}
public function testBasenameUriNotSet() {
$wrapper = new StreamWrapper();
$wrapper
->getBasename();
}
public function testSaveAs() {
$config = StreamWrapperConfiguration::fromConfig([
'bucket' => 'bucket.example.com',
'region' => 'region',
'caching' => FALSE,
'expiration' => 0,
'saveAsPaths' => new MatchablePaths(BasicPath::factory(array(
'force-download/.*',
))),
]);
$wrapper = new StreamWrapper($config);
$wrapper
->setUri('s3://bucket.example.com/force-download/test.jpg');
$this
->assertRegExp('!.*response-content-disposition=attachment%3B%20filename%3D%22test\\.jpg.*!', $wrapper
->getExternalUrl());
}
public function testSaveAsExcluded() {
$config = StreamWrapperConfiguration::fromConfig([
'bucket' => 'bucket.example.com',
'region' => 'region',
'caching' => FALSE,
'expiration' => 0,
'saveAsPaths' => new MatchablePaths(BasicPath::factory(array(
'force-download/*',
))),
]);
$wrapper = new StreamWrapper($config);
$wrapper
->setUri('s3://bucket.example.com/test.jpg');
$this
->assertEquals('https://s3.amazonaws.com/bucket.example.com/test.jpg', $wrapper
->getExternalUrl());
}
public function testSaveAsAll() {
$config = StreamWrapperConfiguration::fromConfig([
'bucket' => 'bucket.example.com',
'region' => 'region',
'caching' => FALSE,
'expiration' => 0,
'saveAsPaths' => new MatchablePaths(BasicPath::factory(array(
'*',
))),
]);
$wrapper = new StreamWrapper($config);
$wrapper
->setUri('s3://bucket.example.com/test.jpg');
$this
->assertRegExp('!.*response-content-disposition=attachment%3B%20filename%3D%22test\\.jpg.*!', $wrapper
->getExternalUrl());
}
public function testAttachmentSpace() {
$config = StreamWrapperConfiguration::fromConfig([
'bucket' => 'bucket.example.com',
'region' => 'region',
'caching' => FALSE,
'expiration' => 0,
'saveAsPaths' => new MatchablePaths(BasicPath::factory(array(
'force-download/.*',
))),
]);
$wrapper = new StreamWrapper($config);
$wrapper
->setUri('s3://bucket.example.com/force-download/test with spaces.jpg');
$this
->assertRegExp('!.*response-content-disposition=attachment%3B%20filename%3D%22test%20with%20spaces\\.jpg.*!', $wrapper
->getExternalUrl());
}
public function testTorrentPath() {
$config = StreamWrapperConfiguration::fromConfig([
'bucket' => 'bucket.example.com',
'region' => 'region',
'caching' => FALSE,
'expiration' => 0,
'torrentPaths' => new MatchablePaths(BasicPath::factory(array(
'torrents/.*',
))),
]);
$wrapper = new StreamWrapper($config);
$wrapper
->setUri('s3://bucket.example.com/torrents/test');
$this
->assertEquals('https://s3.amazonaws.com/bucket.example.com/torrents/test%3Ftorrent', $wrapper
->getExternalUrl());
}
public function testPresignedPath() {
$config = StreamWrapperConfiguration::fromConfig([
'bucket' => 'bucket.example.com',
'region' => 'region',
'caching' => FALSE,
'expiration' => 0,
'presignedPaths' => new MatchablePaths(PresignedPath::factory(array(
'presigned/.*' => 30,
))),
]);
$wrapper = new StreamWrapper($config);
$wrapper
->setUri('s3://bucket.example.com/presigned/test');
$url = Url::factory($wrapper
->getExternalUrl());
$this
->assertNotNull($url
->getQuery()
->get('AWSAccessKeyId'));
$this
->assertNotNull($url
->getQuery()
->get('Signature'));
$this
->assertGreaterThanOrEqual(time(), $url
->getQuery()
->get('Expires'));
$this
->assertLessThanOrEqual(time() + 32, $url
->getQuery()
->get('Expires'));
}
public function testCustomDomain() {
$config = StreamWrapperConfiguration::fromConfig([
'bucket' => 'bucket.example.com',
'region' => 'region',
'domain' => 'static.example.com',
'caching' => FALSE,
'expiration' => 0,
]);
$wrapper = new StreamWrapper($config);
$wrapper
->setUri('s3://bucket.example.com/image.jpg');
$url = Url::factory($wrapper
->getExternalUrl());
$this
->assertEquals('static.example.com', $url
->getHost());
}
public function testNoCustomDomain() {
$config = StreamWrapperConfiguration::fromConfig([
'bucket' => 'bucket.example.com',
'region' => 'region',
'caching' => FALSE,
]);
$wrapper = new StreamWrapper($config);
$wrapper
->setUri('s3://bucket.example.com/image.jpg');
$url = Url::factory($wrapper
->getExternalUrl());
$this
->assertEquals('s3.amazonaws.com', $url
->getHost());
}
public function testReducedRedundancyStorage() {
$config = StreamWrapperConfiguration::fromConfig([
'bucket' => 'bucket.example.com',
'region' => 'region',
'caching' => FALSE,
'reducedRedundancyPaths' => new MatchablePaths(BasicPath::factory(array(
'*',
))),
]);
$wrapper = new StreamWrapper($config);
$wrapper
->setUri('s3://bucket.example.com/styles/thumbnail/image.jpg');
$options = $wrapper
->getOptions();
$this
->assertArrayHasKey('StorageClass', $options);
$this
->assertEquals('REDUCED_REDUNDANCY', $options['StorageClass']);
}
public function testAutomaticUri() {
$wrapper = new StreamWrapper();
$path = NULL;
$uri = 's3://bucket.example.com/image.jpg';
try {
$wrapper
->stream_open($uri, 'r', 0, $path);
} catch (\PHPUnit_Framework_Error_Warning $e) {
}
$this
->assertEquals($uri, $wrapper
->getUri());
$config = array(
'includes' => array(
0 => '_aws',
),
'services' => array(
'default_settings' => array(
'params' => array(
'region' => 'us-east-1',
),
),
'cloudfront' => array(
'extends' => 'cloudfront',
'params' => array(
'private_key' => 'change_me',
'key_pair_id' => 'change_me',
),
),
),
'credentials' => array(
'key' => 'placeholder',
'secret' => 'placeholder',
),
);
$aws = \Aws\Common\Aws::factory($config);
\Guzzle\Tests\GuzzleTestCase::setServiceBuilder($aws);
$client = $this
->getServiceBuilder()
->get('s3', true);
$this
->setMockResponse($client, array(
new Response(200),
new Response(404),
new Response(200),
));
$wrapper = new StreamWrapper();
$wrapper
->setClient($client);
$wrapper
->url_stat($uri, 0);
$this
->assertEquals($uri, $wrapper
->getUri());
$dir_uri = 's3://bucket.example.com/directory';
$wrapper
->mkdir($dir_uri, 0, 0);
$this
->assertEquals($dir_uri, $wrapper
->getUri());
$this
->setMockResponse($client, array(
new Response(200),
));
$wrapper = new StreamWrapper();
$wrapper
->setClient($client);
$wrapper
->dir_opendir($dir_uri, "");
$this
->assertEquals($dir_uri, $wrapper
->getUri());
$this
->setMockResponse($client, array(
new Response(200),
));
$wrapper = new StreamWrapper();
$wrapper
->setClient($client);
$wrapper
->rmdir($dir_uri, 0);
$this
->assertEquals($dir_uri, $wrapper
->getUri());
$this
->setMockResponse($client, array(
new Response(200),
));
$wrapper = new StreamWrapper();
$wrapper
->setClient($client);
$wrapper
->unlink($uri);
$this
->assertEquals($uri, $wrapper
->getUri());
$this
->setMockResponse($client, array(
new Response(200),
new Response(200),
));
$wrapper = new StreamWrapper();
$wrapper
->setClient($client);
$wrapper
->rename($uri, 's3://bucket.example.com/renamed.jpg');
$this
->assertEquals('s3://bucket.example.com/renamed.jpg', $wrapper
->getUri());
}
}