TokensTableTest.php 1.7 KB

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