AdminSessionInfoTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Security\Model\ResourceModel;
  7. class AdminSessionInfoTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \Magento\Framework\Model\AbstractModel
  11. */
  12. protected $model;
  13. /**
  14. * @var \Magento\Framework\ObjectManagerInterface
  15. */
  16. protected $objectManager;
  17. protected function setUp()
  18. {
  19. parent::setUp();
  20. $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  21. $this->model = $this->objectManager->create(\Magento\Security\Model\AdminSessionInfo::class);
  22. }
  23. protected function tearDown()
  24. {
  25. $this->objectManager = null;
  26. parent::tearDown();
  27. }
  28. /**
  29. * Test data for saving
  30. * @return array
  31. */
  32. public function getTestData()
  33. {
  34. return [
  35. 'session_id' => '569e273d752e9',
  36. 'user_id' => 1,
  37. 'status' => 1,
  38. 'created_at' => '2016-01-21 15:00:00',
  39. 'updated_at' => '2016-01-21 18:00:00'
  40. ];
  41. }
  42. /**
  43. * @return mixed
  44. */
  45. protected function saveTestData()
  46. {
  47. foreach ($this->getTestData() as $key => $value) {
  48. $this->model->setData($key, $value);
  49. }
  50. $this->model->save();
  51. return $this->model->getId();
  52. }
  53. /**
  54. * Check that model is saving data to database
  55. *
  56. * @magentoDbIsolation enabled
  57. */
  58. public function testIsModelSavingDataToDatabase()
  59. {
  60. $modelId = $this->saveTestData();
  61. $newModel = $this->model->load($modelId);
  62. $testData = $this->getTestData();
  63. $newModelData = [];
  64. foreach (array_keys($testData) as $key) {
  65. $newModelData[$key] = $newModel->getData($key);
  66. }
  67. $this->assertEquals($testData, $newModelData);
  68. }
  69. /**
  70. * Test for deleteSessionsOlderThen() method
  71. *
  72. * @magentoDataFixture Magento/Security/_files/adminsession.php
  73. */
  74. public function testDeleteSessionsOlderThen()
  75. {
  76. $session = $this->objectManager->create(\Magento\Security\Model\AdminSessionInfo::class);
  77. /** @var $session \Magento\Security\Model\AdminSessionInfo */
  78. $session->getResource()->deleteSessionsOlderThen(strtotime('2016-01-20 12:00:00'));
  79. $collection = $session->getResourceCollection()
  80. ->addFieldToFilter('main_table.updated_at', ['lt' => '2016-01-20 12:00:00'])
  81. ->load();
  82. $count = $collection->count();
  83. $this->assertEquals(0, $count);
  84. }
  85. /**
  86. * Test for updateStatusByUserId() method
  87. *
  88. * @magentoDataFixture Magento/Security/_files/adminsession.php
  89. */
  90. public function testUpdateStatusByUserId()
  91. {
  92. $session = $this->objectManager->create(\Magento\Security\Model\AdminSessionInfo::class);
  93. /** @var $session \Magento\Security\Model\AdminSessionInfo */
  94. $session->getResource()->updateStatusByUserId(
  95. \Magento\Security\Model\AdminSessionInfo::LOGGED_OUT_BY_LOGIN,
  96. 1,
  97. [1],
  98. [1],
  99. '2016-01-19 12:00:00'
  100. );
  101. $collection = $session->getResourceCollection()
  102. ->addFieldToFilter('main_table.user_id', 1)
  103. ->addFieldToFilter('main_table.status', \Magento\Security\Model\AdminSessionInfo::LOGGED_OUT_BY_LOGIN)
  104. ->load();
  105. $count = $collection->count();
  106. $this->assertGreaterThanOrEqual(1, $count);
  107. }
  108. }