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