Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
OutputTest.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Catalog\Helper;
7 
8 class OutputTest extends \PHPUnit\Framework\TestCase
9 {
13  protected $_helper;
14 
15  protected function setUp()
16  {
18  \Magento\Catalog\Helper\Output::class
19  );
20  }
21 
26  public function testAddHandlerGetHandlers()
27  {
28  // invalid handler
29  $this->_helper->addHandler('method', 'handler');
30  $this->assertEquals([], $this->_helper->getHandlers('method'));
31 
32  // add one handler
33  $objectOne = new \StdClass();
34  $this->_helper->addHandler('valid', $objectOne);
35  $this->assertSame([$objectOne], $this->_helper->getHandlers('valid'));
36 
37  // add another one
38  $objectTwo = new \StdClass();
39  $this->_helper->addHandler('valid', $objectTwo);
40  $this->assertSame([$objectOne, $objectTwo], $this->_helper->getHandlers('valid'));
41  }
42 
43  public function testProcess()
44  {
45  $this->_helper->addHandler('sampleProcessor', $this);
46  $this->assertStringStartsWith(__CLASS__, $this->_helper->process('sampleProcessor', uniqid(), []));
47  }
48 
49  public function testProductAttribute()
50  {
51  $this->_testAttribute(
52  'productAttribute',
53  \Magento\Catalog\Model\Product::ENTITY,
54  "&lt;p&gt;line1&lt;/p&gt;<br />\nline2"
55  );
56  }
57 
58  public function testCategoryAttribute()
59  {
60  $this->_testAttribute(
61  'categoryAttribute',
62  \Magento\Catalog\Model\Category::ENTITY,
63  "&lt;p&gt;line1&lt;/p&gt;\nline2"
64  );
65  }
66 
70  public function testIsDirective($html, $expectedResult)
71  {
72  $this->assertEquals($expectedResult, $this->_helper->isDirectivesExists($html));
73  }
74 
75  public function isDirectiveDataProvider()
76  {
77  return [
78  ['{{', false],
79  ['Test string', false],
80  ['{store url="customer/account/login"}', false],
81  ['{{store url="customer/account/login"}}', true],
82  ];
83  }
84 
96  public function sampleProcessor(\Magento\Catalog\Helper\Output $helper, $string, $params)
97  {
98  return __CLASS__ . $string;
99  }
100 
109  protected function _testAttribute($method, $entityCode, $expectedResult)
110  {
111  $attributeName = 'description';
113  \Magento\Eav\Model\Config::class
114  )->getAttribute(
115  $entityCode,
116  $attributeName
117  );
118  $isHtml = $attribute->getIsHtmlAllowedOnFront();
119  $isWysiwyg = $attribute->getIsWysiwygEnabled();
120  $attribute->setIsHtmlAllowedOnFront(0)->setIsWysiwygEnabled(0);
121 
122  try {
123  $this->assertEquals(
124  $expectedResult,
125  $this->_helper->{$method}(uniqid(), "<p>line1</p>\nline2", $attributeName)
126  );
127 
128  $attribute->setIsHtmlAllowedOnFront($isHtml)->setIsWysiwygEnabled($isWysiwyg);
129  } catch (\Exception $e) {
130  $attribute->setIsHtmlAllowedOnFront($isHtml)->setIsWysiwygEnabled($isWysiwyg);
131  throw $e;
132  }
133  }
134 }
$helper
Definition: iframe.phtml:13
sampleProcessor(\Magento\Catalog\Helper\Output $helper, $string, $params)
Definition: OutputTest.php:96
testIsDirective($html, $expectedResult)
Definition: OutputTest.php:70
_testAttribute($method, $entityCode, $expectedResult)
Definition: OutputTest.php:109
$method
Definition: info.phtml:13
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18