class CdnSettingsTest in CDN 8.3
@coversDefaultClass \Drupal\cdn\CdnSettings @group cdn
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\cdn\Unit\CdnSettingsTest
Expanded class hierarchy of CdnSettingsTest
File
- tests/
src/ Unit/ CdnSettingsTest.php, line 13
Namespace
Drupal\Tests\cdn\UnitView source
class CdnSettingsTest extends UnitTestCase {
/**
* @covers ::getLookupTable
* @covers ::getDomains
* @dataProvider settingsProvider
*/
public function test(array $raw_config, array $expected_lookup_table, array $expected_domains) {
$cdn_settings = $this
->createCdnSettings($raw_config);
$this
->assertTrue($cdn_settings
->isEnabled());
$this
->assertSame($expected_lookup_table, $cdn_settings
->getLookupTable());
$this
->assertSame(array_values($expected_domains), array_values($cdn_settings
->getDomains()));
$this
->assertSame($raw_config['scheme'], $cdn_settings
->getScheme());
}
public function settingsProvider() {
return [
'simple, on, no conditions, scheme-relative' => [
[
'status' => TRUE,
'mapping' => [
'type' => 'simple',
'domain' => 'cdn.example.com',
'conditions' => [],
],
'scheme' => '//',
],
[
'*' => 'cdn.example.com',
],
[
'cdn.example.com',
],
],
'simple, on, no conditions, HTTPS' => [
[
'status' => TRUE,
'mapping' => [
'type' => 'simple',
'domain' => 'cdn.example.com',
'conditions' => [],
],
'scheme' => 'https://',
],
[
'*' => 'cdn.example.com',
],
[
'cdn.example.com',
],
],
'simple, on, no conditions, HTTP' => [
[
'status' => TRUE,
'mapping' => [
'type' => 'simple',
'domain' => 'cdn.example.com',
'conditions' => [],
],
'scheme' => 'http://',
],
[
'*' => 'cdn.example.com',
],
[
'cdn.example.com',
],
],
'simple, on, no conditions, IPv4 address + port, scheme-relative' => [
[
'status' => TRUE,
'mapping' => [
'type' => 'simple',
'domain' => '127.0.0.1:8080',
'conditions' => [],
],
'scheme' => '//',
],
[
'*' => '127.0.0.1:8080',
],
[
'127.0.0.1:8080',
],
],
'simple, on, no conditions, IPv6 address + port, scheme-relative' => [
[
'status' => TRUE,
'mapping' => [
'type' => 'simple',
'domain' => '[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80',
'conditions' => [],
],
'scheme' => '//',
],
[
'*' => '[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80',
],
[
'[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80',
],
],
'simple, on, one empty condition, scheme-relative' => [
[
'status' => TRUE,
'mapping' => [
'type' => 'simple',
'domain' => 'cdn.example.com',
'conditions' => [
'extensions' => [],
],
],
'scheme' => '//',
],
[
'*' => 'cdn.example.com',
],
[
'cdn.example.com',
],
],
'simple, on, one condition, scheme-relative' => [
[
'status' => TRUE,
'mapping' => [
'type' => 'simple',
'domain' => 'cdn.example.com',
'conditions' => [
'extensions' => [
'jpg',
'jpeg',
'png',
],
],
],
'scheme' => '//',
],
[
'jpg' => 'cdn.example.com',
'jpeg' => 'cdn.example.com',
'png' => 'cdn.example.com',
],
[
'cdn.example.com',
],
],
'simple, on, one negative condition, scheme-relative' => [
[
'status' => TRUE,
'mapping' => [
'type' => 'simple',
'domain' => 'cdn.example.com',
'conditions' => [
'not' => [
'extensions' => [
'css',
'js',
],
],
],
],
'scheme' => '//',
],
[
'*' => 'cdn.example.com',
'css' => FALSE,
'js' => FALSE,
],
[
'cdn.example.com',
],
],
'auto-balanced, on, scheme-relative' => [
[
'status' => TRUE,
'mapping' => [
'type' => 'auto-balanced',
'domains' => [
'img1.example.com',
'img2.example.com',
],
'conditions' => [
'extensions' => [
'jpg',
'png',
],
],
],
'scheme' => '//',
],
[
'jpg' => [
'img1.example.com',
'img2.example.com',
],
'png' => [
'img1.example.com',
'img2.example.com',
],
],
[
'img1.example.com',
'img2.example.com',
],
],
'complex containing two simple mappings, with fallback, scheme-relative' => [
[
'status' => TRUE,
'mapping' => [
'type' => 'complex',
'fallback_domain' => '[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:42',
'domains' => [
0 => [
'type' => 'simple',
'domain' => 'static.example.com',
'conditions' => [
'extensions' => [
'css',
'jpg',
'jpeg',
'png',
],
],
],
1 => [
'type' => 'simple',
'domain' => 'downloads.example.com',
'conditions' => [
'extensions' => [
'zip',
],
],
],
],
],
'scheme' => '//',
],
[
'*' => '[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:42',
'css' => 'static.example.com',
'jpg' => 'static.example.com',
'jpeg' => 'static.example.com',
'png' => 'static.example.com',
'zip' => 'downloads.example.com',
],
[
'[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:42',
'static.example.com',
'downloads.example.com',
],
],
'complex containing two simple mappings, without fallback, scheme-relative' => [
[
'status' => TRUE,
'mapping' => [
'type' => 'complex',
'fallback_domain' => NULL,
'domains' => [
0 => [
'type' => 'simple',
'domain' => 'static.example.com',
'conditions' => [
'extensions' => [
'css',
'jpg',
'jpeg',
'png',
],
],
],
1 => [
'type' => 'simple',
'domain' => 'downloads.example.com',
'conditions' => [
'extensions' => [
'zip',
],
],
],
],
],
'scheme' => '//',
],
[
'css' => 'static.example.com',
'jpg' => 'static.example.com',
'jpeg' => 'static.example.com',
'png' => 'static.example.com',
'zip' => 'downloads.example.com',
],
[
'static.example.com',
'downloads.example.com',
],
],
'complex containing one simple and one auto-balanced mapping, without fallback, scheme-relative' => [
[
'status' => TRUE,
'mapping' => [
'type' => 'complex',
'fallback_domain' => NULL,
'domains' => [
0 => [
'type' => 'simple',
'domain' => 'static.example.com',
'conditions' => [
'extensions' => [
'css',
'js',
],
],
],
1 => [
'type' => 'auto-balanced',
'domains' => [
'img1.example.com',
'img2.example.com',
],
'conditions' => [
'extensions' => [
'jpg',
'jpeg',
'png',
],
],
],
],
],
'scheme' => '//',
],
[
'css' => 'static.example.com',
'js' => 'static.example.com',
'jpg' => [
'img1.example.com',
'img2.example.com',
],
'jpeg' => [
'img1.example.com',
'img2.example.com',
],
'png' => [
'img1.example.com',
'img2.example.com',
],
],
[
'static.example.com',
'img1.example.com',
'img2.example.com',
],
],
];
}
/**
* @covers ::getLookupTable
*/
public function testSimpleMappingWithConditionsAndNegatedConditions() {
$this
->expectException(\AssertionError::class);
$this
->expectExceptionMessage("It does not make sense to provide an 'extensions' condition as well as a negated 'extensions' condition.");
$this
->createCdnSettings([
'status' => TRUE,
'mapping' => [
'type' => 'simple',
'domain' => 'cdn.example.com',
'conditions' => [
'extensions' => [
'foo',
'bar',
],
'not' => [
'extensions' => [
'baz',
'qux',
],
],
],
],
])
->getLookupTable();
}
/**
* @covers ::getLookupTable
*/
public function testComplexDomainWithNegatedConditions() {
$this
->expectException(\AssertionError::class);
$this
->expectExceptionMessage("The nested mapping 1 includes negated conditions, which is not allowed for complex mappings: the fallback_domain already serves this purpose.");
$this
->createCdnSettings([
'status' => TRUE,
'mapping' => [
'type' => 'complex',
'fallback_domain' => 'cdn.example.com',
'domains' => [
0 => [
'type' => 'simple',
'domain' => 'foo.example.com',
'conditions' => [
'extensions' => [
'png',
],
],
],
1 => [
'type' => 'simple',
'domain' => 'bar.example.com',
'conditions' => [
'not' => [
'extensions' => [
'png',
],
],
],
],
],
],
])
->getLookupTable();
}
/**
* @covers ::getLookupTable
*/
public function testAutobalancedWithoutConditions() {
$this
->expectException(ConfigValueException::class);
$this
->expectExceptionMessage("It does not make sense to apply auto-balancing to all files, regardless of extension.");
$this
->createCdnSettings([
'status' => TRUE,
'mapping' => [
'type' => 'auto-balanced',
'fallback_domain' => NULL,
'domains' => [
'foo.example.com',
'bar.example.com',
],
],
])
->getLookupTable();
}
/**
* @covers ::getLookupTable
*/
public function testComplexMappingWithoutConditions() {
$this
->expectException(\AssertionError::class);
$this
->expectExceptionMessage("The nested mapping 0 includes no conditions, which is not allowed for complex mappings.");
$this
->createCdnSettings([
'status' => TRUE,
'mapping' => [
'type' => 'complex',
'fallback_domain' => 'cdn.example.com',
'domains' => [
0 => [
'type' => 'simple',
'domain' => 'foo.example.com',
],
],
],
])
->getLookupTable();
}
/**
* Creates a CdnSettings object from raw config.
*
* @param array $raw_config
* The raw config for the cdn.settings.yml config.
*
* @return \Drupal\cdn\CdnSettings
* The CdnSettings object to test.
*/
protected function createCdnSettings(array $raw_config) {
return new CdnSettings($this
->getConfigFactoryStub([
'cdn.settings' => $raw_config,
]));
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CdnSettingsTest:: |
protected | function | Creates a CdnSettings object from raw config. | |
CdnSettingsTest:: |
public | function | ||
CdnSettingsTest:: |
public | function | @covers ::getLookupTable @covers ::getDomains @dataProvider settingsProvider | |
CdnSettingsTest:: |
public | function | @covers ::getLookupTable | |
CdnSettingsTest:: |
public | function | @covers ::getLookupTable | |
CdnSettingsTest:: |
public | function | @covers ::getLookupTable | |
CdnSettingsTest:: |
public | function | @covers ::getLookupTable | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. | |
UnitTestCase:: |
protected | function | 340 |