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;
class CloudFlareAdminSettingsFormTest extends BrowserTestBase {
public static $modules = [
'cloudflare',
'cloudflare_form_tester',
'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');
$this
->drupalLogin($this->adminUser);
ZoneMock::mockAssertValidCredentials(TRUE);
ComposerDependenciesCheckMock::mockComposerDependenciesMet(TRUE);
}
public function testValidCredentials() {
$edit = [
'apikey' => '68ow48650j63zfzx1w9jd29cr367u0ezb6a4g',
'email' => 'test@test.com',
];
ComposerDependenciesCheckMock::mockComposerDependenciesMet(TRUE);
$this
->drupalPostForm($this->route, $edit, t('Next'));
$this
->assertSession()
->addressEquals('/admin/config/services/cloudflare/two?js=nojs');
$this
->drupalPostForm(NULL, [], t('Finish'));
$this
->assertSession()
->responseContains('68ow48650j63zfzx1w9jd29cr367u0ezb6a4g');
$this
->assertSession()
->responseContains('test@test.com');
$this
->assertSession()
->responseContains('testdomain.com');
}
public function testMultiZoneSelection() {
ZoneMock::mockAssertValidCredentials(TRUE);
$edit = [
'apikey' => '68ow48650j63zfzx1w9jd29cr367u0ezb6a4g',
'email' => 'test@test.com',
];
ComposerDependenciesCheckMock::mockComposerDependenciesMet(TRUE);
ZoneMock::mockMultiZoneAccount(TRUE);
$this
->drupalPostForm($this->route, $edit, t('Next'));
$this
->assertSession()
->addressEquals('/admin/config/services/cloudflare/two?js=nojs');
$this
->drupalPostForm(NULL, [
'zone_selection' => "123456789999",
], t('Finish'));
$this
->assertSession()
->responseContains('68ow48650j63zfzx1w9jd29cr367u0ezb6a4g');
$this
->assertSession()
->responseContains('testdomain2.com');
}
public function testInvalidBypassHostWithHttps() {
$edit = [
'apikey' => '68ow48650j63zfzx1w9jd29cr367u0ezb6a4g',
'email' => 'test@test.com',
'client_ip_restore_enabled' => TRUE,
'bypass_host' => 'https://blah.com',
];
ZoneMock::mockAssertValidCredentials(TRUE);
$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);
$container
->set('cloudflare.zone', $zone_mock);
$this
->drupalPostForm($this->route, $edit, t('Next'));
$this
->assertSession()
->pageTextContains('Please enter a host without http/https');
}
public function testInvalidBypassHostWithHttp() {
$edit = [
'apikey' => '68ow48650j63zfzx1w9jd29cr367u0ezb6a4g',
'email' => 'test@test.com',
'client_ip_restore_enabled' => TRUE,
'bypass_host' => 'http://blah.com',
];
ZoneMock::mockAssertValidCredentials(TRUE);
$this
->drupalPostForm($this->route, $edit, t('Next'));
$this
->assertSession()
->pageTextContains('Please enter a host without http/https');
}
public function testInvalidBypassHost() {
$edit = [
'apikey' => '68ow48650j63zfzx1w9jd29cr367u0ezb6a4g',
'email' => 'test@test.com',
'client_ip_restore_enabled' => TRUE,
'bypass_host' => 'blah!@#!@',
];
$this
->drupalPostForm($this->route, $edit, t('Next'));
$this
->assertSession()
->pageTextContains('You have entered an invalid host.');
}
}