SimpleRecaptchaWebformJavascriptTest.php in Simple Google reCAPTCHA 8        
                          
                  
                        
  
  
  
  
  
File
  modules/simple_recaptcha_webform/tests/src/FunctionalJavascript/SimpleRecaptchaWebformJavascriptTest.php
  
    View source  
  <?php
namespace Drupal\Tests\simple_recaptcha_webform\FunctionalJavascript;
class SimpleRecaptchaWebformJavascriptTest extends SimpleRecaptchaWebformJavascriptTestBase {
  
  public function testContactWebform() {
    $this
      ->configureModule();
    $this
      ->drupalGet('webform/simple_recaptcha_v2');
    $assert = $this
      ->assertSession();
    $assert
      ->hiddenFieldExists('simple_recaptcha_type');
    $assert
      ->hiddenFieldValueEquals('simple_recaptcha_type', 'v2');
    
    $assert
      ->fieldExists('name')
      ->setValue('test name');
    $assert
      ->fieldExists('email')
      ->setValue('test@example.com');
    $assert
      ->fieldExists('subject')
      ->setValue('subject');
    $assert
      ->fieldExists('message')
      ->setValue('message');
    $this
      ->getSession()
      ->getPage()
      ->pressButton('Submit');
    $this
      ->assignNameToCaptchaIframe();
    $this
      ->getSession()
      ->switchToIFrame('recaptcha-iframe');
    $this
      ->click('.recaptcha-checkbox');
    
    $this
      ->getSession()
      ->wait('2000');
    $this
      ->getSession()
      ->switchToIFrame();
    $this
      ->getSession()
      ->getPage()
      ->pressButton('Submit');
    $assert
      ->pageTextContains('Your message has been sent.');
    $this
      ->htmlOutput();
  }
  
  public function testRecaptchaLibraries() {
    
    $this
      ->configureModule('v3');
    $this
      ->drupalGet('user/password');
    
    $this
      ->assertSession()
      ->elementExists('css', '.grecaptcha-badge');
    $this
      ->drupalGet('webform/simple_recaptcha_v2');
    
    $this
      ->assertSession()
      ->elementNotExists('css', '.grecaptcha-badge');
    
    $this
      ->configureModule();
    $this
      ->drupalGet('user/password');
    
    $this
      ->assertSession()
      ->elementNotExists('css', '.grecaptcha-badge');
    $this
      ->drupalGet('webform/simple_recaptcha_v2');
    
    $this
      ->assertSession()
      ->elementNotExists('css', '.grecaptcha-badge');
  }
  
  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);
  }
}