ConfigTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Test class for \Magento\Framework\View\Page\Config
  8. */
  9. namespace Magento\Framework\View\Test\Unit\Page;
  10. use Magento\Framework\Locale\Resolver;
  11. use Magento\Framework\View\Page\Config;
  12. /**
  13. * @covers \Magento\Framework\View\Page\Config
  14. *
  15. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  16. */
  17. class ConfigTest extends \PHPUnit\Framework\TestCase
  18. {
  19. /**
  20. * @var Config
  21. */
  22. protected $model;
  23. /**
  24. * @var \Magento\Framework\View\Asset\Repository|\PHPUnit_Framework_MockObject_MockObject
  25. */
  26. protected $assetRepo;
  27. /**
  28. * @var \Magento\Framework\View\Asset\GroupedCollection|\PHPUnit_Framework_MockObject_MockObject
  29. */
  30. protected $pageAssets;
  31. /**
  32. * @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  33. */
  34. protected $scopeConfig;
  35. /**
  36. * @var \Magento\Framework\View\Page\FaviconInterface|\PHPUnit_Framework_MockObject_MockObject
  37. */
  38. protected $favicon;
  39. /**
  40. * @var \Magento\Framework\View\Layout\BuilderInterface|\PHPUnit_Framework_MockObject_MockObject
  41. */
  42. protected $builder;
  43. /**
  44. * @var \Magento\Framework\View\Asset\File|\PHPUnit_Framework_MockObject_MockObject
  45. */
  46. protected $asset;
  47. /**
  48. * @var \Magento\Framework\View\Asset\Remote|\PHPUnit_Framework_MockObject_MockObject
  49. */
  50. protected $remoteAsset;
  51. /**
  52. * @var \Magento\Framework\View\Page\Title|\PHPUnit_Framework_MockObject_MockObject
  53. */
  54. protected $title;
  55. /**
  56. * @var \Magento\Framework\App\State|\PHPUnit_Framework_MockObject_MockObject
  57. */
  58. protected $areaResolverMock;
  59. /**
  60. * @var \Magento\Framework\Locale\ResolverInterface|\PHPUnit_Framework_MockObject_MockObject
  61. */
  62. protected $localeMock;
  63. protected function setUp()
  64. {
  65. $this->assetRepo = $this->createMock(\Magento\Framework\View\Asset\Repository::class);
  66. $this->pageAssets = $this->createMock(\Magento\Framework\View\Asset\GroupedCollection::class);
  67. $this->scopeConfig =
  68. $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
  69. $this->favicon = $this->createMock(\Magento\Framework\View\Page\FaviconInterface::class);
  70. $this->builder = $this->createMock(\Magento\Framework\View\Layout\BuilderInterface::class);
  71. $this->asset = $this->createMock(\Magento\Framework\View\Asset\File::class);
  72. $this->remoteAsset = $this->createMock(\Magento\Framework\View\Asset\Remote::class);
  73. $this->title = $this->createMock(\Magento\Framework\View\Page\Title::class);
  74. $this->localeMock =
  75. $this->getMockForAbstractClass(\Magento\Framework\Locale\ResolverInterface::class, [], '', false);
  76. $this->localeMock->expects($this->any())
  77. ->method('getLocale')
  78. ->willReturn(Resolver::DEFAULT_LOCALE);
  79. $this->model = (new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this))
  80. ->getObject(
  81. \Magento\Framework\View\Page\Config::class,
  82. [
  83. 'assetRepo' => $this->assetRepo,
  84. 'pageAssets' => $this->pageAssets,
  85. 'scopeConfig' => $this->scopeConfig,
  86. 'favicon' => $this->favicon,
  87. 'localeResolver' => $this->localeMock
  88. ]
  89. );
  90. $this->areaResolverMock = $this->createMock(\Magento\Framework\App\State::class);
  91. $areaResolverReflection = (new \ReflectionClass(get_class($this->model)))->getProperty('areaResolver');
  92. $areaResolverReflection->setAccessible(true);
  93. $areaResolverReflection->setValue($this->model, $this->areaResolverMock);
  94. }
  95. public function testSetBuilder()
  96. {
  97. $this->assertInstanceOf(
  98. \Magento\Framework\View\Page\Config::class,
  99. $this->model->setBuilder($this->builder)
  100. );
  101. }
  102. public function testBuild()
  103. {
  104. $this->model->setBuilder($this->builder);
  105. $this->builder->expects($this->once())->method('build')->will(
  106. $this->returnValue(\Magento\Framework\View\LayoutInterface::class)
  107. );
  108. $this->model->publicBuild();
  109. }
  110. public function testGetTitle()
  111. {
  112. $this->assertInstanceOf(\Magento\Framework\View\Page\Title::class, $this->model->getTitle());
  113. }
  114. public function testMetadata()
  115. {
  116. $expectedMetadata = [
  117. 'charset' => null,
  118. 'media_type' => null,
  119. 'content_type' => null,
  120. 'description' => null,
  121. 'keywords' => null,
  122. 'robots' => null,
  123. 'title' => null,
  124. 'name' => 'test_value',
  125. 'html_encoded' => '&lt;title&gt;&lt;span class=&quot;test&quot;&gt;Test&lt;/span&gt;&lt;/title&gt;',
  126. ];
  127. $this->model->setMetadata('name', 'test_value');
  128. $this->model->setMetadata('html_encoded', '<title><span class="test">Test</span></title>');
  129. $this->assertEquals($expectedMetadata, $this->model->getMetadata());
  130. }
  131. public function testContentType()
  132. {
  133. $contentType = 'test_content_type';
  134. $this->model->setContentType($contentType);
  135. $this->assertEquals($contentType, $this->model->getContentType());
  136. }
  137. public function testContentTypeEmpty()
  138. {
  139. $expectedData = null;
  140. $this->assertEquals($expectedData, $this->model->getContentType());
  141. }
  142. public function testContentTypeAuto()
  143. {
  144. $expectedData = 'default_media_type; charset=default_charset';
  145. $this->model->setContentType('auto');
  146. $this->scopeConfig->expects($this->at(0))->method('getValue')->with('design/head/default_media_type', 'store')
  147. ->will($this->returnValue('default_media_type'));
  148. $this->scopeConfig->expects($this->at(1))->method('getValue')->with('design/head/default_charset', 'store')
  149. ->will($this->returnValue('default_charset'));
  150. $this->assertEquals($expectedData, $this->model->getContentType());
  151. }
  152. public function testMediaType()
  153. {
  154. $mediaType = 'test_media_type';
  155. $this->model->setMediaType($mediaType);
  156. $this->assertEquals($mediaType, $this->model->getMediaType());
  157. }
  158. public function testMediaTypeEmpty()
  159. {
  160. $expectedData = 'default_media_type';
  161. $this->scopeConfig->expects($this->once())->method('getValue')->with('design/head/default_media_type', 'store')
  162. ->will($this->returnValue('default_media_type'));
  163. $this->assertEquals($expectedData, $this->model->getMediaType());
  164. }
  165. public function testCharset()
  166. {
  167. $charset = 'test_charset';
  168. $this->model->setCharset($charset);
  169. $this->assertEquals($charset, $this->model->getCharset());
  170. }
  171. public function testCharsetEmpty()
  172. {
  173. $expectedData = 'default_charset';
  174. $this->scopeConfig->expects($this->once())->method('getValue')->with('design/head/default_charset', 'store')
  175. ->will($this->returnValue('default_charset'));
  176. $this->assertEquals($expectedData, $this->model->getCharset());
  177. }
  178. public function testDescription()
  179. {
  180. $description = 'test_description';
  181. $this->model->setDescription($description);
  182. $this->assertEquals($description, $this->model->getDescription());
  183. }
  184. public function testDescriptionEmpty()
  185. {
  186. $expectedData = 'default_description';
  187. $this->scopeConfig->expects($this->once())->method('getValue')->with('design/head/default_description', 'store')
  188. ->will($this->returnValue('default_description'));
  189. $this->assertEquals($expectedData, $this->model->getDescription());
  190. }
  191. public function testKeywords()
  192. {
  193. $keywords = 'test_keywords';
  194. $this->model->setKeywords($keywords);
  195. $this->assertEquals($keywords, $this->model->getKeywords());
  196. }
  197. public function testKeywordsEmpty()
  198. {
  199. $expectedData = 'default_keywords';
  200. $this->scopeConfig->expects($this->once())->method('getValue')->with('design/head/default_keywords', 'store')
  201. ->will($this->returnValue('default_keywords'));
  202. $this->assertEquals($expectedData, $this->model->getKeywords());
  203. }
  204. public function testRobots()
  205. {
  206. $this->areaResolverMock->expects($this->once())->method('getAreaCode')->willReturn('frontend');
  207. $robots = 'test_robots';
  208. $this->model->setRobots($robots);
  209. $this->assertEquals($robots, $this->model->getRobots());
  210. }
  211. public function testRobotsEmpty()
  212. {
  213. $this->areaResolverMock->expects($this->once())->method('getAreaCode')->willReturn('frontend');
  214. $expectedData = 'default_robots';
  215. $this->scopeConfig->expects($this->once())->method('getValue')->with(
  216. 'design/search_engine_robots/default_robots',
  217. 'store'
  218. )
  219. ->will($this->returnValue('default_robots'));
  220. $this->assertEquals($expectedData, $this->model->getRobots());
  221. }
  222. public function testRobotsAdminhtml()
  223. {
  224. $this->areaResolverMock->expects($this->once())->method('getAreaCode')->willReturn('adminhtml');
  225. $robots = 'test_robots';
  226. $this->model->setRobots($robots);
  227. $this->assertEquals('NOINDEX,NOFOLLOW', $this->model->getRobots());
  228. }
  229. public function testGetAssetCollection()
  230. {
  231. $this->assertInstanceOf(
  232. \Magento\Framework\View\Asset\GroupedCollection::class,
  233. $this->model->getAssetCollection()
  234. );
  235. }
  236. /**
  237. * @param string $file
  238. * @param array $properties
  239. * @param string|null $name
  240. * @param string $expectedName
  241. *
  242. * @dataProvider pageAssetDataProvider
  243. */
  244. public function testAddPageAsset($file, $properties, $name, $expectedName)
  245. {
  246. $this->assetRepo->expects($this->once())->method('createAsset')->with($file)->will(
  247. $this->returnValue($this->asset)
  248. );
  249. $this->pageAssets->expects($this->once())->method('add')->with($expectedName, $this->asset, $properties);
  250. $this->assertInstanceOf(
  251. \Magento\Framework\View\Page\Config::class,
  252. $this->model->addPageAsset($file, $properties, $name)
  253. );
  254. }
  255. /**
  256. * @return array
  257. */
  258. public function pageAssetDataProvider()
  259. {
  260. return [
  261. [
  262. 'test.php',
  263. ['one', 'two', 3],
  264. 'test_name',
  265. 'test_name',
  266. ],
  267. [
  268. 'filename',
  269. [],
  270. null,
  271. 'filename'
  272. ]
  273. ];
  274. }
  275. /**
  276. * @param string $url
  277. * @param string $contentType
  278. * @param array $properties
  279. * @param string|null $name
  280. * @param string $expectedName
  281. *
  282. * @dataProvider remotePageAssetDataProvider
  283. */
  284. public function testAddRemotePageAsset($url, $contentType, $properties, $name, $expectedName)
  285. {
  286. $this->assetRepo->expects($this->once())->method('createRemoteAsset')->with($url, $contentType)->will(
  287. $this->returnValue($this->remoteAsset)
  288. );
  289. $this->pageAssets->expects($this->once())->method('add')->with($expectedName, $this->remoteAsset, $properties);
  290. $this->assertInstanceOf(
  291. \Magento\Framework\View\Page\Config::class,
  292. $this->model->addRemotePageAsset($url, $contentType, $properties, $name)
  293. );
  294. }
  295. /**
  296. * @return array
  297. */
  298. public function remotePageAssetDataProvider()
  299. {
  300. return [
  301. [
  302. 'http://test.com',
  303. '<body><context>some content</context></body>',
  304. ['one', 'two', 3],
  305. 'test_name',
  306. 'test_name',
  307. ],
  308. [
  309. 'http://test.com',
  310. '',
  311. [],
  312. null,
  313. 'http://test.com'
  314. ]
  315. ];
  316. }
  317. public function testAddRss()
  318. {
  319. $title = 'test title';
  320. $href = 'http://test.com';
  321. $expected = ['attributes' => 'rel="alternate" type="application/rss+xml" title="test title"'];
  322. $this->assetRepo->expects($this->once())->method('createRemoteAsset')->with($href, 'unknown')->will(
  323. $this->returnValue($this->remoteAsset)
  324. );
  325. $this->pageAssets->expects($this->once())->method('add')->with(
  326. 'link/http://test.com',
  327. $this->remoteAsset,
  328. $expected
  329. );
  330. $this->assertInstanceOf(\Magento\Framework\View\Page\Config::class, $this->model->addRss($title, $href));
  331. }
  332. public function testAddBodyClass()
  333. {
  334. $className = 'test class';
  335. $this->assertInstanceOf(\Magento\Framework\View\Page\Config::class, $this->model->addBodyClass($className));
  336. $this->assertEquals('test-class', $this->model->getElementAttribute('body', 'class'));
  337. }
  338. /**
  339. * @param string $elementType
  340. * @param string $attribute
  341. * @param string $value
  342. *
  343. * @dataProvider elementAttributeDataProvider
  344. */
  345. public function testElementAttribute($elementType, $attribute, $value)
  346. {
  347. $this->model->setElementAttribute($elementType, $attribute, $value);
  348. $this->assertEquals($value, $this->model->getElementAttribute($elementType, $attribute));
  349. }
  350. /**
  351. * @return array
  352. */
  353. public function elementAttributeDataProvider()
  354. {
  355. return [
  356. [
  357. 'head',
  358. 'class',
  359. 'test',
  360. ],
  361. [
  362. 'body',
  363. 'class',
  364. 'value'
  365. ],
  366. [
  367. Config::ELEMENT_TYPE_HTML,
  368. Config::HTML_ATTRIBUTE_LANG,
  369. str_replace('_', '-', Resolver::DEFAULT_LOCALE)
  370. ],
  371. ];
  372. }
  373. /**
  374. * @param string $elementType
  375. * @param string $attribute
  376. * @param string $value
  377. *
  378. * @dataProvider elementAttributeExceptionDataProvider
  379. */
  380. public function testElementAttributeException($elementType, $attribute, $value)
  381. {
  382. $this->expectException(\Magento\Framework\Exception\LocalizedException::class);
  383. $this->expectExceptionMessage($elementType . " isn't allowed");
  384. $this->model->setElementAttribute($elementType, $attribute, $value);
  385. }
  386. /**
  387. * @return array
  388. */
  389. public function elementAttributeExceptionDataProvider()
  390. {
  391. return [
  392. [
  393. 'test',
  394. 'class',
  395. 'test',
  396. ],
  397. [
  398. '',
  399. '',
  400. ''
  401. ],
  402. [
  403. null,
  404. null,
  405. null
  406. ]
  407. ];
  408. }
  409. /**
  410. * @param string $elementType
  411. * @param string $attributes
  412. *
  413. * @dataProvider elementAttributesDataProvider
  414. */
  415. public function testElementAttributes($elementType, $attributes)
  416. {
  417. foreach ($attributes as $attribute => $value) {
  418. $this->model->setElementAttribute($elementType, $attribute, $value);
  419. }
  420. $this->assertEquals($attributes, $this->model->getElementAttributes($elementType));
  421. }
  422. /**
  423. * @return array
  424. */
  425. public function elementAttributesDataProvider()
  426. {
  427. return [
  428. [
  429. 'html',
  430. [
  431. 'context' => 'value',
  432. Config::HTML_ATTRIBUTE_LANG => str_replace('_', '-', Resolver::DEFAULT_LOCALE)
  433. ],
  434. ],
  435. ];
  436. }
  437. /**
  438. * @param string $handle
  439. *
  440. * @dataProvider pageLayoutDataProvider
  441. */
  442. public function testPageLayout($handle)
  443. {
  444. $this->model->setPageLayout($handle);
  445. $this->assertEquals($handle, $this->model->getPageLayout());
  446. }
  447. /**
  448. * @return array
  449. */
  450. public function pageLayoutDataProvider()
  451. {
  452. return [
  453. [
  454. 'test',
  455. ],
  456. [
  457. ''
  458. ],
  459. [
  460. null
  461. ],
  462. [
  463. [
  464. 'test',
  465. ]
  466. ]
  467. ];
  468. }
  469. public function testGetFaviconFile()
  470. {
  471. $expected = 'test';
  472. $this->favicon->expects($this->once())->method('getFaviconFile')->will($this->returnValue($expected));
  473. $this->assertEquals($expected, $this->model->getFaviconFile());
  474. }
  475. public function testGetDefaultFavicon()
  476. {
  477. $this->favicon->expects($this->once())->method('getDefaultFavicon');
  478. $this->model->getDefaultFavicon();
  479. }
  480. /**
  481. * @param bool $isAvailable
  482. * @param string $result
  483. * @dataProvider getIncludesDataProvider
  484. */
  485. public function testGetIncludes($isAvailable, $result)
  486. {
  487. $model = (new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this))
  488. ->getObject(
  489. \Magento\Framework\View\Page\Config::class,
  490. [
  491. 'assetRepo' => $this->assetRepo,
  492. 'pageAssets' => $this->pageAssets,
  493. 'scopeConfig' => $this->scopeConfig,
  494. 'favicon' => $this->favicon,
  495. 'localeResolver' => $this->localeMock,
  496. 'isIncludesAvailable' => $isAvailable
  497. ]
  498. );
  499. $this->scopeConfig->expects($isAvailable ? $this->once() : $this->never())
  500. ->method('getValue')
  501. ->with('design/head/includes', 'store')
  502. ->willReturn($result);
  503. $this->assertEquals($result, $model->getIncludes());
  504. }
  505. /**
  506. * @return array
  507. */
  508. public function getIncludesDataProvider()
  509. {
  510. return [
  511. [
  512. true,
  513. '<script type="text/javascript">
  514. Fieldset.addToPrefix(1);
  515. </script>'
  516. ],
  517. [false, null]
  518. ];
  519. }
  520. }