DataTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Search\Test\Unit\Helper;
  7. /**
  8. * Unit test for \Magento\Search\Helper\Data
  9. */
  10. class DataTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \Magento\Search\Helper\Data|\PHPUnit_Framework_MockObject_MockObject
  14. */
  15. protected $model;
  16. /**
  17. * @var \Magento\Framework\App\Helper\Context|\PHPUnit_Framework_MockObject_MockObject
  18. */
  19. protected $contextMock;
  20. /**
  21. * @var \Magento\Framework\Stdlib\StringUtils|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. protected $stringMock;
  24. /** @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject */
  25. protected $requestMock;
  26. /**
  27. * @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $scopeConfigMock;
  30. /**
  31. * @var \Magento\Framework\Escaper|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $escaperMock;
  34. /**
  35. * @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  36. */
  37. protected $storeManagerMock;
  38. /**
  39. * @var \Magento\Framework\UrlInterface|\PHPUnit_Framework_MockObject_MockObject
  40. */
  41. private $urlBuilderMock;
  42. protected function setUp()
  43. {
  44. $this->stringMock = $this->createMock(\Magento\Framework\Stdlib\StringUtils::class);
  45. $this->scopeConfigMock = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
  46. $this->escaperMock = $this->createMock(\Magento\Framework\Escaper::class);
  47. $this->storeManagerMock = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
  48. $this->requestMock = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class)
  49. ->disableOriginalConstructor()
  50. ->setMethods([])
  51. ->getMock();
  52. $this->urlBuilderMock = $this->getMockBuilder(\Magento\Framework\UrlInterface::class)
  53. ->setMethods(['getUrl'])
  54. ->disableOriginalConstructor()
  55. ->getMockForAbstractClass();
  56. $this->contextMock = $this->createMock(\Magento\Framework\App\Helper\Context::class);
  57. $this->contextMock->expects($this->any())->method('getScopeConfig')->willReturn($this->scopeConfigMock);
  58. $this->contextMock->expects($this->any())->method('getRequest')->willReturn($this->requestMock);
  59. $this->contextMock->expects($this->any())->method('getUrlBuilder')->willReturn($this->urlBuilderMock);
  60. $this->model = new \Magento\Search\Helper\Data(
  61. $this->contextMock,
  62. $this->stringMock,
  63. $this->escaperMock,
  64. $this->storeManagerMock
  65. );
  66. }
  67. public function testGetMinQueryLength()
  68. {
  69. $return = 'some_value';
  70. $this->scopeConfigMock->expects($this->once())
  71. ->method('getValue')
  72. ->with(
  73. \Magento\Search\Model\Query::XML_PATH_MIN_QUERY_LENGTH,
  74. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  75. null
  76. )
  77. ->will($this->returnValue($return));
  78. $this->assertEquals($return, $this->model->getMinQueryLength());
  79. }
  80. public function testGetMaxQueryLength()
  81. {
  82. $return = 'some_value';
  83. $this->scopeConfigMock->expects($this->once())
  84. ->method('getValue')
  85. ->with(
  86. \Magento\Search\Model\Query::XML_PATH_MAX_QUERY_LENGTH,
  87. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  88. null
  89. )
  90. ->will($this->returnValue($return));
  91. $this->assertEquals($return, $this->model->getMaxQueryLength());
  92. }
  93. /**
  94. * @dataProvider queryTextDataProvider
  95. */
  96. public function testGetEscapedQueryText($queryText, $maxQueryLength, $expected)
  97. {
  98. $this->requestMock->expects($this->once())->method('getParam')->willReturn($queryText);
  99. $this->stringMock->expects($this->any())->method('cleanString')->willReturnArgument(0);
  100. $this->scopeConfigMock->expects($this->any())->method('getValue')->willReturn($maxQueryLength);
  101. $this->stringMock
  102. ->expects($this->any())
  103. ->method('strlen')
  104. ->will($this->returnCallback(function ($queryText) {
  105. return strlen($queryText);
  106. }));
  107. $this->stringMock
  108. ->expects($this->any())
  109. ->method('substr')
  110. ->with($queryText, 0, $maxQueryLength)
  111. ->willReturn($expected);
  112. $this->escaperMock->expects($this->any())->method('escapeHtml')->willReturnArgument(0);
  113. $this->assertEquals($expected, $this->model->getEscapedQueryText());
  114. }
  115. /**
  116. * @return array
  117. */
  118. public function queryTextDataProvider()
  119. {
  120. return [
  121. ['', 100, ''],
  122. [null, 100, ''],
  123. [['test'], 100, ''],
  124. ['test', 100, 'test'],
  125. ['testtest', 7, 'testtes'],
  126. ];
  127. }
  128. /**
  129. * Test getSuggestUrl() take into consideration type of request(secure, non-secure).
  130. *
  131. * @dataProvider getSuggestUrlDataProvider
  132. * @param bool $isSecure
  133. * @return void
  134. */
  135. public function testGetSuggestUrl(bool $isSecure)
  136. {
  137. $this->requestMock->expects(self::once())
  138. ->method('isSecure')
  139. ->willReturn($isSecure);
  140. $this->urlBuilderMock->expects(self::once())
  141. ->method('getUrl')
  142. ->with(self::identicalTo('search/ajax/suggest'), self::identicalTo(['_secure' => $isSecure]));
  143. $this->model->getSuggestUrl();
  144. }
  145. /**
  146. * Provide test data for testGetSuggestUrl() test.
  147. *
  148. * @return array
  149. */
  150. public function getSuggestUrlDataProvider()
  151. {
  152. return [
  153. 'non-secure' => [
  154. 'isSecure' => false,
  155. ],
  156. 'secure' => [
  157. 'secure' => true,
  158. ],
  159. ];
  160. }
  161. }