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