SimpleRecaptchaJavascriptTest.php in Simple Google reCAPTCHA 8
File
tests/src/FunctionalJavascript/SimpleRecaptchaJavascriptTest.php
View source
<?php
namespace Drupal\Tests\simple_recaptcha\FunctionalJavascript;
class SimpleRecaptchaJavascriptTest extends SimpleRecaptchaJavascriptTestBase {
public function testLoginForm() {
$config = $this
->config('simple_recaptcha.config');
$this
->drupalGet('/user/login');
$this
->assertJsCondition('drupalSettings.simple_recaptcha.sitekey === "' . $config
->get('site_key') . '";');
$this->webAssert
->hiddenFieldExists('simple_recaptcha_token');
$this->webAssert
->hiddenFieldNotExists('simple_recaptcha_message');
$this->page
->pressButton('Log in');
$this->webAssert
->waitForElement('css', 'recaptcha-visible');
$this
->assignNameToCaptchaIframe();
$this
->getSession()
->switchToIFrame('recaptcha-iframe');
$this
->assertStringCOntainsString('This reCAPTCHA is for testing purposes only. Please report to the site admin if you are seeing this.', $this->page
->getContent());
$this
->htmlOutput($this->page
->getHtml());
$this
->getSession()
->switchToIFrame();
$user = $this
->drupalCreateUser([]);
$edit = [
'name' => $user
->getAccountName(),
'pass' => $user->passRaw,
];
$this
->submitForm($edit, t('Log in'));
$error_wrapper = $this->page
->find('css', '.recaptcha-error');
$this
->assertTrue($error_wrapper
->isVisible());
$this->webAssert
->addressEquals('/user/login');
$this
->htmlOutput($this->page
->getHtml());
}
public function testFileUploadWidget() {
$this
->config('system.performance')
->set('js.preprocess', TRUE)
->save();
drupal_flush_all_caches();
$this
->drupalGet('/simple_recaptcha_test/form');
$assert = $this
->assertSession();
$assert
->fieldExists('recaptcha_test_name')
->setValue('test name');
$this->page
->pressButton('Form submit');
$this
->assignNameToCaptchaIframe();
$this
->getSession()
->switchToIFrame('recaptcha-iframe');
$this
->click('.recaptcha-checkbox');
$this
->getSession()
->wait('2000');
$this
->getSession()
->switchToIFrame();
$this->page
->pressButton("simple-recaptcha-submit-button");
$this
->htmlOutput();
$assert
->pageTextContains('Clicked on edit-submit');
}
public function testSessionData() {
$this
->drupalGet('/user/password');
$user = $this
->drupalCreateUser([]);
$edit = [
'name' => $user
->getAccountName(),
];
$this->page
->pressButton('Submit');
$this
->assignNameToCaptchaIframe();
$this
->getSession()
->switchToIFrame('recaptcha-iframe');
$this
->click('.recaptcha-checkbox');
$this
->getSession()
->switchToIFrame();
$this
->getSession()
->wait('2000');
$this
->submitForm($edit, t('Submit'));
$this
->assertSession()
->pageTextContains(t('Further instructions have been sent to your email address.'));
$this
->assertEmpty($this
->getSessionCookies()
->toArray());
}
protected function assignNameToCaptchaIframe() {
$javascript = <<<JS
(function(){
var iframes = document.getElementsByTagName('iframe');
for(var i = 0; i < iframes.length; i++){
var f = iframes[i];
f.name = 'recaptcha-iframe';
}
})()
JS;
$this
->getSession()
->evaluateScript($javascript);
}
}