You are here

class CdnSettingsTest in CDN 8.3

@coversDefaultClass \Drupal\cdn\CdnSettings @group cdn

Hierarchy

Expanded class hierarchy of CdnSettingsTest

File

tests/src/Unit/CdnSettingsTest.php, line 13

Namespace

Drupal\Tests\cdn\Unit
View 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

Namesort descending Modifiers Type Description Overrides
CdnSettingsTest::createCdnSettings protected function Creates a CdnSettings object from raw config.
CdnSettingsTest::settingsProvider public function
CdnSettingsTest::test public function @covers ::getLookupTable @covers ::getDomains @dataProvider settingsProvider
CdnSettingsTest::testAutobalancedWithoutConditions public function @covers ::getLookupTable
CdnSettingsTest::testComplexDomainWithNegatedConditions public function @covers ::getLookupTable
CdnSettingsTest::testComplexMappingWithoutConditions public function @covers ::getLookupTable
CdnSettingsTest::testSimpleMappingWithConditionsAndNegatedConditions public function @covers ::getLookupTable
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUp protected function 340