View source
<?php
namespace Drupal\Tests\cloudflare\Functional;
use Drupal\cloudflare_form_tester\Mocks\ComposerDependenciesCheckMock;
use Drupal\Core\Url;
use Drupal\cloudflare_form_tester\Mocks\ZoneMock;
use Drupal\Tests\BrowserTestBase;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\Psr7\Response;
class CloudFlareAdminSettingsInvalidFormTest extends BrowserTestBase {
public static $modules = [
'cloudflare',
'ctools',
];
protected $adminUser;
protected $route = 'cloudflare.admin_settings_form';
public function setUp() {
parent::setUp();
$this->adminUser = $this
->drupalCreateUser([
'administer cloudflare',
]);
$this->route = Url::fromRoute('cloudflare.admin_settings_form');
ComposerDependenciesCheckMock::mockComposerDependenciesMet(TRUE);
}
public function testConfigFormDisplay() {
$this
->drupalLogin($this->adminUser);
$this
->drupalGet($this->route);
$this
->assertSession()
->pageTextContains('This will help suppress log warnings regarding requests bypassing CloudFlare', 'Helper Text');
$this
->assertSession()
->fieldExists('apikey');
$this
->assertSession()
->fieldExists('email');
$this
->assertSession()
->fieldExists('client_ip_restore_enabled');
$this
->assertSession()
->fieldExists('bypass_host');
}
public function testFormAccess() {
$this
->drupalLogin($this->adminUser);
$this
->drupalGet($this->route);
$this
->assertSession()
->statusCodeEquals(200);
}
public function testInvalidCredentials() {
$mock = new MockHandler([
new Response(403, [], "This could be a problem."),
]);
$container = \Drupal::getContainer();
$config_factory = $container
->get('config.factory');
$logger_channel_cloudflare = $container
->get('logger.channel.cloudflare');
$cloudflare_state = $container
->get('cloudflare.state');
$composer_dependencies_check = $container
->get('cloudflare.composer_dependency_check');
$zone_mock = new ZoneMock($config_factory, $logger_channel_cloudflare, $cloudflare_state, $composer_dependencies_check);
ZoneMock::mockAssertValidCredentials(FALSE);
$container
->set('cloudflare.zone', $zone_mock);
$this
->drupalLogin($this->adminUser);
$edit = [
'apikey' => '68ow48650j63zfzx1w9jd29cr367u0ezb6a4g',
'email' => 'test@test.com',
];
$this
->drupalPostForm($this->route, $edit, t('Next'));
$this
->assertSession()
->addressEquals('/admin/config/services/cloudflare');
}
public function testUpperCaseInvalidCredentials() {
ZoneMock::mockAssertValidCredentials(TRUE);
ComposerDependenciesCheckMock::mockComposerDependenciesMet(TRUE);
$edit = [
'apikey' => 'fDK5M9sf51x6CEAspHSUYM4vt40m5XC2T6i1K',
'email' => 'test@test.com',
];
$this
->drupalLogin($this->adminUser);
$this
->drupalPostForm($this->route, $edit, t('Next'));
$this
->assertSession()
->pageTextContains('Invalid Api Key: Key can only contain lowercase or numerical characters.');
}
public function testInvalidKeyLength() {
ZoneMock::mockAssertValidCredentials(TRUE);
ComposerDependenciesCheckMock::mockComposerDependenciesMet(TRUE);
$edit = [
'apikey' => '68ow48650j63zfzx1w9jd29cr367u0ezb6a4g0',
'email' => 'test@test.com',
];
$this
->drupalLogin($this->adminUser);
$this
->drupalPostForm($this->route, $edit, t('Next'));
$this
->assertSession()
->pageTextContains('Invalid Api Key: Key should be 37 chars long.');
}
public function testInvalidKeySpecialChars() {
ZoneMock::mockAssertValidCredentials(TRUE);
ComposerDependenciesCheckMock::mockComposerDependenciesMet(FALSE);
$edit = [
'apikey' => '!8ow48650j63zfzx1w9jd29cr367u0ezb6a4g',
'email' => 'test@test.com',
];
$this
->drupalLogin($this->adminUser);
$this
->drupalPostForm($this->route, $edit, t('Next'));
$this
->assertSession()
->pageTextContains('Invalid Api Key: Key can only contain alphanumeric characters.');
}
}