View source
<?php
namespace Drupal\Tests\acquia_lift\Unit\Service\Helper;
use Exception;
use Drupal\Tests\UnitTestCase;
use Drupal\acquia_lift\Service\Helper\SettingsHelper;
use Drupal\Tests\acquia_lift\Unit\Traits\SettingsDataTrait;
class SettingsHelperTest extends UnitTestCase {
use SettingsDataTrait;
protected function setUp() {
parent::setUp();
}
public function testIsInvalidCredentialAccountId($setting, $expected) {
$result = SettingsHelper::isInvalidCredentialAccountId($setting);
$this
->assertEquals($expected, $result);
}
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;
}
public function testIsInvalidCredentialSiteId($setting, $expected) {
$result = SettingsHelper::isInvalidCredentialSiteId($setting);
$this
->assertEquals($expected, $result);
}
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;
}
public function testIsInvalidCredentialAssetsUrl($setting, $expected) {
$result = SettingsHelper::isInvalidCredentialAssetsUrl($setting);
$this
->assertEquals($expected, $result);
}
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;
}
public function testIsInvalidCredentialDecisionApiUrl($setting, $expected) {
$result = SettingsHelper::isInvalidCredentialDecisionApiUrl($setting);
$this
->assertEquals($expected, $result);
}
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;
}
public function testIsInvalidCredential($full_settings, $expected) {
$result = SettingsHelper::isInvalidCredential($full_settings);
$this
->assertEquals($expected, $result);
}
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;
}
public function testIsValidContentReplacementMode($test_value, $expected) {
$result = SettingsHelper::isValidContentReplacementMode($test_value);
$this
->assertEquals($expected, $result);
}
public function providerTestIsValidContentReplacementMode() {
$data = [];
$data['valid trusted'] = [
'trusted',
TRUE,
];
$data['valid customized'] = [
'customized',
TRUE,
];
$data['invalid null'] = [
NULL,
FALSE,
];
$data['invalid value'] = [
'another',
FALSE,
];
return $data;
}
public function testGetUdfLimitsForType($test_value, $expected) {
$result = SettingsHelper::getUdfLimitsForType($test_value);
$this
->assertEquals($expected, $result);
}
public function providerTestGetUdfLimitsForType() {
$data = [];
$data['person'] = [
'person',
50,
];
$data['touch'] = [
'touch',
20,
];
$data['event'] = [
'event',
50,
];
return $data;
}
public function testGetUdfLimitsForTypeExpectedException() {
$this
->expectException(Exception::class);
$this
->expectExceptionCode(0);
$this
->expectExceptionMessage('This UDF Field type is not supported.');
SettingsHelper::getUdfLimitsForType('non_exist');
}
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);
}
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;
}
}