summaryrefslogtreecommitdiffstatshomepage
path: root/core/tests/Drupal/Tests/Core/PrivateKeyTest.php
blob: c78304372c6b5446170c71553ed39162daa34918 (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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php

declare(strict_types=1);

namespace Drupal\Tests\Core;

use Drupal\Core\PrivateKey;
use Drupal\Tests\UnitTestCase;
use Drupal\Component\Utility\Crypt;

/**
 * Tests the PrivateKey class.
 *
 * @group PrivateKeyTest
 */
class PrivateKeyTest extends UnitTestCase {

  /**
   * The state mock class.
   *
   * @var \Drupal\Core\State\StateInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $state;

  /**
   * The private key service mock.
   *
   * @var \Drupal\Core\PrivateKey
   */
  protected $privateKey;

  /**
   * The random key to use in tests.
   *
   * @var string
   */
  protected $key;

  /**
   * {@inheritdoc}
   */
  protected function setUp(): void {
    parent::setUp();
    $this->key = Crypt::randomBytesBase64(55);

    $this->state = $this->createMock('Drupal\Core\State\StateInterface');

    $this->privateKey = new PrivateKey($this->state);
  }

  /**
   * Tests PrivateKey::get().
   */
  public function testGet(): void {
    $this->state->expects($this->once())
      ->method('get')
      ->with('system.private_key')
      ->willReturn($this->key);

    $this->assertEquals($this->key, $this->privateKey->get());
  }

  /**
   * Tests PrivateKey::get() with no private key from state.
   */
  public function testGetNoState(): void {
    $this->assertIsString($this->privateKey->get());
  }

  /**
   * Tests PrivateKey::setPrivateKey().
   */
  public function testSet(): void {
    $random_name = $this->randomMachineName();

    $this->state->expects($this->once())
      ->method('set')
      ->with('system.private_key', $random_name)
      ->willReturn(TRUE);

    $this->privateKey->set($random_name);
  }

}