summaryrefslogtreecommitdiffstatshomepage
path: root/core/tests/Drupal/Tests/SessionTestTrait.php
blob: d06f6af7a78ccbd91224b8ab68d8e1f5ed6673aa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php

declare(strict_types=1);

namespace Drupal\Tests;

use Symfony\Component\HttpFoundation\Request;

/**
 * Provides methods to generate and get session name in tests.
 */
trait SessionTestTrait {

  /**
   * The name of the session cookie.
   *
   * @var string
   */
  protected $sessionName;

  /**
   * Generates a session cookie name.
   *
   * @param string $data
   *   The data to generate session name.
   */
  protected function generateSessionName($data) {
    $prefix = (Request::createFromGlobals()->isSecure() ? 'SSESS' : 'SESS');
    $this->sessionName = $prefix . substr(hash('sha256', $data), 0, 32);
  }

  /**
   * Returns the session name in use on the child site.
   *
   * @return string
   *   The name of the session cookie.
   */
  protected function getSessionName() {
    return $this->sessionName;
  }

}