You are here

class SettingsHelperTest in Acquia Lift Connector 8.3

Same name and namespace in other branches
  1. 8.4 tests/src/Unit/Service/Helper/SettingsHelperTest.php \Drupal\Tests\acquia_lift\Unit\Service\Helper\SettingsHelperTest

SettingsHelper Test.

@coversDefaultClass Drupal\acquia_lift\Service\Helper\SettingsHelper @group acquia_lift

Hierarchy

Expanded class hierarchy of SettingsHelperTest

File

tests/src/Unit/Service/Helper/SettingsHelperTest.php, line 15

Namespace

Drupal\Tests\acquia_lift\Unit\Service\Helper
View source
class SettingsHelperTest extends UnitTestCase {
  use SettingsDataTrait;
  protected function setUp() {
    parent::setUp();
  }

  /**
   * Tests the isInvalidCredentialAccountId() method.
   *
   * @covers ::isInvalidCredentialAccountId
   *
   * @param string $setting
   * @param boolean $expected
   *
   * @dataProvider providerTestIsInvalidCredentialAccountId
   */
  public function testIsInvalidCredentialAccountId($setting, $expected) {
    $result = SettingsHelper::isInvalidCredentialAccountId($setting);
    $this
      ->assertEquals($expected, $result);
  }

  /**
   * Data provider for testIsInvalidCredentialAccountId().
   */
  public function providerTestIsInvalidCredentialAccountId() {
    $data = [];
    $data['invalid null'] = [
      NULL,
      TRUE,
    ];
    $data['invalid empty'] = [
      '',
      TRUE,
    ];
    $data['invalid start with number'] = [
      '1a',
      TRUE,
    ];
    $data['invalid has "~" sign'] = [
      'a~',
      TRUE,
    ];
    $data['valid has "_" sign'] = [
      'a_',
      FALSE,
    ];
    $data['valid start with alphabetic then alphanumeric'] = [
      'a123',
      FALSE,
    ];
    return $data;
  }

  /**
   * Tests the isInvalidCredentialSiteId() method.
   *
   * @covers ::isInvalidCredentialSiteId
   *
   * @param string $setting
   * @param boolean $expected
   *
   * @dataProvider providerTestIsInvalidCredentialSiteId
   */
  public function testIsInvalidCredentialSiteId($setting, $expected) {
    $result = SettingsHelper::isInvalidCredentialSiteId($setting);
    $this
      ->assertEquals($expected, $result);
  }

  /**
   * Data provider for testIsInvalidCredentialSiteId().
   */
  public function providerTestIsInvalidCredentialSiteId() {
    $data = [];
    $data['invalid null'] = [
      NULL,
      TRUE,
    ];
    $data['invalid empty'] = [
      '',
      TRUE,
    ];
    $data['valid has "~" sign'] = [
      'a~',
      FALSE,
    ];
    $data['valid alphanumeric 1'] = [
      'a123',
      FALSE,
    ];
    $data['valid alphanumeric 2'] = [
      '3Ab',
      FALSE,
    ];
    return $data;
  }

  /**
   * Tests the isInvalidCredentialAssetsUrl() method.
   *
   * @covers ::isInvalidCredentialAssetsUrl
   *
   * @param string $setting
   * @param boolean $expected
   *
   * @dataProvider providerTestIsInvalidCredentialAssetsUrl
   */
  public function testIsInvalidCredentialAssetsUrl($setting, $expected) {
    $result = SettingsHelper::isInvalidCredentialAssetsUrl($setting);
    $this
      ->assertEquals($expected, $result);
  }

  /**
   * Data provider for testIsInvalidCredentialAssetsUrl().
   */
  public function providerTestIsInvalidCredentialAssetsUrl() {
    $data = [];
    $data['invalid null'] = [
      NULL,
      TRUE,
    ];
    $data['invalid empty'] = [
      '',
      TRUE,
    ];
    $data['invalid has non-ascii characters'] = [
      '不合法',
      TRUE,
    ];
    $data['valid url 1'] = [
      'acquia',
      FALSE,
    ];
    $data['valid url 2'] = [
      'acquia.com',
      FALSE,
    ];
    return $data;
  }

  /**
   * Tests the isInvalidCredentialDecisionApiUrl() method.
   *
   * @covers ::isInvalidCredentialDecisionApiUrl
   *
   * @param string $setting
   * @param boolean $expected
   *
   * @dataProvider providerTestIsInvalidCredentialDecisionApiUrl
   */
  public function testIsInvalidCredentialDecisionApiUrl($setting, $expected) {
    $result = SettingsHelper::isInvalidCredentialDecisionApiUrl($setting);
    $this
      ->assertEquals($expected, $result);
  }

  /**
   * Data provider for testIsInvalidCredentialDecisionApiUrl().
   */
  public function providerTestIsInvalidCredentialDecisionApiUrl() {
    $data = [];
    $data['invalid has non-ascii characters'] = [
      '不合法',
      TRUE,
    ];
    $data['valid null'] = [
      NULL,
      FALSE,
    ];
    $data['valid empty'] = [
      '',
      FALSE,
    ];
    $data['valid url 1'] = [
      'acquia',
      FALSE,
    ];
    $data['valid url 2'] = [
      'acquia.com',
      FALSE,
    ];
    return $data;
  }

  /**
   * Tests the isInvalidCredentialOauthUrl() method.
   *
   * @covers ::isInvalidCredentialOauthUrl
   *
   * @param string $setting
   * @param boolean $expected
   *
   * @dataProvider providerTestIsInvalidCredentialOauthUrl
   */
  public function testIsInvalidCredentialOauthUrl($setting, $expected) {
    $result = SettingsHelper::isInvalidCredentialOauthUrl($setting);
    $this
      ->assertEquals($expected, $result);
  }

  /**
   * Data provider for testIsInvalidCredentialOauthUrl().
   */
  public function providerTestIsInvalidCredentialOauthUrl() {
    $data = [];
    $data['invalid has non-ascii characters'] = [
      '不合法',
      TRUE,
    ];
    $data['valid null'] = [
      NULL,
      FALSE,
    ];
    $data['valid empty'] = [
      '',
      FALSE,
    ];
    $data['valid url 1'] = [
      'acquia',
      FALSE,
    ];
    $data['valid url 2'] = [
      'acquia.com',
      FALSE,
    ];
    return $data;
  }

  /**
   * Tests the isInvalidCredential() method.
   *
   * @covers ::isInvalidCredential
   *
   * @param array $full_settings
   * @param boolean $expected
   *
   * @dataProvider providerTestIsInvalidCredential
   */
  public function testIsInvalidCredential($full_settings, $expected) {
    $result = SettingsHelper::isInvalidCredential($full_settings);
    $this
      ->assertEquals($expected, $result);
  }

  /**
   * Data provider for testIsInvalidCredential().
   */
  public function providerTestIsInvalidCredential() {
    $data = [];
    $valid_settings = $this
      ->getValidCredentialSettings();
    $data['valid data 1'] = [
      $valid_settings,
      FALSE,
    ];
    $data['valid data 2'] = [
      $valid_settings,
      FALSE,
    ];
    $data['missing account_id'] = [
      $valid_settings,
      TRUE,
    ];
    $data['invalid assets_url URL'] = [
      $valid_settings,
      TRUE,
    ];
    $data['invalid decision_api_url URL'] = [
      $valid_settings,
      TRUE,
    ];
    $data['valid data 2'][0]['account_id'] = 'accountId2';
    $data['valid data 2'][0]['site_id'] = '2222';
    $data['valid data 2'][0]['assets_url'] = 'assets_url_2';
    $data['missing account_id'][0]['account_id'] = '';
    $data['invalid assets_url URL'][0]['assets_url'] = 'invalid assets URL';
    $data['invalid decision_api_url URL'][0]['decision_api_url'] = '\\\\////\\\\////';
    return $data;
  }

  /**
   * Tests the isValidContentReplacementMode() method.
   *
   * @covers ::isValidContentReplacementMode
   *
   * @param string $test_value
   * @param boolean $expected
   *
   * @dataProvider providerTestIsValidContentReplacementMode
   */
  public function testIsValidContentReplacementMode($test_value, $expected) {
    $result = SettingsHelper::isValidContentReplacementMode($test_value);
    $this
      ->assertEquals($expected, $result);
  }

  /**
   * Data provider for testIsValidContentReplacementMode().
   */
  public function providerTestIsValidContentReplacementMode() {
    $data = [];
    $data['valid trusted'] = [
      'trusted',
      TRUE,
    ];
    $data['valid untrusted'] = [
      'untrusted',
      TRUE,
    ];
    $data['valid customized'] = [
      'customized',
      TRUE,
    ];
    $data['invalid null'] = [
      NULL,
      FALSE,
    ];
    $data['invalid value'] = [
      'another',
      FALSE,
    ];
    return $data;
  }

  /**
   * Tests the getUdfLimitsForType() method.
   *
   * @covers ::getUdfLimitsForType
   *
   * @param string $test_value
   * @param boolean $expected
   *
   * @dataProvider providerTestGetUdfLimitsForType
   */
  public function testGetUdfLimitsForType($test_value, $expected) {
    $result = SettingsHelper::getUdfLimitsForType($test_value);
    $this
      ->assertEquals($expected, $result);
  }

  /**
   * Data provider for testGetUdfLimitsForType().
   */
  public function providerTestGetUdfLimitsForType() {
    $data = [];
    $data['person'] = [
      'person',
      50,
    ];
    $data['touch'] = [
      'touch',
      20,
    ];
    $data['event'] = [
      'event',
      50,
    ];
    return $data;
  }

  /**
   * Tests the getUdfLimitsForType() method, expected exception.
   *
   * @covers ::getUdfLimitsForType
   *
   * @expectedException \Exception
   * @expectedExceptionCode 0
   * @expectedExceptionMessage This UDF Field type is not supported.
   */
  public function testGetUdfLimitsForTypeExpectedException() {
    SettingsHelper::getUdfLimitsForType('non_exist');
  }

  /**
   * Tests the pingUri() method.
   *
   * @covers ::pingUri
   *
   * @param string $test_value
   * @param boolean $expected
   *
   * @dataProvider providerTestPingUri
   */
  public function testPingUri($test_value, $expected) {
    $response = $this
      ->prophesize(\Psr\Http\Message\ResponseInterface::class);
    $response
      ->getStatusCode()
      ->willReturn($expected['statusCode']);
    $response
      ->getReasonPhrase()
      ->willReturn($expected['reasonPhrase']);
    $client = $this
      ->prophesize(\GuzzleHttp\Client::class);
    $client
      ->get($test_value[1], [
      'http_errors' => false,
    ])
      ->willReturn($response
      ->reveal());
    $clientFactory = $this
      ->prophesize(\Drupal\Core\Http\ClientFactory::class);
    $clientFactory
      ->fromOptions(\Prophecy\Argument::any())
      ->willReturn($client
      ->reveal());
    $container = $this
      ->prophesize(\Drupal\Core\DependencyInjection\ContainerBuilder::class);
    $container
      ->get('http_client_factory')
      ->willReturn($clientFactory
      ->reveal());
    \Drupal::setContainer($container
      ->reveal());
    $result = SettingsHelper::pingUri($test_value[0], $test_value[1]);
    $this
      ->assertEquals($expected, $result);
  }

  /**
   * Data provider for testPingUri().
   */
  public function providerTestPingUri() {
    $data = [];
    $data['invalid uri'] = [
      [
        'uri_1',
        '',
      ],
      [
        'statusCode' => '',
        'reasonPhrase' => '',
      ],
    ];
    $data['valid uri 1'] = [
      [
        'uri_1',
        'path_1',
      ],
      [
        'statusCode' => 123,
        'reasonPhrase' => 'a:1:{s:8:"base_uri";s:5:"uri_1";} path_1 a:1:{s:11:"http_errors";b:0;}',
      ],
    ];
    $data['valid uri 2'] = [
      [
        'uri_2',
        'path_2',
      ],
      [
        'statusCode' => 123,
        'reasonPhrase' => 'a:1:{s:8:"base_uri";s:5:"uri_2";} path_2 a:1:{s:11:"http_errors";b:0;}',
      ],
    ];
    return $data;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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.
SettingsDataTrait::getValidAdvancedSettings private function Get a valid advanced settings array.
SettingsDataTrait::getValidCredentialSettings private function Get a valid credential settings array.
SettingsDataTrait::getValidFieldMappingsSettings private function Get a valid field mappings settings array.
SettingsDataTrait::getValidIdentitySettings private function Get a valid identity settings array.
SettingsDataTrait::getValidUdfEventMappingsSettings private function Get a valid udf event mappings settings array.
SettingsDataTrait::getValidUdfPersonMappingsSettings private function Get a valid udf person mappings settings array.
SettingsDataTrait::getValidUdfTouchMappingsSettings private function Get a valid udf touch mappings settings array.
SettingsDataTrait::getValidVisibilitySettings private function Get a valid visibility settings array.
SettingsHelperTest::providerTestGetUdfLimitsForType public function Data provider for testGetUdfLimitsForType().
SettingsHelperTest::providerTestIsInvalidCredential public function Data provider for testIsInvalidCredential().
SettingsHelperTest::providerTestIsInvalidCredentialAccountId public function Data provider for testIsInvalidCredentialAccountId().
SettingsHelperTest::providerTestIsInvalidCredentialAssetsUrl public function Data provider for testIsInvalidCredentialAssetsUrl().
SettingsHelperTest::providerTestIsInvalidCredentialDecisionApiUrl public function Data provider for testIsInvalidCredentialDecisionApiUrl().
SettingsHelperTest::providerTestIsInvalidCredentialOauthUrl public function Data provider for testIsInvalidCredentialOauthUrl().
SettingsHelperTest::providerTestIsInvalidCredentialSiteId public function Data provider for testIsInvalidCredentialSiteId().
SettingsHelperTest::providerTestIsValidContentReplacementMode public function Data provider for testIsValidContentReplacementMode().
SettingsHelperTest::providerTestPingUri public function Data provider for testPingUri().
SettingsHelperTest::setUp protected function Overrides UnitTestCase::setUp
SettingsHelperTest::testGetUdfLimitsForType public function Tests the getUdfLimitsForType() method.
SettingsHelperTest::testGetUdfLimitsForTypeExpectedException public function Tests the getUdfLimitsForType() method, expected exception.
SettingsHelperTest::testIsInvalidCredential public function Tests the isInvalidCredential() method.
SettingsHelperTest::testIsInvalidCredentialAccountId public function Tests the isInvalidCredentialAccountId() method.
SettingsHelperTest::testIsInvalidCredentialAssetsUrl public function Tests the isInvalidCredentialAssetsUrl() method.
SettingsHelperTest::testIsInvalidCredentialDecisionApiUrl public function Tests the isInvalidCredentialDecisionApiUrl() method.
SettingsHelperTest::testIsInvalidCredentialOauthUrl public function Tests the isInvalidCredentialOauthUrl() method.
SettingsHelperTest::testIsInvalidCredentialSiteId public function Tests the isInvalidCredentialSiteId() method.
SettingsHelperTest::testIsValidContentReplacementMode public function Tests the isValidContentReplacementMode() method.
SettingsHelperTest::testPingUri public function Tests the pingUri() method.
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.