EncryptionBehaviorTest.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace Tools\Test\TestCase\Model\Behavior;
  3. use Cake\Datasource\ConnectionManager;
  4. use Shim\TestSuite\TestCase;
  5. class EncryptionBehaviorTest extends TestCase {
  6. /**
  7. * @var array
  8. */
  9. protected array $fixtures = [
  10. 'plugin.Tools.Sessions',
  11. ];
  12. /**
  13. * @var \Tools\Model\Table\Table|\Tools\Model\Behavior\EncryptionBehavior
  14. */
  15. protected $table;
  16. /**
  17. * @return void
  18. */
  19. public function setUp(): void {
  20. parent::setUp();
  21. $this->table = $this->getTableLocator()->get('Sessions');
  22. $this->table->addBehavior('Tools.Encryption', [
  23. 'fields' => ['data'],
  24. 'key' => 'some-very-long-key-which-needs-to-be-at-least-32-chars-long',
  25. ]);
  26. }
  27. /**
  28. * @return void
  29. */
  30. public function testSaveBasic() {
  31. $data = [
  32. 'id' => 10,
  33. 'data' => 'test save',
  34. ];
  35. $entity = $this->table->newEntity($data);
  36. $entityAfter = $this->table->save($entity);
  37. $this->assertTrue((bool)$entityAfter);
  38. $connection = ConnectionManager::get('default');
  39. $lastInsertedId = $connection->getDriver()->lastInsertId();
  40. $result = $connection->getDriver()->execute('SELECT data FROM sessions WHERE id = :id', ['id' => $lastInsertedId])->fetchAll();
  41. $this->assertNotEquals($data['data'], $result[0][0]);
  42. }
  43. /**
  44. * @return void
  45. */
  46. public function testFindBasic() {
  47. $data = [
  48. 'id' => 10,
  49. 'data' => 'test save',
  50. ];
  51. $entity = $this->table->newEntity($data);
  52. $this->table->save($entity);
  53. $entity = $this->table->get(10);
  54. $this->assertEquals('test save', $entity->data);
  55. }
  56. }