PrivateKeyTest.php 1.84 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
<?php

/**
 * @file
 * Contains \Drupal\Tests\Core\PrivateKeyTest.
 */

namespace Drupal\Tests\Core;

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

/**
 * Tests the private key service.
 */
class PrivateKeyTest extends UnitTestCase {

  /**
   * The state mock class.
   *
22
   * @var \Drupal\Core\KeyValueStore\StateInterface|\PHPUnit_Framework_MockObject_MockObject
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
   */
  protected $state;

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

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

  public static function getInfo() {
    return array(
      'name' => 'PrivateKey test',
      'description' => 'Tests the PrivateKey class.',
      'group' => 'System'
    );
  }

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

55
    $this->state = $this->getMock('Drupal\Core\KeyValueStore\StateInterface');
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 85 86 87 88 89 90 91 92 93

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

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

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

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

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

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

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

}