123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Catalog\Helper;
- class OutputTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var \Magento\Catalog\Helper\Output
- */
- protected $_helper;
- protected function setUp()
- {
- $this->_helper = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
- \Magento\Catalog\Helper\Output::class
- );
- }
- /**
- * addHandler()
- * getHandlers()
- */
- public function testAddHandlerGetHandlers()
- {
- // invalid handler
- $this->_helper->addHandler('method', 'handler');
- $this->assertEquals([], $this->_helper->getHandlers('method'));
- // add one handler
- $objectOne = new \StdClass();
- $this->_helper->addHandler('valid', $objectOne);
- $this->assertSame([$objectOne], $this->_helper->getHandlers('valid'));
- // add another one
- $objectTwo = new \StdClass();
- $this->_helper->addHandler('valid', $objectTwo);
- $this->assertSame([$objectOne, $objectTwo], $this->_helper->getHandlers('valid'));
- }
- public function testProcess()
- {
- $this->_helper->addHandler('sampleProcessor', $this);
- $this->assertStringStartsWith(__CLASS__, $this->_helper->process('sampleProcessor', uniqid(), []));
- }
- public function testProductAttribute()
- {
- $this->_testAttribute(
- 'productAttribute',
- \Magento\Catalog\Model\Product::ENTITY,
- "<p>line1</p><br />\nline2"
- );
- }
- public function testCategoryAttribute()
- {
- $this->_testAttribute(
- 'categoryAttribute',
- \Magento\Catalog\Model\Category::ENTITY,
- "<p>line1</p>\nline2"
- );
- }
- /**
- * @dataProvider isDirectiveDataProvider
- */
- public function testIsDirective($html, $expectedResult)
- {
- $this->assertEquals($expectedResult, $this->_helper->isDirectivesExists($html));
- }
- public function isDirectiveDataProvider()
- {
- return [
- ['{{', false],
- ['Test string', false],
- ['{store url="customer/account/login"}', false],
- ['{{store url="customer/account/login"}}', true],
- ];
- }
- /**
- * Helper method for testProcess()
- *
- * @param \Magento\Catalog\Helper\Output $helper
- * @param string $string
- * @param mixed $params
- * @return string
- * @see testProcess()
- *
- * @SuppressWarnings(PHPMD.UnusedFormalParameter)
- */
- public function sampleProcessor(\Magento\Catalog\Helper\Output $helper, $string, $params)
- {
- return __CLASS__ . $string;
- }
- /**
- * Test productAttribute() or categoryAttribute() method
- *
- * @param string $method
- * @param string $entityCode
- * @param string $expectedResult
- * @throws \Exception on assertion failure
- */
- protected function _testAttribute($method, $entityCode, $expectedResult)
- {
- $attributeName = 'description';
- $attribute = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
- \Magento\Eav\Model\Config::class
- )->getAttribute(
- $entityCode,
- $attributeName
- );
- $isHtml = $attribute->getIsHtmlAllowedOnFront();
- $isWysiwyg = $attribute->getIsWysiwygEnabled();
- $attribute->setIsHtmlAllowedOnFront(0)->setIsWysiwygEnabled(0);
- try {
- $this->assertEquals(
- $expectedResult,
- $this->_helper->{$method}(uniqid(), "<p>line1</p>\nline2", $attributeName)
- );
- $attribute->setIsHtmlAllowedOnFront($isHtml)->setIsWysiwygEnabled($isWysiwyg);
- } catch (\Exception $e) {
- $attribute->setIsHtmlAllowedOnFront($isHtml)->setIsWysiwygEnabled($isWysiwyg);
- throw $e;
- }
- }
- }
|