3 namespace Drupal\Tests\Core;
5 use Drupal\Core\PrivateKey;
6 use Drupal\Tests\UnitTestCase;
7 use Drupal\Component\Utility\Crypt;
10 * Tests the PrivateKey class.
12 * @group PrivateKeyTest
14 class PrivateKeyTest extends UnitTestCase {
17 * The state mock class.
19 * @var \Drupal\Core\State\StateInterface|\PHPUnit_Framework_MockObject_MockObject
24 * The private key service mock.
26 * @var \Drupal\Core\PrivateKey
28 protected $privateKey;
31 * The random key to use in tests.
40 protected function setUp() {
42 $this->key = Crypt::randomBytesBase64(55);
44 $this->state = $this->getMock('Drupal\Core\State\StateInterface');
46 $this->privateKey = new PrivateKey($this->state);
50 * Tests PrivateKey::get().
52 public function testGet() {
53 $this->state->expects($this->once())
55 ->with('system.private_key')
56 ->will($this->returnValue($this->key));
58 $this->assertEquals($this->key, $this->privateKey->get());
62 * Tests PrivateKey::get() with no private key from state.
64 public function testGetNoState() {
65 $this->assertInternalType('string', $this->privateKey->get());
69 * Tests PrivateKey::setPrivateKey().
71 public function testSet() {
72 $random_name = $this->randomMachineName();
74 $this->state->expects($this->once())
76 ->with('system.private_key', $random_name)
77 ->will($this->returnValue(TRUE));
79 $this->privateKey->set($random_name);