PrivateKeyTest.php 1.6 KB

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