PageRenderTime 33ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/internal/Magento/Framework/View/Test/Unit/Element/AbstractBlockTest.php

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