AbstractBlockTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\View\Test\Unit\Element;
  7. use Magento\Framework\View\Element\AbstractBlock;
  8. use Magento\Framework\View\Element\Context;
  9. use Magento\Framework\Config\View;
  10. use Magento\Framework\View\ConfigInterface;
  11. use Magento\Framework\Event\ManagerInterface as EventManagerInterface;
  12. use Magento\Framework\App\Config\ScopeConfigInterface;
  13. use Magento\Framework\App\Cache\StateInterface as CacheStateInterface;
  14. use Magento\Framework\App\CacheInterface;
  15. use Magento\Framework\Session\SidResolverInterface;
  16. use Magento\Framework\Session\SessionManagerInterface;
  17. /**
  18. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  19. */
  20. class AbstractBlockTest extends \PHPUnit\Framework\TestCase
  21. {
  22. /**
  23. * @var AbstractBlock
  24. */
  25. private $block;
  26. /**
  27. * @var EventManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. private $eventManagerMock;
  30. /**
  31. * @var ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. private $scopeConfigMock;
  34. /**
  35. * @var CacheStateInterface|\PHPUnit_Framework_MockObject_MockObject
  36. */
  37. private $cacheStateMock;
  38. /**
  39. * @var CacheInterface|\PHPUnit_Framework_MockObject_MockObject
  40. */
  41. private $cacheMock;
  42. /**
  43. * @var SidResolverInterface|\PHPUnit_Framework_MockObject_MockObject
  44. */
  45. private $sidResolverMock;
  46. /**
  47. * @var SessionManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  48. */
  49. private $sessionMock;
  50. /**
  51. * @return void
  52. */
  53. protected function setUp()
  54. {
  55. $this->eventManagerMock = $this->getMockForAbstractClass(EventManagerInterface::class);
  56. $this->scopeConfigMock = $this->getMockForAbstractClass(ScopeConfigInterface::class);
  57. $this->cacheStateMock = $this->getMockForAbstractClass(CacheStateInterface::class);
  58. $this->cacheMock = $this->getMockForAbstractClass(CacheInterface::class);
  59. $this->sidResolverMock = $this->getMockForAbstractClass(SidResolverInterface::class);
  60. $this->sessionMock = $this->getMockForAbstractClass(SessionManagerInterface::class);
  61. $contextMock = $this->createMock(Context::class);
  62. $contextMock->expects($this->once())
  63. ->method('getEventManager')
  64. ->willReturn($this->eventManagerMock);
  65. $contextMock->expects($this->once())
  66. ->method('getScopeConfig')
  67. ->willReturn($this->scopeConfigMock);
  68. $contextMock->expects($this->once())
  69. ->method('getCacheState')
  70. ->willReturn($this->cacheStateMock);
  71. $contextMock->expects($this->once())
  72. ->method('getCache')
  73. ->willReturn($this->cacheMock);
  74. $contextMock->expects($this->once())
  75. ->method('getSidResolver')
  76. ->willReturn($this->sidResolverMock);
  77. $contextMock->expects($this->once())
  78. ->method('getSession')
  79. ->willReturn($this->sessionMock);
  80. $this->block = $this->getMockForAbstractClass(
  81. AbstractBlock::class,
  82. ['context' => $contextMock]
  83. );
  84. }
  85. /**
  86. * @param string $expectedResult
  87. * @param string $nameInLayout
  88. * @param array $methodArguments
  89. * @dataProvider getUiIdDataProvider
  90. */
  91. public function testGetUiId($expectedResult, $nameInLayout, $methodArguments)
  92. {
  93. $this->block->setNameInLayout($nameInLayout);
  94. $this->assertEquals($expectedResult, call_user_func_array([$this->block, 'getUiId'], $methodArguments));
  95. }
  96. /**
  97. * @return array
  98. */
  99. public function getUiIdDataProvider()
  100. {
  101. return [
  102. [' data-ui-id="" ', null, []],
  103. [' data-ui-id="block" ', 'block', []],
  104. [' data-ui-id="block" ', 'block---', []],
  105. [' data-ui-id="block" ', '--block', []],
  106. [' data-ui-id="bl-ock" ', '--bl--ock---', []],
  107. [' data-ui-id="bl-ock" ', '--bL--Ock---', []],
  108. [' data-ui-id="b-l-o-c-k" ', '--b!@#$%^&**()L--O;:...c<_>k---', []],
  109. [
  110. ' data-ui-id="a0b1c2d3e4f5g6h7-i8-j9k0l1m2n-3o4p5q6r7-s8t9u0v1w2z3y4x5" ',
  111. 'a0b1c2d3e4f5g6h7',
  112. ['i8-j9k0l1m2n-3o4p5q6r7', 's8t9u0v1w2z3y4x5']
  113. ],
  114. [
  115. ' data-ui-id="capsed-block-name-cap-ed-param1-caps2-but-ton" ',
  116. 'CaPSed BLOCK NAME',
  117. ['cAp$Ed PaRaM1', 'caPs2', 'bUT-TOn']
  118. ],
  119. [
  120. ' data-ui-id="capsed-block-name-cap-ed-param1-caps2-but-ton-but-ton" ',
  121. 'CaPSed BLOCK NAME',
  122. ['cAp$Ed PaRaM1', 'caPs2', 'bUT-TOn', 'bUT-TOn']
  123. ],
  124. [' data-ui-id="block-0-1-2-3-4" ', '!block!', range(0, 5)]
  125. ];
  126. }
  127. /**
  128. * @return void
  129. */
  130. public function testGetVar()
  131. {
  132. $config = $this->createPartialMock(View::class, ['getVarValue']);
  133. $module = uniqid();
  134. $config->expects($this->any())
  135. ->method('getVarValue')
  136. ->willReturnMap([
  137. ['Magento_Theme', 'v1', 'one'],
  138. [$module, 'v2', 'two']
  139. ]);
  140. $configManager = $this->createMock(ConfigInterface::class);
  141. $configManager->expects($this->exactly(2))->method('getViewConfig')->willReturn($config);
  142. /** @var $block AbstractBlock|\PHPUnit_Framework_MockObject_MockObject */
  143. $params = ['viewConfig' => $configManager];
  144. $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  145. $block = $this->getMockForAbstractClass(
  146. AbstractBlock::class,
  147. $helper->getConstructArguments(AbstractBlock::class, $params)
  148. );
  149. $block->setData('module_name', 'Magento_Theme');
  150. $this->assertEquals('one', $block->getVar('v1'));
  151. $this->assertEquals('two', $block->getVar('v2', $module));
  152. }
  153. /**
  154. * @return void
  155. */
  156. public function testIsScopePrivate()
  157. {
  158. $this->assertFalse($this->block->isScopePrivate());
  159. }
  160. /**
  161. * @return void
  162. */
  163. public function testGetCacheKey()
  164. {
  165. $cacheKey = 'testKey';
  166. $this->block->setData('cache_key', $cacheKey);
  167. $this->assertEquals(AbstractBlock::CACHE_KEY_PREFIX . $cacheKey, $this->block->getCacheKey());
  168. }
  169. /**
  170. * @return void
  171. */
  172. public function testGetCacheKeyByName()
  173. {
  174. $nameInLayout = 'testBlock';
  175. $this->block->setNameInLayout($nameInLayout);
  176. $cacheKey = sha1($nameInLayout);
  177. $this->assertEquals(AbstractBlock::CACHE_KEY_PREFIX . $cacheKey, $this->block->getCacheKey());
  178. }
  179. /**
  180. * @return void
  181. */
  182. public function testToHtmlWhenModuleIsDisabled()
  183. {
  184. $moduleName = 'Test';
  185. $this->block->setData('module_name', $moduleName);
  186. $this->eventManagerMock->expects($this->any())
  187. ->method('dispatch')
  188. ->with('view_block_abstract_to_html_before', ['block' => $this->block]);
  189. $this->scopeConfigMock->expects($this->once())
  190. ->method('getValue')
  191. ->with('advanced/modules_disable_output/' . $moduleName, \Magento\Store\Model\ScopeInterface::SCOPE_STORE)
  192. ->willReturn(true);
  193. $this->assertSame('', $this->block->toHtml());
  194. }
  195. /**
  196. * @param string|bool $cacheLifetime
  197. * @param string|bool $dataFromCache
  198. * @param string $dataForSaveCache
  199. * @param \PHPUnit\Framework\MockObject\Matcher\InvokedCount $expectsDispatchEvent
  200. * @param \PHPUnit\Framework\MockObject\Matcher\InvokedCount $expectsCacheLoad
  201. * @param \PHPUnit\Framework\MockObject\Matcher\InvokedCount $expectsCacheSave
  202. * @param string $expectedResult
  203. * @return void
  204. * @dataProvider getCacheLifetimeDataProvider
  205. */
  206. public function testGetCacheLifetimeViaToHtml(
  207. $cacheLifetime,
  208. $dataFromCache,
  209. $dataForSaveCache,
  210. $expectsDispatchEvent,
  211. $expectsCacheLoad,
  212. $expectsCacheSave,
  213. $expectedResult
  214. ) {
  215. $moduleName = 'Test';
  216. $cacheKey = 'testKey';
  217. $this->block->setData('cache_key', $cacheKey);
  218. $this->block->setData('module_name', $moduleName);
  219. $this->block->setData('cache_lifetime', $cacheLifetime);
  220. $this->eventManagerMock->expects($expectsDispatchEvent)
  221. ->method('dispatch');
  222. $this->scopeConfigMock->expects($this->once())
  223. ->method('getValue')
  224. ->with('advanced/modules_disable_output/' . $moduleName, \Magento\Store\Model\ScopeInterface::SCOPE_STORE)
  225. ->willReturn(false);
  226. $this->cacheStateMock->expects($this->any())
  227. ->method('isEnabled')
  228. ->with(AbstractBlock::CACHE_GROUP)
  229. ->willReturn(true);
  230. $this->cacheMock->expects($expectsCacheLoad)
  231. ->method('load')
  232. ->with(AbstractBlock::CACHE_KEY_PREFIX . $cacheKey)
  233. ->willReturn($dataFromCache);
  234. $this->cacheMock->expects($expectsCacheSave)
  235. ->method('save')
  236. ->with($dataForSaveCache, AbstractBlock::CACHE_KEY_PREFIX . $cacheKey);
  237. $this->sidResolverMock->expects($this->any())
  238. ->method('getSessionIdQueryParam')
  239. ->with($this->sessionMock)
  240. ->willReturn('sessionIdQueryParam');
  241. $this->sessionMock->expects($this->any())
  242. ->method('getSessionId')
  243. ->willReturn('sessionId');
  244. $this->assertSame($expectedResult, $this->block->toHtml());
  245. }
  246. /**
  247. * @return array
  248. */
  249. public function getCacheLifetimeDataProvider()
  250. {
  251. return [
  252. [
  253. 'cacheLifetime' => null,
  254. 'dataFromCache' => 'dataFromCache',
  255. 'dataForSaveCache' => '',
  256. 'expectsDispatchEvent' => $this->exactly(2),
  257. 'expectsCacheLoad' => $this->never(),
  258. 'expectsCacheSave' => $this->never(),
  259. 'expectedResult' => '',
  260. ],
  261. [
  262. 'cacheLifetime' => false,
  263. 'dataFromCache' => 'dataFromCache',
  264. 'dataForSaveCache' => '',
  265. 'expectsDispatchEvent' => $this->exactly(2),
  266. 'expectsCacheLoad' => $this->never(),
  267. 'expectsCacheSave' => $this->never(),
  268. 'expectedResult' => '',
  269. ],
  270. [
  271. 'cacheLifetime' => 120,
  272. 'dataFromCache' => 'dataFromCache',
  273. 'dataForSaveCache' => '',
  274. 'expectsDispatchEvent' => $this->exactly(2),
  275. 'expectsCacheLoad' => $this->once(),
  276. 'expectsCacheSave' => $this->never(),
  277. 'expectedResult' => 'dataFromCache',
  278. ],
  279. [
  280. 'cacheLifetime' => '120string',
  281. 'dataFromCache' => 'dataFromCache',
  282. 'dataForSaveCache' => '',
  283. 'expectsDispatchEvent' => $this->exactly(2),
  284. 'expectsCacheLoad' => $this->once(),
  285. 'expectsCacheSave' => $this->never(),
  286. 'expectedResult' => 'dataFromCache',
  287. ],
  288. [
  289. 'cacheLifetime' => 120,
  290. 'dataFromCache' => false,
  291. 'dataForSaveCache' => '',
  292. 'expectsDispatchEvent' => $this->exactly(2),
  293. 'expectsCacheLoad' => $this->once(),
  294. 'expectsCacheSave' => $this->once(),
  295. 'expectedResult' => '',
  296. ],
  297. ];
  298. }
  299. }