BlockTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Cms\Model;
  7. use Magento\Cms\Model\ResourceModel\Block;
  8. use Magento\Cms\Model\BlockFactory;
  9. use Magento\Framework\App\ResourceConnection;
  10. use Magento\Framework\ObjectManagerInterface;
  11. use Magento\Framework\Stdlib\DateTime\DateTime;
  12. use Magento\Framework\Stdlib\DateTime\Timezone;
  13. use Magento\TestFramework\Helper\Bootstrap;
  14. use PHPUnit\Framework\TestCase;
  15. /**
  16. * @magentoAppArea adminhtml
  17. */
  18. class BlockTest extends TestCase
  19. {
  20. /**
  21. * @var ObjectManagerInterface
  22. */
  23. private $objectManager;
  24. /**
  25. * @var Block
  26. */
  27. private $blockResource;
  28. /**
  29. * @var BlockFactory
  30. */
  31. private $blockFactory;
  32. /**
  33. * @var GetBlockByIdentifier
  34. */
  35. private $blockIdentifier;
  36. protected function setUp()
  37. {
  38. $this->objectManager = Bootstrap::getObjectManager();
  39. /** @var BlockFactory $blockFactory */
  40. /** @var Block $blockResource */
  41. /** @var GetBlockByIdentifier $getBlockByIdentifierCommand */
  42. $this->blockResource = $this->objectManager->create(Block::class);
  43. $this->blockFactory = $this->objectManager->create(BlockFactory::class);
  44. $this->blockIdentifier = $this->objectManager->create(GetBlockByIdentifier::class);
  45. }
  46. /**
  47. * Tests the get by identifier command
  48. * @param array $blockData
  49. * @throws \Exception
  50. * @throws \Magento\Framework\Exception\NoSuchEntityException
  51. * @magentoDbIsolation enabled
  52. * @dataProvider testGetByIdentifierDataProvider
  53. */
  54. public function testGetByIdentifier(array $blockData)
  55. {
  56. # Prepare and save the temporary block
  57. $tempBlock = $this->blockFactory->create();
  58. $tempBlock->setData($blockData);
  59. $this->blockResource->save($tempBlock);
  60. # Load previously created block and compare identifiers
  61. $storeId = reset($blockData['stores']);
  62. $block = $this->blockIdentifier->execute($blockData['identifier'], $storeId);
  63. $this->assertEquals($blockData['identifier'], $block->getIdentifier());
  64. }
  65. /**
  66. * Tests the get by identifier command
  67. * @param array $blockData
  68. * @throws \Exception
  69. * @throws \Magento\Framework\Exception\NoSuchEntityException
  70. * @magentoDbIsolation enabled
  71. * @dataProvider testGetByIdentifierDataProvider
  72. */
  73. public function testUpdateTime(array $blockData)
  74. {
  75. /**
  76. * @var $db \Magento\Framework\DB\Adapter\AdapterInterface
  77. */
  78. $db = $this->objectManager->get(\Magento\Framework\App\ResourceConnection::class)
  79. ->getConnection(ResourceConnection::DEFAULT_CONNECTION);
  80. # Prepare and save the temporary block
  81. $tempBlock = $this->blockFactory->create();
  82. $tempBlock->setData($blockData);
  83. $beforeTimestamp = $db->fetchCol('SELECT UNIX_TIMESTAMP()')[0];
  84. $this->blockResource->save($tempBlock);
  85. $afterTimestamp = $db->fetchCol('SELECT UNIX_TIMESTAMP()')[0];
  86. # Load previously created block and compare identifiers
  87. $storeId = reset($blockData['stores']);
  88. $block = $this->blockIdentifier->execute($blockData['identifier'], $storeId);
  89. $blockTimestamp = strtotime($block->getUpdateTime());
  90. /*
  91. * These checks prevent a race condition MAGETWO-87353
  92. */
  93. $this->assertGreaterThanOrEqual($beforeTimestamp, $blockTimestamp);
  94. $this->assertLessThanOrEqual($afterTimestamp, $blockTimestamp);
  95. }
  96. /**
  97. * Data provider for "testGetByIdentifier" and "testUpdateTime" method
  98. * @return array
  99. */
  100. public function testGetByIdentifierDataProvider(): array
  101. {
  102. return [
  103. [
  104. 'data' => [
  105. 'title' => 'Test title',
  106. 'stores' => [0],
  107. 'identifier' => 'test-identifier',
  108. 'content' => 'Test content',
  109. 'is_active' => 1
  110. ]
  111. ]
  112. ];
  113. }
  114. }