From c3b566ed5a0105a2d29dcec13f7fbab8e52c79f2 Mon Sep 17 00:00:00 2001 From: Andrew Berry Date: Tue, 21 Apr 2015 17:12:01 -0400 Subject: [PATCH 01/43] Issue #2471823: Test setting default stream configuration. --- src/StreamWrapper.php | 9 +++++++++ tests/StreamWrapperTest.php | 31 +++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/src/StreamWrapper.php b/src/StreamWrapper.php index 5eb958c..9de6098 100644 --- a/src/StreamWrapper.php +++ b/src/StreamWrapper.php @@ -59,6 +59,15 @@ class StreamWrapper extends \Aws\S3\StreamWrapper implements \DrupalStreamWrappe } /** + * Return the default configuration. + * + * @return \Drupal\amazons3\StreamWrapperConfiguration + */ + public static function getDefaultConfig() { + return static::$defaultConfig; + } + + /** * Construct a new stream wrapper. * * @param \Drupal\amazons3\StreamWrapperConfiguration $config diff --git a/tests/StreamWrapperTest.php b/tests/StreamWrapperTest.php index efb1b94..51d1fbc 100644 --- a/tests/StreamWrapperTest.php +++ b/tests/StreamWrapperTest.php @@ -38,7 +38,30 @@ class StreamWrapperTest extends \PHPUnit_Framework_TestCase { } /** + * Test setting and getting the default configuration. + * + * @covers \Drupal\amazons3\StreamWrapper::setDefaultConfig + * @covers \Drupal\amazons3\StreamWrapper::getDefaultConfig + */ + public function testSetDefaultConfig() { + $oldConfig = StreamWrapper::getDefaultConfig(); + + $config = StreamWrapperConfiguration::fromConfig([ + 'bucket' => 'bucket.example.com', + 'caching' => FALSE, + ]); + StreamWrapper::setDefaultConfig($config); + $this->assertEquals($config, StreamWrapper::getDefaultConfig()); + + if ($oldConfig) { + StreamWrapper::setDefaultConfig($oldConfig); + } + } + + /** * Test that a null dirname returns the bucket associated with the wrapper. + * + * @covers \Drupal\amazons3\StreamWrapper::dirname */ public function testDirnameNull() { $this->assertEquals('s3://bucket.example.com', $this->wrapper->dirname()); @@ -46,6 +69,8 @@ class StreamWrapperTest extends \PHPUnit_Framework_TestCase { /** * Test that we can fetch the dirname from a different bucket. + * + * @covers \Drupal\amazons3\StreamWrapper::dirname */ public function testDirnameBucket() { $this->assertEquals('s3://bucket.different.com', $this->wrapper->dirname('s3://bucket.different.com')); @@ -53,6 +78,8 @@ class StreamWrapperTest extends \PHPUnit_Framework_TestCase { /** * Test that dirname works with a key. + * + * @covers \Drupal\amazons3\StreamWrapper::dirname */ public function testDirnameSubdir() { $this->assertEquals('s3://bucket.example.com', $this->wrapper->dirname('s3://bucket.example.com/subdir')); @@ -60,6 +87,8 @@ class StreamWrapperTest extends \PHPUnit_Framework_TestCase { /** * Test that dirname works with a pseudo directory. + * + * @covers \Drupal\amazons3\StreamWrapper::dirname */ public function testDirnameNested() { $this->assertEquals('s3://bucket.example.com/subdir', $this->wrapper->dirname('s3://bucket.example.com/subdir/second-subdir')); @@ -67,6 +96,8 @@ class StreamWrapperTest extends \PHPUnit_Framework_TestCase { /** * Test that dirname properly handles trailing slashes. + * + * @covers \Drupal\amazons3\StreamWrapper::dirname */ public function testDirnameTrailingSlash() { $this->assertEquals('s3://bucket.example.com/subdir/second-subdir', $this->wrapper->dirname('s3://bucket.example.com/subdir/second-subdir/')); -- 2.3.5 From 71db10ef525777c11a8591be09ae59907cc9f361 Mon Sep 17 00:00:00 2001 From: Andrew Berry Date: Wed, 22 Apr 2015 09:42:20 -0400 Subject: [PATCH 02/43] Issue #2471823: Let clients be removed from stream wrappers. --- src/StreamWrapper.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/StreamWrapper.php b/src/StreamWrapper.php index 9de6098..bb874d2 100644 --- a/src/StreamWrapper.php +++ b/src/StreamWrapper.php @@ -114,8 +114,9 @@ class StreamWrapper extends \Aws\S3\StreamWrapper implements \DrupalStreamWrappe * Note that all stream wrapper instances share a global client. * * @param \Aws\S3\S3Client $client + * The client to use. Set to NULL to remove an existing client. */ - public static function setClient(AwsS3Client $client) { + public static function setClient(AwsS3Client $client = NULL) { self::$client = $client; } -- 2.3.5 From 5ae7063d74658d6e15403227ec08346b84a48c05 Mon Sep 17 00:00:00 2001 From: Andrew Berry Date: Wed, 22 Apr 2015 09:42:43 -0400 Subject: [PATCH 03/43] Issue #2471823: Ignore loading from variables. --- src/StreamWrapper.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/StreamWrapper.php b/src/StreamWrapper.php index bb874d2..ab5f903 100644 --- a/src/StreamWrapper.php +++ b/src/StreamWrapper.php @@ -79,7 +79,9 @@ class StreamWrapper extends \Aws\S3\StreamWrapper implements \DrupalStreamWrappe $config = static::$defaultConfig; } else { + // @codeCoverageIgnoreStart $config = StreamWrapperConfiguration::fromDrupalVariables(); + // @codeCoverageIgnoreEnd } } -- 2.3.5 From 36f7df7dc7dea8b13215ec262279021d55ab0f66 Mon Sep 17 00:00:00 2001 From: Andrew Berry Date: Wed, 22 Apr 2015 09:44:26 -0400 Subject: [PATCH 04/43] Issue #2471823: Add a stub for variable_get(). --- tests/StreamWrapperTest.php | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/tests/StreamWrapperTest.php b/tests/StreamWrapperTest.php index 51d1fbc..5bf2e59 100644 --- a/tests/StreamWrapperTest.php +++ b/tests/StreamWrapperTest.php @@ -1,6 +1,21 @@ assertEquals('s3://bucket.example.com/subdir/second-subdir', $this->wrapper->dirname('s3://bucket.example.com/subdir/second-subdir/')); } } + +} -- 2.3.5 From d9acd57bb28ee6c571f81bf973244ba7e8af3d76 Mon Sep 17 00:00:00 2001 From: Andrew Berry Date: Wed, 22 Apr 2015 09:44:46 -0400 Subject: [PATCH 05/43] Issue #2471823: Exclude vendor by default from code coverage. --- phpunit.xml.dist | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 018c8c4..1a6f41d 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -9,4 +9,10 @@ + + + ../../vendor + + + -- 2.3.5 From 93efdf23c0936350c9f9c1f8a87dbc06b2c83378 Mon Sep 17 00:00:00 2001 From: Andrew Berry Date: Wed, 22 Apr 2015 09:45:04 -0400 Subject: [PATCH 06/43] Issue #2471823: Add constructor tests. --- tests/StreamWrapperTest.php | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tests/StreamWrapperTest.php b/tests/StreamWrapperTest.php index 5bf2e59..2354fb3 100644 --- a/tests/StreamWrapperTest.php +++ b/tests/StreamWrapperTest.php @@ -74,6 +74,37 @@ class StreamWrapperTest extends \PHPUnit_Framework_TestCase { } /** + * Test that our default configuration is respected. + * + * @covers \Drupal\amazons3\StreamWrapper::__construct + */ + public function testConstructDefaultConfig() { + $config = StreamWrapperConfiguration::fromConfig([ + 'bucket' => 'defaultconfig.example.com', + 'caching' => FALSE, + ]); + StreamWrapper::setDefaultConfig($config); + $wrapper = new StreamWrapper(); + $wrapper->setUri('s3://'); + $this->assertEquals('s3://defaultconfig.example.com', $wrapper->getUri()); + } + + /** + * Test that when needed the StreamWrapper will create a client. + * + * @covers \Drupal\amazons3\StreamWrapper::__construct + */ + public function testCreateClient() { + StreamWrapper::setClient(null); + $config = StreamWrapperConfiguration::fromConfig([ + 'bucket' => 'bucket.example.com', + 'caching' => FALSE, + ]); + $wrapper = new StreamWrapper($config); + $this->assertNotNull($wrapper->getClient()); + } + + /** * Test that a null dirname returns the bucket associated with the wrapper. * * @covers \Drupal\amazons3\StreamWrapper::dirname -- 2.3.5 From 4448433a9d9d8a0ce2867fb9de5609c4f2ad49ef Mon Sep 17 00:00:00 2001 From: Andrew Berry Date: Wed, 22 Apr 2015 10:13:43 -0400 Subject: [PATCH 07/43] Issue #2471823: Import cache class. --- src/StreamWrapper.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/StreamWrapper.php b/src/StreamWrapper.php index ab5f903..a15b806 100644 --- a/src/StreamWrapper.php +++ b/src/StreamWrapper.php @@ -2,6 +2,7 @@ namespace Drupal\amazons3; +use Capgemini\Cache\DrupalDoctrineCache; use Guzzle\Cache\DoctrineCacheAdapter; use \Aws\S3\S3Client as AwsS3Client; @@ -92,7 +93,7 @@ class StreamWrapper extends \Aws\S3\StreamWrapper implements \DrupalStreamWrappe } if ($this->config->isCaching() && !static::$cache) { - $cache = new \Capgemini\Cache\DrupalDoctrineCache(); + $cache = new DrupalDoctrineCache(); $cache->setCacheTable('cache_amazons3_metadata'); static::attachCache( new DoctrineCacheAdapter($cache), -- 2.3.5 From ef6e0d5b135bf3a5ae772a3e673e52bd9154a40e Mon Sep 17 00:00:00 2001 From: Andrew Berry Date: Wed, 22 Apr 2015 10:13:53 -0400 Subject: [PATCH 08/43] Issue #2471823: Expiration is required if we are caching. --- src/StreamWrapperConfiguration.php | 3 +++ tests/StreamWrapperTest.php | 1 + 2 files changed, 4 insertions(+) diff --git a/src/StreamWrapperConfiguration.php b/src/StreamWrapperConfiguration.php index 9366f06..39cc46f 100644 --- a/src/StreamWrapperConfiguration.php +++ b/src/StreamWrapperConfiguration.php @@ -49,6 +49,9 @@ class StreamWrapperConfiguration extends Collection { $required = self::required(); $data = $config + $defaults; + if ($data['caching']) { + $required[] = 'expiration'; + } if ($missing = array_diff($required, array_keys($data))) { throw new \InvalidArgumentException('Config is missing the following keys: ' . implode(', ', $missing)); diff --git a/tests/StreamWrapperTest.php b/tests/StreamWrapperTest.php index 2354fb3..abe971f 100644 --- a/tests/StreamWrapperTest.php +++ b/tests/StreamWrapperTest.php @@ -44,6 +44,7 @@ class StreamWrapperTest extends \PHPUnit_Framework_TestCase { $config = StreamWrapperConfiguration::fromConfig([ 'bucket' => 'bucket.example.com', 'caching' => FALSE, + 'expiration' => 0, ]); StreamWrapper::setClient( S3Client::factory([ -- 2.3.5 From 53825ff996866a1058506eb515e8446d2e36a69c Mon Sep 17 00:00:00 2001 From: Andrew Berry Date: Wed, 22 Apr 2015 10:14:20 -0400 Subject: [PATCH 09/43] Issue #2471823: Test attaching a cache. --- tests/StreamWrapperTest.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/StreamWrapperTest.php b/tests/StreamWrapperTest.php index abe971f..8f5b12e 100644 --- a/tests/StreamWrapperTest.php +++ b/tests/StreamWrapperTest.php @@ -106,6 +106,25 @@ class StreamWrapperTest extends \PHPUnit_Framework_TestCase { } /** + * Test that a Drupal cache adapter is created. + * + * @covers \Drupal\amazons3\StreamWrapper::__construct + */ + public function testCreateCache() { + $config = StreamWrapperConfiguration::fromConfig([ + 'bucket' => 'bucket.example.com', + 'caching' => TRUE, + 'expiration' => 0, + ]); + + $wrapper = new StreamWrapper($config); + $reflect = new \ReflectionObject($wrapper); + $cache = $reflect->getProperty('cache'); + $cache->setAccessible(TRUE); + $this->assertInstanceOf('Capgemini\Cache\DrupalDoctrineCache', $cache->getValue()->getCacheObject()); + } + + /** * Test that a null dirname returns the bucket associated with the wrapper. * * @covers \Drupal\amazons3\StreamWrapper::dirname -- 2.3.5 From e4faba29d0c6c3064ac6b8a949a0733b0b70fe23 Mon Sep 17 00:00:00 2001 From: Andrew Berry Date: Wed, 22 Apr 2015 10:17:31 -0400 Subject: [PATCH 10/43] Issue #2471823: Test setting a client. --- tests/StreamWrapperTest.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/StreamWrapperTest.php b/tests/StreamWrapperTest.php index 8f5b12e..1e26d18 100644 --- a/tests/StreamWrapperTest.php +++ b/tests/StreamWrapperTest.php @@ -125,6 +125,24 @@ class StreamWrapperTest extends \PHPUnit_Framework_TestCase { } /** + * Test setting an S3 client. + * + * @covers \Drupal\amazons3\StreamWrapper::setClient + * @covers \Drupal\amazons3\StreamWrapper::getClient + */ + public function testSetClient() { + $client = S3Client::factory( + [ + 'credentials' => new Credentials('placeholder', 'placeholder'), + ] + ); + + StreamWrapper::setClient($client); + + $this->assertEquals($client, StreamWrapper::getClient()); + } + + /** * Test that a null dirname returns the bucket associated with the wrapper. * * @covers \Drupal\amazons3\StreamWrapper::dirname -- 2.3.5 From 96176f535e71db2e4ac463fd34ab3abb844415f8 Mon Sep 17 00:00:00 2001 From: Andrew Berry Date: Wed, 22 Apr 2015 10:23:58 -0400 Subject: [PATCH 11/43] Issue #2471823: Ignore unimplemented methods. --- src/StreamWrapper.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/StreamWrapper.php b/src/StreamWrapper.php index a15b806..077a7da 100644 --- a/src/StreamWrapper.php +++ b/src/StreamWrapper.php @@ -145,6 +145,8 @@ class StreamWrapper extends \Aws\S3\StreamWrapper implements \DrupalStreamWrappe * returns TRUE if lock was successful * * @link http://php.net/manual/en/streamwrapper.stream-lock.php + * + * @codeCoverageIgnore */ public function stream_lock($operation) { return FALSE; @@ -272,6 +274,8 @@ class StreamWrapper extends \Aws\S3\StreamWrapper implements \DrupalStreamWrappe /** * {@inheritdoc} + * + * @codeCoverageIgnore */ public function chmod($mode) { // TODO: Implement chmod() method. @@ -281,6 +285,8 @@ class StreamWrapper extends \Aws\S3\StreamWrapper implements \DrupalStreamWrappe /** * @return bool * FALSE, as this stream wrapper does not support realpath(). + * + * @codeCoverageIgnore */ public function realpath() { return FALSE; -- 2.3.5 From 80d3bd13a3154e9e8121d9ad902a7c2271c9a68c Mon Sep 17 00:00:00 2001 From: Andrew Berry Date: Wed, 22 Apr 2015 10:24:10 -0400 Subject: [PATCH 12/43] Issue #2471823: Test setting a URI. --- tests/StreamWrapperTest.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/StreamWrapperTest.php b/tests/StreamWrapperTest.php index 1e26d18..29f0d25 100644 --- a/tests/StreamWrapperTest.php +++ b/tests/StreamWrapperTest.php @@ -46,6 +46,7 @@ class StreamWrapperTest extends \PHPUnit_Framework_TestCase { 'caching' => FALSE, 'expiration' => 0, ]); + StreamWrapper::setDefaultConfig($config); StreamWrapper::setClient( S3Client::factory([ 'credentials' => new Credentials('placeholder', 'placeholder'), @@ -143,6 +144,19 @@ class StreamWrapperTest extends \PHPUnit_Framework_TestCase { } /** + * Test setting a URI. + * + * @covers \Drupal\amazons3\StreamWrapper::setUri + * @covers \Drupal\amazons3\StreamWrapper::getUri + */ + public function testSetUri() { + $wrapper = new StreamWrapper(); + $uri = 's3://bucket.example.com/key'; + $wrapper->setUri($uri); + $this->assertEquals($uri, $wrapper->getUri()); + } + + /** * Test that a null dirname returns the bucket associated with the wrapper. * * @covers \Drupal\amazons3\StreamWrapper::dirname -- 2.3.5 From 7cb3b8d226843acdbfcade8a74b2b9a64774bd8a Mon Sep 17 00:00:00 2001 From: Andrew Berry Date: Wed, 22 Apr 2015 10:29:55 -0400 Subject: [PATCH 13/43] Issue #2471823: Test when a URI is not set. --- tests/StreamWrapperTest.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/StreamWrapperTest.php b/tests/StreamWrapperTest.php index 29f0d25..c0112ba 100644 --- a/tests/StreamWrapperTest.php +++ b/tests/StreamWrapperTest.php @@ -157,6 +157,17 @@ class StreamWrapperTest extends \PHPUnit_Framework_TestCase { } /** + * Test that we throw an exception if a URI is not set. + * + * @expectedException \LogicException + * @covers \Drupal\amazons3\StreamWrapper::getExternalUrl + */ + public function testExternalUriNotSet() { + $wrapper = new StreamWrapper(); + $wrapper->getExternalUrl(); + } + + /** * Test that a null dirname returns the bucket associated with the wrapper. * * @covers \Drupal\amazons3\StreamWrapper::dirname -- 2.3.5 From b0a78f62f671d7bd01b34a980ab224ff3fd2ba18 Mon Sep 17 00:00:00 2001 From: Andrew Berry Date: Wed, 22 Apr 2015 10:54:37 -0400 Subject: [PATCH 14/43] Issue #2471823: Add stubs for url(). --- tests/StreamWrapperTest.php | 99 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) diff --git a/tests/StreamWrapperTest.php b/tests/StreamWrapperTest.php index c0112ba..0584e9d 100644 --- a/tests/StreamWrapperTest.php +++ b/tests/StreamWrapperTest.php @@ -2,6 +2,100 @@ namespace Drupal\amazons3 { + function drupal_encode_path($path) { + return str_replace('%2F', '/', rawurlencode($path)); + } + + function url($path = NULL, array $options = array()) { + // Merge in defaults. + $options += array( + 'fragment' => '', + 'query' => array(), + 'absolute' => FALSE, + 'alias' => FALSE, + 'prefix' => '' + ); + + // A duplicate of the code from url_is_external() to avoid needing another + // function call, since performance inside url() is critical. + if (!isset($options['external'])) { + // Return an external link if $path contains an allowed absolute URL. Avoid + // calling drupal_strip_dangerous_protocols() if there is any slash (/), + // hash (#) or question_mark (?) before the colon (:) occurrence - if any - + // as this would clearly mean it is not a URL. If the path starts with 2 + // slashes then it is always considered an external URL without an explicit + // protocol part. + $colonpos = strpos($path, ':'); + $options['external'] = (strpos($path, '//') === 0) + || ($colonpos !== FALSE + && !preg_match('![/?#]!', substr($path, 0, $colonpos)) + && drupal_strip_dangerous_protocols($path) == $path); + } + + // Preserve the original path before altering or aliasing. + $original_path = $path; + + // Allow other modules to alter the outbound URL and options. + // drupal_alter('url_outbound', $path, $options, $original_path); + + if (isset($options['fragment']) && $options['fragment'] !== '') { + $options['fragment'] = '#' . $options['fragment']; + } + + if ($options['external']) { + // Split off the fragment. + if (strpos($path, '#') !== FALSE) { + list($path, $old_fragment) = explode('#', $path, 2); + // If $options contains no fragment, take it over from the path. + if (isset($old_fragment) && !$options['fragment']) { + $options['fragment'] = '#' . $old_fragment; + } + } + // Append the query. + if ($options['query']) { + $path .= (strpos($path, '?') !== FALSE ? '&' : '?') . drupal_http_build_query($options['query']); + } + if (isset($options['https']) && variable_get('https', FALSE)) { + if ($options['https'] === TRUE) { + $path = str_replace('http://', 'https://', $path); + } + elseif ($options['https'] === FALSE) { + $path = str_replace('https://', 'http://', $path); + } + } + // Reassemble. + return $path . $options['fragment']; + } + + // Strip leading slashes from internal paths to prevent them becoming external + // URLs without protocol. /example.com should not be turned into + // //example.com. + $path = ltrim($path, '/'); + + global $base_url, $base_secure_url, $base_insecure_url; + + // The base_url might be rewritten from the language rewrite in domain mode. + if (!isset($options['base_url'])) { + $options['base_url'] = 'http://amazons3.example.com'; + } + + // The special path '' links to the default front page. + if ($path == '') { + $path = ''; + } + + $base = $options['absolute'] ? $options['base_url'] . '/' : base_path(); + $prefix = empty($path) ? rtrim($options['prefix'], '/') : $options['prefix']; + + $path = drupal_encode_path($prefix . $path); + if ($options['query']) { + return $base . $path . '?' . drupal_http_build_query($options['query']) . $options['fragment']; + } + else { + return $base . $path . $options['fragment']; + } + } + /** * Static version of variable_get() for testing. * @@ -10,6 +104,11 @@ namespace Drupal\amazons3 { * @return string */ function variable_get($name, $default = NULL) { + switch ($name) { + case 'https': + return FALSE; + } + return 'placeholder'; } -- 2.3.5 From fbc32b2e709f2aaaa807cdf70f8248076c0649a1 Mon Sep 17 00:00:00 2001 From: Andrew Berry Date: Wed, 22 Apr 2015 10:54:55 -0400 Subject: [PATCH 15/43] Issue #2471823: Test generating an image style URL. --- tests/StreamWrapperTest.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/StreamWrapperTest.php b/tests/StreamWrapperTest.php index 0584e9d..052f7b7 100644 --- a/tests/StreamWrapperTest.php +++ b/tests/StreamWrapperTest.php @@ -151,6 +151,11 @@ class StreamWrapperTest extends \PHPUnit_Framework_TestCase { '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); } /** @@ -222,6 +227,7 @@ class StreamWrapperTest extends \PHPUnit_Framework_TestCase { $cache = $reflect->getProperty('cache'); $cache->setAccessible(TRUE); $this->assertInstanceOf('Capgemini\Cache\DrupalDoctrineCache', $cache->getValue()->getCacheObject()); + StreamWrapper::detachCache(); } /** @@ -267,6 +273,16 @@ class StreamWrapperTest extends \PHPUnit_Framework_TestCase { } /** + * Test when an image doesn't exist that we return the internal style URL. + * @covers \Drupal\amazons3\StreamWrapper::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()); + } + + /** * Test that a null dirname returns the bucket associated with the wrapper. * * @covers \Drupal\amazons3\StreamWrapper::dirname -- 2.3.5 From 33ac8919d0f32a04408b00ac381f3e0a6ffb50d3 Mon Sep 17 00:00:00 2001 From: Andrew Berry Date: Wed, 22 Apr 2015 10:58:54 -0400 Subject: [PATCH 16/43] Issue #2471823: Complete getExternalUrl() coverage. --- src/StreamWrapper.php | 2 ++ tests/StreamWrapperTest.php | 12 ++++++++++++ 2 files changed, 14 insertions(+) diff --git a/src/StreamWrapper.php b/src/StreamWrapper.php index 077a7da..b5bdcc3 100644 --- a/src/StreamWrapper.php +++ b/src/StreamWrapper.php @@ -230,12 +230,14 @@ class StreamWrapper extends \Aws\S3\StreamWrapper implements \DrupalStreamWrappe // Save as. // @todo Object constructor. + /** foreach ($this->config->getSaveAsPaths() as $path) { if ($path === '*' || preg_match('#' . strtr($path, '#', '\#') . '#', $local_path)) { $args['ResponseContentDisposition'] = 'attachment; filename=' . basename($local_path); break; } } + */ // Generate a standard URL. $url = static::$client->getObjectUrl($this->uri->getBucket(), $this->getLocalPath()); diff --git a/tests/StreamWrapperTest.php b/tests/StreamWrapperTest.php index 052f7b7..ca8273b 100644 --- a/tests/StreamWrapperTest.php +++ b/tests/StreamWrapperTest.php @@ -274,6 +274,7 @@ class StreamWrapperTest extends \PHPUnit_Framework_TestCase { /** * Test when an image doesn't exist that we return the internal style URL. + * * @covers \Drupal\amazons3\StreamWrapper::getExternalUrl */ public function testExternalImageStyleUri() { @@ -283,6 +284,17 @@ class StreamWrapperTest extends \PHPUnit_Framework_TestCase { } /** + * Test regular URL generation. + * + * @covers \Drupal\amazons3\StreamWrapper::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()); + } + + /** * Test that a null dirname returns the bucket associated with the wrapper. * * @covers \Drupal\amazons3\StreamWrapper::dirname -- 2.3.5 From 506fbcfcff06e90ffbac29b4b671b533c923499d Mon Sep 17 00:00:00 2001 From: Andrew Berry Date: Wed, 22 Apr 2015 13:15:17 -0400 Subject: [PATCH 17/43] Issue #2471823: Ignore vendor and composer.lock. --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7579f74 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +vendor +composer.lock -- 2.3.5 From 61e23ce08fe1014c437de5d4103134aa6edca646 Mon Sep 17 00:00:00 2001 From: Andrew Berry Date: Wed, 22 Apr 2015 13:15:43 -0400 Subject: [PATCH 18/43] Issue #2471823: Add autoload-dev support for test classes. --- composer.json | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 9bd62b7..7d0976e 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,15 @@ "phpunit/phpunit": "~4.6" }, "autoload": { - "psr-4": {"Drupal\\amazons3\\": "src/"} + "psr-4": { + "Drupal\\amazons3\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { + "Drupal\\amazons3Test\\": "tests/" + }, + "classmap": ["tests/include"] }, "repositories": [ { -- 2.3.5 From a4175116518e1a2f447e9f54cff3cc35889b9826 Mon Sep 17 00:00:00 2001 From: Andrew Berry Date: Wed, 22 Apr 2015 13:16:07 -0400 Subject: [PATCH 19/43] Issue #2471823: Switch to in-module vendor testing. --- README.md | 13 +++++++++++++ phpunit.xml.dist | 6 ++---- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 65ffd0e..fcd8244 100644 --- a/README.md +++ b/README.md @@ -39,3 +39,16 @@ You can modify the generated URL and it's properties, this is very useful for se You can also alter the metadata for each object saved to S3 with hook_amazons3_save_headers(). This is very useful for forcing the content-disposition header to force download files if they're being delivered through CloudFront presigned URLs. See amazons3.api.php + +## Running PHPUnit tests + +The included unit tests do not have any dependency on a Drupal installation. By +using PHPUnit, you can integrate test results into your IDE of choice. + +* Run `composer install` in the module directory. +* Run `vendor/bin/phpunit tests`. + +In PHPStorm, it's easiest to configure PHPUnit to use the autoloader generated +in vendor/autoload.php. It's also good to mark the vendor directory as +excluded, if you already have a vendor directory indexed from composer_manager. + diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 1a6f41d..4a15a75 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,7 +1,5 @@ - - + @@ -11,7 +9,7 @@ - ../../vendor + ./vendor -- 2.3.5 From 6238c0c99044a93066906cd29541638d0798735e Mon Sep 17 00:00:00 2001 From: Andrew Berry Date: Wed, 22 Apr 2015 13:18:23 -0400 Subject: [PATCH 20/43] Issue #2471823: Use traits for core global functions. --- src/DrupalAdapter/Bootstrap.php | 21 +++++++++++++++++++++ src/DrupalAdapter/Common.php | 29 +++++++++++++++++++++++++++++ src/DrupalAdapter/FileMimetypes.php | 21 +++++++++++++++++++++ src/S3Client.php | 3 ++- src/StreamWrapper.php | 13 ++++++++----- 5 files changed, 81 insertions(+), 6 deletions(-) create mode 100644 src/DrupalAdapter/Bootstrap.php create mode 100644 src/DrupalAdapter/Common.php create mode 100644 src/DrupalAdapter/FileMimetypes.php diff --git a/src/DrupalAdapter/Bootstrap.php b/src/DrupalAdapter/Bootstrap.php new file mode 100644 index 0000000..a012c71 --- /dev/null +++ b/src/DrupalAdapter/Bootstrap.php @@ -0,0 +1,21 @@ +uri)) { - return url($this::stylesCallback . '/' . $this->uri->getBucket() . $this->uri->getPath(), array('absolute' => TRUE)); + return $this->url($this::stylesCallback . '/' . $this->uri->getBucket() . $this->uri->getPath(), array('absolute' => TRUE)); } } @@ -250,8 +250,11 @@ class StreamWrapper extends \Aws\S3\StreamWrapper implements \DrupalStreamWrappe */ public static function getMimeType($uri, $mapping = NULL) { // Load the default file map. - include_once DRUPAL_ROOT . '/includes/file.mimetypes.inc'; - $mapping = file_mimetype_mapping(); + // @codeCoverageIgnoreStart + if (!$mapping) { + $mapping = static::file_mimetype_mapping(); + } + // @codeCoverageIgnoreEnd $extension = ''; $file_parts = explode('.', basename($uri)); -- 2.3.5 From 6a0b6c3b713eee66503f75ad657f1b168930e1f9 Mon Sep 17 00:00:00 2001 From: Andrew Berry Date: Wed, 22 Apr 2015 13:19:01 -0400 Subject: [PATCH 21/43] Issue #2471823: Add a method to set the S3 client class name. --- src/StreamWrapper.php | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/StreamWrapper.php b/src/StreamWrapper.php index c7e2fa0..3862fe7 100644 --- a/src/StreamWrapper.php +++ b/src/StreamWrapper.php @@ -25,6 +25,13 @@ class StreamWrapper extends \Aws\S3\StreamWrapper implements \DrupalStreamWrappe const stylesCallback = 'amazons3/image-derivative'; /** + * The name of the S3Client class to use. + * + * @var string + */ + protected static $s3ClientClass = 'S3Client'; + + /** * Default configuration used when constructing a new stream wrapper. * * @var \Drupal\amazons3\StreamWrapperConfiguration @@ -71,6 +78,15 @@ class StreamWrapper extends \Aws\S3\StreamWrapper implements \DrupalStreamWrappe } /** + * Set the name of the S3Client class to use. + * + * @param string $client + */ + public static function setS3ClientClass($client) { + static::$s3ClientClass = $client; + } + + /** * Construct a new stream wrapper. * * @param \Drupal\amazons3\StreamWrapperConfiguration $config @@ -89,7 +105,9 @@ class StreamWrapper extends \Aws\S3\StreamWrapper implements \DrupalStreamWrappe $this->config = $config; if (!$this->getClient()) { - $this->setClient(S3Client::factory()); + /** @var S3Client $name */ + $name = static::$s3ClientClass; + $this->setClient($name::factory()); } if ($this->config->isCaching() && !static::$cache) { -- 2.3.5 From 1cef219b46c8c7de813b2da0f83e7442ac571778 Mon Sep 17 00:00:00 2001 From: Andrew Berry Date: Wed, 22 Apr 2015 13:28:15 -0400 Subject: [PATCH 22/43] Issue #2471823: Add stubs for tests --- tests/DrupalAdapter/Bootstrap.php | 32 ++ tests/DrupalAdapter/Common.php | 115 +++++ tests/DrupalAdapter/FileMimetypes.php | 874 ++++++++++++++++++++++++++++++++++ tests/Stub/S3Client.php | 15 + tests/Stub/StreamWrapper.php | 17 + 5 files changed, 1053 insertions(+) create mode 100644 tests/DrupalAdapter/Bootstrap.php create mode 100644 tests/DrupalAdapter/Common.php create mode 100644 tests/DrupalAdapter/FileMimetypes.php create mode 100644 tests/Stub/S3Client.php create mode 100644 tests/Stub/StreamWrapper.php diff --git a/tests/DrupalAdapter/Bootstrap.php b/tests/DrupalAdapter/Bootstrap.php new file mode 100644 index 0000000..d069e46 --- /dev/null +++ b/tests/DrupalAdapter/Bootstrap.php @@ -0,0 +1,32 @@ + '', + 'query' => array(), + 'absolute' => FALSE, + 'alias' => FALSE, + 'prefix' => '' + ); + + // A duplicate of the code from url_is_external() to avoid needing another + // function call, since performance inside url() is critical. + if (!isset($options['external'])) { + // Return an external link if $path contains an allowed absolute URL. Avoid + // calling drupal_strip_dangerous_protocols() if there is any slash (/), + // hash (#) or question_mark (?) before the colon (:) occurrence - if any - + // as this would clearly mean it is not a URL. If the path starts with 2 + // slashes then it is always considered an external URL without an explicit + // protocol part. + $colonpos = strpos($path, ':'); + $options['external'] = (strpos($path, '//') === 0) + || ($colonpos !== FALSE + && !preg_match('![/?#]!', substr($path, 0, $colonpos)) + && drupal_strip_dangerous_protocols($path) == $path); + } + + // Preserve the original path before altering or aliasing. + $original_path = $path; + + // Allow other modules to alter the outbound URL and options. + // drupal_alter('url_outbound', $path, $options, $original_path); + + if (isset($options['fragment']) && $options['fragment'] !== '') { + $options['fragment'] = '#' . $options['fragment']; + } + + if ($options['external']) { + // Split off the fragment. + if (strpos($path, '#') !== FALSE) { + list($path, $old_fragment) = explode('#', $path, 2); + // If $options contains no fragment, take it over from the path. + if (isset($old_fragment) && !$options['fragment']) { + $options['fragment'] = '#' . $old_fragment; + } + } + // Append the query. + if ($options['query']) { + $path .= (strpos($path, '?') !== FALSE ? '&' : '?') . drupal_http_build_query($options['query']); + } + if (isset($options['https']) && variable_get('https', FALSE)) { + if ($options['https'] === TRUE) { + $path = str_replace('http://', 'https://', $path); + } + elseif ($options['https'] === FALSE) { + $path = str_replace('https://', 'http://', $path); + } + } + // Reassemble. + return $path . $options['fragment']; + } + + // Strip leading slashes from internal paths to prevent them becoming external + // URLs without protocol. /example.com should not be turned into + // //example.com. + $path = ltrim($path, '/'); + + global $base_url, $base_secure_url, $base_insecure_url; + + // The base_url might be rewritten from the language rewrite in domain mode. + if (!isset($options['base_url'])) { + $options['base_url'] = 'http://amazons3.example.com'; + } + + // The special path '' links to the default front page. + if ($path == '') { + $path = ''; + } + + $base = $options['absolute'] ? $options['base_url'] . '/' : base_path(); + $prefix = empty($path) ? rtrim($options['prefix'], '/') : $options['prefix']; + + $path = static::drupal_encode_path($prefix . $path); + if ($options['query']) { + return $base . $path . '?' . drupal_http_build_query($options['query']) . $options['fragment']; + } + else { + return $base . $path . $options['fragment']; + } + } + + /** + * @param $path + * @return mixed + */ + public static function drupal_encode_path($path) { + return str_replace('%2F', '/', rawurlencode($path)); + } +} diff --git a/tests/DrupalAdapter/FileMimetypes.php b/tests/DrupalAdapter/FileMimetypes.php new file mode 100644 index 0000000..8096f3e --- /dev/null +++ b/tests/DrupalAdapter/FileMimetypes.php @@ -0,0 +1,874 @@ + array( + 0 => 'application/andrew-inset', + 1 => 'application/atom', + 2 => 'application/atomcat+xml', + 3 => 'application/atomserv+xml', + 4 => 'application/cap', + 5 => 'application/cu-seeme', + 6 => 'application/dsptype', + 350 => 'application/epub+zip', + 7 => 'application/hta', + 8 => 'application/java-archive', + 9 => 'application/java-serialized-object', + 10 => 'application/java-vm', + 11 => 'application/mac-binhex40', + 12 => 'application/mathematica', + 13 => 'application/msaccess', + 14 => 'application/msword', + 15 => 'application/octet-stream', + 16 => 'application/oda', + 17 => 'application/ogg', + 18 => 'application/pdf', + 19 => 'application/pgp-keys', + 20 => 'application/pgp-signature', + 21 => 'application/pics-rules', + 22 => 'application/postscript', + 23 => 'application/rar', + 24 => 'application/rdf+xml', + 25 => 'application/rss+xml', + 26 => 'application/rtf', + 27 => 'application/smil', + 349 => 'application/vnd.amazon.ebook', + 28 => 'application/vnd.cinderella', + 29 => 'application/vnd.google-earth.kml+xml', + 30 => 'application/vnd.google-earth.kmz', + 31 => 'application/vnd.mozilla.xul+xml', + 32 => 'application/vnd.ms-excel', + 33 => 'application/vnd.ms-excel.addin.macroEnabled.12', + 34 => 'application/vnd.ms-excel.sheet.binary.macroEnabled.12', + 35 => 'application/vnd.ms-excel.sheet.macroEnabled.12', + 36 => 'application/vnd.ms-excel.template.macroEnabled.12', + 37 => 'application/vnd.ms-pki.seccat', + 38 => 'application/vnd.ms-pki.stl', + 39 => 'application/vnd.ms-powerpoint', + 40 => 'application/vnd.ms-powerpoint.addin.macroEnabled.12', + 41 => 'application/vnd.ms-powerpoint.presentation.macroEnabled.12', + 42 => 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12', + 43 => 'application/vnd.ms-powerpoint.template.macroEnabled.12', + 44 => 'application/vnd.ms-word.document.macroEnabled.12', + 45 => 'application/vnd.ms-word.template.macroEnabled.12', + 46 => 'application/vnd.ms-xpsdocument', + 47 => 'application/vnd.oasis.opendocument.chart', + 48 => 'application/vnd.oasis.opendocument.database', + 49 => 'application/vnd.oasis.opendocument.formula', + 50 => 'application/vnd.oasis.opendocument.graphics', + 51 => 'application/vnd.oasis.opendocument.graphics-template', + 52 => 'application/vnd.oasis.opendocument.image', + 53 => 'application/vnd.oasis.opendocument.presentation', + 54 => 'application/vnd.oasis.opendocument.presentation-template', + 55 => 'application/vnd.oasis.opendocument.spreadsheet', + 56 => 'application/vnd.oasis.opendocument.spreadsheet-template', + 57 => 'application/vnd.oasis.opendocument.text', + 58 => 'application/vnd.oasis.opendocument.text-master', + 59 => 'application/vnd.oasis.opendocument.text-template', + 60 => 'application/vnd.oasis.opendocument.text-web', + 61 => 'application/vnd.openxmlformats-officedocument.presentationml.presentation', + 62 => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow', + 63 => 'application/vnd.openxmlformats-officedocument.presentationml.template', + 64 => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', + 65 => 'application/vnd.openxmlformats-officedocument.spreadsheetml.template', + 66 => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', + 67 => 'application/vnd.openxmlformats-officedocument.wordprocessingml.template', + 68 => 'application/vnd.rim.cod', + 69 => 'application/vnd.smaf', + 70 => 'application/vnd.stardivision.calc', + 71 => 'application/vnd.stardivision.chart', + 72 => 'application/vnd.stardivision.draw', + 73 => 'application/vnd.stardivision.impress', + 74 => 'application/vnd.stardivision.math', + 75 => 'application/vnd.stardivision.writer', + 76 => 'application/vnd.stardivision.writer-global', + 77 => 'application/vnd.sun.xml.calc', + 78 => 'application/vnd.sun.xml.calc.template', + 79 => 'application/vnd.sun.xml.draw', + 80 => 'application/vnd.sun.xml.draw.template', + 81 => 'application/vnd.sun.xml.impress', + 82 => 'application/vnd.sun.xml.impress.template', + 83 => 'application/vnd.sun.xml.math', + 84 => 'application/vnd.sun.xml.writer', + 85 => 'application/vnd.sun.xml.writer.global', + 86 => 'application/vnd.sun.xml.writer.template', + 87 => 'application/vnd.symbian.install', + 88 => 'application/vnd.visio', + 89 => 'application/vnd.wap.wbxml', + 90 => 'application/vnd.wap.wmlc', + 91 => 'application/vnd.wap.wmlscriptc', + 92 => 'application/wordperfect', + 93 => 'application/wordperfect5.1', + 94 => 'application/x-123', + 95 => 'application/x-7z-compressed', + 96 => 'application/x-abiword', + 97 => 'application/x-apple-diskimage', + 98 => 'application/x-bcpio', + 99 => 'application/x-bittorrent', + 100 => 'application/x-cab', + 101 => 'application/x-cbr', + 102 => 'application/x-cbz', + 103 => 'application/x-cdf', + 104 => 'application/x-cdlink', + 105 => 'application/x-chess-pgn', + 106 => 'application/x-cpio', + 107 => 'application/x-debian-package', + 108 => 'application/x-director', + 109 => 'application/x-dms', + 110 => 'application/x-doom', + 111 => 'application/x-dvi', + 112 => 'application/x-flac', + 113 => 'application/x-font', + 114 => 'application/x-freemind', + 115 => 'application/x-futuresplash', + 116 => 'application/x-gnumeric', + 117 => 'application/x-go-sgf', + 118 => 'application/x-graphing-calculator', + 119 => 'application/x-gtar', + 120 => 'application/x-hdf', + 121 => 'application/x-httpd-eruby', + 122 => 'application/x-httpd-php', + 123 => 'application/x-httpd-php-source', + 124 => 'application/x-httpd-php3', + 125 => 'application/x-httpd-php3-preprocessed', + 126 => 'application/x-httpd-php4', + 127 => 'application/x-ica', + 128 => 'application/x-internet-signup', + 129 => 'application/x-iphone', + 130 => 'application/x-iso9660-image', + 131 => 'application/x-java-jnlp-file', + 132 => 'application/x-javascript', + 133 => 'application/x-jmol', + 134 => 'application/x-kchart', + 135 => 'application/x-killustrator', + 136 => 'application/x-koan', + 137 => 'application/x-kpresenter', + 138 => 'application/x-kspread', + 139 => 'application/x-kword', + 140 => 'application/x-latex', + 141 => 'application/x-lha', + 142 => 'application/x-lyx', + 143 => 'application/x-lzh', + 144 => 'application/x-lzx', + 145 => 'application/x-maker', + 146 => 'application/x-mif', + 351 => 'application/x-mobipocket-ebook', + 352 => 'application/x-mobipocket-ebook', + 147 => 'application/x-ms-wmd', + 148 => 'application/x-ms-wmz', + 149 => 'application/x-msdos-program', + 150 => 'application/x-msi', + 151 => 'application/x-netcdf', + 152 => 'application/x-ns-proxy-autoconfig', + 153 => 'application/x-nwc', + 154 => 'application/x-object', + 155 => 'application/x-oz-application', + 156 => 'application/x-pkcs7-certreqresp', + 157 => 'application/x-pkcs7-crl', + 158 => 'application/x-python-code', + 159 => 'application/x-quicktimeplayer', + 160 => 'application/x-redhat-package-manager', + 161 => 'application/x-shar', + 162 => 'application/x-shockwave-flash', + 163 => 'application/x-stuffit', + 164 => 'application/x-sv4cpio', + 165 => 'application/x-sv4crc', + 166 => 'application/x-tar', + 167 => 'application/x-tcl', + 168 => 'application/x-tex-gf', + 169 => 'application/x-tex-pk', + 170 => 'application/x-texinfo', + 171 => 'application/x-trash', + 172 => 'application/x-troff', + 173 => 'application/x-troff-man', + 174 => 'application/x-troff-me', + 175 => 'application/x-troff-ms', + 176 => 'application/x-ustar', + 177 => 'application/x-wais-source', + 178 => 'application/x-wingz', + 179 => 'application/x-x509-ca-cert', + 180 => 'application/x-xcf', + 181 => 'application/x-xfig', + 182 => 'application/x-xpinstall', + 183 => 'application/xhtml+xml', + 184 => 'application/xml', + 185 => 'application/zip', + 186 => 'audio/basic', + 187 => 'audio/midi', + 346 => 'audio/mp4', + 188 => 'audio/mpeg', + 189 => 'audio/ogg', + 190 => 'audio/prs.sid', + 356 => 'audio/webm', + 191 => 'audio/x-aiff', + 192 => 'audio/x-gsm', + 354 => 'audio/x-matroska', + 193 => 'audio/x-mpegurl', + 194 => 'audio/x-ms-wax', + 195 => 'audio/x-ms-wma', + 196 => 'audio/x-pn-realaudio', + 197 => 'audio/x-realaudio', + 198 => 'audio/x-scpls', + 199 => 'audio/x-sd2', + 200 => 'audio/x-wav', + 201 => 'chemical/x-alchemy', + 202 => 'chemical/x-cache', + 203 => 'chemical/x-cache-csf', + 204 => 'chemical/x-cactvs-binary', + 205 => 'chemical/x-cdx', + 206 => 'chemical/x-cerius', + 207 => 'chemical/x-chem3d', + 208 => 'chemical/x-chemdraw', + 209 => 'chemical/x-cif', + 210 => 'chemical/x-cmdf', + 211 => 'chemical/x-cml', + 212 => 'chemical/x-compass', + 213 => 'chemical/x-crossfire', + 214 => 'chemical/x-csml', + 215 => 'chemical/x-ctx', + 216 => 'chemical/x-cxf', + 217 => 'chemical/x-embl-dl-nucleotide', + 218 => 'chemical/x-galactic-spc', + 219 => 'chemical/x-gamess-input', + 220 => 'chemical/x-gaussian-checkpoint', + 221 => 'chemical/x-gaussian-cube', + 222 => 'chemical/x-gaussian-input', + 223 => 'chemical/x-gaussian-log', + 224 => 'chemical/x-gcg8-sequence', + 225 => 'chemical/x-genbank', + 226 => 'chemical/x-hin', + 227 => 'chemical/x-isostar', + 228 => 'chemical/x-jcamp-dx', + 229 => 'chemical/x-kinemage', + 230 => 'chemical/x-macmolecule', + 231 => 'chemical/x-macromodel-input', + 232 => 'chemical/x-mdl-molfile', + 233 => 'chemical/x-mdl-rdfile', + 234 => 'chemical/x-mdl-rxnfile', + 235 => 'chemical/x-mdl-sdfile', + 236 => 'chemical/x-mdl-tgf', + 237 => 'chemical/x-mmcif', + 238 => 'chemical/x-mol2', + 239 => 'chemical/x-molconn-Z', + 240 => 'chemical/x-mopac-graph', + 241 => 'chemical/x-mopac-input', + 242 => 'chemical/x-mopac-out', + 243 => 'chemical/x-mopac-vib', + 244 => 'chemical/x-ncbi-asn1-ascii', + 245 => 'chemical/x-ncbi-asn1-binary', + 246 => 'chemical/x-ncbi-asn1-spec', + 247 => 'chemical/x-pdb', + 248 => 'chemical/x-rosdal', + 249 => 'chemical/x-swissprot', + 250 => 'chemical/x-vamas-iso14976', + 251 => 'chemical/x-vmd', + 252 => 'chemical/x-xtel', + 253 => 'chemical/x-xyz', + 254 => 'image/gif', + 255 => 'image/ief', + 256 => 'image/jpeg', + 257 => 'image/pcx', + 258 => 'image/png', + 259 => 'image/svg+xml', + 260 => 'image/tiff', + 261 => 'image/vnd.djvu', + 262 => 'image/vnd.microsoft.icon', + 263 => 'image/vnd.wap.wbmp', + 355 => 'image/webp', + 264 => 'image/x-cmu-raster', + 265 => 'image/x-coreldraw', + 266 => 'image/x-coreldrawpattern', + 267 => 'image/x-coreldrawtemplate', + 268 => 'image/x-corelphotopaint', + 269 => 'image/x-jg', + 270 => 'image/x-jng', + 271 => 'image/x-ms-bmp', + 272 => 'image/x-photoshop', + 273 => 'image/x-portable-anymap', + 274 => 'image/x-portable-bitmap', + 275 => 'image/x-portable-graymap', + 276 => 'image/x-portable-pixmap', + 277 => 'image/x-rgb', + 278 => 'image/x-xbitmap', + 279 => 'image/x-xpixmap', + 280 => 'image/x-xwindowdump', + 281 => 'message/rfc822', + 282 => 'model/iges', + 283 => 'model/mesh', + 284 => 'model/vrml', + 285 => 'text/calendar', + 286 => 'text/css', + 287 => 'text/csv', + 288 => 'text/h323', + 289 => 'text/html', + 290 => 'text/iuls', + 291 => 'text/mathml', + 292 => 'text/plain', + 293 => 'text/richtext', + 294 => 'text/scriptlet', + 295 => 'text/tab-separated-values', + 296 => 'text/texmacs', + 297 => 'text/vnd.sun.j2me.app-descriptor', + 298 => 'text/vnd.wap.wml', + 299 => 'text/vnd.wap.wmlscript', + 358 => 'text/vtt', + 300 => 'text/x-bibtex', + 301 => 'text/x-boo', + 302 => 'text/x-c++hdr', + 303 => 'text/x-c++src', + 304 => 'text/x-chdr', + 305 => 'text/x-component', + 306 => 'text/x-csh', + 307 => 'text/x-csrc', + 308 => 'text/x-diff', + 309 => 'text/x-dsrc', + 310 => 'text/x-haskell', + 311 => 'text/x-java', + 312 => 'text/x-literate-haskell', + 313 => 'text/x-moc', + 314 => 'text/x-pascal', + 315 => 'text/x-pcs-gcd', + 316 => 'text/x-perl', + 317 => 'text/x-python', + 318 => 'text/x-setext', + 319 => 'text/x-sh', + 320 => 'text/x-tcl', + 321 => 'text/x-tex', + 322 => 'text/x-vcalendar', + 323 => 'text/x-vcard', + 324 => 'video/3gpp', + 325 => 'video/dl', + 326 => 'video/dv', + 327 => 'video/fli', + 328 => 'video/gl', + 329 => 'video/mp4', + 330 => 'video/mpeg', + 331 => 'video/ogg', + 332 => 'video/quicktime', + 333 => 'video/vnd.mpegurl', + 357 => 'video/webm', + 347 => 'video/x-flv', + 334 => 'video/x-la-asf', + 348 => 'video/x-m4v', + 353 => 'video/x-matroska', + 335 => 'video/x-mng', + 336 => 'video/x-ms-asf', + 337 => 'video/x-ms-wm', + 338 => 'video/x-ms-wmv', + 339 => 'video/x-ms-wmx', + 340 => 'video/x-ms-wvx', + 341 => 'video/x-msvideo', + 342 => 'video/x-sgi-movie', + 343 => 'x-conference/x-cooltalk', + 344 => 'x-epoc/x-sisx-app', + 345 => 'x-world/x-vrml', + ), + + // Extensions added to this list MUST be lower-case. + 'extensions' => array( + 'ez' => 0, + 'atom' => 1, + 'atomcat' => 2, + 'atomsrv' => 3, + 'cap' => 4, + 'pcap' => 4, + 'cu' => 5, + 'tsp' => 6, + 'hta' => 7, + 'jar' => 8, + 'ser' => 9, + 'class' => 10, + 'hqx' => 11, + 'nb' => 12, + 'mdb' => 13, + 'dot' => 14, + 'doc' => 14, + 'bin' => 15, + 'oda' => 16, + 'ogx' => 17, + 'pdf' => 18, + 'key' => 19, + 'pgp' => 20, + 'prf' => 21, + 'eps' => 22, + 'ai' => 22, + 'ps' => 22, + 'rar' => 23, + 'rdf' => 24, + 'rss' => 25, + 'rtf' => 26, + 'smi' => 27, + 'smil' => 27, + 'cdy' => 28, + 'kml' => 29, + 'kmz' => 30, + 'xul' => 31, + 'xlb' => 32, + 'xlt' => 32, + 'xls' => 32, + 'xlam' => 33, + 'xlsb' => 34, + 'xlsm' => 35, + 'xltm' => 36, + 'cat' => 37, + 'stl' => 38, + 'pps' => 39, + 'ppt' => 39, + 'ppam' => 40, + 'pptm' => 41, + 'ppsm' => 42, + 'potm' => 43, + 'docm' => 44, + 'dotm' => 45, + 'xps' => 46, + 'odc' => 47, + 'odb' => 48, + 'odf' => 49, + 'odg' => 50, + 'otg' => 51, + 'odi' => 52, + 'odp' => 53, + 'otp' => 54, + 'ods' => 55, + 'ots' => 56, + 'odt' => 57, + 'odm' => 58, + 'ott' => 59, + 'oth' => 60, + 'pptx' => 61, + 'ppsx' => 62, + 'potx' => 63, + 'xlsx' => 64, + 'xltx' => 65, + 'docx' => 66, + 'dotx' => 67, + 'cod' => 68, + 'mmf' => 69, + 'sdc' => 70, + 'sds' => 71, + 'sda' => 72, + 'sdd' => 73, + 'sdw' => 75, + 'sgl' => 76, + 'sxc' => 77, + 'stc' => 78, + 'sxd' => 79, + 'std' => 80, + 'sxi' => 81, + 'sti' => 82, + 'sxm' => 83, + 'sxw' => 84, + 'sxg' => 85, + 'stw' => 86, + 'sis' => 87, + 'vsd' => 88, + 'wbxml' => 89, + 'wmlc' => 90, + 'wmlsc' => 91, + 'wpd' => 92, + 'wp5' => 93, + 'wk' => 94, + '7z' => 95, + 'abw' => 96, + 'dmg' => 97, + 'bcpio' => 98, + 'torrent' => 99, + 'cab' => 100, + 'cbr' => 101, + 'cbz' => 102, + 'cdf' => 103, + 'vcd' => 104, + 'pgn' => 105, + 'cpio' => 106, + 'udeb' => 107, + 'deb' => 107, + 'dir' => 108, + 'dxr' => 108, + 'dcr' => 108, + 'dms' => 109, + 'wad' => 110, + 'dvi' => 111, + 'flac' => 112, + 'pfa' => 113, + 'pfb' => 113, + 'pcf' => 113, + 'gsf' => 113, + 'pcf.z' => 113, + 'mm' => 114, + 'spl' => 115, + 'gnumeric' => 116, + 'sgf' => 117, + 'gcf' => 118, + 'taz' => 119, + 'gtar' => 119, + 'tgz' => 119, + 'hdf' => 120, + 'rhtml' => 121, + 'phtml' => 122, + 'pht' => 122, + 'php' => 122, + 'phps' => 123, + 'php3' => 124, + 'php3p' => 125, + 'php4' => 126, + 'ica' => 127, + 'ins' => 128, + 'isp' => 128, + 'iii' => 129, + 'iso' => 130, + 'jnlp' => 131, + 'js' => 132, + 'jmz' => 133, + 'chrt' => 134, + 'kil' => 135, + 'skp' => 136, + 'skd' => 136, + 'skm' => 136, + 'skt' => 136, + 'kpr' => 137, + 'kpt' => 137, + 'ksp' => 138, + 'kwd' => 139, + 'kwt' => 139, + 'latex' => 140, + 'lha' => 141, + 'lyx' => 142, + 'lzh' => 143, + 'lzx' => 144, + 'maker' => 145, + 'frm' => 145, + 'frame' => 145, + 'fm' => 145, + 'book' => 145, + 'fb' => 145, + 'fbdoc' => 145, + 'mif' => 146, + 'wmd' => 147, + 'wmz' => 148, + 'dll' => 149, + 'bat' => 149, + 'exe' => 149, + 'com' => 149, + 'msi' => 150, + 'nc' => 151, + 'pac' => 152, + 'nwc' => 153, + 'o' => 154, + 'oza' => 155, + 'p7r' => 156, + 'crl' => 157, + 'pyo' => 158, + 'pyc' => 158, + 'qtl' => 159, + 'rpm' => 160, + 'shar' => 161, + 'swf' => 162, + 'swfl' => 162, + 'sitx' => 163, + 'sit' => 163, + 'sv4cpio' => 164, + 'sv4crc' => 165, + 'tar' => 166, + 'gf' => 168, + 'pk' => 169, + 'texi' => 170, + 'texinfo' => 170, + 'sik' => 171, + '~' => 171, + 'bak' => 171, + '%' => 171, + 'old' => 171, + 't' => 172, + 'roff' => 172, + 'tr' => 172, + 'man' => 173, + 'me' => 174, + 'ms' => 175, + 'ustar' => 176, + 'src' => 177, + 'wz' => 178, + 'crt' => 179, + 'xcf' => 180, + 'fig' => 181, + 'xpi' => 182, + 'xht' => 183, + 'xhtml' => 183, + 'xml' => 184, + 'xsl' => 184, + 'zip' => 185, + 'au' => 186, + 'snd' => 186, + 'mid' => 187, + 'midi' => 187, + 'kar' => 187, + 'mpega' => 188, + 'mpga' => 188, + 'm4a' => 188, + 'mp3' => 188, + 'mp2' => 188, + 'ogg' => 189, + 'oga' => 189, + 'spx' => 189, + 'sid' => 190, + 'aif' => 191, + 'aiff' => 191, + 'aifc' => 191, + 'gsm' => 192, + 'm3u' => 193, + 'wax' => 194, + 'wma' => 195, + 'rm' => 196, + 'ram' => 196, + 'ra' => 197, + 'pls' => 198, + 'sd2' => 199, + 'wav' => 200, + 'alc' => 201, + 'cac' => 202, + 'cache' => 202, + 'csf' => 203, + 'cascii' => 204, + 'cbin' => 204, + 'ctab' => 204, + 'cdx' => 205, + 'cer' => 206, + 'c3d' => 207, + 'chm' => 208, + 'cif' => 209, + 'cmdf' => 210, + 'cml' => 211, + 'cpa' => 212, + 'bsd' => 213, + 'csml' => 214, + 'csm' => 214, + 'ctx' => 215, + 'cxf' => 216, + 'cef' => 216, + 'emb' => 217, + 'embl' => 217, + 'spc' => 218, + 'gam' => 219, + 'inp' => 219, + 'gamin' => 219, + 'fchk' => 220, + 'fch' => 220, + 'cub' => 221, + 'gau' => 222, + 'gjf' => 222, + 'gjc' => 222, + 'gal' => 223, + 'gcg' => 224, + 'gen' => 225, + 'hin' => 226, + 'istr' => 227, + 'ist' => 227, + 'dx' => 228, + 'jdx' => 228, + 'kin' => 229, + 'mcm' => 230, + 'mmd' => 231, + 'mmod' => 231, + 'mol' => 232, + 'rd' => 233, + 'rxn' => 234, + 'sdf' => 235, + 'sd' => 235, + 'tgf' => 236, + 'mcif' => 237, + 'mol2' => 238, + 'b' => 239, + 'gpt' => 240, + 'mopcrt' => 241, + 'zmt' => 241, + 'mpc' => 241, + 'dat' => 241, + 'mop' => 241, + 'moo' => 242, + 'mvb' => 243, + 'prt' => 244, + 'aso' => 245, + 'val' => 245, + 'asn' => 246, + 'ent' => 247, + 'pdb' => 247, + 'ros' => 248, + 'sw' => 249, + 'vms' => 250, + 'vmd' => 251, + 'xtel' => 252, + 'xyz' => 253, + 'gif' => 254, + 'ief' => 255, + 'jpeg' => 256, + 'jpe' => 256, + 'jpg' => 256, + 'pcx' => 257, + 'png' => 258, + 'svgz' => 259, + 'svg' => 259, + 'tif' => 260, + 'tiff' => 260, + 'djvu' => 261, + 'djv' => 261, + 'ico' => 262, + 'wbmp' => 263, + 'ras' => 264, + 'cdr' => 265, + 'pat' => 266, + 'cdt' => 267, + 'cpt' => 268, + 'art' => 269, + 'jng' => 270, + 'bmp' => 271, + 'psd' => 272, + 'pnm' => 273, + 'pbm' => 274, + 'pgm' => 275, + 'ppm' => 276, + 'rgb' => 277, + 'xbm' => 278, + 'xpm' => 279, + 'xwd' => 280, + 'eml' => 281, + 'igs' => 282, + 'iges' => 282, + 'silo' => 283, + 'msh' => 283, + 'mesh' => 283, + 'icz' => 285, + 'ics' => 285, + 'css' => 286, + 'csv' => 287, + '323' => 288, + 'html' => 289, + 'htm' => 289, + 'shtml' => 289, + 'uls' => 290, + 'mml' => 291, + 'txt' => 292, + 'pot' => 292, + 'text' => 292, + 'asc' => 292, + 'rtx' => 293, + 'wsc' => 294, + 'sct' => 294, + 'tsv' => 295, + 'ts' => 296, + 'tm' => 296, + 'jad' => 297, + 'wml' => 298, + 'wmls' => 299, + 'bib' => 300, + 'boo' => 301, + 'hpp' => 302, + 'hh' => 302, + 'h++' => 302, + 'hxx' => 302, + 'cxx' => 303, + 'cc' => 303, + 'cpp' => 303, + 'c++' => 303, + 'h' => 304, + 'htc' => 305, + 'csh' => 306, + 'c' => 307, + 'patch' => 308, + 'diff' => 308, + 'd' => 309, + 'hs' => 310, + 'java' => 311, + 'lhs' => 312, + 'moc' => 313, + 'pas' => 314, + 'p' => 314, + 'gcd' => 315, + 'pm' => 316, + 'pl' => 316, + 'py' => 317, + 'etx' => 318, + 'sh' => 319, + 'tk' => 320, + 'tcl' => 320, + 'cls' => 321, + 'ltx' => 321, + 'sty' => 321, + 'tex' => 321, + 'vcs' => 322, + 'vcf' => 323, + '3gp' => 324, + 'dl' => 325, + 'dif' => 326, + 'dv' => 326, + 'fli' => 327, + 'gl' => 328, + 'mp4' => 329, + 'f4v' => 329, + 'f4p' => 329, + 'mpe' => 330, + 'mpeg' => 330, + 'mpg' => 330, + 'ogv' => 331, + 'qt' => 332, + 'mov' => 332, + 'mxu' => 333, + 'lsf' => 334, + 'lsx' => 334, + 'mng' => 335, + 'asx' => 336, + 'asf' => 336, + 'wm' => 337, + 'wmv' => 338, + 'wmx' => 339, + 'wvx' => 340, + 'avi' => 341, + 'movie' => 342, + 'ice' => 343, + 'sisx' => 344, + 'wrl' => 345, + 'vrm' => 345, + 'vrml' => 345, + 'f4a' => 346, + 'f4b' => 346, + 'flv' => 347, + 'm4v' => 348, + 'azw' => 349, + 'epub' => 350, + 'mobi' => 351, + 'prc' => 352, + 'mkv' => 353, + 'mka' => 354, + 'webp' => 355, + 'weba' => 356, + 'webm' => 357, + 'vtt' => 358, + ), + ); + } +} diff --git a/tests/Stub/S3Client.php b/tests/Stub/S3Client.php new file mode 100644 index 0000000..f281996 --- /dev/null +++ b/tests/Stub/S3Client.php @@ -0,0 +1,15 @@ + Date: Wed, 22 Apr 2015 13:28:52 -0400 Subject: [PATCH 23/43] Issue #2471823: Use stub traits in our tests --- tests/StreamWrapperTest.php | 123 ++------------------------------------------ 1 file changed, 4 insertions(+), 119 deletions(-) diff --git a/tests/StreamWrapperTest.php b/tests/StreamWrapperTest.php index ca8273b..6b9fb95 100644 --- a/tests/StreamWrapperTest.php +++ b/tests/StreamWrapperTest.php @@ -1,124 +1,10 @@ '', - 'query' => array(), - 'absolute' => FALSE, - 'alias' => FALSE, - 'prefix' => '' - ); - - // A duplicate of the code from url_is_external() to avoid needing another - // function call, since performance inside url() is critical. - if (!isset($options['external'])) { - // Return an external link if $path contains an allowed absolute URL. Avoid - // calling drupal_strip_dangerous_protocols() if there is any slash (/), - // hash (#) or question_mark (?) before the colon (:) occurrence - if any - - // as this would clearly mean it is not a URL. If the path starts with 2 - // slashes then it is always considered an external URL without an explicit - // protocol part. - $colonpos = strpos($path, ':'); - $options['external'] = (strpos($path, '//') === 0) - || ($colonpos !== FALSE - && !preg_match('![/?#]!', substr($path, 0, $colonpos)) - && drupal_strip_dangerous_protocols($path) == $path); - } - - // Preserve the original path before altering or aliasing. - $original_path = $path; - - // Allow other modules to alter the outbound URL and options. - // drupal_alter('url_outbound', $path, $options, $original_path); - - if (isset($options['fragment']) && $options['fragment'] !== '') { - $options['fragment'] = '#' . $options['fragment']; - } - - if ($options['external']) { - // Split off the fragment. - if (strpos($path, '#') !== FALSE) { - list($path, $old_fragment) = explode('#', $path, 2); - // If $options contains no fragment, take it over from the path. - if (isset($old_fragment) && !$options['fragment']) { - $options['fragment'] = '#' . $old_fragment; - } - } - // Append the query. - if ($options['query']) { - $path .= (strpos($path, '?') !== FALSE ? '&' : '?') . drupal_http_build_query($options['query']); - } - if (isset($options['https']) && variable_get('https', FALSE)) { - if ($options['https'] === TRUE) { - $path = str_replace('http://', 'https://', $path); - } - elseif ($options['https'] === FALSE) { - $path = str_replace('https://', 'http://', $path); - } - } - // Reassemble. - return $path . $options['fragment']; - } - - // Strip leading slashes from internal paths to prevent them becoming external - // URLs without protocol. /example.com should not be turned into - // //example.com. - $path = ltrim($path, '/'); - - global $base_url, $base_secure_url, $base_insecure_url; - - // The base_url might be rewritten from the language rewrite in domain mode. - if (!isset($options['base_url'])) { - $options['base_url'] = 'http://amazons3.example.com'; - } - - // The special path '' links to the default front page. - if ($path == '') { - $path = ''; - } - - $base = $options['absolute'] ? $options['base_url'] . '/' : base_path(); - $prefix = empty($path) ? rtrim($options['prefix'], '/') : $options['prefix']; - - $path = drupal_encode_path($prefix . $path); - if ($options['query']) { - return $base . $path . '?' . drupal_http_build_query($options['query']) . $options['fragment']; - } - else { - return $base . $path . $options['fragment']; - } - } - - /** - * Static version of variable_get() for testing. - * - * @param string $name - * @param null $default - * @return string - */ - function variable_get($name, $default = NULL) { - switch ($name) { - case 'https': - return FALSE; - } - - return 'placeholder'; - } - -} - -namespace Drupal\amazons3Test { +namespace Drupal\amazons3Test; use Aws\Common\Credentials\Credentials; use Aws\S3\S3Client; -use Drupal\amazons3\StreamWrapper; +use Drupal\amazons3Test\Stub\StreamWrapper; use Drupal\amazons3\StreamWrapperConfiguration; /** @@ -138,7 +24,8 @@ class StreamWrapperTest extends \PHPUnit_Framework_TestCase { * {@inheritdoc} */ public function setUp() { - require_once __DIR__ . '/include/DrupalStreamWrapperInterface.inc'; + + StreamWrapper::setS3ClientClass('Drupal\amazons3Test\Stub\S3Client'); $config = StreamWrapperConfiguration::fromConfig([ 'bucket' => 'bucket.example.com', @@ -339,5 +226,3 @@ class StreamWrapperTest extends \PHPUnit_Framework_TestCase { $this->assertEquals('s3://bucket.example.com/subdir/second-subdir', $this->wrapper->dirname('s3://bucket.example.com/subdir/second-subdir/')); } } - -} -- 2.3.5 From 94d7cae593c416b9f267b79b67c0e6b4c0c1b888 Mon Sep 17 00:00:00 2001 From: Andrew Berry Date: Wed, 22 Apr 2015 13:32:35 -0400 Subject: [PATCH 24/43] Issue #2471823: Test getting a mime type. --- tests/StreamWrapperTest.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/StreamWrapperTest.php b/tests/StreamWrapperTest.php index 6b9fb95..570918e 100644 --- a/tests/StreamWrapperTest.php +++ b/tests/StreamWrapperTest.php @@ -182,6 +182,16 @@ class StreamWrapperTest extends \PHPUnit_Framework_TestCase { } /** + * Test getting a mime type. + * + * @covers \Drupal\amazons3\StreamWrapper::getMimeType + */ + public function testGetMimeType() { + $mimeType = StreamWrapper::getMimeType('s3://bucket.example.com/image.jpg'); + $this->assertEquals('image/jpeg', $mimeType); + } + + /** * Test that a null dirname returns the bucket associated with the wrapper. * * @covers \Drupal\amazons3\StreamWrapper::dirname -- 2.3.5 From d3ef5e12731faa11544bd50c3fae6dcbdac0ffc4 Mon Sep 17 00:00:00 2001 From: Andrew Berry Date: Wed, 22 Apr 2015 13:36:04 -0400 Subject: [PATCH 25/43] Issue #2471823: Test setting a scheme-only URI. --- src/StreamWrapper.php | 2 ++ tests/StreamWrapperTest.php | 11 +++++++++++ 2 files changed, 13 insertions(+) diff --git a/src/StreamWrapper.php b/src/StreamWrapper.php index 3862fe7..f81bfbd 100644 --- a/src/StreamWrapper.php +++ b/src/StreamWrapper.php @@ -98,7 +98,9 @@ class StreamWrapper extends \Aws\S3\StreamWrapper implements \DrupalStreamWrappe $config = static::$defaultConfig; } else { + // @codeCoverageIgnoreStart $config = StreamWrapperConfiguration::fromDrupalVariables(); + // @codeCoverageIgnoreEnd } } diff --git a/tests/StreamWrapperTest.php b/tests/StreamWrapperTest.php index 570918e..5850905 100644 --- a/tests/StreamWrapperTest.php +++ b/tests/StreamWrapperTest.php @@ -149,6 +149,17 @@ class StreamWrapperTest extends \PHPUnit_Framework_TestCase { } /** + * Test setting a scheme-only URI. + * + * @covers \Drupal\amazons3\StreamWrapper::setUri + */ + public function testSetSchemeUri() { + $wrapper = new StreamWrapper(); + $wrapper->setUri('s3://'); + $this->assertEquals('s3://bucket.example.com', $wrapper->getUri()); + } + + /** * Test that we throw an exception if a URI is not set. * * @expectedException \LogicException -- 2.3.5 From 50dddbef49582d0ae8e4740823f2b8c4b3f05f25 Mon Sep 17 00:00:00 2001 From: Andrew Berry Date: Wed, 22 Apr 2015 13:39:01 -0400 Subject: [PATCH 26/43] Issue #2471823: Ignore traits in code coverage. --- src/DrupalAdapter/Bootstrap.php | 1 + src/DrupalAdapter/Common.php | 1 + src/DrupalAdapter/FileMimetypes.php | 1 + tests/DrupalAdapter/Bootstrap.php | 1 + tests/DrupalAdapter/Common.php | 1 + tests/DrupalAdapter/FileMimetypes.php | 1 + 6 files changed, 6 insertions(+) diff --git a/src/DrupalAdapter/Bootstrap.php b/src/DrupalAdapter/Bootstrap.php index a012c71..ca90777 100644 --- a/src/DrupalAdapter/Bootstrap.php +++ b/src/DrupalAdapter/Bootstrap.php @@ -7,6 +7,7 @@ namespace Drupal\amazons3\DrupalAdapter; * * @class Bootstrap * @package Drupal\amazons3\DrupalAdapter + * @codeCoverageIgnore */ trait Bootstrap { diff --git a/src/DrupalAdapter/Common.php b/src/DrupalAdapter/Common.php index fd3e88d..1f7e99a 100644 --- a/src/DrupalAdapter/Common.php +++ b/src/DrupalAdapter/Common.php @@ -7,6 +7,7 @@ namespace Drupal\amazons3\DrupalAdapter; * * @trait Common * @package Drupal\amazons3\DrupalAdapter + * @codeCoverageIgnore */ trait Common { diff --git a/src/DrupalAdapter/FileMimetypes.php b/src/DrupalAdapter/FileMimetypes.php index 3fe5400..2150699 100644 --- a/src/DrupalAdapter/FileMimetypes.php +++ b/src/DrupalAdapter/FileMimetypes.php @@ -7,6 +7,7 @@ namespace Drupal\amazons3\DrupalAdapter; * * @trait FileMimetypes * @package Drupal\amazons3\DrupalAdapter + * @codeCoverageIgnore */ trait FileMimetypes { diff --git a/tests/DrupalAdapter/Bootstrap.php b/tests/DrupalAdapter/Bootstrap.php index d069e46..2ea54e5 100644 --- a/tests/DrupalAdapter/Bootstrap.php +++ b/tests/DrupalAdapter/Bootstrap.php @@ -7,6 +7,7 @@ namespace Drupal\amazons3Test\DrupalAdapter; * * @class Bootstrap * @package Drupal\amazons3\DrupalAdapter + * @codeCoverageIgnore */ trait Bootstrap { diff --git a/tests/DrupalAdapter/Common.php b/tests/DrupalAdapter/Common.php index cb3b074..b19e3f2 100644 --- a/tests/DrupalAdapter/Common.php +++ b/tests/DrupalAdapter/Common.php @@ -7,6 +7,7 @@ namespace Drupal\amazons3Test\DrupalAdapter; * * @trait Common * @package Drupal\amazons3\DrupalAdapter + * @codeCoverageIgnore */ trait Common { diff --git a/tests/DrupalAdapter/FileMimetypes.php b/tests/DrupalAdapter/FileMimetypes.php index 8096f3e..391c77e 100644 --- a/tests/DrupalAdapter/FileMimetypes.php +++ b/tests/DrupalAdapter/FileMimetypes.php @@ -8,6 +8,7 @@ namespace Drupal\amazons3Test\DrupalAdapter; * * @trait FileMimetypes * @package Drupal\amazons3\DrupalAdapter + * @codeCoverageIgnore */ trait FileMimetypes { -- 2.3.5 From 7831ca7afb61e6e3bdcdcc20f4a9740619e0989e Mon Sep 17 00:00:00 2001 From: Andrew Berry Date: Wed, 22 Apr 2015 13:43:32 -0400 Subject: [PATCH 27/43] Issue #2471823: Test the default mime type. --- src/StreamWrapper.php | 2 -- tests/StreamWrapperTest.php | 10 ++++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/StreamWrapper.php b/src/StreamWrapper.php index f81bfbd..6f3069b 100644 --- a/src/StreamWrapper.php +++ b/src/StreamWrapper.php @@ -339,8 +339,6 @@ class StreamWrapper extends \Aws\S3\StreamWrapper implements \DrupalStreamWrappe /** * Return the local filesystem path. * - * @todo Test this. - * * @return string * The local path. */ diff --git a/tests/StreamWrapperTest.php b/tests/StreamWrapperTest.php index 5850905..4998958 100644 --- a/tests/StreamWrapperTest.php +++ b/tests/StreamWrapperTest.php @@ -203,6 +203,16 @@ class StreamWrapperTest extends \PHPUnit_Framework_TestCase { } /** + * Test getting the default mime type. + * + * @covers \Drupal\amazons3\StreamWrapper::getMimeType + */ + public function testGetMimeTypeDefault() { + $mimeType = StreamWrapper::getMimeType('s3://bucket.example.com/image'); + $this->assertEquals('application/octet-stream', $mimeType); + } + + /** * Test that a null dirname returns the bucket associated with the wrapper. * * @covers \Drupal\amazons3\StreamWrapper::dirname -- 2.3.5 From cf07af24a9776a3d9cfd8b4b3c1b2dfae2eb9815 Mon Sep 17 00:00:00 2001 From: Andrew Berry Date: Wed, 22 Apr 2015 13:51:25 -0400 Subject: [PATCH 28/43] Issue #2471823: Test stream registrations. --- src/StreamWrapper.php | 4 ++++ tests/StreamWrapperTest.php | 15 +++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/StreamWrapper.php b/src/StreamWrapper.php index 6f3069b..25edf57 100644 --- a/src/StreamWrapper.php +++ b/src/StreamWrapper.php @@ -341,6 +341,10 @@ class StreamWrapper extends \Aws\S3\StreamWrapper implements \DrupalStreamWrappe * * @return string * The local path. + * + * @codeCoverageIgnore + * This method is tested via getExternalUrl(), but phpunit's covers + * annotation won't detect this method. */ protected function getLocalPath() { if (!isset($this->uri)) { diff --git a/tests/StreamWrapperTest.php b/tests/StreamWrapperTest.php index 4998958..e7e00bd 100644 --- a/tests/StreamWrapperTest.php +++ b/tests/StreamWrapperTest.php @@ -256,4 +256,19 @@ class StreamWrapperTest extends \PHPUnit_Framework_TestCase { public function testDirnameTrailingSlash() { $this->assertEquals('s3://bucket.example.com/subdir/second-subdir', $this->wrapper->dirname('s3://bucket.example.com/subdir/second-subdir/')); } + + /** + * Test that stream registrations are blocked. + * + * @covers \Drupal\amazons3\StreamWrapper::register + * @expectedException \LogicException + */ + public function testRegister() { + $client = S3Client::factory( + [ + 'credentials' => new Credentials('placeholder', 'placeholder'), + ] + ); + StreamWrapper::register($client); + } } -- 2.3.5 From 86db042821581d612acd835fca5e5d81abfc2233 Mon Sep 17 00:00:00 2001 From: Andrew Berry Date: Wed, 22 Apr 2015 13:57:53 -0400 Subject: [PATCH 29/43] Issue #2471823: Test getOptions(). --- src/StreamWrapper.php | 2 +- tests/StreamWrapperTest.php | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/StreamWrapper.php b/src/StreamWrapper.php index 25edf57..5698e58 100644 --- a/src/StreamWrapper.php +++ b/src/StreamWrapper.php @@ -370,7 +370,7 @@ class StreamWrapper extends \Aws\S3\StreamWrapper implements \DrupalStreamWrappe * * @return array */ - protected function getOptions() { + public function getOptions() { $options = parent::getOptions(); $options['ACL'] = 'public-read'; return $options; diff --git a/tests/StreamWrapperTest.php b/tests/StreamWrapperTest.php index e7e00bd..f1020d9 100644 --- a/tests/StreamWrapperTest.php +++ b/tests/StreamWrapperTest.php @@ -271,4 +271,14 @@ class StreamWrapperTest extends \PHPUnit_Framework_TestCase { ); StreamWrapper::register($client); } + + /** + * Test that the ACL is set to public-read by default. + * + * @covers \Drupal\amazons3\StreamWrapper::getOptions + */ + public function testGetOptions() { + $wrapper = new StreamWrapper(); + $this->assertArraySubset(array('ACL' => 'public-read'), $wrapper->getOptions()); + } } -- 2.3.5 From aed7ac9f55e1515d521113e342ae77b58287335f Mon Sep 17 00:00:00 2001 From: Andrew Berry Date: Wed, 22 Apr 2015 14:11:13 -0400 Subject: [PATCH 30/43] Issue #2471823: Fix code coverage errors. --- src/StreamWrapper.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/StreamWrapper.php b/src/StreamWrapper.php index 5698e58..353314d 100644 --- a/src/StreamWrapper.php +++ b/src/StreamWrapper.php @@ -212,10 +212,8 @@ class StreamWrapper extends \Aws\S3\StreamWrapper implements \DrupalStreamWrappe // Delivers the first request to an image from the private file system // otherwise it returns an external URL to an image that has not been // created yet. - if ($path_segments[0] === 'styles') { - if (!file_exists((string) $this->uri)) { + if ($path_segments[0] === 'styles' && !file_exists((string) $this->uri)) { return $this->url($this::stylesCallback . '/' . $this->uri->getBucket() . $this->uri->getPath(), array('absolute' => TRUE)); - } } // Allow other modules to change the download link type. @@ -292,7 +290,9 @@ class StreamWrapper extends \Aws\S3\StreamWrapper implements \DrupalStreamWrappe if (isset($mapping['extensions'][$extension])) { return $mapping['mimetypes'][$mapping['extensions'][$extension]]; } + // @codeCoverageIgnoreStart } + // @codeCoverageIgnoreEnd return 'application/octet-stream'; } -- 2.3.5 From 5389215f56b67925ce6cd40887e559c1c97de096 Mon Sep 17 00:00:00 2001 From: Andrew Berry Date: Wed, 22 Apr 2015 14:11:26 -0400 Subject: [PATCH 31/43] Issue #2471823: Test setting a specific S3 client. --- tests/StreamWrapperTest.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/StreamWrapperTest.php b/tests/StreamWrapperTest.php index f1020d9..f37567c 100644 --- a/tests/StreamWrapperTest.php +++ b/tests/StreamWrapperTest.php @@ -281,4 +281,17 @@ class StreamWrapperTest extends \PHPUnit_Framework_TestCase { $wrapper = new StreamWrapper(); $this->assertArraySubset(array('ACL' => 'public-read'), $wrapper->getOptions()); } + + /** + * Test that we can set a different S3 client class. + * + * @covers \Drupal\amazons3\StreamWrapper::setS3ClientClass + */ + public function testSetS3ClientClass() { + StreamWrapper::setS3ClientClass('Drupal\amazons3Test\Stub\S3Client'); + StreamWrapper::setClient(NULL); + $wrapper = new StreamWrapper(); + $credentials = $wrapper->getClient()->getCredentials(); + $this->assertEquals('placeholder', $credentials->getAccessKeyId()); + } } -- 2.3.5 From ad4f52ed5a3851a9dfe76233e0abd2762e927fa0 Mon Sep 17 00:00:00 2001 From: Andrew Berry Date: Wed, 22 Apr 2015 14:34:11 -0400 Subject: [PATCH 32/43] Issue #2471823: Fix double slashes in URLs. --- src/S3Url.php | 12 +++++++++--- tests/S3UrlTest.php | 9 +++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 tests/S3UrlTest.php diff --git a/src/S3Url.php b/src/S3Url.php index d48da5d..4217237 100644 --- a/src/S3Url.php +++ b/src/S3Url.php @@ -18,9 +18,15 @@ class S3Url extends Url { * * @param string $bucket * The bucket to use for the URL. + * @param string $key + * (optional) Key for the URL. */ - public function __construct($bucket, $path = null) { - parent::__construct('s3', $bucket, null, null, null, $path); + public function __construct($bucket, $key = null) { + if ($key) { + $key = '/' . $key; + } + + parent::__construct('s3', $bucket, null, null, null, $key); } @@ -122,7 +128,7 @@ class S3Url extends Url { $parts += $defaults; - return new static($parts['host'], $parts['path']); + return new static($parts['host'], substr($parts['path'], 1)); } /** diff --git a/tests/S3UrlTest.php b/tests/S3UrlTest.php new file mode 100644 index 0000000..1236f41 --- /dev/null +++ b/tests/S3UrlTest.php @@ -0,0 +1,9 @@ + Date: Wed, 22 Apr 2015 16:17:01 -0400 Subject: [PATCH 33/43] Issue #2471823: Add tests for S3Url. --- tests/S3UrlTest.php | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 72 insertions(+), 1 deletion(-) diff --git a/tests/S3UrlTest.php b/tests/S3UrlTest.php index 1236f41..a625581 100644 --- a/tests/S3UrlTest.php +++ b/tests/S3UrlTest.php @@ -1,9 +1,80 @@ assertEquals('s3://bucket/key', (string) $url); + $this->assertEquals('bucket', $url->getBucket()); + $this->assertEquals('key', $url->getKey()); + } + + /** + * @covers Drupal\amazons3\S3Url::setBucket + * @covers Drupal\amazons3\S3Url::getBucket + */ + public function testGetBucket() { + $url = new S3Url('bucket'); + $url->setBucket('second-bucket'); + $this->assertEquals('second-bucket', $url->getBucket()); + } + + /** + * @covers Drupal\amazons3\S3Url::setKey + * @covers Drupal\amazons3\S3Url::getKey + */ + public function testGetKey() { + $url = new S3Url('bucket'); + $url->setKey('key'); + $this->assertEquals('key', $url->getKey()); + } + + /** + * @covers Drupal\amazons3\S3Url::setPath + */ + public function testSetPath() { + $url = new S3Url('bucket'); + $url->setPath(array('directory', 'key')); + $this->assertEquals('/directory/key', $url->getPath()); + } + + /** + * @covers Drupal\amazons3\S3Url::getImageStyleUrl + */ + public function testGetImageStyleUrl() { + $url = new S3Url('bucket', 'key'); + $styleUrl = $url->getImageStyleUrl('style_name'); + $this->assertEquals($styleUrl->getKey(), "styles/style_name/key"); + } + + /** + * @covers Drupal\amazons3\S3Url::factory + */ + public function testFactory() { + $url = S3Url::factory('s3://bucket/key'); + $this->assertInstanceOf('Drupal\amazons3\S3Url', $url); + } + + /** + * @expectedException \InvalidArgumentException + * @covers Drupal\amazons3\S3Url::factory + */ + public function testFactoryInvalidUrl() { + $url = S3Url::factory(':'); + } } -- 2.3.5 From 07bfcb122ca4f7c7cc2e3fa99d3b195f13ba3b59 Mon Sep 17 00:00:00 2001 From: Andrew Berry Date: Wed, 22 Apr 2015 16:17:13 -0400 Subject: [PATCH 34/43] Issue #2471823: fromKey() is now replaced by the constructor. --- amazons3.module | 2 +- src/S3Url.php | 17 ----------------- 2 files changed, 1 insertion(+), 18 deletions(-) diff --git a/amazons3.module b/amazons3.module index e8affd1..6a2d534 100644 --- a/amazons3.module +++ b/amazons3.module @@ -84,7 +84,7 @@ function amazons3_image_deliver() { $path = $args; $key = implode('/', $path); - $source = S3Url::fromKey($bucket, $key); + $source = new S3Url($bucket, $key); $destination = $source->getImageStyleUrl($style_name); // Check that the image style token is valid. diff --git a/src/S3Url.php b/src/S3Url.php index 4217237..d4d240d 100644 --- a/src/S3Url.php +++ b/src/S3Url.php @@ -130,21 +130,4 @@ class S3Url extends Url { return new static($parts['host'], substr($parts['path'], 1)); } - - /** - * Generate a URL from a bucket and key. - * - * @param string $bucket - * The bucket the key is in. - * @param string $key - * The key of the object. - * - * @return \Drupal\amazons3\S3Url - * A S3Url object. - */ - public static function fromKey($bucket, $key) { - $url = new S3Url($bucket); - $url->setPath('/' . $key); - return $url; - } } -- 2.3.5 From 93682e1410a66fa77d80662f711bec4b8b1fba66 Mon Sep 17 00:00:00 2001 From: Andrew Berry Date: Wed, 22 Apr 2015 16:17:28 -0400 Subject: [PATCH 35/43] Issue #2471823: Correct variable case. --- src/S3Url.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/S3Url.php b/src/S3Url.php index d4d240d..0d558c3 100644 --- a/src/S3Url.php +++ b/src/S3Url.php @@ -93,15 +93,15 @@ class S3Url extends Url { /** * Return the image style URL associated with this URL. * - * @param string $style_name + * @param string $styleName * The name of the image style. * * @return \Drupal\amazons3\S3Url * An image style URL. */ - public function getImageStyleUrl($style_name) { + public function getImageStyleUrl($styleName) { $styleUrl = new S3Url($this->getBucket()); - $styleUrl->setPath("/styles/$style_name/" . $this->getKey()); + $styleUrl->setPath("/styles/$styleName/" . $this->getKey()); return $styleUrl; } -- 2.3.5 From 647e04632e99fef42200b8cce7b5a6cfd1336940 Mon Sep 17 00:00:00 2001 From: Andrew Berry Date: Wed, 22 Apr 2015 16:52:47 -0400 Subject: [PATCH 36/43] Issue #2471823: Add S3Client tests. --- src/S3Client.php | 4 --- tests/S3ClientTest.php | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 4 deletions(-) create mode 100644 tests/S3ClientTest.php diff --git a/src/S3Client.php b/src/S3Client.php index 7ea84b2..2b86efc 100644 --- a/src/S3Client.php +++ b/src/S3Client.php @@ -24,8 +24,6 @@ class S3Client { /** * Create a new S3Client using aws_key / aws_secret $conf variables. * - * @todo Needs tests. - * * @param array|Collection $config * An array of configuration options to pass to \Aws\S3\S3Client::factory(). * If 'credentials' are set they will be used instead of aws_key and @@ -48,8 +46,6 @@ class S3Client { * bucket doesn't exist at all, or if it exists but is owned by another S3 * account. * - * @todo Needs tests. - * * @param string $bucket * The name of the bucket to test. * @param \Aws\S3\S3Client $client diff --git a/tests/S3ClientTest.php b/tests/S3ClientTest.php new file mode 100644 index 0000000..3d9688f --- /dev/null +++ b/tests/S3ClientTest.php @@ -0,0 +1,75 @@ +assertInstanceOf('Aws\S3\S3Client', $client); + $this->assertEquals('placeholder', $client->getCredentials()->getAccessKeyId('placeholder')); + } + + /** + * @covers \Drupal\amazons3\S3Client::validateBucketExists + * @expectedException \Drupal\amazons3\Exception\S3ConnectValidationException + */ + public function testValidateBucketExistsFail() { + $client = DrupalS3Client::factory(); + DrupalS3Client::validateBucketExists('bucket', $client); + } + + /** + * @covers \Drupal\amazons3\S3Client::validateBucketExists + */ + public function testValidateBucketExists() { + // Instantiate the AWS service builder. + $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', + ), + ), + ), + ); + $aws = \Aws\Common\Aws::factory($config); + + // Configure the tests to use the instantiated AWS service builder + \Guzzle\Tests\GuzzleTestCase::setServiceBuilder($aws); + $client = $this->getServiceBuilder()->get('s3', true); + + $this->setMockResponse($client, array(new Response(200))); + + $exception = NULL; + try { + DrupalS3Client::validateBucketExists('bucket', $client); + } + catch (\Exception $exception) { + } + $this->assertNull($exception, 'The bucket was validated to exist.'); + } +} -- 2.3.5 From 88c8f69784a6d3c3dcc1206ec0b5fbdd278ecb7f Mon Sep 17 00:00:00 2001 From: Andrew Berry Date: Wed, 22 Apr 2015 17:21:22 -0400 Subject: [PATCH 37/43] Issue #2471823: Fix not respecting incoming defaults. --- src/StreamWrapperConfiguration.php | 9 +++++++-- tests/StreamWrapperConfigurationTest.php | 6 ++++++ 2 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 tests/StreamWrapperConfigurationTest.php diff --git a/src/StreamWrapperConfiguration.php b/src/StreamWrapperConfiguration.php index 39cc46f..b1cb23a 100644 --- a/src/StreamWrapperConfiguration.php +++ b/src/StreamWrapperConfiguration.php @@ -45,8 +45,13 @@ class StreamWrapperConfiguration extends Collection { * The stream wrapper configuration. */ public static function fromConfig(array $config = array(), array $defaults = array(), array $required = array()) { - $defaults = self::defaults(); - $required = self::required(); + if (empty($defaults)) { + $defaults = self::defaults(); + } + + if (empty($required)) { + $required = self::required(); + } $data = $config + $defaults; if ($data['caching']) { diff --git a/tests/StreamWrapperConfigurationTest.php b/tests/StreamWrapperConfigurationTest.php new file mode 100644 index 0000000..2fbbec0 --- /dev/null +++ b/tests/StreamWrapperConfigurationTest.php @@ -0,0 +1,6 @@ + Date: Wed, 22 Apr 2015 17:21:34 -0400 Subject: [PATCH 38/43] Issue #2471823: Default to not caching. --- src/StreamWrapperConfiguration.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/StreamWrapperConfiguration.php b/src/StreamWrapperConfiguration.php index b1cb23a..4571aa1 100644 --- a/src/StreamWrapperConfiguration.php +++ b/src/StreamWrapperConfiguration.php @@ -77,7 +77,7 @@ class StreamWrapperConfiguration extends Collection { 'saveAsPaths' => array(), 'cloudFront' => array(), 'domain' => NULL, - 'caching' => TRUE, + 'caching' => FALSE, 'cacheLifetime' => NULL, 'reducedRedundancyPaths' => array(), ); -- 2.3.5 From bdb847803b56f95563d4b54cab8c71a8b1da08dc Mon Sep 17 00:00:00 2001 From: Andrew Berry Date: Wed, 22 Apr 2015 17:21:46 -0400 Subject: [PATCH 39/43] Issue #2471823: Unset expiration when disabling caching. --- src/StreamWrapperConfiguration.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/StreamWrapperConfiguration.php b/src/StreamWrapperConfiguration.php index 4571aa1..824a12b 100644 --- a/src/StreamWrapperConfiguration.php +++ b/src/StreamWrapperConfiguration.php @@ -241,6 +241,7 @@ class StreamWrapperConfiguration extends Collection { */ public function disableCaching() { $this->data['caching'] = FALSE; + $this->data['expiration'] = NULL; } /** -- 2.3.5 From b2c5c7d839e21dafe73a5187a8ea36bcb42269f4 Mon Sep 17 00:00:00 2001 From: Andrew Berry Date: Wed, 22 Apr 2015 17:21:58 -0400 Subject: [PATCH 40/43] Issue #2471823: Ignore fromDrupalVariables() for now. --- src/StreamWrapperConfiguration.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/StreamWrapperConfiguration.php b/src/StreamWrapperConfiguration.php index 824a12b..bb64f58 100644 --- a/src/StreamWrapperConfiguration.php +++ b/src/StreamWrapperConfiguration.php @@ -286,6 +286,8 @@ class StreamWrapperConfiguration extends Collection { * Set the stream wrapper configuration using Drupal variables. * * @return StreamWrapperConfiguration + * + * @codeCoverageIgnore */ public static function fromDrupalVariables() { $config = new static(); -- 2.3.5 From 35bf53d58be770f053309da08bff1b9ba69f7ee3 Mon Sep 17 00:00:00 2001 From: Andrew Berry Date: Wed, 22 Apr 2015 17:23:43 -0400 Subject: [PATCH 41/43] Issue #2471823: Test StreamWrapperConfiguration. --- tests/StreamWrapperConfigurationTest.php | 72 +++++++++++++++++++++++++++++++- 1 file changed, 71 insertions(+), 1 deletion(-) diff --git a/tests/StreamWrapperConfigurationTest.php b/tests/StreamWrapperConfigurationTest.php index 2fbbec0..c67026e 100644 --- a/tests/StreamWrapperConfigurationTest.php +++ b/tests/StreamWrapperConfigurationTest.php @@ -1,6 +1,76 @@ 'bucket'); + $config = StreamWrapperConfiguration::fromConfig($settings); + $this->assertInstanceOf('Drupal\amazons3\StreamWrapperConfiguration', $config); + } + + /** + * @covers Drupal\amazons3\StreamWrapperConfiguration::fromConfig + * @expectedException \InvalidArgumentException + */ + public function testFromConfigMissingExpiration() { + $settings = array('bucket' => 'bucket', 'caching' => TRUE); + $config = StreamWrapperConfiguration::fromConfig($settings); + } + + public function testSetters() { + $config = StreamWrapperConfiguration::fromConfig(array('bucket' => 'bucket')); + + $config->setBucket('different-bucket'); + $this->assertEquals('different-bucket', $config->getBucket()); + + $config->enableCaching(); + $this->assertTrue($config->isCaching()); + + $config->setCacheLifetime(666); + $this->assertEquals(666, $config->getCacheLifetime()); + + $config->disableCaching(); + $this->assertFalse($config->isCaching()); + + $config->setDomain('api.example.com'); + $this->assertEquals('api.example.com', $config->getDomain()); + + $config->setHostname('cdn.example.com'); + $this->assertEquals('cdn.example.com', $config->getHostname()); + + $config->serveWithCloudFront(); + $this->assertTrue($config->isCloudFront()); + + $config->serveWithS3(); + $this->assertFalse($config->isCloudFront()); + + $config->setPresignedPaths(array('/')); + $this->assertEquals(array('/'), $config->getPresignedPaths()); + + $config->setReducedRedundancyPaths(array('/')); + $this->assertEquals(array('/'), $config->getReducedRedundancyPaths()); + + $config->setSaveAsPaths(array('/')); + $this->assertEquals(array('/'), $config->getSaveAsPaths()); + + $config->setTorrentPaths(array('/')); + $this->assertEquals(array('/'), $config->getTorrentPaths()); + } + + /** + * @expectedException \LogicException + */ + public function testCacheLifetimeException() { + $config = StreamWrapperConfiguration::fromConfig(array('bucket' => 'bucket')); + $config->setCacheLifetime(0); + } } -- 2.3.5 From c5efa2ad9e470a059c833aebab2eaa7b0caae23b Mon Sep 17 00:00:00 2001 From: Andrew Berry Date: Wed, 22 Apr 2015 17:23:56 -0400 Subject: [PATCH 42/43] Issue #2471823: Assert identical objects. --- tests/StreamWrapperTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/StreamWrapperTest.php b/tests/StreamWrapperTest.php index f37567c..78c8fc5 100644 --- a/tests/StreamWrapperTest.php +++ b/tests/StreamWrapperTest.php @@ -59,7 +59,7 @@ class StreamWrapperTest extends \PHPUnit_Framework_TestCase { 'caching' => FALSE, ]); StreamWrapper::setDefaultConfig($config); - $this->assertEquals($config, StreamWrapper::getDefaultConfig()); + $this->assertSame($config, StreamWrapper::getDefaultConfig()); if ($oldConfig) { StreamWrapper::setDefaultConfig($oldConfig); @@ -132,7 +132,7 @@ class StreamWrapperTest extends \PHPUnit_Framework_TestCase { StreamWrapper::setClient($client); - $this->assertEquals($client, StreamWrapper::getClient()); + $this->assertSame($client, StreamWrapper::getClient()); } /** -- 2.3.5 From 56fab6c1f0f7737192dad6f436d3e23a4c606e6d Mon Sep 17 00:00:00 2001 From: Andrew Berry Date: Thu, 23 Apr 2015 07:23:57 -0400 Subject: [PATCH 43/43] Issue #2471823: Mark testing getLocalPath(). --- src/StreamWrapper.php | 8 -------- tests/StreamWrapperTest.php | 1 + 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/src/StreamWrapper.php b/src/StreamWrapper.php index 353314d..ea9a865 100644 --- a/src/StreamWrapper.php +++ b/src/StreamWrapper.php @@ -341,16 +341,8 @@ class StreamWrapper extends \Aws\S3\StreamWrapper implements \DrupalStreamWrappe * * @return string * The local path. - * - * @codeCoverageIgnore - * This method is tested via getExternalUrl(), but phpunit's covers - * annotation won't detect this method. */ protected function getLocalPath() { - if (!isset($this->uri)) { - throw new \LogicException('A URI must be set before calling getLocalPath().'); - } - $path = $this->uri->getPath(); $path = trim($path, '/'); return $path; diff --git a/tests/StreamWrapperTest.php b/tests/StreamWrapperTest.php index 78c8fc5..16fdf30 100644 --- a/tests/StreamWrapperTest.php +++ b/tests/StreamWrapperTest.php @@ -185,6 +185,7 @@ class StreamWrapperTest extends \PHPUnit_Framework_TestCase { * Test regular URL generation. * * @covers \Drupal\amazons3\StreamWrapper::getExternalUrl + * @covers \Drupal\amazons3\StreamWrapper::getLocalPath */ public function testExternalUri() { $wrapper = new StreamWrapper(); -- 2.3.5