You are here

public function CaptchaCronTest::addCaptchaSession in CAPTCHA 8

Add test CAPTCHA session data.

Parameters

string $form_id: Form id.

int $request_time: Timestamp.

Return value

int CAPTCHA session id.

1 call to CaptchaCronTest::addCaptchaSession()
CaptchaCronTest::setUp in tests/src/Functional/CaptchaCronTest.php

File

tests/src/Functional/CaptchaCronTest.php, line 63

Class

CaptchaCronTest
Tests CAPTCHA cron.

Namespace

Drupal\Tests\captcha\Functional

Code

public function addCaptchaSession($form_id, $request_time) {

  // Initialize solution with random data.
  $solution = hash('sha256', mt_rand());

  // Insert an entry and thankfully receive the value
  // of the autoincrement field 'csid'.
  $connection = Database::getConnection();
  $captcha_sid = $connection
    ->insert('captcha_sessions')
    ->fields([
    'uid' => 0,
    'sid' => session_id(),
    'ip_address' => \Drupal::request()
      ->getClientIp(),
    'timestamp' => $request_time,
    'form_id' => $form_id,
    'solution' => $solution,
    'status' => 1,
    'attempts' => 0,
  ])
    ->execute();
  return $captcha_sid;
}