AdminSessionInfoTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Security\Test\Unit\Model\ResourceModel;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  8. /**
  9. * Test class for \Magento\Security\Model\ResourceModel\AdminSessionInfo testing
  10. */
  11. class AdminSessionInfoTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /** @var \Magento\Security\Model\ResourceModel\AdminSessionInfo */
  14. protected $model;
  15. /** @var \Magento\Framework\Stdlib\DateTime */
  16. protected $dateTimeMock;
  17. /** @var \Magento\Framework\App\ResourceConnection */
  18. protected $resourceMock;
  19. /** @var \Magento\Framework\DB\Adapter\AdapterInterface */
  20. protected $dbAdapterMock;
  21. /**
  22. * Init mocks for tests
  23. * @return void
  24. */
  25. protected function setUp()
  26. {
  27. $objectManager = new ObjectManager($this);
  28. $this->dateTimeMock = $this->createMock(\Magento\Framework\Stdlib\DateTime::class);
  29. $this->resourceMock = $this->createMock(\Magento\Framework\App\ResourceConnection::class);
  30. $this->dbAdapterMock = $this->createMock(\Magento\Framework\DB\Adapter\AdapterInterface::class);
  31. $this->model = $objectManager->getObject(
  32. \Magento\Security\Model\ResourceModel\AdminSessionInfo::class,
  33. [
  34. 'resource' => $this->resourceMock,
  35. 'dateTime' => $this->dateTimeMock
  36. ]
  37. );
  38. }
  39. /**
  40. * @return void
  41. */
  42. public function testDeleteSessionsOlderThen()
  43. {
  44. $timestamp = 12345;
  45. $this->resourceMock->expects($this->once())
  46. ->method('getConnection')
  47. ->willReturn($this->dbAdapterMock);
  48. $this->dbAdapterMock->expects($this->once())
  49. ->method('delete')
  50. ->with($this->model->getMainTable(), ['updated_at < ?' => $this->dateTimeMock->formatDate($timestamp)])
  51. ->willReturnSelf();
  52. $this->assertEquals($this->model, $this->model->deleteSessionsOlderThen($timestamp));
  53. }
  54. /**
  55. * @return void
  56. */
  57. public function testUpdateStatusByUserId()
  58. {
  59. $status = 2;
  60. $userId = 10;
  61. $withStatuses = [1, 5];
  62. $excludedSessionIds = [20, 21, 22];
  63. $updateOlderThen = '2015-12-31 23:59:59';
  64. $whereStatement = [
  65. 'updated_at > ?' => $this->dateTimeMock->formatDate($updateOlderThen),
  66. 'user_id = ?' => (int) $userId,
  67. ];
  68. if (!empty($excludedSessionIds)) {
  69. $whereStatement['session_id NOT IN (?)'] = $excludedSessionIds;
  70. }
  71. if (!empty($withStatuses)) {
  72. $whereStatement['status IN (?)'] = $withStatuses;
  73. }
  74. $this->resourceMock->expects($this->once())
  75. ->method('getConnection')
  76. ->willReturn($this->dbAdapterMock);
  77. $this->dbAdapterMock->expects($this->once())
  78. ->method('update')
  79. ->with($this->model->getMainTable(), ['status' => $status], $whereStatement)
  80. ->willReturnSelf();
  81. $this->model->updateStatusByUserId($status, $userId, $withStatuses, $excludedSessionIds, $updateOlderThen);
  82. }
  83. }