View source
<?php
namespace Drupal\amazons3Test;
use Drupal\amazons3\Matchable\MatchablePaths;
use Drupal\amazons3Test\Stub\StreamWrapperConfiguration;
class StreamWrapperConfigurationTest extends \PHPUnit_Framework_TestCase {
public function testFromConfig() {
$settings = array(
'bucket' => 'bucket',
'region' => 'region',
);
$config = StreamWrapperConfiguration::fromConfig($settings);
$this
->assertInstanceOf('Drupal\\amazons3\\StreamWrapperConfiguration', $config);
}
public function testFromConfigMissingExpiration() {
$settings = array(
'bucket' => 'bucket',
'caching' => TRUE,
);
$config = StreamWrapperConfiguration::fromConfig($settings);
}
public function testMissingBucket() {
StreamWrapperConfiguration::fromConfig();
}
public function testSetters() {
$config = StreamWrapperConfiguration::fromConfig(array(
'bucket' => 'bucket',
'region' => 'region',
));
$config
->setBucket('different-bucket');
$this
->assertEquals('different-bucket', $config
->getBucket());
$config
->setRegion('different-region');
$this
->assertEquals('different-region', $config
->getRegion());
$config
->enableCaching();
$this
->assertTrue($config
->isCaching());
$config
->setCacheLifetime(666);
$this
->assertEquals(666, $config
->getCacheLifetime());
$config
->disableCaching();
$this
->assertFalse($config
->isCaching());
$config
->setDomain('cdn.example.com');
$this
->assertEquals('cdn.example.com', $config
->getDomain());
$this
->assertEquals('https', $config
->getDomainScheme());
$config
->setDomainScheme('http');
$this
->assertEquals('http', $config
->getDomainScheme());
$config
->setHostname('api.example.com');
$this
->assertEquals('api.example.com', $config
->getHostname());
$config
->serveWithCloudFront();
$this
->assertTrue($config
->isCloudFront());
$config
->serveWithS3();
$this
->assertFalse($config
->isCloudFront());
$mp = new MatchablePaths(array(
'/',
));
$config
->setPresignedPaths($mp);
$this
->assertEquals($mp, $config
->getPresignedPaths());
$config
->setReducedRedundancyPaths($mp);
$this
->assertEquals($mp, $config
->getReducedRedundancyPaths());
$config
->setSaveAsPaths($mp);
$this
->assertEquals($mp, $config
->getSaveAsPaths());
$config
->setTorrentPaths($mp);
$this
->assertEquals($mp, $config
->getTorrentPaths());
$config
->serveWithCloudFront();
$config
->setCloudFrontCredentials('/dev/null', 'keypair-id');
$this
->assertInstanceOf('Aws\\CloudFront\\CloudFrontClient', $config
->getCloudFront());
}
public function testCacheLifetimeException() {
$config = StreamWrapperConfiguration::fromConfig(array(
'bucket' => 'bucket',
'region' => 'region',
));
$config
->setCacheLifetime(0);
}
public function testCloudFrontCredentials() {
$config = StreamWrapperConfiguration::fromConfig(array(
'bucket' => 'bucket',
'region' => 'region',
));
$config
->setCloudFrontCredentials('/does-not-exist', 'asdf');
}
public function testCloudFrontNotSetUp() {
$config = StreamWrapperConfiguration::fromConfig(array(
'bucket' => 'bucket',
'region' => 'region',
));
$config
->getCloudFront();
}
public function testDefaultHostname() {
$config = StreamWrapperConfiguration::fromConfig(array(
'bucket' => 'bucket',
'region' => 'region',
));
$this
->assertEquals('bucket.s3.amazonaws.com', $config
->getDomain());
}
public function testFromDrupalVariables() {
StreamWrapperConfiguration::setVariableData([
'amazons3_bucket' => 'default.example.com',
'amazons3_region' => 'region',
'amazons3_hostname' => 'api.example.com',
'amazons3_cname' => TRUE,
'amazons3_domain' => 'static.example.com',
'amazons3_domain_scheme' => 'http',
'amazons3_cloudfront' => TRUE,
'amazons3_cloudfront_private_key' => '/dev/null',
'amazons3_cloudfront_keypair_id' => 'example',
'amazons3_cache' => TRUE,
'amazons3_torrents' => array(
'.*',
),
'amazons3_presigned_urls' => array(
array(
'pattern' => '.*',
'timeout' => '60',
),
),
'amazons3_saveas' => array(
'.*',
),
'amazons3_rrs' => array(
'.*',
),
]);
$config = StreamWrapperConfiguration::fromDrupalVariables();
$this
->assertEquals($config
->getBucket(), 'default.example.com');
$this
->assertEquals($config
->getHostname(), 'api.example.com');
$this
->assertEquals($config
->getDomain(), 'static.example.com');
$this
->assertEquals($config
->getDomainScheme(), 'http');
$this
->assertEquals($config
->isCloudFront(), TRUE);
$this
->assertInstanceOf('Aws\\CloudFront\\CloudFrontClient', $config
->getCloudFront());
$this
->assertEquals($config
->isCaching(), TRUE);
$this
->assertInstanceOf('Drupal\\amazons3\\Matchable\\MatchablePaths', $config
->getTorrentPaths());
$this
->assertInstanceOf('Drupal\\amazons3\\Matchable\\MatchablePaths', $config
->getPresignedPaths());
$this
->assertInstanceOf('Drupal\\amazons3\\Matchable\\MatchablePaths', $config
->getSaveAsPaths());
$this
->assertInstanceOf('Drupal\\amazons3\\Matchable\\MatchablePaths', $config
->getReducedRedundancyPaths());
StreamWrapperConfiguration::setVariableData([
'amazons3_bucket' => 'default.example.com',
'amazons3_region' => 'region',
'amazons3_cname' => TRUE,
'amazons3_cache' => FALSE,
]);
$config = StreamWrapperConfiguration::fromDrupalVariables();
$this
->assertEquals($config
->getBucket(), $config
->getDomain());
$this
->assertFalse($config
->isCaching());
StreamWrapperConfiguration::setVariableData([
'amazons3_bucket' => 'default.example.com',
'amazons3_region' => 'region',
]);
$config = StreamWrapperConfiguration::fromDrupalVariables();
$this
->assertEquals('s3.amazonaws.com', $config
->getDomain());
StreamWrapperConfiguration::setVariableData([
'amazons3_bucket' => 'bucket',
'amazons3_region' => 'region',
]);
$config = StreamWrapperConfiguration::fromDrupalVariables();
$this
->assertEquals('bucket.s3.amazonaws.com', $config
->getDomain());
StreamWrapperConfiguration::setVariableData(array());
}
public function testEmptyRequiredStringFails() {
StreamWrapperConfiguration::fromConfig([
'bucket' => '',
]);
}
}