comparison core/tests/Drupal/Tests/Core/PrivateKeyTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\Tests\Core;
4
5 use Drupal\Core\PrivateKey;
6 use Drupal\Tests\UnitTestCase;
7 use Drupal\Component\Utility\Crypt;
8
9 /**
10 * Tests the PrivateKey class.
11 *
12 * @group PrivateKeyTest
13 */
14 class PrivateKeyTest extends UnitTestCase {
15
16 /**
17 * The state mock class.
18 *
19 * @var \Drupal\Core\State\StateInterface|\PHPUnit_Framework_MockObject_MockObject
20 */
21 protected $state;
22
23 /**
24 * The private key service mock.
25 *
26 * @var \Drupal\Core\PrivateKey
27 */
28 protected $privateKey;
29
30 /**
31 * The random key to use in tests.
32 *
33 * @var string
34 */
35 protected $key;
36
37 /**
38 * {@inheritdoc}
39 */
40 protected function setUp() {
41 parent::setUp();
42 $this->key = Crypt::randomBytesBase64(55);
43
44 $this->state = $this->getMock('Drupal\Core\State\StateInterface');
45
46 $this->privateKey = new PrivateKey($this->state);
47 }
48
49 /**
50 * Tests PrivateKey::get().
51 */
52 public function testGet() {
53 $this->state->expects($this->once())
54 ->method('get')
55 ->with('system.private_key')
56 ->will($this->returnValue($this->key));
57
58 $this->assertEquals($this->key, $this->privateKey->get());
59 }
60
61 /**
62 * Tests PrivateKey::get() with no private key from state.
63 */
64 public function testGetNoState() {
65 $this->assertInternalType('string', $this->privateKey->get());
66 }
67
68 /**
69 * Tests PrivateKey::setPrivateKey().
70 */
71 public function testSet() {
72 $random_name = $this->randomMachineName();
73
74 $this->state->expects($this->once())
75 ->method('set')
76 ->with('system.private_key', $random_name)
77 ->will($this->returnValue(TRUE));
78
79 $this->privateKey->set($random_name);
80 }
81
82 }