TokensTableTest.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace Tools\Test\TestCase\Model\Table;
  3. use Shim\TestSuite\TestCase;
  4. use Tools\Model\Table\TokensTable;
  5. class TokensTableTest extends TestCase {
  6. /**
  7. * @var string[]
  8. */
  9. protected $fixtures = [
  10. 'plugin.Tools.Tokens',
  11. ];
  12. /**
  13. * @var \Tools\Model\Table\TokensTable
  14. */
  15. protected $Tokens;
  16. /**
  17. * @return void
  18. */
  19. public function setUp(): void {
  20. parent::setUp();
  21. $this->Tokens = $this->getTableLocator()->get('Tools.Tokens');
  22. }
  23. /**
  24. * @return void
  25. */
  26. public function tearDown(): void {
  27. $this->getTableLocator()->clear();
  28. parent::tearDown();
  29. }
  30. /**
  31. * @return void
  32. */
  33. public function testTokenInstance() {
  34. $this->assertInstanceOf(TokensTable::class, $this->Tokens);
  35. }
  36. /**
  37. * @return void
  38. */
  39. public function testGenerateKey() {
  40. $key = $this->Tokens->generateKey(4);
  41. $this->assertTrue(!empty($key) && strlen($key) === 4);
  42. }
  43. /**
  44. * @return void
  45. */
  46. public function testNewKeySpendKey() {
  47. $key = $this->Tokens->newKey('test', null, null, 'xyz');
  48. $this->assertTrue(!empty($key));
  49. $res = $this->Tokens->useKey('test', $key);
  50. $this->assertTrue(!empty($res));
  51. $res = $this->Tokens->useKey('test', $key);
  52. $this->assertTrue(!empty($res) && !empty($res->used));
  53. $res = $this->Tokens->useKey('test', $key . 'x');
  54. $this->assertNull($res);
  55. $res = $this->Tokens->useKey('testx', $key);
  56. $this->assertNull($res);
  57. }
  58. /**
  59. * @return void
  60. */
  61. public function testGarbageCollector() {
  62. $data = [
  63. 'created' => date(FORMAT_DB_DATETIME, time() - 3 * MONTH),
  64. 'type' => 'y',
  65. 'token' => 'x',
  66. ];
  67. $entity = $this->Tokens->newEntity($data, ['validate' => false]);
  68. $this->Tokens->save($entity);
  69. $count = $this->Tokens->find()->count();
  70. $this->Tokens->garbageCollector();
  71. $count2 = $this->Tokens->find()->count();
  72. $this->assertTrue($count > $count2);
  73. }
  74. }