OutputTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Catalog\Helper;
  7. class OutputTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \Magento\Catalog\Helper\Output
  11. */
  12. protected $_helper;
  13. protected function setUp()
  14. {
  15. $this->_helper = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  16. \Magento\Catalog\Helper\Output::class
  17. );
  18. }
  19. /**
  20. * addHandler()
  21. * getHandlers()
  22. */
  23. public function testAddHandlerGetHandlers()
  24. {
  25. // invalid handler
  26. $this->_helper->addHandler('method', 'handler');
  27. $this->assertEquals([], $this->_helper->getHandlers('method'));
  28. // add one handler
  29. $objectOne = new \StdClass();
  30. $this->_helper->addHandler('valid', $objectOne);
  31. $this->assertSame([$objectOne], $this->_helper->getHandlers('valid'));
  32. // add another one
  33. $objectTwo = new \StdClass();
  34. $this->_helper->addHandler('valid', $objectTwo);
  35. $this->assertSame([$objectOne, $objectTwo], $this->_helper->getHandlers('valid'));
  36. }
  37. public function testProcess()
  38. {
  39. $this->_helper->addHandler('sampleProcessor', $this);
  40. $this->assertStringStartsWith(__CLASS__, $this->_helper->process('sampleProcessor', uniqid(), []));
  41. }
  42. public function testProductAttribute()
  43. {
  44. $this->_testAttribute(
  45. 'productAttribute',
  46. \Magento\Catalog\Model\Product::ENTITY,
  47. "&lt;p&gt;line1&lt;/p&gt;<br />\nline2"
  48. );
  49. }
  50. public function testCategoryAttribute()
  51. {
  52. $this->_testAttribute(
  53. 'categoryAttribute',
  54. \Magento\Catalog\Model\Category::ENTITY,
  55. "&lt;p&gt;line1&lt;/p&gt;\nline2"
  56. );
  57. }
  58. /**
  59. * @dataProvider isDirectiveDataProvider
  60. */
  61. public function testIsDirective($html, $expectedResult)
  62. {
  63. $this->assertEquals($expectedResult, $this->_helper->isDirectivesExists($html));
  64. }
  65. public function isDirectiveDataProvider()
  66. {
  67. return [
  68. ['{{', false],
  69. ['Test string', false],
  70. ['{store url="customer/account/login"}', false],
  71. ['{{store url="customer/account/login"}}', true],
  72. ];
  73. }
  74. /**
  75. * Helper method for testProcess()
  76. *
  77. * @param \Magento\Catalog\Helper\Output $helper
  78. * @param string $string
  79. * @param mixed $params
  80. * @return string
  81. * @see testProcess()
  82. *
  83. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  84. */
  85. public function sampleProcessor(\Magento\Catalog\Helper\Output $helper, $string, $params)
  86. {
  87. return __CLASS__ . $string;
  88. }
  89. /**
  90. * Test productAttribute() or categoryAttribute() method
  91. *
  92. * @param string $method
  93. * @param string $entityCode
  94. * @param string $expectedResult
  95. * @throws \Exception on assertion failure
  96. */
  97. protected function _testAttribute($method, $entityCode, $expectedResult)
  98. {
  99. $attributeName = 'description';
  100. $attribute = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  101. \Magento\Eav\Model\Config::class
  102. )->getAttribute(
  103. $entityCode,
  104. $attributeName
  105. );
  106. $isHtml = $attribute->getIsHtmlAllowedOnFront();
  107. $isWysiwyg = $attribute->getIsWysiwygEnabled();
  108. $attribute->setIsHtmlAllowedOnFront(0)->setIsWysiwygEnabled(0);
  109. try {
  110. $this->assertEquals(
  111. $expectedResult,
  112. $this->_helper->{$method}(uniqid(), "<p>line1</p>\nline2", $attributeName)
  113. );
  114. $attribute->setIsHtmlAllowedOnFront($isHtml)->setIsWysiwygEnabled($isWysiwyg);
  115. } catch (\Exception $e) {
  116. $attribute->setIsHtmlAllowedOnFront($isHtml)->setIsWysiwygEnabled($isWysiwyg);
  117. throw $e;
  118. }
  119. }
  120. }